cancel
Showing results for 
Search instead for 
Did you mean: 

Executing the main while loop every 60us

Tsemr.1
Associate II

Dear ST Hello,

I am implementing an infinite state machine on stm32F409 board and I have an issue.

I Have a periodic pwm of 60us triggered. I Want to move all the code to the main while loop from the Interrupt service routine.

My objective is to run each 60us the main while loop and pass through the states (wait, open loop, close loop).

For now the code in the main loop is executed many times in the while loop every 60us.

My question is how to execute the while loop every 1 pwm tick(60)?

I am open to your ideas.

Thank you in advance,

S.Tarik

5 REPLIES 5
Uwe Bonnes
Principal II

Trigger an interup on timer overflow or compare and run the code in the interrupt context or signal from the IRQ context a thread waiting on that signal.

Ozone
Lead

> My objective is to run each 60us the main while loop and pass through the states (wait, open loop, close loop).

This would be a very unfortunate design choice.

As Uwe Bonnes suggested, use interrupts.

You need to relate your state changes to interrupts signals.

In the handler, set an appropriate flag.

In the main loop, execute the state machine transitions with the appropriate actions (like starting/stopping/erconfiguring PWM) upon a flag change.

Trying to achieve a narrow loop cycle time is both very fragile and non-portable.

In this moment I am coding all my code in the interrupt service routine.

you sagest to create a thread on that signal could you give an example please thank you.

Yes the idea is to not use the minimal code in the interrupt service routine.

Could you please write a small pseudo code,

Thank you very much.

A verbal description.

I suppose you know what a state machine is.

In the interrupt handler, you set an event flag - whatever that is in your application context. Like, a timer runs out and a PWM must subsequently be started or stopped.

You run a "while (1)" loop that sequentially checks for event flags, and executes the appropriate action. The flag is then reset (in the main loop).

Avoid treating more than one flag/event in one main cycle, this is prone to race condition bugs.

For more complex designs, this is done with realtime OSs. I think this would be overkill in your case.

But basically, a RTOS-based application would do the same.

It would be your responsibility to set the main cycle time and the interrupt execution time (complexity) so that no critical overflow (unhandled events) occur, and the system reacts fast enough to handle it's task.

Not as fast as possible.

Systems/applications tend to require maintainance and improvements. A controller with almost 100% system load will fall over at seemingly insignificant changes.