62 lines
1.6 KiB
C
62 lines
1.6 KiB
C
|
/*
|
|||
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|||
|
*
|
|||
|
* SPDX-License-Identifier: Apache-2.0
|
|||
|
*
|
|||
|
* Change Logs:
|
|||
|
* Date Author Notes
|
|||
|
* 2023-06-19 murmur the first version
|
|||
|
*/
|
|||
|
/*
|
|||
|
** 程序清单:这是一个 RTC 设备使用例程
|
|||
|
** 例程导出了 alarm_sample 命令到控制终端
|
|||
|
** 命令调用格式:alarm_sample
|
|||
|
** 程序功能:设置RTC时间,创建闹钟,模式:每秒触发,启动闹钟
|
|||
|
**/
|
|||
|
|
|||
|
#include <rtthread.h>
|
|||
|
#include <rtdevice.h>
|
|||
|
|
|||
|
#define LOG_TAG "alarm"
|
|||
|
#define LOG_LVL LOG_LVL_DBG
|
|||
|
#include <ulog.h>
|
|||
|
|
|||
|
void user_alarm_callback(rt_alarm_t alarm, time_t timestamp)
|
|||
|
{
|
|||
|
rt_kprintf("user alarm callback function.\n");
|
|||
|
}
|
|||
|
|
|||
|
void alarm_sample(int argc, char *argv[])
|
|||
|
{
|
|||
|
rt_device_t dev = rt_device_find("rtc");
|
|||
|
struct rt_alarm_setup setup;
|
|||
|
struct rt_alarm * alarm = RT_NULL;
|
|||
|
static time_t now;
|
|||
|
struct tm p_tm;
|
|||
|
|
|||
|
if (alarm != RT_NULL)
|
|||
|
return;
|
|||
|
|
|||
|
/* 获取当前时间戳,并把下一秒时间设置为闹钟时间 */
|
|||
|
now = time(NULL) + 10;
|
|||
|
gmtime_r(&now,&p_tm);
|
|||
|
|
|||
|
setup.flag = RT_ALARM_DAILY;
|
|||
|
setup.wktime.tm_year = p_tm.tm_year;
|
|||
|
setup.wktime.tm_mon = p_tm.tm_mon;
|
|||
|
setup.wktime.tm_mday = p_tm.tm_mday;
|
|||
|
setup.wktime.tm_wday = p_tm.tm_wday;
|
|||
|
setup.wktime.tm_hour = atoi(argv[1]);//p_tm.tm_hour;
|
|||
|
setup.wktime.tm_min = 0;//p_tm.tm_min;
|
|||
|
setup.wktime.tm_sec = 0;//p_tm.tm_sec;
|
|||
|
|
|||
|
alarm = rt_alarm_create(user_alarm_callback, &setup);
|
|||
|
if(RT_NULL != alarm)
|
|||
|
{
|
|||
|
rt_alarm_start(alarm);
|
|||
|
LOG_D("alarm started.");
|
|||
|
}
|
|||
|
}
|
|||
|
/* export msh cmd */
|
|||
|
MSH_CMD_EXPORT(alarm_sample,alarm sample);
|