diff options
| author | cliff-cmc <[email protected]> | 2021-02-26 11:09:27 +0800 |
|---|---|---|
| committer | Cliff Chen <[email protected]> | 2021-02-26 11:45:19 +0800 |
| commit | eea0c3d74a40f73235da45c6f77e8b411ac94cf2 (patch) | |
| tree | a6906f1aed7118ab53dbe18f2ee4727a754583af /components/drivers/rtc | |
| parent | 1a9ecf27f31277750d92c921b62503f75275e122 (diff) | |
| parent | fae0fa8eea4c0e797940bf1eddc8ee6c1711734a (diff) | |
Add memtrace command for memheap.c
Diffstat (limited to 'components/drivers/rtc')
| -rw-r--r-- | components/drivers/rtc/alarm.c | 236 | ||||
| -rw-r--r-- | components/drivers/rtc/rtc.c | 40 | ||||
| -rw-r--r-- | components/drivers/rtc/soft_rtc.c | 67 |
3 files changed, 296 insertions, 47 deletions
diff --git a/components/drivers/rtc/alarm.c b/components/drivers/rtc/alarm.c index c497ebf727..d8e4e468a7 100644 --- a/components/drivers/rtc/alarm.c +++ b/components/drivers/rtc/alarm.c @@ -7,13 +7,20 @@ * Date Author Notes * 2012-10-27 heyuanjie87 first version. * 2013-05-17 aozima initial alarm event & mutex in system init. + * 2020-10-15 zhangsz add alarm flags hour minute second. + * 2020-11-09 zhangsz fix alarm set when modify rtc time. */ #include <rtthread.h> #include <rtdevice.h> +#include <sys/time.h> #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 @@ -39,10 +46,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 @@ -86,8 +95,8 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now) { case RT_ALARM_ONESHOT: { - sec_alarm = mktime(&alarm->wktime); - sec_now = mktime(now); + sec_alarm = timegm(&alarm->wktime); + sec_now = timegm(now); if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm)) { /* stop alarm */ @@ -97,6 +106,61 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now) } } break; + case RT_ALARM_SECOND: + { + alarm->wktime.tm_hour = now->tm_hour; + alarm->wktime.tm_min = now->tm_min; + alarm->wktime.tm_sec = now->tm_sec + 1; + if (alarm->wktime.tm_sec > 59) + { + alarm->wktime.tm_sec = 0; + alarm->wktime.tm_min = alarm->wktime.tm_min + 1; + if (alarm->wktime.tm_min > 59) + { + alarm->wktime.tm_min = 0; + alarm->wktime.tm_hour = alarm->wktime.tm_hour + 1; + if (alarm->wktime.tm_hour > 23) + { + alarm->wktime.tm_hour = 0; + } + } + } + wakeup = RT_TRUE; + } + break; + case RT_ALARM_MINUTE: + { + alarm->wktime.tm_hour = now->tm_hour; + if (alarm->wktime.tm_sec == now->tm_sec) + { + alarm->wktime.tm_min = now->tm_min + 1; + if (alarm->wktime.tm_min > 59) + { + alarm->wktime.tm_min = 0; + alarm->wktime.tm_hour = alarm->wktime.tm_hour + 1; + if (alarm->wktime.tm_hour > 23) + { + alarm->wktime.tm_hour = 0; + } + } + wakeup = RT_TRUE; + } + } + break; + case RT_ALARM_HOUR: + { + if ((alarm->wktime.tm_min == now->tm_min) && + (alarm->wktime.tm_sec == now->tm_sec)) + { + alarm->wktime.tm_hour = now->tm_hour + 1; + if (alarm->wktime.tm_hour > 23) + { + alarm->wktime.tm_hour = 0; + } + wakeup = RT_TRUE; + } + } + break; case RT_ALARM_DAILY: { if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm)) @@ -158,7 +222,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) { @@ -168,7 +236,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) @@ -176,8 +248,7 @@ 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) && (alarm != _container.current)) + if (alarm->flag & RT_ALARM_STATE_START) { sec_tmp = sec_alarm - sec_now; if (sec_tmp > 0) @@ -200,6 +271,7 @@ static void alarm_update(rt_uint32_t event) } } } + /* enable the alarm after now first */ if (sec_next < 24 * 3600) { @@ -212,6 +284,11 @@ static void alarm_update(rt_uint32_t event) if (alarm_set(alm_prev) == RT_EOK) _container.current = alm_prev; } + else + { + if (_container.current != RT_NULL) + alarm_set(_container.current); + } } rt_mutex_release(&_container.mutex); } @@ -267,7 +344,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)) @@ -279,6 +361,51 @@ static rt_err_t alarm_setup(rt_alarm_t alarm, struct tm *wktime) switch (alarm->flag & 0xFF00) { + case RT_ALARM_SECOND: + { + alarm->wktime.tm_hour = now.tm_hour; + alarm->wktime.tm_min = now.tm_min; + alarm->wktime.tm_sec = now.tm_sec + 1; + if (alarm->wktime.tm_sec > 59) + { + alarm->wktime.tm_sec = 0; + alarm->wktime.tm_min = alarm->wktime.tm_min + 1; + if (alarm->wktime.tm_min > 59) + { + alarm->wktime.tm_min = 0; + alarm->wktime.tm_hour = alarm->wktime.tm_hour + 1; + if (alarm->wktime.tm_hour > 23) + { + alarm->wktime.tm_hour = 0; + } + } + } + } + break; + case RT_ALARM_MINUTE: + { + alarm->wktime.tm_hour = now.tm_hour; + alarm->wktime.tm_min = now.tm_min + 1; + if (alarm->wktime.tm_min > 59) + { + alarm->wktime.tm_min = 0; + alarm->wktime.tm_hour = alarm->wktime.tm_hour + 1; + if (alarm->wktime.tm_hour > 23) + { + alarm->wktime.tm_hour = 0; + } + } + } + break; + case RT_ALARM_HOUR: + { + alarm->wktime.tm_hour = now.tm_hour + 1; + if (alarm->wktime.tm_hour > 23) + { + alarm->wktime.tm_hour = 0; + } + } + break; case RT_ALARM_DAILY: { /* do nothing but needed */ @@ -419,24 +546,31 @@ 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_INITED)) + + if (!(alarm->flag & RT_ALARM_STATE_START)) { if (alarm_setup(alarm, &alarm->wktime) != RT_EOK) + { + ret = RT_ERROR; goto _exit; - } - if ((alarm->flag & 0x01) == RT_ALARM_STATE_STOP) - { + } + 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; + /* set alarm */ if (_container.current == RT_NULL) { @@ -486,10 +620,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; @@ -518,10 +652,10 @@ _exit: */ rt_err_t rt_alarm_delete(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); /* stop the alarm */ alarm->flag &= ~RT_ALARM_STATE_START; @@ -551,9 +685,11 @@ rt_alarm_t rt_alarm_create(rt_alarm_callback_t callback, struct rt_alarm_setup * if (setup == RT_NULL) return (RT_NULL); + alarm = rt_malloc(sizeof(struct rt_alarm)); if (alarm == RT_NULL) return (RT_NULL); + rt_list_init(&alarm->list); alarm->wktime = setup->wktime; @@ -586,13 +722,69 @@ 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("| 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_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_tbl[flag_index].name, alarm->flag & RT_ALARM_STATE_START); + } + rt_kprintf("+----+---------------------+------+------+----+\n"); +} + +FINSH_FUNCTION_EXPORT_ALIAS(rt_alarm_dump, __cmd_alarm_dump, dump alarm info); /** \brief initialize alarm service system * * \param none * \return none */ -void rt_alarm_system_init(void) +int rt_alarm_system_init(void) { rt_thread_t tid; @@ -602,8 +794,12 @@ void rt_alarm_system_init(void) tid = rt_thread_create("alarmsvc", rt_alarmsvc_thread_init, RT_NULL, - 512, 8, 1); + 2048, 10, 5); if (tid != RT_NULL) rt_thread_startup(tid); + + return 0; } + +INIT_PREV_EXPORT(rt_alarm_system_init); #endif diff --git a/components/drivers/rtc/rtc.c b/components/drivers/rtc/rtc.c index e17c0fe7dd..ef688ac3cd 100644 --- a/components/drivers/rtc/rtc.c +++ b/components/drivers/rtc/rtc.c @@ -11,7 +11,7 @@ * 2018-02-16 armink add auto sync time by NTP */ -#include <time.h> +#include <sys/time.h> #include <string.h> #include <rtthread.h> @@ -30,7 +30,7 @@ #endif /* RTC_SYNC_USING_NTP */ /** - * Set system date(time not modify). + * Set system date(time not modify, local timezone). * * @param rt_uint32_t year e.g: 2012. * @param rt_uint32_t month e.g: 12 (1~12). @@ -52,10 +52,10 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) /* lock scheduler. */ rt_enter_critical(); - /* converts calendar time time into local time. */ + /* converts calendar time into local time. */ p_tm = localtime(&now); /* copy the statically located variable */ - memcpy(&tm_new, p_tm, sizeof(struct tm)); + rt_memcpy(&tm_new, p_tm, sizeof(struct tm)); /* unlock scheduler. */ rt_exit_critical(); @@ -64,7 +64,7 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */ tm_new.tm_mday = day; - /* converts the local time in time to calendar time. */ + /* converts the local time into the calendar time. */ now = mktime(&tm_new); device = rt_device_find("rtc"); @@ -80,7 +80,7 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) } /** - * Set system time(date not modify). + * Set system time(date not modify, local timezone). * * @param rt_uint32_t hour e.g: 0~23. * @param rt_uint32_t minute e.g: 0~59. @@ -102,10 +102,10 @@ rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) /* lock scheduler. */ rt_enter_critical(); - /* converts calendar time time into local time. */ + /* converts calendar time into local time. */ p_tm = localtime(&now); /* copy the statically located variable */ - memcpy(&tm_new, p_tm, sizeof(struct tm)); + rt_memcpy(&tm_new, p_tm, sizeof(struct tm)); /* unlock scheduler. */ rt_exit_critical(); @@ -114,7 +114,7 @@ rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) tm_new.tm_min = minute; tm_new.tm_sec = second; - /* converts the local time in time to calendar time. */ + /* converts the local time into the calendar time. */ now = mktime(&tm_new); device = rt_device_find("rtc"); @@ -174,19 +174,25 @@ INIT_COMPONENT_EXPORT(rt_rtc_ntp_sync_init); #include <finsh.h> #include <rtdevice.h> +/** + * show date and time (local timezone) + */ void list_date(void) { time_t now; now = time(RT_NULL); - rt_kprintf("%s\n", ctime(&now)); + rt_kprintf("%.*s\n", 25, ctime(&now)); } -FINSH_FUNCTION_EXPORT(list_date, show date and time.) +FINSH_FUNCTION_EXPORT(list_date, show date and time (local timezone)) +FINSH_FUNCTION_EXPORT(set_date, set date(local timezone) e.g: set_date(2010,2,28)) +FINSH_FUNCTION_EXPORT(set_time, set time(local timezone) e.g: set_time(23,59,59)) -FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28)) -FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59)) #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) +/** + * get date and time or set (local timezone) [year month day hour min sec] + */ static void date(uint8_t argc, char **argv) { if (argc == 1) @@ -194,7 +200,7 @@ static void date(uint8_t argc, char **argv) time_t now; /* output current time */ now = time(RT_NULL); - rt_kprintf("%s", ctime(&now)); + rt_kprintf("%.*s", 25, ctime(&now)); } else if (argc >= 7) { @@ -246,9 +252,9 @@ static void date(uint8_t argc, char **argv) rt_kprintf("e.g: date 2018 01 01 23 59 59 or date\n"); } } -MSH_CMD_EXPORT(date, get date and time or set [year month day hour min sec]); -#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */ +MSH_CMD_EXPORT(list_date, show date and time (local timezone)) +MSH_CMD_EXPORT(date, get date and time or set (local timezone) [year month day hour min sec]) +#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */ #endif /* RT_USING_FINSH */ - #endif /* RT_USING_RTC */ diff --git a/components/drivers/rtc/soft_rtc.c b/components/drivers/rtc/soft_rtc.c index adcbf81fe3..240301f683 100644 --- a/components/drivers/rtc/soft_rtc.c +++ b/components/drivers/rtc/soft_rtc.c @@ -8,11 +8,10 @@ * 2018-01-30 armink the first version */ -#include <time.h> +#include <sys/time.h> #include <string.h> #include <rtthread.h> - -#include <drivers/rtc.h> +#include <rtdevice.h> #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,8 +116,17 @@ 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); + init_time = timegm(&time_new); soft_rtc_dev.type = RT_Device_Class_RTC; |
