hello
I have a question when i use the stateflow in the MPC5744P_MBD.
I want to use stateflow to do some pin voltage-level changes for some communication,
But when i do a simple test, every index++ in simulink-stateflow is inconsistent with index++ in MCU(200MHz index++ instruction will take ~50ns).
For example,.
When this program is downloaded to the MCU by openSDA, "Sample" pin was measured by oscilloscope with a period of 1ms. Why not 50ns*40=2us?
Can you help me explain it?
thank you very much.
best regards
hao
解決済! 解決策の投稿を見る。
Hello haoxue1027@qq.com,
The Stateflow that you are mentioning in the question is located in the main model. A function call is triggering the Speed_Measure subsystem that contains the Stateflow code. So you are expecting like the code to be executed every infinite loop iteration that is running up at 200MHz, right?
Well, the Simulink generates the code in a different way than you are expecting. Let me explain. Simulink is a time-driven environment, which means that the code from the main model (obviously with the subsystem called from there) will be executed at the specific time interval. For now, the model time execution is computed automatically by the Simulink. To specify the time interval, you have to go to the Model Settings, Solver, and there set the interval in seconds.
For the embedded target, Simulink generates three main functions (you can see this in the generated code):
- Initialize function - this function is called only once and here the clocks and all the required peripherals are initialized
- Step function - basically this is the function where all the code goes (obviously, without the interrupts routines which have to be treated asynchronously). This Step function is triggered at the period mentioned in the Model Settings (specified or automatically set) by a board timer ( in our case is the PIT 0).
- Terminate function - not used.
So the model will not generate any code in the main loop function.
If you need to trigger s-functions (like the one mentioned in your question) at different timebase than the one for the step function, you can use other PIT timers to generates interrupts faster or not for the code required.
Obviously, you can also change the entire Model Sample Time as required.
I hope this helps you to understand why the model is not running as expected.
Hope this helps,
Marius
Hello haoxue1027@qq.com,
The Stateflow that you are mentioning in the question is located in the main model. A function call is triggering the Speed_Measure subsystem that contains the Stateflow code. So you are expecting like the code to be executed every infinite loop iteration that is running up at 200MHz, right?
Well, the Simulink generates the code in a different way than you are expecting. Let me explain. Simulink is a time-driven environment, which means that the code from the main model (obviously with the subsystem called from there) will be executed at the specific time interval. For now, the model time execution is computed automatically by the Simulink. To specify the time interval, you have to go to the Model Settings, Solver, and there set the interval in seconds.
For the embedded target, Simulink generates three main functions (you can see this in the generated code):
- Initialize function - this function is called only once and here the clocks and all the required peripherals are initialized
- Step function - basically this is the function where all the code goes (obviously, without the interrupts routines which have to be treated asynchronously). This Step function is triggered at the period mentioned in the Model Settings (specified or automatically set) by a board timer ( in our case is the PIT 0).
- Terminate function - not used.
So the model will not generate any code in the main loop function.
If you need to trigger s-functions (like the one mentioned in your question) at different timebase than the one for the step function, you can use other PIT timers to generates interrupts faster or not for the code required.
Obviously, you can also change the entire Model Sample Time as required.
I hope this helps you to understand why the model is not running as expected.
Hope this helps,
Marius
hello mariuslucianandrei
Thank you very much for your explaining.
Your answer was very helpful to me.
now I also have a little of confuse about the sample time of slover and sample time of the function call.
Are the two sample times generally set to the same value?
I did a test for the sample time where the condition was set to "[index>1]{index=0;Sample=1;}"
when solver sample time was set to 1e-6 and function call sample time was set to 1e-4 ,the T was 1400us;
when solver sample time was set to 1e-6 and function call sample time was set to 1e-6 ,the T was 24us;
when solver sample time was set to 1e-4 and function call sample time was set to 1e-4 ,the T was 600us;
Could you take a moment to explain the difference between the two sampling times?
thank you very much
best regards
hao
Hello haoxue1027@qq.com,
I assume that the Simulink will try to trigger the subsystem in a software approach the desired subsystem. If that is the only subsystem you need, please remove the function call and set the entire step size at the required time interval. If you need to be triggered at a specific time interval, then you have to use an LPIT interrupt 1 or 2. This will configure a hardware timer on the board and will trigger the required subsystem with the desired frequency.
Hope this helps,
Marius
hello mariuslucianandrei
Thank you very much for your explaining.
I'll debug with interrupt later.
But what's the difference between the two sampling times?
I also have a little of confuse about it .
best regards
hao
Could someone give me a hand?