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 bare-metal scheduler for scheduling application code. While the example presented here shows how to implement a bare-metal scheduler, the same conceptual steps also apply to an operating system scheduler.
Create and add a new
OperatingSystemobject,os, to yourTargetobject,tgt, by callingaddNewBaremetalSchedulerwith the name of the scheduler, for example,'My Baremetal Scheduler'.scheduler = addNewBaremetalScheduler(tgt,'My Baremetal Scheduler');Do not delete the
Baremetal Schedulerobject,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.
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 bare-metal 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 showing0in the corresponding position for the hardware.Map the
BaremetalSchedulerobject,scheduler, to theHardwareobject,hw.map(tgt,hw,scheduler);
Confirm that the bare-metal 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 bare-metal scheduler
'My Baremetal Scheduler'is used for the hardware'My ARM Cortex MBoard', and this fact is denoted by showing1in the corresponding position for the hardware.Create and add a new
BaseRateTriggerobject,baseRateTrigger, to theBaremetalSchedulerobject,scheduler, by callingaddNewBaremetalSchedularwith the name of the scheduler, for example,'My BaremetalScheduler'.baseRateTrigger = addNewBaseRateTrigger(scheduler,'My Base Rate Trigger');Do not delete the
BaseRateTriggerobject,baseRateTrigger, from the MATLAB workspace before you save the target.Set the properties of the
BaseRateTriggerobject,baseRateTrigger, as needed for your hardware. For example, you may set the source code function that configures the base rate trigger by setting theConfigurationFcnproperty.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 itsmainfunction.Similarly, set the
BaseRateTriggerobject propertiesEnableInterruptFcnandDisableInterruptFcnto register the functions that enable and disable the base rate trigger interrupt.baseRateTrigger.EnableInterruptFcn = 'myBaseRateTriggerInterruptEnable()'; baseRateTrigger.DisableInterruptFcn = 'myBaseRateTriggerInterruptDisable()';
Save the information that describes a target to its framework.
saveTarget(tgt);
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 testFAILED, 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
Incompletein the test.
Confirm the Model Builds Successfully
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 astest.In the model, select Simulation > Model Configuration Parameters.
In the Configuration Parameters dialog box, select
Solver.From the Type list, select
Fixed-step. From the Solver list, selectauto.In the Configuration Parameters dialog box, select the
Hardware Implementationtab.Set Hardware board to the hardware you registered, for example,
'My ARM Cortex M Board'.On the
Solvertab, set Tasking mode for periodic sample times to Auto.On the
Optimizationtab, clear Block reduction.On the Code Generation > Interface tab, select MAT-file logging. Click OK.
In MATLAB, on the Home tab, select Simulink® Library.
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.Add another Constant block to the model. Double-click the
Constant1block and set Sample time to2and Constant value to2.In the Simulink library, open Sinks and add a To Workspace block to your model. Click the block and set Save format to
Array.Copy and paste this To Workspace block to the model.
Connect the Constant block to the To Workspace, and Constant1 and To Workspace1.
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.