drv_rtc.c 12 KB

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