drv_rtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. * 2022-05-16 shelton first version
  9. * 2023-04-08 shelton add support f423
  10. * 2023-10-18 shelton add support f402/f405
  11. * 2024-04-12 shelton add support a403a and a423
  12. * 2024-08-30 shelton add support m412 and m416
  13. */
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include <sys/time.h>
  17. #include "drv_common.h"
  18. #ifdef BSP_USING_RTC
  19. //#define DRV_DEBUG
  20. #define LOG_TAG "drv.rtc"
  21. #include <drv_log.h>
  22. #define BKUP_REG_DATA 0xA5A5
  23. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  24. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32A403A)
  25. #define Alarm_IRQn RTCAlarm_IRQn
  26. #define Alarm_IRQHandler RTCAlarm_IRQHandler
  27. #elif defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32F425)
  28. #define Alarm_IRQn RTC_IRQn
  29. #define Alarm_IRQHandler RTC_IRQHandler
  30. #else
  31. #define Alarm_IRQn ERTCAlarm_IRQn
  32. #define Alarm_IRQHandler ERTCAlarm_IRQHandler
  33. #endif
  34. struct rtc_device_object
  35. {
  36. rt_rtc_dev_t rtc_dev;
  37. #ifdef RT_USING_ALARM
  38. struct rt_rtc_wkalarm wkalarm;
  39. #endif
  40. };
  41. static struct rtc_device_object rtc_device;
  42. static time_t get_rtc_timestamp(void)
  43. {
  44. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  45. defined (SOC_SERIES_AT32F415) || defined (SOC_SERIES_AT32F421) || \
  46. defined (SOC_SERIES_AT32F425) || defined (SOC_SERIES_AT32F423) || \
  47. defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  48. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  49. defined (SOC_SERIES_AT32M416)
  50. struct tm tm_new;
  51. ertc_time_type ertc_time_struct;
  52. ertc_calendar_get(&ertc_time_struct);
  53. tm_new.tm_sec = ertc_time_struct.sec;
  54. tm_new.tm_min = ertc_time_struct.min;
  55. tm_new.tm_hour = ertc_time_struct.hour;
  56. tm_new.tm_mday = ertc_time_struct.day;
  57. tm_new.tm_mon = ertc_time_struct.month - 1;
  58. tm_new.tm_year = ertc_time_struct.year + 100;
  59. LOG_D("get rtc time.");
  60. return timegm(&tm_new);
  61. #else
  62. return rtc_counter_get();
  63. #endif
  64. }
  65. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  66. {
  67. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  68. defined (SOC_SERIES_AT32F415) || defined (SOC_SERIES_AT32F421) || \
  69. defined (SOC_SERIES_AT32F425) || defined (SOC_SERIES_AT32F423) || \
  70. defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  71. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  72. defined (SOC_SERIES_AT32M416)
  73. struct tm now;
  74. gmtime_r(&time_stamp, &now);
  75. if (now.tm_year < 100)
  76. {
  77. return -RT_ERROR;
  78. }
  79. /* set time */
  80. if(ertc_time_set(now.tm_hour, now.tm_min, now.tm_sec, ERTC_AM) != SUCCESS)
  81. {
  82. return -RT_ERROR;
  83. }
  84. /* set date */
  85. if(ertc_date_set(now.tm_year - 100, now.tm_mon + 1, now.tm_mday, now.tm_wday + 1) != SUCCESS)
  86. {
  87. return -RT_ERROR;
  88. }
  89. LOG_D("set rtc time.");
  90. /* indicator for the ertc configuration */
  91. ertc_bpr_data_write(ERTC_DT1, BKUP_REG_DATA);
  92. #else
  93. /* set the rtc counter value */
  94. rtc_counter_set(time_stamp);
  95. /* wait until last write operation on rtc registers has finished */
  96. rtc_wait_config_finish();
  97. LOG_D("set rtc time.");
  98. bpr_data_write(BPR_DATA1, BKUP_REG_DATA);
  99. #endif
  100. return RT_EOK;
  101. }
  102. static rt_err_t rt_rtc_config(void)
  103. {
  104. /* allow access to pattery powered domain */
  105. pwc_battery_powered_domain_access(TRUE);
  106. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  107. defined (SOC_SERIES_AT32F415) || defined (SOC_SERIES_AT32F421) || \
  108. defined (SOC_SERIES_AT32F425) || defined (SOC_SERIES_AT32F423) || \
  109. defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  110. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  111. defined (SOC_SERIES_AT32M416)
  112. /* select rtc clock source */
  113. #ifdef BSP_RTC_USING_LICK
  114. crm_ertc_clock_select(CRM_ERTC_CLOCK_LICK);
  115. #else
  116. crm_ertc_clock_select(CRM_ERTC_CLOCK_LEXT);
  117. #endif /* BSP_RTC_USING_LICK */
  118. /* enable rtc */
  119. crm_ertc_clock_enable(TRUE);
  120. /* wait for ertc registers update */
  121. ertc_wait_update();
  122. if (ertc_bpr_data_read(ERTC_DT1)!= BKUP_REG_DATA)
  123. {
  124. LOG_I("RTC hasn't been configured, please use <date> command to config.");
  125. /* configure the ertc divider */
  126. ertc_divider_set(0x7F, 0xFF);
  127. /* configure the ertc hour mode */
  128. ertc_hour_mode_set(ERTC_HOUR_MODE_24);
  129. }
  130. #else
  131. #ifdef BSP_RTC_USING_LICK
  132. crm_rtc_clock_select(CRM_RTC_CLOCK_LICK);
  133. #else
  134. crm_rtc_clock_select(CRM_RTC_CLOCK_LEXT);
  135. #endif /* BSP_RTC_USING_LICK */
  136. /* enable rtc */
  137. crm_rtc_clock_enable(TRUE);
  138. /* wait for rtc registers update finish */
  139. rtc_wait_update_finish();
  140. /* wait until last write operation on rtc registers has finished */
  141. rtc_wait_config_finish();
  142. if (bpr_data_read(BPR_DATA1) != BKUP_REG_DATA)
  143. {
  144. LOG_I("RTC hasn't been configured, please use <date> command to config.");
  145. /* set rtc divider: set rtc period to 1sec */
  146. rtc_divider_set(32767);
  147. /* wait until last write operation on rtc registers has finished */
  148. rtc_wait_config_finish();
  149. }
  150. #endif
  151. return RT_EOK;
  152. }
  153. static rt_err_t _rtc_init(void)
  154. {
  155. crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
  156. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  157. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32A403A)
  158. crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
  159. #endif
  160. #ifdef BSP_RTC_USING_LICK
  161. crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
  162. while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET);
  163. #else
  164. pwc_battery_powered_domain_access(TRUE);
  165. crm_clock_source_enable(CRM_CLOCK_SOURCE_LEXT, TRUE);
  166. while(crm_flag_get(CRM_LEXT_STABLE_FLAG) == RESET);
  167. #endif /* BSP_RTC_USING_LICK */
  168. if (rt_rtc_config() != RT_EOK)
  169. {
  170. LOG_E("rtc init failed.");
  171. return -RT_ERROR;
  172. }
  173. return RT_EOK;
  174. }
  175. static rt_err_t _rtc_get_secs(time_t *args)
  176. {
  177. *(rt_uint32_t *)args = get_rtc_timestamp();
  178. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  179. return RT_EOK;
  180. }
  181. static rt_err_t _rtc_set_secs(time_t *args)
  182. {
  183. rt_err_t result = RT_EOK;
  184. if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  185. {
  186. result = -RT_ERROR;
  187. }
  188. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  189. return result;
  190. }
  191. #ifdef RT_USING_ALARM
  192. static rt_err_t rtc_alarm_time_set(struct rtc_device_object* p_dev)
  193. {
  194. exint_init_type exint_init_struct;
  195. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  196. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32A403A)
  197. struct tm tm_new;
  198. time_t sec_count;
  199. #endif
  200. /* config the exint line of the rtc alarm */
  201. exint_init_struct.line_select = EXINT_LINE_17;
  202. exint_init_struct.line_enable = TRUE;
  203. exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
  204. exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
  205. exint_init(&exint_init_struct);
  206. if (p_dev->wkalarm.enable)
  207. {
  208. nvic_irq_enable(Alarm_IRQn, 0, 0);
  209. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  210. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32A403A)
  211. /* clear alarm flag */
  212. rtc_flag_clear(RTC_TA_FLAG);
  213. /* wait for the register write to complete */
  214. rtc_wait_config_finish();
  215. /* enable alarm interrupt */
  216. rtc_interrupt_enable(RTC_TA_INT, TRUE);
  217. /* wait for the register write to complete */
  218. rtc_wait_config_finish();
  219. tm_new.tm_sec = p_dev->wkalarm.tm_sec;
  220. tm_new.tm_min = p_dev->wkalarm.tm_min;
  221. tm_new.tm_hour = p_dev->wkalarm.tm_hour;
  222. tm_new.tm_mday = p_dev->wkalarm.tm_mday;
  223. tm_new.tm_mon = p_dev->wkalarm.tm_mon;
  224. tm_new.tm_year = p_dev->wkalarm.tm_year;
  225. sec_count = timegm(&tm_new);
  226. rtc_alarm_set(sec_count);
  227. /* wait for the register write to complete */
  228. rtc_wait_config_finish();
  229. #else
  230. ertc_alarm_enable(ERTC_ALA, FALSE);
  231. ertc_flag_clear(ERTC_ALAF_FLAG);
  232. ertc_alarm_mask_set(ERTC_ALA, ERTC_ALARM_MASK_DATE_WEEK);
  233. ertc_alarm_week_date_select(ERTC_ALA, ERTC_SLECT_DATE);
  234. ertc_alarm_set(ERTC_ALA, p_dev->wkalarm.tm_mday, p_dev->wkalarm.tm_hour, \
  235. p_dev->wkalarm.tm_min, p_dev->wkalarm.tm_sec, ERTC_24H);
  236. ertc_interrupt_enable(ERTC_ALA_INT, TRUE);
  237. ertc_alarm_enable(ERTC_ALA, TRUE);
  238. ertc_flag_clear(ERTC_ALAF_FLAG);
  239. #endif
  240. }
  241. return RT_EOK;
  242. }
  243. void Alarm_IRQHandler(void)
  244. {
  245. rt_interrupt_enter();
  246. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  247. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32A403A)
  248. if(rtc_flag_get(RTC_TA_FLAG) != RESET)
  249. {
  250. /* clear exint line flag */
  251. exint_flag_clear(EXINT_LINE_17);
  252. /* wait for the register write to complete */
  253. rtc_wait_config_finish();
  254. /* clear alarm flag */
  255. rtc_flag_clear(RTC_TA_FLAG);
  256. /* wait for the register write to complete */
  257. rtc_wait_config_finish();
  258. rt_alarm_update(&rtc_device.rtc_dev.parent, 1);
  259. }
  260. #else
  261. if(ertc_flag_get(ERTC_ALAF_FLAG) != RESET)
  262. {
  263. /* clear alarm flag */
  264. ertc_flag_clear(ERTC_ALAF_FLAG);
  265. /* clear exint flag */
  266. exint_flag_clear(EXINT_LINE_17);
  267. rt_alarm_update(&rtc_device.rtc_dev.parent, 1);
  268. }
  269. #endif
  270. rt_interrupt_leave();
  271. }
  272. #endif
  273. static rt_err_t _rtc_get_alarm(struct rt_rtc_wkalarm *alarm)
  274. {
  275. #ifdef RT_USING_ALARM
  276. *alarm = rtc_device.wkalarm;
  277. LOG_D("GET_ALARM %d:%d:%d",rtc_device.wkalarm.tm_hour,
  278. rtc_device.wkalarm.tm_min,rtc_device.wkalarm.tm_sec);
  279. return RT_EOK;
  280. #else
  281. return -RT_ERROR;
  282. #endif
  283. }
  284. static rt_err_t _rtc_set_alarm(struct rt_rtc_wkalarm *alarm)
  285. {
  286. #ifdef RT_USING_ALARM
  287. LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM");
  288. if (alarm != RT_NULL)
  289. {
  290. rtc_device.wkalarm.enable = alarm->enable;
  291. rtc_device.wkalarm.tm_year = alarm->tm_year;
  292. rtc_device.wkalarm.tm_mon = alarm->tm_mon;
  293. rtc_device.wkalarm.tm_mday = alarm->tm_mday;
  294. rtc_device.wkalarm.tm_hour = alarm->tm_hour;
  295. rtc_device.wkalarm.tm_min = alarm->tm_min;
  296. rtc_device.wkalarm.tm_sec = alarm->tm_sec;
  297. rtc_alarm_time_set(&rtc_device);
  298. }
  299. else
  300. {
  301. LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!");
  302. return -RT_ERROR;
  303. }
  304. LOG_D("SET_ALARM %d:%d:%d",alarm->tm_hour,
  305. alarm->tm_min, alarm->tm_sec);
  306. return RT_EOK;
  307. #else
  308. return -RT_ERROR;
  309. #endif
  310. }
  311. static rt_err_t _rtc_get_timeval(struct timeval *tv)
  312. {
  313. tv->tv_sec = get_rtc_timestamp();
  314. return RT_EOK;
  315. }
  316. static const struct rt_rtc_ops _rtc_ops =
  317. {
  318. _rtc_init,
  319. _rtc_get_secs,
  320. _rtc_set_secs,
  321. _rtc_get_alarm,
  322. _rtc_set_alarm,
  323. _rtc_get_timeval,
  324. RT_NULL,
  325. };
  326. int rt_hw_rtc_init(void)
  327. {
  328. rt_err_t result;
  329. rtc_device.rtc_dev.ops = &_rtc_ops;
  330. result = rt_hw_rtc_register(&rtc_device.rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  331. if (result != RT_EOK)
  332. {
  333. LOG_E("rtc register err code: %d", result);
  334. return result;
  335. }
  336. LOG_D("rtc init success");
  337. return RT_EOK;
  338. }
  339. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  340. #endif /* BSP_USING_RTC */