drv_rtc.c 13 KB

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