Page 1 of 2

STM32F407 Strange SerialUsbDriver behavior

Posted: Mon Apr 03, 2017 2:03 pm
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.

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Mon Apr 03, 2017 3:11 pm
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

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Mon Apr 03, 2017 4:30 pm
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?

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Mon Apr 03, 2017 5:45 pm
by Giovanni
What kind of host do you use? the host should send zero packets automatically.

Giovanni

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Tue Apr 04, 2017 7:37 am
by kioskpayprogrammers
PC Windows 7 + .Net Application

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Tue Apr 04, 2017 8:44 am
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

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Tue Apr 04, 2017 9:40 am
by kioskpayprogrammers
ChibiOS ver 16.1.0

Now i am trying to rewrite code without using events.

Thanks

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Tue Apr 04, 2017 11:20 am
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(

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Tue Apr 11, 2017 12:31 pm
by Giovanni
Hi,

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

Giovanni

Re: STM32F407 Strange SerialUsbDriver behavior

Posted: Fri Apr 14, 2017 9:19 am
by kioskpayprogrammers
Oh....
Thanks, i'll attach test project.
moment