【简介】
C的标准库(ANSI C)中提供了时间相关的接口函数,使用C开发对C标准库的使用还是很常见的。常见的开发环境下都会包含C库,本次使用的环境为IAR 9.60.3 ,ANSI C 库中定义了time 相关的接口函数,用户只要include <time.h> 就可以使用标准库定义的time 相关的接口了,time.h 里定义的接口函数如下。
在IAR 的开发环境中time.h 可以配置为32bit 和 64bit 的版本,可以通过_DLIB_TIME_USES_64 的值来配置,不定义该宏默认使用的64bit的配置,对应文档说明如下:
IAR 环境下time ,上述_DLIB_TIME_USES_64 配置宏配置选择代码如下
跟我们使用标准库的printf 接口一样,time 相关的接口依赖用户实现__time32 __time64函数来支持time 相关接口的对接,文档说明如下:
【time.h 接口适配】
按照上面的梳理我们只要实现 __time32 __time64 接口函数就可以使用time.h 的接口,对应底层依赖的接口实现可以使用RTC模块来对接,我们在之前的贴子中已经是配了RTC 模块(【S32K146】S32K146片内RTC使用)我们基于此忒代码添加time 依赖的底层接口的实现,
#if (_DLIB_TIME_USES_64 == 1) __time64_t __time64(__time64_t *t) { rtc_timedate_t currentTime; *t = 0; RTC_DRV_GetCurrentTimeDate ( RTC_1, ¤tTime ); /* get rtc time */ RTC_DRV_ConvertTimeDateToSeconds(¤tTime ,(uint32_t *)t); *t = *t - 1; return *t; } #else __time32_t __time32(__time32_t *t) { rtc_timedate_t currentTime; *t = 0; RTC_DRV_GetCurrentTimeDate ( RTC_1, ¤tTime ); /* get rtc time */ RTC_DRV_ConvertTimeDateToSeconds(¤tTime ,(uint32_t *)t); *t = *t - 1; return *t; } #endif
我们添加如下的测试代码使用time.h 中的接口
unsigned int date(char argc,char *argv[]) { time_t t; if(argc == 1) { time(&t); LOG_I("%s",ctime(&t)); } }
执行date 函数已经可以使用time.h 的接口来读取RTC时间了,RTC 时间默认配置为2025-01-01 00:00:00