?? timer.cpp
字號(hào):
/*
* Copyright (C) 2004, Thejesh AP. All rights reserved.
*/
#include <sys\types.h>
#include <null.h>
#include <stdlib.h>
#include <string.h>
#include <jazmyn\handlers.h>
#include <jazmyn\kernel.h>
#include <fs\devmgr.h>
#include <drivers\console.h>
#include <drivers\keyboard.h>
#include <drivers\timer.h>
extern device_manager _dev_mgr;
timer::timer(char *name) : driver(name)
{
int err;
if((err = _dev_mgr.register_driver(1,this->name,0,timer_main))<0)
{
cout<<"register_driver ERROR : "<<this->name<<" "<<err<<endl;
return;
}
if((err = set_handler(IRQ0,timer_handler))<0)
{
cout<<"set_handler ERROR : "<<this->name<<" "<<err<<endl;
return;
}
prog_pit(PROG_HZ,TMR_SC0);
for(int i=0;i<MAX_CALL;i++)
{
call_arr[i].end_time = 0;
call_arr[i].fun = NULL;
}
sys_boot_time = get_time();
curr_time = sys_boot_time;
enable_irq(IRQ0);
cout<<"timer driver succesfully registered [name] = "<<this->name<<endl;
}
timer::~timer()
{
}
void timer::prog_pit(float Hz,byte counter)
{
uint val=0;
if(Hz < 18.206759) Hz = 18.206759;
val = 1193180/Hz;
outportb(TMR_CTRL, counter | TMR_BOTH | TMR_MD3);
outportb((0x40+counter),val & 0xFF);
outportb((0x40+counter),(val>>8) & 0xFF);
}
int timer::call_after(uint millisec,void (*fun)())
{
ullong end = curr_time + millisec;
for(int i=0;i<MAX_CALL;i++)
{
if(call_arr[i].fun == NULL)
{
call_arr[i].fun = fun;
call_arr[i].end_time = end;
}
}
}
ullong timer::get_time()
{
return 0;
}
timer _timer_obj("timer");
int ticks = 0;
extern int sched_ready;
void timer_handler()
{
_timer_obj.curr_time += (ullong)(1000 / PROG_HZ);
ullong curr = _timer_obj.curr_time;
for(int i=0;i<MAX_CALL;i++)
{
if(_timer_obj.call_arr[i].fun && curr >= _timer_obj.call_arr[i].end_time)
{
void (*f)() = _timer_obj.call_arr[i].fun;
_timer_obj.call_arr[i].fun = NULL;
f();
}
}
ticks++;
if(ticks >= 2 && sched_ready)
{
ticks = 0;
schedule();
}
}
int timer_main(void *req)
{
/*
*There is no support to directly program the timer as it may affect the functioning
*of scheduler & other pending calls.
*/
return -1;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -