- Muchas notas - Fran Acién

20241129 - STM32 USB Virtual Com

I was able to make it work on STMCube IDE with the next information. Also with Arduino IDE, out of the box. But impossible to make it work on platformio.

I am doing some tests about using USB instead of UART. The code is the next:

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t tx_buff[]={65,66,67,68,69,70,71,72,73,74};
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_9);
	  HAL_UART_Transmit(&huart2, tx_buff, 10, 1000);
	  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_9);
	  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);
	  CDC_Transmit_FS(tx_buff, 10);
	  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);

	  HAL_Delay(1000);

  }
  /* USER CODE END 3 */
}

The clocks look something like this:

a4a9332969ee10f28dab087ce2934af4.png

e05f66ec0fac8053ce4cee131d0fd6b6.png

And the timming results are just amazing. The USB is the fastest by far:

e3035500ebaf244ad43448217a941b78.png

Taking 500ns the USB transaction to send the data.

To receive I used the next command:

tio /dev/serial/by-id/usb-STMicroelectronics_STM32_Virtual_ComPort_207D36624134-if00
[11:27:03.391] tio v2.7
[11:27:03.391] Press ctrl-t q to quit
[11:27:03.392] Connected
ABCDEFGHIJABCDEFGHIJABCDEFGHIJ