<?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>Measuring Microsecond Time</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/74746/measuring-microsecond-time</link><description>Hi, I am trying to expand on the ESB PRX example and want to recreate functions with the same functionality as Arduino&amp;#39;s micros() and millis() functions. I&amp;#39;d like the following: 
 a) 1 μs resolution 
 b) Low latency time read 
 c) Safe to use from either</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 06 May 2021 16:58:52 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/74746/measuring-microsecond-time" /><item><title>RE: Measuring Microsecond Time</title><link>https://devzone.nordicsemi.com/thread/308611?ContentTypeID=1</link><pubDate>Thu, 06 May 2021 16:58:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:38ab8282-439f-458c-8782-63e8215ec681</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Thanks for sharing!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Measuring Microsecond Time</title><link>https://devzone.nordicsemi.com/thread/308596?ContentTypeID=1</link><pubDate>Thu, 06 May 2021 15:21:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45808a4c-3e38-46e6-a016-3d7936f5f16e</guid><dc:creator>microderm</dc:creator><description>&lt;p&gt;Thanks, that worked for me. Here is what I did:&lt;/p&gt;
&lt;p&gt;1) Added the code below.&lt;/p&gt;
&lt;p&gt;2) Copied the TIMER block from the sdk_config.h file of a TIMER example and set TIMER1_ENABLED 1.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static uint32_t curr_sys_micros_32 = 0;
static uint64_t curr_sys_micros_64 = 0;

/**
 * Initialises the timer.
 */
void micros64_init() {
    NRF_TIMER1-&amp;gt;TASKS_STOP = 1;   // Stop timer  //
    NRF_TIMER1-&amp;gt;MODE = 0;         // Timer mode //
    NRF_TIMER1-&amp;gt;BITMODE = 3;      // 32-bit counter //
    NRF_TIMER1-&amp;gt;PRESCALER = 4;    // Prescalar 2^4 //
    NRF_TIMER1-&amp;gt;TASKS_CLEAR = 1;  // Clear counter //
    NRF_TIMER1-&amp;gt;TASKS_START = 1;  // Start timer //
}

/**
 * Returns the current 64-bit microsecond system time. Must be called within 71 minutes of previous call.
 * @return the current 64-bit microsecond system time.
 */
uint64_t micros64() {
    NRF_TIMER1-&amp;gt;TASKS_CAPTURE[0] = 1; // Capture current value //
    uint32_t prev_value = curr_sys_micros_32;
    curr_sys_micros_32 = NRF_TIMER1-&amp;gt;CC[0]; // Store value //
    curr_sys_micros_64 += curr_sys_micros_32 - prev_value;
    return curr_sys_micros_64;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The code accounts for the 32-bit overflow to give the correct 64-bit timestamp in microseconds, so long as micros64() is called periodically with period&amp;nbsp;less than 71 minutes.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Measuring Microsecond Time</title><link>https://devzone.nordicsemi.com/thread/308336?ContentTypeID=1</link><pubDate>Wed, 05 May 2021 11:49:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8572d83e-afb8-4e23-9923-8d40dd0c76ed</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;It should be possible to use a TIMER for this:&lt;br /&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/timer.html#concept_xbd_hqp_sr"&gt;https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/timer.html#concept_xbd_hqp_sr&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you set PRESCALER=4, then the TIMER will run on 1MHz, so every tick increment would be 1us. To read the timer you would need to execute a TASKS_CAPTURE followed by reading CC register. If you are using BITMODE=3 (32-bit mode), then the TIMER will overflow&amp;nbsp;after 1us * 2^32 = 1.2hours, so you need to also add a global variable to keep track of overflows, but as long as you read the timer more frequently than 1.2hours, then it should be possible to compare previous value with latest, and then also keep track of the number of overflows (e.g. n * 1.2hours).&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>