nrf52 + NRF SDK 17.1.0 - looking for semaphore to signal from interrupt to main loop

Hi !

I'm using nrf52 + SDK 17.1.0. 

I found sd_mutex_acquire/sd_mutex_release, which is great for locking variables that are set/get asynchronously. 
Next, I'd like to use something like a semaphore, so I can indicate from my interrupt to the main loop that an event has occurred. 

The closest thing I've found is nrf_atomic, but I'm not clear on how to use this like a semaphore. 
Question - What are the *_fetch() APIs?  eg - nrf_atomic_flag_clear() vs nrf_atomic_flag_clear_fetch()

Is it as simple as the following? 

nrf_atomic_flag_t event1_flag = 0;

void interrupt() {
  nrf_atomic_flag_set(&event1_flag);

void main() {
  while(1) {
    if (event1_flag) {
      nrf_atomic_flag_clear(&event1_flag);
      //execute semaphore triggered code ? 
    }
  }
}

Parents
  • I suggest that you use SWI or EGU (event generator unit) instead and then run your desired event in an interrupt handler at your desired interrupt priority level (typically lower than your peripheral interrupt level). That's how the rest of the sdk is designed to work. The main loop should be as short as possible and go to WFI or WFE as quickly as possible to not consume too much energy.

  • Thanks Emil.. 

    So instead of signalling the main loop of an event, SWI/EGU would be running the event triggered code in a lower priority callback?

    What if you wanted things to be checked in the main loop and run in a specific order? IE - service a timer before servicing a UART. I don't usually design things like this, relying on priorities to dictate processing order, rather than explicit main loop handling. 

    Am I SOL on my preferred design pattern?

Reply
  • Thanks Emil.. 

    So instead of signalling the main loop of an event, SWI/EGU would be running the event triggered code in a lower priority callback?

    What if you wanted things to be checked in the main loop and run in a specific order? IE - service a timer before servicing a UART. I don't usually design things like this, relying on priorities to dictate processing order, rather than explicit main loop handling. 

    Am I SOL on my preferred design pattern?

Children
Related