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
OperatingSystem
object,os
, to yourTarget
object,tgt
, by callingaddNewBaremetalScheduler
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.
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 showing0
in the corresponding position for the hardware.Map the
BaremetalScheduler
object,scheduler
, to theHardware
object,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 showing1
in the corresponding position for the hardware.Create and add a new
BaseRateTrigger
object,baseRateTrigger
, to theBaremetalScheduler
object,scheduler
, by callingaddNewBaremetalSchedular
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.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 theConfigurationFcn
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 itsmain
function.Similarly, set the
BaseRateTrigger
object propertiesEnableInterruptFcn
andDisableInterruptFcn
to 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
Incomplete
in 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 Implementation
tab.Set Hardware board to the hardware you registered, for example,
'My ARM Cortex M Board'
.On the
Solver
tab, set Tasking mode for periodic sample times to Auto.On the
Optimization
tab, 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
Constant1
block and set Sample time to2
and 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.