#include #define GPIO_BASE 0x40000000u static volatile uint32_t * const R_FREQ = (volatile uint32_t *)(GPIO_BASE+0); static volatile uint32_t * const LEDS = (volatile uint32_t *)(GPIO_BASE+4); static volatile uint32_t * const LEDGR = (volatile uint32_t *)(GPIO_BASE+8); #define TIMER_BASE 0x40010000u static volatile uint32_t * const TIMER = (volatile uint32_t *)(TIMER_BASE+0); #define MSTATUS_MIE (1u << 3) #define MIE_MTIE (1u << 7) extern void trap_entry(); static inline void irq_init() { /* mtvec first */ asm volatile ("csrw mtvec, %0" :: "r"(trap_entry)); /* enable machine timer interrupt */ asm volatile ("csrs mie, %0" :: "r"(MIE_MTIE)); /* global enable last */ asm volatile ("csrs mstatus, %0" :: "r"(MSTATUS_MIE)); } void timer_isr(){ static int set = 0; *TIMER = 1840000*2; *LEDGR = ~(*LEDGR); } void main(){ irq_init(); *LEDGR = 3; *TIMER = 1840000*2; for(;;){ for(int i=1000; i<10000; i++){ *R_FREQ = i; for(int j=0; j<80; j++) asm volatile("nop"); } } }