1
0

drv_rtc.c 7.0 KB

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