MCU crashes randomly

Hi everyone. I’m using Freedom Studio (on windows) to program FE310-G002 on LoFive R1 using a J-Link - JTAG debugger.

I’m running an algorithm on the MCU and it crashes randomly. It can complete one loop of the algorithm when all the variables are float, then crashed. When I change all variables to double, it crashed right away in the first loop, and stoped at early_trap_vector in trap.S

I used Segger Embedded Studio to compile the program and it takes only 6kB/16KB RAM in the MCU and a few KB of SPI Flash. However, I can’t find a way to see this information in Freedom Studio build results.

The algorithm has been tested with ARM-core STM32 and it works very well without any problem. So I think there is no problem with the code but there is something wrong with the RISC-V MCU.

Has anyone here ever encountered this problem? Please let me know your thinking on this.

Thank you!


Any chance you can provide a little more information on this “algorithm”? Also, it sounds like you are compiling your code with one IDE (Segger) and then programming/debugging with a different one (FreedomStudio) - is this accurate?

It might be worthwhile to provide your build options and show the contents of the mcause, mepc, and mtval CSRs and this will pinpoint the source of your problem. Have you tried single-stepping your program to understand which instruction is causing the exception?

Thanks David. I’m running extended kalman filter. No, I compiled the code sepatately by both Segger IDE and FreedomStudio.

In FreedomStudio, I tried single-stepping as you said but the MCU crashed at different functions when I change all variables in my program from float to double.

My build tool chain is SiFive RISC-V GNU GCC Newlib, it think it’s RISCV64 GNU ELF tooth chain. Every functions worked fine when I test them separately. I set MCU clock at 256MHz and it can read data from IMU MUP9250 correctly. Calculations for float variables are precise and eveything seems fine.

These are the registor values before crashing:
2021-07-23 10_06_45-wsFreedomStudio - C-EKF-FE310_src_main.c - FreedomStudio

And these are after crashing:
2021-07-23 10_07_27-wsFreedomStudio - C-EKF-FE310_src_main.c - FreedomStudio

Update: it worked well on Segger Embedded Studio, I can see the output data from the algorithm continuously through global live watch, and the MCU doesn’t crash. But I can’t find any library to work with the MCU peripherals such as UART and I2C. Does anyone know a library similar to Freedom Metal but for Segger IDE?