📄 time.c
字号:
#include <44b.h>#include <sys.h>#include <stdio.h>#include <stdlib.h>#include <sys/tmr.h>#include <sys/intr.h>#include <sys/time.h>#include <sys/queue.h>#include <sys/timer.h>#include <sys/config.h>#define EPOCH_YEAR 1970/* Duration added to current time of day for each timer interrupt */u_long tick = 1000000 * (128/CLOCK) / 128; static struct timeval systemclock = { 0, 0 };static int timerclock = 0;static voidclock_isr(void *params){#if _TIME_SLICING proc_t proc; int preempt = 0;#endif disable; // Increment system time systemclock.tv_usec += tick; timernormalize(&systemclock);
// Increment timer clock timerclock += tick; if (timerclock >= TIMER_ISR_PERIOD) { timer_isr(); timerclock = 0; #if _TIME_SLICING preempt = 1; #endif } // Issue timer eoi intr_eoi(INTR_TICK);
#if _TIME_SLICING if (preempt) { proc = firstq(&ready); if (proc != NULL) { current->state = PS_READY; insq(current, &ready); proc_transfer(); // enables interrupts } else enable; }#else enable;#endif
// #define _TEST 1
//
// if(_TEST)
// {
// unsigned char test_sec = 0;
//
// rRTCCON = 0x01;
// if(test_sec != rBCDSEC)
// {
// test_sec = rBCDSEC;
// kprintf( "%02x, %02x, %02x, %02x, %02x, %02x, ",
// rBCDYEAR,rBCDMON,rBCDDAY,rBCDHOUR,rBCDMIN,rBCDSEC);
// kprintf("\b\b\b\b\b\b\b\b\b\b\b\b");
// kprintf("\b\b\b\b\b\b\b\b\b\b\b\b");
// }
// rRTCCON = 0x00;
// }
}void rtc_init(){
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
rBCDYEAR = 0x02;
rBCDMON = 0x01;
rBCDDAY = 0x01; // SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
rBCDDATE = 0x03;
rBCDHOUR = 0x00;
rBCDMIN = 0x00;
rBCDSEC = 0x00;
rRTCCON = 0x00; // R/W enable, 1/32768, Normal(merge), No reset
}voidtime_init(){ char buf[sizeof(struct tm)]; struct tm *tm; isr_inst(INTR_TICK, clock_isr, NULL); /* A tmr frequency of zero causes the default CLOCK to be used */ tmrstart(); intr_unmask(INTR_TICK); tm = (struct tm *) buf;
rRTCCON = 0x01; tm->tm_year = bcd2int(rBCDYEAR); tm->tm_mon = bcd2int(rBCDMON) - 1; tm->tm_mday = bcd2int(rBCDDAY); tm->tm_hour = bcd2int(rBCDHOUR); tm->tm_min = bcd2int(rBCDMIN); tm->tm_sec = bcd2int(rBCDSEC);
if(tm->tm_sec == 0)
{
tm->tm_year = bcd2int(rBCDYEAR); tm->tm_mon = bcd2int(rBCDMON) - 1; tm->tm_mday = bcd2int(rBCDDAY); tm->tm_hour = bcd2int(rBCDHOUR); tm->tm_min = bcd2int(rBCDMIN); tm->tm_sec = bcd2int(rBCDSEC);
} rRTCCON = 0x00;
/* Y2K */ if (tm->tm_year < EPOCH_YEAR % 100) tm->tm_year += 100; tm->tm_wday = 0; tm->tm_yday = 0; tm->tm_isdst = 0; systemclock.tv_sec = tm2time(tm);}
time_ttime(){ return systemclock.tv_sec;}/* * The caller must disable/enable interrupts */voidutime(long *tv_sec, long *tv_usec){ *tv_sec = systemclock.tv_sec; *tv_usec = systemclock.tv_usec;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -