【Ai-WB2中级篇】RTC实时时钟

[复制链接]
查看1029 | 回复9 | 2024-9-3 11:35:06 | 显示全部楼层 |阅读模式
RTC(real-time clock)为操作系统中的实时时钟设备,为操作系统提供精准的实时时间和定时报警功能。当设备下电后,通过外置电池供电,RTC继续记录操作系统时间;设备上电后,RTC提供实时时钟给操作系统,确保断电后系统时间的连续性。但是BL602芯片没有内置RTC模块,也没有提供外置电池供电,但是提供的软件RTC功能可以在带电情况下使用。
本文将详细介绍如何使用Ai-WB2的bl_iot_sdk中的RTC模块。
一:RTC介绍
BL602芯片没有内置RTC模块,但是 b1_iot_sdk 提供了软件RTC功能。RTC支持两种计时模式:DEC模式和BCDQ模式。
RTC模块的HOSAL层APl在 components/platform/hosal/include/hosal_rtc.h 中定义。常用的API如下:
· int hosal_rtc_init(hosal_rtc_dev_t *rtc):初始化RTC设备。参数说明如下:
      · rtc:RTC设备实例。其定义如下:
  1.   typedef struct {
  2.       uint8_t       port;   /**< rtc port */
  3.       hosal_rtc_config_t  config; /**< rtc config */
  4.       void         *priv;   /**< priv data */
  5.   } hosal_rtc_dev_t;
复制代码
其中,hosal_rtc_config_t定义如下:
  1. #define HOSAL_RTC_FORMAT_DEC 1 /**< RTC DEC format */
  2. #define HOSAL_RTC_FORMAT_BCD 2 /**< RTC BCD format */

  3. typedef struct {
  4.     uint8_t format; /**< time formart DEC or BCD */
  5. } hosal_rtc_config_t;
复制代码
      · 返回值:当调用成功时,返回0;否则返回非零值。
· int hosal_rtc_set_time(hosal_rtc_dev_t *rtc, const hosal_rtc_time_t *time):设置RTC日期时间。参数说明如下:
      · rtc:RTC设备
      · time:需要设置的时间。其定义如下:
  1.   /**
  2.    * @brief RTC time struct
  3.    */
  4.   typedef struct {
  5.       uint8_t sec;     /**< DEC format:value range from 0 to 59, BCD format:value range from 0x00 to 0x59 */
  6.       uint8_t min;     /**< DEC format:value range from 0 to 59, BCD format:value range from 0x00 to 0x59 */
  7.       uint8_t hr;      /**< DEC format:value range from 0 to 23, BCD format:value range from 0x00 to 0x23 */
  8.       uint8_t date;    /**< DEC format:value range from 1 to 31, BCD format:value range from 0x01 to 0x31 */
  9.       uint8_t month;   /**< DEC format:value range from 1 to 12, BCD format:value range from 0x01 to 0x12 */
  10.       uint16_t year;   /**< DEC format:value range from 0 to 9999, BCD format:value range from 0x0000 to 0x9999 */
  11.   } hosal_rtc_time_t;
复制代码
      · 返回值:当调用成功时,返回0;否则返回非零值。
· int hosal_rtc_get_time(hosal_rtc_dev_t *rtc, hosal_rtc_time_t *time):查询当前时间。参数说明如下:
      · rtc:RTC设备
      · time:需要设置的时间
      · 返回值:当调用成功时,返回0;否则返回非零值。
· int hosal_rtc_set_count(hosal_rtc_dev_t *rtc,uint64_t *time_stamp):设置RTC新的计数值。参数说明如下:
      · rtc:RTC设备
      · time stamp:需要设置的时间
      · 返回值:当调用成功时,返回0;否则返回非零值。
· int hosal_rtc_get_count(hosal_rtc_dev_t *rtc,uint64_t*time_stamp):获取当前RTC的时间计数值。参数说明如下:
      · rtc:RTC设备
      · time stamp:返回当前的时间计数值
      · 返回值:当调用成功时,返回0;否则返回非零值。
· int hosal_rtc_finalize(hosal_rtc_dev_t *rtc):销毁当前RTC设备实例,释放相关资源。参数说明如下:
      · rtc:RTC设备
      · 返回值:当调用成功时,返回0;否则返回非零值。
二:RTC使用实例
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <FreeRTOS.h>
  4. #include <task.h>
  5. #include <stdio.h>
  6. #include <stdbool.h>

  7. #include <hosal_rtc.h>
  8. #include <blog.h>

  9. #define TAG "rtc_demo"

  10. static hosal_rtc_dev_t rtc;

  11. bool rtc_demo_init(void) {
  12.     int ret1 = -1;
  13.     /* init rtc DEC format*/
  14.     hosal_rtc_dev_t rtc;
  15.     rtc.port = 0;
  16.     rtc.config.format = HOSAL_RTC_FORMAT_DEC;
  17.     hosal_rtc_init(&rtc);

  18.     /* set rtc time */
  19.     hosal_rtc_time_t time_buf;
  20.     time_buf.sec = 10;
  21.     time_buf.min = 56;
  22.     time_buf.hr = 10;
  23.     time_buf.date = 26;
  24.     time_buf.month = 4;
  25.     time_buf.year = 23;
  26.     ret1 = hosal_rtc_set_time(&rtc, &time_buf);
  27.     if (ret1 == 0) {
  28.         printf("set time sec     = %d\r\n", time_buf.sec);
  29.         printf("set time min     = %d\r\n", time_buf.min);
  30.         printf("set time hr      = %d\r\n", time_buf.hr);
  31.         printf("set time date    = %d\r\n", time_buf.date);
  32.         printf("set time month   = %d\r\n", time_buf.month);
  33.         printf("set time year    = %d\r\n", time_buf.year + 2000);
  34.     }
  35.     return (ret1 == 0);
  36. }

  37. void rtc_task(void* params) {
  38.     printf("rtc task started\r\n");
  39.     bool ret = rtc_demo_init();
  40.     hosal_rtc_time_t time_buf;
  41.     if (!ret) {
  42.         printf("rtc init failed\r\n");
  43.         while (true) {

  44.             vTaskDelay(1);
  45.         }
  46.     }
  47.     printf("rtc task started\r\n");
  48.     while (true) {
  49.         memset(&time_buf, 0, sizeof(hosal_rtc_time_t));
  50.         ret = hosal_rtc_get_time(&rtc, &time_buf);
  51.         if(ret == 0){
  52.             printf("set time sec     = %d\r\n", time_buf.sec);
  53.             printf("set time min     = %d\r\n", time_buf.min);
  54.             printf("set time hr      = %d\r\n", time_buf.hr);
  55.             printf("set time date    = %d\r\n", time_buf.date);
  56.             printf("set time month   = %d\r\n", time_buf.month);
  57.             printf("set time year    = %d\r\n", time_buf.year + 2000);
  58.         }
  59.         vTaskDelay(1000 / portTICK_PERIOD_MS);
  60.     }
  61. }

  62. void main(void) {
  63.     printf("rtc demo inited\r\n");
  64.     xTaskCreate(rtc_task, "rtc_task", 1024, NULL, 15, NULL);
  65. }
复制代码
用心做好保姆工作
回复

使用道具 举报

妖猊 | 2024-9-4 09:18:18 | 显示全部楼层
软件RTC功能应该是属于计时而已,掉电后数据都不在了吧,没法确保断电后系统时间的连续性吧
回复 支持 反对

使用道具 举报

bzhou830 | 2024-9-4 11:10:22 | 显示全部楼层
妖猊 发表于 2024-9-4 09:18
软件RTC功能应该是属于计时而已,掉电后数据都不在了吧,没法确保断电后系统时间的连续性吧 ...

设备下电后,通过外置电池供电,RTC继续记录操作系统时间;

上电再网络对时
选择去发光,而不是被照亮
回复 支持 反对

使用道具 举报

妖猊 | 2024-9-4 11:18:58 | 显示全部楼层
bzhou830 发表于 2024-9-4 11:10
设备下电后,通过外置电池供电,RTC继续记录操作系统时间;

上电再网络对时 ...

这是软件RTC啊,而且芯片好像没有备用供电引脚,一旦MCU下电了,不是整个MCU都没电了吗,软件RTC还怎么工作?
回复 支持 反对

使用道具 举报

bzhou830 | 2024-9-4 11:37:18 | 显示全部楼层
妖猊 发表于 2024-9-4 11:18
这是软件RTC啊,而且芯片好像没有备用供电引脚,一旦MCU下电了,不是整个MCU都没电了吗,软件RTC还怎么工 ...

BL602芯片没有内置RTC模块。

你说的是对的,没有硬件RTC模块,下电后就没法走时。

@园长,修改下
选择去发光,而不是被照亮
回复 支持 1 反对 0

使用道具 举报

妖猊 | 2024-9-4 15:26:34 | 显示全部楼层
bzhou830 发表于 2024-9-4 11:37
BL602芯片没有内置RTC模块。

你说的是对的,没有硬件RTC模块,下电后就没法走时。

而且内置RTC模块,在设置RTC也要设置供电方式,只有设置为备用供电时,主供电断电时有备用供电才行,如果没用备供电,或者设置为主供电,下电后一样不走时
回复 支持 反对

使用道具 举报

djy876 | 7 天前 | 显示全部楼层
学习打卡
回复

使用道具 举报

爱笑 | 7 天前 | 显示全部楼层
bzhou830 发表于 2024-9-4 11:37
BL602芯片没有内置RTC模块。

你说的是对的,没有硬件RTC模块,下电后就没法走时。

我需要怎么修改呀?
用心做好保姆工作
回复 支持 反对

使用道具 举报

爱笑 发表于 2024-9-10 10:18
我需要怎么修改呀?

RTC(real-time clock)为操作系统中的实时时钟设备,为操作系统提供精准的实时时间和定时报警功能。当设备下电后,通过外置电池供电,RTC继续记录操作系统时间;设备上电后,RTC提供实时时钟给操作系统,确保断电后系统时间的连续性。

但是BL602芯片没有内置RTC模块,也没有提供外置电池供电。但是提供的软件RTC功能可以在带电情况下使用。

第一段加上上面这一句更合理。
选择去发光,而不是被照亮
回复 支持 反对

使用道具 举报

爱笑 | 7 天前 | 显示全部楼层
bzhou830 发表于 2024-9-10 17:43
RTC(real-time clock)为操作系统中的实时时钟设备,为操作系统提供精准的实时时间和定时报警功能。当设备 ...

好的,谢谢玛丽哥。
用心做好保姆工作
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则