vxworks for x86读取bios时间的解决方法
系统时间与bsp有关,在vzworks for x86系列的目标没有直接读取RTC(实时时钟控制器)的函数,用time.h中的函数读到的始终是 00:00:00, Jan. 1 1970.
所以在x86系列的机器中,我们可以从bios中读取当前的时钟。用sysInByte(),sysOutByte(),在70,和71端口读取或写bios里的时间.
首先要分析bios的内容,找出秒,分,时,天,月,年的存放地址。
他们分别是: 0x00,0x02,0x04,0x07,0x08,0x09
然后从71端口读出相应的值,进行转换。
如:秒
sysOutByte(0x70,0x00);
second = sysInByte(0x71);
读出的second进行转换,:
second = (second &0x0F) + 10*((second &0xF0)>>4);
示例代码:
time_t biostime()
{
struct tm ahora;
unsigned char cHour, cMin, cSec;
unsigned char cDay, cMonth, cYear;
sysOutByte(0x70,0x00/*second*/);
cSec = sysInByte(0x71);
ahora.tm_sec = (cSec&0x0F) + 10*((cSec&0xF0)>>4);
sysOutByte(0x70,0x02/*minut*/);
cMin = sysInByte(0x71);
ahora.tm_min = (cMin&0x0F) + 10*((cMin&0xF0)>>4);
sysOutByte(0x70,0x04/*hour*/);
cHour = sysInByte(0x71);
ahora.tm_hour = (cHour&0x0F) + 10*((cHour&0xF0)>>4);
sysOutByte(0x70,0x07/*day*/);
cDay = sysInByte(0x71);
ahora.tm_mday = (cDay&0x0F) + 10*((cDay&0xF0)>>4);
sysOutByte(0x70,0x08/*month*/);
cMonth = sysInByte(0x71);
ahora.tm_mon = (cMonth&0x0F) + 10*((cMonth&0xF0)>>4) - 1;
sysOutByte(0x70,0x09/*year*/);
cYear = sysInByte(0x71);
ahora.tm_year = 100 + (cYear&0x0F) + 10*((cYear&0xF0)>>4);
return mktime(&ahora);
}
我们在系统初始化时读取bios时间一次,然后修改系统时钟:
用
clock_settime(..)
以后我们得到的时间就都是当前的正确时间
示例:
void inittime()
{
int res;
struct timespec ts;
struct tm daytime;
time_t stime;
ts.tv_sec = biostime();
ts.tv_nsec = 0;
res = clock_settime(CLOCK_REALTIME, &ts);
stime = time(NULL);
daytime = *localtime(&stime);
printf ( "time is :%s\n", asctime(&daytime) );
}