diff --git a/cpu/riscv.c b/cpu/riscv.c index f9f5467..c0ecfc0 100644 --- a/cpu/riscv.c +++ b/cpu/riscv.c @@ -115,6 +115,7 @@ uint32_t mem_w_read(riscv_t* riscv, uint32_t addr) { if (addr & 0x3) { riscv->exc_code = 4; riscv->mtval = addr; + printf("unaligned access addr:%08x\n", addr); return ret; } if (addr >= MEM_ADDR_BASE && addr < MEM_ADDR_BASE + MEM_SIZE) { @@ -125,6 +126,7 @@ uint32_t mem_w_read(riscv_t* riscv, uint32_t addr) { device_read(riscv, addr, ret); riscv->exc_code = 5; riscv->mtval = addr; + printf("error addr:%08x\n", addr); } return ret; } @@ -353,7 +355,7 @@ void ins_mret(riscv_t* riscv) { } else { riscv->mstatus &= ~MSTATUS_MIE; } - printf("mret: pc=%08x\n", riscv->pc); + // printf("mret: pc=%08x\n", riscv->pc); } void ins_or(riscv_t* riscv, int rs2, int rs1, int rd) { diff --git a/cpu/timer.c b/cpu/timer.c index fa8d82b..300fb82 100644 --- a/cpu/timer.c +++ b/cpu/timer.c @@ -75,7 +75,7 @@ static void run(riscv_t *riscv, const struct device_t* device) { static void *timer_thread(void *arg) { while (1) { - milliseconds_sleep(1000); + milliseconds_sleep(1); pthread_mutex_lock(&g_self.lock); g_self.ins_count++; pthread_mutex_unlock(&g_self.lock); diff --git a/riscv/rtthread/libcpu/risc-v/common/interrupt_gcc.S b/riscv/rtthread/libcpu/risc-v/common/interrupt_gcc.S index 04521a2..9fa36c5 100755 --- a/riscv/rtthread/libcpu/risc-v/common/interrupt_gcc.S +++ b/riscv/rtthread/libcpu/risc-v/common/interrupt_gcc.S @@ -17,7 +17,7 @@ .align 6 #else .align 2 -#endif +#endif .global SW_handler SW_handler: @@ -352,3 +352,24 @@ trap_entry: spurious_interrupt: tail rt_hw_context_switch_exit + + + + + + .globl rt_hw_do_after_save_above + .type rt_hw_do_after_save_above,@function +rt_hw_do_after_save_above: + addi sp, sp, -4 + STORE ra, 0 * REGBYTES(sp) + + csrr a0, mcause + csrr a1, mepc + mv a2, sp + call rt_rv32_system_irq_handler + + LOAD ra, 0 * REGBYTES(sp) + addi sp, sp, 4 + ret + + diff --git a/riscv/startup/main.c b/riscv/startup/main.c index c42cf2a..5dd91bf 100644 --- a/riscv/startup/main.c +++ b/riscv/startup/main.c @@ -2,6 +2,7 @@ #include "stdio.h" #include "stdarg.h" #include "head.h" +#include "rtthread.h" #define PRINT_BASE_ADDR *(uint8_t *)0x40000000 @@ -32,36 +33,36 @@ int my_printf(const char* fmt, ...) { -int add(int a, int b) { - return a+b; -} - char g_string[] = "string from ram"; + + +void task1(void* par) { + (void)par; + while (1) { + my_printf("task1: %s\n", g_string); + rt_thread_delay(500); + } +} + + + + + int main() { - int a=1; - int b=2; - typedef void (*fun_t)(void); - fun_t fun = (fun_t)0; - set_csr(mstatus, 0x00000008); - set_csr(mie, (1 << 3) | (1 << 7)); - set_csr(mip, (1 << 3)); - fun(); - int c = add(a, b); my_printf("Hello World! %s\n", "Andy"); - my_printf("add(%d, %d)=%d\n", a, b, c); - a = 67;b = -78; - fun=(fun_t)0xff000000; - fun(); - my_printf("mul(%d, %d)=%d\n", a, b, a * b); - my_printf("ram_val test: %s\n", g_string); - cpu_test(); + // cpu_test(); + rt_thread_t t = rt_thread_create("task1", task1, 0, 1024, 10, 10); + (void)t; + rt_thread_startup(t); my_printf("enter while 1\n"); while (1) { + my_printf("main: %s\n", g_string); + rt_thread_delay(1000); } return 0; } \ No newline at end of file diff --git a/riscv/startup/rtthread_irq.c b/riscv/startup/rtthread_irq.c index 7e30640..2afb0e3 100644 --- a/riscv/startup/rtthread_irq.c +++ b/riscv/startup/rtthread_irq.c @@ -1,15 +1,100 @@ #include "stddef.h" #include "head.h" #include "rtthread.h" +#include "rt_hw_stack_frame.h" + +static const char *g_fault_cause[]={ + "Instruction address misaligned",// 0 + "Instruction access fault",// 1 + "Illegal instruction",// 2 + "Breakpoint",// 3 + "Load address misaligned",// 4 + "Load access fault",// 5 + "Store address misaligned",// 6 + "Store access fault",// 7 + "Environment call from U-mode",// 8 + "Environment call from S-mode",// 9 + "", + "Environment call from M-mode",// 11 + "Instruction page fault",// 12 + "Load page fault",// 13 + "", + "Store page fault",// 15 +}; + + +static void trap_print(uint32_t irq_num, rt_hw_stack_frame_t *stack) { + my_printf("fault reason is %s\n", g_fault_cause[irq_num]); + my_printf("x1(ra): %08x\n", stack->ra); + my_printf("x3(gp): %08x\n", stack->gp); + my_printf("x4(tp): %08x\n", stack->tp); + my_printf("x5(t0): %08x\n", stack->t0); + my_printf("x6(t1): %08x\n", stack->t1); + my_printf("x7(t2): %08x\n", stack->t2); + my_printf("x8(s0|fp): %08x\n", stack->s0_fp); + my_printf("x9(s1): %08x\n", stack->s1); + my_printf("x10(a0): %08x\n", stack->a0); + my_printf("x11(a1): %08x\n", stack->a1); + my_printf("x12(a2): %08x\n", stack->a2); + my_printf("x13(a3): %08x\n", stack->a3); + my_printf("x14(a4): %08x\n", stack->a4); + my_printf("x15(a5): %08x\n", stack->a5); + my_printf("x16(a6): %08x\n", stack->a6); + my_printf("x17(a7): %08x\n", stack->a7); + my_printf("x18(s2): %08x\n", stack->s2); + my_printf("x19(s3): %08x\n", stack->s3); + my_printf("x20(s4): %08x\n", stack->s4); + my_printf("x21(s5): %08x\n", stack->s5); + my_printf("x22(s6): %08x\n", stack->s6); + my_printf("x23(s7): %08x\n", stack->s7); + my_printf("x24(s8): %08x\n", stack->s8); + my_printf("x25(s9): %08x\n", stack->s9); + my_printf("x26(s10): %08x\n", stack->s10); + my_printf("x27(s11): %08x\n", stack->s11); + my_printf("x28(t3): %08x\n", stack->t3); + my_printf("x29(t4): %08x\n", stack->t4); + my_printf("x30(t5): %08x\n", stack->t5); + my_printf("x31(t6): %08x\n", stack->t6); + my_printf("mstatus: %08x\n", stack->mstatus); + my_printf("mepc: %08x\n", stack->epc); + // my_printf("mtval: %08x\n", read_csr(mtval)); + + // 函数调用错误,直接跳过这个函数 + if(irq_num==1){ + my_printf("skip the function call, return to %08x\n", stack->ra); + stack->epc = stack->ra; + } + + // write_csr(mscratch, 0xffffffff); +} + + void handle_trap(size_t mcause, size_t mepc, size_t sp) { - my_printf("interupt mcause=%08x, mepc=%08x, sp=%08x\n", mcause, mepc, sp); + if (mcause & 0x80000000) { + my_printf("interupt mcause=%08x, mepc=%08x, sp=%08x\n", mcause, mepc, sp); + } else { + trap_print(mcause, (rt_hw_stack_frame_t*)sp); + while (1); + } } + +static void tick_handler(int vec, void* par) { + (void)vec; + (void)par; + rt_tick_increase(); + // my_printf("tick irq %d\n", vec); +} + + +static uint32_t g_mem_heap[200 * 1024 / 4]; void rt_hw_board_init(void) { - + set_csr(mie, (1 << 3) | (1 << 7)); + set_csr(mip, (1 << 3)); + rt_system_heap_init(g_mem_heap, g_mem_heap + sizeof(g_mem_heap) / 4); + rt_hw_interrupt_init(); + rt_hw_interrupt_install(7, tick_handler, 0, "tick_handler"); } -// rt_device_t rt_device_find(const char* name) { -// return 0; -// } + diff --git a/riscv/startup/start.S b/riscv/startup/start.S index 03cd0ee..8f7a312 100644 --- a/riscv/startup/start.S +++ b/riscv/startup/start.S @@ -5,7 +5,7 @@ .extern main # .extern trap_handler -.extern trap_entry +.extern SW_handler .extern entry _start: @@ -33,7 +33,7 @@ _start: addi a0, a0, 4 bltu a0, a1, 1b 2: - la a0, trap_entry + la a0, SW_handler csrw mtvec, a0 /* Call global constructors */ diff --git a/riscv/startup/test.c b/riscv/startup/test.c index 19c0a35..a2689a4 100644 --- a/riscv/startup/test.c +++ b/riscv/startup/test.c @@ -96,6 +96,12 @@ void test12(int8_t a,int b){ my_printf("shift right %08x, %08x\n",a,a>>b); } + +int add(int a, int b) { + return a+b; +} + + void cpu_test() { my_printf("test 32 bit\n"); @@ -109,7 +115,7 @@ void cpu_test() { test3(1,-2); test4(1,2); test4(1,-2); - + my_printf("test 8 bit\n"); test5(1,2); test5(1,-2); @@ -124,6 +130,19 @@ void cpu_test() { test11(0x80,7); test12(0x80,7); + int a=1; + int b=2; + typedef void (*fun_t)(void); + fun_t fun = (fun_t)0; + set_csr(mstatus, 0x00000008); + fun(); + int c = add(a, b); + my_printf("add(%d, %d)=%d\n", a, b, c); + a = 67;b = -78; + fun=(fun_t)0xff000000; + fun(); + my_printf("mul(%d, %d)=%d\n", a, b, a * b); + my_printf("ram_val test: %s\n", "test string"); }