From e01455155ab5a564eddd467b02a0226b3fa5ea7d Mon Sep 17 00:00:00 2001 From: zhangjun <2281979437@qq.com> Date: Mon, 17 Jul 2017 15:44:00 +0800 Subject: add context_gcc.s --- libcpu/risc-v/e310/init.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 libcpu/risc-v/e310/init.c (limited to 'libcpu/risc-v/e310/init.c') diff --git a/libcpu/risc-v/e310/init.c b/libcpu/risc-v/e310/init.c new file mode 100644 index 0000000000..621a6e2dd6 --- /dev/null +++ b/libcpu/risc-v/e310/init.c @@ -0,0 +1,238 @@ +#include +#include +#include + +#include "platform.h" +#include "encoding.h" + +extern int main(int argc, char** argv); +extern void trap_entry(); + +static unsigned long mtime_lo(void) +{ + return *(volatile unsigned long *)(CLINT_CTRL_ADDR + CLINT_MTIME); +} + +#ifdef __riscv32 + +static uint32_t mtime_hi(void) +{ + return *(volatile uint32_t *)(CLINT_CTRL_ADDR + CLINT_MTIME + 4); +} + +uint64_t get_timer_value() +{ + while (1) { + uint32_t hi = mtime_hi(); + uint32_t lo = mtime_lo(); + if (hi == mtime_hi()) + return ((uint64_t)hi << 32) | lo; + } +} + +#else /* __riscv32 */ + +uint64_t get_timer_value() +{ + return mtime_lo(); +} + +#endif + +unsigned long get_timer_freq() +{ + return 32768; +} + +static void use_hfrosc(int div, int trim) +{ + // Make sure the HFROSC is running at its default setting + PRCI_REG(PRCI_HFROSCCFG) = (ROSC_DIV(div) | ROSC_TRIM(trim) | ROSC_EN(1)); + while ((PRCI_REG(PRCI_HFROSCCFG) & ROSC_RDY(1)) == 0) ; + PRCI_REG(PRCI_PLLCFG) &= ~PLL_SEL(1); +} + +static void use_pll(int refsel, int bypass, int r, int f, int q) +{ + // Ensure that we aren't running off the PLL before we mess with it. + if (PRCI_REG(PRCI_PLLCFG) & PLL_SEL(1)) { + // Make sure the HFROSC is running at its default setting + use_hfrosc(4, 16); + } + + // Set PLL Source to be HFXOSC if available. + uint32_t config_value = 0; + + config_value |= PLL_REFSEL(refsel); + + if (bypass) { + // Bypass + config_value |= PLL_BYPASS(1); + + PRCI_REG(PRCI_PLLCFG) = config_value; + + // If we don't have an HFXTAL, this doesn't really matter. + // Set our Final output divide to divide-by-1: + PRCI_REG(PRCI_PLLDIV) = (PLL_FINAL_DIV_BY_1(1) | PLL_FINAL_DIV(0)); + } else { + // In case we are executing from QSPI, + // (which is quite likely) we need to + // set the QSPI clock divider appropriately + // before boosting the clock frequency. + + // Div = f_sck/2 + SPI0_REG(SPI_REG_SCKDIV) = 8; + + // Set DIV Settings for PLL + // Both HFROSC and HFXOSC are modeled as ideal + // 16MHz sources (assuming dividers are set properly for + // HFROSC). + // (Legal values of f_REF are 6-48MHz) + + // Set DIVR to divide-by-2 to get 8MHz frequency + // (legal values of f_R are 6-12 MHz) + + config_value |= PLL_BYPASS(1); + config_value |= PLL_R(r); + + // Set DIVF to get 512Mhz frequncy + // There is an implied multiply-by-2, 16Mhz. + // So need to write 32-1 + // (legal values of f_F are 384-768 MHz) + config_value |= PLL_F(f); + + // Set DIVQ to divide-by-2 to get 256 MHz frequency + // (legal values of f_Q are 50-400Mhz) + config_value |= PLL_Q(q); + + // Set our Final output divide to divide-by-1: + PRCI_REG(PRCI_PLLDIV) = (PLL_FINAL_DIV_BY_1(1) | PLL_FINAL_DIV(0)); + + PRCI_REG(PRCI_PLLCFG) = config_value; + + // Un-Bypass the PLL. + PRCI_REG(PRCI_PLLCFG) &= ~PLL_BYPASS(1); + + // Wait for PLL Lock + // Note that the Lock signal can be glitchy. + // Need to wait 100 us + // RTC is running at 32kHz. + // So wait 4 ticks of RTC. + uint32_t now = mtime_lo(); + while (mtime_lo() - now < 4) ; + + // Now it is safe to check for PLL Lock + while ((PRCI_REG(PRCI_PLLCFG) & PLL_LOCK(1)) == 0) ; + } + + // Switch over to PLL Clock source + PRCI_REG(PRCI_PLLCFG) |= PLL_SEL(1); +} + +static void use_default_clocks() +{ + // Turn off the LFROSC + AON_REG(AON_LFROSC) &= ~ROSC_EN(1); + + // Use HFROSC + use_hfrosc(4, 16); +} + +static unsigned long __attribute__((noinline)) measure_cpu_freq(size_t n) +{ + unsigned long start_mtime, delta_mtime; + unsigned long mtime_freq = get_timer_freq(); + + // Don't start measuruing until we see an mtime tick + unsigned long tmp = mtime_lo(); + do { + start_mtime = mtime_lo(); + } while (start_mtime == tmp); + + unsigned long start_mcycle = read_csr(mcycle); + + do { + delta_mtime = mtime_lo() - start_mtime; + } while (delta_mtime < n); + + unsigned long delta_mcycle = read_csr(mcycle) - start_mcycle; + + return (delta_mcycle / delta_mtime) * mtime_freq + + ((delta_mcycle % delta_mtime) * mtime_freq) / delta_mtime; +} + +unsigned long get_cpu_freq() +{ + static uint32_t cpu_freq; + + if (!cpu_freq) { + // warm up I$ + measure_cpu_freq(1); + // measure for real + cpu_freq = measure_cpu_freq(10); + } + + return cpu_freq; +} + +static void uart_init(size_t baud_rate) +{ + GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK; + GPIO_REG(GPIO_IOF_EN) |= IOF0_UART0_MASK; + UART0_REG(UART_REG_DIV) = get_cpu_freq() / baud_rate - 1; + UART0_REG(UART_REG_TXCTRL) |= UART_TXEN; +} + + + +#ifdef USE_PLIC +extern void handle_m_ext_interrupt(); +#endif + +#ifdef USE_M_TIME +extern void handle_m_time_interrupt(); +#endif + +uintptr_t handle_trap(uintptr_t mcause, uintptr_t epc) +{ + if (0){ +#ifdef USE_PLIC + // External Machine-Level interrupt from PLIC + } else if ((mcause & MCAUSE_INT) && ((mcause & MCAUSE_CAUSE) == IRQ_M_EXT)) { + handle_m_ext_interrupt(); +#endif +#ifdef USE_M_TIME + // External Machine-Level interrupt from PLIC + } else if ((mcause & MCAUSE_INT) && ((mcause & MCAUSE_CAUSE) == IRQ_M_TIMER)){ + handle_m_time_interrupt(); +#endif + } + else { + write(1, "trap\n", 5); + _exit(1 + mcause); + } + return epc; +} + +void _init() +{ + + #ifndef NO_INIT + use_default_clocks(); + use_pll(0, 0, 1, 31, 1); + uart_init(115200); + + printf("core freq at %d Hz\n", get_cpu_freq()); + + write_csr(mtvec, &trap_entry); + if (read_csr(misa) & (1 << ('F' - 'A'))) { // if F extension is present + write_csr(mstatus, MSTATUS_FS); // allow FPU instructions without trapping + write_csr(fcsr, 0); // initialize rounding mode, undefined at reset + } + #endif + +} + +void _fini() +{ +} -- cgit v1.3.1 From 3c51848d33d2c13d0029f3e796af9f78a882eac3 Mon Sep 17 00:00:00 2001 From: zhangjun <2281979437@qq.com> Date: Sat, 29 Jul 2017 15:37:20 +0800 Subject: fix trap_entry --- bsp/risc-v/Makefile | 6 ++-- bsp/risc-v/applications/applications.c | 10 +++++- bsp/risc-v/applications/startup.c | 2 +- bsp/risc-v/drivers/board.c | 5 +-- bsp/risc-v/drivers/usart.c | 15 ++++++-- bsp/risc-v/rtconfig.h | 2 ++ libcpu/risc-v/e310/context_gcc.S | 66 +++++++++++++++++----------------- libcpu/risc-v/e310/init.c | 10 +----- libcpu/risc-v/e310/start_gcc.S | 26 ++++++-------- 9 files changed, 74 insertions(+), 68 deletions(-) (limited to 'libcpu/risc-v/e310/init.c') diff --git a/bsp/risc-v/Makefile b/bsp/risc-v/Makefile index 1fa59e1f53..b7def9d3f3 100644 --- a/bsp/risc-v/Makefile +++ b/bsp/risc-v/Makefile @@ -105,20 +105,18 @@ DEBUG_INTERFACE = jlink #DEBUG_INTERFACE = ftdi/openjtag GDB_CMD = -ex "tar ext 127.0.0.1:3333" -GDB_CMD += -ex "monitor reset halt" +#GDB_CMD += -ex "monitor reset halt" #GDB_CMD += -ex "monitor step 0x20400000" GDB_CMD += --command=.gdbinit run: setsid ${OPENOCD} > /dev/null 2>&1 & -# (sleep 1 && echo -e "halt" && sleep 1) | telnet 127.0.0.1 4444 ${GDB} ${TARGET}.axf ${GDB_CMD} -# arm-none-eabi-gdb ${TARGET} -ex "tar ext 127.0.0.1:3333" -ex "b main" -ex "lay n" -ex "lay n" -ex "lay n" killall -9 openocd programe: setsid ${OPENOCD} > /dev/null 2>&1 & - ${GDB} ${TARGET}.axf -ex "tar ext 127.0.0.1:3333" -ex "load ${TARGET}.axf" + ${GDB} ${TARGET}.axf -ex "tar ext 127.0.0.1:3333" -ex "monitor step 0x20400000" -ex "load ${TARGET}.axf" killall -9 openocd clean: diff --git a/bsp/risc-v/applications/applications.c b/bsp/risc-v/applications/applications.c index c595c1c2b7..b8adfe53eb 100644 --- a/bsp/risc-v/applications/applications.c +++ b/bsp/risc-v/applications/applications.c @@ -8,12 +8,20 @@ static void rt_init_thread_entry(void* parameter) #endif rt_thread_delay( RT_TIMER_TICK_PER_SECOND*2 ); /* sleep 0.5 second and switch to other thread */ } +#include +#include static void led_thread_entry(void* parameter) { unsigned int count=0; rt_hw_led_init(); + rt_hw_interrupt_enable(0x888); + set_csr(mstatus, MSTATUS_MIE); + asm( + "csrr a5, mie" + ); + rt_kprintf("core freq at %d Hz\n", get_cpu_freq()); while (1) { /* led1 on */ @@ -21,8 +29,8 @@ static void led_thread_entry(void* parameter) /* rt_kprintf("led on, count : %d\r\n",count);*/ #endif count++; - rt_hw_led_on(0); rt_thread_delay( RT_TIMER_TICK_PER_SECOND*2 ); /* sleep 0.5 second and switch to other thread */ + rt_hw_led_on(0); /* led1 off */ #ifndef RT_USING_FINSH diff --git a/bsp/risc-v/applications/startup.c b/bsp/risc-v/applications/startup.c index 4f4c6c4806..59bf44c83b 100644 --- a/bsp/risc-v/applications/startup.c +++ b/bsp/risc-v/applications/startup.c @@ -29,8 +29,8 @@ static void rtthread_startup(void) /* initialize idle thread */ rt_thread_idle_init(); - while(1); /* start scheduler */ + while(1); rt_system_scheduler_start(); /* never reach here */ diff --git a/bsp/risc-v/drivers/board.c b/bsp/risc-v/drivers/board.c index 8452af75ed..60a4de3baa 100644 --- a/bsp/risc-v/drivers/board.c +++ b/bsp/risc-v/drivers/board.c @@ -27,11 +27,12 @@ static void rt_hw_timer_init(void) GPIO_REG(GPIO_OUTPUT_VAL) |= (0x1 << BLUE_LED_OFFSET) ; GPIO_REG(GPIO_OUTPUT_VAL) &= ~((0x1<< RED_LED_OFFSET) | (0x1<< GREEN_LED_OFFSET)) ; rt_hw_interrupt_enable(1); -/* set_csr(mie, MIP_MTIP);*/ CLINT_REG(CLINT_MTIME) = 0x0; CLINT_REG(CLINT_MTIMECMP) = 0x10000; + volatile uint64_t * mtimecmp = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIMECMP); set_csr(mstatus, MSTATUS_MIE); + *mtimecmp = 0x20000; return; } void rt_hw_board_init(void) @@ -43,7 +44,7 @@ void rt_hw_board_init(void) /* initialize the system clock */ //rt_hw_clock_init(); //set each pll etc. - + /* initialize uart */ rt_hw_uart_init(); rt_console_set_device(RT_CONSOLE_DEVICE_NAME); diff --git a/bsp/risc-v/drivers/usart.c b/bsp/risc-v/drivers/usart.c index 975815e00b..6b540c814a 100644 --- a/bsp/risc-v/drivers/usart.c +++ b/bsp/risc-v/drivers/usart.c @@ -1,13 +1,18 @@ #include #include "usart.h" +#include +#include /** * @brief set uartdbg buard * * @param buard */ -static usart_init(int buard) +static void usart_init(int buard) { - return; + GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK; + GPIO_REG(GPIO_IOF_EN) |= IOF0_UART0_MASK; + UART0_REG(UART_REG_DIV) = get_cpu_freq() / buard - 1; + UART0_REG(UART_REG_TXCTRL) |= UART_TXEN; } static void usart_handler(int vector, void *param) { @@ -17,6 +22,10 @@ static void usart_handler(int vector, void *param) static rt_err_t usart_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { + GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK; + GPIO_REG(GPIO_IOF_EN) |= IOF0_UART0_MASK; + UART0_REG(UART_REG_DIV) = get_cpu_freq() / 115200 - 1; + UART0_REG(UART_REG_TXCTRL) |= UART_TXEN; return RT_EOK; } static rt_err_t usart_control(struct rt_serial_device *serial, @@ -33,6 +42,8 @@ static rt_err_t usart_control(struct rt_serial_device *serial, } static int usart_putc(struct rt_serial_device *serial, char c) { + while (UART0_REG(UART_REG_TXFIFO) & 0x80000000) ; + UART0_REG(UART_REG_TXFIFO) = c; return 0; } static int usart_getc(struct rt_serial_device *serial) diff --git a/bsp/risc-v/rtconfig.h b/bsp/risc-v/rtconfig.h index 8a87537617..aa5669f736 100644 --- a/bsp/risc-v/rtconfig.h +++ b/bsp/risc-v/rtconfig.h @@ -258,4 +258,6 @@ */ //#define RT_USING_CPU_FFS #define RT_USING_COMPONENTS_INIT + +#define IDLE_THREAD_STACK_SIZE 512 #endif diff --git a/libcpu/risc-v/e310/context_gcc.S b/libcpu/risc-v/e310/context_gcc.S index 57a929eb09..f92c80748d 100644 --- a/libcpu/risc-v/e310/context_gcc.S +++ b/libcpu/risc-v/e310/context_gcc.S @@ -32,6 +32,7 @@ rt_hw_interrupt_disable: addi sp, sp, -12 sw a5, (sp) + csrr a0, mie li a5, MIP_MEIP|MIP_MTIP|MIP_MSIP csrrc a5, mie, a5 /* csrrc a5, mstatus, MSTATUS_MIE*/ @@ -60,18 +61,18 @@ rt_hw_interrupt_enable: */ .globl rt_hw_context_switch rt_hw_context_switch: - addi sp, sp, -32*REGBYTES + addi sp, sp, -32*REGBYTES - STORE sp, (a0) + STORE sp, (a0) STORE x30, 1*REGBYTES(sp) STORE x31, 2*REGBYTES(sp) - STORE x3, 3*REGBYTES(sp) - STORE x4, 4*REGBYTES(sp) - STORE x5, 5*REGBYTES(sp) - STORE x6, 6*REGBYTES(sp) - STORE x7, 7*REGBYTES(sp) - STORE x8, 8*REGBYTES(sp) - STORE x9, 9*REGBYTES(sp) + STORE x3, 3*REGBYTES(sp) + STORE x4, 4*REGBYTES(sp) + STORE x5, 5*REGBYTES(sp) + STORE x6, 6*REGBYTES(sp) + STORE x7, 7*REGBYTES(sp) + STORE x8, 8*REGBYTES(sp) + STORE x9, 9*REGBYTES(sp) STORE x10, 10*REGBYTES(sp) STORE x11, 11*REGBYTES(sp) STORE x12, 12*REGBYTES(sp) @@ -91,17 +92,15 @@ rt_hw_context_switch: STORE x26, 26*REGBYTES(sp) STORE x27, 27*REGBYTES(sp) STORE x28, 28*REGBYTES(sp) - STORE x1, 31*REGBYTES(sp) + STORE x1, 31*REGBYTES(sp) STORE x10, 29*REGBYTES(sp) - STORE x1, 30*REGBYTES(sp) - - csrr a0, mcause + STORE x1, 30*REGBYTES(sp) # Remain in M-mode after mret li t0, MSTATUS_MPP csrs mstatus, t0 - LOAD sp, (a1) + LOAD sp, (a1) LOAD x30, 1*REGBYTES(sp) LOAD x31, 2*REGBYTES(sp) LOAD x3, 3*REGBYTES(sp) @@ -131,12 +130,11 @@ rt_hw_context_switch: LOAD x27, 27*REGBYTES(sp) LOAD x28, 28*REGBYTES(sp) LOAD x10, 31*REGBYTES(sp) - csrw mepc, a0 + csrw mepc,a0 LOAD x10, 29*REGBYTES(sp) LOAD x1, 30*REGBYTES(sp) - - addi sp, sp, 32*REGBYTES + addi sp, sp, 32*REGBYTES mret /* @@ -145,7 +143,7 @@ rt_hw_context_switch: */ .globl rt_hw_context_switch_to rt_hw_context_switch_to: - LOAD sp, (a0) + LOAD sp, (a0) LOAD x30, 1*REGBYTES(sp) LOAD x31, 2*REGBYTES(sp) LOAD x3, 3*REGBYTES(sp) @@ -175,12 +173,12 @@ rt_hw_context_switch_to: LOAD x27, 27*REGBYTES(sp) LOAD x28, 28*REGBYTES(sp) LOAD x10, 31*REGBYTES(sp) - csrw mepc, a0 + csrw mepc,a0 LOAD x10, 29*REGBYTES(sp) LOAD x1, 30*REGBYTES(sp) - addi sp, sp, 32*REGBYTES + addi sp, sp, 32*REGBYTES mret @@ -193,21 +191,21 @@ rt_hw_context_switch_to: .globl rt_hw_context_switch_interrupt rt_hw_context_switch_interrupt: addi sp, sp, -16 - sw s0, 12(sp) - sw a0, 8(sp) - sw a5, 4(sp) - la a0, rt_thread_switch_interrupt_flag + sw s0, 12(sp) + sw a0, 8(sp) + sw a5, 4(sp) + la a0, rt_thread_switch_interrupt_flag beqz a5, _reswitch - li a5, 1 - sw a5, (a0) - la a5, rt_interrupt_from_thread - lw a0, 8(sp) - sw a0, (a5) + li a5, 1 + sw a5, (a0) + la a5, rt_interrupt_from_thread + lw a0, 8(sp) + sw a0, (a5) _reswitch: - la a5, rt_interrupt_to_thread - sw a1, (a5) - lw a5, 4(sp) - lw a0, 8(sp) - lw s0, 12(sp) + la a5, rt_interrupt_to_thread + sw a1, (a5) + lw a5, 4(sp) + lw a0, 8(sp) + lw s0, 12(sp) addi sp, sp, 16 ret diff --git a/libcpu/risc-v/e310/init.c b/libcpu/risc-v/e310/init.c index 621a6e2dd6..3a2b39614c 100644 --- a/libcpu/risc-v/e310/init.c +++ b/libcpu/risc-v/e310/init.c @@ -175,13 +175,7 @@ unsigned long get_cpu_freq() return cpu_freq; } -static void uart_init(size_t baud_rate) -{ - GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK; - GPIO_REG(GPIO_IOF_EN) |= IOF0_UART0_MASK; - UART0_REG(UART_REG_DIV) = get_cpu_freq() / baud_rate - 1; - UART0_REG(UART_REG_TXCTRL) |= UART_TXEN; -} + @@ -220,9 +214,7 @@ void _init() #ifndef NO_INIT use_default_clocks(); use_pll(0, 0, 1, 31, 1); - uart_init(115200); - printf("core freq at %d Hz\n", get_cpu_freq()); write_csr(mtvec, &trap_entry); if (read_csr(misa) & (1 << ('F' - 'A'))) { // if F extension is present diff --git a/libcpu/risc-v/e310/start_gcc.S b/libcpu/risc-v/e310/start_gcc.S index e421cd5bd6..d05ef7b8b2 100644 --- a/libcpu/risc-v/e310/start_gcc.S +++ b/libcpu/risc-v/e310/start_gcc.S @@ -17,8 +17,7 @@ _start: la gp, __global_pointer$ .option pop la sp, _sp -/*disable all interrupt*/ - csrw mie, 0 + csrrsi a5, mstatus, 0 #if defined(ENABLE_SMP) smp_pause(t0, t1) @@ -124,13 +123,13 @@ trap_entry: STORE x30, 1*REGBYTES(sp) STORE x31, 2*REGBYTES(sp) - STORE x3, 3*REGBYTES(sp) - STORE x4, 4*REGBYTES(sp) - STORE x5, 5*REGBYTES(sp) - STORE x6, 6*REGBYTES(sp) - STORE x7, 7*REGBYTES(sp) - STORE x8, 8*REGBYTES(sp) - STORE x9, 9*REGBYTES(sp) + STORE x3, 3*REGBYTES(sp) + STORE x4, 4*REGBYTES(sp) + STORE x5, 5*REGBYTES(sp) + STORE x6, 6*REGBYTES(sp) + STORE x7, 7*REGBYTES(sp) + STORE x8, 8*REGBYTES(sp) + STORE x9, 9*REGBYTES(sp) STORE x10, 10*REGBYTES(sp) STORE x11, 11*REGBYTES(sp) STORE x12, 12*REGBYTES(sp) @@ -150,13 +149,11 @@ trap_entry: STORE x26, 26*REGBYTES(sp) STORE x27, 27*REGBYTES(sp) STORE x28, 28*REGBYTES(sp) - STORE x1, 31*REGBYTES(sp) STORE x10, 29*REGBYTES(sp) - STORE x1, 30*REGBYTES(sp) + STORE x1, 30*REGBYTES(sp) + csrr x1, mepc + STORE x1, 31*REGBYTES(sp) - csrr a0, mcause - csrr a1, mepc - csrw mepc, a0 call rt_interrupt_enter call rt_hw_trap_irq @@ -167,7 +164,6 @@ trap_entry: lw a1, (a0) beqz a1, rt_hw_context_switch_interrupt_do - csrw mepc, a0 # Remain in M-mode after mret li t0, MSTATUS_MPP csrs mstatus, t0 -- cgit v1.3.1 From b032dff1616fe64a4dfec63e593f0fac634ef3ab Mon Sep 17 00:00:00 2001 From: zhangjun <2281979437@qq.com> Date: Sun, 30 Jul 2017 19:46:28 +0800 Subject: fix bug in rt_hw_context_switch_interrupt_do save sp to old thread clear rt_thread_switch_interrupt_flag always enable interrupt after rt_hw_context_switch judeg the type of interrupt in trap_entry, then call handler(Machine timer interrupt of Machine external interrupt) --- bsp/risc-v/applications/applications.c | 9 +- bsp/risc-v/drivers/board.c | 14 ++- bsp/risc-v/drivers/led.c | 157 ++++++++++++++++----------------- libcpu/risc-v/e310/context_gcc.S | 27 ++++-- libcpu/risc-v/e310/init.c | 20 ----- libcpu/risc-v/e310/start_gcc.S | 37 ++++++-- 6 files changed, 134 insertions(+), 130 deletions(-) (limited to 'libcpu/risc-v/e310/init.c') diff --git a/bsp/risc-v/applications/applications.c b/bsp/risc-v/applications/applications.c index c26b4903d8..bba2b58053 100644 --- a/bsp/risc-v/applications/applications.c +++ b/bsp/risc-v/applications/applications.c @@ -15,17 +15,16 @@ static void led_thread_entry(void* parameter) unsigned int count=0; rt_hw_led_init(); - rt_kprintf("core freq at %d Hz\n", get_cpu_freq()); while (1) { /* led1 on */ #ifndef RT_USING_FINSH /* rt_kprintf("led on, count : %d\r\n",count);*/ #endif + rt_kprintf("core freq at %d Hz\n", get_cpu_freq()); count++; rt_thread_delay( RT_TIMER_TICK_PER_SECOND/2 ); /* sleep 0.5 second and switch to other thread */ - while(1); - rt_hw_led_on(0); +/* rt_hw_led_on(0);*/ /* led1 off */ #ifndef RT_USING_FINSH @@ -33,10 +32,10 @@ static void led_thread_entry(void* parameter) #endif rt_hw_led_off(0); - rt_thread_delay( RT_TIMER_TICK_PER_SECOND*2); +/* rt_thread_delay( RT_TIMER_TICK_PER_SECOND*2);*/ } } -static rt_uint8_t led_stack[ 2048 ]; +static rt_uint8_t led_stack[ 1024]; static struct rt_thread led_thread; void rt_application_init() { diff --git a/bsp/risc-v/drivers/board.c b/bsp/risc-v/drivers/board.c index da324a83b1..3915d4bac3 100644 --- a/bsp/risc-v/drivers/board.c +++ b/bsp/risc-v/drivers/board.c @@ -14,11 +14,6 @@ static struct mem_desc hw_mem_desc[] = }; #endif -static void rt_systick_handler(int vector, void *param) -{ - rt_tick_increase(); - return; -} #include static void rt_hw_timer_init(void) { @@ -26,11 +21,12 @@ static void rt_hw_timer_init(void) GPIO_REG(GPIO_OUTPUT_EN) |= ((0x1<< RED_LED_OFFSET)| (0x1<< GREEN_LED_OFFSET) | (0x1 << BLUE_LED_OFFSET)) ; GPIO_REG(GPIO_OUTPUT_VAL) |= (0x1 << BLUE_LED_OFFSET) ; GPIO_REG(GPIO_OUTPUT_VAL) &= ~((0x1<< RED_LED_OFFSET) | (0x1<< GREEN_LED_OFFSET)) ; - rt_hw_interrupt_enable(1); - - CLINT_REG(CLINT_MTIME) = 0x0; - CLINT_REG(CLINT_MTIMECMP) = 0x10000; + +/* enable timer interrupt*/ + set_csr(mie, MIP_MTIP); volatile uint64_t * mtimecmp = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIMECMP); + CLINT_REG(CLINT_MTIME) = 0x0; +/* CLINT_REG(CLINT_MTIMECMP) = 0x10000;*/ *mtimecmp = 0x20000; return; } diff --git a/bsp/risc-v/drivers/led.c b/bsp/risc-v/drivers/led.c index 900bb40f7d..6aa3ddcd4b 100644 --- a/bsp/risc-v/drivers/led.c +++ b/bsp/risc-v/drivers/led.c @@ -75,87 +75,82 @@ void rt_hw_led_on(int led) { /* - // Make sure the HFROSC is on before the next line: - PRCI_REG(PRCI_HFROSCCFG) |= ROSC_EN(1); - // Run off 16 MHz Crystal for accuracy. Note that the - // first line is - PRCI_REG(PRCI_PLLCFG) = (PLL_REFSEL(1) | PLL_BYPASS(1)); - PRCI_REG(PRCI_PLLCFG) |= (PLL_SEL(1)); - // Turn off HFROSC to save power - PRCI_REG(PRCI_HFROSCCFG) &= ~(ROSC_EN(1)); - */ - - - // Wait a bit to avoid corruption on the UART. - // (In some cases, switching to the IOF can lead - // to output glitches, so need to let the UART - // reciever time out and resynchronize to the real - // start of the stream. - volatile int i=0; - while(i < 10000){i++;} - - _puts(sifive_msg); - //_puts("Config String:\n\r"); - //_puts(*((const char **) 0x100C)); - //_puts("\n\r"); - _puts(led_msg); - uint16_t r=0xFF; - uint16_t g=0; - uint16_t b=0; - char c = 0; - - // Set up RGB PWM - - /* - PWM1_REG(PWM_CFG) = 0; - // To balance the power consumption, make one left, one right, and one center aligned. - PWM1_REG(PWM_CFG) = (PWM_CFG_ENALWAYS) | (PWM_CFG_CMP2CENTER); - PWM1_REG(PWM_COUNT) = 0; - - // Period is approximately 244 Hz - // the LEDs are intentionally left somewhat dim, - // as the full brightness can be painful to look at. - PWM1_REG(PWM_CMP0) = 0; - */ - - - while(1){ - - if(r > 0 && b == 0){ - r--; - g++; - } - if(g > 0 && r == 0){ - g--; - b++; - } - if(b > 0 && g == 0){ - r++; - b--; - } - - uint32_t G = g; - uint32_t R = r; - uint32_t B = b; - - PWM1_REG(PWM_CMP1) = G << 4; // PWM is low on the left, GPIO is low on the left side, LED is ON on the left. - PWM1_REG(PWM_CMP2) = (B << 1) << 4; // PWM is high on the middle, GPIO is low in the middle, LED is ON in the middle. - PWM1_REG(PWM_CMP3) = 0xFFFF - (R << 4); // PWM is low on the left, GPIO is low on the right, LED is on on the right. - - // Check for user input - if (c == 0){ - if (_getc(&c) != 0){ - _putc(c); - _puts("\n\r"); - - if ((c == 'y') || (c == 'Y')){ - _puts("PASS\n\r"); - } else{ - _puts("FAIL\n\r"); - } - } - } - } + // Make sure the HFROSC is on before the next line: + PRCI_REG(PRCI_HFROSCCFG) |= ROSC_EN(1); + // Run off 16 MHz Crystal for accuracy. Note that the + // first line is + PRCI_REG(PRCI_PLLCFG) = (PLL_REFSEL(1) | PLL_BYPASS(1)); + PRCI_REG(PRCI_PLLCFG) |= (PLL_SEL(1)); + // Turn off HFROSC to save power + PRCI_REG(PRCI_HFROSCCFG) &= ~(ROSC_EN(1)); + */ + + + // Wait a bit to avoid corruption on the UART. + // (In some cases, switching to the IOF can lead + // to output glitches, so need to let the UART + // reciever time out and resynchronize to the real + // start of the stream. + volatile int i=0; + while(i < 10000){i++;} + + _puts(sifive_msg); + //_puts("Config String:\n\r"); + //_puts(*((const char **) 0x100C)); + //_puts("\n\r"); + _puts(led_msg); + uint16_t r=0xFF; + uint16_t g=0; + uint16_t b=0; + char c = 0; + + // Set up RGB PWM + + /* + PWM1_REG(PWM_CFG) = 0; + // To balance the power consumption, make one left, one right, and one center aligned. + PWM1_REG(PWM_CFG) = (PWM_CFG_ENALWAYS) | (PWM_CFG_CMP2CENTER); + PWM1_REG(PWM_COUNT) = 0; + + // Period is approximately 244 Hz + // the LEDs are intentionally left somewhat dim, + // as the full brightness can be painful to look at. + PWM1_REG(PWM_CMP0) = 0; + */ + if(r > 0 && b == 0){ + r--; + g++; + } + if(g > 0 && r == 0){ + g--; + b++; + } + if(b > 0 && g == 0){ + r++; + b--; + } + + uint32_t G = g; + uint32_t R = r; + uint32_t B = b; + + PWM1_REG(PWM_CMP1) = G << 4; // PWM is low on the left, GPIO is low on the left side, LED is ON on the left. + PWM1_REG(PWM_CMP2) = (B << 1) << 4; // PWM is high on the middle, GPIO is low in the middle, LED is ON in the middle. + PWM1_REG(PWM_CMP3) = 0xFFFF - (R << 4); // PWM is low on the left, GPIO is low on the right, LED is on on the right. + + // Check for user input + if (c == 0){ + if (_getc(&c) != 0){ + _putc(c); + _puts("\n\r"); + + if ((c == 'y') || (c == 'Y')){ + _puts("PASS\n\r"); + } else{ + _puts("FAIL\n\r"); + } + } + } return; } void rt_hw_led_off(int led) diff --git a/libcpu/risc-v/e310/context_gcc.S b/libcpu/risc-v/e310/context_gcc.S index ea2e205e4b..eee3cc128d 100644 --- a/libcpu/risc-v/e310/context_gcc.S +++ b/libcpu/risc-v/e310/context_gcc.S @@ -32,9 +32,21 @@ rt_hw_interrupt_disable: addi sp, sp, -12 sw a5, (sp) + li a5, 0x800 csrr a0, mie - li a5, MIP_MEIP|MIP_MTIP|MIP_MSIP -/* csrrc a5, mstatus, MSTATUS_MIE*/ + blt a0, a5, 1f +/* interrupt is enable before disable it*/ + addi a0, a0, 1 + + li a5, 0x1 + addi a5, a5, -2048 + csrrc a5, mie, a5 +/* csrrc a5, mie, 128*/ + j 2f +/* interrupt is disabled before disable it*/ +1: + li a0, 0 +2: lw a5, (sp) addi sp, sp, 12 ret @@ -46,9 +58,12 @@ rt_hw_interrupt_disable: rt_hw_interrupt_enable: addi sp, sp, -12 sw a5, (sp) - li a5, MIP_MEIP|MIP_MTIP|MIP_MSIP + beqz a0, 1f + li a5, 0x1 + addi a5, a5, -2048 csrrs a5, mie, a5 -/* csrrsi a5, mstatus, MSTATUS_MIE*/ +/* csrrs a5, mie, 128*/ +1: lw a5, (sp) addi sp, sp, 12 ret @@ -98,8 +113,8 @@ rt_hw_context_switch: *Remain in M-mode after mret *enable interrupt in M-mode */ - li t0, MSTATUS_MPIE|MSTATUS_MPP - csrs mstatus, t0 + li t0, 136 + csrrs t0, mstatus, t0 LOAD sp, (a1) LOAD x30, 1*REGBYTES(sp) diff --git a/libcpu/risc-v/e310/init.c b/libcpu/risc-v/e310/init.c index 3a2b39614c..a8db529804 100644 --- a/libcpu/risc-v/e310/init.c +++ b/libcpu/risc-v/e310/init.c @@ -187,26 +187,6 @@ extern void handle_m_ext_interrupt(); extern void handle_m_time_interrupt(); #endif -uintptr_t handle_trap(uintptr_t mcause, uintptr_t epc) -{ - if (0){ -#ifdef USE_PLIC - // External Machine-Level interrupt from PLIC - } else if ((mcause & MCAUSE_INT) && ((mcause & MCAUSE_CAUSE) == IRQ_M_EXT)) { - handle_m_ext_interrupt(); -#endif -#ifdef USE_M_TIME - // External Machine-Level interrupt from PLIC - } else if ((mcause & MCAUSE_INT) && ((mcause & MCAUSE_CAUSE) == IRQ_M_TIMER)){ - handle_m_time_interrupt(); -#endif - } - else { - write(1, "trap\n", 5); - _exit(1 + mcause); - } - return epc; -} void _init() { diff --git a/libcpu/risc-v/e310/start_gcc.S b/libcpu/risc-v/e310/start_gcc.S index b986f3f5e8..e7aaddcae9 100644 --- a/libcpu/risc-v/e310/start_gcc.S +++ b/libcpu/risc-v/e310/start_gcc.S @@ -36,7 +36,10 @@ _start: la gp, __global_pointer$ .option pop la sp, _sp - csrrsi a5, mstatus, 0 +/* + *disable all interrupt at startup + */ + csrrc a5, mstatus, 0xb #if defined(ENABLE_SMP) smp_pause(t0, t1) @@ -69,6 +72,7 @@ _start: la a0, __libc_fini_array call atexit call __libc_init_array + /*call _init directly in rt-thread*/ call _init #ifndef __riscv_float_abi_soft @@ -175,17 +179,31 @@ trap_entry: call rt_interrupt_enter + csrr a0, mcause + lui a5, 0x80000 + not a5, a5 + and a5, a5, a0 + li a4, 11 + mv s1, a1 + /*Machine external interrupt*/ + bne a5, a4, 1f call rt_hw_trap_irq - call handle_m_time_interrupt +1: + /*Machine software interrupt*/ + li a4, 7 + bne a5, a4, 2f + call rt_systick_handler +2: call rt_interrupt_leave - la a0, rt_thread_switch_interrupt_flag - lw a1, (a0) + # Remain in M-mode after mret + li t0, 136 + csrrs t0, mstatus, t0 + + la a0, rt_thread_switch_interrupt_flag + lw a1, (a0) bnez a1, rt_hw_context_switch_interrupt_do - # Remain in M-mode after mret - li t0, MSTATUS_MPP - csrs mstatus, t0 LOAD x30, 1*REGBYTES(sp) LOAD x31, 2*REGBYTES(sp) @@ -225,12 +243,13 @@ trap_entry: mret rt_hw_context_switch_interrupt_do: - + /*clear rt_thread_switch_interrupt_flag*/ la a0, rt_thread_switch_interrupt_flag - lw a5, (a0) li a5, 0 sw a5, (a0) + LOAD a0, rt_interrupt_from_thread + STORE sp, (a0) LOAD a0, rt_interrupt_to_thread LOAD sp, (a0) LOAD x30, 1*REGBYTES(sp) -- cgit v1.3.1