<?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>Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts</link><description>Hey guys, 
 I want to create an accurate timer without interrupts. My timer has to be very accurate because I have to send HIGH and LOW signals for 1us. I have read this pos t to know that I need high-frequency clock source (HFCLK, 16 MHz), which means</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 06 Mar 2020 17:04:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts" /><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238599?ContentTypeID=1</link><pubDate>Fri, 06 Mar 2020 17:04:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:81e5d5e2-ad1f-46a2-b06d-0bd121ab21e1</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;Hey H&amp;aring;kon,&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts/238532"] Same goes for GPIO outputs, which needs to be configured[/quote]
&lt;p&gt;that&amp;#39;s interesting because for toggling the output like&amp;nbsp;&lt;em&gt;NRF_P0-&amp;gt;OUT ^= (1 &amp;lt;&amp;lt; OUTPUT_PIN); &lt;/em&gt;I don&amp;#39;t need to configure the output pin as output (in my project)...&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="tlid-translation translation" lang="en"&gt;&lt;span title=""&gt;But thanks again for your detailed explanations.&lt;/span&gt; &lt;span class="" title=""&gt;It helps me a lot. &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="tlid-translation translation" lang="en"&gt;&lt;span class="" title=""&gt;If I have some problems again, I hope we can work together again. &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="tlid-translation translation" lang="en"&gt;&lt;span class="" title=""&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="tlid-translation translation" lang="en"&gt;&lt;span class="" title=""&gt;Have a nice weekend too, H&amp;aring;kon!&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="tlid-translation translation" lang="en"&gt;&lt;span class="" title=""&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="tlid-translation translation" lang="en"&gt;&lt;span class="" title=""&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="tlid-translation translation" lang="en"&gt;&lt;span class="" title=""&gt;Best wishes and kind regards,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="tlid-translation translation" lang="en"&gt;&lt;span class="" title=""&gt;Christoph&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238532?ContentTypeID=1</link><pubDate>Fri, 06 Mar 2020 13:28:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7a01a3ea-a8b9-403f-82d4-eaea51427646</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi &lt;span&gt;Christoph&lt;/span&gt;,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Christoph_I"]As note: when I&amp;#39;m trying this I had to set the INPUT_PIN as input as follows[/quote]
&lt;p&gt;Ah, yes, that is a pre-requisite. Same goes for GPIO outputs, which needs to be configured (nrf_gpio_cfg_output(my_pin_number); for instance), but those you set/clear via NRF_GPIO-&amp;gt;OUTSET, OUTCLR, or OUT (or any other &amp;quot;helper&amp;quot; functions, like nrf_gpio_pin_toggle()).&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Christoph_I"]What is doing the ICACHE?[/quote]
&lt;p&gt;&amp;nbsp;This is the instruction cache. This cache reads instructions from flash, assuming that the program execution is incremental, thus speeding up your program. Its sped up as flash is actually a bit slower than the frequency the CPU run on. The implementation is explained more in detail here:&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/nvmc.html?cp=4_2_0_10_5#concept_dh3_cyy_vr"&gt;https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/nvmc.html?cp=4_2_0_10_5#concept_dh3_cyy_vr&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But, for generic cache explanations, wiki would help you out as well.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Christoph_I"]Does the pragma set the priority for this function at the highest level or which settings for optimization will be overritten by pragma?[/quote]
&lt;p&gt;&amp;nbsp;The pragma sets the optimization level to the compiler. Let&amp;#39;s say you want to optimize your firmware for size (-Os flag to the compiler), but you have certain functions that you want to run as fast as possible, you can wrap that option around those functions. You can even optimize a full .c file individually if you&amp;#39;d like.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Christoph_I"]I&amp;#39;ve check the input with the new GCC optimization and your amount of __NOP() (50 i think) and I&amp;#39;m getting the same values as you[/quote]
&lt;p&gt;This is good news! You can ofcourse add or remove amount of NOPs to see if you&amp;#39;re able to fine-tune it even further.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Christoph_I"]But first I have to thank you for your patience and devotiation. Without your help I would be very helpless. Thanks Håkon, you are a very nice and smart one! I really enjoyed working with you.[/quote]
&lt;p&gt;&amp;nbsp;Thank you for the kind words! I am very glad to hear that I was of good use! If you run into issues or questions in the future, you know where to ask&amp;nbsp;&lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Hope you have a nice weekend, Christoph!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238496?ContentTypeID=1</link><pubDate>Fri, 06 Mar 2020 10:46:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cffb2f27-6ffc-4865-918a-e1c602634171</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;Hey H&amp;aring;kon,&lt;/p&gt;
&lt;p&gt;it&amp;#39;s the last day before weekend. &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts/238438"]uint32_t pin =&amp;nbsp; (&lt;span&gt;NRF_P0-&amp;gt;IN &amp;amp; (1 &amp;lt;&amp;lt; INPUT_PIN));&lt;/span&gt;[/quote]
&lt;p&gt;As note: when I&amp;#39;m trying this I had to set the INPUT_PIN as input as follows:&lt;/p&gt;
&lt;p&gt;&lt;em&gt; nrf_gpio_cfg_input(INPUT_PIN, NRF_GPIO_PIN_NOPULL);&lt;/em&gt;&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts/238438"]Because they are function calls, [/quote]
&lt;p&gt;Thanks for your explanation.&lt;/p&gt;
&lt;p&gt;What is doing the ICACHE?&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts/238438"]Your overall settings for optimization will be overridden for this specific function only.[/quote]
&lt;p&gt;Does the pragma set the priority for this function at the highest level or which settings for optimization will be overritten by pragma?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts/238438"]Please check with these pragma&amp;#39;s for GCC optimization and see if you also see similar behavior.[/quote]
&lt;p&gt;I&amp;#39;ve check the input with the new GCC optimization and your amount of __NOP() (50 i think) and I&amp;#39;m getting the same values as you. &lt;/p&gt;
&lt;p&gt;1 us input = 1.08 us&lt;/p&gt;
&lt;p&gt;2 us input = 1.88 us&lt;/p&gt;
&lt;p&gt;10 us input = 9.9 us&lt;/p&gt;
&lt;p&gt;60 us input = 60 us&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;But first I have to thank you for your patience and devotiation. Without your help I would be very helpless. Thanks H&amp;aring;kon, you are a very nice and smart one! I really enjoyed working with you.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Christoph&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238438?ContentTypeID=1</link><pubDate>Fri, 06 Mar 2020 08:02:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45375ed3-d9c7-4dda-b197-fe9c113cbf4d</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi Christoph,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user="Christoph_I"]Now I&amp;#39;m getting nearly the 1us! :)[/quote]
&lt;p&gt;That is good news!&lt;/p&gt;
[quote user="Christoph_I"]To read from input: NRF_P0-&amp;gt;IN = (1 &amp;lt;&amp;lt; INPUT_PIN);[/quote]
&lt;p&gt;Try this: uint32_t pin =&amp;nbsp; (&lt;span&gt;NRF_P0-&amp;gt;IN &amp;amp; (1 &amp;lt;&amp;lt; INPUT_PIN));&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Others look OK.&lt;/span&gt;&lt;/p&gt;
[quote user="Christoph_I"]nrf_gpio_pin_set() / clear() ?[/quote]
&lt;p&gt;Because they are function calls, although static inlined, which use stacked memory to access the register. If you look at the assembly output, you&amp;#39;ll likely see more instructions than just directly accessing the register. The output depends on the compiler used as well.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For GCC, you need to force the optimization like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#pragma GCC push_options
#pragma GCC optimize (&amp;quot;O3&amp;quot;)
void timer_sleep(uint32_t timeout_us)
{

    if (timeout_us == 0) 
    {
        return;
    }
    if (timeout_us == 1)
    {
        /* Adjust the amount of NOPs here */
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop();
        return;
    }

    NRF_TIMER1-&amp;gt;CC[CC_USED] = (timeout_us &amp;lt;&amp;lt; 4) - 18;
    NRF_TIMER1-&amp;gt;TASKS_START = 1;
    while(NRF_TIMER1-&amp;gt;EVENTS_COMPARE[CC_USED] == 0) 
    {
      /* Call __WFE() if loop is long */
    }
    NRF_TIMER1-&amp;gt;EVENTS_COMPARE[CC_USED] = 0;
}
#pragma GCC pop_options&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Your overall settings for optimization will be overridden for this specific function only.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;*edit*&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Tested with &amp;quot;O3&amp;quot; optimization on SES v4.22, and got:&lt;/p&gt;
&lt;p&gt;1 us input = 1.05 to 1.1 us&lt;/p&gt;
&lt;p&gt;2 us input = 1.9 us&lt;/p&gt;
&lt;p&gt;10 us input = 9.9 us&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please check with these pragma&amp;#39;s for GCC optimization and see if you also see similar behavior.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238340?ContentTypeID=1</link><pubDate>Thu, 05 Mar 2020 14:52:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bb99a129-cb3e-4211-a45e-a2649ff756aa</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;Ok H&amp;aring;kon,&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts/238258"]Did you run the example as-is to see if you are able replicate my measurements?[/quote]
&lt;p&gt;that is a good objection. I run your example and reduced the amount of __NOP() a little bit.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;

#define OUTPUT_PIN 18
#define CC_USED 0

void timer_setup(void)
{
    NRF_TIMER1-&amp;gt;BITMODE = TIMER_BITMODE_BITMODE_32Bit;
    NRF_TIMER1-&amp;gt;SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE0_CLEAR_Pos| TIMER_SHORTS_COMPARE0_STOP_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE0_STOP_Pos;
    NRF_TIMER1-&amp;gt;PRESCALER = 0;
}


#pragma push
#pragma O3
void timer_sleep(uint32_t timeout_us)
{

    if (timeout_us == 0) 
    {
        return;
    }
    if (timeout_us == 1)
    {
        /* Adjust the amount of NOPs here */
        __NOP(); __NOP(); __NOP(); __NOP(); 
        __NOP(); __NOP(); __NOP(); __NOP(); 
        __NOP(); __NOP(); __NOP(); __NOP(); 
        __NOP(); __NOP(); __NOP(); __NOP(); 
        __NOP(); __NOP(); __NOP(); __NOP(); 
        __NOP(); __NOP(); __NOP(); __NOP(); 
        __NOP(); __NOP(); __NOP(); __NOP(); 
        __NOP(); __NOP(); __NOP(); __NOP(); 
        __NOP(); __NOP(); __NOP(); __NOP(); 
        /*__nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop(); __nop(); __nop(); 
        __nop(); __nop();*/
        return;
    }

    NRF_TIMER1-&amp;gt;CC[CC_USED] = (timeout_us &amp;lt;&amp;lt; 4) - 18;
    NRF_TIMER1-&amp;gt;TASKS_START = 1;
    while(NRF_TIMER1-&amp;gt;EVENTS_COMPARE[CC_USED] == 0) 
    {
      /* Call __WFE() if loop is long */
    }
    NRF_TIMER1-&amp;gt;EVENTS_COMPARE[CC_USED] = 0;
}
#pragma pop

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    /* Configure board. */
    //bsp_board_init(BSP_INIT_LEDS);
    bsp_board_leds_init();
    NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1;
    while (NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0);
    NRF_NVMC-&amp;gt;ICACHECNF = NVMC_ICACHECNF_CACHEEN_Msk;
    timer_setup();
    /* Toggle LEDs. */
    while (true)
    {
        timer_sleep(2);
        NRF_P0-&amp;gt;OUT ^= (1 &amp;lt;&amp;lt; OUTPUT_PIN);

    }
}

/**
 *@}
 **/
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Now I&amp;#39;m getting nearly the 1us! :)&lt;/p&gt;
&lt;p&gt;But why is there a delay when I&amp;#39;m using:&lt;/p&gt;
&lt;p&gt;nrf_gpio_pin_set() / clear() ?&lt;/p&gt;
&lt;p&gt;Can you please check if my adoptions are true:&lt;/p&gt;
&lt;p&gt;To set the output I&amp;#39;m using: NRF_P0-&amp;gt;OUT |= (1 &amp;lt;&amp;lt; OUTPUT_PIN);&lt;/p&gt;
&lt;p&gt;To clear the output: NRF_P0-&amp;gt;OUT &amp;amp;= ~(1 &amp;lt;&amp;lt; OUTPUT_PIN);&lt;/p&gt;
&lt;p&gt;To read from input: NRF_P0-&amp;gt;IN = (1 &amp;lt;&amp;lt; INPUT_PIN);&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m using SES and SDK 14.2.0&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Christoph&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238258?ContentTypeID=1</link><pubDate>Thu, 05 Mar 2020 11:52:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:28a3bafb-0130-4f64-999d-ccc7a2799b06</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi Christoph,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Did you run the example as-is to see if you are able replicate my measurements? Note the ICACHE is enabled, and external HFCLK is running, and the function itself is with O3 optimization (Keil syntax). Which compiler are you using?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238249?ContentTypeID=1</link><pubDate>Thu, 05 Mar 2020 11:34:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:524e1a0b-a7a3-49f4-9d76-03534c0cf173</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;Hey H&amp;aring;kon,&lt;/p&gt;
&lt;p&gt;thank you, the times have become much more accurate. This is a big step forward!&lt;/p&gt;
&lt;p&gt;With your function &lt;em&gt;timer_sleep &lt;/em&gt;without adjust the amount of NOPs I&amp;#39;m getting 2us instead of 1us on my end. But deleting all NOPs in this function I&amp;#39;m getting 1.340us (LOW signal, HIGH: 1.370) instead of 1us and that&amp;#39;s very close to what I want.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void timer_sleep(...)
{
    ...
    if (timeout_us == 1)
        {
            /* NO NOPs here */
            return;
        }
    ...
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Do you know if there are any other options to reduce the inaccuracy more and more?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Christoph&lt;/p&gt;
&lt;p&gt;______________________________________&lt;/p&gt;
&lt;p&gt;Edit:&lt;/p&gt;
&lt;p&gt;P.S.:&lt;/p&gt;
&lt;p&gt;Without NOPs I&amp;#39;m getting this times:&lt;/p&gt;
&lt;p&gt;2us -&amp;gt; HIGH: 3.08us; LOW: 2.840&lt;/p&gt;
&lt;p&gt;10us -&amp;gt; HIGH: 11.10us; LOW: 10.70us&lt;/p&gt;
&lt;p&gt;60us --&amp;gt; LOW: 60.80us&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238189?ContentTypeID=1</link><pubDate>Thu, 05 Mar 2020 08:46:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3afc2588-450e-40e2-95c3-ee473a955f2f</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I see what you mean. I get a delay of ~2.3 us when using the timer, and approx. 1.2 us when using nrf_delay_us().&lt;/p&gt;
&lt;p&gt;When dealing with such low delays, the overhead with handling the actual timer tasks and events will give a significant error in your GPIO toggling.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For the 1 us specific delay, I think you have to use a busy-loop, and not the timer.&lt;/p&gt;
&lt;p&gt;I have played a bit around and tried to create a function that can give a more reliable delay.&lt;/p&gt;
&lt;p&gt;1 us input:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-6b3f38ae485b4e9da4c78ff8d610dbf0/pastedimage1583397892509v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;2 us input:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-6b3f38ae485b4e9da4c78ff8d610dbf0/pastedimage1583397918785v3.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;10 us input:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-6b3f38ae485b4e9da4c78ff8d610dbf0/pastedimage1583397872517v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could you try this and see if it works similarly on your end?&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-6b3f38ae485b4e9da4c78ff8d610dbf0/main.c"&gt;devzone.nordicsemi.com/.../main.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Christoph_I"]For example I set timeout_us = 4400 seconds. This timeout_us is never reached because after 4294 seconds there will happen an overflow and the timer will restart at 0seconds, right? This will be an endless loop, right?[/quote]
&lt;p&gt;&amp;nbsp;a uint32_t will overflow in this scenario, resulting in a lower timeout. If you set the timer to 0x 12 0000 1000, the timer will count to 0x1000, and give an event.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/238053?ContentTypeID=1</link><pubDate>Wed, 04 Mar 2020 13:59:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5a922139-8ff4-44c8-9faf-4261c266614e</guid><dc:creator>Christoph_I</dc:creator><description>&lt;p&gt;Hey H&amp;aring;kon,&lt;/p&gt;
&lt;p&gt;thank you for your fast reply. I have some questions for my understanding (1,2,3,4) and another question (5) for my time issue.&lt;/p&gt;
&lt;p&gt;1) In your Code:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;while(NRF_TIMERx-&amp;gt;EVENTS_COMPARE[CC_USED] == 0) &amp;nbsp;&amp;nbsp; //e.g. timeout_us = 40us&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;the events_compare[CC_USED] is as long as zero until the 40us are reached, right? And when the 40us are reached, the counter is incremented which causes the compare event (&lt;em&gt;EVENTS_COMPARE[CC_USED]&lt;/em&gt;) to be generated and the&amp;nbsp;&lt;em&gt;EVENTS_COMPARE&lt;/em&gt; becomes equal to the value specified in &lt;em&gt;CC_USED (40us)&lt;/em&gt;, right?&amp;nbsp; This leads to question 2.&lt;em&gt; &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;2) In your Code:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;NRF_TIMERx-&amp;gt;EVENTS_COMPARE[CC_USED] = 0;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I have to delete the value because of the &lt;em&gt;EVENTS_COMPARE[CC_USED]&lt;/em&gt; have the value &amp;quot;40us&amp;quot; now?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;3) After &lt;em&gt;NRF_TIMERx-&amp;gt;EVENTS_COMPARE[CC_USED] = 0;&lt;/em&gt; why I don&amp;#39;t set &lt;em&gt;&lt;/em&gt; at the end of the function the stop and clear command (&lt;em&gt;NRF_TIMERx-&amp;gt;TASKS_STOP = 1; &lt;/em&gt;and &lt;em&gt;NRF_TIMERx-&amp;gt;TASKS_CLEAR = 1;&lt;/em&gt;)? I ask because if I don&amp;#39;t set the stop command at the end of the function the timer will expire the whole time, not? Why it&amp;#39;s better to set the stop and clear commands at the beginning of the function?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;4)&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/58621/create-accurate-microsecond-timer-without-interrupts/237768"]32 bit = 0 to 4 294 seconds[/quote]
&lt;p&gt;For example I set timeout_us = 4400 seconds. This timeout_us is never reached because after 4294 seconds there will happen an overflow and the timer will restart at 0seconds, right? This will be an endless loop, right?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;5) I tried our code (inclusive your idea) and I get the same problem like nrf_delay_us. I messure with an oscilloscope the high and low signals of my output pin and the time is as accurate as nrf_delay_us ...&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void Write_Byte(uint8_t byte)
{
    int walk;

    for (walk = 0; walk &amp;lt; 8; ++walk)
    {
        nrf_gpio_pin_clear(OUTPUT_PIN);    
        if (byte &amp;amp; 0x01)
        {
            TIMEROUT_TIMER1_Start(1);       //instead of nrf_delay_us()                      
            nrf_gpio_pin_set(OUTPUT_PIN);                
            TIMEROUT_TIMER1_Start(60);      //instead of nrf_delay_us()                   
        }
        ....&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In line 10 I want a timeout of 1us and in oscilloscope the timout is nearly 3us.. and in line 12 i want a timeout of 60us and get a timeout between 66 and 67us... Do you know what it can be? Maybe it&amp;#39;s my board which causes my issue? I&amp;#39;m using the DVK-BL652 from Laird.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Christoph&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Create accurate microsecond timer without interrupts</title><link>https://devzone.nordicsemi.com/thread/237768?ContentTypeID=1</link><pubDate>Tue, 03 Mar 2020 13:23:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be09974c-4575-42a8-92cc-25dc4fbcbd13</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Since the prescaler is already setup with 4 (1 us resolution), you can set the specific CC directly with the input timeout_us.&lt;/p&gt;
&lt;p&gt;Something like this maybe?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define CC_USED 0

if (timeout_us == 0) return;

NRF_TIMERx-&amp;gt;TASKS_STOP = 1;
NRF_TIMERx-&amp;gt;TASKS_CLEAR = 1;

NRF_TIMERx-&amp;gt;CC[CC_USED] = timeout_us;
NRF_TIMERx-&amp;gt;TASKS_START = 1;
while(NRF_TIMERx-&amp;gt;EVENTS_COMPARE[CC_USED] == 0) 
{
  /* Call __WFE() if loop is long */
}
NRF_TIMERx-&amp;gt;EVENTS_COMPARE[CC_USED] = 0;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user=""]In line 19: (Init) Why should I use 32 bits and not 16 bits? where is the difference?[/quote]
&lt;p&gt;16 bit = 0 to 65535 us&lt;/p&gt;
&lt;p&gt;32 bit = 0 to 4 294 seconds&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Depends on the range you&amp;#39;d like.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>