Merge pull request #4114 from zhangsz0516/zhangsz_1203

[RTC alarm]alarm功能优化,SOFT_RTC增加alarm功能
This commit is contained in:
Bernard Xiong 2020-12-04 11:51:56 +08:00 committed by GitHub
commit 21116a56ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 22 deletions

View File

@ -49,6 +49,8 @@ struct rt_alarm
rt_uint32_t flag; rt_uint32_t flag;
rt_alarm_callback_t callback; rt_alarm_callback_t callback;
struct tm wktime; struct tm wktime;
void *user_data;
}; };
struct rt_alarm_setup struct rt_alarm_setup

View File

@ -14,8 +14,16 @@
#include <rtthread.h> #include <rtthread.h>
#include <rtdevice.h> #include <rtdevice.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#define RT_RTC_YEARS_MAX 137 #define RT_RTC_YEARS_MAX 137
#ifdef RT_USING_SOFT_RTC
#define RT_ALARM_DELAY 0
#else
#define RT_ALARM_DELAY 2 #define RT_ALARM_DELAY 2
#endif
#define RT_ALARM_STATE_INITED 0x02 #define RT_ALARM_STATE_INITED 0x02
#define RT_ALARM_STATE_START 0x01 #define RT_ALARM_STATE_START 0x01
#define RT_ALARM_STATE_STOP 0x00 #define RT_ALARM_STATE_STOP 0x00
@ -41,10 +49,12 @@ static rt_err_t alarm_set(struct rt_alarm *alarm)
rt_err_t ret; rt_err_t ret;
device = rt_device_find("rtc"); device = rt_device_find("rtc");
if (device == RT_NULL) if (device == RT_NULL)
{ {
return (RT_ERROR); return (RT_ERROR);
} }
if (alarm->flag & RT_ALARM_STATE_START) if (alarm->flag & RT_ALARM_STATE_START)
wkalarm.enable = RT_TRUE; wkalarm.enable = RT_TRUE;
else else
@ -215,7 +225,11 @@ static void alarm_update(rt_uint32_t event)
{ {
/* get time of now */ /* get time of now */
timestamp = time(RT_NULL); timestamp = time(RT_NULL);
localtime_r(&timestamp, &now); #ifdef _WIN32
_gmtime32_s(&now, &timestamp);
#else
gmtime_r(&timestamp, &now);
#endif
for (next = _container.head.next; next != &_container.head; next = next->next) 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); timestamp = time(RT_NULL);
localtime_r(&timestamp, &now); #ifdef _WIN32
_gmtime32_s(&now, &timestamp);
#else
gmtime_r(&timestamp, &now);
#endif
sec_now = alarm_mkdaysec(&now); sec_now = alarm_mkdaysec(&now);
for (next = _container.head.next; next != &_container.head; next = next->next) 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); alarm = rt_list_entry(next, struct rt_alarm, list);
/* calculate seconds from 00:00:00 */ /* calculate seconds from 00:00:00 */
sec_alarm = alarm_mkdaysec(&alarm->wktime); sec_alarm = alarm_mkdaysec(&alarm->wktime);
if (alarm->flag & RT_ALARM_STATE_START) if (alarm->flag & RT_ALARM_STATE_START)
{ {
sec_tmp = sec_alarm - sec_now; sec_tmp = sec_alarm - sec_now;
@ -257,6 +274,7 @@ static void alarm_update(rt_uint32_t event)
} }
} }
} }
/* enable the alarm after now first */ /* enable the alarm after now first */
if (sec_next < 24 * 3600) 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 = &alarm->wktime;
*setup = *wktime; *setup = *wktime;
timestamp = time(RT_NULL); timestamp = time(RT_NULL);
localtime_r(&timestamp, &now);
#ifdef _WIN32
_gmtime32_s(&now, &timestamp);
#else
gmtime_r(&timestamp, &now);
#endif
/* if these are a "don't care" value,we set them to now*/ /* if these are a "don't care" value,we set them to now*/
if ((setup->tm_sec > 59) || (setup->tm_sec < 0)) 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_err_t rt_alarm_start(rt_alarm_t alarm)
{ {
rt_int32_t sec_now, sec_old, sec_new; rt_int32_t sec_now, sec_old, sec_new;
rt_err_t ret = RT_ERROR; rt_err_t ret = RT_EOK;
time_t timestamp; time_t timestamp;
struct tm now; struct tm now;
if (alarm == RT_NULL) if (alarm == RT_NULL)
return (ret); return (RT_ERROR);
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
if (!(alarm->flag & RT_ALARM_STATE_START)) if (!(alarm->flag & RT_ALARM_STATE_START))
{ {
if (alarm_setup(alarm, &alarm->wktime) != RT_EOK) if (alarm_setup(alarm, &alarm->wktime) != RT_EOK)
{
ret = RT_ERROR;
goto _exit; goto _exit;
}
timestamp = time(RT_NULL); timestamp = time(RT_NULL);
localtime_r(&timestamp, &now); #ifdef _WIN32
_gmtime32_s(&now, &timestamp);
#else
gmtime_r(&timestamp, &now);
#endif
alarm->flag |= RT_ALARM_STATE_START; alarm->flag |= RT_ALARM_STATE_START;
@ -593,10 +623,10 @@ _exit:
*/ */
rt_err_t rt_alarm_stop(rt_alarm_t alarm) 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) if (alarm == RT_NULL)
return (ret); return (RT_ERROR);
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER); rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
if (!(alarm->flag & RT_ALARM_STATE_START)) if (!(alarm->flag & RT_ALARM_STATE_START))
goto _exit; 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) void rt_alarm_dump(void)
{ {
rt_list_t *next; rt_list_t *next;
rt_alarm_t alarm; rt_alarm_t alarm;
rt_uint8_t index = 0; rt_uint8_t index = 0;
rt_kprintf("| alarm_id | YYYY-MM-DD hh:mm:ss | weekday | flags |\n"); rt_kprintf("| id | YYYY-MM-DD hh:mm:ss | week | flag | en |\n");
rt_kprintf("+----------+---------------------+---------+---------+\n"); rt_kprintf("+----+---------------------+------+------+----+\n");
for (next = _container.head.next; next != &_container.head; next = next->next) for (next = _container.head.next; next != &_container.head; next = next->next)
{ {
alarm = rt_list_entry(next, struct rt_alarm, list); 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, 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_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); FINSH_FUNCTION_EXPORT_ALIAS(rt_alarm_dump, __cmd_alarm_dump, dump alarm info);

View File

@ -11,8 +11,7 @@
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <rtthread.h> #include <rtthread.h>
#include <rtdevice.h>
#include <drivers/rtc.h>
#ifdef RT_USING_SOFT_RTC #ifdef RT_USING_SOFT_RTC
@ -28,9 +27,37 @@ static struct rt_device soft_rtc_dev;
static rt_tick_t init_tick; static rt_tick_t init_tick;
static time_t init_time; 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) 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; struct tm time_temp;
RT_ASSERT(dev != RT_NULL); 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) switch (cmd)
{ {
case RT_DEVICE_CTRL_RTC_GET_TIME: case RT_DEVICE_CTRL_RTC_GET_TIME:
time = (time_t *) args; t = (time_t *) args;
*time = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; *t = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
break; break;
case RT_DEVICE_CTRL_RTC_SET_TIME: case RT_DEVICE_CTRL_RTC_SET_TIME:
{ {
time = (time_t *) args; t = (time_t *) args;
init_time = *time - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; init_time = *t - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
#ifdef RT_USING_ALARM
soft_rtc_alarm_update(&wkalarm);
#endif
break; 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; return RT_EOK;
@ -78,6 +116,15 @@ int rt_soft_rtc_init(void)
/* make sure only one 'rtc' device */ /* make sure only one 'rtc' device */
RT_ASSERT(!rt_device_find("rtc")); 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_tick = rt_tick_get();
init_time = mktime(&time_new); init_time = mktime(&time_new);