1

I'm trying to figure out how to enable USB VCP functionality on my STM32F103-based Blue Pill board. In following Shawn Hymell's guide and attempting to troubleshoot with this other SO Q/A, I currently have the following (minified) main.c: (Git Gist with full file)

#include "main.h"
#include "usb_device.h"
#include <string.h>

int main(void) {
  char msg[50];
  uint8_t state = 0;

  HAL_StatusTypeDef ret_status;
  MX_USB_DEVICE_Init();

  sprintf(msg, "Restart!\n");
  ret_status = HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
  HAL_Delay(1000);

  while (1) {
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, state ? GPIO_PIN_RESET : GPIO_PIN_SET);
      sprintf(msg, "Hello World! LED State: %d\n", state);
      ret_status = HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
      ret_status = CDC_Transmit_FS((uint8_t*)msg, strlen(msg));

      state = state == 1 ? 0 : 1;
      HAL_Delay(500);
  }
}

I can tell that the program is (otherwise) running properly because the UART adapter shows that char msg is being output at the correct interval and the board's PC13 LED is flashing. However, my Windows 10 computer doesn't even recognize that there is a USB device plugged in, only showing the FTDI adapter's COM port and my STLink v2:

enter image description here

As of now, I've tried the following potential fixes from the other SO Q/A:

  • In usbd_cdc_if.c set APP_RX_DATA_SIZE 64 and APP_TX_DATA_SIZE 64
  • In usbd_cdc_if.c add below code to the CDC_Control_FS() function:
case CDC_SET_LINE_CODING:
  tempbuf[0]=pbuf[0];
  tempbuf[1]=pbuf[1];
  tempbuf[2]=pbuf[2];
  tempbuf[3]=pbuf[3];
  tempbuf[4]=pbuf[4];
  tempbuf[5]=pbuf[5];
  tempbuf[6]=pbuf[6];
  break;
case CDC_GET_LINE_CODING:
  pbuf[0]=tempbuf[0];
  pbuf[1]=tempbuf[1];
  pbuf[2]=tempbuf[2];
  pbuf[3]=tempbuf[3];
  pbuf[4]=tempbuf[4];
  pbuf[5]=tempbuf[5];
  pbuf[6]=tempbuf[6];
  break;
  • Add HAL_Delay(1000); before the first instance of calling CDC_Transmit_FS
  • Confirmed that the USB cable I'm using has data lines
  • Setting Minimum Heap Size to 0x1000 in the CubeMX Device Configuration Tool

Has anyone else seen something like this before? I'm not sure what to try next.

ifconfig
  • 6,242
  • 7
  • 41
  • 65
  • Check the cable :) Your main rather not sufficient. You need to set clock initialize hal, gpio etcetc. – 0___________ Oct 01 '21 at 22:54
  • Another problem - check pullup resistors on D+ & D- lines. Many BP have them wrong – 0___________ Oct 01 '21 at 22:56
  • Well, not sure if this helps, but I've done some more digging and found that the check for `TxState != 0` in `CDC_Transmit_FS` is being triggered, resulting in the return code `USBD_BUSY`. I don't exactly know how to interpret this... – ifconfig Oct 01 '21 at 23:01
  • Hrm, @0___________, what should they be? – ifconfig Oct 01 '21 at 23:01
  • Also, my minification of the `main.c` file removed those lines. All of the normal config functions are there and being called. – ifconfig Oct 01 '21 at 23:03
  • Simply generate the empty project in CubeMx ***without*** your changes and try. I use it all the time without any problems. – 0___________ Oct 01 '21 at 23:03
  • Sorry, I meant what the resistor values should be. I measure `R10 = 10k` and `R9 = R11 = 20` – ifconfig Oct 01 '21 at 23:08
  • @0___________ Here is a copy of my full `main.c` https://gist.github.com/neilbalch/719ddb38c225f5465789872843b7a93a – ifconfig Oct 01 '21 at 23:17

1 Answers1

1

Actually, my problem was far more simple than I thought. In my infinite wisdom, I decided to try this on a breadboard I'd previously been using for CANbus testing, without disconnecting the CAN circuit.

enter image description here (ref)

This was never going to work, as the CANbus pins I'd been trying to use (and left plugged in) share functionality with the USB port. The VCP immediately showed up and worked after I unplugged the breadboard wires I'd connected to PA11 and PA12. I feel dumb, but relieved, right now.

ifconfig
  • 6,242
  • 7
  • 41
  • 65