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

Microseconds timer using RTC

Hi everyone,

I need to set up a timer using the RTC. The default function that converts ms to ticks APP_TIMER_TICKS() accepts only milliseconds. So, I wrote my own conversion function as below

#define APP_TIMER_TICKS_US(US)               \
  ((uint32_t)ROUNDED_DIV(                    \
      (US) * (uint64_t)APP_TIMER_CLOCK_FREQ, \
      1000000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))

and now I can set up a timer in microseconds like this:

#define SAADC_INTERVAL APP_TIMER_TICKS_US(200)

My questions are:

1. Is this approach correct?

2. RTC running on 32768Hz meaning that the resolution is ~30us. Does this mean that I can set up a timer down to 30us? For example

#define SAADC_INTERVAL APP_TIMER_TICKS_US(30)

Thanks in advance

Nick

Parents
  • Hello Nick,

    Sorry for my late reply, yesterday there was a national holiday here in Norway.

    Have you seen the nrfx_timer_us_to_ticks function?
    I suppose you can implement this conversion yourself also, it should not make any difference.

    2. RTC running on 32768Hz meaning that the resolution is ~30us. Does this mean that I can set up a timer down to 30us? For example

    Yes, you may set a timer to trigger/count only 1 tick.
    However, keep in mind that if you have a repeated interrupt happening every 30 us it might cause trouble for the rest of your application. As a side note, I will also emphasize that the SoftDevice(if present) takes priority over this interrupt, so you might not get an event every 30 us.

    Best regards,
    Karl

  • Hi Karl,

    No I didn't know about the  nrfx_timer_us_to_ticks, thanks!!

    Softdevice is presented :) I'm intended to use a timer of 200us. Softdevide uses RTC0 while app_timer RTC1 right? I remember that I've read this information somewhere but I cannot find the link. Where can I find this information as well as the RTC priorities?

    Also can I set priorities between the different timers of RTC1?

    Thanks

    Nick

  • Hello Nick,

    Nikosant03 said:
    No I didn't know about the  nrfx_timer_us_to_ticks, thanks!!

    Great, I am glad it was helpful!

    Nikosant03 said:
    Softdevide uses RTC0 while app_timer RTC1 right?

    Yes, this is correct. 

    Nikosant03 said:
    I remember that I've read this information somewhere but I cannot find the link. Where can I find this information as well as the RTC priorities?

    Perhaps you are thinking of the app_timer library documentation?
    You may read about the different interrupt priority levels here

    Nikosant03 said:
    Also can I set priorities between the different timers of RTC1?

    Not directly, since the all the events and interrupts are generated by the RTC1 timer. Thus, all RTC1 timers will have the same priority as RTC1 itself.
    However, you may achieve this by implementing your own scheduler for the lower priority interrupts.
    Please see this answer by my colleague in this ticket.

    After having taken a look at the source code for the RTC1, I see now that I was mistaken earlier when I told you that you may have it trigger an interrupt every tick - the minimum amount of RCT1 ticks it can be configured to is 5.
    This is the documentation where it is stated.
    This does not make any difference for your 200 us timer.
    I am terribly sorry for the inaccuracy of my previous reply, and I hope it does not cause you any trouble!

    Best regards,
    Karl

  • the minimum amount of RCT1 ticks it can be configured to is 5.

    I see.. so, the minimum time you can set is around 153us. Fair enough :)

    I have one last question. Looking the sdk_config.f file I can see that by default the priority level for the app timer is at level 6

    // <o> APP_TIMER_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    
    // <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef APP_TIMER_CONFIG_IRQ_PRIORITY
    #define APP_TIMER_CONFIG_IRQ_PRIORITY 6
    #endif

    I am wondering if it is better to set it at lower priority such as level 2 (I am using nRF52840) or leave as it is... What is your opinion?

    Thanks in advance

    Nick

  • Hello again Nick,

    Nikosant03 said:
    I am wondering if it is better to set it at lower priority such as level 2 (I am using nRF52840) or leave as it is... What is your opinion?

    This depends on what the timer is connected to - how critical is it that it happens on-time, and how long does the connected event take to handle?

    An exempt from the priority level documentation I linked reads:

            As seen from Exception model, the application has available priority level 2 and 3, located between the higher and lower priority levels reserved by the SoftDevice. This enables a low-latency application interrupt to support fast sensor interfaces. An application interrupt at priority level 2 or 3 will only experience latency from SoftDevice interrupts at priority levels 0 and 1, while application interrupts at priority levels 5, 6, or 7 can experience latency from all SoftDevice priority levels.
     
    For most use cases, the lower levels 5, 6, and 7 is acceptable, and gives leeway to prioritize certain application tasks before others. IN other words; if there is no explicit need to change the priority to ensure that the interrupt is prioritized over all other application activity, then you should be fine leaving it at the default value - or at most changing it up or down a single level.

    If you are interfacing with a high-frequency sensor or interface, then the issue might also be alleviated by making use of the PPI and easyDMA features.
    However, it is hard for me to say anything more certain regarding interrupt priorities without knowing more of the context.

    Please do not hesitate to ask if you should encounter any other questions or issues! :) 

    Best regards,
    Karl

Reply
  • Hello again Nick,

    Nikosant03 said:
    I am wondering if it is better to set it at lower priority such as level 2 (I am using nRF52840) or leave as it is... What is your opinion?

    This depends on what the timer is connected to - how critical is it that it happens on-time, and how long does the connected event take to handle?

    An exempt from the priority level documentation I linked reads:

            As seen from Exception model, the application has available priority level 2 and 3, located between the higher and lower priority levels reserved by the SoftDevice. This enables a low-latency application interrupt to support fast sensor interfaces. An application interrupt at priority level 2 or 3 will only experience latency from SoftDevice interrupts at priority levels 0 and 1, while application interrupts at priority levels 5, 6, or 7 can experience latency from all SoftDevice priority levels.
     
    For most use cases, the lower levels 5, 6, and 7 is acceptable, and gives leeway to prioritize certain application tasks before others. IN other words; if there is no explicit need to change the priority to ensure that the interrupt is prioritized over all other application activity, then you should be fine leaving it at the default value - or at most changing it up or down a single level.

    If you are interfacing with a high-frequency sensor or interface, then the issue might also be alleviated by making use of the PPI and easyDMA features.
    However, it is hard for me to say anything more certain regarding interrupt priorities without knowing more of the context.

    Please do not hesitate to ask if you should encounter any other questions or issues! :) 

    Best regards,
    Karl

Children
Related