<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: RTC seconds not updated properly in Kinetis Microcontrollers</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400643#M22229</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Jeremy,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another experiment shows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; * On our first read, say time was&lt;/P&gt;&lt;P&gt; * 0/7FFF&lt;/P&gt;&lt;P&gt; * &lt;/P&gt;&lt;P&gt; * It is possible that the above read yields:&lt;/P&gt;&lt;P&gt; * 7FFF, then 0 (which is correct)&lt;/P&gt;&lt;P&gt; * or&lt;/P&gt;&lt;P&gt; * 7FFF, then 1&amp;nbsp; --&amp;gt; when second has incremented.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Cecylia&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 13 Jul 2015 19:37:49 GMT</pubDate>
    <dc:creator>cwati</dc:creator>
    <dc:date>2015-07-13T19:37:49Z</dc:date>
    <item>
      <title>RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400637#M22223</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;I'm monitoring the behavior of the second and TPR (Prescaler) of this K22F board.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;I notice that sometimes the Second register (HW_RTC_TSR_RD) hasn't been updated when the Prescaler (HW_RTC_TPR_RD) has.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;Code snippet is as follows:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;static uint32_t last_sec = 0, last_tpr = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;current_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;while (dummy_tpr != current_tpr) {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;dummy_rtc = HW_RTC_TSR_RD(RTC_BASE);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;current_sec = HW_RTC_TSR_RD(RTC_BASE);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;while (dummy_tpr != current_tpr) {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dummy_rtc = HW_RTC_TSR_RD(RTC_BASE);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current_sec = HW_RTC_TSR_RD(RTC_BASE);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;if ((last_sec != 0) &amp;amp;&amp;amp; (last_tpr != 0)) {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((last_sec == current_sec) &amp;amp;&amp;amp; (last_tpr &amp;gt; current_tpr)) {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //This is disaster...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //STOP HERE in DEBUGGER&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;last_sec = current_sec;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;last_tpr = current_tpr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: Arial, sans-serif;"&gt;Note: I re-read registers per this entry:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.nxp.com/message/362746"&gt;RTC counters behavior (seconds and prescaler registers)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I notice that these cases happen sometimes&lt;/P&gt;&lt;P&gt;first read:&lt;BR /&gt;second: 0x2, tpr: 0x7FFF&lt;/P&gt;&lt;P&gt;then next read:&lt;/P&gt;&lt;P&gt;second: 0x2, tpr: 0x0&amp;nbsp; ----&amp;gt; or sometimes even 0x1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Which tells me that most likely TPR has incremented while second hasn't?&lt;/P&gt;&lt;P&gt;Is this a known HW problem? Or, did I miss some settings?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help is appreciated.&amp;nbsp; Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jul 2015 23:54:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400637#M22223</guid>
      <dc:creator>cwati</dc:creator>
      <dc:date>2015-07-10T23:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400638#M22224</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hmpf seems like some sort of race condition&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I read time is, say 0/7FFF&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I read TPR first to get: 7FFF&lt;BR /&gt;By the time I got second, it has incremented (time is now 1/000), so I get: 1&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I end up with 1/7FFF, which is wrong.&amp;nbsp; It should be 0/7FFF or 1/0000.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Cecylia&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Jul 2015 02:15:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400638#M22224</guid>
      <dc:creator>cwati</dc:creator>
      <dc:date>2015-07-11T02:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400639#M22225</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Try temporarily disabling the RTC when reading the prescalter:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RTC_SR = 0; // disable&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;read values&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RTC_SR = RTC_SR_TCE; // re-enable&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kinetis: &lt;A href="http://www.utasker.com/kinetis.html" title="http://www.utasker.com/kinetis.html"&gt;µTasker Kinetis support&lt;/A&gt; &lt;/P&gt;&lt;P&gt;K22: &lt;A href="http://www.utasker.com/kinetis/FRDM-K22F.html" title="http://www.utasker.com/kinetis/FRDM-K22F.html"&gt;µTasker Kinetis FRDM-K22F support&lt;/A&gt;&amp;nbsp; / &lt;A href="http://www.utasker.com/kinetis/TWR-K22F120M.html" title="http://www.utasker.com/kinetis/TWR-K22F120M.html"&gt;µTasker Kinetis TWR-K22F120M support&lt;/A&gt; &lt;/P&gt;&lt;P&gt;RTC/Time Management: &lt;A href="http://www.utasker.com/docs/uTasker/uTasker_Time.pdf" title="http://www.utasker.com/docs/uTasker/uTasker_Time.pdf"&gt;http://www.utasker.com/docs/uTasker/uTasker_Time.pdf&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;EM&gt;For the complete "out-of-the-box" Kinetis experience and faster time to market&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Jul 2015 10:38:37 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400639#M22225</guid>
      <dc:creator>mjbcswitzerland</dc:creator>
      <dc:date>2015-07-11T10:38:37Z</dc:date>
    </item>
    <item>
      <title>Re: RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400640#M22226</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Cecylia,&lt;/P&gt;&lt;P&gt;I'd like to confirm with you about this phenomenon below whether always happens or does sometimes happen.&lt;/P&gt;&lt;P&gt;I'm looking forward to your reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;first read:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;second: 0x0, tpr: 0x7FFF&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;second read:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;second: 0x1, tpr: 1x7FFF&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Have a great day,&lt;BR /&gt;Ping&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Jul 2015 06:46:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400640#M22226</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2015-07-13T06:46:01Z</dc:date>
    </item>
    <item>
      <title>Re: RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400641#M22227</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Jeremy, please look at my previous reply to my own question.&amp;nbsp; It looks like a race condition, so it'd happen every time TPR moves from 0x7FFF to 0.&lt;BR /&gt;If I put a break point, I'd say it get hit every few seconds.&lt;BR /&gt;I'm going to try Mark's solution.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Jul 2015 18:31:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400641#M22227</guid>
      <dc:creator>cwati</dc:creator>
      <dc:date>2015-07-13T18:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400642#M22228</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Mark,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That actually sounds like a very legit solution, but I still hit the breakpoints very often.&lt;BR /&gt;Almost everytime the TPR transitions from 0x7FFF to 0.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code snippet below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Cecylia&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*******************************************************************************&lt;/P&gt;&lt;P&gt; * Get K22F's RTC in milliseconds&lt;/P&gt;&lt;P&gt; ******************************************************************************/&lt;/P&gt;&lt;P&gt;uint32_t k22f_get_rtc(void) {&lt;/P&gt;&lt;P&gt;&amp;nbsp; static uint32_t last_sec = 0, last_tpr = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; uint32_t dummy_tpr, dummy_tpr1, current_tpr;&lt;/P&gt;&lt;P&gt;&amp;nbsp; uint32_t rtc_milliseconds;&lt;/P&gt;&lt;P&gt;&amp;nbsp; uint32_t dummy_rtc, current_sec;&lt;/P&gt;&lt;P&gt;&amp;nbsp; uint32_t cnt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; const uint32_t cnt_max = 0xFF;&lt;/P&gt;&lt;P&gt;&amp;nbsp; uint32_t temp_SR;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; //debug&lt;/P&gt;&lt;P&gt;&amp;nbsp; static uint32_t last_total_ms = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; uint32_t total_ms_diff, current_total_ms;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* WARNING: There will be consequences if you change the order of the reading!&lt;/P&gt;&lt;P&gt;&amp;nbsp; * Make sure you know what you're doing.&lt;/P&gt;&lt;P&gt;&amp;nbsp; *&lt;/P&gt;&lt;P&gt;&amp;nbsp; * For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp; * At time 0 second, 0x7FFF&lt;/P&gt;&lt;P&gt;&amp;nbsp; * Read TPR: get 0x7FFF&lt;/P&gt;&lt;P&gt;&amp;nbsp; * Read second: get 1 (already incremented)&lt;/P&gt;&lt;P&gt;&amp;nbsp; * So you need to fix it.&lt;/P&gt;&lt;P&gt;&amp;nbsp; *&lt;/P&gt;&lt;P&gt;&amp;nbsp; * If you read second first&lt;/P&gt;&lt;P&gt;&amp;nbsp; * second: get 0,&lt;/P&gt;&lt;P&gt;&amp;nbsp; * TPR: get 0 (already incremented).&lt;/P&gt;&lt;P&gt;&amp;nbsp; * Must handle these cases.&lt;/P&gt;&lt;P&gt;&amp;nbsp; */&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; //cw testing todo&lt;/P&gt;&lt;P&gt;&amp;nbsp; // Disable time counters TSR and TPR before writing&lt;/P&gt;&lt;P&gt;&amp;nbsp; temp_SR = BR_RTC_SR_TCE(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; BW_RTC_SR_TCE(RTC_BASE, 0);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* We have to read twice or more until we get same values */&lt;/P&gt;&lt;P&gt;&amp;nbsp; dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; current_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; cnt = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; while (dummy_tpr != current_tpr) {&lt;/P&gt;&lt;P&gt;&amp;nbsp; if (++cnt == cnt_max) {&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* Put a count to avoid infinite loop.&amp;nbsp; Shouldn't arrive here unless HW&lt;/P&gt;&lt;P&gt;&amp;nbsp; * goes kaput. */&lt;/P&gt;&lt;P&gt;&amp;nbsp; return UINT32_MAX;&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp; dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; current_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* We have to read twice or more until we get same values */&lt;/P&gt;&lt;P&gt;&amp;nbsp; dummy_rtc = HW_RTC_TSR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; current_sec = HW_RTC_TSR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; cnt = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; while (dummy_rtc != current_sec) {&lt;/P&gt;&lt;P&gt;&amp;nbsp; if (++cnt == cnt_max) {&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* Put a count to avoid infinite loop.&amp;nbsp; Shouldn't arrive here unless HW&lt;/P&gt;&lt;P&gt;&amp;nbsp; * goes kaput. */&lt;/P&gt;&lt;P&gt;&amp;nbsp; return UINT32_MAX;&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp; dummy_rtc = HW_RTC_TSR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; current_sec = HW_RTC_TSR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// /* Check again.&amp;nbsp; If TPR has gone back to 0, then we should reduce the sec.&lt;/P&gt;&lt;P&gt;// * Meaning, on our first read, say time was&lt;/P&gt;&lt;P&gt;// * 0/7FFF&lt;/P&gt;&lt;P&gt;// * during read we got: 7FFF, then 1. ---&amp;gt; 1/7FFF which is 1 second off&lt;/P&gt;&lt;P&gt;// * now time is actually 1/000&lt;/P&gt;&lt;P&gt;// * We reduce the second by 1 to get: 0/7FFF.&lt;/P&gt;&lt;P&gt;// */&lt;/P&gt;&lt;P&gt;// dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;// dummy_tpr1 = HW_RTC_TPR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;// while (dummy_tpr != dummy_tpr1) {&lt;/P&gt;&lt;P&gt;// if (++cnt == cnt_max) {&lt;/P&gt;&lt;P&gt;// /* Put a count to avoid infinite loop.&amp;nbsp; Shouldn't arrive here unless HW&lt;/P&gt;&lt;P&gt;// * goes kaput. */&lt;/P&gt;&lt;P&gt;// return UINT32_MAX;&lt;/P&gt;&lt;P&gt;// }&lt;/P&gt;&lt;P&gt;// dummy_tpr = HW_RTC_TPR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;// dummy_tpr1 = HW_RTC_TPR_RD(RTC_BASE);&lt;/P&gt;&lt;P&gt;// }&lt;/P&gt;&lt;P&gt;// if (dummy_tpr &amp;lt; current_tpr) {&lt;/P&gt;&lt;P&gt;// current_sec = current_sec - 1;&lt;/P&gt;&lt;P&gt;// }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; // TPR = 1 equals to ~30.5us = 1000000us / 32768Hz = (30.5/1000)ms = (1000/32768)ms&lt;/P&gt;&lt;P&gt;&amp;nbsp; rtc_milliseconds = current_tpr * 1000 / 32768;&lt;/P&gt;&lt;P&gt;&amp;nbsp; current_total_ms = (current_sec * 1000) + rtc_milliseconds;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if (current_total_ms &amp;lt; last_total_ms) {&lt;/P&gt;&lt;P&gt;&amp;nbsp; //dummy.&amp;nbsp; this is not good!&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; dummy_tpr = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ----&amp;gt; [cw] Put a breakpoint here&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp; last_sec = current_sec;&lt;/P&gt;&lt;P&gt;&amp;nbsp; last_tpr = current_tpr;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; last_total_ms = current_total_ms;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; //cw todo re enable Timer&lt;/P&gt;&lt;P&gt;&amp;nbsp; BW_RTC_SR_TCE(RTC_BASE, temp_SR);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; return current_total_ms;&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*******************************************************************************&lt;/P&gt;&lt;P&gt; * Enable K22F's RTC registers (TSR - seconds counter, TPR - prescaler counter&lt;/P&gt;&lt;P&gt; * TSR is incremented every second&lt;/P&gt;&lt;P&gt; * TPR is incremented every 32.768 kHz&lt;/P&gt;&lt;P&gt; ******************************************************************************/&lt;/P&gt;&lt;P&gt;void k22f_enable_rtc() {&lt;/P&gt;&lt;P&gt;&amp;nbsp; // Enable 32.768 kHz oscillator&lt;/P&gt;&lt;P&gt;&amp;nbsp; BW_RTC_CR_OSCE(RTC_BASE, 1);&lt;/P&gt;&lt;P&gt;&amp;nbsp; // Wait 100ms for oscillator output to settle down&lt;/P&gt;&lt;P&gt;&amp;nbsp; OSA_TimeDelay(100);&lt;/P&gt;&lt;P&gt;&amp;nbsp; // Disable time counters TSR and TPR before writing&lt;/P&gt;&lt;P&gt;&amp;nbsp; BW_RTC_SR_TCE(RTC_BASE, 0);&lt;/P&gt;&lt;P&gt;&amp;nbsp; // Clear the RTC_TPR counter&lt;/P&gt;&lt;P&gt;&amp;nbsp; HW_RTC_TPR_CLR(RTC_BASE, 0xFFFFFFFFU);&lt;/P&gt;&lt;P&gt;&amp;nbsp; // Write the RTC_TSR with 1 second value&lt;/P&gt;&lt;P&gt;&amp;nbsp; HW_RTC_TSR_WR(RTC_BASE, 1);&lt;/P&gt;&lt;P&gt;&amp;nbsp; // Enable time counters TSR and TPR after writing&lt;/P&gt;&lt;P&gt;&amp;nbsp; BW_RTC_SR_TCE(RTC_BASE, 1);&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;// Main loop&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //some init&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; k22f_enable_rtc();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; while (true)&lt;/P&gt;&lt;P&gt;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; k22f_get_rtc();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (uint16_t tmp_cnt = 0; tmp_cnt &amp;lt; 0x255; tmp_cnt++) {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //doing other bunch of stuff&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Jul 2015 19:16:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400642#M22228</guid>
      <dc:creator>cwati</dc:creator>
      <dc:date>2015-07-13T19:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400643#M22229</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Jeremy,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another experiment shows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; * On our first read, say time was&lt;/P&gt;&lt;P&gt; * 0/7FFF&lt;/P&gt;&lt;P&gt; * &lt;/P&gt;&lt;P&gt; * It is possible that the above read yields:&lt;/P&gt;&lt;P&gt; * 7FFF, then 0 (which is correct)&lt;/P&gt;&lt;P&gt; * or&lt;/P&gt;&lt;P&gt; * 7FFF, then 1&amp;nbsp; --&amp;gt; when second has incremented.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Cecylia&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Jul 2015 19:37:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400643#M22229</guid>
      <dc:creator>cwati</dc:creator>
      <dc:date>2015-07-13T19:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400644#M22230</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Cecylia,&lt;/P&gt;&lt;P&gt;Thanks for your reply,&lt;/P&gt;&lt;P&gt;I'll appreciate if you can share the whole demo, then I'm going to run the demo on FRDM-K22F for testing.&lt;/P&gt;&lt;P&gt;I'm looking forward to your reply.&lt;/P&gt;&lt;P&gt;And If you get the result by using the Mark's method, please let me know.&lt;BR /&gt;Have a great day,&lt;BR /&gt;Ping&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Jul 2015 09:39:57 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400644#M22230</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2015-07-14T09:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: RTC seconds not updated properly</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400645#M22231</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Jeremy, sorry I can't share the demo as it contains proprietary functions.&lt;BR /&gt;The functions I pasted up there should be enough.&amp;nbsp; You can create your own demo, and if it works then you can share here.&amp;nbsp; But, as far as I'm concerned there seems to be an obvious problem with the hardware being inconsistent in updating its registers' values.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark's method doesn't work.&amp;nbsp; I'm creating my own software function to make sure at least the timer doesn't revert back in time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Cecylia&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jul 2015 23:46:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/RTC-seconds-not-updated-properly/m-p/400645#M22231</guid>
      <dc:creator>cwati</dc:creator>
      <dc:date>2015-07-21T23:46:06Z</dc:date>
    </item>
  </channel>
</rss>

