drv_rtc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. * 2022-03-04 stevetong459 first version
  9. * 2022-07-15 Aligagago add APM32F4 series MCU support
  10. * 2022-12-26 luobeihai add APM32F0 series MCU support
  11. * 2023-03-18 luobeihai fix RT-Thread Studio compile error bug
  12. * 2023-03-27 luobeihai add APM32E1/S1 series MCU support
  13. */
  14. #include "board.h"
  15. #include <sys/time.h>
  16. #ifdef BSP_USING_ONCHIP_RTC
  17. #define DBG_TAG "drv.rtc"
  18. #define DBG_LVL DBG_INFO
  19. #include <rtdbg.h>
  20. #ifndef LSI_VALUE
  21. #define LSI_VALUE ((uint32_t)40000)
  22. #endif
  23. #ifndef LSE_VALUE
  24. #define LSE_VALUE ((uint32_t)32768)
  25. #endif
  26. #define DRV_RTC_TIME_OUT 0xFFFFF
  27. static rt_rtc_dev_t apm32_rtc_dev;
  28. static rt_uint8_t rtc_init_flag = RESET;
  29. /**
  30. * @brief This function will initialize the rtc on chip.
  31. *
  32. * @return RT_EOK indicates successful initialize, other value indicates failed;
  33. */
  34. static rt_err_t apm32_rtc_init(void)
  35. {
  36. volatile rt_uint32_t counter = 0;
  37. /* Enable RTC Clock */
  38. #if defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  39. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU | RCM_APB1_PERIPH_BAKR);
  40. #elif defined(SOC_SERIES_APM32F0) || defined(SOC_SERIES_APM32F4)
  41. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU);
  42. #endif
  43. PMU_EnableBackupAccess();
  44. /* Config RTC clock */
  45. #ifdef BSP_RTC_USING_LSI
  46. RCM_EnableLSI();
  47. while (!RCM_ReadStatusFlag(RCM_FLAG_LSIRDY))
  48. {
  49. if (++counter > DRV_RTC_TIME_OUT)
  50. {
  51. return -RT_ETIMEOUT;
  52. }
  53. }
  54. RCM_ConfigRTCCLK(RCM_RTCCLK_LSI);
  55. #elif defined(BSP_RTC_USING_LSE)
  56. RCM_DisableLSI();
  57. RCM_ConfigLSE(RCM_LSE_OPEN);
  58. while (!RCM_ReadStatusFlag(RCM_FLAG_LSERDY))
  59. {
  60. if (++counter > DRV_RTC_TIME_OUT)
  61. {
  62. return -RT_ETIMEOUT;
  63. }
  64. }
  65. RCM_ConfigRTCCLK(RCM_RTCCLK_LSE);
  66. #endif /* BSP_RTC_USING_LSI */
  67. RCM_EnableRTCCLK();
  68. RTC_WaitForSynchro();
  69. #if defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  70. counter = 0;
  71. while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
  72. {
  73. if (++counter > DRV_RTC_TIME_OUT)
  74. {
  75. return -RT_ETIMEOUT;
  76. }
  77. }
  78. RTC_EnableConfigMode();
  79. RTC_ClearStatusFlag(RTC_FLAG_OVR | RTC_FLAG_ALR | RTC_FLAG_SEC);
  80. #ifdef BSP_RTC_USING_LSI
  81. RTC_ConfigPrescaler(LSI_VALUE - 1);
  82. #elif defined(BSP_RTC_USING_LSE)
  83. RTC_ConfigPrescaler(LSE_VALUE - 1);
  84. #endif /* BSP_RTC_USING_LSI */
  85. #elif defined(SOC_SERIES_APM32F4)
  86. RTC_EnableInit();
  87. RTC_Config_T rtcConfig;
  88. RTC_ConfigStructInit(&rtcConfig);
  89. RTC_Config(&rtcConfig);
  90. #elif defined(SOC_SERIES_APM32F0)
  91. RTC_EnableInit();
  92. RTC_Config_T rtcConfig;
  93. RTC_ConfigStructInit(&rtcConfig);
  94. #ifdef BSP_RTC_USING_LSI
  95. rtcConfig.AsynchPrediv = 0x63;
  96. rtcConfig.SynchPrediv = 0x18F;
  97. #else
  98. rtcConfig.AsynchPrediv = 0x7F;
  99. rtcConfig.SynchPrediv = 0x130;
  100. #endif /* BSP_RTC_USING_LSI */
  101. RTC_Config(&rtcConfig);
  102. #endif /* SOC_SERIES_APM32F1 || SOC_SERIES_APM32E1 || SOC_SERIES_APM32S1 */
  103. if (!rtc_init_flag)
  104. {
  105. rtc_init_flag = SET;
  106. }
  107. return RT_EOK;
  108. }
  109. #if defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  110. /**
  111. * @brief This function will initialize the rtc on chip.
  112. *
  113. * @return RT_EOK indicates successful initialize, other value indicates failed;
  114. */
  115. static rt_err_t apm32_rtc_get_secs(time_t *sec)
  116. {
  117. volatile rt_uint32_t counter = 0;
  118. while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
  119. {
  120. if (++counter > DRV_RTC_TIME_OUT)
  121. {
  122. return -RT_ETIMEOUT;
  123. }
  124. }
  125. *(time_t *) sec = RTC_ReadCounter();
  126. return RT_EOK;
  127. }
  128. static rt_err_t apm32_rtc_set_secs(time_t *sec)
  129. {
  130. volatile rt_uint32_t counter = 0;
  131. if (!rtc_init_flag)
  132. {
  133. apm32_rtc_init();
  134. }
  135. while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
  136. {
  137. if (++counter > DRV_RTC_TIME_OUT)
  138. {
  139. return -RT_ETIMEOUT;
  140. }
  141. }
  142. RTC_ConfigCounter(*(rt_uint32_t *)sec);
  143. return RT_EOK;
  144. }
  145. #elif defined(SOC_SERIES_APM32F0) || defined(SOC_SERIES_APM32F4)
  146. static rt_err_t apm32_rtc_get_timeval(void *args)
  147. {
  148. struct timeval *tv = (struct timeval *) args;
  149. #if defined(SOC_SERIES_APM32F0)
  150. RTC_TIME_T timeConfig;
  151. RTC_DATE_T dateConfig;
  152. #elif defined(SOC_SERIES_APM32F4)
  153. RTC_TimeConfig_T timeConfig;
  154. RTC_DateConfig_T dateConfig;
  155. #endif
  156. struct tm tm_new = {0};
  157. RTC_ReadTime(RTC_FORMAT_BIN, &timeConfig);
  158. RTC_ReadDate(RTC_FORMAT_BIN, &dateConfig);
  159. tm_new.tm_sec = timeConfig.seconds;
  160. tm_new.tm_min = timeConfig.minutes;
  161. tm_new.tm_hour = timeConfig.hours;
  162. tm_new.tm_mday = dateConfig.date;
  163. tm_new.tm_mon = dateConfig.month - 1;
  164. tm_new.tm_year = dateConfig.year + 100;
  165. tv->tv_sec = timegm(&tm_new);
  166. return RT_EOK;
  167. }
  168. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  169. {
  170. #if defined(SOC_SERIES_APM32F0)
  171. RTC_TIME_T timeConfig;
  172. RTC_DATE_T dateConfig;
  173. #elif defined(SOC_SERIES_APM32F4)
  174. RTC_TimeConfig_T timeConfig;
  175. RTC_DateConfig_T dateConfig;
  176. #endif
  177. struct tm tm = {0};
  178. if (!rtc_init_flag)
  179. {
  180. apm32_rtc_init();
  181. }
  182. gmtime_r(&time_stamp, &tm);
  183. if (tm.tm_year < 100)
  184. {
  185. return -RT_ERROR;
  186. }
  187. timeConfig.seconds = tm.tm_sec ;
  188. timeConfig.minutes = tm.tm_min ;
  189. timeConfig.hours = tm.tm_hour;
  190. dateConfig.date = tm.tm_mday;
  191. #if defined(SOC_SERIES_APM32F4)
  192. dateConfig.month = (RTC_MONTH_T)(tm.tm_mon + 1);
  193. dateConfig.weekday = (RTC_WEEKDAY_T)(tm.tm_wday + 1);
  194. #else
  195. dateConfig.month = tm.tm_mon + 1 ;
  196. dateConfig.weekday = tm.tm_wday + 1;
  197. #endif
  198. dateConfig.year = tm.tm_year - 100;
  199. RTC_ConfigTime(RTC_FORMAT_BIN, &timeConfig);
  200. RTC_ConfigDate(RTC_FORMAT_BIN, &dateConfig);
  201. /* wait for set time completed */
  202. for (int i = 0; i < 0xFFFF; i++);
  203. return RT_EOK;
  204. }
  205. /**
  206. * @brief This function will initialize the rtc on chip.
  207. *
  208. * @return RT_EOK indicates successful initialize, other value indicates failed;
  209. */
  210. static rt_err_t apm32_rtc_get_secs(void *args)
  211. {
  212. struct timeval tv;
  213. apm32_rtc_get_timeval(&tv);
  214. *(rt_uint32_t *) args = tv.tv_sec;
  215. return RT_EOK;
  216. }
  217. static rt_err_t apm32_rtc_set_secs(void *args)
  218. {
  219. rt_err_t result = RT_EOK;
  220. if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  221. {
  222. result = -RT_ERROR;
  223. }
  224. return result;
  225. }
  226. #endif /* SOC_SERIES_APM32F1 || SOC_SERIES_APM32E1 || SOC_SERIES_APM32S1 */
  227. static const struct rt_rtc_ops apm32_rtc_ops =
  228. {
  229. apm32_rtc_init,
  230. apm32_rtc_get_secs,
  231. apm32_rtc_set_secs,
  232. RT_NULL,
  233. RT_NULL,
  234. #if defined(SOC_SERIES_APM32F0) || defined(SOC_SERIES_APM32F4)
  235. apm32_rtc_get_timeval,
  236. #else
  237. RT_NULL,
  238. #endif
  239. RT_NULL,
  240. };
  241. /**
  242. * @brief RTC initialization function.
  243. *
  244. * @return RT_EOK indicates successful initialization, other value indicates failed;
  245. */
  246. static int rt_hw_rtc_init(void)
  247. {
  248. rt_err_t result = RT_EOK;
  249. apm32_rtc_dev.ops = &apm32_rtc_ops;
  250. if (rt_hw_rtc_register(&apm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
  251. {
  252. LOG_E("rtc init failed");
  253. result = -RT_ERROR;
  254. }
  255. else
  256. {
  257. LOG_D("rtc init success");
  258. }
  259. return result;
  260. }
  261. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  262. #endif /* BSP_USING_ONCHIP_RTC */