|
| 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 | + * 2021-02-20 iysheng first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <board.h> |
| 12 | +#include <sys/time.h> |
| 13 | +#include <drivers/drv_comm.h> |
| 14 | + |
| 15 | +#define DBG_TAG "drv.rtc" |
| 16 | +#define DBG_LVL DBG_INFO |
| 17 | + |
| 18 | +#include <rtdbg.h> |
| 19 | + |
| 20 | +#ifdef RT_USING_RTC |
| 21 | + |
| 22 | +typedef struct { |
| 23 | + struct rt_device rtc_dev; |
| 24 | +} gd32_rtc_device; |
| 25 | + |
| 26 | +static gd32_rtc_device g_gd32_rtc_dev; |
| 27 | + |
| 28 | +static time_t get_rtc_timestamp(void) |
| 29 | +{ |
| 30 | + time_t rtc_counter; |
| 31 | + |
| 32 | + rtc_counter = (time_t)RTC_GetCounter(); |
| 33 | + |
| 34 | + return rtc_counter; |
| 35 | +} |
| 36 | + |
| 37 | +static rt_err_t set_rtc_timestamp(time_t time_stamp) |
| 38 | +{ |
| 39 | + uint32_t rtc_counter; |
| 40 | + |
| 41 | + rtc_counter = (uint32_t)time_stamp; |
| 42 | + |
| 43 | + /* wait until LWOFF bit in RTC_CTL to 1 */ |
| 44 | + RTC_WaitLWOFF(); |
| 45 | + /* enter configure mode */ |
| 46 | + RTC_EnterConfigMode(); |
| 47 | + /* write data to rtc register */ |
| 48 | + RTC_SetCounter(rtc_counter); |
| 49 | + /* exit configure mode */ |
| 50 | + RTC_ExitConfigMode(); |
| 51 | + /* wait until LWOFF bit in RTC_CTL to 1 */ |
| 52 | + RTC_WaitLWOFF(); |
| 53 | + |
| 54 | + return RT_EOK; |
| 55 | +} |
| 56 | + |
| 57 | +static rt_err_t rt_gd32_rtc_control(rt_device_t dev, int cmd, void *args) |
| 58 | +{ |
| 59 | + rt_err_t result = RT_EOK; |
| 60 | + |
| 61 | + RT_ASSERT(dev != RT_NULL); |
| 62 | + switch (cmd) |
| 63 | + { |
| 64 | + case RT_DEVICE_CTRL_RTC_GET_TIME: |
| 65 | + *(rt_uint32_t *)args = get_rtc_timestamp(); |
| 66 | + break; |
| 67 | + |
| 68 | + case RT_DEVICE_CTRL_RTC_SET_TIME: |
| 69 | + if (set_rtc_timestamp(*(rt_uint32_t *)args)) |
| 70 | + { |
| 71 | + result = -RT_ERROR; |
| 72 | + } |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +#ifdef RT_USING_DEVICE_OPS |
| 80 | +const static struct rt_device_ops g_gd32_rtc_ops = |
| 81 | +{ |
| 82 | + RT_NULL, |
| 83 | + RT_NULL, |
| 84 | + RT_NULL, |
| 85 | + RT_NULL, |
| 86 | + RT_NULL, |
| 87 | + rt_gd32_rtc_control |
| 88 | +}; |
| 89 | +#endif |
| 90 | + |
| 91 | +static int rt_hw_rtc_init(void) |
| 92 | +{ |
| 93 | + rt_err_t ret; |
| 94 | + time_t rtc_counter; |
| 95 | + |
| 96 | + rcu_periph_clock_enable(RCU_PMU); |
| 97 | + PWR_BackupAccess_Enable(ENABLE); |
| 98 | + rcu_periph_clock_enable(RCU_BKPI); |
| 99 | + |
| 100 | + rtc_counter = get_rtc_timestamp(); |
| 101 | + /* once the rtc clock source has been selected, if can't be changed |
| 102 | + * anymore unless the Backup domain is reset */ |
| 103 | + rcu_bkp_reset_enable(); |
| 104 | + rcu_bkp_reset_disable(); |
| 105 | + rcu_periph_clock_enable(RCU_RTC); |
| 106 | + rcu_osci_on(RCU_LXTAL); |
| 107 | + if (SUCCESS == rcu_osci_stab_wait(RCU_LXTAL)) |
| 108 | + { |
| 109 | + /* set lxtal as rtc clock source */ |
| 110 | + rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); |
| 111 | + } |
| 112 | + set_rtc_timestamp(rtc_counter); |
| 113 | + |
| 114 | +#ifdef RT_USING_DEVICE_OPS |
| 115 | + g_gd32_rtc_dev.rtc_dev.ops = &g_gd32_rtc_ops; |
| 116 | +#else |
| 117 | + g_gd32_rtc_dev.rtc_dev.init = RT_NULL; |
| 118 | + g_gd32_rtc_dev.rtc_dev.open = RT_NULL; |
| 119 | + g_gd32_rtc_dev.rtc_dev.close = RT_NULL; |
| 120 | + g_gd32_rtc_dev.rtc_dev.read = RT_NULL; |
| 121 | + g_gd32_rtc_dev.rtc_dev.write = RT_NULL; |
| 122 | + g_gd32_rtc_dev.rtc_dev.control = rt_gd32_rtc_control; |
| 123 | +#endif |
| 124 | + g_gd32_rtc_dev.rtc_dev.type = RT_Device_Class_RTC; |
| 125 | + g_gd32_rtc_dev.rtc_dev.rx_indicate = RT_NULL; |
| 126 | + g_gd32_rtc_dev.rtc_dev.tx_complete = RT_NULL; |
| 127 | + g_gd32_rtc_dev.rtc_dev.user_data = RT_NULL; |
| 128 | + |
| 129 | + ret = rt_device_register(&g_gd32_rtc_dev.rtc_dev, "rtc", \ |
| 130 | + RT_DEVICE_FLAG_RDWR); |
| 131 | + if (ret != RT_EOK) |
| 132 | + { |
| 133 | + LOG_E("failed register internal rtc device, err=%d", ret); |
| 134 | + } |
| 135 | + |
| 136 | + return ret; |
| 137 | +} |
| 138 | +INIT_DEVICE_EXPORT(rt_hw_rtc_init); |
| 139 | +#endif |
0 commit comments