cancel
Showing results for 
Search instead for 
Did you mean: 

get hardfault when do division func on stm32g070 with gcc cross compile tool

sheng yang
Associate III

my cross compile tool version: 

▸ arm-none-eabi-gcc -v Using built-in specs. COLLECT_GCC=arm-none-eabi-gcc COLLECT_LTO_WRAPPER=/home/red/.local/gcc-arm-none-eabi-10-2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/lto-wrapper Target: arm-none-eabi Configured with: /mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/src/gcc/configure --target=arm-none-eabi --prefix=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/install-native --libexecdir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/install-native/lib --infodir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/install-native/share/doc/gcc-arm-none-eabi/info --mandir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/install-native/share/doc/gcc-arm-none-eabi/man --htmldir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/install-native/share/doc/gcc-arm-none-eabi/html --pdfdir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/install-native/share/doc/gcc-arm-none-eabi/pdf --enable-languages=c,c++ --enable-plugins --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-newlib --with-headers=yes --with-python-dir=share/gcc-arm-none-eabi --with-sysroot=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/install-native/arm-none-eabi --build=x86_64-linux-gnu --host=x86_64-linux-gnu --with-gmp=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/build-native/host-libs/usr --with-mpfr=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/build-native/host-libs/usr --with-mpc=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/build-native/host-libs/usr --with-isl=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/build-native/host-libs/usr --with-libelf=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-48_20201124_1606180641/build-native/host-libs/usr --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-pkgversion='GNU Arm Embedded Toolchain 10-2020-q4-major' --with-multilib-list=rmprofile,aprofile Thread model: single Supported LTO compression algorithms: zlib gcc version 10.2.1 20201103 (release) (GNU Arm Embedded Toolchain 10-2020-q4-major)

And My Final link config as:

[1/1] arm-none-eabi-g++ -nostartfiles -nostdlib --specs=nosys.specs -Xlinker -print-memory-usage -Xlinker --sort-section=alignment obj/kernel/core/core.tos_barrier.o obj/kernel/core/core.tos_binary_heap.o obj/kernel/core/core.tos_bitmap.o obj/kernel/core/core.tos_char_fifo.o obj/kernel/core/core.tos_completion.o obj/kernel/core/core.tos_countdownlatch.o obj/kernel/core/core.tos_event.o obj/kernel/core/core.tos_global.o obj/kernel/core/core.tos_mail_queue.o obj/kernel/core/core.tos_message_queue.o obj/kernel/core/core.tos_mmblk.o obj/kernel/core/core.tos_mmheap.o obj/kernel/core/core.tos_mutex.o obj/kernel/core/core.tos_pend.o obj/kernel/core/core.tos_priority_mail_queue.o obj/kernel/core/core.tos_priority_message_queue.o obj/kernel/core/core.tos_priority_queue.o obj/kernel/core/core.tos_ring_queue.o obj/kernel/core/core.tos_robin.o obj/kernel/core/core.tos_rwlock.o obj/kernel/core/core.tos_sched.o obj/kernel/core/core.tos_sem.o obj/kernel/core/core.tos_stopwatch.o obj/kernel/core/core.tos_sys.o obj/kernel/core/core.tos_task.o obj/kernel/core/core.tos_tick.o obj/kernel/core/core.tos_time.o obj/kernel/core/core.tos_timer.o obj/kernel/osal/cmsis_os/cmsis_os.cmsis_os.o obj/kernel/hal/tos_hal_armv6m_gcc.o obj/kernel/arch/arm/arm-v6m/common/common.tos_cpu.o obj/kernel/arch/arm/arm-v6m/common/common.tos_fault.o obj/kernel/arch/arm/arm-v6m/cortex-m0/gcc/cortex-m0.port_c.o obj/kernel/arch/arm/arm-v6m/cortex-m0/gcc/port_s.o obj/platform/vendor_bsp/st/CMSIS/Device/ST/STM32G0xx/Source/Templates/platform.system_stm32g0xx.o obj/board/XYJ_STM32G070CBTX/APP/hello_world/startup_stm32g070xx.o obj/board/XYJ_STM32G070CBTX/APP/hello_world/APP.main.o obj/board/XYJ_STM32G070CBTX/APP/hello_world/HAL.stm32g0xx_it.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_uart.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_uart_ex.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_dma.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_rcc.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_rcc_ex.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_pwr.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_pwr_ex.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_gpio.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_cortex.o obj/board/XYJ_STM32G070CBTX/BSP/Src/BSP.mcu_init.o obj/board/XYJ_STM32G070CBTX/BSP/Src/BSP.gpio.o obj/board/XYJ_STM32G070CBTX/BSP/Src/BSP.bsp_usart.o obj/board/XYJ_STM32G070CBTX/BSP/Src/BSP.usart.o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/vendor_bsp_hal.stm32g0xx_hal.o -T /home/red/LearnProjects/ChipAdaptation/board/XYJ_STM32G070CBTX/LEDsingle.ld -L/home/red/.local/gcc-arm-none-eabi-10-2020-q4-major/arm-none-eabi/lib/thumb/v6-m/nofp -lgcc -Wl,-Map=LEDsingle.map -o LEDsingle.elf && arm-none-eabi-objcopy -O binary LEDsingle.elf LEDsingle.bin && arm-none-eabi-objcopy -O ihex LEDsingle.elf LEDsingle.hex && arm-none-eabi-size LEDsingle.elf 

And when I use gdb to debug, I found I will get in Hardfault_Handler, and the stack backtrace as:

(gdb) bt

#0 HardFault_Handler () at ../board/XYJ_STM32G070CBTX/APP/hello_world/stm32g0xx_it.c:89

#1

#2 0x08008ff8 in ?? ()

#3 0x08003ca2 in cpu_init () at ../kernel/arch/arm/arm-v6m/common/tos_cpu.c:81

the cpu_init func is just as: 

k_cpu_cycle_per_tick = TOS_CFG_CPU_CLOCK / k_cpu_tick_per_second;

it's equa with k_cpu_cycle_per_tick = 16000000 / 1000; 

I wonder how could this happen, I build this on stm32g070cb chip.

w

5 REPLIES 5

Make sure it's building for CM0 core. No divide instruction, done with library code.

Check if dividing by zero

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

1. I build this with cflags

arm-none-eabi-gcc -MMD -MF obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_uart.o.d -DSTM32G070xx -I../kernel/arch/arm/arm-v6m/common/include -I../kernel/arch/arm/arm-v6m/cortex-m0/gcc -I../kernel/core/include -I../kernel/hal/include -I../kernel/osal/cmsis_os -I../kernel/pm/include -I../board/XYJ_STM32G070CBTX/TOS-CONFIG -I../platform/vendor_bsp/st/CMSIS/Device/ST/STM32G0xx/Include -I../platform/vendor_bsp/st/CMSIS/Include -I../platform/vendor_bsp/st/CMSIS/Core/Include -I../platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Inc -I../board/XYJ_STM32G070CBTX/BSP/Inc -O2 -g -fno-builtin -fno-strict-aliasing -fdiagnostics-color=always -fshort-enums -mcpu=cortex-m0plus -mthumb -Wa,-mimplicit-it=thumb -std=c99 -MP -MD -Wno-misleading-indentation -c ../platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c -o obj/platform/vendor_bsp/st/STM32G0xx_HAL_Driver/Src/HAL.stm32g0xx_hal_uart.o

as you can see here are flags with  ``-mcpu=cortex-m0plus -mthumb``. and I use -lgcc library to link with divide library code.

2. I debug with gdb step there no dividing by zero,

I do as below :

abc = abc / 1000;

can still get fault.

Decode the registers and code that's specifically faulting.

If you get the faulting PC, identify that in the .MAP and .LST files.

Ideally have a Hard Fault Handler that dumps these things.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

When I debug with reg info ,I get

(gdb) step

Program received signal SIGINT, Interrupt.
HardFault_Handler () at ../board/XYJ_STM32G070CBTX/APP/hello_world/stm32g0xx_it.c:89
89 while (1)
(gdb) bt
#0 HardFault_Handler () at ../board/XYJ_STM32G070CBTX/APP/hello_world/stm32g0xx_it.c:89
#1 <signal handler called>
#2 0x08008ff4 in ?? ()
#3 0x08003ca2 in cpu_init () at ../kernel/arch/arm/arm-v6m/common/tos_cpu.c:81
#4 0x08002884 in tos_knl_init () at ../kernel/core/tos_sys.c:24
#5 0x080038e6 in osKernelInitialize () at ../kernel/osal/cmsis_os/cmsis_os.c:35
#6 0x08008ba2 in main () at ../board/XYJ_STM32G070CBTX/APP/hello_world/main.c:36
(gdb) info reg
r0 0x40022000 1073881088
r1 0x0 0
r2 0x3e8 1000
r3 0x0 0
r4 0xffffffff -1
r5 0xffffffff -1
r6 0xffffffff -1
r7 0x2000002f 536870959
r8 0xffffffff -1
r9 0xffffffff -1
r10 0xffffffff -1
r11 0xffffffff -1
r12 0xe000e100 -536813312
sp 0x200001e0 0x200001e0
lr 0xfffffff9 0xfffffff9
pc 0x8003f6c 0x8003f6c <HardFault_Handler>
xpsr 0x1000003 16777219
msp 0x200001e0 0x200001e0
psp 0xfffffffc 0xfffffffc
primask 0x0 0 '\000'
basepri 0x0 0 '\000'
faultmask 0x0 0 '\000'
control 0x0 0 '\000'

and before I do this divise instruct , the regs info as

(gdb) info reg
r0 0x0 0
r1 0x20 32
r2 0x40013800 1073821696
r3 0x2d 45
r4 0xffffffff -1
r5 0xffffffff -1
r6 0xffffffff -1
r7 0x2000002f 536870959
r8 0xffffffff -1
r9 0xffffffff -1
r10 0xffffffff -1
r11 0xffffffff -1
r12 0xe000e100 -536813312
sp 0x2000020c 0x2000020c
lr 0x8002885 0x8002885 <tos_knl_init+8>
pc 0x8003c90 0x8003c90 <cpu_init>
xpsr 0x1000000 16777216
msp 0x2000020c 0x2000020c
psp 0xfffffffc 0xfffffffc
primask 0x0 0 '\000'
basepri 0x0 0 '\000'
faultmask 0x0 0 '\000'
control 0x0 0 '\000'

 I though something wrong in libgcc.a file ???

sheng yang
Associate III

Final I found the mistake, there are two factors in my project:

1. I lost -mcpu=cortex-m0plus and -mthumb flags in final link process so link with wrong libgcc.a, this is the root cause.

2. I set uart1 handler enable early but the uart init func is wrong, and I wonder why this will hand cpu,:frowning_face: