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®-R processor typically uses a operating system (OS) for scheduling application code. While the example presented here shows how to implement an operating system scheduler, the same conceptual steps also apply to a baremetal scheduler.

  1. Use the reference OperatingSystem object, os, to your Target object, tgt, by calling getOperatingSystem with the 'reference'.

    os = getOperatingSystem(tgt,'reference');

    Do not delete the OperatingSystem object, os, from the MATLAB® workspace before you save the target.

  2. View the operating system name. The target being created will use the operating system provided with the reference target ARM Cortex-R.

    os{1}.Name
    ans =
    
        'FreeRTOS'

    Note

    The OperatingSystem object must be accessed as a cell-array.

  3. Confirm that the operating system 'FreeRTOS' is added to your target.

    show(tgt);
                          TIRM46Lx2 Launchpad      
    Display Name          TIRM46Lx2 Launchpad      
    RM46 TI Deployer               1               
    FreeRTOS                       0   

    The output shows that operating system 'FreeRTOS' is added to the target. However, note that the scheduler is not used for the hardware 'TIRM46Lx2 Launchpad', and this fact is denoted by showing 0 in the corresponding position for the hardware.

  4. Map the OperatingSystem object, os, to the Hardware object, hw.

    map(tgt,hw,os);
  5. Confirm that the boperating system 'FreeRTOS' is used for the hardware 'TIRM46Lx2 Launchpad'

    show(tgt);
                          TIRM46Lx2 Launchpad      
    Display Name          TIRM46Lx2 Launchpad      
    RM46 TI Deployer               1               
    FreeRTOS                       1     

    The output shows that the operating system 'FreeRTOS' is used for the hardware 'TIRM46Lx2 Launchpad', and this fact is denoted by showing 1 in the corresponding position for the hardware.

  6. Add the token to the Deployer to point to the FreeRTOS components.

    token.Name = 'FREE_RTOS_SHRD_COMP_DIR';
    token.Value = 'matlabshared.freertos.internal.getShrdCompRootDir';
    deployerObj.Tokens{end+1} = token;
  7. Add the compiler include paths to the BuildConfiguration by setting the IncludePaths property to '$(TARGET_ROOT)/include'.

    buildConfigurationObj.IncludePaths{end+1} = '$(TARGET_ROOT)/include';

    If the source, header, and library files are not located within the $(TARGET_ROOT)/include folder, assign a cell array of all required paths to IncludePaths.

    Note

    $(TARGET_ROOT) is a token that stands for the root folder of the target for ARM Cortex-R hardware board and will be resolved by the code generation software. This token was automatically assigned in the Create the Framework task.

  8. Define a MATLAB function to invoke after the code for the Simulink® model is generated.

    deployerObj.AfterCodeGenFcn = 'matlabshared.target.tirm46.onAfterCodeGenHook_FreeRTOS';

    The 'matlabshared.target.tirm46.onAfterCodeGenHook_freeRTOS' function now specifies FreeRTOS as the operating system of your target and uses the predefined HALCoGen project files and generates specific source files for the TIRM46L852 device configuration. The previous freeRunning configuration defined in the Activate the Application Deployment Feature is overwritten.

  9. Save the information that describes a target to its framework.

    saveTarget(tgt);
  10. Copy the FreeRTOS specific header files, HALCoGen project files, FreeRTOS specific onAfterCodeGenHook_FreeRTOS.m files and other files to the TARGET_ROOT/src folder.

    copyfile(fullfile(codertarget.arm_cortex_r.internal.getSpPkgRootDir(),...
         'arm_cortex_r_examples', 'scheduler'),...
         fullfile(tgt.Folder),'f');
  11. 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 the Hardware Implementation tab.

  4. Set Hardware board to the hardware you registered, for example, TIRM46Lx2 Launchpad.

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

  6. In the All Parameters tab, find and disable Block reduction.

  7. Find and enable MAT-file logging.

  8. In MATLAB, on the Home tab, select Simulink Library.

  9. 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.

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

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

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

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

  14. 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.