硬汉嵌入式论坛

 找回密码
 立即注册
查看: 4450|回复: 4
收起左侧

[HAL] RTC精密数字校准

[复制链接]

55

主题

131

回帖

296

积分

高级会员

积分
296
发表于 2021-4-30 16:57:55 | 显示全部楼层 |阅读模式
库函数HAL_RTCEx_SetSmoothCalib实现精密校准,假设RTC一天快了1秒,怎么理解使用这个函数呢?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106726
QQ
发表于 2021-5-1 08:24:55 | 显示全部楼层
看了下这个函数,只有简单的寄存器设置,这个有必要研究下手册了

  1. /**
  2.   * @brief  Set the Smooth calibration parameters.
  3.   * @param  hrtc RTC handle
  4.   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
  5.   *          This parameter can be can be one of the following values :
  6.   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  7.   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  8.   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  9.   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  10.   *          This parameter can be one of the following values:
  11.   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  12.   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  13.   * @param  SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
  14.   *          This parameter can be one any value from 0 to 0x000001FF.
  15.   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  16.   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  17.   *         SmoothCalibMinusPulsesValue must be equal to 0.
  18.   * @retval HAL status
  19.   */
  20. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
  21. {
  22.   uint32_t tickstart;

  23.   /* Check the parameters */
  24.   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  25.   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  26.   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));

  27.   /* Process Locked */
  28.   __HAL_LOCK(hrtc);

  29.   hrtc->State = HAL_RTC_STATE_BUSY;

  30.   /* Disable the write protection for RTC registers */
  31.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);

  32.   /* check if a calibration operation is pending */
  33. #if defined(RTC_ICSR_RECALPF)
  34.   if ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
  35. #endif /* RTC_ICSR_RECALPF */
  36. #if defined(RTC_ISR_RECALPF)
  37.     if ((hrtc->Instance->ISR  & RTC_ISR_RECALPF)  != 0U)
  38. #endif /* RTC_ISR_RECALPF */
  39.     {
  40.       tickstart = HAL_GetTick();

  41.       /* Wait for pending calibration operation to finish */
  42. #if defined(RTC_ICSR_RECALPF)
  43.       while ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
  44. #endif /* RTC_ICSR_RECALPF */
  45. #if defined(RTC_ISR_RECALPF)
  46.         while ((hrtc->Instance->ISR  & RTC_ISR_RECALPF)  != 0U)
  47. #endif /* RTC_ISR_RECALPF */
  48.         {
  49.           if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  50.           {
  51.             /* Enable the write protection for RTC registers */
  52.             __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

  53.             /* Change RTC state */
  54.             hrtc->State = HAL_RTC_STATE_TIMEOUT;

  55.             /* Process Unlocked */
  56.             __HAL_UNLOCK(hrtc);

  57.             return HAL_TIMEOUT;
  58.           }
  59.         }
  60.     }

  61.   /* Configure the Smooth calibration settings */
  62.   MODIFY_REG(hrtc->Instance->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM), (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));

  63.   /* Enable the write protection for RTC registers */
  64.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);

  65.   /* Change RTC state */
  66.   hrtc->State = HAL_RTC_STATE_READY;

  67.   /* Process Unlocked */
  68.   __HAL_UNLOCK(hrtc);

  69.   return HAL_OK;
  70. }
复制代码


回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106726
QQ
发表于 2021-5-1 08:33:55 | 显示全部楼层
找到了,有个专门的软件包和相关文档。
https://www.st.com/en/embedded-software/x-cube-rtc.html
回复

使用道具 举报

210

主题

1043

回帖

1683

积分

至尊会员

More we do, more we can do.

积分
1683
发表于 2021-5-1 14:36:55 | 显示全部楼层
我们是有自己做高精度时钟仪,能到0.01秒/天
回复

使用道具 举报

55

主题

131

回帖

296

积分

高级会员

积分
296
 楼主| 发表于 2021-5-6 15:34:49 | 显示全部楼层
eric2013 发表于 2021-5-1 08:33
找到了,有个专门的软件包和相关文档。
https://www.st.com/en/embedded-software/x-cube-rtc.html

仍然一头雾水,想要那种傻瓜式的,谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-2 13:25 , Processed in 0.277036 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表