|
| 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-04-13 armink the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <sys/time.h> |
| 12 | +#include <string.h> |
| 13 | +#include <rtthread.h> |
| 14 | +#include <rtdevice.h> |
| 15 | + |
| 16 | +#ifdef RT_USING_RTC |
| 17 | + |
| 18 | +static struct rt_device rtc_dev; |
| 19 | + |
| 20 | +#ifdef RT_USING_ALARM |
| 21 | + |
| 22 | +static struct rt_rtc_wkalarm wkalarm; |
| 23 | +static struct rt_timer alarm_time; |
| 24 | + |
| 25 | +static void alarm_timeout(void *param) |
| 26 | +{ |
| 27 | + rt_alarm_update(param, 1); |
| 28 | +} |
| 29 | + |
| 30 | +static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm) |
| 31 | +{ |
| 32 | + rt_tick_t next_tick; |
| 33 | + |
| 34 | + if (palarm->enable) |
| 35 | + { |
| 36 | + next_tick = RT_TICK_PER_SECOND; |
| 37 | + rt_timer_control(&alarm_time, RT_TIMER_CTRL_SET_TIME, &next_tick); |
| 38 | + rt_timer_start(&alarm_time); |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + rt_timer_stop(&alarm_time); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#endif |
| 47 | + |
| 48 | +static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) |
| 49 | +{ |
| 50 | + __time32_t *t; |
| 51 | + struct tm *newtime; |
| 52 | + |
| 53 | + RT_ASSERT(dev != RT_NULL); |
| 54 | + |
| 55 | + switch (cmd) |
| 56 | + { |
| 57 | + case RT_DEVICE_CTRL_RTC_GET_TIME: |
| 58 | + { |
| 59 | + t = (__time32_t *)args; |
| 60 | + _time32(t); |
| 61 | + /* TODO The libc time module not support timezone now. So get the time from locatime. */ |
| 62 | + newtime = _localtime32(t); |
| 63 | + *t = _mkgmtime32(newtime); |
| 64 | + break; |
| 65 | + } |
| 66 | + case RT_DEVICE_CTRL_RTC_SET_TIME: |
| 67 | + { |
| 68 | +#ifdef RT_USING_ALARM |
| 69 | + soft_rtc_alarm_update(&wkalarm); |
| 70 | +#endif |
| 71 | + break; |
| 72 | + } |
| 73 | +#ifdef RT_USING_ALARM |
| 74 | + case RT_DEVICE_CTRL_RTC_GET_ALARM: |
| 75 | + *((struct rt_rtc_wkalarm *)args) = wkalarm; |
| 76 | + break; |
| 77 | + case RT_DEVICE_CTRL_RTC_SET_ALARM: |
| 78 | + wkalarm = *((struct rt_rtc_wkalarm *)args); |
| 79 | + soft_rtc_alarm_update(&wkalarm); |
| 80 | + break; |
| 81 | +#endif |
| 82 | + } |
| 83 | + |
| 84 | + return RT_EOK; |
| 85 | +} |
| 86 | + |
| 87 | +#ifdef RT_USING_DEVICE_OPS |
| 88 | +const static struct rt_device_ops soft_rtc_ops = |
| 89 | +{ |
| 90 | + RT_NULL, |
| 91 | + RT_NULL, |
| 92 | + RT_NULL, |
| 93 | + RT_NULL, |
| 94 | + RT_NULL, |
| 95 | + soft_rtc_control |
| 96 | +}; |
| 97 | +#endif |
| 98 | + |
| 99 | +int rt_win_rtc_init(void) |
| 100 | +{ |
| 101 | + static rt_bool_t init_ok = RT_FALSE; |
| 102 | + |
| 103 | + if (init_ok) |
| 104 | + { |
| 105 | + return 0; |
| 106 | + } |
| 107 | + /* make sure only one 'rtc' device */ |
| 108 | + RT_ASSERT(!rt_device_find("rtc")); |
| 109 | + |
| 110 | +#ifdef RT_USING_ALARM |
| 111 | + rt_timer_init(&alarm_time, |
| 112 | + "alarm", |
| 113 | + alarm_timeout, |
| 114 | + &rtc_dev, |
| 115 | + 0, |
| 116 | + RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT); |
| 117 | +#endif |
| 118 | + |
| 119 | + rtc_dev.type = RT_Device_Class_RTC; |
| 120 | + |
| 121 | + /* register rtc device */ |
| 122 | +#ifdef RT_USING_DEVICE_OPS |
| 123 | + rtc_dev.ops = &soft_rtc_ops; |
| 124 | +#else |
| 125 | + rtc_dev.init = RT_NULL; |
| 126 | + rtc_dev.open = RT_NULL; |
| 127 | + rtc_dev.close = RT_NULL; |
| 128 | + rtc_dev.read = RT_NULL; |
| 129 | + rtc_dev.write = RT_NULL; |
| 130 | + rtc_dev.control = soft_rtc_control; |
| 131 | +#endif |
| 132 | + |
| 133 | + /* no private */ |
| 134 | + rtc_dev.user_data = RT_NULL; |
| 135 | + |
| 136 | + rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR); |
| 137 | + |
| 138 | + init_ok = RT_TRUE; |
| 139 | + |
| 140 | + return 0; |
| 141 | +} |
| 142 | +INIT_BOARD_EXPORT(rt_win_rtc_init); |
| 143 | + |
| 144 | +#endif /* RT_USING_RTC */ |
0 commit comments