Docs

Micro-controllers, wireless transmission and database

Head

PC to Blue Pill via USD CDC using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

STM32CubeIDE Settings

Click RCC → High Speed Clock (HSE) to Crystal/Ceramic Resonator

Set PA1, PC13 and PB9 to GPIO_Output

Click connectivity → Click USB

(Tick) Device (FS)

Click Middleware → Click USB_DEVICE

For Class for FS IP → select Communication Device Class (virtual Port Com)

Click Clock Configuration tab → HCLK (MHz) to 72

Additional code on top of STM32CubeIDE generated code

< -------------- Inside usbd_cdc_if.c -------------- >

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
extern uint8_t recData[3];
/* USER CODE END PV */

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  memset(recData,'\0',3);
  memcpy(recData, Buf, 3);
  memset(Buf,'\0',3);
  return (USBD_OK);
  /* USER CODE END 6 */
}

< -------------- Inside main.c -------------- >

/* USER CODE BEGIN PV */
volatile uint8_t recData[3];
/* USER CODE END PV */

  /* USER CODE BEGIN WHILE */
  while (1)
  {
    if (recData[0]=='N') HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
    else if (recData[0]=='Y') HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 1);
    if (recData[1]=='N') HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, 0);
    else if (recData[1]=='Y') HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, 1);
    if (recData[2]=='Y') HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
    else if (recData[2]=='N') HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1);
    /* USER CODE END WHILE */