[信息]

【实战经验】STM32 RTC不更新原因分析

[复制链接]
2617|4
手机看帖
扫描二维码
随时随地手机跟帖
香水城|  楼主 | 2016-6-28 11:47 | 显示全部楼层 |阅读模式
本帖最后由 香水城 于 2017-8-14 12:51 编辑

STM32 RTC不更新原因分析

问题现象
客户在使用STM32L4 RTC功能的时候,调试时发现日历不再更新,而通过IAR跟踪RTC相关寄存器,同样发现没有更新,奇怪的是,在单步运行时,寄存器是可以更新的。

问题原因分析
跟踪客户代码,发现客户使用Cube库带的HAL函数获取时间:
HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN);
函数本身没有任何问题,通过读RTC相应的寄存器去获取时间,那么问题出在什么地方呢?
通过对比测试,找一块STM32L4的Nucleo单板,运行Cube库自带的例程:
STM32Cube_FW_L4_V1.5.0\Projects\STM32L476RG-Nucleo\Examples\RTC\RTC_TimeStamp
发现Calendar更新正常,而Cube库同样是调用HAL的代码库,通过反复对比示例,发现了其中的问题,在代码库中,在获取时间的同时,同样获取了日期的信息:
HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN); // 获取时间
HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN); // 获取日期
同时获取之后,问题解决,更新正常!

结论
在使用驱动时,需要特别注意原厂提供的官方示例,因为示例都是经过测试验证的,不要因为个人喜好随意对其做更改,以免引起奇怪的错误。

对应PDF: STM32 RTC不更新原因分析
更多实战经验请看:【ST MCU实战经验汇总贴】

mmuuss586| | 2016-6-28 20:28 | 显示全部楼层

使用特权

评论回复
butterf1y| | 2018-10-30 15:21 | 显示全部楼层
/**
  * @brief  Get RTC current time.
  * @param  hrtc: RTC handle
  * @param  sTime: Pointer to Time structure with Hours, Minutes and Seconds fields returned
  *                with input format (BIN or BCD), also SubSeconds field returning the
  *                RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
  *                factor to be used for second fraction ratio computation.
  * @param  Format: Specifies the format of the entered parameters.
  *          This parameter can be one of the following values:
  *            @arg RTC_FORMAT_BIN: Binary data format
  *            @arg RTC_FORMAT_BCD: BCD data format
  * @NOTE  You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  *        value in second fraction ratio with time unit following generic formula:
  *        Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  *        This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  * @NOTE  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  *        Reading RTC current time locks the values in calendar shadow registers until Current date is read
  *        to ensure consistency between the time and date values.
  * @retval HAL status
  */

使用特权

评论回复
junpeng324| | 2018-10-30 19:57 | 显示全部楼层
又是bug吗?

使用特权

评论回复
junpeng324| | 2018-10-30 19:57 | 显示全部楼层
RTC 真的没有研究那么深,能正常跑起来就好了

使用特权

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

本版积分规则

认证:意法半导体(中国)投资有限公司
简介:STM32技术专家

596

主题

17055

帖子

283

粉丝