<?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>k_uptime_get() drift way too much using LFXO.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/117899/k_uptime_get-drift-way-too-much-using-lfxo</link><description>Hi 
 
 This project is using nRF52840 and NCS 2.5.2, using LFXO as LFCLK source. 
 I added a code snippet which will be called once a button is pressed 
 
 You can see that the delta between k_uptime_get() and RTT Viewer&amp;#39;s timestamp increase to 738 ms</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 11 Jan 2025 03:07:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/117899/k_uptime_get-drift-way-too-much-using-lfxo" /><item><title>RE: k_uptime_get() drift way too much using LFXO.</title><link>https://devzone.nordicsemi.com/thread/518036?ContentTypeID=1</link><pubDate>Sat, 11 Jan 2025 03:07:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:86490eec-a372-4fcb-9096-f025b29862f7</guid><dc:creator>Anthony Yuan</dc:creator><description>&lt;p&gt;Hi H&amp;aring;kon,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for the update.&lt;/p&gt;
&lt;p&gt;1. I use clock_skew to test RC32k Skew and it &amp;lt; 50 ppm&lt;/p&gt;
&lt;p&gt;2. I put sometime read the zephyr kernel code and figure it out what cause my issue:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;My project in NCS 2.3.0, no time drift&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//generated autoconf.h
#define CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC 32768
#define CONFIG_SYS_CLOCK_TICKS_PER_SEC 32768&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;My project in NCS 2.5.2, have time drift&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//generated autoconf.h
#define CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC 32768
#define CONFIG_SYS_CLOCK_TICKS_PER_SEC 1000&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;cycles&amp;nbsp;per&amp;nbsp;tick&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt; = CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC = 32768 / 1000 = &lt;span class="mord"&gt;32.768 &lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;cycles&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Since the hardware cannot generate fractional cycles, this value is typically rounded up or down to the nearest integer, such as 32 cycles or 33 cycles. And Tick period of kernel is 1 ms.&lt;/p&gt;
&lt;p&gt;There may be cumulative timing errors due to rounding, as 32.768 is not an exact match for the hardware clock cycles.&lt;/p&gt;
&lt;p&gt;After change the CONFIG_SYS_CLOCK_TICKS_PER_SEC to 1024, Tick period change to &lt;span class="mord"&gt;0.97656&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;ms&lt;/span&gt;&lt;/span&gt; and cycles per tick is 32 and there is no need to rounding, and that eliminate the time drift.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Anthony Yuan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: k_uptime_get() drift way too much using LFXO.</title><link>https://devzone.nordicsemi.com/thread/517993?ContentTypeID=1</link><pubDate>Fri, 10 Jan 2025 15:26:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:19c9402f-bf73-4e69-b842-056e0a7585f4</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;You are changing the prescaler for the NRF_RTC instance that is used by the kernel for timekeeping by changing that specific kconfig.&lt;br /&gt;&lt;br /&gt;Could you try to run the sample in zephyr/samples/boards/nordic/clock_skew ?&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Then&amp;nbsp;add the 32k RC config:&lt;br /&gt;&lt;br /&gt;CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y&lt;br /&gt;&amp;nbsp;&lt;br /&gt;And add this to the startup sequence to use HFXO:&lt;br /&gt;&lt;br /&gt;&lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/v2.8.0/samples/cellular/at_client/src/main.c#L18-L23"&gt;https://github.com/nrfconnect/sdk-nrf/blob/v2.8.0/samples/cellular/at_client/src/main.c#L18-L23&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;My output running on RC32k:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Ty  Latest           Base             Span             Err
HF  00:03:40.572049  00:00:00.017209  00:03:40.554840
LF  00:03:40.577789  00:00:00.033599  00:03:40.544189 -00:00:00.010651
RHF 00:03:40.572019                                   -00:00:00.000030
Skew 1.000048 ; err -48279 ppb

Ty  Latest           Base             Span             Err
HF  00:03:50.597951  00:00:00.017209  00:03:50.580741
LF  00:03:50.603271  00:00:00.033599  00:03:50.569671 -00:00:00.011070
RHF 00:03:50.597928                                   -00:00:00.000022
Skew 1.000048 ; err -48041 ppb

Ty  Latest           Base             Span             Err
HF  00:04:00.623685  00:00:00.017209  00:04:00.606475
LF  00:04:00.628753  00:00:00.033599  00:04:00.595153 -00:00:00.011322
RHF 00:04:00.623685                                    00:00:00.000000
Skew 1.000047 ; err -47087 ppb&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;lt; 50 ppm drift.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Changing to LFXO gives &amp;lt;2 ppm initial drift:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Ty  Latest           Base             Span             Err
HF  00:00:10.026283  00:00:00.017210  00:00:10.009073
LF  00:00:10.523406  00:00:00.514312  00:00:10.009094  00:00:00.000021
RHF 00:00:10.026273                                   -00:00:00.000009
Skew 0.999998 ; err 2086 ppb&lt;/pre&gt;&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&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: k_uptime_get() drift way too much using LFXO.</title><link>https://devzone.nordicsemi.com/thread/517825?ContentTypeID=1</link><pubDate>Fri, 10 Jan 2025 02:11:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4936080b-c4e5-4a70-b1d8-ebff9a1f423d</guid><dc:creator>Anthony Yuan</dc:creator><description>&lt;p&gt;Hi H&amp;aring;kon,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Just tested with CONFIG_SYS_CLOCK_TICKS_PER_SEC=1024 in .conf file&lt;/p&gt;
&lt;p&gt;And now the k_uptime_get() matching with RTT log and matching with my stop watch.&lt;/p&gt;
&lt;p&gt;I am not quite familiar with zephyr/kernel, I have 2 questions about CONFIG_SYS_CLOCK_TICKS_PER_SEC:&lt;/p&gt;
&lt;p&gt;1. I can&amp;#39;t find out why NCS 2.3.0 and NCS 2.5.2 use the same CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 but have different result.&lt;/p&gt;
&lt;p&gt;2. I change CONFIG_SYS_CLOCK_TICKS_PER_SEC=2048 and the k_uptime_get() works properly too, as my understanding that it will make zephyr kernel more accurate if I set CONFIG_SYS_CLOCK_TICKS_PER_SEC=32768, but is there any side effects?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Anthony Yuan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: k_uptime_get() drift way too much using LFXO.</title><link>https://devzone.nordicsemi.com/thread/517750?ContentTypeID=1</link><pubDate>Thu, 09 Jan 2025 14:59:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1871f57f-b03d-4c8b-a11b-2ada9aa66575</guid><dc:creator>Anthony Yuan</dc:creator><description>&lt;p&gt;Thanks for your reply H&amp;aring;kon,&lt;/p&gt;
&lt;p&gt;I will try your suggestion tomorrow.&lt;/p&gt;
&lt;p&gt;And I realize that there is a pattern in the data:&lt;/p&gt;
&lt;p&gt;I use the RTT timestamp as &amp;quot;total&amp;quot; and you can see the &amp;quot;delta/total&amp;quot; is linear relative and close to 0.024&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;total&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;delta&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;delta/total&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;9706&lt;/td&gt;
&lt;td&gt;233&lt;/td&gt;
&lt;td&gt;0.02400577&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;17055&lt;/td&gt;
&lt;td&gt;409&lt;/td&gt;
&lt;td&gt;0.02398124&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;20708&lt;/td&gt;
&lt;td&gt;497&lt;/td&gt;
&lt;td&gt;0.02400039&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;23586&lt;/td&gt;
&lt;td&gt;566&lt;/td&gt;
&lt;td&gt;0.02399729&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;25461&lt;/td&gt;
&lt;td&gt;611&lt;/td&gt;
&lt;td&gt;0.02399749&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;27160&lt;/td&gt;
&lt;td&gt;652&lt;/td&gt;
&lt;td&gt;0.02400589&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;30735&lt;/td&gt;
&lt;td&gt;738&lt;/td&gt;
&lt;td&gt;0.02401171&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;0.024 == (1024 -1000)/1000&lt;/p&gt;
&lt;p&gt;Maybe I should change&lt;/p&gt;
&lt;p&gt;CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000&lt;/p&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;p&gt;CONFIG_SYS_CLOCK_TICKS_PER_SEC=1024&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: k_uptime_get() drift way too much using LFXO.</title><link>https://devzone.nordicsemi.com/thread/517746?ContentTypeID=1</link><pubDate>Thu, 09 Jan 2025 14:51:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8fa62226-6771-4fd3-91d9-145ec3d6e03b</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;Now you are introducing uncertainty in the RTT process itself, so this is not a very reliable way of checking the accuracy.&lt;/p&gt;
&lt;p&gt;What I would recommend is to check the timestamp against a known stable high speed timer on the nRF itself, as this is sourced from the HFCLK.&lt;/p&gt;
&lt;p&gt;What is important then is to first start the HFXO, either via clock_control API, or by just running this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART=1;
while(NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0);
NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED = 0;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>