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
Nikita91
Lead II

Use an RTOS and semaphore, signal, etc.

instead of active waiting the RTOS will switch tasks until the resouce is available or the delay expired.

And don't use active waiting like HAL_Delay()!

TDK
Guru

Or use an interrupt to perform the task while letting the main loop do less important tasks. Implementation will depend on your exact needs. Small frequent tasks will be best served by an interrupt.

If you feel a post has answered your question, please click "Accept as Solution".
Andrew Neil
Evangelist III

You can also do it without RTOS or interrupt; eg,

// flag to be set by the long-running process when it completes
bool long_running_process_completed = false; 
 
initiate_long_running_process();
 
:
:
// do other stuff while process runs
:
:
 
if( long_running_process_completed )
{
    // handle completion
}
else
{
    // do some more other stuff while process still runs
}

State Machines (aka Finite State Machines, FSM) give another approach ...

LCE
Principal

One more vote for state machines (coming from FPGAs and almost zero knowledge about OS...) plus interrupts.

Put one in main(), and every function called in main, use flags.

For high priority stuff check according flags in main outside of state machine.

Yes.

This is "Cooperative multitasking".

S.Ma
Principal

To answer the question, C can generate assembly code, so yes. Now without RTOS, if the goal is not keeping the core self warming and plan low power mode in the next quest, use interrupts. More specificallu use a state machine that gets kicked by chosen interrupt signals. For example, EXTI and SPI and DMA interrupt events going to the FSM to perform SPI slave. If need delays, feed the FSM with timer compare interrupts. All the sources should be having same IRQ priority. This way, smarter peripherals can be built under a bare metal or an OS.

Javier1
Principal

(NO RTOS)

For nonblocking delays i always use HAL_GetTick()

its similar to the millis() funtion in arduino

And do something like this for a periodic 500ms task (not as precise as interruptions and sccesptible to delays in your firmware.

uint32_t lastGetTick;
 
 
 
 
.....
 //check if 500ms passed since last time this if() was triggered
if((HAL_GetTick()-lastGetTick)>=500){
 
//do your thing
 
//reset the tick
lastGetTick=HAL_GetTick()
}
 
 
.....

we dont need to firmware by ourselves, lets talk

You have to be careful with this. Have a look to: https://community.st.com/s/question/0D50X00009XkgXi/years-on-continue-to-see-the-same-bad-timeout-code

It is better to do a subtraction for each loop

thanks @Nikita91​  i changed my example

we dont need to firmware by ourselves, lets talk