# MCU crashes randomly

**URL:** <https://forums.sifive.com/t/mcu-crashes-randomly/4992>\
**Category:** HiFive1 Rev B\
**Created:** [July 21, 2021, 8:04am UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992 "2021-07-21T08:04:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hienvu](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@hienvu](https://forums.sifive.com/u/hienvu)\
**Post date:** [July 21, 2021, 8:04am UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/1 "2021-07-21T08:04:18Z")

</div>

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

 ![2021-07-21 17_11_15-wsFreedomStudio - C-EKF-HiFive1_freedom-metal_src_trap.S - FreedomStudio](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/7/7cf38d15884d4649f819207db081105d67cb1b4c.jpeg)

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!

---

<div class="post-metadata">

**Author:** ![dconn](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dconn](https://forums.sifive.com/u/dconn)\
**Post date:** [July 22, 2021, 8:37pm UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/2 "2021-07-22T20:37:38Z")

</div>

Hi,

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?

---

<div class="post-metadata">

**Author:** ![hienvu](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@hienvu](https://forums.sifive.com/u/hienvu)\
**Post date:** [July 23, 2021, 1:24am UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/3 "2021-07-23T01:24:27Z")

</div>

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](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/4/49eb1f93b2a31d0fce7847b8fde85c30f075617d.jpeg)

And these are after crashing:  
 ![2021-07-23 10_07_27-wsFreedomStudio - C-EKF-FE310_src_main.c - FreedomStudio](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/c/c7c22dfe7868b630ec58b300356856f29562ad97.jpeg)

---

<div class="post-metadata">

**Author:** ![hienvu](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@hienvu](https://forums.sifive.com/u/hienvu)\
**Post date:** [July 23, 2021, 2:55am UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/4 "2021-07-23T02:55:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dconn](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dconn](https://forums.sifive.com/u/dconn)\
**Post date:** [July 26, 2021, 8:47pm UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/5 "2021-07-26T20:47:34Z")

</div>

It’s often difficult to debug a random crash but you may want to consider not touching the PLL as a first step, and using a simple example and building from there. There is a clue here that different instructions at different times cause the CPU to crash. Are you using printf() anywhere in your code?

---

<div class="post-metadata">

**Author:** ![hienvu](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@hienvu](https://forums.sifive.com/u/hienvu)\
**Post date:** [July 27, 2021, 4:59am UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/6 "2021-07-27T04:59:32Z")

</div>

Thanks dconn, I did not encounter this problem when using Segger IDE with a correct package. When I dig into their library, in their core initialization, I found this function:

 ![2021-07-27 13_56_24-EFK_HiFive1 - SEGGER Embedded Studio for RISC-V V5.50b (64-bit) - Non-Commercial](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/e/ef7a184d7a5d88a0a1ff3109d41954267a91f7ff.jpeg)

I think this is the reason why it worked on Segger IDE. So setting mstatus register can help solving this problem. But I can’t find any way to set it up in Freedom Studio.

---

<div class="post-metadata">

**Author:** ![dconn](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dconn](https://forums.sifive.com/u/dconn)\
**Post date:** [July 27, 2021, 2:10pm UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/7 "2021-07-27T14:10:38Z")

</div>

If you disable ‘Start target execution’ in the Freedom Studio Debug configuration (Startup tab) you can single step all the entry code, and you should see in freedom-metal/gloss/crt0.S a similar init routine:

```auto
...
  /* Check RISC-V isa and enable FS bits if Floating Point architecture. */
  li a4, 0x10028
  and a5, a5, a4
  beqz a5, 1f
  csrr a5, mstatus
  lui a4, 0x2
  or a5, a5, a4
  csrw mstatus, a5
  csrwi fcsr, 0
1:
...

```

Keep in mind that `freedom-metal/src/entry.S` holds the entry point, and `_start` label within crt0.S is called from here.

---

<div class="post-metadata">

**Author:** ![hienvu](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@hienvu](https://forums.sifive.com/u/hienvu)\
**Post date:** [July 28, 2021, 11:18am UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/8 "2021-07-28T11:18:49Z")

</div>

Thank you. I tried to run as you suggested, but it didn’t stop there.  
Besides, it was placed in “\_skip\_init”. In the beginning of the file, I saw the following code and I don’t really understand what that means:

```auto
  /* If we're not hart 0, skip the initialization work */
  la t0, __metal_boot_hart
  bne a0, t0, _skip_init

```

Please give me some insight on this if possible.

---

<div class="post-metadata">

**Author:** ![Disasm](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/disasm/32/690_2.png) [@Disasm](https://forums.sifive.com/u/Disasm)\
**Post date:** [July 28, 2021, 7:32pm UTC](https://forums.sifive.com/t/mcu-crashes-randomly/4992/9 "2021-07-28T19:32:34Z")

</div>

Make sure you build the firmware with soft floating point support: HiFive1 doesn’t have floating point support in hardware.
