1
0

drv_rtc.c 8.4 KB

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