Skip to content

Commit

Permalink
add timer driver and testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
saursin committed May 19, 2024
1 parent 12a7823 commit d8c0a8d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sw/examples/interrupt/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src_files = interrupt.c
executable = interrupt.elf
35 changes: 35 additions & 0 deletions sw/examples/interrupt/interrupt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <arch.h>
#include <timer.h>

#define TIMER_INTERRUPT_DELAY 1000

void timer_interrupt_handler(){
// Clear interrupt
timer_clear_interrupt();
puts("!!! Caught timer interrupt !!!\n");
}

void setup_timer_interrupt() {
// Register Interrupt handler
register_interrupt_handler(7, timer_interrupt_handler);

// Request timer interrupt
timer_get_interrupt(TIMER_INTERRUPT_DELAY);

// Enable interrupts in core
arch_en_mti();
arch_en_int();
}

void main()
{
serial_init(UART_BAUD_115200);
puts("Before interrupt\n");

setup_timer_interrupt();
asm volatile("wfi"); // wait for interrupt

puts("After interrupt\n");
return;
}
8 changes: 8 additions & 0 deletions sw/lib/include/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <stdint.h>

uint64_t timer_get_time();

void timer_get_interrupt(uint64_t time);

void timer_clear_interrupt();
27 changes: 27 additions & 0 deletions sw/lib/libcatom/hydrogensoc/timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <timer.h>
#include <mmio.h>
#include <platform.h>

#define TIMER_REG_MTIME 0x0
#define TIMER_REG_MTIMEH 0x4
#define TIMER_REG_MTIMECMP 0x8
#define TIMER_REG_MTIMECMPH 0xc

uint64_t timer_get_time() {
uint64_t time = (uint64_t) REG32(TIMER_ADDR, TIMER_REG_MTIMEH);
time = (time << 32 ) | (uint64_t) REG32(TIMER_ADDR, TIMER_REG_MTIME);
return time;
}

void timer_get_interrupt(uint64_t time){
uint64_t curtime = (uint64_t) REG32(TIMER_ADDR, TIMER_REG_MTIMEH);
curtime = (curtime << 32 ) | (uint64_t) REG32(TIMER_ADDR, TIMER_REG_MTIME);
uint64_t exp_time = curtime + time;
REG32(TIMER_ADDR, TIMER_REG_MTIMECMPH) = 0xffffffff & (exp_time >> 32);
REG32(TIMER_ADDR, TIMER_REG_MTIMECMP) = 0xffffffff & exp_time;
}

void timer_clear_interrupt(){
REG32(TIMER_ADDR, TIMER_REG_MTIMECMPH) = (uint32_t)-1;
REG32(TIMER_ADDR, TIMER_REG_MTIMECMP) = (uint32_t)-1;
}

0 comments on commit d8c0a8d

Please sign in to comment.