Search code examples
gccembeddedstm32halstm32f4

HAL_GetTick() crash mcu


I created a simple project using STCubeMX for my nucleo-f446ZE(STM32F446ZET6).

The project should be a USB device HID but it fail to start. After messing around with the debugger, I discovered that the MCU PC register go to 0x00000000 or 0xFFFFFFFF or sometimes random invalid value.

I didn't modify any code. I compiled the code with MDK-ARM (modified GCC, Vision IDE), and with GCC (openSTM32) and the same thing happen.

Callstack :

  • Main
  • SystemClock_Config
  • HAL_RCC_ClockConfig (632)
  • Hal_GetTick

Ps: RAM got corrupted after 0x080149A and that why the program do weird stuff Image Debugger,assembly

Solution

CubeMX didn't setup clocks very well. here is the setup i used to make work the usb.

  //RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  //RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  //RCC_OscInitStruct.HSICalibrationValue = 16;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 192;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  RCC_OscInitStruct.PLL.PLLR = 2;

Solution

  • The RCC_ClkInitStruct is probably not initialized properly (or at all)