drv_rtc.c 13 KB

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