Skip to main content
Visitor II
February 7, 2022
Question

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

  • February 7, 2022
  • 7 replies
  • 5197 views

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....

    This topic has been closed for replies.

    7 replies

    Explorer II
    February 7, 2022

    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()!

    Super User
    February 7, 2022

    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.

    Super User
    February 7, 2022

    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 ...

    Graduate II
    February 7, 2022

    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.

    Explorer II
    February 7, 2022

    Yes.

    This is "Cooperative multitasking".

    Visitor II
    February 8, 2022

    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.

    Graduate II
    February 8, 2022

    (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()
    }
     
     
    .....

    Explorer II
    February 8, 2022

    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

    Graduate II
    February 8, 2022

    thanks @Nikita91​  i changed my example

    Graduate
    February 9, 2022

    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/

    Explorer II
    February 9, 2022

    And this is the way to go for energy saving.

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