drv_rtc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-01-25 iysheng first version
  9. * 2025-09-25 kurisaw adapt to rt_rtc_ops interface
  10. * 2025-09-25 kurisaw add alarm interrupt support
  11. */
  12. #include <board.h>
  13. #include <rtdevice.h>
  14. #include <rthw.h>
  15. #include <sys/time.h>
  16. #define DBG_TAG "drv.rtc"
  17. #define DBG_LVL DBG_INFO
  18. #include <rtdbg.h>
  19. #ifdef RT_USING_RTC
  20. #if defined(BSP_USING_ALARM)
  21. #if defined(BSP_USING_ALARM0) && defined(BSP_USING_ALARM1)
  22. #error "Only supports using one alarm at a time."
  23. #elif !defined(BSP_USING_ALARM0) && !defined(BSP_USING_ALARM1)
  24. #error "Please Enable RTC alarm define (BSP_USING_ALARM0 | BSP_USING_ALARM1)"
  25. #elif defined(BSP_USING_ALARM0)
  26. #define BSP_ALARM_FLAG RTC_FLAG_ALARM0
  27. #define BSP_RTC_ALARM RTC_ALARM0
  28. #define BSP_RTC_INT_ALARM RTC_INT_ALARM0
  29. #elif defined(BSP_USING_ALARM1)
  30. #define BSP_ALARM_FLAG RTC_FLAG_ALARM1
  31. #define BSP_RTC_ALARM RTC_ALARM1
  32. #define BSP_RTC_INT_ALARM RTC_INT_ALARM1
  33. #endif
  34. #endif
  35. #if defined(SOC_SERIES_GD32H7xx) || defined(SOC_SERIES_GD32F5xx)
  36. #define rtc_year year
  37. #define rtc_month month
  38. #define rtc_date date
  39. #define rtc_day_of_week day_of_week
  40. #define rtc_hour hour
  41. #define rtc_minute minute
  42. #define rtc_second second
  43. #define rtc_display_format display_format
  44. #endif
  45. static time_t get_rtc_timestamp(void);
  46. static rt_err_t set_rtc_timestamp(time_t time_stamp);
  47. /**
  48. * @brief Helper function: Convert BCD value to binary.
  49. * @param val: BCD value.
  50. * @return Binary value.
  51. */
  52. static rt_uint8_t bcd_to_bin(rt_uint8_t val)
  53. {
  54. return (val & 0x0F) + ((val >> 4) & 0x0F) * 10;
  55. }
  56. /**
  57. * @brief Helper function: Convert binary to BCD.
  58. * @param val: Binary value.
  59. * @return BCD value.
  60. */
  61. static rt_uint8_t bin_to_bcd(rt_uint8_t val)
  62. {
  63. return ((val / 10) << 4) | (val % 10);
  64. }
  65. #ifdef BSP_USING_ALARM
  66. /* RTC device for alarm callback */
  67. static rt_device_t g_rtc_device = RT_NULL;
  68. /**
  69. * @brief RTC Alarm Interrupt Handler
  70. */
  71. void RTC_Alarm_IRQHandler(void)
  72. {
  73. rt_interrupt_enter();
  74. /* Check if alarm interrupt occurred */
  75. if (rtc_flag_get(RTC_FLAG_ALARM0) != RESET)
  76. {
  77. /* Clear alarm flag */
  78. rtc_flag_clear(RTC_FLAG_ALARM0);
  79. exti_flag_clear(EXTI_17);
  80. /* Notify RTC framework about alarm event */
  81. if (g_rtc_device != RT_NULL)
  82. {
  83. rt_alarm_update(g_rtc_device, 1);
  84. }
  85. LOG_D("RTC Alarm0 triggered");
  86. }
  87. if (rtc_flag_get(RTC_FLAG_ALARM1) != RESET)
  88. {
  89. /* Clear alarm flag */
  90. rtc_flag_clear(RTC_FLAG_ALARM1);
  91. exti_flag_clear(EXTI_17);
  92. /* Notify RTC framework about alarm event */
  93. if (g_rtc_device != RT_NULL)
  94. {
  95. rt_alarm_update(g_rtc_device, 1);
  96. }
  97. LOG_D("RTC Alarm1 triggered");
  98. }
  99. rt_interrupt_leave();
  100. }
  101. #endif /* BSP_USING_ALARM */
  102. static rt_err_t gd_rtc_init(void)
  103. {
  104. /* Enable PMU and backup domain clocks */
  105. rcu_periph_clock_enable(RCU_PMU);
  106. pmu_backup_write_enable();
  107. /* Enable BKP and RTC clocks */
  108. #ifdef SOC_SERIES_GD32F10x
  109. rcu_periph_clock_enable(RCU_BKPI);
  110. #else
  111. rcu_periph_clock_enable(RCU_RTC);
  112. #endif
  113. /* Check if RTC is already initialized by backup domain reset */
  114. if (RTC_STAT & RTC_STAT_INITF)
  115. {
  116. LOG_D("RTC already initialized");
  117. return RT_EOK;
  118. }
  119. /* Reset backup domain only if RTC is not running */
  120. if ((RTC_STAT & RTC_STAT_RSYNF) == 0)
  121. {
  122. rcu_bkp_reset_enable();
  123. rcu_bkp_reset_disable();
  124. /* Re-enable clocks after reset */
  125. #ifdef SOC_SERIES_GD32F10x
  126. rcu_periph_clock_enable(RCU_BKPI);
  127. #else
  128. rcu_periph_clock_enable(RCU_RTC);
  129. #endif
  130. }
  131. /* Use LSE (32.768kHz) as RTC clock source */
  132. #define PRESCALER_S 0xFF
  133. #define PRESCALER_A 0x7F
  134. rcu_osci_on(RCU_LXTAL);
  135. if (rcu_osci_stab_wait(RCU_LXTAL) != SUCCESS)
  136. {
  137. LOG_E("LSE oscillator failed to stabilize");
  138. return -RT_ERROR;
  139. }
  140. rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
  141. LOG_D("RTC clock source: LSE (32.768kHz)");
  142. /* Wait for RTC registers synchronization */
  143. if (rtc_register_sync_wait() != SUCCESS)
  144. {
  145. LOG_E("RTC register synchronization failed");
  146. return -RT_ERROR;
  147. }
  148. /* Set default time if RTC is not initialized */
  149. if ((RTC_DATE == 0) || (RTC_TIME == 0))
  150. {
  151. time_t default_time = 1704067200; /* 2024-01-01 00:00:00 */
  152. if (set_rtc_timestamp(default_time) != RT_EOK)
  153. {
  154. LOG_E("Failed to set default RTC time");
  155. return -RT_ERROR;
  156. }
  157. LOG_D("RTC set to default time: 2024-01-01 00:00:00");
  158. }
  159. LOG_D("RTC initialization successful");
  160. return RT_EOK;
  161. }
  162. static time_t get_rtc_timestamp(void)
  163. {
  164. struct tm tm_new = {0};
  165. rtc_parameter_struct rtc_current_time;
  166. /* Wait for register synchronization before reading */
  167. if (rtc_register_sync_wait() != SUCCESS)
  168. {
  169. LOG_E("RTC sync failed before reading time");
  170. return 0;
  171. }
  172. rtc_current_time_get(&rtc_current_time);
  173. /* Convert BCD to binary and adjust year/month values */
  174. tm_new.tm_year = bcd_to_bin(rtc_current_time.rtc_year) + 100; /* RTC year starts from 2000 */
  175. tm_new.tm_mon = bcd_to_bin(rtc_current_time.rtc_month) - 1; /* tm_mon: 0-11 */
  176. tm_new.tm_mday = bcd_to_bin(rtc_current_time.rtc_date);
  177. tm_new.tm_hour = bcd_to_bin(rtc_current_time.rtc_hour);
  178. tm_new.tm_min = bcd_to_bin(rtc_current_time.rtc_minute);
  179. tm_new.tm_sec = bcd_to_bin(rtc_current_time.rtc_second);
  180. /* Convert weekday: RTC uses 1-7 (Monday-Sunday), tm uses 0-6 (Sunday-Saturday) */
  181. uint8_t rtc_wday = bcd_to_bin(rtc_current_time.rtc_day_of_week);
  182. tm_new.tm_wday = (rtc_wday == 7) ? 0 : rtc_wday; /* Sunday conversion */
  183. /* Calculate day of year */
  184. tm_new.tm_yday = 0; /* Will be calculated by timegm */
  185. tm_new.tm_isdst = 0; /* No daylight saving */
  186. /* Use timegm instead of mktime to avoid timezone issues */
  187. return timegm(&tm_new);
  188. }
  189. static rt_err_t gd_get_secs(time_t *sec)
  190. {
  191. if (sec == RT_NULL)
  192. {
  193. return -RT_EINVAL;
  194. }
  195. *sec = get_rtc_timestamp();
  196. LOG_D("RTC: get timestamp %lu", *sec);
  197. return RT_EOK;
  198. }
  199. static rt_err_t set_rtc_timestamp(time_t time_stamp)
  200. {
  201. struct tm now;
  202. rtc_parameter_struct rtc_init_struct;
  203. ErrStatus status;
  204. /* Use gmtime_r for thread safety */
  205. gmtime_r(&time_stamp, &now);
  206. if (now.tm_year < 100)
  207. {
  208. LOG_E("Year must be >= 2000");
  209. return -RT_ERROR;
  210. }
  211. /* Convert to BCD format */
  212. rtc_init_struct.rtc_year = bin_to_bcd(now.tm_year - 100); /* RTC year: 0-99 (2000-2099) */
  213. rtc_init_struct.rtc_month = bin_to_bcd(now.tm_mon + 1); /* RTC month: 1-12 */
  214. rtc_init_struct.rtc_date = bin_to_bcd(now.tm_mday);
  215. /* Convert weekday: tm_wday 0-6 (Sun-Sat) to RTC 1-7 (Mon-Sun) */
  216. rtc_init_struct.rtc_day_of_week = bin_to_bcd(now.tm_wday == 0 ? 7 : now.tm_wday);
  217. rtc_init_struct.rtc_hour = bin_to_bcd(now.tm_hour);
  218. rtc_init_struct.rtc_minute = bin_to_bcd(now.tm_min);
  219. rtc_init_struct.rtc_second = bin_to_bcd(now.tm_sec);
  220. rtc_init_struct.rtc_display_format = RTC_24HOUR;
  221. /* Use default prescaler values */
  222. rtc_init_struct.factor_asyn = PRESCALER_A;
  223. rtc_init_struct.factor_syn = PRESCALER_S;
  224. rtc_init_struct.am_pm = RTC_AM;
  225. status = rtc_init(&rtc_init_struct);
  226. if (status != SUCCESS)
  227. {
  228. LOG_E("RTC time set failed: %d", status);
  229. return -RT_ERROR;
  230. }
  231. /* Wait for synchronization after setting time */
  232. if (rtc_register_sync_wait() != SUCCESS)
  233. {
  234. LOG_E("RTC sync failed after setting time");
  235. return -RT_ERROR;
  236. }
  237. LOG_D("RTC time set successfully: %lu", time_stamp);
  238. return RT_EOK;
  239. }
  240. static rt_err_t gd_set_secs(time_t *sec)
  241. {
  242. if (sec == RT_NULL)
  243. {
  244. return -RT_EINVAL;
  245. }
  246. rt_err_t result = set_rtc_timestamp(*sec);
  247. if (result == RT_EOK)
  248. {
  249. LOG_D("RTC: set rtc_time %lu", *sec);
  250. }
  251. else
  252. {
  253. LOG_E("RTC: set rtc_time failed %lu", *sec);
  254. }
  255. return result;
  256. }
  257. #ifdef BSP_USING_ALARM
  258. static rt_err_t gd_get_alarm(struct rt_rtc_wkalarm *alarm)
  259. {
  260. if (alarm == RT_NULL)
  261. {
  262. return -RT_EINVAL;
  263. }
  264. rtc_alarm_struct rtc_alarm;
  265. /* Get current alarm configuration */
  266. rtc_alarm_get(BSP_RTC_ALARM, &rtc_alarm);
  267. /* Convert RTC alarm to RT-Thread alarm format */
  268. alarm->tm_hour = bcd_to_bin(rtc_alarm.alarm_hour);
  269. alarm->tm_min = bcd_to_bin(rtc_alarm.alarm_minute);
  270. alarm->tm_sec = bcd_to_bin(rtc_alarm.alarm_second);
  271. /* Check if alarm is enabled */
  272. uint32_t alarm_enable_bit = (BSP_RTC_ALARM == RTC_ALARM0) ? RTC_CTL_ALRM0EN : RTC_CTL_ALRM1EN;
  273. alarm->enable = (RTC_CTL & alarm_enable_bit) ? 1 : 0;
  274. LOG_D("RTC: get alarm %02d:%02d:%02d, enable: %d",
  275. alarm->tm_hour, alarm->tm_min, alarm->tm_sec, alarm->enable);
  276. return RT_EOK;
  277. }
  278. static rt_err_t gd_set_alarm(struct rt_rtc_wkalarm *alarm)
  279. {
  280. if (alarm == RT_NULL)
  281. {
  282. return -RT_EINVAL;
  283. }
  284. rtc_alarm_struct rtc_alarm;
  285. rtc_alarm_disable(BSP_RTC_ALARM);
  286. /* Initialize alarm structure */
  287. rtc_alarm.alarm_mask = RTC_ALARM_ALL_MASK;
  288. rtc_alarm.weekday_or_date = RTC_ALARM_DATE_SELECTED;
  289. rtc_alarm.alarm_day = 1; /* Not used when mask is set to ALL_MASK */
  290. rtc_alarm.alarm_hour = bin_to_bcd(alarm->tm_hour);
  291. rtc_alarm.alarm_minute = bin_to_bcd(alarm->tm_min);
  292. rtc_alarm.alarm_second = bin_to_bcd(alarm->tm_sec);
  293. rtc_alarm.am_pm = RTC_AM;
  294. /* Configure alarm */
  295. rtc_alarm_config(BSP_RTC_ALARM, &rtc_alarm);
  296. /* Enable or disable alarm */
  297. if (alarm->enable)
  298. {
  299. /* Clear any pending alarm flag first */
  300. rtc_flag_clear(BSP_ALARM_FLAG);
  301. /* Clear EXTI line 17 flag */
  302. exti_flag_clear(EXTI_17);
  303. /* Enable RTC alarm interrupt */
  304. rtc_interrupt_enable(BSP_RTC_INT_ALARM);
  305. /* Enable alarm */
  306. rtc_alarm_enable(BSP_RTC_ALARM);
  307. /* Configure EXTI line 17 for RTC alarm interrupt */
  308. exti_init(EXTI_17, EXTI_INTERRUPT, EXTI_TRIG_RISING);
  309. /* Enable RTC Alarm global interrupt in NVIC */
  310. nvic_irq_enable(RTC_Alarm_IRQn, 0, 0);
  311. LOG_D("RTC Alarm enabled with interrupt");
  312. }
  313. else
  314. {
  315. /* Disable alarm interrupt first */
  316. rtc_interrupt_disable(BSP_RTC_INT_ALARM);
  317. /* Disable alarm */
  318. rtc_alarm_disable(BSP_RTC_ALARM);
  319. /* Clear alarm flag */
  320. rtc_flag_clear(BSP_ALARM_FLAG);
  321. exti_flag_clear(EXTI_17);
  322. LOG_D("RTC Alarm disabled");
  323. }
  324. LOG_D("RTC: set alarm %02d:%02d:%02d, enable: %d",
  325. alarm->tm_hour, alarm->tm_min, alarm->tm_sec, alarm->enable);
  326. return RT_EOK;
  327. }
  328. #endif /* BSP_USING_ALARM */
  329. static const struct rt_rtc_ops gd_rtc_ops =
  330. {
  331. .init = gd_rtc_init,
  332. .get_secs = gd_get_secs,
  333. .set_secs = gd_set_secs,
  334. #ifdef BSP_USING_ALARM
  335. .set_alarm = gd_set_alarm,
  336. .get_alarm = gd_get_alarm,
  337. #endif
  338. };
  339. static rt_rtc_dev_t gd_rtc_dev;
  340. static int rt_hw_rtc_init(void)
  341. {
  342. rt_err_t result;
  343. gd_rtc_dev.ops = &gd_rtc_ops;
  344. result = rt_hw_rtc_register(&gd_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  345. if (result != RT_EOK)
  346. {
  347. LOG_E("RTC register failed: %d", result);
  348. return result;
  349. }
  350. #ifdef BSP_USING_ALARM
  351. /* Store RTC device for alarm callback */
  352. g_rtc_device = rt_device_find("rtc");
  353. if (g_rtc_device == RT_NULL)
  354. {
  355. LOG_W("RTC device not found for alarm callback");
  356. }
  357. #endif
  358. LOG_D("RTC hardware init success");
  359. return RT_EOK;
  360. }
  361. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  362. #endif /* RT_USING_RTC */