<?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>FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/18209/freertos-changes-for-better-rtos-timer-accuracy</link><description>Hi, 
 I have made some changes with how we update tick timer inside FreeRTOS and the results are promising
.
The improvement is 
 
 RTOS tick will auto correct if masked for more than one tick. BLE activity will not affect the RTOS tick accuracy</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 29 Mar 2017 15:16:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/18209/freertos-changes-for-better-rtos-timer-accuracy" /><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70311?ContentTypeID=1</link><pubDate>Wed, 29 Mar 2017 15:16:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0808eb06-6dc2-488b-bfc5-f763f8c1e012</guid><dc:creator>ahedin25</dc:creator><description>&lt;p&gt;Should the SDK 13 solution call xTaskGetTickCountFromISR?  So that it reads properly.  Implemenation is the same.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70310?ContentTypeID=1</link><pubDate>Fri, 24 Mar 2017 14:57:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4cac964d-dc8d-4548-9d49-85f15239b005</guid><dc:creator>ahedin25</dc:creator><description>&lt;p&gt;The patch in this thread:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if(diff &amp;gt; 1)
{
    /* This could happen if this interupt was masked for more than one RTOS tick period */
    vTaskStepTick(diff - 1);
}


 if ( xTaskIncrementTick() != pdFALSE )
 {
     SCB-&amp;gt;ICSR = SCB_ICSR_PENDSVSET_Msk;
     __SEV();
 }
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70309?ContentTypeID=1</link><pubDate>Fri, 24 Mar 2017 14:56:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:793c558e-222b-4ca0-b725-5c0ed9231964</guid><dc:creator>ahedin25</dc:creator><description>&lt;p&gt;SDK 13.0.0&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if (configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG == 0)
{
    /* check FreeRTOSConfig.h file for more details on configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG */
    TickType_t diff;
    diff = ((m_tick_overflow_count &amp;lt;&amp;lt; portNRF_RTC_BITWIDTH) + systick_counter) - xTaskGetTickCount();

    while((diff--) &amp;gt; 0)
    {
        switch_req |= xTaskIncrementTick();
    }
}
else
{
    switch_req = xTaskIncrementTick();
}

/* Increment the RTOS tick as usual which checks if there is a need for rescheduling */
if ( switch_req != pdFALSE )
{ 
   	 ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70308?ContentTypeID=1</link><pubDate>Fri, 24 Mar 2017 14:56:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ebacedbd-e171-4427-8e81-9edb847f352e</guid><dc:creator>ahedin25</dc:creator><description>&lt;p&gt;I am sorry, but I am still don&amp;#39;t understand the explanation.  SDK 13 repeatedly calls increment tick, but your patch only calls
step tick once and increment tick once.&lt;/p&gt;
&lt;p&gt;When using the patch code in my system, vTaskStepTick would assert. The FreeRTOS discussion indicates that vTaskStepTick can&amp;#39;t be used because it might jump over the tick of when a task unblocks.
Your explanation indicates that in certain situations the time to increment the tick for all of the lost ticks could take a millisecond.&lt;/p&gt;
&lt;p&gt;So, it seems that xTaskIncrementTick must be used to corret the tick and in some cases it will cause another tick to be lost.  What happens when another tick occurs when running in the tick interrupt handler?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70302?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 21:50:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cc44309e-cefc-4ae7-86b2-11dac312482e</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Thanks Darren,  yes these changes are included in sdk13.0.0 version. No need for these patch in this release anymore.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70305?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 21:25:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0138b4f-808d-4d9e-bbc8-791c4aee6c38</guid><dc:creator>WestCoastDaz</dc:creator><description>&lt;p&gt;Aryan,&lt;/p&gt;
&lt;p&gt;I think what ahedin25 is asking is have these changes been incorporated into SDK 13 or do we have to update port_cmsis_systick.c and task.c in SDK 13?&lt;/p&gt;
&lt;p&gt;Cheers,
Darren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70304?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 21:11:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f3d372a9-ea5a-4e43-b0fc-fb41aacddf65</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Can you please explain what you mean by that? The current implementation is how it should be&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70306?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 21:01:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a2107437-3a08-4551-b787-4539cc3445ab</guid><dc:creator>ahedin25</dc:creator><description>&lt;p&gt;So is the current implementation in SDK 13.0.0 not what it should be?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70307?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 20:51:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f248ca47-04f4-443b-827e-b49b476b2064</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Hi ahedin25,
Yes, that was the reason i saw interrupts being pended after wakeup from tickless idle sleep and were causing wrong increments of RTOS ticks. Also the latest RTC counter value is sniffed just before this line, so we do not need to count an already pended interrupt because we sniffed the value directly from the RTC counter. The advantage of sniffing the value from RTC counter is the autocorrection capabilities now the tick interrupt has when it is masked for more than a tick period. That is the reason I added the overflow feature of RTC so that we can sniff RTC counter without any side affects.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Please provide some explanation as to
why you switched from incrementing the
tick once using the sleep function
(vTaskAdjustTick) vs. repeatedly
calling xTaskIncrementTick.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Because of the softdevice ability to mask the FreeRTOS tick interrupt to many tick periods, i have noticed that FreeRTOS behaved differently when it is in critical section (disabled freertos interrupts) as it pended the actual increment to be done later after the RTOS came out of critical section. The result of this was that when RTOS came out of critical section, it had to increment the tick multiple times which was quite processor hungry (took more than 1 ms sometimes). Which directly lead to lost ticks. It was clearly not the efficient way to increment the tick multiple times because of how the critical sections for tick update methods are designed. Have you followed the discussion in FreeRTOS thread given in the answer. You can also find it &lt;a href="https://sourceforge.net/p/freertos/discussion/382005/thread/edc6e9f0/"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This change should work fine with or without BLE softdevice, and as far as i see , there are no consequences of this as tick interrupt is running at lowest priority so we do not have to worry about the RTOS asserts as all other contexts are done processing when we are here .&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70303?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 19:44:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:608c8b9f-5270-4ce2-9b19-058040c68781</guid><dc:creator>ahedin25</dc:creator><description>&lt;p&gt;Susheel,&lt;/p&gt;
&lt;p&gt;Please provide more explanation for this snippet (SDK 13.0.0):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    /* It is important that we clear pending here so that our corrections are latest and in sync with tick_interrupt handler */
    NVIC_ClearPendingIRQ(portNRF_RTC_IRQn);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is this because the tick interrupt which is disabled, the event cleared, and then enabled in the sleep function will have always pended an interrupt (with a default tick of 1 ms and and an expected idle time of at least 2 ms)?  I don&amp;#39;t remember the interrupt firing immediately after vPortSuppressTicksAndSleep was exited, but it could be how I was testing it.&lt;/p&gt;
&lt;p&gt;In what situation does this code in xPortSysTickHandler (line ~100) occur?  And what is the reason for it?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#if configUSE_TICKLESS_IDLE == 1
    nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0);
#endif
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Please provide some explanation as to why you switched from incrementing the tick once using the sleep function (vTaskAdjustTick) vs. repeatedly calling xTaskIncrementTick.&lt;/p&gt;
&lt;p&gt;Thank you,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70301?ContentTypeID=1</link><pubDate>Thu, 08 Dec 2016 23:36:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f8fa49e8-f808-4c3e-8568-32a130c39287</guid><dc:creator>WestCoastDaz</dc:creator><description>&lt;p&gt;Hi Aryan,&lt;/p&gt;
&lt;p&gt;No unfortunately I didn&amp;#39;t I was intregrating SDK 12.2 into our system yesterday and I had a multiple include issue that sucked up my day:(&lt;/p&gt;
&lt;p&gt;Also, I was waiting on the further response on the Sourceforge post you made. I reviewed the thread and I am satisfied with the answer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70300?ContentTypeID=1</link><pubDate>Thu, 08 Dec 2016 20:30:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c182953f-b62d-4ac2-99b4-9bb19831a7e9</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Hi Darren, did you get time to test this?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70296?ContentTypeID=1</link><pubDate>Wed, 07 Dec 2016 08:30:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b83f436-181a-4f19-bed4-07c74d94c94d</guid><dc:creator>cp52</dc:creator><description>&lt;p&gt;Is the patch for the nordic SDK 12. or 12.2 or does it not matter?&lt;/p&gt;
&lt;p&gt;Sorry. I cant read I see that the answer is below.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70299?ContentTypeID=1</link><pubDate>Wed, 07 Dec 2016 06:19:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e2711160-07e9-423b-8ec1-624406fca895</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Hi Darren,&lt;/p&gt;
&lt;p&gt;I attached a patch for it. I think it is unfair to make vTaskStepTick available only for tickless mode.
I have created a post in FreeRTOS forum to convey this.&lt;br /&gt;
&lt;a href="https://sourceforge.net/p/freertos/discussion/382005/thread/edc6e9f0/"&gt;Sourceforge post&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Anyways, since we have source code, I have removed this restriction for now by removing the compile flag. This must be agreeable given the nature of our BLE stack.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70298?ContentTypeID=1</link><pubDate>Wed, 07 Dec 2016 04:26:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:20fddaca-fe6e-4cc9-9c54-9dae883d60c7</guid><dc:creator>WestCoastDaz</dc:creator><description>&lt;p&gt;Aryan,&lt;/p&gt;
&lt;p&gt;I tried to compile this new file but I can&amp;#39;t link vTaskStepTick when not using TicklessIDLE.  From your initial post it seems like this should work for either Tick mode and Tickless mode.&lt;/p&gt;
&lt;p&gt;Thanks,
Darren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS changes for better RTOS timer accuracy</title><link>https://devzone.nordicsemi.com/thread/70297?ContentTypeID=1</link><pubDate>Tue, 06 Dec 2016 22:53:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:15bc086b-9b47-4554-9425-9b30a8c56003</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;NOTE. &lt;code&gt;tested on SDK12.x only (might work with other SDKs, but i am not sure)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;apply this patch in SDK\external\freertos folder&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/freertos.diff"&gt;No_Ticks_lost.patch&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Discussion for a clarification for vTaskStepTick is started
&lt;a href="https://sourceforge.net/p/freertos/discussion/382005/thread/edc6e9f0/"&gt;here&lt;/a&gt;&lt;br /&gt;
Update: The change in task.c::vTaskStepTick  is now confirmed by FreeRTOS team to be safe and required in our case&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;EDIT: 19th May 2017&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;DO NOT USE THIS PATCH IN YOUR NEW DESIGNS.&lt;/strong&gt;
better patch available in &lt;a href="https://devzone.nordicsemi.com/question/132346/freertos-rtc-issue-in-port_cmsis_systickc/"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>