cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX generated code with USB Host, ends in HardFault_Handler()

bitlischieber
Associate III

Hy all

I have a STM32469NI_Disco Board and start a project using CubeMX 5.2.0 as following:

  • Select board by Board Selector.
  • Let initialize standard peripherals.
  • Connectivity - USB_OTG_FS: Select "Host_Only" and "Activate_VBUS"
  • Middleware - USB_HOST: Select "Mass Storage Host Class" for FS IP.
  • Generate Code for Atollic TrueSTUDIO (or Keil, if you like).
  • Open the Project
  • Fix the errors generated by CubeMX:
    • Src\freertos.c: Task name, replace "1" by an name e.g. "StartDefaultTask" and add a prototype in the header. Correct the osThreadStaticDef arguments to:
osThreadStaticDef(defaultTask, StartDefaultTask, osPriorityNormal, 0 , 4096, NULL, NULL);

  • Add code to enable the USB power in Src\usbh_conf.c, USBH_LL_DriverVBUS(..):
/* USER CODE BEGIN 0 */
if(state == 0)
{
  HAL_GPIO_WritePin(OTG_FS1_PowerSwitchOn_GPIO_Port, OTG_FS1_PowerSwitchOn_Pin, GPIO_PIN_RESET);
}
else
{
  HAL_GPIO_WritePin(OTG_FS1_PowerSwitchOn_GPIO_Port, OTG_FS1_PowerSwitchOn_Pin, GPIO_PIN_SET);
}
/* USER CODE END 0*/
  • Compile and start debug.

The program will, after a thumb driver connected, fall in to the HardFault_Handler().

The call hierarchy is:

MX_USB_HOST_Init() -> USBH_Start() -> USBH_LL_Start() -> HAL_HCD_Start() -> __HAL_HCD_ENABLE() -> USB_EnableGlobalInt()

USBx->GAHBCFG |= USB_OTG_GAHBCFG_GINT; // Crash!

I can't find the cause of the crash and currently even don't know how to precede.

The effect is the same if I generate the Project for CMSIS_v1, CMSIS_v2 or for Keil instead of TrueSTUDIO.

Someone have an Idea what I could try as next?

Thanks and best regards!

1 REPLY 1
bitlischieber
Associate III

Ok, I found the problem.

After USB_EnableGlobalInt(), the Interrupt was triggered. Deep in some of the methods called by the interrupt handler there are several debug messages posted. The methods USBH_UsrLog() and USBH_ErrLog() are using "printf" which is normally not correctly redirected.

According to a CubeMX example you have to add this code somewhere:

#ifdef __GNUC__
  /* With GCC, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
 
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart6, (uint8_t *)&ch, 1, 0xFFFF);
 
  return ch;
}