This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Detecting interrupts in a block of code

I'm working with the nRF51822, using the S110 soft device.

My application includes some measurement functions that are timing critical. Ideally, the code would be atomic, but I think this is impractical while using the soft device.

Instead, I would like to be able to detect if a measurement is interrupted and, if so, discard the measurement and repeat it. Is there any way to do this?

The measurement cycles vary in time, but will generally be < 100 us.

Thanks, Philip

  • I think <100us is fast enough to have it runs within the Interrupt routine and set your Interrupt has High priority. Then your measurement wouldn't be interrupted at all.

  • Yes it will - the softdevice gets the highest priority interrupts and if you try this, however fast the routine is, it will occasionally get interrupted.

  • You can't detect directly if your code has been interrupted, but you can use ways to try and figure it out after the fact.

    If you use say TIMER1 or TIMER2, CLEAR, clear the COMPARE event and start it counting when you start your routine, then read it when you're done, you'll know how many timer ticks have elapsed. If you also know basically how long it should have taken for your code to run and the timer has counted off more, you've probably been interrupted. Check COMPARE too incase the counter completely wrapped around. Those timers count fast enough to make this a possible as long as your code has a stable and knowable running time.

    As the previous answer suggested, you definitely want to run in the highest priority interrupt mode you can so that only the softdevice critical interrupts can interrupt you, so use the app high priority interrupt level.

    Finally, you could try using the radio notifications API to move your measurement outside the window the radio is in use, that's not directly related, however the softdevice tends to be using the lower stack (highest priority) interrupts during radio events, if you can move yourself away from those times you may find a window in which, running at Application High interrupt level, you are rarely interrupted.

Related