(How-to) Multitasking on S32K144 MBDT Simulink

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

(How-to) Multitasking on S32K144 MBDT Simulink

6,620 Views
gramirezv
Contributor III

Simulink has a mechanism to generate tasks at different rates within your model, this example shows a way to create three tasks running at 1ms, 10ms and 100ms each, this may be helpful to someone looking to implement this type of systems without having to reinvent the wheel.

 

Mathworks has some articles with more detail on Modeling for Multitasking Execution, you can visit their website for more details. This example is intended to be a simple tutorial on how to create a simple multitasking model and toggle three digital outputs at different rates as defined in each task.

 

This example uses

 

Starting from a blank model then you first must define the step size, go to Model Configuration Parameters on the menu as shown below, or Ctrl + E

 pastedImage_1.png

 

Make sure “Solver” is selected on the left menu, and set the settings as shown below:

  • Solver Selection
    • Type: Fixed-step
    • Solver: discrete (no continuous states)
  • Fixed-step size (fundamental sample time): 0.001 (1ms)
  • Tasking and sample time options
    • Periodic sample time constraint: Unconstrained
    • Enable the "Treat each discrete rate as a separate task" checkbox

pastedImage_2.png

You can change the settings on the solver to suit your specific project, as always Mathworks help is a great resource.

Next I will create three subsystems and name each one with the rate at which they will be running.

Then, right click on each Subsystem block and select the "Block Parameters (Subsystem)", on the window change the following:

  • On the "Main" tab:
    • Select the "Treat as atomic unit" checkbox
    • Set the appropriate Sample time for each Subsystem
      • 0.001 for 1 ms
      • 0.010 for 10 ms
      • 0.100 for 100 ms

pastedImage_4.png

  • On the "Code Generation" tab
    • Select Function packaging as "Reusable Function" and leave all the parameters as default

pastedImage_6.png

At the end you'll have your three atomic subsystems with the sampling rate defined for each task.

pastedImage_8.png

 Inside each task subsystem, place a GPO Write to toggle an output. I used the Color LED so I could have the LED changing accordingly.

For the 1ms task (Red LED):

pastedImage_3.png

For the 10ms task (Blue LED):

pastedImage_4.png

For the 100ms task (Green LED):

pastedImage_6.png

Once the system is built, Simulink uses a color coded scheme to differentiate the blocks running at a different rate as you can observe below:

pastedImage_1.png

 If you take a look under the hood into the generated code you can see that Simulink generates some functions multitasking_step0, multitasking_step1 & multitasking_step2, each function is called according the Rate Monotonic Scheduler implemented by the Simulink coder.

Below is the oscilloscope capture from the PTD15, PTD0 & PTD16 outputs:

pastedImage_1.png

I set up some measurement boxes to measure the positive pulse duration on each signal, as you can see the pin is toggling at the expected rates of 1ms, 10ms and 100ms respectively.

Hopefully this example is helpful to someone in this forum.

Regards,

Labels (2)
5 Replies

4,319 Views
rathi_tg
Contributor III

Hello @gramirezv @mariuslucianand 

The above Explanation for the topic is very clear and too useful.

I have done few trails using the same method and have observed various result would like to share you my observation and understand the reason in broader level. I am using S32k144.

Here is the description of test and there observations. I have even attached file containing all trails and their results. 

S.No

Subsytem_1

Sample Time

Subsytem_2

Sample Time

DescriptionImage Name 

Observation on Scope

 PTD15PTD0  PTD15PTD0
10.0010.002Data Stores(A B C) Placed outside subsystem 2.Data been Processed in MATLAB function but some Constant are  transmitted on CAN MultiTasking_01 +ve  Width shows two values 0.002 and 0.003
 -ve width is 0.001
 +ve and  -ve width both has 0.004
20.0010.002Data Stores(A B C) Placed in subsystem 2.Data been Processed in MATLAB function and then transmitted on CAN MultiTasking_02 +ve  Width shows two values 0.002 and 0.003
 -ve width is 0.001
 +ve and  -ve width both has 0.004
30.0010.002Data Stores(A B C) Placed in subsystem 2.Data been Processed in MATLAB function but some Constant are  transmitted on CAN MultiTasking_03 +ve and  -ve width both has 0.001 +ve and  -ve width both has 0.002

 

I am unable to figure the difference in LED behavior with change in position of Data Stores.(Observed  in CASE 1 and CASE 3).

Similarly, Change in LED when data store and Constant is transmitted Via CAN (Observed  in CASE 2 and CASE 3).

Hope for you Help!!

Regards 

Gopal Rathi

   

0 Kudos

4,550 Views
bcelikte
Contributor III

hi @gramirezv and @constantinrazva 

I have a complex model (BMS model) for S32K144EVB.

When I try 1s sample time, everything is calculating correctly, but when I changed sample time 50ms some blocks of model (stateflow) is lagging for couple of timesteps (basically showing same values for couple of of steps which are increased 50ms when I look at freemaster). My model solver is discrete and has one sample time. So at this point should I use multitasking mode and change some blocks sample time? By the way, blocks are using inputs/outputs each other. Is using different sample times a problem for data exchange for that blocks? 

Or maybe it is not possible to run all model at that sample time, how should I know the sample time that I can run without any problem on thi MCU?

Thanks

0 Kudos

4,521 Views
gramirezv
Contributor III

Hello @bcelikte,

From your description it looks like you would need to profile your software. You need to verify what parts of your model are taking more time.

Without having more details on your model. Your problem could be from different sources, but here are some suggestions you could take a look at:

One is the refresh rate from freemaster, I haven't played in deep with it's settings but what you're seeing could be freemaster not updating fast enough, hence you're seeing the same data for more than one timestamp.

Also could be that when you have your time step to 1s your code has enough time to complete, and when you crank it down it takes more than 1 step to complete. The best thing to do is to profile your software and see blocks take more time to execute. I haven't worked a lot with the profiling blocks on the S32K144 but i'm sure you can find some examples in the toolbox.

Obviously all depends on your application and the way you architected your model, you need to analyze and verify what blocks can work at different rates. The key is that it's a wild broad topic to be summarized without specific details.

Regards,

 

0 Kudos

4,769 Views
constantinrazva
NXP Employee
NXP Employee

Thank you gramirezv‌ for this insightful article ! It's a great addition for our featured content. Very detailed explanation with a hands-on example as well (+oscilloscope screenshot bonus!).

Thank you for having the initiative to write such an article and share knowledge with all the members of our community! Hopefully you've also inspired others to share their insights, too.

Best regards,

Razvan. 

4,769 Views
gramirezv
Contributor III

Hi constantinrazvan.chivu‌,

 I'm glad you find this helpful for the community, I always believe that if I had troubles figuring out something others may have faced something similar.

Regards!