drv_rtc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2006-2021, 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. #ifdef BSP_USING_ONCHIP_RTC
  16. #ifndef RTC_BKP_DR1
  17. #define RTC_BKP_DR1 RT_NULL
  18. #endif
  19. //#define DRV_DEBUG
  20. #define LOG_TAG "drv.rtc"
  21. #include <drv_log.h>
  22. #define BKUP_REG_DATA 0xA5A5
  23. static RTC_HandleTypeDef RTC_Handler;
  24. RT_WEAK uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  25. {
  26. return (~BKUP_REG_DATA);
  27. }
  28. RT_WEAK void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  29. {
  30. return;
  31. }
  32. static rt_err_t stm32_rtc_get_timeval(struct timeval *tv)
  33. {
  34. RTC_TimeTypeDef RTC_TimeStruct = {0};
  35. RTC_DateTypeDef RTC_DateStruct = {0};
  36. struct tm tm_new = {0};
  37. HAL_RTC_GetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN);
  38. HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN);
  39. tm_new.tm_sec = RTC_TimeStruct.Seconds;
  40. tm_new.tm_min = RTC_TimeStruct.Minutes;
  41. tm_new.tm_hour = RTC_TimeStruct.Hours;
  42. tm_new.tm_mday = RTC_DateStruct.Date;
  43. tm_new.tm_mon = RTC_DateStruct.Month - 1;
  44. tm_new.tm_year = RTC_DateStruct.Year + 100;
  45. tv->tv_sec = timegm(&tm_new);
  46. #if defined(SOC_SERIES_STM32H7)
  47. tv->tv_usec = (255.0 - RTC_TimeStruct.SubSeconds * 1.0) / 256.0 * 1000.0 * 1000.0;
  48. #endif
  49. return RT_EOK;
  50. }
  51. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  52. {
  53. RTC_TimeTypeDef RTC_TimeStruct = {0};
  54. RTC_DateTypeDef RTC_DateStruct = {0};
  55. struct tm tm = {0};
  56. gmtime_r(&time_stamp, &tm);
  57. if (tm.tm_year < 100)
  58. {
  59. return -RT_ERROR;
  60. }
  61. RTC_TimeStruct.Seconds = tm.tm_sec ;
  62. RTC_TimeStruct.Minutes = tm.tm_min ;
  63. RTC_TimeStruct.Hours = tm.tm_hour;
  64. RTC_DateStruct.Date = tm.tm_mday;
  65. RTC_DateStruct.Month = tm.tm_mon + 1 ;
  66. RTC_DateStruct.Year = tm.tm_year - 100;
  67. RTC_DateStruct.WeekDay = tm.tm_wday + 1;
  68. if (HAL_RTC_SetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN) != HAL_OK)
  69. {
  70. return -RT_ERROR;
  71. }
  72. if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
  73. {
  74. return -RT_ERROR;
  75. }
  76. LOG_D("set rtc time.");
  77. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);
  78. #ifdef SOC_SERIES_STM32F1
  79. /* F1 series does't save year/month/date datas. so keep those datas to bkp reg */
  80. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year);
  81. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month);
  82. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date);
  83. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay);
  84. #endif
  85. return RT_EOK;
  86. }
  87. #ifdef SOC_SERIES_STM32F1
  88. /* update RTC_BKP_DRx*/
  89. static void rt_rtc_f1_bkp_update(void)
  90. {
  91. RTC_DateTypeDef RTC_DateStruct = {0};
  92. HAL_PWR_EnableBkUpAccess();
  93. __HAL_RCC_BKP_CLK_ENABLE();
  94. RTC_DateStruct.Year = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR2);
  95. RTC_DateStruct.Month = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR3);
  96. RTC_DateStruct.Date = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4);
  97. RTC_DateStruct.WeekDay = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR5);
  98. if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
  99. {
  100. Error_Handler();
  101. }
  102. HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN);
  103. if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4) != RTC_DateStruct.Date)
  104. {
  105. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);
  106. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year);
  107. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month);
  108. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date);
  109. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay);
  110. }
  111. }
  112. #endif
  113. static rt_err_t rt_rtc_config(void)
  114. {
  115. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  116. HAL_PWR_EnableBkUpAccess();
  117. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  118. #ifdef BSP_RTC_USING_LSI
  119. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  120. #else
  121. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  122. #endif
  123. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  124. #if defined(SOC_SERIES_STM32WL)
  125. __HAL_RCC_RTCAPB_CLK_ENABLE();
  126. #endif
  127. /* Enable RTC Clock */
  128. __HAL_RCC_RTC_ENABLE();
  129. RTC_Handler.Instance = RTC;
  130. if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR1) != BKUP_REG_DATA)
  131. {
  132. LOG_I("RTC hasn't been configured, please use <date> command to config.");
  133. #if defined(SOC_SERIES_STM32F1)
  134. RTC_Handler.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
  135. RTC_Handler.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  136. #elif defined(SOC_SERIES_STM32F0)
  137. /* set the frequency division */
  138. #ifdef BSP_RTC_USING_LSI
  139. RTC_Handler.Init.AsynchPrediv = 0XA0;
  140. RTC_Handler.Init.SynchPrediv = 0xFA;
  141. #else
  142. RTC_Handler.Init.AsynchPrediv = 0X7F;
  143. RTC_Handler.Init.SynchPrediv = 0x0130;
  144. #endif /* BSP_RTC_USING_LSI */
  145. RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  146. RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  147. RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  148. RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  149. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB)
  150. /* set the frequency division */
  151. #ifdef BSP_RTC_USING_LSI
  152. RTC_Handler.Init.AsynchPrediv = 0X7D;
  153. #else
  154. RTC_Handler.Init.AsynchPrediv = 0X7F;
  155. #endif /* BSP_RTC_USING_LSI */
  156. RTC_Handler.Init.SynchPrediv = 0XFF;
  157. RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  158. RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  159. RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  160. RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  161. #endif
  162. if (HAL_RTC_Init(&RTC_Handler) != HAL_OK)
  163. {
  164. return -RT_ERROR;
  165. }
  166. }
  167. #ifdef SOC_SERIES_STM32F1
  168. else
  169. {
  170. /* F1 series need update by bkp reg datas */
  171. rt_rtc_f1_bkp_update();
  172. }
  173. #endif
  174. return RT_EOK;
  175. }
  176. static rt_err_t stm32_rtc_init(void)
  177. {
  178. #if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB)
  179. __HAL_RCC_PWR_CLK_ENABLE();
  180. #endif
  181. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  182. #ifdef BSP_RTC_USING_LSI
  183. #ifdef SOC_SERIES_STM32WB
  184. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
  185. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  186. RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  187. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  188. #else
  189. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  190. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  191. RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  192. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  193. #endif
  194. #else
  195. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  196. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  197. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  198. RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
  199. #endif
  200. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  201. if (rt_rtc_config() != RT_EOK)
  202. {
  203. LOG_E("rtc init failed.");
  204. return -RT_ERROR;
  205. }
  206. return RT_EOK;
  207. }
  208. static rt_err_t stm32_rtc_get_secs(time_t *sec)
  209. {
  210. struct timeval tv;
  211. stm32_rtc_get_timeval(&tv);
  212. *(time_t *) sec = tv.tv_sec;
  213. LOG_D("RTC: get rtc_time %d", *sec);
  214. return RT_EOK;
  215. }
  216. static rt_err_t stm32_rtc_set_secs(time_t *sec)
  217. {
  218. rt_err_t result = RT_EOK;
  219. if (set_rtc_time_stamp(*sec))
  220. {
  221. result = -RT_ERROR;
  222. }
  223. LOG_D("RTC: set rtc_time %d", *sec);
  224. return result;
  225. }
  226. static const struct rt_rtc_ops stm32_rtc_ops =
  227. {
  228. stm32_rtc_init,
  229. stm32_rtc_get_secs,
  230. stm32_rtc_set_secs,
  231. RT_NULL,
  232. RT_NULL,
  233. stm32_rtc_get_timeval,
  234. RT_NULL,
  235. };
  236. static rt_rtc_dev_t stm32_rtc_dev;
  237. static int rt_hw_rtc_init(void)
  238. {
  239. rt_err_t result;
  240. stm32_rtc_dev.ops = &stm32_rtc_ops;
  241. result = rt_hw_rtc_register(&stm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  242. if (result != RT_EOK)
  243. {
  244. LOG_E("rtc register err code: %d", result);
  245. return result;
  246. }
  247. LOG_D("rtc init success");
  248. return RT_EOK;
  249. }
  250. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  251. #endif /* BSP_USING_ONCHIP_RTC */