mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 08:06:32 +00:00
🔧 更改目录结构,将定时器独立出来
将定时器独立出来
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
#include <mm/mm.h>
|
||||
#include <driver/interrupt/apic/apic.h>
|
||||
#include <exception/softirq.h>
|
||||
#include <driver/timers/timer.h>
|
||||
#include <time/timer.h>
|
||||
#include <process/process.h>
|
||||
#include <sched/sched.h>
|
||||
#include <smp/ipi.h>
|
||||
@ -149,6 +149,7 @@ int HPET_init()
|
||||
HPET_COUNTER_CLK_PERIOD = (tmp >> 32) & 0xffffffff;
|
||||
HPET_freq = 1.0 * 1e15 / HPET_COUNTER_CLK_PERIOD;
|
||||
HPET_NUM_TIM_CAP = (tmp >> 8) & 0x1f; // 读取计时器数量
|
||||
kinfo("Total HPET timers: %d", HPET_NUM_TIM_CAP);
|
||||
|
||||
// kinfo("HPET CLK_PERIOD=%#03lx Frequency=%f", HPET_COUNTER_CLK_PERIOD, (double)HPET_freq);
|
||||
// kdebug("HPET_freq=%ld", (long)HPET_freq);
|
||||
@ -167,15 +168,18 @@ int HPET_init()
|
||||
while (1)
|
||||
hlt();
|
||||
}
|
||||
|
||||
// kdebug("[HPET0] conf register=%#018lx conf register[63:32]=%#06lx", (*(uint64_t *)(HPET_REG_BASE + TIM0_CONF)), ((*(uint64_t *)(HPET_REG_BASE + TIM0_CONF))>>32)&0xffffffff);
|
||||
*(uint64_t *)(HPET_REG_BASE + MAIN_CNT) = 0;
|
||||
io_mfence();
|
||||
*(uint64_t *)(HPET_REG_BASE + TIM0_CONF) = 0x004c; // 设置定时器0为周期定时,边沿触发,投递到IO APIC的2号引脚(这里有点绕,写的是8259的引脚号,但是因为禁用了8259,因此会被路由到IO APIC的2号引脚)
|
||||
*(uint64_t *)(HPET_REG_BASE + TIM0_CONF) = 0x004c; // 设置定时器0为周期定时,边沿触发,默认投递到IO APIC的2号引脚(看conf寄存器的高32bit,哪一位被置1,则可以投递到哪一个I/O apic引脚)
|
||||
io_mfence();
|
||||
*(uint64_t *)(HPET_REG_BASE + TIM0_COMP) = clks_to_intr; // 5ms触发一次中断
|
||||
//*(uint64_t *)(HPET_REG_BASE + TIM0_COMP) = HPET_freq; // 1s触发一次中断
|
||||
|
||||
io_mfence();
|
||||
|
||||
// kdebug("[HPET0] conf register after modify=%#018lx", ((*(uint64_t *)(HPET_REG_BASE + TIM0_CONF))));
|
||||
// kdebug("[HPET1] conf register =%#018lx", ((*(uint64_t *)(HPET_REG_BASE + TIM1_CONF))));
|
||||
|
||||
rtc_get_cmos_time(&rtc_now);
|
||||
|
||||
kinfo("HPET Initialized.");
|
||||
|
@ -1,92 +0,0 @@
|
||||
#include "timer.h"
|
||||
#include <common/kprint.h>
|
||||
#include <exception/softirq.h>
|
||||
#include <mm/slab.h>
|
||||
#include <driver/timers/HPET/HPET.h>
|
||||
#include <process/process.h>
|
||||
|
||||
struct timer_func_list_t timer_func_head;
|
||||
|
||||
// 定时器循环阈值,每次最大执行10个定时器任务
|
||||
#define TIMER_RUN_CYCLE_THRESHOLD 10
|
||||
|
||||
void test_timer()
|
||||
{
|
||||
printk_color(ORANGE, BLACK, "(test_timer)");
|
||||
}
|
||||
|
||||
void timer_init()
|
||||
{
|
||||
timer_jiffies = 0;
|
||||
timer_func_init(&timer_func_head, NULL, NULL, -1UL);
|
||||
register_softirq(TIMER_SIRQ, &do_timer_softirq, NULL);
|
||||
|
||||
struct timer_func_list_t *tmp = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
|
||||
timer_func_init(tmp, &test_timer, NULL, 5);
|
||||
timer_func_add(tmp);
|
||||
|
||||
kdebug("timer func initialized.");
|
||||
}
|
||||
|
||||
void do_timer_softirq(void *data)
|
||||
{
|
||||
|
||||
struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
|
||||
int cycle_count = 0;
|
||||
while ((!list_empty(&timer_func_head.list)) && (tmp->expire_jiffies <= timer_jiffies))
|
||||
{
|
||||
|
||||
timer_func_del(tmp);
|
||||
tmp->func(tmp->data);
|
||||
kfree(tmp);
|
||||
|
||||
++cycle_count;
|
||||
// 当前定时器达到阈值
|
||||
if(cycle_count == TIMER_RUN_CYCLE_THRESHOLD)
|
||||
break;
|
||||
tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化定时功能
|
||||
*
|
||||
* @param timer_func 队列结构体
|
||||
* @param func 定时功能处理函数
|
||||
* @param data 传输的数据
|
||||
* @param expire_ms 定时时长(单位:ms)
|
||||
*/
|
||||
void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms)
|
||||
{
|
||||
list_init(&timer_func->list);
|
||||
timer_func->func = func;
|
||||
timer_func->data = data,
|
||||
// timer_func->expire_jiffies = timer_jiffies + expire_ms / 5 + expire_ms % HPET0_INTERVAL ? 1 : 0; // 设置过期的时间片
|
||||
timer_func->expire_jiffies = cal_next_n_ms_jiffies(expire_ms); // 设置过期的时间片
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将定时功能添加到列表中
|
||||
*
|
||||
* @param timer_func 待添加的定时功能
|
||||
*/
|
||||
void timer_func_add(struct timer_func_list_t *timer_func)
|
||||
{
|
||||
struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
|
||||
|
||||
if (list_empty(&timer_func_head.list) == false)
|
||||
while (tmp->expire_jiffies < timer_func->expire_jiffies)
|
||||
tmp = container_of(list_next(&tmp->list), struct timer_func_list_t, list);
|
||||
|
||||
list_add(&tmp->list, &(timer_func->list));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将定时功能从列表中删除
|
||||
*
|
||||
* @param timer_func
|
||||
*/
|
||||
void timer_func_del(struct timer_func_list_t *timer_func)
|
||||
{
|
||||
list_del(&timer_func->list);
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <common/glib.h>
|
||||
#include "HPET/HPET.h"
|
||||
#include "rtc/rtc.h"
|
||||
|
||||
uint64_t volatile timer_jiffies = 0; // 系统时钟计数
|
||||
|
||||
// 计算接下来n毫秒对应的系统时间片
|
||||
#define cal_next_n_ms_jiffies(expire_ms) (timer_jiffies + expire_ms / 5 + ((expire_ms % HPET0_INTERVAL) ? 1 : 0))
|
||||
|
||||
void timer_init();
|
||||
|
||||
void do_timer_softirq(void *data);
|
||||
|
||||
/**
|
||||
* @brief 定时功能队列
|
||||
*
|
||||
*/
|
||||
struct timer_func_list_t
|
||||
{
|
||||
struct List list;
|
||||
uint64_t expire_jiffies;
|
||||
void (*func)(void *data);
|
||||
void *data;
|
||||
};
|
||||
|
||||
extern struct timer_func_list_t timer_func_head;
|
||||
/**
|
||||
* @brief 初始化定时功能
|
||||
*
|
||||
* @param timer_func 队列结构体
|
||||
* @param func 定时功能处理函数
|
||||
* @param data 传输的数据
|
||||
* @param expire_ms 定时时长(单位:ms)
|
||||
*/
|
||||
void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms);
|
||||
|
||||
/**
|
||||
* @brief 将定时功能添加到列表中
|
||||
*
|
||||
* @param timer_func 待添加的定时功能
|
||||
*/
|
||||
void timer_func_add(struct timer_func_list_t *timer_func);
|
||||
|
||||
/**
|
||||
* @brief 将定时功能从列表中删除
|
||||
*
|
||||
* @param timer_func
|
||||
*/
|
||||
void timer_func_del(struct timer_func_list_t *timer_func);
|
@ -2,7 +2,7 @@
|
||||
#include <mm/mm.h>
|
||||
#include <common/printk.h>
|
||||
#include <driver/multiboot2/multiboot2.h>
|
||||
#include <driver/timers/timer.h>
|
||||
#include <time/timer.h>
|
||||
#include <common/kprint.h>
|
||||
#include <mm/mm.h>
|
||||
#include <mm/slab.h>
|
||||
|
Reference in New Issue
Block a user