-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
src_files = interrupt.c | ||
executable = interrupt.elf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |