1、我们直接调用官方的库即可,主要以下几个函数
//RTC初始化
int32_t hal_rtc_init(rtc_dev_t *rtc)
//获取RTC时间
int32_t hal_rtc_get_time(rtc_dev_t *rtc, rtc_time_t *time)
//设置RTC时间
int32_t hal_rtc_set_time(rtc_dev_t *rtc, const rtc_time_t *time)
2、这些函数属于hal_drv模块,因此我们需要把工程路径下的Makefile文件中的INCLUDE_COMPONENTS字段添加hal_drv
3、下面我们创建一个RTC任务,间隔1秒获取一次RTC时间,并打印出来
/********************************************************************
*@brief RTC任务
*@input
*@return
********************************************************************/
static void user_rtc_task(void *arg)
{
rtc_dev_t rtc_dev;
rtc_dev.config.format = HAL_RTC_FORMAT_DEC;
hal_rtc_init(&rtc_dev);
rtc_time_t rtc_time;
rtc_time.year = 2022-1900;
rtc_time.month = 12;
rtc_time.date = 31;
rtc_time.hr = 23;
rtc_time.min = 58;
rtc_time.sec = 30;
rtc_time.weekday = 4;
int32_t flag = hal_rtc_set_time(&rtc_dev,&rtc_time);
printf("hal_rtc_set_time:%d\r\n",flag);
while(1)
{
vTaskDelay(1000/portTICK_RATE_MS);
hal_rtc_get_time(&rtc_dev,&rtc_time);
printf("year %d, ",rtc_time.year+1900);
printf("month %d, ",rtc_time.month);
printf("date %d, ",rtc_time.date);
printf("hr %d, ",rtc_time.hr);
printf("min %d, ",rtc_time.min);
printf("sec %d, ",rtc_time.sec);
printf("weekday %d\r\n",rtc_time.weekday);
}
vTaskDelete(NULL);
}
/********************************************************************
*@brief RTC初始化
*@input
*@return
********************************************************************/
void user_rtc_init()
{
printf("%s\r\n",__func__);
xTaskCreate(user_rtc_task, "", 512, NULL, 2, NULL);
}