【VxWorks系统时间同步】

VxWorks BIOS、系统时间同步

一、相关接口

1 char *asctime(const struct tm *timeptr) 函数可以将时间结构体转换为时间字符串格式。
2 struct tm *localtime(const time_t *timer)使用 timer 的值来填充 tm 结构体,而这个timer 的值可以通过time函数来获取。而这个tm 结构体刚好就是asctime函数的参数,所以一般情况下localtime函数都是其他时间函数配合使用的
3 time_t mktime(struct tm *timeptr)把 timeptr 所指向的结构转换为一个依据本地时区的 time_t 值。
4 char *ctime(const time_t *timer)返回一个表示当地时间的字符串,当地时间是基于参数 timer。
5 struct tm *gmtime(const time_t *timer)timer 的值被分解为 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
6 time_t time(time_t *timer)计算当前日历时间,并把它编码成 time_t 格式。

二、使用说明

1、获取BIOS时间


struct tm GetBiosTime(void)
{
	struct tm tmBios;
	unsigned char Hour, Min, Sec;
	unsigned char Day, Mouth, Year;
	
	/* 将BIOS的时间读取到tmBios中,把转换后的实时时钟的秒、
	 * 分、时,日、月、年等数据赋给tm型结构变量的相应成员 */
	sysOutByte(0x70, 0x00/*second*/);
	Sec = sysInByte(0x71);
	tmBios.tm_sec = (Sec&0x0F) + 10*((Sec&0xF0)>>4);
	
	sysOutByte(0x70,0x02/*minut*/);
	Min = sysInByte(0x71);
	tmBios.tm_min = (Min&0x0F) + 10*((Min&0xF0)>>4);
	
	sysOutByte(0x70,0x04/*hour*/);
	Hour = sysInByte(0x71);
	tmBios.tm_hour = (Hour&0x0F) + 10*((Hour&0xF0)>>4);
	
	sysOutByte(0x70,0x07/*day*/);
	Day = sysInByte(0x71);
	tmBios.tm_mday = (Day&0x0F) + 10*((Day&0xF0)>>4);
	
	sysOutByte(0x70,0x08/*month*/);
	Mouth = sysInByte(0x71);
	tmBios.tm_mon = (Mouth&0x0F) + 10*((Mouth&0xF0)>>4) - 1;
	
	sysOutByte(0x70,0x09/*year*/);
	Year = sysInByte(0x71);
	tmBios.tm_year = 100 + (Year&0x0F) + 10*((Year&0xF0)>>4);

	return (tmBios);
}

2、设置BIOS时间

int SetBiosTime(tm curr_t)
{
	unsigned char temp;
	struct timespec tpp;
	struct tm tmBios;
	
	if(curr_t.tm_year > 1900)
	tmBios.tm_year = curr_t.year-1900;

	if((curr_t.tm_mon > 0) && (curr_t.tm_mon < 13))
		tmBios.tm_mon = curr_t.month - 1;
	if((curr_t.tm_mday > 0) && (curr_t.tm_mday < 32))
		tmBios.tm_mday = curr_t.tm_mday;
	if((curr_t.tm_hour >= 0) && (curr_t.tm_hour < 24))
		tmBios.tm_hour = curr_t.tm_hour;
	if((curr_t.tm_min >= 0) && (curr_t.tm_min < 60))
		tmBios.tm_min = curr_t.tm_min;
	if((curr_t.tm_sec >= 0) && (curr_t.tm_sec < 60))
		tmBios.tm_sec = curr_t.tm_sec;
	
	/* 将tmBios中的时间设为BIOS的时间 */
	temp=tmBios.tm_sec/10*16+tmBios.tm_sec%10;
	sysOutByte(0x70,0x00);
	sysOutByte(0x71,temp);
	
	temp=tmBios.tm_min/10*16+tmBios.tm_min%10;
	sysOutByte(0x70,0x02);
	sysOutByte(0x71,temp);
	
	temp=tmBios.tm_hour/10*16+tmBios.tm_hour%10;
	sysOutByte(0x70,0x04);
	sysOutByte(0x71,temp);
	
	temp=tmBios.tm_mday/10*16+tmBios.tm_mday%10;
	sysOutByte(0x70,0x07);
	sysOutByte(0x71,temp);
	
	temp=(tmBios.tm_mon+1)/10*16+(tmBios.tm_mon+1)%10;
	sysOutByte(0x70,0x08);
	sysOutByte(0x71,temp);
	
	temp=(tmBios.tm_year-100)/10*16+(tmBios.tm_year-100)%10;
	sysOutByte(0x70,0x09);
	sysOutByte(0x71,temp);
	
	/* 将tmBios中的时间设为系统的时间 */
	/* 使用ansiTime库(time.h)中的mktime ()函数将tm型结构的日期和时间转换成秒钟形式的日历时间 */
	tpp.tv_sec = mktime(&tmBios);
	tpp.tv_nsec = 0;
	clock_settime(CLOCK_REALTIME, &tpp);/*将系统时间设置为实时时钟的时间*/
	
	return OK;
}

3、通过shell设置BIOS时间

int SetBiosTime_shell(int year, int month, int day, int hour, int min, int sec)
{
	unsigned char temp;
	struct timespec tpp;
	struct tm tmBios;
	
	if(year > 1900)
	tmBios.tm_year = year-1900;

	if((month > 0) && (month < 13))
		tmBios.tm_mon = month - 1;
	if((day > 0) && (day < 32))
		tmBios.tm_mday = day;
	if((hour >= 0) && (hour < 24))
		tmBios.tm_hour = hour;
	if((min >= 0) && (min < 60))
		tmBios.tm_min = min;
	if((sec >= 0) && (sec < 60))
		tmBios.tm_sec = sec;
	
	/* 将tmBios中的时间设为BIOS的时间 */
	temp=tmBios.tm_sec/10*16+tmBios.tm_sec%10;
	sysOutByte(0x70,0x00);
	sysOutByte(0x71,temp);
	
	temp=tmBios.tm_min/10*16+tmBios.tm_min%10;
	sysOutByte(0x70,0x02);
	sysOutByte(0x71,temp);
	
	temp=tmBios.tm_hour/10*16+tmBios.tm_hour%10;
	sysOutByte(0x70,0x04);
	sysOutByte(0x71,temp);
	
	temp=tmBios.tm_mday/10*16+tmBios.tm_mday%10;
	sysOutByte(0x70,0x07);
	sysOutByte(0x71,temp);
	
	temp=(tmBios.tm_mon+1)/10*16+(tmBios.tm_mon+1)%10;
	sysOutByte(0x70,0x08);
	sysOutByte(0x71,temp);
	
	temp=(tmBios.tm_year-100)/10*16+(tmBios.tm_year-100)%10;
	sysOutByte(0x70,0x09);
	sysOutByte(0x71,temp);
	
	/* 将tmBios中的时间设为系统的时间 */
	/* 使用ansiTime库(time.h)中的mktime ()函数将tm型结构的日期和时间转换成秒钟形式的日历时间 */
	tpp.tv_sec = mktime(&tmBios);
	tpp.tv_nsec = 0;
	clock_settime(CLOCK_REALTIME, &tpp);/*将系统时间设置为实时时钟的时间*/
	
	return OK;
}

4、将系统时间与BIOS同步

int SyncSysTime(void)
{
	time_t cur;
	struct tm newtime;
	struct timespec ts;

	newtime = GetBiosTime();
	
	ts.tv_sec = mktime(&newtime);
	ts.tv_nsec = 0;
	clock_settime(CLOCK_REALTIME, &ts);
	
	cur = time((time_t*)NULL);		/* NULL相当于给time()初始化 */
	time(&cur);						/* 获取系统时间,单位为秒; */
	newtime= *localtime(&cur);		/* 转换成tm类型的结构体; */
	printf("%s",asctime(&newtime));	
	return (OK);
}

5、获取系统时间

struct tm GetSysTime(void)
{
	time_t stime;
	struct tm systime;

	time(&stime);
	systime = *localtime(&stime);
	#if DBG_SBS_RTC == 1
	printf("Time is:%s", asctime(&systime));
	#endif	
	return (systime);
}

6、时间测试

int GetBiosTime_test(void)
{
	struct tm tmBios;
	struct tm tmSys;
	
	tmBios = GetBiosTime();
	tmSys  = GetSysTime();
	printf("BIOS time:%d-%d-%d %d:%d:%d\n", tmBios.tm_year + 1900,
			tmBios.tm_mon + 1, tmBios.tm_mday, tmBios.tm_hour, tmBios.tm_min, tmBios.tm_sec);
	printf("System time:%d-%d-%d %d:%d:%d\n\n", tmSys.tm_year + 1900,
		tmSys.tm_mon + 1, tmSys.tm_mday, tmSys.tm_hour, tmSys.tm_min, tmSys.tm_sec);
	return (OK);
}
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值