<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nrf52 timer example, over 5 minutes event handler.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/23666/nrf52-timer-example-over-5-minutes-event-handler</link><description>When I test the timer example with nrf52832 DK, it so nice to 4 minutes. 
 uint32_t time_ms = 240000;
 
 But when I set the time interval over 5 minutes, then the event handler run more rapidly. 
 So, what should I do for long time period?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 21 Jul 2017 13:36:16 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/23666/nrf52-timer-example-over-5-minutes-event-handler" /><item><title>RE: nrf52 timer example, over 5 minutes event handler.</title><link>https://devzone.nordicsemi.com/thread/93028?ContentTypeID=1</link><pubDate>Fri, 21 Jul 2017 13:36:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2bd6c9bf-0d0a-4afe-866c-cd463ca39e5a</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;It should not affect other projects, as this only changes the multiplication result from 32 to 64 bits. The ASSERT in the &lt;code&gt;nrf_timer_ms_to_ticks()&lt;/code&gt; should have fired when you called the function with time_ms = 300000, but this does not seems to have happened. I&amp;#39;m not sure why, but I have reported this issue internally.&lt;/p&gt;
&lt;p&gt;If you want to keep the ASSERT and make sure all inputs are valid, you can use this function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;__STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms,
                                               nrf_timer_frequency_t frequency)
{
    // The &amp;quot;frequency&amp;quot; parameter here is actually the prescaler value, and the
    // timer runs at the following frequency: f = 16000 kHz / 2^prescaler.
    uint32_t prescaler = (uint32_t)frequency;
    uint64_t ticks = ((time_ms * 16000ULL) &amp;gt;&amp;gt; prescaler);
    ASSERT(ticks &amp;lt;= UINT32_MAX);
    return (uint32_t)ticks;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can configure the timer frequency all the way down to 31250 Hz, which should give you a maximum of ~2290.6 minutes. If you want to run timers longer, you can use the &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/rtc.html?cp=2_1_0_24#concept_rvn_vkj_sr"&gt;RTC&lt;/a&gt;. The RTC can be configured to run for &lt;a href="https://devzone.nordicsemi.com/question/72753/clocks-timers-rtc-why/?answer=72870#post-id-72870"&gt;up to ~582.5 hours before overflowing&lt;/a&gt;. RTC is also lower power than the TIMER.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 timer example, over 5 minutes event handler.</title><link>https://devzone.nordicsemi.com/thread/93027?ContentTypeID=1</link><pubDate>Fri, 21 Jul 2017 13:33:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:513468e5-3786-4520-91c6-02de790bb0ad</guid><dc:creator>JW</dc:creator><description>&lt;p&gt;It works!! Thank you so much! But actually &amp;#39;nrf_timer.h&amp;#39; is referred by every Keil projects. So..isn&amp;#39;t there any problem with 16000ULL in other case? In my opinion, it might be okay because the difference between UL and ULL is just size.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 timer example, over 5 minutes event handler.</title><link>https://devzone.nordicsemi.com/thread/93029?ContentTypeID=1</link><pubDate>Fri, 21 Jul 2017 11:31:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1e4b7b15-8614-4d24-9661-5e550ad08aa1</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;I see there is a 32 bit multiplication inside the function, resulting in the return value being wrong. If you change line 618 in &lt;em&gt;nrf_timer.h&lt;/em&gt; from &lt;code&gt;return ((time_ms * 16000UL) &amp;gt;&amp;gt; prescaler);&lt;/code&gt; to &lt;code&gt;return ((time_ms * 16000ULL) &amp;gt;&amp;gt; prescaler);&lt;/code&gt;, you should get correct behavior.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 timer example, over 5 minutes event handler.</title><link>https://devzone.nordicsemi.com/thread/93031?ContentTypeID=1</link><pubDate>Fri, 21 Jul 2017 11:06:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:09d0e34d-c970-4d29-a2fc-81a1b97b7c27</guid><dc:creator>JW</dc:creator><description>&lt;p&gt;Well, but when I set the time interval 5 minutes with &lt;code&gt;time_ms = 300000&lt;/code&gt; after change frequency to 8MHz, the event handler also run every 30 seconds.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 timer example, over 5 minutes event handler.</title><link>https://devzone.nordicsemi.com/thread/93030?ContentTypeID=1</link><pubDate>Fri, 21 Jul 2017 10:53:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c07833a2-26cf-47c8-8541-5e1588fe32fa</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Your observations are correct, this change will not affect the time between event handler calls. What it will change is the output from &lt;code&gt;nrf_drv_timer_ms_to_ticks()&lt;/code&gt;, the variable &lt;code&gt;time_ticks&lt;/code&gt;. With &lt;code&gt;time_ms = 1000&lt;/code&gt; and frequency set to 16 MHz, the returned value will be 16 000 000. With &lt;code&gt;time_ms = 1000&lt;/code&gt; and frequency set to 8 MHz, it will return 8 000 000. Reducing the frequency will reduce the resolution of the timer, but it will increase the time the timer can run before overflowing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 timer example, over 5 minutes event handler.</title><link>https://devzone.nordicsemi.com/thread/93032?ContentTypeID=1</link><pubDate>Fri, 21 Jul 2017 10:13:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0c76ae61-46ae-4249-ac93-f39a387035e1</guid><dc:creator>JW</dc:creator><description>&lt;p&gt;Hello Jørgen,&lt;/p&gt;
&lt;p&gt;Thank you for answering my question.
I can understand the timer configuration by your explanation.&lt;/p&gt;
&lt;p&gt;But when I changed the frequency on &amp;#39;nrf_drv_config.h&amp;#39;&lt;/p&gt;
&lt;p&gt;from 16MHz&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#define TIMER0_ENABLED 1

#if (TIMER0_ENABLED == 1)
#define TIMER0_CONFIG_FREQUENCY    NRF_TIMER_FREQ_16MHz
#define TIMER0_CONFIG_MODE         TIMER_MODE_MODE_Timer
#define TIMER0_CONFIG_BIT_WIDTH    TIMER_BITMODE_BITMODE_32Bit
#define TIMER0_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW

#define TIMER0_INSTANCE_INDEX      0
#endif
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to 8MHz&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#define TIMER0_CONFIG_FREQUENCY    NRF_TIMER_FREQ_8MHz
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and set the timer interval 1 second&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uint32_t time_ms = 1000;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;nothing changed.&lt;/p&gt;
&lt;p&gt;The timer event handler run every 1 second at both configuration.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52 timer example, over 5 minutes event handler.</title><link>https://devzone.nordicsemi.com/thread/93026?ContentTypeID=1</link><pubDate>Fri, 21 Jul 2017 07:17:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ca389348-2ed5-49b0-add1-d3dd9505fe2b</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi JW,&lt;/p&gt;
&lt;p&gt;By default, the timer runs at 16 MHz and the maximum bit width of the timer is 32-bit. This corresponds to a maximum timer value of:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(2^32)-1 = 4294967295
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The reason why you see event handler more rapidly with 5 minutes, is that &lt;code&gt;nrf_drv_timer_ms_to_ticks&lt;/code&gt; use the configured frequency and the &lt;code&gt;time_ms&lt;/code&gt; parameter to generate the number of ticks, and return this in a 32 bit variable:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;5*60*16000000 = 4800000000
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This overflows the 32 bit variable, giving you a tick count of 505032705 = ~31.5 seconds.&lt;/p&gt;
&lt;p&gt;You need to change the frequency of the timer to make it run run slower. This is done by changing the timer configuration:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;timer_cfg.frequency = NRF_TIMER_FREQ_8MHz;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can set it to &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v13.1.0/group__nrf__timer__hal.html?cp=4_0_0_6_8_23_0_5#ga28862835cd77a9c8481ed04c7b1918eb"&gt;any of the frequencies documented here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>