Demo
26 Nov 2024
Read time: 1 minute(s)
如果要获取 RTC 时间,调用 RT-Thread 的标准时间接口即可,比如,- time() - clock_gettime() - gettimeofday()
本 Demo 是 test_alarm 的源码
bsp/examples/test-alarm/test_alarm.c,展示了在 RT-Thread 中,设置一个
oneshot 类型的 alarm
并使用标准时间接口获取当前时间的参考示例:
static void test_alarm_callback(rt_alarm_t alarm, time_t timestamp)
{
pr_info("Test alarm callback function.\n");
}
static void cmd_test_alarm(int argc, char **argv)
{
struct rt_alarm_setup setup;
struct rt_alarm *alarm = RT_NULL;
u32 timeout = 0;
time_t now;
struct tm p_tm;
if (argc != 2) {
pr_err("Invalid parameter\n");
return;
}
sscanf((char *)argv[1], "%u", &timeout);
now = time(NULL) + timeout;
gmtime_r(&now, &p_tm);
setup.wktime = p_tm;
alarm = rt_alarm_create(test_alarm_callback, &setup);
if (alarm) {
alarm->flag = RT_ALARM_ONESHOT;
rt_alarm_start(alarm);
}
}