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

Can app_timer_cnt_get be interrupted by SD?

As the title says, can the call app_timer_cnt_get() be interrupted by the SoftDevice and return an outdated value?

  • Anything which takes longer than one instruction can get interrupted by anything with a higher interrupt priority, so not only could app_timer_cnt_get() be interrupted after it reads the count and before it returns it, the code it returns to could get interrupted before it processes the value (which is much more likely as when optimised reading the counter is one single instruction).

    You should basically assume that between any two instructions anywhere in your code there could be an indeterminate gap caused by an interrupt, softdevice or other.

  • I see. But this method is still the most exact way to measure elapsed time, correct? Perhaps using the one-line expression instead of the function call could reduce the delay. This would be used in an application with a configured connection interval of around 10ms, so plenty of SD interrupts I believe...

  • You haven't really said what your problem is. In the end the code reads a register and then processes the data, it might read the register and then get interrupted before it processes, it might read it and process immediately, then get interrupted. It doesn't matter whether you use an inline expression (which is what optimised code does anyway), the time you read in one line of code may not be the time the next line of code is executed. If you rely on it being so, you are going to get broken.

    What is your actual issue?

  • I would like to measure how long a certain function takes to run.

  • well that really has nothing to do with your original question. It doesn't matter how you read the counter, that's not the issue, it's the fact your function itself gets interrupted so the counter keeps counting whilst the softdevice or other interrupt is running the function.

    You can't do it like that. You need to use a proper profiler which can track where your code is and allow for interrupted times or ... something else.

Related