prettyprint

2020年10月25日 星期日

STM32 RTC實驗備忘錄

  1. STM32F1有ONE_SECOND interrupt,STM32F4沒有,只能用RTC Wakeup代替。
  2. STM32F4 注意要在HAL_RTC_GetTime之後呼叫HAL_RTC_GetDate才能取得取得數值(浪費一天得到)。在HAL_RTC_GetTime有說明: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.
  3. STM32F4 有ALARM A AND B,STM32F1 只有ALARM A
  4. STM32F1接ST-Link LSE可能電源不過,因此外接電源即可正常。
  5. STM32F1 RTC 在電池模式下(Power off, Standby, low power mode)日期不會更新問題,網路上搜尋解決方法較好為smt32fxx_hal_rtc.c 的RTC_DateUpdate function 下最後加入

uint32_t dateToStore;

   memcpy(&dateToStore,&hrtc->DateToUpdate,sizeof(uint32_t));

   BKP->DR2 = dateToStore >> 16;

   BKP->DR3 = dateToStore & 0xffff;

然後

在main.c的MX_RTC_Init 

判斷若backup register 是否儲存,在restore回日期

/* USER CODE BEGIN Check_RTC_BKUP */

  if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x30)

  {

  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date

  */

.

.

.

/* USER CODE BEGIN RTC_Init 2 */

        uint32_t dateToStore;

       memcpy(&dateToStore,&hrtc->DateToUpdate,sizeof(uint32_t));

       BKP->DR2 = dateToStore >> 16;

       BKP->DR3 = dateToStore & 0xffff;

  HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x30);

}

else

  {

  uint32_t dateMem;

  dateMem = BKP->DR2 << 16;

  dateMem |= BKP->DR3;

  memcpy(&DateToUpdate,&dateMem,sizeof(uint32_t));

  HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN);

  }

/* USER CODE END RTC_Init 2 */

沒有留言:

張貼留言