roberto Monti

CDC_Transmit_FS how to send the zero-length-packet (ZLP)

Discussion created by roberto Monti on Aug 29, 2017
Latest reply on Aug 30, 2017 by roberto Monti

Hello,  

I'm working with STM32F103 and the USB CDC.

I've configured the project with stm32cubemx.

I'm trying to send data from uC to PC host using the CDC_Transmit_FS function.

 

uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{
  uint8_t result = USBD_OK;
  /* USER CODE BEGIN 7 */
  USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;

  if ( hcdc->TxState != 0 )
  {
    return USBD_BUSY;
  }
  if ( hUsbDeviceFS->pClassData == NULL )
  {
    return USBD_FAIL;
  }

  USBD_CDC_SetTxBuffer( &hUsbDeviceFS, Buf, Len );
  do
  {
    result |= USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  }
  while ( ( result != USBD_OK ) && ( result != USBD_FAIL ) );

 

The function works well if I send a non-multiple of 64 number of bytes.

If the buffer has n*64 bytes size the USB_CDC data are sent only when I tried to send 4094 bytes.

 

I'm trying to send the ZLP for flush out the data in this way, like in this thread

 

https://community.st.com/thread/25569

 

 if ( ( Len % 64 == 0 ) && ( result == USBD_OK ) )
{
  while ( hcdc->TxState );
  USBD_CDC_SetTxBuffer( &hUsbDeviceFS, Buf, 0 );
  result |= USBD_CDC_TransmitPacket( &hUsbDeviceFS );
}

but the TxState is stuk to 1.

 

How can I send the ZLP?

 

Should not the library handle the thing by itself?

 

Thanks, Roberto

Outcomes