How to change Main function process from Interrupt based routine to polling based routine

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

How to change Main function process from Interrupt based routine to polling based routine

Jump to solution
1,444 Views
p_a_u_darshana
Contributor III

Hello,

When I tried to run some testing models, I saw that the 'while loop' in the main function is always empty.

Later I found that the code is executed(generated) through 'Interrupt based routine.'

1. What is the reason that the NXP toolbox follows the Interrupt based routine as a default setting for code generation rather than a polling-based routine?

2. If I want to use a polling-based routine, how should I change my settings or model?

   - I used a simple testing model as I attached below to check the code generation process.

p_a_u_darshana_0-1636427339425.png

Thank you

0 Kudos
1 Solution
1,425 Views
mariuslucianand
NXP Employee
NXP Employee

Hello @p_a_u_darshana ,

In order to generate code from a Simulink model, you have to use a fixed-step solver which maintains a constant step size for the model. This means that you "execute" the model at regular time intervals as long as you run the simulation.

When you translate this to a microcontroller the simulation will never stop, so the "Simulation" total time is going to run to infinity (of course, or until a reset appears ) but you still have to execute the model at fixed time intervals. Now, because we run this on a microcontroller, and the step model execution may take longer or less time, depending on inputs or on application state, we chose to execute the model from inside an interrupt, triggered by a timer. The main reason for this is that a timer is a peripheral which counts ticks by its own, with no interaction by the processor's core, and can trigger interrupts at fixed time intervals, regardless of what the MCU is computing at that moment.

Let's assume that the step time is 1 second, and the model takes an average of 0.25 seconds to be executed. So by triggering the step function every second, we can guarantee that the model is resolved at each second. But 0.25 is an average. This means that one time it can take 0.2, other time 0.3 or 0.245. And if you are using FreeMaster as well in polling mode, it becomes very difficult to count the time.

If the step time is 1 second, and the model takes 1.5 seconds to execute then you end up in a totally different scenario which I have explained here https://community.nxp.com/t5/Model-Based-Design-Toolbox-MBDT/S32k144-Simulation-Time/m-p/1193243#M54... 

So this is why everything is triggered by an interrupt: using a fixed-step solver, requires executing the model at fixed time size intervals. 

Unfortunately, you cannot execute the entire algorithm in a while loop in our toolbox, because of the reason above.

But you can do two things here:

1. to profile the model execution time, and to set the fixed step size according to your measurements results, basically to minimize the lost time

2. to generate the code only for the model and to include it inside an S32DS studio project and call the generated code according to the application requirements.

Hope this helps,

Marius

 

 

View solution in original post

1 Reply
1,426 Views
mariuslucianand
NXP Employee
NXP Employee

Hello @p_a_u_darshana ,

In order to generate code from a Simulink model, you have to use a fixed-step solver which maintains a constant step size for the model. This means that you "execute" the model at regular time intervals as long as you run the simulation.

When you translate this to a microcontroller the simulation will never stop, so the "Simulation" total time is going to run to infinity (of course, or until a reset appears ) but you still have to execute the model at fixed time intervals. Now, because we run this on a microcontroller, and the step model execution may take longer or less time, depending on inputs or on application state, we chose to execute the model from inside an interrupt, triggered by a timer. The main reason for this is that a timer is a peripheral which counts ticks by its own, with no interaction by the processor's core, and can trigger interrupts at fixed time intervals, regardless of what the MCU is computing at that moment.

Let's assume that the step time is 1 second, and the model takes an average of 0.25 seconds to be executed. So by triggering the step function every second, we can guarantee that the model is resolved at each second. But 0.25 is an average. This means that one time it can take 0.2, other time 0.3 or 0.245. And if you are using FreeMaster as well in polling mode, it becomes very difficult to count the time.

If the step time is 1 second, and the model takes 1.5 seconds to execute then you end up in a totally different scenario which I have explained here https://community.nxp.com/t5/Model-Based-Design-Toolbox-MBDT/S32k144-Simulation-Time/m-p/1193243#M54... 

So this is why everything is triggered by an interrupt: using a fixed-step solver, requires executing the model at fixed time size intervals. 

Unfortunately, you cannot execute the entire algorithm in a while loop in our toolbox, because of the reason above.

But you can do two things here:

1. to profile the model execution time, and to set the fixed step size according to your measurements results, basically to minimize the lost time

2. to generate the code only for the model and to include it inside an S32DS studio project and call the generated code according to the application requirements.

Hope this helps,

Marius