Skip to content

Serial USB on STM32G431KB not working #1283

Closed
@JiriBilek

Description

@JiriBilek

First I want to thank you for the effort in maintaining this library for us. I like the opportunity to quickly create an app for various STM32 MCUs.
The problem I am going to describe is related to my custom board.
If this library and its support is constrained to STM32 Boards only, please feel free to close this issue.

My device
I made a custom board with STM32G431KB. The board works fine with eclipse compiled code not related to Arduino (CubeMX generated). As I realized the board is quite similar to Nucleo-G431KB, I was curious if the board would work with stm32duino. The basic difference between my board and the Nucleo is that I don't solder the oscillator and hence don't use external clock (HSE).

Describe the bug
I am stuck with the USB CDC. The board runs fine in other configurations but selecting any of the two USB Support (if available): CDC options causes the board to freeze on start (it never reaches setup() function) and the PC is reporting unrecognized USB Device.
The board does not freeze when selecting the HID (keyboard and mouse) option but I can't test it if it works somehow :-(

To Reproduce
Try any sketch and compile it with USB Support: CDC option.

Desktop:

  • OS: Win10
  • Arduino IDE version: 1.8.13
  • STM32 core version: 1.9.0
  • Tools menu settings if not the default: Nucleo G431KB, U(S)ART Support: Enabled, USB Support: CDC, USB Speed: Low/Full Speed
  • Upload method: STM32CubeProgrammer DFU

Board:
Custom design based on STM32G431KBT6, similar to Nucleo-G431KB except the external oscillator is not soldered on mine board. USB CDC runs fine on code generated with CubeMX.

Additional context
The lack of the external oscillator should not be an issue, I learned from the code that this library doesn't use it anyway.

/* HSE is available but SB9 and SB10 OFF so not usable per default */

I somehow traced the application from the start and found that the clock setup may be wrong. However, after fixing it (code posted below), the execution of the code ends on


From this call the processor never returns. It may be related with the USB enumeration process that's interrupt driven but tracing this is unfortunately over my abilities.

Fixing the clock setup
I think the USB Clock domain must be enabled. As I am not able to test it on Nucleo board I am inserting the code here instead of making a PR.

It is replacement of the function:

WEAK void SystemClock_Config(void)

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI48;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
  RCC_OscInitStruct.PLL.PLLN = 85;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the peripherals clocks
  */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

My other thoughts
I compared stm32g4xx_hal_pcd.c file with mine generated by MXCube on another project and they are different. I tried to replace this file but it didn't help. It was only a shot in the dark, I don't think it's a good idea to change only one file in the HAL layer.

Metadata

Metadata

Assignees

Labels

bug 🐛Something isn't workingon goingCurrently work on this

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions