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

STM32F407 Strange SerialUsbDriver behavior

Post by kioskpayprogrammers »

Hi.

I am using SerialUsbDriver to realize simple protocol.
My device wait a command from host, do some actions and return a response.

Code: Select all


   eventflags_t flags;
   event_listener_t protocolDataEvent;
   
   //.....

   chEvtRegisterMask((event_source_t *)chnGetEventSource(&usb_serial),
   &protocolDataEvent,
   EVENT_MASK(CHN_INPUT_AVAILABLE));

   while (true)
   {
      chEvtWaitOneTimeout(EVENT_MASK(CHN_INPUT_AVAILABLE), MS2ST(10));
      flags = chEvtGetAndClearFlags(&protocolDataEvent);

      if (flags & CHN_INPUT_AVAILABLE)
      {
      //WORK LOGIC...
      }
   }


I noticed a strange behavior (or bug)
when i send packet from host to device with Length(bytes) % 64 == 0(64, 128, 192 etc...) event not fired
after few second i can send one(or more) byte from host and event will fire.

Is it a magic feature? or i dont understand anything??
i suppose driver wait for another bytes, why??

Thanks.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F407 Strange SerialUsbDriver behavior

Post by Giovanni »

Hi,

The driver does not work byte-per-byte, USB transfers are done using the buffer size (SERIAL_USB_BUFFERS_SIZE=256 by default), an event is triggered when:
1) A buffer has been filled or
2) an USB transaction is over.

Sizes that are not multiples of packet sizes (or zero sized) mark transaction ends in USB transfer, this is why you are getting an event when sending one more byte.

If you need to rely on events you may reduce the buffer size to be equal to packet size, that would impact performance however. You may also forcibly terminate transactions by sending zero sized packets from host, that would close the current transaction and generate an event I believe.

Giovanni
kioskpayprogrammers
Posts: 7
Joined: Wed Mar 01, 2017 8:40 am

Re: STM32F407 Strange SerialUsbDriver behavior

Post by kioskpayprogrammers »

i changed default buffer size to
#define SERIAL_USB_BUFFERS_SIZE 1024
due to my app logic buffer have never filled out

if i send single packet (63 or 65 bytes length) my event successfully triggered, i read this bytes -> all correct
but if i send single packet with 64 byte nothing triggered.

actions from host side in this cases are equal: SerialPort.Write(buffer, buffer.Length)
all bytes fly away from host.

What do you think about:
1)Which reasons on host side can influence on usb-transaction termination?
2)why in the thirst case usb-transaction successfully terminates?
3)do you advise to work without using events?
Right after the driver was started, i have to do...

Code: Select all

while (true)
{
 msg_t c = chnGetTimeout(&usb_serial, MS2ST(10));
   if (c != Q_TIMEOUT)
   {
   //TO DO...
   }   
}

is this way more reliable?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F407 Strange SerialUsbDriver behavior

Post by Giovanni »

What kind of host do you use? the host should send zero packets automatically.

Giovanni
kioskpayprogrammers
Posts: 7
Joined: Wed Mar 01, 2017 8:40 am

Re: STM32F407 Strange SerialUsbDriver behavior

Post by kioskpayprogrammers »

PC Windows 7 + .Net Application
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F407 Strange SerialUsbDriver behavior

Post by Giovanni »

This is a strange issue, I will have a look to the driver code. Does it work without events for you? this is even more strange.

Which ChibiOS version are you using?

Giovanni
kioskpayprogrammers
Posts: 7
Joined: Wed Mar 01, 2017 8:40 am

Re: STM32F407 Strange SerialUsbDriver behavior

Post by kioskpayprogrammers »

ChibiOS ver 16.1.0

Now i am trying to rewrite code without using events.

Thanks
kioskpayprogrammers
Posts: 7
Joined: Wed Mar 01, 2017 8:40 am

Re: STM32F407 Strange SerialUsbDriver behavior

Post by kioskpayprogrammers »

Code: Select all

   while (true)
   {
      msg_t c;
         while ( (c = chnGetTimeout(&usb_serial, MS2ST(50))) != Q_TIMEOUT )
         {
            debug_format("%02X ", c);
         }
   }


same situation without of events using:
#define SERIAL_USB_BUFFERS_SIZE 1024
then i send 64 byte packet from host - nothing readed on device
then send another length - all correct

after changing
#define SERIAL_USB_BUFFERS_SIZE 64
everything was working correctly - very strange behavior(
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F407 Strange SerialUsbDriver behavior

Post by Giovanni »

Hi,

Maybe I found a reason for this behavior, could you suggest an easy way to test this problem?

Giovanni
kioskpayprogrammers
Posts: 7
Joined: Wed Mar 01, 2017 8:40 am

Re: STM32F407 Strange SerialUsbDriver behavior

Post by kioskpayprogrammers »

Oh....
Thanks, i'll attach test project.
moment
Post Reply