cancel
Showing results for 
Search instead for 
Did you mean: 

Adding new STM32 USB interface, receiving wrong data after about 1250 bytes

Jack Phan
Associate II
Posted on September 22, 2017 at 07:26

Hi

All,

I have a problem after adding a new interface for stm32 USB-CDC. I receive wrong data after sending about 1250 bytes.

My code has:

a) Two interfaces generated from STM32 CubeMX:

     . the first interface has one endpoint with address 0x82

     . the second interface has two endpoints, bulk IN and bulk OUT, with addresses are 0x01 and 0x81

b) I add a new interface with two endpoints, bulk In and bulk OUT, with addresses are 0x03 and 0x83

This new interface runs well in sending some packages, and I receive correct data in about 1250 bytes. After that, I receive wrong data.

I run a simple experiment:

    . sending a known eight bytes of a package in a loop in every 0.2 second. After 159 packages, I receive wrong packages. Each wrong package has twelve bytes ( I send only eight bytes!) and wrong data.

My code is generated from STM32 CubeMX version 4.22.0 USB, with freeRTOS, and using a development board, STM32 Nucleo-144 boards (NUCLEO-L496ZG).

Any idea or suggestion is very appreciated.

Jack

// ================= my code =========================================

uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)

{

  uint8_t result = USBD_OK;

  // test

  if (hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED)

       return USBD_FAIL;

  // end test

  /* USER CODE BEGIN 7 */

  USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;

  if (hcdc->TxState != 0){

    return USBD_BUSY;

  }

  // test

  for (int i = 0; i < Len; i++) {

    UserTxBufferFS[i] = Buf[i];

  }

  // end test

  USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, Len);

  result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);

  /* USER CODE END 7 */

  return result;

}

// =====================================================

void StartDefaultTask(void const * argument)

{

  /* init code for USB_DEVICE */

  osDelay(200);

  MX_USB_DEVICE_Init();

  osDelay(200);

  /* USER CODE BEGIN 5 */

  /* Infinite loop */

  for(;;)

  {

              // USB communication

              if (hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED) {

                if (recDataLength != 0) {       // process a new request package

                  if ( (recData[1] == recData[2]) && (recData[1] == 99) ) {

                     isRequestSending = 1;

                  }

                  // finished processing one request package

                  recDataLength = 0;

                }

 

              }

              if ( (hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED) && (isRequestSending == 1) ) {

                     uint8_t testBuf[8];

                     testBuf[0] = 8;

                     testBuf[1] = 3;

                     testBuf[2] = 0x0B;

                     testBuf[3] = TestCounter;

                     testBuf[4] = 4;

                     testBuf[5] = 0x08;

                     testBuf[6] = 1;

                     testBuf[7] = 0;

                     // send data 

                     CDC_Transmit_FS(testBuf, 8);

                     //HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);

                     TestCounter++;

              }

           osDelay(200);

  } // end for loop

  /* USER CODE END 5 */

}

#usb #usb-new-interface
0 REPLIES 0