Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
alarm
RT-Thread 4.1.0 开启 Alarm模块
发布于 2022-05-31 21:45:58 浏览:1417
订阅该版
[tocm] [RT-Thread 4.1.0 开启 RTC模块](https://club.rt-thread.org/ask/article/59f40cef330c8d5b.html) [RT-Thread 4.1.0 开启 Alarm模块](https://club.rt-thread.org/ask/article/99fb3f0ffada4a8d.html) ## 前言 - RT-Thread 的驱动框架里有RTC,Alarm组件附属与RTC,基于STM32 的BSP有现成的RTC驱动,STM32本身有ST官方的RTC HAL 支持,在STM32的BSP上开启RTC,简单配置一下即可。 - 开启RTC后,就可以基于RTC开启闹钟Alarm功能等 ## 开发环境 - stm32l476-st-nucleo开发板,基于STM32L476RG - RT-Thread 4.1.0 - Keil MDK5 ## 操作步骤 - 这里可以搭建一个 stm32l476-st-nucleo 的最小RT-Thread 系统 - 开启Alarm组件,这里使用RT-Thread ENV 工具:`menuconfig` ![2022-05-31_205319.png](https://oss-club.rt-thread.org/uploads/20220531/3d054f87e68750a1b980cb1ae193b84e.png) - 开启RT-Thread 4.1.0 (当前最新)的Alarm组件,就一个配置项,Alarm附属于RTC,也就是Alarm工作必须开启RTC功能 ## 调试 - 配置使能Alarm组件,编译成功下载后,发现没有任何的效果 - 这里需要【适配Alarm】,适配的文件位置是RTC的驱动文件:`libraries/HAL_Drivers/drv_rtc.c` - 由于RTC驱动目前没有合入Alarm的驱动,所以需要添加Alarm的驱动部分,才能让Alarm组件驱动起来 - 这里为了偷懒,直接把修改后的:`drv_rtc.c` 贴出来,可以根据:`RT_USING_ALARM` 了解添加的Alarm驱动代码 ```c /* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-12-04 balanceTWK first version * 2020-10-14 Dozingfiretruck Porting for stm32wbxx * 2021-02-05 Meco Man fix the problem of mixing local time and UTC time * 2021-07-05 iysheng implement RTC framework V2.0 */ #include "board.h" #include
#ifdef BSP_USING_ONCHIP_RTC struct rtc_device_object { rt_rtc_dev_t parent; #ifdef RT_USING_ALARM struct rt_rtc_wkalarm wkalarm; #endif }; static struct rtc_device_object rtc_device; #ifdef RT_USING_ALARM static rt_err_t rtc_alarm_time_set(struct rtc_device_object* p_dev); static int rt_rtc_alarm_init(void); static RTC_AlarmTypeDef salarmstructure; #endif #ifndef RTC_BKP_DR1 #define RTC_BKP_DR1 RT_NULL #endif //#define DRV_DEBUG #define LOG_TAG "drv.rtc" #include
#define BKUP_REG_DATA 0xA5A5 static RTC_HandleTypeDef RTC_Handler; RT_WEAK uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister) { return (~BKUP_REG_DATA); } RT_WEAK void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data) { return; } static rt_err_t stm32_rtc_get_timeval(struct timeval *tv) { RTC_TimeTypeDef RTC_TimeStruct = {0}; RTC_DateTypeDef RTC_DateStruct = {0}; struct tm tm_new = {0}; HAL_RTC_GetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN); tm_new.tm_sec = RTC_TimeStruct.Seconds; tm_new.tm_min = RTC_TimeStruct.Minutes; tm_new.tm_hour = RTC_TimeStruct.Hours; tm_new.tm_mday = RTC_DateStruct.Date; tm_new.tm_mon = RTC_DateStruct.Month - 1; tm_new.tm_year = RTC_DateStruct.Year + 100; tv->tv_sec = timegm(&tm_new); #if defined(SOC_SERIES_STM32H7) tv->tv_usec = (255.0 - RTC_TimeStruct.SubSeconds * 1.0) / 256.0 * 1000.0 * 1000.0; #endif return RT_EOK; } static rt_err_t set_rtc_time_stamp(time_t time_stamp) { RTC_TimeTypeDef RTC_TimeStruct = {0}; RTC_DateTypeDef RTC_DateStruct = {0}; struct tm tm = {0}; gmtime_r(&time_stamp, &tm); if (tm.tm_year < 100) { return -RT_ERROR; } RTC_TimeStruct.Seconds = tm.tm_sec ; RTC_TimeStruct.Minutes = tm.tm_min ; RTC_TimeStruct.Hours = tm.tm_hour; RTC_DateStruct.Date = tm.tm_mday; RTC_DateStruct.Month = tm.tm_mon + 1 ; RTC_DateStruct.Year = tm.tm_year - 100; RTC_DateStruct.WeekDay = tm.tm_wday + 1; if (HAL_RTC_SetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN) != HAL_OK) { return -RT_ERROR; } if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK) { return -RT_ERROR; } LOG_D("set rtc time."); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA); #ifdef SOC_SERIES_STM32F1 /* F1 series does't save year/month/date datas. so keep those datas to bkp reg */ HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay); #endif return RT_EOK; } #ifdef SOC_SERIES_STM32F1 /* update RTC_BKP_DRx*/ static void rt_rtc_f1_bkp_update(void) { RTC_DateTypeDef RTC_DateStruct = {0}; HAL_PWR_EnableBkUpAccess(); __HAL_RCC_BKP_CLK_ENABLE(); RTC_DateStruct.Year = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR2); RTC_DateStruct.Month = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR3); RTC_DateStruct.Date = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4); RTC_DateStruct.WeekDay = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR5); if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK) { Error_Handler(); } HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN); if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4) != RTC_DateStruct.Date) { HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay); } } #endif static rt_err_t rt_rtc_config(void) { RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; HAL_PWR_EnableBkUpAccess(); PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; #ifdef BSP_RTC_USING_LSI PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; #else PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; #endif HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); #if defined(SOC_SERIES_STM32WL) __HAL_RCC_RTCAPB_CLK_ENABLE(); #endif /* Enable RTC Clock */ __HAL_RCC_RTC_ENABLE(); RTC_Handler.Instance = RTC; if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR1) != BKUP_REG_DATA) { LOG_I("RTC hasn't been configured, please use
command to config."); #if defined(SOC_SERIES_STM32F1) RTC_Handler.Init.OutPut = RTC_OUTPUTSOURCE_NONE; RTC_Handler.Init.AsynchPrediv = RTC_AUTO_1_SECOND; #elif defined(SOC_SERIES_STM32F0) /* set the frequency division */ #ifdef BSP_RTC_USING_LSI RTC_Handler.Init.AsynchPrediv = 0XA0; RTC_Handler.Init.SynchPrediv = 0xFA; #else RTC_Handler.Init.AsynchPrediv = 0X7F; RTC_Handler.Init.SynchPrediv = 0x0130; #endif /* BSP_RTC_USING_LSI */ RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24; RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE; RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB) /* set the frequency division */ #ifdef BSP_RTC_USING_LSI RTC_Handler.Init.AsynchPrediv = 0X7D; #else RTC_Handler.Init.AsynchPrediv = 0X7F; #endif /* BSP_RTC_USING_LSI */ RTC_Handler.Init.SynchPrediv = 0XFF; RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24; RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE; RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; #else #warning "This series doesn't support yet!" #endif if (HAL_RTC_Init(&RTC_Handler) != HAL_OK) { return -RT_ERROR; } } #ifdef SOC_SERIES_STM32F1 else { /* F1 series need update by bkp reg datas */ rt_rtc_f1_bkp_update(); } #endif return RT_EOK; } static rt_err_t stm32_rtc_init(void) { #if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB) __HAL_RCC_PWR_CLK_ENABLE(); #endif RCC_OscInitTypeDef RCC_OscInitStruct = {0}; #ifdef BSP_RTC_USING_LSI #ifdef SOC_SERIES_STM32WB RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; #else RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; #endif #else RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.LSIState = RCC_LSI_OFF; #endif HAL_RCC_OscConfig(&RCC_OscInitStruct); if (rt_rtc_config() != RT_EOK) { LOG_E("rtc init failed."); return -RT_ERROR; } return RT_EOK; } static rt_err_t stm32_rtc_get_secs(time_t *sec) { struct timeval tv; stm32_rtc_get_timeval(&tv); *(time_t *) sec = tv.tv_sec; LOG_D("RTC: get rtc_time %d", *sec); return RT_EOK; } static rt_err_t stm32_rtc_set_secs(time_t *sec) { rt_err_t result = RT_EOK; if (set_rtc_time_stamp(*sec)) { result = -RT_ERROR; } LOG_D("RTC: set rtc_time %d", *sec); #ifdef RT_USING_ALARM rt_alarm_update(&rtc_device.parent.parent, 1); #endif return result; } static rt_err_t stm32_rtc_get_alarm(struct rt_rtc_wkalarm *alarm) { #ifdef RT_USING_ALARM *alarm = rtc_device.wkalarm; LOG_D("GET_ALARM %d:%d:%d",rtc_device.wkalarm.tm_hour, rtc_device.wkalarm.tm_min,rtc_device.wkalarm.tm_sec); return RT_EOK; #else return -RT_ERROR; #endif } static rt_err_t stm32_rtc_set_alarm(struct rt_rtc_wkalarm *alarm) { #ifdef RT_USING_ALARM LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM"); if (alarm != RT_NULL) { rtc_device.wkalarm.enable = alarm->enable; rtc_device.wkalarm.tm_hour = alarm->tm_hour; rtc_device.wkalarm.tm_min = alarm->tm_min; rtc_device.wkalarm.tm_sec = alarm->tm_sec; rtc_alarm_time_set(&rtc_device); } else { LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!"); return -RT_ERROR; } LOG_D("SET_ALARM %d:%d:%d",alarm->tm_hour, alarm->tm_min, alarm->tm_sec); return RT_EOK; #else return -RT_ERROR; #endif } static const struct rt_rtc_ops stm32_rtc_ops = { stm32_rtc_init, stm32_rtc_get_secs, stm32_rtc_set_secs, stm32_rtc_get_alarm, stm32_rtc_set_alarm, stm32_rtc_get_timeval, RT_NULL, }; static rt_rtc_dev_t stm32_rtc_dev; #ifdef RT_USING_ALARM void rt_rtc_alarm_enable(void) { HAL_RTC_SetAlarm_IT(&RTC_Handler,&salarmstructure,RTC_FORMAT_BIN); HAL_RTC_GetAlarm(&RTC_Handler,&salarmstructure,RTC_ALARM_A,RTC_FORMAT_BIN); LOG_D("alarm read:%d:%d:%d", salarmstructure.AlarmTime.Hours, salarmstructure.AlarmTime.Minutes, salarmstructure.AlarmTime.Seconds); HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x02, 0); HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); } void rt_rtc_alarm_disable(void) { HAL_RTC_DeactivateAlarm(&RTC_Handler, RTC_ALARM_A); HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn); } static int rt_rtc_alarm_init(void) { return RT_EOK; } static rt_err_t rtc_alarm_time_set(struct rtc_device_object* p_dev) { if (p_dev->wkalarm.enable) { salarmstructure.Alarm = RTC_ALARM_A; salarmstructure.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY; salarmstructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY; salarmstructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY; salarmstructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE; salarmstructure.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM; salarmstructure.AlarmTime.Hours = p_dev->wkalarm.tm_hour; salarmstructure.AlarmTime.Minutes = p_dev->wkalarm.tm_min; salarmstructure.AlarmTime.Seconds = p_dev->wkalarm.tm_sec; LOG_D("alarm set:%d:%d:%d", salarmstructure.AlarmTime.Hours, salarmstructure.AlarmTime.Minutes, salarmstructure.AlarmTime.Seconds); rt_rtc_alarm_enable(); } return RT_EOK; } void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) { //LOG_D("rtc alarm isr.\n"); rt_alarm_update(&rtc_device.parent.parent, 1); } void RTC_Alarm_IRQHandler(void) { rt_interrupt_enter(); HAL_RTC_AlarmIRQHandler(&RTC_Handler); rt_interrupt_leave(); } #endif static int rt_hw_rtc_init(void) { rt_err_t result; stm32_rtc_dev.ops = &stm32_rtc_ops; result = rt_hw_rtc_register(&stm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL); if (result != RT_EOK) { LOG_E("rtc register err code: %d", result); return result; } LOG_D("rtc init success"); #ifdef RT_USING_ALARM rt_rtc_alarm_init(); #endif return RT_EOK; } INIT_DEVICE_EXPORT(rt_hw_rtc_init); #endif /* BSP_USING_ONCHIP_RTC */ ``` ## 闹钟应用 - 添加了基于RTC的Alarm驱动,还需要写Alarm的应用逻辑,如创建闹钟、开启闹钟、接收闹钟回调等操作,测试程序如下: ```c #include
#include "board.h" #include
#include
#include
#ifdef RT_USING_ALARM #define RTC_DEBUG #define DBG_ENABLE #define DBG_SECTION_NAME "rtc.test" #define DBG_LEVEL DBG_LOG #include
#ifdef RTC_DEBUG static struct rt_alarm * p_alarm_sec = RT_NULL; static struct rt_alarm * p_alarm_min = RT_NULL; static struct rt_alarm * p_alarm_hour = RT_NULL; static struct rt_alarm * p_alarm_time = RT_NULL; static time_t rtc_gettime(void) { static time_t now; static struct tm tm; now = time(NULL); gmtime_r(&now, &tm); LOG_D("BJ time:%04d-%02d-%02d %02d:%02d:%02d.%03d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour + 8, tm.tm_min, tm.tm_sec, rt_tick_get() % 1000); return now; } static void alarm_time_cb(rt_alarm_t alarm, time_t timestamp) { LOG_D("alarm_time_cb ok!\n"); } static void alarm_hour_cb(rt_alarm_t alarm, time_t timestamp) { LOG_D("alarm_hour_cb ok!\n"); } static void alarm_minute_cb(rt_alarm_t alarm, time_t timestamp) { LOG_D("alarm_minute_cb ok!\n"); } static void alarm_second_cb(rt_alarm_t alarm, time_t timestamp) { LOG_D("alarm_second_cb ok!\n"); } static struct rt_alarm * rtc_alarm_test_create(rt_alarm_callback_t callback, rt_uint32_t flag, struct tm *p_tm) { struct rt_alarm_setup alarm_setup_test; alarm_setup_test.flag = flag; alarm_setup_test.wktime.tm_year = p_tm->tm_year; alarm_setup_test.wktime.tm_mon = p_tm->tm_mon; alarm_setup_test.wktime.tm_mday = p_tm->tm_mday; alarm_setup_test.wktime.tm_wday = p_tm->tm_wday; alarm_setup_test.wktime.tm_hour = p_tm->tm_hour; alarm_setup_test.wktime.tm_min = p_tm->tm_min; alarm_setup_test.wktime.tm_sec = p_tm->tm_sec; return rt_alarm_create(callback, &alarm_setup_test); } static void rtc_alarm_time_create(void) { static time_t now; static struct tm tm; if (p_alarm_time != RT_NULL) return; now = time(NULL) + 65; gmtime_r(&now, &tm); p_alarm_time = rtc_alarm_test_create(alarm_time_cb, RT_ALARM_DAILY, &tm); } static void rtc_alarm_time_start(void) { if (p_alarm_time != RT_NULL) rt_alarm_start(p_alarm_time); } static void rtc_alarm_time_stop(void) { if (p_alarm_time != RT_NULL) rt_alarm_stop(p_alarm_time); } static void rtc_alarm_time_delete(void) { if (p_alarm_time != RT_NULL) { if (rt_alarm_delete(p_alarm_time) == RT_EOK) p_alarm_time = RT_NULL; } } static void rtc_alarm_second_create(void) { static time_t now; static struct tm tm; if (p_alarm_sec != RT_NULL) return; now = time(NULL) + 1; gmtime_r(&now, &tm); p_alarm_sec = rtc_alarm_test_create(alarm_second_cb, RT_ALARM_SECOND, &tm); } static void rtc_alarm_second_start(void) { if (p_alarm_sec != RT_NULL) rt_alarm_start(p_alarm_sec); } static void rtc_alarm_second_stop(void) { if (p_alarm_sec != RT_NULL) rt_alarm_stop(p_alarm_sec); } static void rtc_alarm_second_delete(void) { if (p_alarm_sec != RT_NULL) { if (rt_alarm_delete(p_alarm_sec) == RT_EOK) p_alarm_sec = RT_NULL; } } static void rtc_alarm_minute_create(void) { static time_t now; static struct tm tm; if (p_alarm_min != RT_NULL) return; now = time(NULL) + 60; gmtime_r(&now, &tm); p_alarm_min = rtc_alarm_test_create(alarm_minute_cb, RT_ALARM_MINUTE, &tm); } static void rtc_alarm_minute_start(void) { if (p_alarm_min != RT_NULL) rt_alarm_start(p_alarm_min); } static void rtc_alarm_minute_stop(void) { if (p_alarm_min != RT_NULL) rt_alarm_stop(p_alarm_min); } static void rtc_alarm_minute_delete(void) { if (p_alarm_min != RT_NULL) { if (rt_alarm_delete(p_alarm_min) == RT_EOK) p_alarm_min = RT_NULL; } } static void rtc_alarm_hour_create(void) { static time_t now; static struct tm tm; if (p_alarm_hour != RT_NULL) return; now = time(NULL) + 3600; gmtime_r(&now, &tm); p_alarm_hour = rtc_alarm_test_create(alarm_hour_cb, RT_ALARM_HOUR, &tm); } static void rtc_alarm_hour_start(void) { if (p_alarm_hour != RT_NULL) rt_alarm_start(p_alarm_hour); } static void rtc_alarm_hour_stop(void) { if (p_alarm_hour != RT_NULL) rt_alarm_stop(p_alarm_hour); } static void rtc_alarm_hour_delete(void) { if (p_alarm_hour != RT_NULL) { if (rt_alarm_delete(p_alarm_hour) == RT_EOK) p_alarm_hour = RT_NULL; } } void rtc_alarm_start(int argc, char **argv) { int index = 0; if (argc >= 2) { index = atoi(argv[1]); } switch(index) { case 0: rtc_alarm_hour_start(); break; case 1: rtc_alarm_minute_start(); break; case 2: rtc_alarm_second_start(); break; case 3: rtc_alarm_time_start(); break; default: break; } } void rtc_alarm_stop(int argc, char **argv) { int index = 0; if (argc >= 2) { index = atoi(argv[1]); } switch(index) { case 0: rtc_alarm_hour_stop(); break; case 1: rtc_alarm_minute_stop(); break; case 2: rtc_alarm_second_stop(); break; case 3: rtc_alarm_time_stop(); break; default: break; } } void rtc_alarm_create(int argc, char **argv) { int index = 0; if (argc >= 2) { index = atoi(argv[1]); } switch(index) { case 0: rtc_alarm_hour_create(); break; case 1: rtc_alarm_minute_create(); break; case 2: rtc_alarm_second_create(); break; case 3: rtc_alarm_time_create(); break; default: break; } } void rtc_alarm_delete(int argc, char **argv) { int index = 0; if (argc >= 2) { index = atoi(argv[1]); } switch(index) { case 0: rtc_alarm_hour_delete(); break; case 1: rtc_alarm_minute_delete(); break; case 2: rtc_alarm_second_delete(); break; case 3: rtc_alarm_time_delete(); break; default: break; } } MSH_CMD_EXPORT(rtc_gettime, rtc get time); MSH_CMD_EXPORT(rtc_alarm_create, rtc alarm_create); MSH_CMD_EXPORT(rtc_alarm_delete, rtc alarm_delete); MSH_CMD_EXPORT(rtc_alarm_start, rtc alarm_start); MSH_CMD_EXPORT(rtc_alarm_stop, rtc alarm_stop); #endif #endif //RT_USING_ALARM ``` ## Alarm 调试 - Alarm 有好几个类型:单次、周期性的,周期的又分为:周、天、时、分、秒等,这个介绍在前面的文章提过,如果需要可以查看前面的介绍 ```c msh >rtc_alarm_create 1 /* 创建闹钟:分钟为周期,也就是每分钟触发一次回调 */ msh >rtc_alarm_start 1 /* 使能闹钟:使能后闹钟才会触发 */ msh >list_alarm /* 查看闹钟列表,注意 M 代表类型【分钟】, en:1 代表使能 */ | hh:mm:ss | week | flag | en | +----------+------+------+----+ | 13:17:05 | 2 | M | 1 | +----------+------+------+----+ ``` ![2022-05-31_212027.png](https://oss-club.rt-thread.org/uploads/20220531/0ff11a134584a1bf7750d2a191aa4a96.png.webp) - 停止闹钟:周期性的闹钟,如果不需要时需要手动停止 ![2022-05-31_214130.png](https://oss-club.rt-thread.org/uploads/20220531/c490b82e1694485c68e4d53ce9bcb1a5.png) - 删除闹钟:闹钟创建后,不会自动删除,可以删除节省【内存RAM】,删除闹钟指令:`msh >rtc_alarm_delete 1` ## 小结 - 本篇基于STM32L4 系列、STM32H5系列测试通过,但不代表其他的平台会直接能测试 - alarm 与RTC的时间:均为UTC 时间,也就是没有【时区】的,这个一定要注意
2
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
张世争
学以致用
文章
131
回答
813
被采纳
177
关注TA
发私信
相关文章
1
带有alarm 的drv_rtc.c 如何下载
2
跪求RTC drv_rtc.c 最新带alarm文件
3
关于rtc_alarm的使用
4
请问有人知道rtc的alarm要如何调用吗
5
请问在stm32f103上如何使用rtt studio的alarm功能
6
rt thread studio 有关于stm32待机周期性唤醒的示例吗?
7
RTC ALARM中断,为什么没有执行闹钟中断回调函数?
8
如何实现RTC中的set_alarm 和 get_alarm 接口
9
开启Alarm设备 链接时出错
10
根据文档进行了alarm创建和启动,发现并没有跳到闹钟回调函数
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
PWM
cubemx
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
xusiwei1236
5
个答案
2
次被采纳
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
7
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
张世争
1
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
2
次点赞
Ghost_Girls
1
篇文章
6
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部