drv_rtc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  10. #include "board.h"
  11. #ifdef BSP_USING_ONCHIP_RTC
  12. #ifndef RTC_BKP_DR1
  13. #define RTC_BKP_DR1 RT_NULL
  14. #endif
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.rtc"
  17. #include <drv_log.h>
  18. #define BKUP_REG_DATA 0xA5A5
  19. static struct rt_device rtc;
  20. static RTC_HandleTypeDef RTC_Handler;
  21. RT_WEAK uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  22. {
  23. return (~BKUP_REG_DATA);
  24. }
  25. RT_WEAK void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  26. {
  27. return;
  28. }
  29. static time_t get_rtc_timestamp(void)
  30. {
  31. RTC_TimeTypeDef RTC_TimeStruct = {0};
  32. RTC_DateTypeDef RTC_DateStruct = {0};
  33. struct tm tm_new = {0};
  34. HAL_RTC_GetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN);
  35. HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN);
  36. tm_new.tm_sec = RTC_TimeStruct.Seconds;
  37. tm_new.tm_min = RTC_TimeStruct.Minutes;
  38. tm_new.tm_hour = RTC_TimeStruct.Hours;
  39. tm_new.tm_mday = RTC_DateStruct.Date;
  40. tm_new.tm_mon = RTC_DateStruct.Month - 1;
  41. tm_new.tm_year = RTC_DateStruct.Year + 100;
  42. LOG_D("get rtc time.");
  43. return mktime(&tm_new);
  44. }
  45. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  46. {
  47. RTC_TimeTypeDef RTC_TimeStruct = {0};
  48. RTC_DateTypeDef RTC_DateStruct = {0};
  49. struct tm *p_tm;
  50. p_tm = localtime(&time_stamp);
  51. if (p_tm->tm_year < 100)
  52. {
  53. return -RT_ERROR;
  54. }
  55. RTC_TimeStruct.Seconds = p_tm->tm_sec ;
  56. RTC_TimeStruct.Minutes = p_tm->tm_min ;
  57. RTC_TimeStruct.Hours = p_tm->tm_hour;
  58. RTC_DateStruct.Date = p_tm->tm_mday;
  59. RTC_DateStruct.Month = p_tm->tm_mon + 1 ;
  60. RTC_DateStruct.Year = p_tm->tm_year - 100;
  61. RTC_DateStruct.WeekDay = p_tm->tm_wday + 1;
  62. if (HAL_RTC_SetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN) != HAL_OK)
  63. {
  64. return -RT_ERROR;
  65. }
  66. if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
  67. {
  68. return -RT_ERROR;
  69. }
  70. LOG_D("set rtc time.");
  71. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);
  72. #ifdef SOC_SERIES_STM32F1
  73. /* F1 series does't save year/month/date datas. so keep those datas to bkp reg */
  74. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year);
  75. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month);
  76. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date);
  77. HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay);
  78. #endif
  79. return RT_EOK;
  80. }
  81. static void rt_rtc_init(void)
  82. {
  83. #ifndef SOC_SERIES_STM32H7
  84. __HAL_RCC_PWR_CLK_ENABLE();
  85. #endif
  86. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  87. #ifdef BSP_RTC_USING_LSI
  88. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  89. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  90. RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  91. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  92. #else
  93. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  94. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  95. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  96. RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
  97. #endif
  98. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  99. }
  100. #ifdef SOC_SERIES_STM32F1
  101. /* update RTC_BKP_DRx*/
  102. static void rt_rtc_f1_bkp_update(void)
  103. {
  104. RTC_DateTypeDef RTC_DateStruct = {0};
  105. HAL_PWR_EnableBkUpAccess();
  106. __HAL_RCC_BKP_CLK_ENABLE();
  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(struct rt_device *dev)
  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. /* Enable RTC Clock */
  138. __HAL_RCC_RTC_ENABLE();
  139. RTC_Handler.Instance = RTC;
  140. if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR1) != BKUP_REG_DATA)
  141. {
  142. LOG_I("RTC hasn't been configured, please use <date> command to config.");
  143. #if defined(SOC_SERIES_STM32F1)
  144. RTC_Handler.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
  145. RTC_Handler.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  146. #elif defined(SOC_SERIES_STM32F0)
  147. /* set the frequency division */
  148. #ifdef BSP_RTC_USING_LSI
  149. RTC_Handler.Init.AsynchPrediv = 0XA0;
  150. RTC_Handler.Init.SynchPrediv = 0xFA;
  151. #else
  152. RTC_Handler.Init.AsynchPrediv = 0X7F;
  153. RTC_Handler.Init.SynchPrediv = 0x0130;
  154. #endif /* BSP_RTC_USING_LSI */
  155. RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  156. RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  157. RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  158. RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  159. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32H7)
  160. /* set the frequency division */
  161. #ifdef BSP_RTC_USING_LSI
  162. RTC_Handler.Init.AsynchPrediv = 0X7D;
  163. #else
  164. RTC_Handler.Init.AsynchPrediv = 0X7F;
  165. #endif /* BSP_RTC_USING_LSI */
  166. RTC_Handler.Init.SynchPrediv = 0XFF;
  167. RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  168. RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  169. RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  170. RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  171. #endif
  172. if (HAL_RTC_Init(&RTC_Handler) != HAL_OK)
  173. {
  174. return -RT_ERROR;
  175. }
  176. }
  177. #ifdef SOC_SERIES_STM32F1
  178. else
  179. {
  180. /* F1 series need update by bkp reg datas */
  181. rt_rtc_f1_bkp_update();
  182. }
  183. #endif
  184. return RT_EOK;
  185. }
  186. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  187. {
  188. rt_err_t result = RT_EOK;
  189. RT_ASSERT(dev != RT_NULL);
  190. switch (cmd)
  191. {
  192. case RT_DEVICE_CTRL_RTC_GET_TIME:
  193. *(rt_uint32_t *)args = get_rtc_timestamp();
  194. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  195. break;
  196. case RT_DEVICE_CTRL_RTC_SET_TIME:
  197. if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  198. {
  199. result = -RT_ERROR;
  200. }
  201. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  202. break;
  203. }
  204. return result;
  205. }
  206. #ifdef RT_USING_DEVICE_OPS
  207. const static struct rt_device_ops rtc_ops =
  208. {
  209. RT_NULL,
  210. RT_NULL,
  211. RT_NULL,
  212. RT_NULL,
  213. RT_NULL,
  214. rt_rtc_control
  215. };
  216. #endif
  217. static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
  218. {
  219. RT_ASSERT(device != RT_NULL);
  220. rt_rtc_init();
  221. if (rt_rtc_config(device) != RT_EOK)
  222. {
  223. return -RT_ERROR;
  224. }
  225. #ifdef RT_USING_DEVICE_OPS
  226. device->ops = &rtc_ops;
  227. #else
  228. device->init = RT_NULL;
  229. device->open = RT_NULL;
  230. device->close = RT_NULL;
  231. device->read = RT_NULL;
  232. device->write = RT_NULL;
  233. device->control = rt_rtc_control;
  234. #endif
  235. device->type = RT_Device_Class_RTC;
  236. device->rx_indicate = RT_NULL;
  237. device->tx_complete = RT_NULL;
  238. device->user_data = RT_NULL;
  239. /* register a character device */
  240. return rt_device_register(device, name, flag);
  241. }
  242. int rt_hw_rtc_init(void)
  243. {
  244. rt_err_t result;
  245. result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  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 */