drv_rtc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #if defined(BSP_RTC_USING_LSI)
  132. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  133. #elif defined(BSP_RTC_USING_LSE)
  134. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  135. #else
  136. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
  137. #endif
  138. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  139. #if defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G0)
  140. __HAL_RCC_RTCAPB_CLK_ENABLE();
  141. #endif
  142. /* Enable RTC Clock */
  143. __HAL_RCC_RTC_ENABLE();
  144. RTC_Handler.Instance = RTC;
  145. if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR1) != BKUP_REG_DATA)
  146. {
  147. LOG_I("RTC hasn't been configured, please use <date> command to config.");
  148. #if defined(SOC_SERIES_STM32F1)
  149. RTC_Handler.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
  150. RTC_Handler.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  151. #elif defined(SOC_SERIES_STM32F0)
  152. /* set the frequency division */
  153. #ifdef BSP_RTC_USING_LSI
  154. RTC_Handler.Init.AsynchPrediv = 0XA0;
  155. RTC_Handler.Init.SynchPrediv = 0xFA;
  156. #else
  157. RTC_Handler.Init.AsynchPrediv = 0X7F;
  158. RTC_Handler.Init.SynchPrediv = 0x0130;
  159. #endif /* BSP_RTC_USING_LSI */
  160. RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  161. RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  162. RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  163. RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  164. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L0) \
  165. || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB) \
  166. || defined(SOC_SERIES_STM32G0)
  167. /* set the frequency division */
  168. #ifdef BSP_RTC_USING_LSI
  169. RTC_Handler.Init.AsynchPrediv = 0X7D;
  170. #else
  171. RTC_Handler.Init.AsynchPrediv = 0X7F;
  172. #endif /* BSP_RTC_USING_LSI */
  173. RTC_Handler.Init.SynchPrediv = 0XFF;
  174. RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  175. RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  176. RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  177. RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  178. #else
  179. #warning "This series doesn't support yet!"
  180. #endif
  181. if (HAL_RTC_Init(&RTC_Handler) != HAL_OK)
  182. {
  183. return -RT_ERROR;
  184. }
  185. }
  186. #ifdef SOC_SERIES_STM32F1
  187. else
  188. {
  189. /* F1 series need update by bkp reg datas */
  190. rt_rtc_f1_bkp_update();
  191. }
  192. #endif
  193. return RT_EOK;
  194. }
  195. static rt_err_t stm32_rtc_init(void)
  196. {
  197. #if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB)
  198. __HAL_RCC_PWR_CLK_ENABLE();
  199. #ifdef SOC_SERIES_STM32F1
  200. __HAL_RCC_BKP_CLK_ENABLE();
  201. #endif
  202. #endif
  203. #if defined(BSP_RTC_USING_LSI) || defined(BSP_RTC_USING_LSE)
  204. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  205. #ifdef BSP_RTC_USING_LSI
  206. #ifdef SOC_SERIES_STM32WB
  207. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
  208. #else
  209. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  210. #endif
  211. RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  212. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  213. #else
  214. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  215. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  216. RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
  217. #endif
  218. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  219. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  220. #endif
  221. if (rt_rtc_config() != RT_EOK)
  222. {
  223. LOG_E("rtc init failed.");
  224. return -RT_ERROR;
  225. }
  226. return RT_EOK;
  227. }
  228. static rt_err_t stm32_rtc_get_secs(time_t *sec)
  229. {
  230. struct timeval tv;
  231. stm32_rtc_get_timeval(&tv);
  232. *(time_t *) sec = tv.tv_sec;
  233. LOG_D("RTC: get rtc_time %d", *sec);
  234. return RT_EOK;
  235. }
  236. static rt_err_t stm32_rtc_set_secs(time_t *sec)
  237. {
  238. rt_err_t result = RT_EOK;
  239. if (set_rtc_time_stamp(*sec))
  240. {
  241. result = -RT_ERROR;
  242. }
  243. LOG_D("RTC: set rtc_time %d", *sec);
  244. #ifdef RT_USING_ALARM
  245. rt_alarm_update(&rtc_device.rtc_dev.parent, 1);
  246. #endif
  247. return result;
  248. }
  249. static rt_err_t stm32_rtc_get_alarm(struct rt_rtc_wkalarm *alarm)
  250. {
  251. #ifdef RT_USING_ALARM
  252. *alarm = rtc_device.wkalarm;
  253. LOG_D("GET_ALARM %d:%d:%d",rtc_device.wkalarm.tm_hour,
  254. rtc_device.wkalarm.tm_min,rtc_device.wkalarm.tm_sec);
  255. return RT_EOK;
  256. #else
  257. return -RT_ERROR;
  258. #endif
  259. }
  260. static rt_err_t stm32_rtc_set_alarm(struct rt_rtc_wkalarm *alarm)
  261. {
  262. #ifdef RT_USING_ALARM
  263. LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM");
  264. if (alarm != RT_NULL)
  265. {
  266. rtc_device.wkalarm.enable = alarm->enable;
  267. rtc_device.wkalarm.tm_hour = alarm->tm_hour;
  268. rtc_device.wkalarm.tm_min = alarm->tm_min;
  269. rtc_device.wkalarm.tm_sec = alarm->tm_sec;
  270. rtc_alarm_time_set(&rtc_device);
  271. }
  272. else
  273. {
  274. LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!");
  275. return -RT_ERROR;
  276. }
  277. LOG_D("SET_ALARM %d:%d:%d",alarm->tm_hour,
  278. alarm->tm_min, alarm->tm_sec);
  279. return RT_EOK;
  280. #else
  281. return -RT_ERROR;
  282. #endif
  283. }
  284. static const struct rt_rtc_ops stm32_rtc_ops =
  285. {
  286. stm32_rtc_init,
  287. stm32_rtc_get_secs,
  288. stm32_rtc_set_secs,
  289. stm32_rtc_get_alarm,
  290. stm32_rtc_set_alarm,
  291. stm32_rtc_get_timeval,
  292. RT_NULL,
  293. };
  294. #ifdef RT_USING_ALARM
  295. void rt_rtc_alarm_enable(void)
  296. {
  297. HAL_RTC_SetAlarm_IT(&RTC_Handler,&Alarm_InitStruct,RTC_FORMAT_BIN);
  298. HAL_RTC_GetAlarm(&RTC_Handler,&Alarm_InitStruct,RTC_ALARM_A,RTC_FORMAT_BIN);
  299. LOG_D("alarm read:%d:%d:%d", Alarm_InitStruct.AlarmTime.Hours,
  300. Alarm_InitStruct.AlarmTime.Minutes,
  301. Alarm_InitStruct.AlarmTime.Seconds);
  302. HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x02, 0);
  303. HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
  304. }
  305. void rt_rtc_alarm_disable(void)
  306. {
  307. HAL_RTC_DeactivateAlarm(&RTC_Handler, RTC_ALARM_A);
  308. HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
  309. }
  310. static int rt_rtc_alarm_init(void)
  311. {
  312. return RT_EOK;
  313. }
  314. static rt_err_t rtc_alarm_time_set(struct rtc_device_object* p_dev)
  315. {
  316. if (p_dev->wkalarm.enable)
  317. {
  318. Alarm_InitStruct.Alarm = RTC_ALARM_A;
  319. Alarm_InitStruct.AlarmTime.Hours = p_dev->wkalarm.tm_hour;
  320. Alarm_InitStruct.AlarmTime.Minutes = p_dev->wkalarm.tm_min;
  321. Alarm_InitStruct.AlarmTime.Seconds = p_dev->wkalarm.tm_sec;
  322. #ifndef SOC_SERIES_STM32F1
  323. Alarm_InitStruct.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
  324. Alarm_InitStruct.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
  325. Alarm_InitStruct.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
  326. Alarm_InitStruct.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
  327. Alarm_InitStruct.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
  328. #endif /* SOC_SERIES_STM32F1 */
  329. LOG_D("alarm set:%d:%d:%d", Alarm_InitStruct.AlarmTime.Hours,
  330. Alarm_InitStruct.AlarmTime.Minutes,
  331. Alarm_InitStruct.AlarmTime.Seconds);
  332. rt_rtc_alarm_enable();
  333. }
  334. return RT_EOK;
  335. }
  336. void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  337. {
  338. //LOG_D("rtc alarm isr.\n");
  339. rt_alarm_update(&rtc_device.rtc_dev.parent, 1);
  340. }
  341. void RTC_Alarm_IRQHandler(void)
  342. {
  343. rt_interrupt_enter();
  344. HAL_RTC_AlarmIRQHandler(&RTC_Handler);
  345. rt_interrupt_leave();
  346. }
  347. #endif
  348. static int rt_hw_rtc_init(void)
  349. {
  350. rt_err_t result;
  351. rtc_device.rtc_dev.ops = &stm32_rtc_ops;
  352. result = rt_hw_rtc_register(&rtc_device.rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  353. if (result != RT_EOK)
  354. {
  355. LOG_E("rtc register err code: %d", result);
  356. return result;
  357. }
  358. LOG_D("rtc init success");
  359. #ifdef RT_USING_ALARM
  360. rt_rtc_alarm_init();
  361. #endif
  362. return RT_EOK;
  363. }
  364. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  365. #endif /* BSP_USING_ONCHIP_RTC */