drv_rtc.c 8.7 KB

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