cancel
Showing results for 
Search instead for 
Did you mean: 

How to make STM32F103C8T6 (blue pill) USB CDC work properly

RLu
Associate

I have such dev board and try to run it as virtual com port. I created project cubemx, enable only usb as device in CDC mode. After generating and running project with next small fixes I run it on board.

int main(void)

{

//....skip

 while (1)

 {

   /* USER CODE END WHILE */

    const char* str = "Test\n";

    CDC_Transmit_FS((uint8_t*)str, 5);

    HAL_Delay(1000);

   /* USER CODE BEGIN 3 */

 }

gtkterm connects to the dev but no received and sent data.

When I started to debug interrupt handler I found that usb device always in USBD_STATE_DEFAULT mode since start and state isn't being changed instead of ISRs..

USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev)

{

 if (pdev->dev_state == USBD_STATE_CONFIGURED)

 {

   if (pdev->pClass->SOF != NULL)

   {

     pdev->pClass->SOF(pdev);

   }

 }

 return USBD_OK;

}

I have read many tutorials and sample but the result is negative.

I have done USB CDC device successfully on STM32L476-DISCOVERY.

1 REPLY 1
RLu
Associate

Finally I found the bug. CubeMX doesn't generate whole setup.

Next code must be added into usbd_cdc_if.c:

/* Private variables ---------------------------------------------------------*/
USBD_CDC_LineCodingTypeDef LineCoding =
{
    9600,   /* baud rate*/
    0x00,   /* stop bits-1*/
    0x00,   /* parity - none*/
    0x08    /* nb. of bits 8*/
};
/* USER CODE END PV */

And:

    case CDC_SET_LINE_CODING:
		LineCoding.bitrate    = (uint32_t)(pbuf[0] | (pbuf[1] << 8) | (pbuf[2] << 16) | (pbuf[3] << 24));
		LineCoding.format     = pbuf[4];
		LineCoding.paritytype = pbuf[5];
		LineCoding.datatype   = pbuf[6];
		break;
 
    case CDC_GET_LINE_CODING:
        pbuf[0] = (uint8_t)(LineCoding.bitrate);
        pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8);
        pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16);
        pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24);
        pbuf[4] = LineCoding.format;
        pbuf[5] = LineCoding.paritytype;
        pbuf[6] = LineCoding.datatype;
        break;
 
    case CDC_SET_CONTROL_LINE_STATE:

Is this bug or not?