嵌入式技术william hill官网
直播中

李桂兰

7年用户 1474经验值
私信 关注
[经验]

教你使用RT-Thread开启STM32H7系列的RTC与Alarm组件

背景
  • 【ART-PI】默认没有开启Alarm组件,尝试适配并开启
  • 有部分工程师想使用RT-Thread 基于STM32H7系列的RTC 与 Alarm(闹钟功能)
验证平台
  • Keil MDK5(使用RT-Thread Studio 可以作为参考)
  • 【ART-PI】STM32H750XBH6,使用Pandoro STM32L4系列MCU,同样验证通过
移植方法
  • 只前做过一次基于Apollo3平台的Alarm组件的移植,适配部分与STM32有些不同,直接Copy过去编译,发现编译不过,很正常!!
  • 这次基于STM32H7、STM32L4平台,如果也编译不过去,只需要微调!
1.png
2.png
3.png
4.png
5.png
6.png

回帖(2)

李桂兰

2022-3-16 15:05:19
功能验证
1.png
2.png
3.png
举报

李桂兰

2022-3-16 15:07:21
drv_rtc.c 代码,供参考
  1. #include "board.h"
  2. #include
  3. #include

  4. #ifdef BSP_USING_ONCHIP_RTC

  5. struct rt_rtc_device
  6. {
  7.     struct rt_device        device;
  8. #ifdef RT_USING_ALARM
  9.     struct rt_rtc_wkalarm   wkalarm;
  10. #endif
  11. };

  12. #define DBG_ENABLE
  13. #define DBG_SECTION_NAME    "drv_rtc"
  14. #define DBG_LEVEL           DBG_INFO
  15. #include

  16. static struct rt_rtc_device rtc_device;

  17. #ifdef RT_USING_ALARM
  18. static rt_err_t rtc_alarm_time_set(struct rt_rtc_device* p_dev);
  19. static int rt_rtc_alarm_init(void);
  20. static RTC_AlarmTypeDef salarmstructure;
  21. #endif

  22. #ifndef RTC_BKP_DR1
  23. #define RTC_BKP_DR1 RT_NULL
  24. #endif

  25. #define BKUP_REG_DATA 0xA5A5

  26. static RTC_HandleTypeDef RTC_Handler;

  27. RT_WEAK uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  28. {
  29.     return (~BKUP_REG_DATA);
  30. }

  31. RT_WEAK void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  32. {
  33.     return;
  34. }

  35. static time_t get_rtc_timestamp(void)
  36. {
  37.     RTC_TimeTypeDef RTC_TimeStruct = {0};
  38.     RTC_DateTypeDef RTC_DateStruct = {0};
  39.     struct tm tm_new = {0};

  40.     HAL_RTC_GetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN);
  41.     HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN);

  42.     tm_new.tm_sec  = RTC_TimeStruct.Seconds;
  43.     tm_new.tm_min  = RTC_TimeStruct.Minutes;
  44.     tm_new.tm_hour = RTC_TimeStruct.Hours;
  45.     tm_new.tm_mday = RTC_DateStruct.Date;
  46.     tm_new.tm_mon  = RTC_DateStruct.Month - 1;
  47.     tm_new.tm_year = RTC_DateStruct.Year + 100;

  48.     return mktime(&tm_new);
  49. }

  50. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  51. {
  52.     RTC_TimeTypeDef RTC_TimeStruct = {0};
  53.     RTC_DateTypeDef RTC_DateStruct = {0};
  54.     struct tm *p_tm;

  55.     p_tm = localtime(&time_stamp);
  56.     if (p_tm->tm_year < 100)
  57.     {
  58.         return -RT_ERROR;
  59.     }

  60.     RTC_TimeStruct.Seconds = p_tm->tm_sec ;
  61.     RTC_TimeStruct.Minutes = p_tm->tm_min ;
  62.     RTC_TimeStruct.Hours   = p_tm->tm_hour;
  63.     RTC_DateStruct.Date    = p_tm->tm_mday;
  64.     RTC_DateStruct.Month   = p_tm->tm_mon + 1 ;
  65.     RTC_DateStruct.Year    = p_tm->tm_year - 100;
  66.     RTC_DateStruct.WeekDay = p_tm->tm_wday + 1;

  67.     if (HAL_RTC_SetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN) != HAL_OK)
  68.     {
  69.         return -RT_ERROR;
  70.     }
  71.     if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
  72.     {
  73.         return -RT_ERROR;
  74.     }

  75.     LOG_D("set rtc time.");
  76.     HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);

  77. #ifdef SOC_SERIES_STM32F1
  78.     /* F1 series does't save year/month/date datas. so keep those datas to bkp reg */
  79.     HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year);
  80.     HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month);
  81.     HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date);
  82.     HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay);
  83. #endif

  84.     return RT_EOK;
  85. }

  86. static void rt_rtc_init(void)
  87. {
  88. #if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WB)
  89.     __HAL_RCC_PWR_CLK_ENABLE();
  90. #endif

  91.     RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  92. #ifdef BSP_RTC_USING_LSI
  93. #ifdef SOC_SERIES_STM32WB
  94. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
  95.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  96.     RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  97.     RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  98. #else
  99.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  100.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  101.     RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  102.     RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  103. #endif
  104. #else
  105.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  106.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  107.     RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  108.     RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
  109. #endif
  110.     HAL_RCC_OscConfig(&RCC_OscInitStruct);
  111. }

  112. #ifdef SOC_SERIES_STM32F1
  113. /* update RTC_BKP_DRx*/
  114. static void rt_rtc_f1_bkp_update(void)
  115. {
  116.     RTC_DateTypeDef RTC_DateStruct = {0};

  117.     HAL_PWR_EnableBkUpAccess();
  118.     __HAL_RCC_BKP_CLK_ENABLE();

  119.     RTC_DateStruct.Year    = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR2);
  120.     RTC_DateStruct.Month   = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR3);
  121.     RTC_DateStruct.Date    = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4);
  122.     RTC_DateStruct.WeekDay = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR5);
  123.     if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK)
  124.     {
  125.         Error_Handler();
  126.     }

  127.     HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN);
  128.     if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4) != RTC_DateStruct.Date)
  129.     {
  130.         HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA);
  131.         HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year);
  132.         HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month);
  133.         HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date);
  134.         HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay);
  135.     }
  136. }
  137. #endif

  138. static rt_err_t rt_rtc_config(struct rt_device *dev)
  139. {
  140.     RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  141.     HAL_PWR_EnableBkUpAccess();
  142.     PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  143. #ifdef BSP_RTC_USING_LSI
  144.     PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  145. #else
  146.     PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  147. #endif
  148.     HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

  149.     /* Enable RTC Clock */
  150.     __HAL_RCC_RTC_ENABLE();

  151.     RTC_Handler.Instance = RTC;
  152.     if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR1) != BKUP_REG_DATA)
  153.     {
  154.         LOG_I("RTC hasn't been configured, please use command to config.");

  155. #if defined(SOC_SERIES_STM32F1)
  156.         RTC_Handler.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
  157.         RTC_Handler.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  158. #elif defined(SOC_SERIES_STM32F0)

  159.         /* set the frequency division */
  160. #ifdef BSP_RTC_USING_LSI
  161.         RTC_Handler.Init.AsynchPrediv = 0XA0;
  162.         RTC_Handler.Init.SynchPrediv = 0xFA;
  163. #else
  164.         RTC_Handler.Init.AsynchPrediv = 0X7F;
  165.         RTC_Handler.Init.SynchPrediv = 0x0130;
  166. #endif /* BSP_RTC_USING_LSI */

  167.         RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  168.         RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  169.         RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  170.         RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  171. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB)

  172.         /* set the frequency division */
  173. #ifdef BSP_RTC_USING_LSI
  174.         RTC_Handler.Init.AsynchPrediv = 0X7D;
  175. #else
  176.         RTC_Handler.Init.AsynchPrediv = 0X7F;
  177. #endif /* BSP_RTC_USING_LSI */
  178.         RTC_Handler.Init.SynchPrediv = 0XFF;

  179.         RTC_Handler.Init.HourFormat = RTC_HOURFORMAT_24;
  180.         RTC_Handler.Init.OutPut = RTC_OUTPUT_DISABLE;
  181.         RTC_Handler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  182.         RTC_Handler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  183. #endif
  184.         if (HAL_RTC_Init(&RTC_Handler) != HAL_OK)
  185.         {
  186.             return -RT_ERROR;
  187.         }
  188.     }
  189. #ifdef SOC_SERIES_STM32F1
  190.     else
  191.     {
  192.         /* F1 series need update by bkp reg datas */
  193.         rt_rtc_f1_bkp_update();
  194.     }
  195. #endif

  196.     return RT_EOK;
  197. }

  198. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  199. {
  200.     rt_err_t result = RT_EOK;
  201. #ifdef RT_USING_ALARM
  202.     struct rt_rtc_wkalarm *p_wkalarm = RT_NULL;
  203. #endif
  204.     RT_ASSERT(dev != RT_NULL);

  205.     switch (cmd)
  206.     {
  207.     case RT_DEVICE_CTRL_RTC_GET_TIME:
  208.         *(rt_uint32_t *)args = get_rtc_timestamp();
  209.         LOG_D("RTC: get rtc_time %xn", *(rt_uint32_t *)args);
  210.         break;

  211.     case RT_DEVICE_CTRL_RTC_SET_TIME:
  212.         if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  213.         {
  214.             result = -RT_ERROR;
  215.         }
  216. #ifdef RT_USING_ALARM
  217.         rt_alarm_update(&rtc_device.device, 1);
  218. #endif
  219.         LOG_D("RTC: set rtc_time %xn", *(rt_uint32_t *)args);
  220.         break;

  221. #ifdef RT_USING_ALARM
  222.     case RT_DEVICE_CTRL_RTC_GET_ALARM:
  223.         args = &rtc_device.wkalarm;
  224.         LOG_D("GET_ALARM %d:%d:%d",rtc_device.wkalarm.tm_hour,
  225.             rtc_device.wkalarm.tm_min,rtc_device.wkalarm.tm_sec);
  226.         break;

  227.     case RT_DEVICE_CTRL_RTC_SET_ALARM:
  228.         LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM");
  229.         p_wkalarm = (struct rt_rtc_wkalarm *)args;
  230.         if (p_wkalarm != RT_NULL)
  231.         {
  232.             rtc_device.wkalarm.enable = p_wkalarm->enable;
  233.             rtc_device.wkalarm.tm_hour = p_wkalarm->tm_hour;
  234.             rtc_device.wkalarm.tm_min = p_wkalarm->tm_min;
  235.             rtc_device.wkalarm.tm_sec = p_wkalarm->tm_sec;
  236.             rtc_alarm_time_set(&rtc_device);
  237.         }
  238.         else
  239.         {
  240.             result = -RT_ERROR;
  241.             LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!");
  242.         }
  243.         LOG_D("SET_ALARM %d:%d:%d",p_wkalarm->tm_hour,
  244.             p_wkalarm->tm_min,p_wkalarm->tm_sec);
  245.         break;
  246. #endif
  247.     }

  248.     return result;
  249. }

  250. #ifdef RT_USING_DEVICE_OPS
  251. const static struct rt_device_ops rtc_ops =
  252. {
  253.     RT_NULL,
  254.     RT_NULL,
  255.     RT_NULL,
  256.     RT_NULL,
  257.     RT_NULL,
  258.     rt_rtc_control
  259. };
  260. #endif

  261. static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
  262. {
  263.     RT_ASSERT(device != RT_NULL);

  264.     rt_rtc_init();
  265.     if (rt_rtc_config(device) != RT_EOK)
  266.     {
  267.         return -RT_ERROR;
  268.     }
  269. #ifdef RT_USING_DEVICE_OPS
  270.     device->ops         = &rtc_ops;
  271. #else
  272.     device->init        = RT_NULL;
  273.     device->open        = RT_NULL;
  274.     device->close       = RT_NULL;
  275.     device->read        = RT_NULL;
  276.     device->write       = RT_NULL;
  277.     device->control     = rt_rtc_control;
  278. #endif
  279.     device->type        = RT_Device_Class_RTC;
  280.     device->rx_indicate = RT_NULL;
  281.     device->tx_complete = RT_NULL;
  282.     device->user_data   = RT_NULL;

  283.     /* register a character device */
  284.     return rt_device_register(device, name, flag);
  285. }

  286. int rt_hw_rtc_init(void)
  287. {
  288.     rt_err_t result;

  289.     result = rt_hw_rtc_register(&rtc_device.device, "rtc", RT_DEVICE_FLAG_RDWR);
  290.     if (result != RT_EOK)
  291.     {
  292.         LOG_E("rtc register err code: %dn", result);
  293.         return result;
  294.     }
  295. #ifdef RT_USING_ALARM
  296.     rt_rtc_alarm_init();
  297. #endif
  298.     LOG_D("rtc init successn");
  299.     return RT_EOK;
  300. }

  301. #ifdef RT_USING_ALARM
  302. void rt_rtc_alarm_enable(void)
  303. {
  304.     HAL_RTC_SetAlarm_IT(&RTC_Handler,&salarmstructure,RTC_FORMAT_BIN);
  305.     HAL_RTC_GetAlarm(&RTC_Handler,&salarmstructure,RTC_ALARM_A,RTC_FORMAT_BIN);
  306.     LOG_D("alarm read:%d:%d:%d", salarmstructure.AlarmTime.Hours,
  307.         salarmstructure.AlarmTime.Minutes,
  308.         salarmstructure.AlarmTime.Seconds);
  309.     HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x02, 0);
  310.     HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
  311. }

  312. void rt_rtc_alarm_disable(void)
  313. {
  314.     HAL_RTC_DeactivateAlarm(&RTC_Handler, RTC_ALARM_A);
  315.     HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
  316. }

  317. static int rt_rtc_alarm_init(void)
  318. {
  319.     return RT_EOK;
  320. }

  321. static rt_err_t rtc_alarm_time_set(struct rt_rtc_device* p_dev)
  322. {
  323.     if (p_dev->wkalarm.enable)
  324.     {
  325.         salarmstructure.Alarm = RTC_ALARM_A;
  326.         salarmstructure.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
  327.         salarmstructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
  328.         salarmstructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
  329.         salarmstructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
  330.         salarmstructure.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
  331.         salarmstructure.AlarmTime.Hours = p_dev->wkalarm.tm_hour;
  332.         salarmstructure.AlarmTime.Minutes = p_dev->wkalarm.tm_min;
  333.         salarmstructure.AlarmTime.Seconds = p_dev->wkalarm.tm_sec;
  334.         LOG_D("alarm set:%d:%d:%d", salarmstructure.AlarmTime.Hours,
  335.             salarmstructure.AlarmTime.Minutes,
  336.             salarmstructure.AlarmTime.Seconds);
  337.         rt_rtc_alarm_enable();
  338.     }

  339.     return RT_EOK;
  340. }

  341. void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  342. {
  343.     //LOG_D("rtc alarm isr.n");
  344.     rt_alarm_update(&rtc_device.device, 1);
  345. }

  346. void RTC_Alarm_IRQHandler(void)
  347. {
  348.     HAL_RTC_AlarmIRQHandler(&RTC_Handler);
  349. }

  350. #endif

  351. INIT_DEVICE_EXPORT(rt_hw_rtc_init);

  352. #endif /* BSP_USING_ONCHIP_RTC */


RTC_Alarm 测试代码,供参考
  1. #include
  2. #include "board.h"
  3. #include
  4. #include

  5. #define RTC_DEBUG

  6. #define DBG_ENABLE
  7. #define DBG_SECTION_NAME    "rtc.test"
  8. #define DBG_LEVEL           DBG_LOG
  9. #include

  10. #ifdef RTC_DEBUG

  11. static struct rt_alarm * p_alarm_sec = RT_NULL;
  12. static struct rt_alarm * p_alarm_min = RT_NULL;
  13. static struct rt_alarm * p_alarm_hour = RT_NULL;
  14. static struct rt_alarm * p_alarm_time = RT_NULL;

  15. static rt_err_t rtc_set_time(time_t timestamp)
  16. {
  17.     /* converts the local time in time to calendar time. */
  18.     rt_device_t rtc_device;
  19.     rt_err_t ret = -RT_ERROR;

  20.     rtc_device = rt_device_find("rtc");

  21.     if (rtc_device == RT_NULL)
  22.     {
  23.         return -RT_ERROR;
  24.     }

  25.     /* update to RTC device. */
  26.     ret = rt_device_control(rtc_device, RT_DEVICE_CTRL_RTC_SET_TIME, ×tamp);

  27.     return ret;
  28. }

  29. static time_t rtc_gettime(void)
  30. {
  31.     static time_t now;
  32.     static struct tm tm;

  33.     now = time(NULL);
  34. #ifdef _WIN32
  35.     _gmtime32_s(&tm, &now);
  36. #else
  37.     gmtime_r(&now, &tm);
  38. #endif

  39.     LOG_D("BJ time:%04d-%02d-%02d %02d:%02d:%02d.%03dn",
  40.                 tm.tm_year + 1900, tm.tm_mon + 1,
  41.                 tm.tm_mday, tm.tm_hour + 8, tm.tm_min,
  42.                 tm.tm_sec, rt_tick_get() % 1000);

  43.     return now;
  44. }

  45. static void alarm_time_cb(rt_alarm_t alarm, time_t timestamp)
  46. {
  47.     LOG_D("alarm_time_cb ok!n");
  48. }

  49. static void alarm_hour_cb(rt_alarm_t alarm, time_t timestamp)
  50. {
  51.     LOG_D("alarm_hour_cb ok!n");
  52. }

  53. static void alarm_minute_cb(rt_alarm_t alarm, time_t timestamp)
  54. {
  55.     LOG_D("alarm_minute_cb ok!n");
  56. }

  57. static void alarm_second_cb(rt_alarm_t alarm, time_t timestamp)
  58. {
  59.     LOG_D("alarm_second_cb ok!n");
  60. }

  61. static struct rt_alarm * rtc_alarm_test_create(rt_alarm_callback_t callback, rt_uint32_t flag, struct tm *p_tm)
  62. {
  63.     struct rt_alarm_setup alarm_setup_test;

  64.     alarm_setup_test.flag = flag;
  65.     alarm_setup_test.wktime.tm_year = p_tm->tm_year;
  66.     alarm_setup_test.wktime.tm_mon = p_tm->tm_mon;
  67.     alarm_setup_test.wktime.tm_mday = p_tm->tm_mday;
  68.     alarm_setup_test.wktime.tm_wday = p_tm->tm_wday;
  69.     alarm_setup_test.wktime.tm_hour = p_tm->tm_hour;
  70.     alarm_setup_test.wktime.tm_min = p_tm->tm_min;
  71.     alarm_setup_test.wktime.tm_sec = p_tm->tm_sec;

  72.     return rt_alarm_create(callback, &alarm_setup_test);
  73. }

  74. static void rtc_alarm_time_create(void)
  75. {
  76.     static time_t now;
  77.     static struct tm tm;

  78.     if (p_alarm_time != RT_NULL)
  79.         return;

  80.     now = time(NULL) + 65;
  81. #ifdef _WIN32
  82.     _gmtime32_s(&tm, &now);
  83. #else
  84.     gmtime_r(&now, &tm);
  85. #endif

  86.     p_alarm_time = rtc_alarm_test_create(alarm_time_cb, RT_ALARM_DAILY, &tm);
  87. }

  88. static void rtc_alarm_time_start(void)
  89. {
  90.     if (p_alarm_time != RT_NULL)
  91.         rt_alarm_start(p_alarm_time);
  92. }

  93. static void rtc_alarm_time_stop(void)
  94. {
  95.     if (p_alarm_time != RT_NULL)
  96.         rt_alarm_stop(p_alarm_time);
  97. }

  98. static void rtc_alarm_time_delete(void)
  99. {
  100.     if (p_alarm_time != RT_NULL)
  101.     {
  102.         if (rt_alarm_delete(p_alarm_time) == RT_EOK)
  103.             p_alarm_time = RT_NULL;
  104.     }
  105. }

  106. static void rtc_alarm_second_create(void)
  107. {
  108.     static time_t now;
  109.     static struct tm tm;

  110.     if (p_alarm_sec != RT_NULL)
  111.         return;

  112.     now = time(NULL) + 1;
  113. #ifdef _WIN32
  114.     _gmtime32_s(&tm, &now);
  115. #else
  116.     gmtime_r(&now, &tm);
  117. #endif

  118.     p_alarm_sec = rtc_alarm_test_create(alarm_second_cb, RT_ALARM_SECOND, &tm);
  119. }

  120. static void rtc_alarm_second_start(void)
  121. {
  122.     if (p_alarm_sec != RT_NULL)
  123.         rt_alarm_start(p_alarm_sec);
  124. }

  125. static void rtc_alarm_second_stop(void)
  126. {
  127.     if (p_alarm_sec != RT_NULL)
  128.         rt_alarm_stop(p_alarm_sec);
  129. }

  130. static void rtc_alarm_second_delete(void)
  131. {
  132.     if (p_alarm_sec != RT_NULL)
  133.     {
  134.         if (rt_alarm_delete(p_alarm_sec) == RT_EOK)
  135.             p_alarm_sec = RT_NULL;
  136.     }
  137. }

  138. static void rtc_alarm_minute_create(void)
  139. {
  140.     static time_t now;
  141.     static struct tm tm;

  142.     if (p_alarm_min != RT_NULL)
  143.         return;

  144.     now = time(NULL) + 60;
  145. #ifdef _WIN32
  146.     _gmtime32_s(&tm, &now);
  147. #else
  148.     gmtime_r(&now, &tm);
  149. #endif

  150.     p_alarm_min = rtc_alarm_test_create(alarm_minute_cb, RT_ALARM_MINUTE, &tm);
  151. }

  152. static void rtc_alarm_minute_start(void)
  153. {
  154.     if (p_alarm_min != RT_NULL)
  155.         rt_alarm_start(p_alarm_min);
  156. }

  157. static void rtc_alarm_minute_stop(void)
  158. {
  159.     if (p_alarm_min != RT_NULL)
  160.         rt_alarm_stop(p_alarm_min);
  161. }

  162. static void rtc_alarm_minute_delete(void)
  163. {
  164.     if (p_alarm_min != RT_NULL)
  165.     {
  166.         if (rt_alarm_delete(p_alarm_min) == RT_EOK)
  167.             p_alarm_min = RT_NULL;
  168.     }
  169. }

  170. static void rtc_alarm_hour_create(void)
  171. {
  172.     static time_t now;
  173.     static struct tm tm;

  174.     if (p_alarm_hour != RT_NULL)
  175.         return;

  176.     now = time(NULL) + 3600;
  177. #ifdef _WIN32
  178.     _gmtime32_s(&tm, &now);
  179. #else
  180.     gmtime_r(&now, &tm);
  181. #endif

  182.     p_alarm_hour = rtc_alarm_test_create(alarm_hour_cb, RT_ALARM_HOUR, &tm);
  183. }

  184. static void rtc_alarm_hour_start(void)
  185. {
  186.     if (p_alarm_hour != RT_NULL)
  187.         rt_alarm_start(p_alarm_hour);
  188. }

  189. static void rtc_alarm_hour_stop(void)
  190. {
  191.     if (p_alarm_hour != RT_NULL)
  192.         rt_alarm_stop(p_alarm_hour);
  193. }

  194. static void rtc_alarm_hour_delete(void)
  195. {
  196.     if (p_alarm_hour != RT_NULL)
  197.     {
  198.         if (rt_alarm_delete(p_alarm_hour) == RT_EOK)
  199.             p_alarm_hour = RT_NULL;
  200.     }
  201. }

  202. void rtc_alarm_start(int argc, char **argv)
  203. {
  204.     int index = 0;

  205.     if (argc >= 2)
  206.     {
  207.         index = atoi(argv[1]);
  208.     }

  209.     switch(index)
  210.     {
  211.         case 0:
  212.             rtc_alarm_hour_start();
  213.             break;
  214.         case 1:
  215.             rtc_alarm_minute_start();
  216.             break;
  217.         case 2:
  218.             rtc_alarm_second_start();
  219.             break;
  220.         case 3:
  221.             rtc_alarm_time_start();
  222.             break;
  223.         default:
  224.             break;
  225.     }
  226. }

  227. void rtc_alarm_stop(int argc, char **argv)
  228. {
  229.     int index = 0;

  230.     if (argc >= 2)
  231.     {
  232.         index = atoi(argv[1]);
  233.     }

  234.     switch(index)
  235.     {
  236.         case 0:
  237.             rtc_alarm_hour_stop();
  238.             break;
  239.         case 1:
  240.             rtc_alarm_minute_stop();
  241.             break;
  242.         case 2:
  243.             rtc_alarm_second_stop();
  244.             break;
  245.         case 3:
  246.             rtc_alarm_time_stop();
  247.             break;
  248.         default:
  249.             break;
  250.     }
  251. }

  252. void rtc_alarm_create(int argc, char **argv)
  253. {
  254.     int index = 0;

  255.     if (argc >= 2)
  256.     {
  257.         index = atoi(argv[1]);
  258.     }

  259.     switch(index)
  260.     {
  261.         case 0:
  262.             rtc_alarm_hour_create();
  263.             break;
  264.         case 1:
  265.             rtc_alarm_minute_create();
  266.             break;
  267.         case 2:
  268.             rtc_alarm_second_create();
  269.             break;
  270.         case 3:
  271.             rtc_alarm_time_create();
  272.             break;
  273.         default:
  274.             break;
  275.     }
  276. }

  277. void rtc_alarm_delete(int argc, char **argv)
  278. {
  279.     int index = 0;

  280.     if (argc >= 2)
  281.     {
  282.         index = atoi(argv[1]);
  283.     }

  284.     switch(index)
  285.     {
  286.         case 0:
  287.             rtc_alarm_hour_delete();
  288.             break;
  289.         case 1:
  290.             rtc_alarm_minute_delete();
  291.             break;
  292.         case 2:
  293.             rtc_alarm_second_delete();
  294.             break;
  295.         case 3:
  296.             rtc_alarm_time_delete();
  297.             break;
  298.         default:
  299.             break;
  300.     }
  301. }

  302. MSH_CMD_EXPORT(rtc_gettime, rtc get time);
  303. MSH_CMD_EXPORT(rtc_alarm_create, rtc alarm_create);
  304. MSH_CMD_EXPORT(rtc_alarm_delete, rtc alarm_delete);
  305. MSH_CMD_EXPORT(rtc_alarm_start, rtc alarm_start);
  306. MSH_CMD_EXPORT(rtc_alarm_stop, rtc alarm_stop);

  307. #endif


总结
RT-Thread RTC alarm组件,配置起来,稍微有点麻烦。
Alarm闹钟功能,使能组件后,还是需要创建闹钟、开启闹钟、停止闹钟、删除闹钟业务代码的配合。
多使用,多总结。

调试记录
  1. msh />rtc
  2. rtc_gettime
  3. rtc_alarm_create
  4. rtc_alarm_delete
  5. rtc_alarm_start
  6. rtc_alarm_stop
  7. msh />rtc_al
  8. rtc_alarm_create
  9. rtc_alarm_delete
  10. rtc_alarm_start
  11. rtc_alarm_stop
  12. msh />rtc_alarm_c
  13. rtc_alarm_create
  14. msh />rtc_alarm_create 0
  15. msh />rtc
  16. rtc_gettime
  17. rtc_alarm_create
  18. rtc_alarm_delete
  19. rtc_alarm_start
  20. rtc_alarm_stop
  21. msh />rtc_al
  22. rtc_alarm_create
  23. rtc_alarm_delete
  24. rtc_alarm_start
  25. rtc_alarm_stop
  26. msh />rtc_alarm_c
  27. rtc_alarm_create
  28. msh />rtc_alarm_create 1
  29. msh />al
  30. alarm_dump
  31. msh />alarm_dump
  32. | id | YYYY-MM-DD hh:mm:ss | week | flag | en |
  33. +----+---------------------+------+------+----+
  34. |  0 | 2000-01-01 08:13:12 |   6  |   M  |  0 |
  35. |  1 | 2000-01-01 09:12:07 |   6  |   H  |  0 |
  36. |  2 | 2000-01-01 08:12:13 |   6  |   S  |  1 |
  37. +----+---------------------+------+------+----+
  38. msh />rt
  39. rtc_gettime
  40. rtc_alarm_create
  41. rtc_alarm_delete
  42. rtc_alarm_start
  43. rtc_alarm_stop
  44. msh />rtc_al
  45. rtc_alarm_create
  46. rtc_alarm_delete
  47. rtc_alarm_start
  48. rtc_alarm_stop
  49. msh />rtc_alarm_st
  50. rtc_alarm_start
  51. rtc_alarm_stop
  52. msh />rtc_alarm_sta
  53. rtc_alarm_start
  54. msh />rtc_alarm_start 0
  55. msh />rtc
  56. rtc_gettime
  57. rtc_alarm_create
  58. rtc_alarm_delete
  59. rtc_alarm_start
  60. rtc_alarm_stop
  61. msh />rtc_al
  62. rtc_alarm_create
  63. rtc_alarm_delete
  64. rtc_alarm_start
  65. rtc_alarm_stop
  66. msh />rtc_alarm_st
  67. rtc_alarm_start
  68. rtc_alarm_stop
  69. msh />rtc_alarm_sta
  70. rtc_alarm_start
  71. msh />rtc_alarm_start 1
  72. msh />al
  73. alarm_dump
  74. msh />alarm_dump
  75. | id | YYYY-MM-DD hh:mm:ss | week | flag | en |
  76. +----+---------------------+------+------+----+
  77. |  0 | 2000-01-01 08:13:12 |   6  |   M  |  1 |
  78. |  1 | 2000-01-01 09:12:07 |   6  |   H  |  1 |
  79. |  2 | 2000-01-01 08:12:29 |   6  |   S  |  1 |
  80. +----+---------------------+------+------+----+
  81. msh />
  82. msh />rtc_al
  83. rtc_alarm_create
  84. rtc_alarm_delete
  85. rtc_alarm_start
  86. rtc_alarm_stop
  87. msh />rtc_alarm_crw
  88. msh />rtc_alarm_cr
  89. rtc_alarm_create
  90. msh />rtc_alarm_create 2
  91. msh />rtc
  92. rtc_gettime
  93. rtc_alarm_create
  94. rtc_alarm_delete
  95. rtc_alarm_start
  96. rtc_alarm_stop
  97. msh />rtc_al
  98. rtc_alarm_create
  99. rtc_alarm_delete
  100. rtc_alarm_start
  101. rtc_alarm_stop
  102. msh />rtc_alarm_star
  103. rtc_alarm_start
  104. msh />rtc_alarm_start 2
  105. msh />[D/rtc.test] alarm_second_cb ok!

  106. [D/rtc.test] alarm_second_cb ok!

  107. [D/rtc.test] alarm_second_cb ok!

  108. [D/rtc.test] alarm_second_cb ok!

  109. rtc
  110. rtc_gettime
  111. rtc_alarm_create
  112. rtc_alarm_delete
  113. rtc_alarm_start
  114. rtc_alarm_stop
  115. msh />rtc_[D/rtc.test] alarm_second_cb ok!

  116. a-[D/rtc.test] alarm_second_cb ok!

  117. msh />rtc_a-[D/rtc.test] alarm_second_cb ok!
  118.                                                                                                                                                            
  119. rtc_alarm_create
  120. rtc_alarm_delete
  121. rtc_alarm_start
  122. rtc_alarm_stop
  123. msh />rtc_alarm_[D/rtc.test] alarm_second_cb ok!

  124. s[D/rtc.test] alarm_second_cb ok!

  125. rtc_alarm_start
  126. rtc_alarm_stop
  127. msh />rtc_alarm_st
  128. rtc_alarm_start
  129. rtc_alarm_stop
  130. msh />rtc_alarm_sto
  131. rtc_alarm_stop
  132. msh />rtc_alarm_stop[D/rtc.test] alarm_second_cb ok!

  133. rtc_alarm_stop
  134. msh />rtc_alarm_stop
  135. rtc_alarm_stop
  136. msh />rtc_alarm_stop 2
  137. msh />al
  138. alarm_dump
  139. msh />alarm_dump
  140. | id | YYYY-MM-DD hh:mm:ss | week | flag | en |
  141. +----+---------------------+------+------+----+
  142. |  0 | 2000-01-01 08:13:05 |   6  |   S  |  0 |
  143. |  1 | 2000-01-01 08:13:12 |   6  |   M  |  1 |
  144. |  2 | 2000-01-01 09:12:07 |   6  |   H  |  1 |
  145. |  3 | 2000-01-01 08:13:11 |   6  |   S  |  1 |
  146. +----+---------------------+------+------+----+
  147. msh />[D/rtc.test] alarm_minute_cb ok!
举报

更多回帖

发帖
×
20
完善资料,
赚取积分