1. 概述
RTC (Real-Time Clock)实时时钟可以提供精确的实时时间,它可以用于产生年、月、日、时、分、秒等信息。
TG6210的 RTC设备为操作系统的时间系统提供了基础服务。面对越来越多的 IoT 场景,RTC 已经成为产品的标配,甚至在诸如 SSL 的安全传输过程中,RTC 已经成为不可或缺的部分。
2. API参考
2.1 RTC初始化
- 函数原型:
int hosal_rtc_init(hosal_rtc_dev_t *rtc);
- 功能描述:初始化RTC
- 函数参数:
- rtc:需要初始化的RTC句柄 ```c typedef struct { uint8_t format; /*< time formart DEC or BCD / } hosal_rtc_config_t;
```c
typedef struct {
uint8_t port; /**< rtc port */
hosal_rtc_config_t config; /**< rtc config */
void *priv; /**< priv data */
} hosal_rtc_dev_t;
返回值:
函数原型:
int hosal_rtc_set_time(hosal_rtc_dev_t *rtc, const hosal_rtc_time_t *time);
- 功能描述:设置时间。
- 函数参数:
- rtc:RTC的句柄
- time:需要设置时间的句柄 ```c typedef struct { uint8_t sec; /< DEC format:value range from 0 to 59, BCD format:value range from 0x00 to 0x59 */ uint8_t min; /< DEC format:value range from 0 to 59, BCD format:value range from 0x00 to 0x59 / uint8_t hr; /**< DEC format:value range from 0 to 23, BCD format:value range from 0x00 to 0x23 / uint8_t date; /< DEC format:value range from 1 to 31, BCD format:value range from 0x01 to 0x31 */ uint8_t month; /< DEC format:value range from 1 to 12, BCD format:value range from 0x01 to 0x12 / uint16_t year; /**< DEC format:value range from 0 to 9999, BCD format:value range from 0x0000 to 0x9999 / } hosal_rtc_time_t;
- 返回值:
- 成功返回0。
- 错误返回-1。
<a name="bMUff"></a>
## 2.3 获取当前时间
- 函数原型:<br />`int hosal_rtc_get_time(hosal_rtc_dev_t *rtc, hosal_rtc_time_t *time);`
- 功能描述:获取时间。
- 函数参数:
- rtc:RTC的句柄
- time:存储时间的句柄
- 返回值:
- 成功返回0。
- 错误返回-1。
<a name="GSBej"></a>
## 2.4 设置RTC Counter值
- 函数原型:<br />`int hosal_rtc_set_count(hosal_rtc_dev_t *rtc, uint64_t *time_stamp);`
- 功能描述:设置counter值
- 函数参数:
- rtc:RTC的句柄
- time_stamp:需要设置counter的句柄
- 返回值:
- 成功返回0。
- 错误返回-1。
<a name="zqNFf"></a>
## 2.5 获取RTC Counter值
- 函数原型:<br />`int hosal_rtc_get_count(hosal_rtc_dev_t *rtc, uint64_t *time_stamp);`
- 功能描述:获取counter值
- 函数参数:
- rtc:RTC的句柄
- time_stamp:存储counter的句柄
- 返回值:
- 成功返回0。
- 错误返回-1。
<a name="ZCGap"></a>
## 2.6 反初始化RTC
- 函数原型:<br />`int hosal_rtc_finalize(hosal_rtc_dev_t *rtc);`
- 功能描述:deinit RTC
- 函数参数:
- rtc:RTC的句柄
- 返回值:
- 成功返回0。
- 错误返回-1。
<a name="r8Yi5"></a>
# 3. Demo示例
```c
int ret = -1
hosal_rtc_dev_t rtc;
rtc.port = 0;
rtc.config.format =HOSAL_RTC_FORMAT_BCD;
- 通过 hosal_rtc_dev_t 定义一个rtc设备,并设置port,格式设置为BCD格式。
ret = hosal_rtc_init(&rtc);
if (ret != 0) {
blog_error("init rtc error\r\n");
}
- 调用 hosal_rtc_init 初始化rtc 。
int ret = -1;
hosal_rtc_time_t time1;
time1.sec = 0x59;
time1.min = 0x59;
time1.hr = 0x23;
time1.data = 0x31;
time1.month = 0x12;
time1.year = 0x37;
ret = hosal_rtc_set_time(&rtc, &time1);
if (ret != 0) {
blog_error("set rtc time error\r\n");
}
- 通过 hosal_rtc_set_time 来设置rtc时间,此时按照BCD格式设置。
hosal_rtc_time_t time2;
memset(&time, 0, sizeof(hosal_rtc_time_t));
ret = hosal_rtc_get_time(&rtc, &time2);
if(ret != 0) {
blog_error("get rtc time error\r\n");
}
blog_info(" get time sec = %x\r\n", time2.sec);
blog_info(" get time min = %x\r\n", time2.min);
blog_info(" get time hr = %x\r\n", time2.hr);
blog_info(" get time weekday = %x\r\n", time2.weekday);
blog_info(" get time data = %x\r\n", time2.data);
blog_info(" get time month = %x\r\n", time2.month);
blog_info(" get time year = %x\r\n", time2.year);
- 通过 hosal_rtc_get_time 接口来获取rtc时间,注意获取到的时间也是BCD格式的。
hosal_rtc_finalize(&rtc);
- 通过 hosal_rtc_finalize 结束此rtc的使用。