mirror of
https://github.com/yourtion/30dayMakeOS.git
synced 2026-05-03 03:40:30 +08:00
设定多个定时器
This commit is contained in:
@@ -4,40 +4,69 @@
|
||||
|
||||
#define PIT_CTRL 0x0043
|
||||
#define PIT_CNT0 0x0040
|
||||
#define TIMER_FLAGS_ALLOC 1 /* 已配置状态 */
|
||||
#define TIMER_FLAGS_USING 2 /* 定时器运行中 */
|
||||
|
||||
struct TIMERCTL timerctl;
|
||||
|
||||
void init_pit(void)
|
||||
{
|
||||
int i;
|
||||
io_out8(PIT_CTRL, 0x34);
|
||||
io_out8(PIT_CNT0, 0x9c);
|
||||
io_out8(PIT_CNT0, 0x2e);
|
||||
timerctl.count = 0;
|
||||
timerctl.timeout = 0;
|
||||
for (i = 0; i < MAX_TIMER; i++) {
|
||||
timerctl.timer[i].flags = 0; /* 未使用 */
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
struct TIMER *timer_alloc(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_TIMER; i++) {
|
||||
if (timerctl.timer[i].flags == 0) {
|
||||
timerctl.timer[i].flags = TIMER_FLAGS_ALLOC;
|
||||
return &timerctl.timer[i];
|
||||
}
|
||||
}
|
||||
return 0; /* 没找到 */
|
||||
}
|
||||
|
||||
void timer_free(struct TIMER *timer)
|
||||
{
|
||||
timer->flags = 0; /* 未使用 */
|
||||
return;
|
||||
}
|
||||
|
||||
void timer_init(struct TIMER *timer, struct FIFO8 *fifo, unsigned char data)
|
||||
{
|
||||
timer->fifo = fifo;
|
||||
timer->data = data;
|
||||
return;
|
||||
}
|
||||
|
||||
void timer_settime(struct TIMER *timer, unsigned int timeout)
|
||||
{
|
||||
timer->timeout = timeout;
|
||||
timer->flags = TIMER_FLAGS_USING;
|
||||
return;
|
||||
}
|
||||
|
||||
void inthandler20(int *esp)
|
||||
{
|
||||
int i;
|
||||
io_out8(PIC0_OCW2, 0x60); /* 把IRQ-00信号接收完了的信息通知给PIC */
|
||||
timerctl.count++;
|
||||
if (timerctl.timeout > 0) { /* 如果已经设定了超时 */
|
||||
timerctl.timeout--;
|
||||
if (timerctl.timeout == 0) {
|
||||
fifo8_put(timerctl.fifo, timerctl.data);
|
||||
for (i = 0; i < MAX_TIMER; i++) {
|
||||
if (timerctl.timer[i].flags == TIMER_FLAGS_USING) {
|
||||
timerctl.timer[i].timeout--;
|
||||
if (timerctl.timer[i].timeout == 0) {
|
||||
timerctl.timer[i].flags = TIMER_FLAGS_ALLOC;
|
||||
fifo8_put(timerctl.timer[i].fifo, timerctl.timer[i].data);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void settimer(unsigned int timeout, struct FIFO8 *fifo, unsigned char data)
|
||||
{
|
||||
int eflags;
|
||||
eflags = io_load_eflags();
|
||||
io_cli();
|
||||
timerctl.timeout = timeout;
|
||||
timerctl.fifo = fifo;
|
||||
timerctl.data = data;
|
||||
io_store_eflags(eflags);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user