cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 USBD lost first bulk packet

VBKesha -
Associate II
Posted on May 29, 2017 at 17:01

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:

&sharpinclude <stdio.h>

&sharpinclude <stdint.h>

&sharpinclude <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?

#stm32f103 #lost #usbd #bulk
1 ACCEPTED SOLUTION

Accepted Solutions
VBKesha -
Associate II
Posted on May 31, 2017 at 12:05

I do error in Init fucntion, now i add in end of function this code and all work fine.

USBD_LL_FlushEP(pdev, SPI_EPOUT_ADDR);

USBD_LL_FlushEP(pdev, SPI_EPIN_ADDR);

USBD_LL_PrepareReceive(pdev, SPI_EPOUT_ADDR, buff, 100);

View solution in original post

1 REPLY 1
VBKesha -
Associate II
Posted on May 31, 2017 at 12:05

I do error in Init fucntion, now i add in end of function this code and all work fine.

USBD_LL_FlushEP(pdev, SPI_EPOUT_ADDR);

USBD_LL_FlushEP(pdev, SPI_EPIN_ADDR);

USBD_LL_PrepareReceive(pdev, SPI_EPOUT_ADDR, buff, 100);