drv_rtc.c 12 KB

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