STM32F407 Strange SerialUsbDriver behavior

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
kioskpayprogrammers
Posts: 7
Joined: Wed Mar 01, 2017 8:40 am

Re: STM32F407 Strange SerialUsbDriver behavior

Postby kioskpayprogrammers » Fri Apr 14, 2017 10:51 am

attachments:
1)usb_serial.zip contains configuration of serial_usb system
2)SerialUsbTest.zip contains test visual studio 2010 C# project

SerialUsbTest\bin\Debug\SerialUsbTest.exe - test aplication

approximately device code

Code: Select all


void USB_init_serial(void)
{
   sduObjectInit(&usb_serial);
   sduStart(&usb_serial, &usb_serial_config);

   usbDisconnectBus(usb_serial_config.usbp);
   chThdSleepMilliseconds(1000);
   usbStart(usb_serial_config.usbp, &usb_config);
   usbConnectBus(usb_serial_config.usbp);
}

void debug_buffer(BaseSequentialStream* debugStream, uint8_t* data, uint16_t length)
{
   for (int i = 0; i < length; i++)
   {
      chprintf(debugStream, "%02x", data[i]);
      if (i < length - 1)
         chprintf(debugStream, " ");
   }
}

static uint8_t temp_buffer[2048] = {};

int main(void)
{
  halInit();
  chSysInit();
  sdStart(&SD3, &sd_cfg);

  USB_init_serial();
  chThdSleepMilliseconds(3000);

   msg_t c;
   uint16_t index = 0;
   

   while(true)
   {
      while ( (c = chnGetTimeout(&usb_serial, MS2ST(50))) != Q_TIMEOUT )
      {
         temp_buffer[index++] = (uint8_t)c;
      }

      if (index > 0)
      {
         debug_buffer((BaseSequentialStream*)&SD3, temp_buffer, index);
         chnPutTimeout(&usb_serial, '@', TIME_INFINITE);
      }

      index = 0;
   }

}
Attachments
usb_serial.zip
usb_serial params
(2.59 KiB) Downloaded 149 times
SerialUsbTest.zip
VS2010 test client project
(44.18 KiB) Downloaded 148 times

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32F407 Strange SerialUsbDriver behavior

Postby Giovanni » Sun Apr 16, 2017 9:23 am

Hi,

I committed a change in repository (trunk), could you check if it does fix the problem?

What I fixed is the check for transaction terminated by short/zero packets, it failed to detect transactions terminated by zero packets and this would explain why you had problems with writes that are a multiple of packet size.

If you can confirm correct behavior then this will go in next release.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32F407 Strange SerialUsbDriver behavior

Postby Giovanni » Mon Apr 17, 2017 2:29 pm

Nevermind, the change is not effective, for some reason the driver is not terminating OUT transaction when the received size is smaller than the specified transaction size AND the last packet is equal to the max packet.

It could be:
1) The host does not send a zero packet.
2) The driver fails to detect the zero packet for some reason.

I am working hoping that 2 is true, if it is 1 then there is nothing I could do. I reverted the previous change.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32F407 Strange SerialUsbDriver behavior

Postby Giovanni » Mon Apr 17, 2017 4:22 pm

I found this article (see last section):

https://msdn.microsoft.com/en-us/librar ... 38112.aspx

Apparently the host does not send zero sized packets, it is responsibility of the "client driver" (upper layers) to insert those zero packets as appropriate.

This means that the device driver on the STM32 has no mean to detect short transfers aligned to packet size by itself unless the host application sends an explicit "wakeup".

I guess the same is true for Linux hosts.

This could be confirmed using an real USB analyzer but I don't have one right now.

Giovanni

baoshi
Posts: 9
Joined: Wed May 15, 2013 5:49 am
Been thanked: 1 time

Re: STM32F407 Strange SerialUsbDriver behavior

Postby baoshi » Thu Jun 15, 2017 4:17 pm

I can confirm this behavior using Saleae Logic tapping on USB wires. I'm using a STM32F303.
When I'm sending 64 bytes, I see one 64 bytes packet and one 1 byte packet.
When I'm sending 64 bytes via CDC, only one OUT packet is captured with length 64 bytes, no zero length packet follows.

I see no easy fix unless I define SERIAL_USB_BUFFERS_SIZE to 64, this causes RX callback to be called for every packets or 64 bytes or below.

The ST USB stack does not suffer this issue because they do not buffer up packets. RX callback is called for every received packets no matter what length it is.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32F407 Strange SerialUsbDriver behavior

Postby Giovanni » Thu Jun 15, 2017 6:00 pm

Hi,

Without a zero size packet there is no way for the driver to terminate the transaction and generate an event, reducing buffers is the only way I believe... of course there is a performance impact.

Giovanni


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 31 guests