Added timer module

This commit is contained in:
lucic71 2022-06-28 19:40:51 +03:00
parent 4721ec22a5
commit aeb4561364
3 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,16 @@
#ifndef TIMER_H_
#define TIMER_H_
#include <stdint.h>
/*
* timer_init:
* Initializes the timer to a give tick frequency.
*
* @param freq - Frequency
*
*/
void timer_init(uint32_t freq);
#endif

View File

@ -0,0 +1,22 @@
#ifndef TIMER_INTERNALS_H_
#define TIMER_INTERNALS_H_
/* Command ports. */
#define PIT_COMMAND_PORT 0x43
/* Data ports. */
#define PIT_CH0_DATA_PORT 0x40
#define PIT_CH1_DATA_PORT 0x41
#define PIT_CH2_DATA_PORT 0x42
/* Command bytes. */
#define PIT_DIV_READY 0x36
/* Default frequency of PIT's internal clock. */
#define PIT_DEFAULT_FREQ 1193180
#endif

51
kernel/timer/src/timer.c Normal file
View File

@ -0,0 +1,51 @@
#include "kernel/timer.h"
#include "timer_internals.h"
#include "kernel/screen.h"
#include "i386/irq.h"
#include "lib/memio.h"
#include <stdio.h>
static uint32_t tick;
/*
* timer_intrpt_handler:
* ---------------------
*
*/
static void timer_intrpt_handler(__attribute__ ((unused)) context_t ctx) {
tick++;
screen_delete_line();
printf("Tick: %d", tick);
}
/*
* timer_init:
* -----------
*
* Register the IRQ and sent the frequency to PIT.
*
*/
void timer_init(uint32_t freq) {
register_irq(IRQ0, timer_intrpt_handler);
outb(PIT_COMMAND_PORT, PIT_DIV_READY);
uint16_t divisor = PIT_DEFAULT_FREQ / freq;
uint8_t freq_low = (uint8_t) (divisor & 0xFF);
uint8_t freq_high = (uint8_t) (divisor >> 8);
outb(PIT_CH0_DATA_PORT, freq_low);
outb(PIT_CH0_DATA_PORT, freq_high);
}