HARD FAULT & ZEPHYR FATAL ERROR 0: CPU exception on CPU

Hello, everyone! I am using the ADC nrf52832 to read the NTC temperature, and the NTC calculation formula is used in the application as follows:

#define NTC_B 3950.0
#define NTC_T2 298.15 //(273.15+25)
#define NTC_R 10000.0 //(273.15+25)
#define NTC_Rt(u) (u*NTC_R)/(VCC-u) // The unit mv
# define GET_NTC_TEMP (u) (1 / (log (NTC_Rt (u)/NTC_R)/NTC_B + 1 / NTC_T2) - 273.15 + 0.5).

But using that executes GET_NTC_TEMP () debugging information submitted to the following error:

[00:00:09. 320495] [1; 31 m < err > OS: * * * * * HARD FAULT * * * * * [0 m
[00:00:09.320,526] [1;31m<err> os: Debug event[0m]
[00:00:09.320,556] [1;31m<err> os: r0/a1: 0x00000000 r1/a2: 0x40c02d80 r2/a3: 0x00000000[0m]
[00:00:09.320,556] [1;31m<err> os: r3/a4: 0x3fe00000 r12/ip: 0x00000000 r14/lr: 0x000112d7[0m]
[00:00:09.320,556] [1;31m<err> os: xpsr: 0x61000000[0m]
[00:00:05.320,587] [1;31m<err> os: s[0]: 0x418a1680 s[1]: 0x3fd818af s[2]: 0x00000000 s[3]: 0x00000000[0m]
[00:00:09.320,587] [1;31m<err> os: s[4]: 0x00000000 s[5]: 0x00000000 s[6]: 0x00000000 s[7]: 0x00000000[0m]
[00:00:09.320,617] [1;31m<err> os: s[8]: 0x00000000 s[9]: 0x00000000 s[10]: 0x00000000 s[11]: 0x00000000[0m]
[00:00:09.320,617] [1;31m<err> os: s[12]: 0x00000000 s[13]: 0x00000000 s[14]: 0x00000000 s[15]: 0x00000000[0m]
[00:00:09.320,648] [1;31m<err> os: fpscr: 0x00000000[0m]
[00:00:09.320,648] [1;31m<err> os: Faulting instruction address (r15/pc): 0x000112da[0m]
[00:00:09.320,709] [1;31m<err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0[0m]
[00:00:09.320,739] [1;31m<err> os: Current thread: 0x20002e68 (unknown)[0m
[00:00:10.887,664] [1;31m<err> fatal_error: Resetting system[0m]
In the application, I configured CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_IDLE_STACK_SIZE=3072
CONFIG_FPU=y
CONFIG_MAIN_STACK_SIZE=1024;

Seeking solutions, thank you!

  • Could be a divide by zero or possibly undefined symbol for log?

    I've noticed undefined symbols may not cause build to fail. 

  • Try breaking the computation performed by GET_NTC_TEMP into multiple statements and then in the debugger single step through each step until you determine the specific calculation that produces the hardfault. 

  • Hi,

     

    In addition to what is already mentioned here, try to increase the main stack size to 2k or higher:

    CONFIG_MAIN_STACK_SIZE=1024

     

    Kind regards,

    Håkon

  • Hello! My tests found that no error messages were printed when I turned off printk.

    The zephyr project enabled printk in accordance with the Developer Academy Basic tutorial, and an "MPU FAULT" error occurred when printing debugging information.

    The error information is as follows:

    U=4081mv,I=26mA,T1=17.66,T2=17.50
    [00:00:15. 566986] [1; 31 m < err > OS: * * * * * MPU FAULT * * * * * [0 m
    [00:00:15.566,986] [1;31m<err> os: Stacking error (context area might be not valid)[0m]
    [00:00:15.567,016] [1;31m<err> os: r0/a1: 0x52ca6621 r1/a2: 0xc50addbf r2/a3: 0x18853204[0m]
    [00:00:15.567,047] [1;31m<err> os: r3/a4: 0x214b5299r12 /ip: 0x52927c02 r14/lr: 0xa29a467c[0m]
    [00:00:15.567,047] [1;31m<err> os: xpsr: 0x41006200[0m]
    [00:00:15.567,077] [1;31m<err> os: s[0]: 0x00000001 s[1]: 0x00000011s [2]: 0x00000000 s[3]: 0x00023a21[0m]
    [00:00:15.567,077] [1;31m<err> os: s[4]: 0x9fa93901 s[5]: 0xcccccccd s[6]: 0x33333333 s[7]: 0x00000003[0m]
    [00:00:15.567,108] [1;31m<err> os: s[8]: 0x8dc502f0 s[9]: 0x00023951 s[10]: 0x1e4e1d05 s[11]: 0x00000004[0m]
    [00:00:15.567,108] [1;31m<err> os: s[12]: 0xcccccccd s[13]: 0xcccccccc s[14]: 0x8dc502f0 s[15]: 0x00023951[0m]
    [00:00:15.567,138] [1;31m<err> os: fpscr: 0x00029650[0m]
    [00:00:15.567,138] [1;31m<err> os: Faulting instruction address (r15/pc): 0x00029686[0m]
    [00:00:15.567,199] [1;31m<err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0[0m]
    U = 4080 mv, I = 26 ma, T1 = 17.69, T2 = 17.50
    [00:00:15.567,230] [1;31m<err> os: Current thread: 0x200019e0 (unknown)[0m
    [00:00:16.865,142] [1;31m<err> fatal_error: Resetting system[0m]

    If you run the printk application for a while, it will report the above error and restart; The above error does not occur when printk is removed and runs for a long time.
    This is my config:
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_SMP=y
    CONFIG_BT_GATT_CLIENT=y
    CONFIG_BT_DEVICE_NAME="Zephyr GATT Write"

    CONFIG_BT_BUF_ACL_RX_SIZE=255
    CONFIG_BT_BUF_ACL_TX_SIZE=251
    CONFIG_BT_BUF_CMD_TX_SIZE=255
    CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE=255

    CONFIG_BT_L2CAP_TX_MTU=247

    CONFIG_LOG=y

    CONFIG_ADC=y
    CONFIG_ADC_ASYNC=y
    #CONFIG_LOG_MODE_MINIMAL=y

    # enable I2C
    CONFIG_I2C=y

    # enable TIMER
    CONFIG_TIMER=y

    # enable GPIO
    CONFIG_GPIO=y
    #CONFIG_GPIO_HOGS=y

    # enable uart driver
    CONFIG_SERIAL=y
    # uart 异步API
    CONFIG_UART_ASYNC_API=y

    CONFIG_PWM=y

    CONFIG_LED=y
    #CONFIG_CAF_BUTTONS==y

    CONFIG_NFCT_PINS_AS_GPIOS=y

    CONFIG_PM_DEVICE=y
    CONFIG_PM_CPU_OPS=y
    CONFIG_PM_DEVICE_RUNTIME=y
    CONFIG_CBPRINTF_FP_SUPPORT=y

    CONFIG_IDLE_STACK_SIZE=2048
    CONFIG_FPU=y
    CONFIG_MAIN_STACK_SIZE=4096

    Can not understand the cause of the above problems, ask for help! Thank you very much!

  • Hi,

     

    Try to resolve the addresses listed in the assertion/fault log:

    wang chengteng said:
    os: Faulting instruction address (r15/pc): 0x00029686[0m]

    Using arm-zephyr-eabi-addr2line, similar to as described here, but with your own faulting address:

     RE: Asset Tracker V2 - Crashes trying to read non-existent config 

    wang chengteng said:
    Current thread: 0x200019e0 (unknown)[0m

    This is RAM mapped address, consisting with which thread that was executed when the stack overflow occurred. If you open build-folder/zephyr/zephyr.map file in a editor and search for "200019e0" - which thread does this correspond to?

     

    Kind regards,

    Håkon

Related