drv_rtc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-04 balanceTWK first version
  9. * 2020-10-14 PeakRacing Porting for stm32wbxx
  10. * 2021-02-05 Meco Man fix the problem of mixing local time and UTC time
  11. * 2021-07-05 iysheng implement RTC framework V2.0
  12. * 2025-06-05 RCSN add local time conversion for get timeval and set stamp
  13. * 0206-02-03 wdfk_prog compute tv_usec from SecondFraction/SubSeconds
  14. */
  15. #include "board.h"
  16. #include <sys/time.h>
  17. #include <rtdevice.h>
  18. #include <drv_common.h>
  19. #ifdef BSP_USING_ONCHIP_RTC
  20. #ifndef RTC_BKP_DR1
  21. #define RTC_BKP_DR1 RT_NULL
  22. #endif
  23. /* #define DRV_DEBUG*/
  24. #define LOG_TAG "drv.rtc"
  25. #include <drv_log.h>
  26. #define BKUP_REG_DATA 0xA5A5
  27. struct rtc_device_object
  28. {
  29. rt_rtc_dev_t rtc_dev;
  30. #ifdef RT_USING_ALARM
  31. struct rt_rtc_wkalarm wkalarm;
  32. #endif
  33. };
  34. #ifdef RT_USING_ALARM
  35. static rt_err_t rtc_alarm_time_set(struct rtc_device_object* p_dev);
  36. static int rt_rtc_alarm_init(void);
  37. static RTC_AlarmTypeDef Alarm_InitStruct = { 0 };
  38. #endif
  39. static struct rtc_device_object rtc_device;
  40. static RTC_HandleTypeDef RTC_Handler;
  41. #ifdef SOC_SERIES_STM32H7
  42. rt_weak uint32_t HAL_RTCEx_BKUPRead(const RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  43. #else
  44. rt_weak uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  45. #endif
  46. {
  47. return (~BKUP_REG_DATA);
  48. }
  49. #ifdef SOC_SERIES_STM32H7
  50. rt_weak void HAL_RTCEx_BKUPWrite(const RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  51. #else
  52. rt_weak void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  53. #endif
  54. {
  55. return;
  56. }
  57. static rt_err_t stm32_rtc_get_timeval(struct timeval *tv)
  58. {
  59. RTC_TimeTypeDef RTC_TimeStruct = {0};
  60. RTC_DateTypeDef RTC_DateStruct = {0};
  61. struct tm tm_new = {0};
  62. HAL_RTC_GetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN);
  63. HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN);
  64. tm_new.tm_sec = RTC_TimeStruct.Seconds;
  65. tm_new.tm_min = RTC_TimeStruct.Minutes;
  66. tm_new.tm_hour = RTC_TimeStruct.Hours;
  67. tm_new.tm_mday = RTC_DateStruct.Date;
  68. tm_new.tm_mon = RTC_DateStruct.Month - 1;
  69. tm_new.tm_year = RTC_DateStruct.Year + 100;
  70. #ifdef RT_ALARM_USING_LOCAL_TIME
  71. tv->tv_sec = mktime(&tm_new);
  72. #else
  73. tv->tv_sec = timegm(&tm_new);
  74. #endif
  75. tv->tv_usec = 0U;
  76. /* F1 RTC does not have SSR/PRER */
  77. #if defined(RTC_SSR_SS) && defined(RTC_PRER_PREDIV_S)
  78. /*
  79. * You can use SubSeconds and SecondFraction (sTime structure fields
  80. * returned) to convert SubSeconds value in second fraction ratio with
  81. * time unit following generic formula:
  82. * Second fraction ratio * time_unit =
  83. * [(SecondFraction - SubSeconds) / (SecondFraction + 1)] * time_unit
  84. * This conversion can be performed only if no shift operation is pending
  85. * (ie. SHFP=0) when PREDIV_S >= SS
  86. */
  87. #if defined(RTC_ISR_SHPF)
  88. if (READ_BIT(RTC->ISR, RTC_ISR_SHPF) == 0U)
  89. #endif /* RTC_ISR_SHPF */
  90. {
  91. uint32_t sf = RTC_TimeStruct.SecondFraction;
  92. uint32_t ss = RTC_TimeStruct.SubSeconds;
  93. if ((sf != 0U) && (ss <= sf))
  94. {
  95. tv->tv_usec = (uint32_t)(((sf - ss) * 1000000ULL) / (sf + 1U));
  96. }
  97. }
  98. #endif /* defined(RTC_SSR_SS) && defined(RTC_PRER_PREDIV_S) */
  99. return RT_EOK;
  100. }
  101. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  102. {
  103. RTC_TimeTypeDef RTC_TimeStruct = {0};
  104. RTC_DateTypeDef RTC_DateStruct = {0};
  105. struct tm tm = {0};
  106. #ifdef RT_ALARM_USING_LOCAL_TIME
  107. localtime_r(&time_stamp,&tm);
  108. #else
  109. gmtime_r(&time_stamp, &tm);
  110. #endif
  111. if (tm.tm_year < 100)
  112. {
  113. return -RT_ERROR;
  114. }
  115. RTC_TimeStruct.Seconds = tm.tm_sec ;
  116. RTC_TimeStruct.Minutes = tm.tm_min ;
  117. RTC_TimeStruct.Hours = tm.tm_hour;
  118. RTC_DateStruct.Date = tm.tm_mday;
  119. RTC_DateStruct.Month = tm.tm_mon + 1 ;
  120. RTC_DateStruct.Year = tm.tm_year - 100;
  121. RTC_DateStruct.WeekDay = tm.tm_wday + 1;
  122. if (HAL_RTC_SetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN) != HAL_OK)
  123. {
  124. return -RT_ERROR;
  125. }
  126. if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
  127. {
  128. return -RT_ERROR;
  129. }
  130. LOG_D("set rtc time.");
  131. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);
  132. #ifdef SOC_SERIES_STM32F1
  133. /* F1 series does't save year/month/date datas. so keep those datas to bkp reg */
  134. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year);
  135. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month);
  136. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date);
  137. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay);
  138. #endif
  139. return RT_EOK;
  140. }
  141. #ifdef SOC_SERIES_STM32F1
  142. /* update RTC_BKP_DRx*/
  143. static void rt_rtc_f1_bkp_update(void)
  144. {
  145. RTC_DateTypeDef RTC_DateStruct = {0};
  146. HAL_PWR_EnableBkUpAccess();
  147. RTC_DateStruct.Year = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR2);
  148. RTC_DateStruct.Month = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR3);
  149. RTC_DateStruct.Date = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4);
  150. RTC_DateStruct.WeekDay = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR5);
  151. if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
  152. {
  153. Error_Handler();
  154. }
  155. HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN);
  156. if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4) != RTC_DateStruct.Date)
  157. {
  158. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);
  159. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year);
  160. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month);
  161. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date);
  162. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay);
  163. }
  164. }
  165. #endif
  166. static rt_err_t rt_rtc_config(void)
  167. {
  168. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  169. HAL_PWR_EnableBkUpAccess();
  170. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  171. #if defined(BSP_RTC_USING_LSI)
  172. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  173. #elif defined(BSP_RTC_USING_LSE)
  174. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  175. #else
  176. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
  177. #endif
  178. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  179. #if defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G0)
  180. __HAL_RCC_RTCAPB_CLK_ENABLE();
  181. #endif
  182. /* Enable RTC Clock */
  183. __HAL_RCC_RTC_ENABLE();
  184. RTC_Handler.Instance = RTC;
  185. if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR1) != BKUP_REG_DATA)
  186. {
  187. LOG_I("RTC hasn't been configured, please use <date> command to config.");
  188. #if defined(SOC_SERIES_STM32F1)
  189. RTC_Handler.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
  190. RTC_Handler.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  191. #elif defined(SOC_SERIES_STM32F0)
  192. /* set the frequency division */
  193. #ifdef BSP_RTC_USING_LSI
  194. RTC_Handler.Init.AsynchPrediv = 0XA0;
  195. RTC_Handler.Init.SynchPrediv = 0xFA;
  196. #else
  197. RTC_Handler.Init.AsynchPrediv = 0X7F;
  198. RTC_Handler.Init.SynchPrediv = 0x0130;
  199. #endif /* BSP_RTC_USING_LSI */
  200. RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  201. RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  202. RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  203. RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  204. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L0) \
  205. || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB) \
  206. || defined(SOC_SERIES_STM32G0)
  207. /* set the frequency division */
  208. #ifdef BSP_RTC_USING_LSI
  209. RTC_Handler.Init.AsynchPrediv = 0X7D;
  210. #else
  211. RTC_Handler.Init.AsynchPrediv = 0X7F;
  212. #endif /* BSP_RTC_USING_LSI */
  213. RTC_Handler.Init.SynchPrediv = 0XFF;
  214. RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  215. RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  216. RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  217. RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  218. #else
  219. #warning "This series doesn't support yet!"
  220. #endif
  221. if (HAL_RTC_Init(&RTC_Handler) != HAL_OK)
  222. {
  223. return -RT_ERROR;
  224. }
  225. }
  226. #ifdef SOC_SERIES_STM32F1
  227. else
  228. {
  229. /* F1 series need update by bkp reg datas */
  230. rt_rtc_f1_bkp_update();
  231. }
  232. #endif
  233. return RT_EOK;
  234. }
  235. static rt_err_t stm32_rtc_init(void)
  236. {
  237. #if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB)
  238. __HAL_RCC_PWR_CLK_ENABLE();
  239. #ifdef SOC_SERIES_STM32F1
  240. __HAL_RCC_BKP_CLK_ENABLE();
  241. #endif
  242. #endif
  243. #if defined(BSP_RTC_USING_LSI) || defined(BSP_RTC_USING_LSE)
  244. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  245. #ifdef BSP_RTC_USING_LSI
  246. #ifdef SOC_SERIES_STM32WB
  247. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
  248. #else
  249. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  250. #endif
  251. RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  252. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  253. #else
  254. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  255. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  256. RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
  257. #endif
  258. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  259. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  260. #endif
  261. if (rt_rtc_config() != RT_EOK)
  262. {
  263. LOG_E("rtc init failed.");
  264. return -RT_ERROR;
  265. }
  266. return RT_EOK;
  267. }
  268. static rt_err_t stm32_rtc_get_secs(time_t *sec)
  269. {
  270. struct timeval tv;
  271. stm32_rtc_get_timeval(&tv);
  272. *(time_t *) sec = tv.tv_sec;
  273. LOG_D("RTC: get rtc_time %d", *sec);
  274. return RT_EOK;
  275. }
  276. static rt_err_t stm32_rtc_set_secs(time_t *sec)
  277. {
  278. rt_err_t result = RT_EOK;
  279. if (set_rtc_time_stamp(*sec))
  280. {
  281. result = -RT_ERROR;
  282. }
  283. LOG_D("RTC: set rtc_time %d", *sec);
  284. #ifdef RT_USING_ALARM
  285. rt_alarm_update(&rtc_device.rtc_dev.parent, 1);
  286. #endif
  287. return result;
  288. }
  289. static rt_err_t stm32_rtc_get_alarm(struct rt_rtc_wkalarm *alarm)
  290. {
  291. #ifdef RT_USING_ALARM
  292. *alarm = rtc_device.wkalarm;
  293. LOG_D("GET_ALARM %d:%d:%d",rtc_device.wkalarm.tm_hour,
  294. rtc_device.wkalarm.tm_min,rtc_device.wkalarm.tm_sec);
  295. return RT_EOK;
  296. #else
  297. return -RT_ERROR;
  298. #endif
  299. }
  300. static rt_err_t stm32_rtc_set_alarm(struct rt_rtc_wkalarm *alarm)
  301. {
  302. #ifdef RT_USING_ALARM
  303. LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM");
  304. if (alarm != RT_NULL)
  305. {
  306. rtc_device.wkalarm.enable = alarm->enable;
  307. rtc_device.wkalarm.tm_hour = alarm->tm_hour;
  308. rtc_device.wkalarm.tm_min = alarm->tm_min;
  309. rtc_device.wkalarm.tm_sec = alarm->tm_sec;
  310. /* must include the year, month, and day */
  311. /* as the alarm in RT_ALARM_ONESHOT mode compares the current timestamp with the alarm timestamp */
  312. rtc_device.wkalarm.tm_year = alarm->tm_year;
  313. rtc_device.wkalarm.tm_mon = alarm->tm_mon;
  314. rtc_device.wkalarm.tm_mday = alarm->tm_mday;
  315. rtc_alarm_time_set(&rtc_device);
  316. }
  317. else
  318. {
  319. LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!");
  320. return -RT_ERROR;
  321. }
  322. LOG_D("SET_ALARM %d:%d:%d",alarm->tm_hour,
  323. alarm->tm_min, alarm->tm_sec);
  324. return RT_EOK;
  325. #else
  326. return -RT_ERROR;
  327. #endif
  328. }
  329. static const struct rt_rtc_ops stm32_rtc_ops =
  330. {
  331. stm32_rtc_init,
  332. stm32_rtc_get_secs,
  333. stm32_rtc_set_secs,
  334. stm32_rtc_get_alarm,
  335. stm32_rtc_set_alarm,
  336. stm32_rtc_get_timeval,
  337. RT_NULL,
  338. };
  339. #ifdef RT_USING_ALARM
  340. void rt_rtc_alarm_enable(void)
  341. {
  342. HAL_RTC_SetAlarm_IT(&RTC_Handler,&Alarm_InitStruct,RTC_FORMAT_BIN);
  343. HAL_RTC_GetAlarm(&RTC_Handler,&Alarm_InitStruct,RTC_ALARM_A,RTC_FORMAT_BIN);
  344. LOG_D("alarm read:%d:%d:%d", Alarm_InitStruct.AlarmTime.Hours,
  345. Alarm_InitStruct.AlarmTime.Minutes,
  346. Alarm_InitStruct.AlarmTime.Seconds);
  347. HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x02, 0);
  348. HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
  349. }
  350. void rt_rtc_alarm_disable(void)
  351. {
  352. HAL_RTC_DeactivateAlarm(&RTC_Handler, RTC_ALARM_A);
  353. HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
  354. }
  355. static int rt_rtc_alarm_init(void)
  356. {
  357. return RT_EOK;
  358. }
  359. static rt_err_t rtc_alarm_time_set(struct rtc_device_object* p_dev)
  360. {
  361. if (p_dev->wkalarm.enable)
  362. {
  363. Alarm_InitStruct.Alarm = RTC_ALARM_A;
  364. Alarm_InitStruct.AlarmTime.Hours = p_dev->wkalarm.tm_hour;
  365. Alarm_InitStruct.AlarmTime.Minutes = p_dev->wkalarm.tm_min;
  366. Alarm_InitStruct.AlarmTime.Seconds = p_dev->wkalarm.tm_sec;
  367. #ifndef SOC_SERIES_STM32F1
  368. Alarm_InitStruct.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
  369. Alarm_InitStruct.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
  370. Alarm_InitStruct.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
  371. Alarm_InitStruct.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
  372. Alarm_InitStruct.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
  373. #endif /* SOC_SERIES_STM32F1 */
  374. LOG_D("alarm set:%d:%d:%d", Alarm_InitStruct.AlarmTime.Hours,
  375. Alarm_InitStruct.AlarmTime.Minutes,
  376. Alarm_InitStruct.AlarmTime.Seconds);
  377. rt_rtc_alarm_enable();
  378. }
  379. return RT_EOK;
  380. }
  381. void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  382. {
  383. /*LOG_D("rtc alarm isr.\n");*/
  384. rt_alarm_update(&rtc_device.rtc_dev.parent, 1);
  385. }
  386. void RTC_Alarm_IRQHandler(void)
  387. {
  388. rt_interrupt_enter();
  389. HAL_RTC_AlarmIRQHandler(&RTC_Handler);
  390. rt_interrupt_leave();
  391. }
  392. #endif
  393. static int rt_hw_rtc_init(void)
  394. {
  395. rt_err_t result;
  396. rtc_device.rtc_dev.ops = &stm32_rtc_ops;
  397. result = rt_hw_rtc_register(&rtc_device.rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  398. if (result != RT_EOK)
  399. {
  400. LOG_E("rtc register err code: %d", result);
  401. return result;
  402. }
  403. LOG_D("rtc init success");
  404. #ifdef RT_USING_ALARM
  405. rt_rtc_alarm_init();
  406. #endif
  407. return RT_EOK;
  408. }
  409. INIT_BOARD_EXPORT(rt_hw_rtc_init);
  410. #endif /* BSP_USING_ONCHIP_RTC */