Page 1 of 1

Hard fault when enabling HSI on STM32F042

Posted: Tue Dec 15, 2015 12:25 am
by kisielk
Just trying out the ChibiOS HAL on STM32F042 for the first time, but I'm running in to a hard fault almost right away. I tracked it down to the very first part of __early_init when HSI is enabled. Here's my GDB session:

Code: Select all

(gdb) si
__early_init () at ../../ChibiOS/os/hal/boards/FREQMEASURE_PROTO/board.c:70
70   void __early_init(void) {
=> 0x8000ca0 <__early_init()>:   push   {r3, lr}
(gdb) si
72     stm32_clock_init();
=> 0x8000ca2 <__early_init()+2>:   bl   0x8000510 <stm32_clock_init()>
(gdb) si
stm32_clock_init () at ../../ChibiOS/os/hal/ports/STM32/STM32F0xx/hal_lld.c:140
140     RCC->CR |= RCC_CR_HSION;                  /* Make sure HSI is ON.         */
=> 0x8000510 <stm32_clock_init()>:   ldr   r2, [pc, #164]   ; (0x80005b8 <stm32_clock_init()+168>)
(gdb) si
0x08000512   140     RCC->CR |= RCC_CR_HSION;                  /* Make sure HSI is ON.         */
=> 0x8000512 <stm32_clock_init()+2>:   ldr   r3, [r2, #0]
(gdb) si
0x08000514   140     RCC->CR |= RCC_CR_HSION;                  /* Make sure HSI is ON.         */
=> 0x8000514 <stm32_clock_init()+4>:   orr.w   r3, r3, #1
(gdb) si
_unhandled_exception () at ../../ChibiOS/os/common/ports/ARMCMx/compilers/GCC/vectors.c:77
77   void _unhandled_exception(void) {
=> 0x8000c40 <_unhandled_exception()>:   b.n   0x8000c40 <_unhandled_exception()>
(gdb) mon reg
===== arm v7m registers
(0) r0 (/32): 0x00000002
(1) r1 (/32): 0xFFFFFFFF
(2) r2 (/32): 0x40021000
(3) r3 (/32): 0x00006483
(4) r4 (/32): 0xFFFFFFFF
(5) r5 (/32): 0xFFFFFFFF
(6) r6 (/32): 0xFFFFFFFF
(7) r7 (/32): 0xFFFFFFFF
(8) r8 (/32): 0xFFFFFFFF
(9) r9 (/32): 0xFFFFFFFF
(10) r10 (/32): 0xFFFFFFFF
(11) r11 (/32): 0xFFFFFFFF
(12) r12 (/32): 0xFFFFFFFF
(13) sp (/32): 0x20000400
(14) lr (/32): 0xFFFFFFFD
(15) pc (/32): 0x08000C40
(16) xPSR (/32): 0x01000003
(17) msp (/32): 0x20000400
(18) psp (/32): 0x200007D8
(19) primask (/1): 0x01
(20) basepri (/8): 0x00
(21) faultmask (/1): 0x00
(22) control (/2): 0x00
===== Cortex-M DWT registers
(23) dwt_ctrl (/32)
(24) dwt_cyccnt (/32)
(25) dwt_0_comp (/32)
(26) dwt_0_mask (/4)
(27) dwt_0_function (/32)
(28) dwt_1_comp (/32)
(29) dwt_1_mask (/4)
(30) dwt_1_function (/32)
(gdb)


The top of my mcuconf.h is:

Code: Select all

#define STM32F0xx_MCUCONF

/*
 * HAL driver system settings.
 */
#define STM32_NO_INIT                       FALSE
#define STM32_PVD_ENABLE                    FALSE
#define STM32_PLS                           STM32_PLS_LEV0
#define STM32_HSI_ENABLED                   TRUE
#define STM32_HSI14_ENABLED                 FALSE
#define STM32_LSI_ENABLED                   TRUE
#define STM32_HSE_ENABLED                   TRUE
#define STM32_LSE_ENABLED                   FALSE
#define STM32_SW                            STM32_SW_PLL
#define STM32_PLLSRC                        STM32_PLLSRC_HSI_DIV2
#define STM32_PREDIV_VALUE                  1
#define STM32_PLLMUL_VALUE                  12
#define STM32_HPRE                          STM32_HPRE_DIV1
#define STM32_PPRE                          STM32_PPRE_DIV1
#define STM32_ADCSW                         STM32_ADCSW_PCLK
#define STM32_ADCPRE                        STM32_ADCPRE_DIV4
#define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK
#define STM32_ADCPRE                        STM32_ADCPRE_DIV4
#define STM32_CECSW                         STM32_CECSW_HSI
#define STM32_I2C1SW                        STM32_I2C1SW_HSI
#define STM32_USART1SW                      STM32_USART1SW_PCLK
#define STM32_RTCSEL                        STM32_RTCSEL_LSI


Not sure what is going on, seems like very basic code that should work. I must be making a simple mistake somewhere...

The board is a custom board we've designed, but I've verified the hardware functionality using the project template from GNU ARM Eclipse, just trying to make it work with the HAL now.

Re: Hard fault when enabling HSI on STM32F042

Posted: Tue Dec 15, 2015 7:45 am
by Giovanni
Have you tried the Nucleo32 demo for the F042? it works here.

Giovanni

Re: Hard fault when enabling HSI on STM32F042

Posted: Tue Dec 15, 2015 7:39 pm
by kisielk
Giovanni wrote:Have you tried the Nucleo32 demo for the F042? it works here.

Giovanni


Not yet since I was on 3.0.3, will update to 3.0.4 and try it out.

Re: Hard fault when enabling HSI on STM32F042

Posted: Tue Dec 15, 2015 8:16 pm
by Giovanni
hi,

Those demos are on the trunk code, you need to checkout the subversion repository or get ChibiStudio 15 which also includes a snapshot of the repository.

giovanni

Re: Hard fault when enabling HSI on STM32F042

Posted: Tue Dec 15, 2015 8:18 pm
by kisielk
Thanks, I figured that out and checked it out from GitHub.

The Nucleo32 demo runs fine, so clearly I must have made some mistake when adapting one of the other boards. I guess I'll try using the Nucleo32 demo as a base and changing it to be os-less.

Re: Hard fault when enabling HSI on STM32F042

Posted: Sat Dec 26, 2015 3:53 pm
by rew
I always get the hardfaults when I've used a wrong board.c/board.h that was made for a different processor. Then you get initialization code for GPIO ports that are not available.