<?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>nRF52840 timer that counts to a specific value and wraps up</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/89559/nrf52840-timer-that-counts-to-a-specific-value-and-wraps-up</link><description>I&amp;#39;m trying to implement a timer with 1ms resolution that my application can use externally using a callback function. 
 The idea is to 
 
 Implement the timer as a 32-bit counter that counts up starting at 0x00000000 and wraps back to 0 after reaching</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 06 Jul 2022 10:19:21 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/89559/nrf52840-timer-that-counts-to-a-specific-value-and-wraps-up" /><item><title>RE: nRF52840 timer that counts to a specific value and wraps up</title><link>https://devzone.nordicsemi.com/thread/375729?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 10:19:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:67132a9e-f7a1-4d5d-8200-4955aab0b6c3</guid><dc:creator>Simon</dc:creator><description>[quote user=""]This atomic function simply returns an uint32_t type variable containing the current value of the counter variable.[/quote]
&lt;p&gt;I have not worked with the nRF5 SDK for 2-3 years, but I tried to take a look.&lt;/p&gt;
&lt;p&gt;In the nRF Connect SDK, th&amp;nbsp;function&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/samples/drivers/counter/alarm/src/main.c#L48"&gt;counter_get_value()&lt;/a&gt;&amp;nbsp;would call &lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/drivers/counter/counter_nrfx_timer.c#L89"&gt;get_value()&lt;/a&gt;--&amp;gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/drivers/counter/counter_nrfx_timer.c#L78"&gt;read()&lt;/a&gt;--&amp;gt;nrf_timer_cc_get()&lt;/p&gt;
&lt;p&gt;The equivalent function in nRF5 SDK would be&amp;nbsp;nrf_drv_timer_capture()--&amp;gt;&lt;span&gt;nrfx_timer_capture()--&amp;gt;&lt;/span&gt;nrf_timer_cc_read()&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t nrfx_timer_capture(nrfx_timer_t const * const p_instance,
                            nrf_timer_cc_channel_t     cc_channel)
{
    NRFX_ASSERT(m_cb[p_instance-&amp;gt;instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
    NRFX_ASSERT(cc_channel &amp;lt; p_instance-&amp;gt;cc_channel_count);

    nrf_timer_task_trigger(p_instance-&amp;gt;p_reg,
        nrf_timer_capture_task_get(cc_channel));
    return nrf_timer_cc_read(p_instance-&amp;gt;p_reg, cc_channel);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You can find&amp;nbsp;&lt;span&gt;nrf_drv_timer_capture in&lt;/span&gt;&amp;nbsp;nRF5_SDK_17.1.0\integration\nrfx\legacy\nrf_drv_timer.h&lt;/p&gt;
&lt;p&gt;Oh, I see that you&amp;#39;re using 15.3.0, but it seems like this function is present there as well:&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.3.0/group__nrf__drv__timer.html?cp=8_5_2_6_9_0_35_0_1#gab47bc0277a884dc727fe4d4b7f378398"&gt;https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.3.0/group__nrf__drv__timer.html?cp=8_5_2_6_9_0_35_0_1#gab47bc0277a884dc727fe4d4b7f378398&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 timer that counts to a specific value and wraps up</title><link>https://devzone.nordicsemi.com/thread/375601?ContentTypeID=1</link><pubDate>Tue, 05 Jul 2022 14:30:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:490f9114-9087-4277-8701-d4994f7d967f</guid><dc:creator>Ad1tya0</dc:creator><description>&lt;div id="MathJax_Message" style="display:none;"&gt;&lt;/div&gt;
&lt;div id="MathJax_Message" style="display:none;"&gt;&lt;/div&gt;
&lt;div id="MathJax_Message" style="display:none;"&gt;&lt;/div&gt;
&lt;p&gt;Hi &lt;a href="https://devzone.nordicsemi.com/members/simoniversen"&gt;Simon&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for the response, I checked it out. However because of limitations with the project I&amp;#39;m not using zephyr or even the nRFConnect SDK, the development is being done bare metal on the PCA10056 board of the nRF52840, using nRF_SDK_15.3.0&lt;/p&gt;
&lt;p&gt;Accordingly, I&amp;#39;d only be able to use the nRF SDK 15.3.0 files to implement this funcitonability.&lt;br /&gt;I figure, I can set up a timer of 1ms, that as a timer event increments a variable and I can just read the variable and return it. I dont understand the NRF_TIMER_EVENT_COMPAREX modes and also the&amp;nbsp;NRF_TIMER_SHORT_COMPAREX_CLEAR_MASK modes.&lt;/p&gt;
&lt;p&gt;Would there be an easier method of implementing what I require using the SDK files only?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 timer that counts to a specific value and wraps up</title><link>https://devzone.nordicsemi.com/thread/375420?ContentTypeID=1</link><pubDate>Mon, 04 Jul 2022 21:03:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e239277-a147-4265-a3bf-eeb03a2ee56a</guid><dc:creator>Simon</dc:creator><description>[quote user=""]Implement the timer as a 32-bit counter that counts up starting at 0x00000000 and wraps back to 0 after reaching 0xffffffff.[/quote]
&lt;p&gt;Check out the sample&amp;nbsp;&lt;em&gt;\zephyr\samples\drivers\counter\alarm. &lt;/em&gt;You set the counter value here:&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/samples/drivers/counter/alarm/src/main.c#L90"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/samples/drivers/counter/alarm/src/main.c#L90&lt;/a&gt;, where ticks is of type&amp;nbsp;uint32_t.&lt;/p&gt;
[quote user=""]&lt;ul&gt;&lt;li&gt;Implement &amp;quot;Atomic&amp;quot; function to return 1ms counter.&lt;/li&gt;
&lt;li&gt;This atomic function simply returns an uint32_t type variable containing the current value of the counter variable.&lt;/li&gt;&lt;/ul&gt;[/quote]
&lt;p&gt;You can use the function &lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.0.99-ncs1/samples/drivers/counter/alarm/src/main.c#L48"&gt;counter_get_value()&lt;/a&gt; to achieve this.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>