Activate the Scheduling Feature

The generated code includes scheduling code that executes application code based on sample times specified in the model. An ARM® Cortex®-M processor typically uses a baremetal scheduler for scheduling application code. While the example presented here shows how to implement a baremetal scheduler, the same conceptual steps also apply to an operating system scheduler.

  1. Create and add a new OperatingSystem object, os, to your Target object, tgt, by calling addNewBaremetalScheduler with the name of the scheduler, for example, 'My Baremetal Scheduler'.

    scheduler = addNewBaremetalScheduler(tgt,'My Baremetal Scheduler');

    Do not delete the Baremetal Scheduler object, scheduler, from the MATLAB® workspace before you save the target.

    Note

    The scheduler provided with base target for ARM Cortex-M target only can be used with the GNU toolchain.

  2. Confirm that the operating system 'My Baremetal Scheduler' is added to your target.

    show(tgt);
                               My ARM Cortex M Board
    Display Name               My ARM Cortex M Board
    My New Deployer                     1
    My Baremetal Scheduler              0
    

    The output shows that baremetal scheduler 'My Baremetal Scheduler' is added to the target. However, note that the scheduler is not used for the hardware 'My ARM Cortex M Board', and this fact is denoted by showing 0 in the corresponding position for the hardware.

  3. Map the BaremetalScheduler object, scheduler, to the Hardware object, hw.

    map(tgt,hw,scheduler);
  4. Confirm that the baremetal scheduler 'My Baremetal Scheduler' is used for the hardware 'My ARM Cortex M Board'

    show(tgt);
                                 My ARM Cortex M Board
    Display Name                 My ARM Cortex M Board
    My New Deployer                       1
    My Baremetal Scheduler                1
    

    The output shows that the baremetal scheduler 'My Baremetal Scheduler' is used for the hardware 'My ARM Cortex MBoard', and this fact is denoted by showing 1 in the corresponding position for the hardware.

  5. Create and add a new BaseRateTrigger object, baseRateTrigger, to the BaremetalScheduler object, scheduler, by calling addNewBaremetalSchedular with the name of the scheduler, for example, 'My BaremetalScheduler'.

    baseRateTrigger = addNewBaseRateTrigger(scheduler,'My Base Rate Trigger');

    Do not delete the BaseRateTrigger object, baseRateTrigger, from the MATLAB workspace before you save the target.

  6. Set the properties of the BaseRateTrigger object, baseRateTrigger, as needed for your hardware. For example, you may set the source code function that configures the base rate trigger by setting the ConfigurationFcn property.

    baseRateTrigger.ConfigurationFcn = 'myBaseRateTrigger_ConfigFcn(modelBaseRate)';

    The configuration function typically sets up a hardware interrupt, such as a timer, at the rate that corresponds to the base rate of the model. For that purpose, the function takes the model base rate as a parameter modelBaseRate. The generated code will call the configuration function from its main function.

  7. Similarly, set the BaseRateTrigger object properties EnableInterruptFcn and DisableInterruptFcn to register the functions that enable and disable the base rate trigger interrupt.

    baseRateTrigger.EnableInterruptFcn = 'myBaseRateTriggerInterruptEnable()';
    baseRateTrigger.DisableInterruptFcn = 'myBaseRateTriggerInterruptDisable()';
    
  8. Save the information that describes a target to its framework.

    saveTarget(tgt);
  9. Test that the scheduler works correctly.

    testTarget(tgt,'scheduler')

    Upon completion of the test, a summary result is displayed. If the test PASSED, then you can proceed with adding the next feature. Otherwise, if the test FAILED, a link to the test diagnostic logs is shown below the test summary.

    Note

    Until an External Mode feature is added to the target, the scheduler cannot be fully tested and will return Incomplete in the test.

Confirm the Model Builds Successfully

  1. In MATLAB, on the Home tab, select New > Simulink Model. The default name of the model is untitled. Click File > Save As and save your model as test.

  2. In the model, select Simulation > Model Configuration Parameters.

  3. In the Configuration Parameters dialog box, select Solver.

  4. From the Type list, select Fixed-step. From the Solver list, select auto.

  5. In the Configuration Parameters dialog box, select the Hardware Implementation tab.

  6. Set Hardware board to the hardware you registered, for example, 'My ARM Cortex M Board'.

  7. On the Solver tab, set Tasking mode for periodic sample times to Auto.

  8. On the Optimization tab, clear Block reduction.

  9. On the Code Generation > Interface tab, select MAT-file logging. Click OK.

  10. In MATLAB, on the Home tab, select Simulink® Library.

  11. In the Simulink library, open Sources and add a Constant block to your model. Double-click the Constant block and set Sample time to 1.

  12. Add another Constant block to the model. Double-click the Constant1 block and set Sample time to 2 and Constant value to 2.

  13. In the Simulink library, open Sinks and add a To Workspace block to your model. Click the block and set Save format to Array.

  14. Copy and paste this To Workspace block to the model.

  15. Connect the Constant block to the To Workspace, and Constant1 and To Workspace1 .

  16. Build your model. After the build completes, the code will run on your hardware. You need to verify whether the code actually runs. The technique you use for verification will be specific to your hardware.