cancel
Showing results for 
Search instead for 
Did you mean: 

Non blocking or Event base programming for MCU in C is possible?

parth kothiya
Senior

for MCU time is very important to do task.

in programming instead of blocking time of MCU we can do other important task how can i do in c programming language?

ex: HAL_Delay(); block code execution vest of MCU time.

while() waiting for sensor IP etc....

11 REPLIES 11
gbm
Lead III

Of course it's possible. Cortex-M interrupt controller makes it possible to generate hardware interrupts in software, so the whole firmware may be implemented without the famous main loop, as a collection of event handlers with some "small" events generating "big" events, all of them being scheduled by NVIC with few different priority levels. I did several projects with few thousand lines of C source in this way. The binary code size reaches 120 KiB for the biggest one. No RTOS, no main loop and not a single delay(), just interrupt handlers.

You may even have a "blocking" lowest-priority interrupt handler if it's too hard to make it non-blocking for some nasty peripheral.

QuantumLeaps website may be a good starting point for more info on a similar approach (although without hardware scheduling)

https://www.state-machine.com/

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

And this is the way to go for energy saving.

Between interrupts handling the MCU can go to sleep (WFI instruction).