打印
[Kinetis]

【经验分享】RTC_TSR寄存器读出不正确,咋回事?

[复制链接]
1735|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 FSL_TICS_A 于 2014-4-23 10:12 编辑

客户问题介绍:
·       芯片型号:MK60DN512ZVMD10
·       描述:在程序中,连续不断的读取RTC_TSR寄存器值,并将结果传给上位机,但发现连续读出的值出现前后不匹配的问题,从理论上分析,RTC_TSR寄存器内的值应该逐渐递增的,但从打印的输出结果看,存在个别前后不一致的现象(如图1所示),所以怀疑这是不是芯片的Bug呢?


图1
解决过程:
        在开始阶段,在确认客户代码(如表1)无误后,在TWR-K60D100开发板上调试,出现了与客户相似的问题,当时怀疑:1.可能这是个芯片的BUG;2。猜测RTC_TSR寄存器的值在递增后的最初阶段是不稳定的,如果刚好这时读取到的RTC_TSR寄存器值,那么得到的值是不准确的。
表1

#include  <stdio.h>
  
#include  <drivers/uart/uart.h>
  
#include  "cmd_rtcdebug.h"
  
#include  "common.h"
   
  
extern  "C"
  {
  
#include  "wdog.h"
  }
   
  
extern  "C"
  
int cmd_rtcdebug(const  char *args)
  {
              uint32_t lowestValue;
              uint32_t lastFailValue = 0;
              uint32_t failCountInRow = 0;
      int wdCount = 0;
   
              lowestValue = RTC_TSR;
   
  
            printf("press any key to  exit\r\n");
   
              while(1)
              {
  
                        uint32_t nextValue =  RTC_TSR;
   
                          if (uart_kbhit())
                          {
                                      break;
                          }
   
  
                        if (nextValue <  lowestValue)
                          {
  
                                    if  (lastFailValue != nextValue)
                                      {
  
                                                printf("RTC  fail: lowest value: %u, next value: %u\r\n", lowestValue, nextValue);
                                      }
  
                                    lastFailValue  = nextValue;
                                      failCountInRow++;
                          }
  
                        else if (nextValue ==  lowestValue)
                          {
  
                                    if  (failCountInRow)
                                      {
  
                                                printf("RTC  fails in row: %u\r\n", failCountInRow);
  
                                                failCountInRow  = 0;
                                      }
                          }
  
                        else if (nextValue  > lowestValue)
                          {
  
                                    if  (failCountInRow)
                                      {
  
                                                printf("RTC  fails in row: %u\r\n", failCountInRow);
  
                                                failCountInRow  = 0;
                                      }
   
              if (wdCount > 300)
              {
                  wdog_refresh();
                  wdCount = 0;
              }
              wdCount++;
              
  
                                    printf("RTC_TSR:  %u\r\n", nextValue);
  
                                    lowestValue  = nextValue;
                          }
              }
   
              return 0;
  }
  

         针对第一个猜测,在查阅Errata文档后,大致可以排除;至于第二个猜测,我应对的方法是在每次读取RTC_TSR寄存器之前,添加一个延时函数,经过一段时间的测试后,发现出错的频率虽然没有原来那么多,但还是会出现,看来这是个治标不本的方法啊,只能另想方法啊。
          各位网友有没有好的想法,欢迎来讨论!

相关帖子

沙发
FSL_TICS_ZJJ| | 2014-4-23 10:59 | 只看该作者
大家有兴趣的可以参与讨论,我们后续会揭晓答案。

使用特权

评论回复
板凳
dongshan| | 2014-4-23 11:26 | 只看该作者
什么情况都不清楚,就不叫讨论。我看是瞎猜。
是不是中断造成的,在读秒寄存过程中,被中断了。。。

使用特权

评论回复
地板
FSL_TICS_Jeremy|  楼主 | 2014-4-23 14:20 | 只看该作者
dongshan 发表于 2014-4-23 11:26
什么情况都不清楚,就不叫讨论。我看是瞎猜。
是不是中断造成的,在读秒寄存过程中,被中断了。。。 ...

可能我说的不清楚让你误会啊,上面的代码就是客户代码,在其代码中没有其他模块的操作,只要RTC寄存器读取。而最上方图片为结果的打印输出。

使用特权

评论回复
5
FSL_TICS_Jeremy|  楼主 | 2014-4-30 10:19 | 只看该作者
    看来大家的讨论不积极啊,答案揭晓!
       在咨询过AE部门后,发现问题的根源是K60的参考没有给出读取RTCRTC_TSR需要注意的细节。而在K64的参考手册却有这么一段话(如下所示),而问题也就迎仍而解啊!

  Reading the time  counter (either seconds or prescaler) while is  incrementing may return invalid data due to  synchronization of the read data bus. If it is necessary for software to read  the prescaler or seconds counter when they could be incrementing, it is  recommended that two  read accesses are performed and that software verifies that the same data was  returned for both reads.  

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

113

主题

2865

帖子

38

粉丝