drv_rtc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. */
  10. #include <board.h>
  11. #include <sys/time.h>
  12. #define DBG_TAG "drv.rtc"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #ifdef RT_USING_RTC
  16. typedef struct {
  17. struct rt_device rtc_dev;
  18. } gd32_rtc_device;
  19. static gd32_rtc_device g_gd32_rtc_dev;
  20. /**
  21. * @brief Helper function: Convert BCD value to binary.
  22. * @param val: BCD value.
  23. * @return Binary value.
  24. */
  25. static rt_uint8_t bcd_to_bin(rt_uint8_t val)
  26. {
  27. return (val & 0x0F) + ((val >> 4) & 0x0F) * 10;
  28. }
  29. /**
  30. * @brief Helper function: Convert binary to BCD.
  31. * @param val: Binary value.
  32. * @return BCD value.
  33. */
  34. static rt_uint8_t bin_to_bcd(rt_uint8_t val)
  35. {
  36. return ((val / 10) << 4) | (val % 10);
  37. }
  38. static time_t get_rtc_timestamp(void)
  39. {
  40. #if defined SOC_SERIES_GD32E23x
  41. struct tm tm_new;
  42. rtc_parameter_struct rtc_current_time;
  43. rtc_current_time_get(&rtc_current_time);
  44. tm_new.tm_year = bcd_to_bin(rtc_current_time.rtc_year) + 100; /* tm_year: years since 1900 */
  45. tm_new.tm_mon = bcd_to_bin(rtc_current_time.rtc_month) - 1; /* tm_mon: month (0 = January, 11 = December) */
  46. tm_new.tm_mday = bcd_to_bin(rtc_current_time.rtc_date);
  47. tm_new.tm_hour = bcd_to_bin(rtc_current_time.rtc_hour);
  48. tm_new.tm_min = bcd_to_bin(rtc_current_time.rtc_minute);
  49. tm_new.tm_sec = bcd_to_bin(rtc_current_time.rtc_second);
  50. return mktime(&tm_new);
  51. #else
  52. time_t rtc_counter;
  53. rtc_counter = (time_t)rtc_counter_get();
  54. return rtc_counter;
  55. #endif
  56. }
  57. static rt_err_t set_rtc_timestamp(time_t time_stamp)
  58. {
  59. #if defined SOC_SERIES_GD32E23x
  60. struct tm *p_tm;
  61. rtc_parameter_struct rtc_init_struct;
  62. p_tm = gmtime(&time_stamp);
  63. /* GD32 RTC uses year starting from 2000; thus tm_year must be at least 100 (i.e., 2000 - 1900) */
  64. if (p_tm->tm_year < 100)
  65. {
  66. return -RT_ERROR;
  67. }
  68. rtc_init_struct.rtc_year = bin_to_bcd(p_tm->tm_year - 100);
  69. rtc_init_struct.rtc_month = bin_to_bcd(p_tm->tm_mon + 1);
  70. rtc_init_struct.rtc_date = bin_to_bcd(p_tm->tm_mday);
  71. rtc_init_struct.rtc_day_of_week = bin_to_bcd(p_tm->tm_wday == 0 ? 7 : p_tm->tm_wday);
  72. rtc_init_struct.rtc_hour = bin_to_bcd(p_tm->tm_hour);
  73. rtc_init_struct.rtc_minute = bin_to_bcd(p_tm->tm_min);
  74. rtc_init_struct.rtc_second = bin_to_bcd(p_tm->tm_sec);
  75. rtc_init_struct.rtc_display_format = RTC_24HOUR;
  76. #if defined(BSP_RTC_USING_LSI)
  77. rtc_init_struct.rtc_factor_asyn = 39;
  78. rtc_init_struct.rtc_factor_syn = 999;
  79. #elif defined(BSP_RTC_USING_LSE)
  80. rtc_init_struct.rtc_factor_asyn = 127;
  81. rtc_init_struct.rtc_factor_syn = 255;
  82. #endif
  83. if (rtc_init(&rtc_init_struct) != SUCCESS)
  84. {
  85. LOG_E("Failed to set RTC time.");
  86. return -RT_ERROR;
  87. }
  88. return RT_EOK;
  89. #else
  90. uint32_t rtc_counter;
  91. rtc_counter = (uint32_t)time_stamp;
  92. /* wait until LWOFF bit in RTC_CTL to 1 */
  93. rtc_lwoff_wait();
  94. /* enter configure mode */
  95. rtc_configuration_mode_enter();
  96. /* write data to rtc register */
  97. rtc_counter_set(rtc_counter);
  98. /* exit configure mode */
  99. rtc_configuration_mode_exit();
  100. /* wait until LWOFF bit in RTC_CTL to 1 */
  101. rtc_lwoff_wait();
  102. return RT_EOK;
  103. #endif
  104. }
  105. static rt_err_t rt_gd32_rtc_control(rt_device_t dev, int cmd, void *args)
  106. {
  107. rt_err_t result = RT_EOK;
  108. RT_ASSERT(dev != RT_NULL);
  109. switch (cmd)
  110. {
  111. case RT_DEVICE_CTRL_RTC_GET_TIME:
  112. *(rt_uint32_t *)args = get_rtc_timestamp();
  113. break;
  114. case RT_DEVICE_CTRL_RTC_SET_TIME:
  115. if (set_rtc_timestamp(*(rt_uint32_t *)args))
  116. {
  117. result = -RT_ERROR;
  118. }
  119. break;
  120. }
  121. return result;
  122. }
  123. #ifdef RT_USING_DEVICE_OPS
  124. const static struct rt_device_ops g_gd32_rtc_ops =
  125. {
  126. RT_NULL,
  127. RT_NULL,
  128. RT_NULL,
  129. RT_NULL,
  130. RT_NULL,
  131. rt_gd32_rtc_control
  132. };
  133. #endif
  134. static int rt_hw_rtc_init(void)
  135. {
  136. rt_err_t ret;
  137. time_t rtc_counter;
  138. rcu_periph_clock_enable(RCU_PMU);
  139. pmu_backup_write_enable();
  140. #ifndef SOC_SERIES_GD32E23x
  141. rcu_periph_clock_enable(RCU_BKPI);
  142. #endif
  143. rtc_counter = get_rtc_timestamp();
  144. /* once the rtc clock source has been selected, if can't be changed
  145. * anymore unless the Backup domain is reset */
  146. rcu_bkp_reset_enable();
  147. rcu_bkp_reset_disable();
  148. rcu_periph_clock_enable(RCU_RTC);
  149. #if defined(BSP_RTC_USING_LSE)
  150. rcu_osci_on(RCU_LXTAL);
  151. if (SUCCESS == rcu_osci_stab_wait(RCU_LXTAL))
  152. {
  153. /* set lxtal as rtc clock source */
  154. rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
  155. }
  156. #elif defined(BSP_RTC_USING_LSI)
  157. rcu_osci_on(RCU_IRC40K);
  158. if (SUCCESS == rcu_osci_stab_wait(RCU_IRC40K))
  159. {
  160. /* set IRC40K as rtc clock source */
  161. rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
  162. }
  163. #endif
  164. set_rtc_timestamp(rtc_counter);
  165. #ifdef RT_USING_DEVICE_OPS
  166. g_gd32_rtc_dev.rtc_dev.ops = &g_gd32_rtc_ops;
  167. #else
  168. g_gd32_rtc_dev.rtc_dev.init = RT_NULL;
  169. g_gd32_rtc_dev.rtc_dev.open = RT_NULL;
  170. g_gd32_rtc_dev.rtc_dev.close = RT_NULL;
  171. g_gd32_rtc_dev.rtc_dev.read = RT_NULL;
  172. g_gd32_rtc_dev.rtc_dev.write = RT_NULL;
  173. g_gd32_rtc_dev.rtc_dev.control = rt_gd32_rtc_control;
  174. #endif
  175. g_gd32_rtc_dev.rtc_dev.type = RT_Device_Class_RTC;
  176. g_gd32_rtc_dev.rtc_dev.rx_indicate = RT_NULL;
  177. g_gd32_rtc_dev.rtc_dev.tx_complete = RT_NULL;
  178. g_gd32_rtc_dev.rtc_dev.user_data = RT_NULL;
  179. ret = rt_device_register(&g_gd32_rtc_dev.rtc_dev, "rtc", \
  180. RT_DEVICE_FLAG_RDWR);
  181. if (ret != RT_EOK)
  182. {
  183. LOG_E("failed register internal rtc device, err=%d", ret);
  184. }
  185. return ret;
  186. }
  187. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  188. #endif