commit
c5dd2feb07
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* File : alarm.c
|
* File : alarm.c
|
||||||
* This file is part of RT-Thread RTOS
|
* This file is part of RT-Thread RTOS
|
||||||
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* The license and distribution terms for this file may be
|
* The license and distribution terms for this file may be
|
||||||
* found in the file LICENSE in this distribution or at
|
* found in the file LICENSE in this distribution or at
|
||||||
|
@ -10,6 +10,7 @@
|
||||||
* Change Logs:
|
* Change Logs:
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2012-10-27 heyuanjie87 first version.
|
* 2012-10-27 heyuanjie87 first version.
|
||||||
|
* 2013-05-17 aozima initial alarm event & mutex in system init.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
|
@ -21,7 +22,7 @@
|
||||||
#define RT_ALARM_STATE_START 0x01
|
#define RT_ALARM_STATE_START 0x01
|
||||||
#define RT_ALARM_STATE_STOP 0x00
|
#define RT_ALARM_STATE_STOP 0x00
|
||||||
|
|
||||||
#if(defined(RT_USING_RTC) && defined(RT_USING_ALARM))
|
#if (defined(RT_USING_RTC) && defined(RT_USING_ALARM))
|
||||||
static struct rt_alarm_container _container;
|
static struct rt_alarm_container _container;
|
||||||
|
|
||||||
rt_inline rt_uint32_t alarm_mkdaysec(struct tm *time)
|
rt_inline rt_uint32_t alarm_mkdaysec(struct tm *time)
|
||||||
|
@ -61,7 +62,7 @@ static rt_err_t alarm_set(struct rt_alarm *alarm)
|
||||||
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_ALARM, &wkalarm);
|
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_ALARM, &wkalarm);
|
||||||
if (ret == RT_EOK)
|
if (ret == RT_EOK)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
some RTC device like RX8025,it's alarms precision is 1 minute.
|
some RTC device like RX8025,it's alarms precision is 1 minute.
|
||||||
in this case,low level RTC driver should set wkalarm->tm_sec to 0.
|
in this case,low level RTC driver should set wkalarm->tm_sec to 0.
|
||||||
*/
|
*/
|
||||||
|
@ -79,7 +80,7 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now)
|
||||||
rt_uint32_t sec_alarm, sec_now;
|
rt_uint32_t sec_alarm, sec_now;
|
||||||
rt_bool_t wakeup = RT_FALSE;
|
rt_bool_t wakeup = RT_FALSE;
|
||||||
time_t timestamp;
|
time_t timestamp;
|
||||||
|
|
||||||
sec_alarm = alarm_mkdaysec(&alarm->wktime);
|
sec_alarm = alarm_mkdaysec(&alarm->wktime);
|
||||||
sec_now = alarm_mkdaysec(now);
|
sec_now = alarm_mkdaysec(now);
|
||||||
|
|
||||||
|
@ -97,7 +98,7 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now)
|
||||||
alarm->flag &= ~RT_ALARM_STATE_START;
|
alarm->flag &= ~RT_ALARM_STATE_START;
|
||||||
alarm_set(alarm);
|
alarm_set(alarm);
|
||||||
wakeup = RT_TRUE;
|
wakeup = RT_TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RT_ALARM_DAILY:
|
case RT_ALARM_DAILY:
|
||||||
|
@ -129,10 +130,10 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now)
|
||||||
case RT_ALARM_YAERLY:
|
case RT_ALARM_YAERLY:
|
||||||
{
|
{
|
||||||
if ((alarm->wktime.tm_mday == now->tm_mday) && \
|
if ((alarm->wktime.tm_mday == now->tm_mday) && \
|
||||||
(alarm->wktime.tm_mon == now->tm_mon))
|
(alarm->wktime.tm_mon == now->tm_mon))
|
||||||
{
|
{
|
||||||
if ((sec_now - sec_alarm) <= RT_ALARM_DELAY)
|
if ((sec_now - sec_alarm) <= RT_ALARM_DELAY)
|
||||||
wakeup = RT_TRUE;
|
wakeup = RT_TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -142,7 +143,7 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now)
|
||||||
{
|
{
|
||||||
timestamp = time(RT_NULL);
|
timestamp = time(RT_NULL);
|
||||||
alarm->callback(alarm, timestamp);
|
alarm->callback(alarm, timestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,7 +254,7 @@ static rt_bool_t is_valid_date(struct tm *date)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((date->tm_mday < 1) || \
|
if ((date->tm_mday < 1) || \
|
||||||
(date->tm_mday > days_of_year_month(date->tm_year, date->tm_mon)))
|
(date->tm_mday > days_of_year_month(date->tm_year, date->tm_mon)))
|
||||||
{
|
{
|
||||||
return (RT_FALSE);
|
return (RT_FALSE);
|
||||||
}
|
}
|
||||||
|
@ -351,7 +352,7 @@ static rt_err_t alarm_setup(rt_alarm_t alarm, struct tm *wktime)
|
||||||
goto _exit;
|
goto _exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((setup->tm_hour == 23) && (setup->tm_min == 59) && (setup->tm_sec == 59))
|
if ((setup->tm_hour == 23) && (setup->tm_min == 59) && (setup->tm_sec == 59))
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
@ -363,7 +364,7 @@ static rt_err_t alarm_setup(rt_alarm_t alarm, struct tm *wktime)
|
||||||
/* set initialized state */
|
/* set initialized state */
|
||||||
alarm->flag |= RT_ALARM_STATE_INITED;
|
alarm->flag |= RT_ALARM_STATE_INITED;
|
||||||
ret = RT_EOK;
|
ret = RT_EOK;
|
||||||
|
|
||||||
_exit:
|
_exit:
|
||||||
|
|
||||||
return (ret);
|
return (ret);
|
||||||
|
@ -372,7 +373,7 @@ _exit:
|
||||||
/** \brief send a rtc alarm event
|
/** \brief send a rtc alarm event
|
||||||
*
|
*
|
||||||
* \param dev pointer to RTC device(currently unused,you can ignore it)
|
* \param dev pointer to RTC device(currently unused,you can ignore it)
|
||||||
* \param event RTC event(currently unused)
|
* \param event RTC event(currently unused)
|
||||||
* \return none
|
* \return none
|
||||||
*/
|
*/
|
||||||
void rt_alarm_update(rt_device_t dev, rt_uint32_t event)
|
void rt_alarm_update(rt_device_t dev, rt_uint32_t event)
|
||||||
|
@ -391,30 +392,30 @@ rt_err_t rt_alarm_control(rt_alarm_t alarm, rt_uint8_t cmd, void *arg)
|
||||||
rt_err_t ret = RT_ERROR;
|
rt_err_t ret = RT_ERROR;
|
||||||
|
|
||||||
RT_ASSERT(alarm != RT_NULL);
|
RT_ASSERT(alarm != RT_NULL);
|
||||||
|
|
||||||
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
||||||
switch (cmd)
|
switch (cmd)
|
||||||
{
|
{
|
||||||
case RT_ALARM_CTRL_MODIFY:
|
case RT_ALARM_CTRL_MODIFY:
|
||||||
{
|
{
|
||||||
struct rt_alarm_setup *setup;
|
struct rt_alarm_setup *setup;
|
||||||
|
|
||||||
RT_ASSERT(arg != RT_NULL);
|
RT_ASSERT(arg != RT_NULL);
|
||||||
setup = arg;
|
setup = arg;
|
||||||
rt_alarm_stop(alarm);
|
rt_alarm_stop(alarm);
|
||||||
alarm->flag = setup->flag & 0xFF00;
|
alarm->flag = setup->flag & 0xFF00;
|
||||||
alarm->wktime = setup->wktime;
|
alarm->wktime = setup->wktime;
|
||||||
ret = alarm_setup(alarm, &alarm->wktime);
|
ret = alarm_setup(alarm, &alarm->wktime);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
rt_mutex_release(&_container.mutex);
|
rt_mutex_release(&_container.mutex);
|
||||||
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief start a alarm
|
/** \brief start an alarm
|
||||||
*
|
*
|
||||||
* \param alarm pointer to alarm
|
* \param alarm pointer to alarm
|
||||||
* \return RT_EOK
|
* \return RT_EOK
|
||||||
|
@ -425,7 +426,7 @@ rt_err_t rt_alarm_start(rt_alarm_t alarm)
|
||||||
rt_err_t ret = RT_ERROR;
|
rt_err_t ret = RT_ERROR;
|
||||||
time_t timestamp;
|
time_t timestamp;
|
||||||
struct tm now;
|
struct tm now;
|
||||||
|
|
||||||
if (alarm == RT_NULL)
|
if (alarm == RT_NULL)
|
||||||
return (ret);
|
return (ret);
|
||||||
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
||||||
|
@ -433,12 +434,12 @@ rt_err_t rt_alarm_start(rt_alarm_t alarm)
|
||||||
{
|
{
|
||||||
if (alarm_setup(alarm, &alarm->wktime) != RT_EOK)
|
if (alarm_setup(alarm, &alarm->wktime) != RT_EOK)
|
||||||
goto _exit;
|
goto _exit;
|
||||||
}
|
}
|
||||||
if ((alarm->flag & 0x01) == RT_ALARM_STATE_STOP)
|
if ((alarm->flag & 0x01) == RT_ALARM_STATE_STOP)
|
||||||
{
|
{
|
||||||
timestamp = time(RT_NULL);
|
timestamp = time(RT_NULL);
|
||||||
localtime_r(×tamp, &now);
|
localtime_r(×tamp, &now);
|
||||||
|
|
||||||
alarm->flag |= RT_ALARM_STATE_START;
|
alarm->flag |= RT_ALARM_STATE_START;
|
||||||
/* set alarm */
|
/* set alarm */
|
||||||
if (_container.current == RT_NULL)
|
if (_container.current == RT_NULL)
|
||||||
|
@ -482,7 +483,7 @@ _exit:
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief stop a alarm
|
/** \brief stop an alarm
|
||||||
*
|
*
|
||||||
* \param alarm pointer to alarm
|
* \param alarm pointer to alarm
|
||||||
* \return RT_EOK
|
* \return RT_EOK
|
||||||
|
@ -490,7 +491,7 @@ _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_ERROR;
|
||||||
|
|
||||||
if (alarm == RT_NULL)
|
if (alarm == RT_NULL)
|
||||||
return (ret);
|
return (ret);
|
||||||
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
||||||
|
@ -498,12 +499,12 @@ rt_err_t rt_alarm_stop(rt_alarm_t alarm)
|
||||||
goto _exit;
|
goto _exit;
|
||||||
/* stop alarm */
|
/* stop alarm */
|
||||||
alarm->flag &= ~RT_ALARM_STATE_START;
|
alarm->flag &= ~RT_ALARM_STATE_START;
|
||||||
|
|
||||||
if (_container.current == alarm)
|
if (_container.current == alarm)
|
||||||
{
|
{
|
||||||
ret = alarm_set(alarm);
|
ret = alarm_set(alarm);
|
||||||
_container.current = RT_NULL;
|
_container.current = RT_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == RT_EOK)
|
if (ret == RT_EOK)
|
||||||
alarm_update(0);
|
alarm_update(0);
|
||||||
|
@ -514,7 +515,7 @@ _exit:
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief delete a alarm
|
/** \brief delete an alarm
|
||||||
*
|
*
|
||||||
* \param alarm pointer to alarm
|
* \param alarm pointer to alarm
|
||||||
* \return RT_EOK
|
* \return RT_EOK
|
||||||
|
@ -522,10 +523,10 @@ _exit:
|
||||||
rt_err_t rt_alarm_delete(rt_alarm_t alarm)
|
rt_err_t rt_alarm_delete(rt_alarm_t alarm)
|
||||||
{
|
{
|
||||||
rt_err_t ret = RT_ERROR;
|
rt_err_t ret = RT_ERROR;
|
||||||
|
|
||||||
if (alarm == RT_NULL)
|
if (alarm == RT_NULL)
|
||||||
return (ret);
|
return (ret);
|
||||||
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
||||||
/* stop the alarm */
|
/* stop the alarm */
|
||||||
alarm->flag &= ~RT_ALARM_STATE_START;
|
alarm->flag &= ~RT_ALARM_STATE_START;
|
||||||
if (_container.current == alarm)
|
if (_container.current == alarm)
|
||||||
|
@ -533,17 +534,17 @@ rt_err_t rt_alarm_delete(rt_alarm_t alarm)
|
||||||
ret = alarm_set(alarm);
|
ret = alarm_set(alarm);
|
||||||
_container.current = RT_NULL;
|
_container.current = RT_NULL;
|
||||||
/* set new alarm if necessary */
|
/* set new alarm if necessary */
|
||||||
alarm_update(0);
|
alarm_update(0);
|
||||||
}
|
}
|
||||||
rt_list_remove(&alarm->list);
|
rt_list_remove(&alarm->list);
|
||||||
rt_free(alarm);
|
rt_free(alarm);
|
||||||
|
|
||||||
rt_mutex_release(&_container.mutex);
|
rt_mutex_release(&_container.mutex);
|
||||||
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief create a alarm
|
/** \brief create an alarm
|
||||||
*
|
*
|
||||||
* \param flag set alarm mode e.g: RT_ALARM_DAILY
|
* \param flag set alarm mode e.g: RT_ALARM_DAILY
|
||||||
* \param setup pointer to setup infomation
|
* \param setup pointer to setup infomation
|
||||||
|
@ -551,18 +552,18 @@ rt_err_t rt_alarm_delete(rt_alarm_t alarm)
|
||||||
rt_alarm_t rt_alarm_create(rt_alarm_callback_t callback, struct rt_alarm_setup *setup)
|
rt_alarm_t rt_alarm_create(rt_alarm_callback_t callback, struct rt_alarm_setup *setup)
|
||||||
{
|
{
|
||||||
struct rt_alarm *alarm;
|
struct rt_alarm *alarm;
|
||||||
|
|
||||||
if (setup == RT_NULL)
|
if (setup == RT_NULL)
|
||||||
return (RT_NULL);
|
return (RT_NULL);
|
||||||
alarm = rt_malloc(sizeof(struct rt_alarm));
|
alarm = rt_malloc(sizeof(struct rt_alarm));
|
||||||
if (alarm == RT_NULL)
|
if (alarm == RT_NULL)
|
||||||
return (RT_NULL);
|
return (RT_NULL);
|
||||||
rt_list_init(&alarm->list);
|
rt_list_init(&alarm->list);
|
||||||
|
|
||||||
alarm->wktime = setup->wktime;
|
alarm->wktime = setup->wktime;
|
||||||
alarm->flag = setup->flag & 0xFF00;
|
alarm->flag = setup->flag & 0xFF00;
|
||||||
alarm->callback = callback;
|
alarm->callback = callback;
|
||||||
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
|
||||||
rt_list_insert_after(&_container.head, &alarm->list);
|
rt_list_insert_after(&_container.head, &alarm->list);
|
||||||
rt_mutex_release(&_container.mutex);
|
rt_mutex_release(&_container.mutex);
|
||||||
|
|
||||||
|
@ -576,9 +577,6 @@ static void rt_alarmsvc_thread_init(void *param)
|
||||||
{
|
{
|
||||||
rt_uint32_t recv;
|
rt_uint32_t recv;
|
||||||
|
|
||||||
rt_list_init(&_container.head);
|
|
||||||
rt_event_init(&_container.event, "alarmsvc", RT_IPC_FLAG_FIFO);
|
|
||||||
rt_mutex_init(&_container.mutex, "alarmsvc", RT_IPC_FLAG_FIFO);
|
|
||||||
_container.current = RT_NULL;
|
_container.current = RT_NULL;
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
|
@ -587,7 +585,7 @@ static void rt_alarmsvc_thread_init(void *param)
|
||||||
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
|
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
|
||||||
RT_WAITING_FOREVER, &recv) == RT_EOK)
|
RT_WAITING_FOREVER, &recv) == RT_EOK)
|
||||||
{
|
{
|
||||||
alarm_update(recv);
|
alarm_update(recv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -602,6 +600,10 @@ void rt_alarm_system_init(void)
|
||||||
{
|
{
|
||||||
rt_thread_t tid;
|
rt_thread_t tid;
|
||||||
|
|
||||||
|
rt_list_init(&_container.head);
|
||||||
|
rt_event_init(&_container.event, "alarmsvc", RT_IPC_FLAG_FIFO);
|
||||||
|
rt_mutex_init(&_container.mutex, "alarmsvc", RT_IPC_FLAG_FIFO);
|
||||||
|
|
||||||
tid = rt_thread_create("alarmsvc",
|
tid = rt_thread_create("alarmsvc",
|
||||||
rt_alarmsvc_thread_init, RT_NULL,
|
rt_alarmsvc_thread_init, RT_NULL,
|
||||||
512, 8, 1);
|
512, 8, 1);
|
||||||
|
|
Loading…
Reference in New Issue