From 9609bc032affdf667dea9417c45036b38b16b261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=96=E4=BA=89?= Date: Thu, 3 Dec 2020 17:52:03 +0800 Subject: [PATCH] =?UTF-8?q?[RTC=20alarm]alarm=E5=8A=9F=E8=83=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8CSOFT=5FRTC=E5=A2=9E=E5=8A=A0alarm=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/drivers/alarm.h | 2 + components/drivers/rtc/alarm.c | 94 ++++++++++++++++++---- components/drivers/rtc/soft_rtc.c | 63 +++++++++++++-- 3 files changed, 137 insertions(+), 22 deletions(-) diff --git a/components/drivers/include/drivers/alarm.h b/components/drivers/include/drivers/alarm.h index dd59f4c1e8..e59c4142c7 100644 --- a/components/drivers/include/drivers/alarm.h +++ b/components/drivers/include/drivers/alarm.h @@ -49,6 +49,8 @@ struct rt_alarm rt_uint32_t flag; rt_alarm_callback_t callback; struct tm wktime; + + void *user_data; }; struct rt_alarm_setup diff --git a/components/drivers/rtc/alarm.c b/components/drivers/rtc/alarm.c index 53cb538321..ea1b150e7a 100644 --- a/components/drivers/rtc/alarm.c +++ b/components/drivers/rtc/alarm.c @@ -14,8 +14,16 @@ #include #include +#ifndef _WIN32 +#include +#endif + #define RT_RTC_YEARS_MAX 137 +#ifdef RT_USING_SOFT_RTC +#define RT_ALARM_DELAY 0 +#else #define RT_ALARM_DELAY 2 +#endif #define RT_ALARM_STATE_INITED 0x02 #define RT_ALARM_STATE_START 0x01 #define RT_ALARM_STATE_STOP 0x00 @@ -41,10 +49,12 @@ static rt_err_t alarm_set(struct rt_alarm *alarm) rt_err_t ret; device = rt_device_find("rtc"); + if (device == RT_NULL) { return (RT_ERROR); } + if (alarm->flag & RT_ALARM_STATE_START) wkalarm.enable = RT_TRUE; else @@ -215,7 +225,11 @@ static void alarm_update(rt_uint32_t event) { /* get time of now */ timestamp = time(RT_NULL); - localtime_r(×tamp, &now); +#ifdef _WIN32 + _gmtime32_s(&now, ×tamp); +#else + gmtime_r(×tamp, &now); +#endif for (next = _container.head.next; next != &_container.head; next = next->next) { @@ -225,7 +239,11 @@ static void alarm_update(rt_uint32_t event) } timestamp = time(RT_NULL); - localtime_r(×tamp, &now); +#ifdef _WIN32 + _gmtime32_s(&now, ×tamp); +#else + gmtime_r(×tamp, &now); +#endif sec_now = alarm_mkdaysec(&now); for (next = _container.head.next; next != &_container.head; next = next->next) @@ -233,7 +251,6 @@ static void alarm_update(rt_uint32_t event) alarm = rt_list_entry(next, struct rt_alarm, list); /* calculate seconds from 00:00:00 */ sec_alarm = alarm_mkdaysec(&alarm->wktime); - if (alarm->flag & RT_ALARM_STATE_START) { sec_tmp = sec_alarm - sec_now; @@ -257,6 +274,7 @@ static void alarm_update(rt_uint32_t event) } } } + /* enable the alarm after now first */ if (sec_next < 24 * 3600) { @@ -329,7 +347,12 @@ static rt_err_t alarm_setup(rt_alarm_t alarm, struct tm *wktime) setup = &alarm->wktime; *setup = *wktime; timestamp = time(RT_NULL); - localtime_r(×tamp, &now); + +#ifdef _WIN32 + _gmtime32_s(&now, ×tamp); +#else + gmtime_r(×tamp, &now); +#endif /* if these are a "don't care" value,we set them to now*/ if ((setup->tm_sec > 59) || (setup->tm_sec < 0)) @@ -526,21 +549,28 @@ rt_err_t rt_alarm_control(rt_alarm_t alarm, int cmd, void *arg) rt_err_t rt_alarm_start(rt_alarm_t alarm) { rt_int32_t sec_now, sec_old, sec_new; - rt_err_t ret = RT_ERROR; + rt_err_t ret = RT_EOK; time_t timestamp; struct tm now; if (alarm == RT_NULL) - return (ret); + return (RT_ERROR); rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); if (!(alarm->flag & RT_ALARM_STATE_START)) { if (alarm_setup(alarm, &alarm->wktime) != RT_EOK) + { + ret = RT_ERROR; goto _exit; + } timestamp = time(RT_NULL); - localtime_r(×tamp, &now); +#ifdef _WIN32 + _gmtime32_s(&now, ×tamp); +#else + gmtime_r(×tamp, &now); +#endif alarm->flag |= RT_ALARM_STATE_START; @@ -593,10 +623,10 @@ _exit: */ rt_err_t rt_alarm_stop(rt_alarm_t alarm) { - rt_err_t ret = RT_ERROR; + rt_err_t ret = RT_EOK; if (alarm == RT_NULL) - return (ret); + return (RT_ERROR); rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); if (!(alarm->flag & RT_ALARM_STATE_START)) goto _exit; @@ -695,23 +725,59 @@ static void rt_alarmsvc_thread_init(void *param) } } +struct _alarm_flag +{ + const char* name; + rt_uint32_t flag; +}; + +static const struct _alarm_flag _alarm_flag_tbl[] = +{ + {"N", 0xffff}, /* none */ + {"O", RT_ALARM_ONESHOT}, /* only alarm onece */ + {"D", RT_ALARM_DAILY}, /* alarm everyday */ + {"W", RT_ALARM_WEEKLY}, /* alarm weekly at Monday or Friday etc. */ + {"Mo", RT_ALARM_MONTHLY}, /* alarm monthly at someday */ + {"Y", RT_ALARM_YAERLY}, /* alarm yearly at a certain date */ + {"H", RT_ALARM_HOUR}, /* alarm each hour at a certain min:second */ + {"M", RT_ALARM_MINUTE}, /* alarm each minute at a certain second */ + {"S", RT_ALARM_SECOND}, /* alarm each second */ +}; + +static rt_uint8_t _alarm_flag_tbl_size = sizeof(_alarm_flag_tbl) / sizeof(_alarm_flag_tbl[0]); + +static rt_uint8_t get_alarm_flag_index(rt_uint32_t alarm_flag) +{ + for (rt_uint8_t index = 0; index < _alarm_flag_tbl_size; index++) + { + alarm_flag &= 0xff00; + if (alarm_flag == _alarm_flag_tbl[index].flag) + { + return index; + } + } + + return 0; +} + void rt_alarm_dump(void) { rt_list_t *next; rt_alarm_t alarm; rt_uint8_t index = 0; - rt_kprintf("| alarm_id | YYYY-MM-DD hh:mm:ss | weekday | flags |\n"); - rt_kprintf("+----------+---------------------+---------+---------+\n"); + rt_kprintf("| id | YYYY-MM-DD hh:mm:ss | week | flag | en |\n"); + rt_kprintf("+----+---------------------+------+------+----+\n"); for (next = _container.head.next; next != &_container.head; next = next->next) { alarm = rt_list_entry(next, struct rt_alarm, list); - rt_kprintf("| No %5d | %04d-%02d-%02d %02d:%02d:%02d | %7d | 0x%04x |\n", + rt_uint8_t flag_index = get_alarm_flag_index(alarm->flag); + rt_kprintf("| %2d | %04d-%02d-%02d %02d:%02d:%02d | %2d | %2s | %2d |\n", index++, alarm->wktime.tm_year + 1900, alarm->wktime.tm_mon + 1, alarm->wktime.tm_mday, alarm->wktime.tm_hour, alarm->wktime.tm_min, alarm->wktime.tm_sec, - alarm->wktime.tm_wday, alarm->flag); + alarm->wktime.tm_wday, _alarm_flag_tbl[flag_index].name, alarm->flag & RT_ALARM_STATE_START); } - rt_kprintf("+----------+---------------------+---------+---------+\n"); + rt_kprintf("+----+---------------------+------+------+----+\n"); } FINSH_FUNCTION_EXPORT_ALIAS(rt_alarm_dump, __cmd_alarm_dump, dump alarm info); diff --git a/components/drivers/rtc/soft_rtc.c b/components/drivers/rtc/soft_rtc.c index adcbf81fe3..6a04314675 100644 --- a/components/drivers/rtc/soft_rtc.c +++ b/components/drivers/rtc/soft_rtc.c @@ -11,8 +11,7 @@ #include #include #include - -#include +#include #ifdef RT_USING_SOFT_RTC @@ -28,9 +27,37 @@ static struct rt_device soft_rtc_dev; static rt_tick_t init_tick; static time_t init_time; +#ifdef RT_USING_ALARM + +static struct rt_rtc_wkalarm wkalarm; +static struct rt_timer alarm_time; + +static void alarm_timeout(void *param) +{ + rt_alarm_update(param, 1); +} + +static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm) +{ + rt_tick_t next_tick; + + if (palarm->enable) + { + next_tick = RT_TICK_PER_SECOND; + rt_timer_control(&alarm_time, RT_TIMER_CTRL_SET_TIME, &next_tick); + rt_timer_start(&alarm_time); + } + else + { + rt_timer_stop(&alarm_time); + } +} + +#endif + static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) { - time_t *time; + time_t *t; struct tm time_temp; RT_ASSERT(dev != RT_NULL); @@ -39,16 +66,27 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) switch (cmd) { case RT_DEVICE_CTRL_RTC_GET_TIME: - time = (time_t *) args; - *time = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; + t = (time_t *) args; + *t = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; break; - case RT_DEVICE_CTRL_RTC_SET_TIME: { - time = (time_t *) args; - init_time = *time - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; + t = (time_t *) args; + init_time = *t - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; +#ifdef RT_USING_ALARM + soft_rtc_alarm_update(&wkalarm); +#endif break; } +#ifdef RT_USING_ALARM + case RT_DEVICE_CTRL_RTC_GET_ALARM: + *((struct rt_rtc_wkalarm *)args) = wkalarm; + break; + case RT_DEVICE_CTRL_RTC_SET_ALARM: + wkalarm = *((struct rt_rtc_wkalarm *)args); + soft_rtc_alarm_update(&wkalarm); + break; +#endif } return RT_EOK; @@ -78,6 +116,15 @@ int rt_soft_rtc_init(void) /* make sure only one 'rtc' device */ RT_ASSERT(!rt_device_find("rtc")); +#ifdef RT_USING_ALARM + rt_timer_init(&alarm_time, + "alarm", + alarm_timeout, + &soft_rtc_dev, + 0, + RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_ONE_SHOT); +#endif + init_tick = rt_tick_get(); init_time = mktime(&time_new);