AnsweredAssumed Answered

STM32F103 USBD lost first bulk packet

Question asked by VBKesha - on May 29, 2017
Latest reply on May 31, 2017 by VBKesha -

I try doing usb device with two bulk endpoint IN/OUT.

All work fine but i lost every first packet on start device. Every new packet work fine, and work fine if i unplug and plug usb cable. And i recevie num of bytes in packet but not his content.

Code on STM32:

USBD_ClassTypeDef USBD_SPI =
{
   USBD_SPI_Init, //USBD_MSC_Init,
   USBD_SPI_DeInit, //USBD_MSC_DeInit,
   NULL, //USBD_MSC_Setup,
   NULL, /*EP0_TxSent*/
   NULL, /*EP0_RxReady*/
   USBD_SPI_DataIn, //USBD_MSC_DataIn,
   USBD_SPI_DataOut, //USBD_MSC_DataOut,
   NULL, /*SOF */
   NULL,
   NULL,
   USBD_SPI_GetHSCfgDesc,
   USBD_SPI_GetFSCfgDesc,
   USBD_SPI_GetOtherSpeedCfgDesc,
   NULL, //USBD_MSC_GetDeviceQualifierDescriptor,
};

 

uint8_t buff[100];

uint8_t USBD_SPI_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum){
   printf("Data OUT - %d\r\n", USBD_GetRxCount(pdev, epnum));
   printf("TR - %d\r\n", USBD_LL_PrepareReceive(pdev, epnum, buff, 100));
   printf("usb_print:0x%02X 0x%02X 0x%02X\r\n", buff[0], buff[1], buff[2]);
   return 0;
}

 

Code what i use to test transfer from linux:

#include <stdio.h>
#include <stdint.h>
#include <libusb-1.0/libusb.h>

 

libusb_device_handle * dev;

int main(int argc, char **argv){
   int x;
   char buff[100];

   libusb_init(NULL);

   dev = libusb_open_device_with_vid_pid(NULL, 0x1234, 0x1234);

   if (libusb_kernel_driver_active(dev, 0)){
      libusb_detach_kernel_driver(dev, 0);
   }

 

   if (libusb_claim_interface(dev, 0) < 0){
      printf("Interface error\n");
   }

 

   printf("Prepare\r\n");

   

   buff[0] = 0x1; buff[1] = 0x1; buff[2] = 0x1;
   libusb_bulk_transfer(dev, 0x01, buff, 3, &x, 0);

 

   buff[0] = 0x2; buff[1] = 0x2; buff[2] = 0x2;
   libusb_bulk_transfer(dev, 0x01, buff, 3, &x, 0);

 

   buff[0] = 0x3; buff[1] = 0x3; buff[2] = 0x3;
   libusb_bulk_transfer(dev, 0x01, buff, 3, &x, 0);


   libusb_close(dev);
   libusb_exit(NULL);
}

 

After start firmware i run transfer data and see in console:

USB RUN
Data OUT - 3
TR - 0
usb_print:0x00 0x00 0x00
Data OUT - 3
TR - 0
usb_print:0x02 0x02 0x02
Data OUT - 3
TR - 0
usb_print:0x03 0x03 0x03

 

On second tranfer i see normal data:

Data OUT - 3
TR - 0
usb_print:0x01 0x01 0x01
Data OUT - 3
TR - 0
usb_print:0x02 0x02 0x02
Data OUT - 3
TR - 0
usb_print:0x03 0x03 0x03

 

How i can fix this?

 

Outcomes