forked from VictorYXL/MyOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
52 lines (52 loc) · 1.06 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include"timer.h"
#include"nasmfunc.h"
#include"buffer.h"
/*
设置定时中断频率为100Hz(设定值为11932,0x2e9c):
向0x43写入0x34
向0x40写入设定值的低8位
向0x40写入设定值的高8位
*/
struct TimerCTL timerctl;
struct Timer *taskTimer;
void initPit()
{
io_out8(PIT_CTRL,0x34);
io_out8(PIT_CNT0,0x9c);
io_out8(PIT_CNT0,0x2e);
timerctl.count=0;
timerctl.next=TIME_MAX;
int i;
for (i=0;i<TIMER_MAX;i++)
timerctl.timer[i].flag=TIMER_UNALLOCED;
return;
}
struct Timer *allocTimer()
{
int i;
for (i=0;i<TIMER_MAX;i++)
if (timerctl.timer[i].flag==TIMER_UNALLOCED)
{
timerctl.timer[i].flag=TIMER_ALLOCED;
return &timerctl.timer[i];
}
return 0;
}
void freeTimer(struct Timer *timer)
{
timer->flag=TIMER_UNALLOCED;
return;
}
void initTimer(struct Timer *timer,struct Buffer *buffer,unsigned char timeoutData)
{
timer->timeoutBuffer=buffer;
timer->timeoutData=timeoutData;
}
void setTimer(struct Timer *timer,unsigned int timeout)
{
timer->timeout=timeout+timerctl.count;
timer->flag=TIMER_USING;
if (timer->timeout<timerctl.next)
timerctl.next=timer->timeout;
return;
}