Serrial communication massive data loss

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Serrial communication massive data loss

Post by DrunkenDonkey »

Hey all,

It is another of the very newb questions I have... I have 2 components on serial ports, one being 57600, the other 115200 speed, the uarts are initialized as {115200,0,USART_CR2_STOP1_BITS|USART_CR2_LINEN,0}; and generally both having a thread that is reading data and analyzing it. I'm using the serial system with sdGetTimeout, sdReadTimeout and increased the serial buffers to 64 bytes.

What I found out is that I can't really debug incoming data, if I don't read every single incoming byte right away, it is missing and subsequent read operations timeout. I found out that I can't reliably get the data from the port on 4mhz (STM32L) on 57600 and sometimes even losing bytes on 16mhz on 115200. That with a 2 threads running, one in direct sleep after sending the data, the other just looping to read data. The data should stay in serial buffers until read or overflow right? But even 1 millisecond delay before calling sdGet and I can no longer read the incoming byte. The test:

Code: Select all

static msg_t readThread(void *arg)
{
   (void)arg;
   s16 b;

   while (TRUE)
   {
      //wait for a packet header
      if(sdGetTimeout(&SERIAL_DRIVER,MS2ST(1000))!=CSOH)
         continue;
      chThdSleepMilliseconds(1);
      if(sdGetTimeout(&SERIAL_DRIVER,MS2ST(1000))!=0x01)
         continue;


Then I put a breakpoint on the second "continue" and device sends the packet, I can see on the logic analyzer that all the data is sent, yet, I hit the breakpoint. If I remove the 1ms sleep it is able to get most of the data, but still losing. I'm obviously missing something badly...

ps: 2.4.0 release
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serrial communication massive data loss

Post by Giovanni »

Could you post the whole code, I see only part of it.

Anyway, the queues should store up to 64 bytes, does your application continue to send data? 1mS is enough to overflow the queues.

Giovanni
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: Serrial communication massive data loss

Post by DrunkenDonkey »

Development! ;)

The problem got split in 2:
1. I took the ARMCM3-STM32L152-DISCOVERY project from the 2.4.0, stripped it to the bare bones, switched over to MSI 4k, enabled UART2 and added simple powering on the module, sending command to it and receiving the response in a thread. Again, massive data loss, this is what I get from the analyzer:
rx.png
rx.png (11.04 KiB) Viewed 8434 times


And this is what I get:
rxdata.png
rxdata.png (9.78 KiB) Viewed 8434 times


The data I'm getting is variable length, floating between 17 to 21 bytes, while 24 should be received. If I switch to HSI, problem solved, but I really don't need the 16mhz and I need very low power, so trying to stay in 2-4 range is best. I wonder if the MSI is just... crap?

2. On my original project I was still losing data even when using HSI, it turned out that it is CH_DBG_SYSTEM_STATE_CHECK that is doing it, turning it to FALSE got rid of the communication problems.

In both cases the receive packet happily fits fully into the serial rx buffer.
Attachments
SerialTest.zip
(14.27 KiB) Downloaded 503 times
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serrial communication massive data loss

Post by Giovanni »

MSI default is 2MHz if I remember well, you may increase it to 4MHz.

You should also monitor the Serial driver status flags, probably you are getting uncaught overflow conditions (data is lost before reaching the software queue).

Another solution could be to use the UART driver, probably using the DMA would help handling high data rates at low speed (and reduce power usage because less ISRs to handle).

Giovanni
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: Serrial communication massive data loss

Post by DrunkenDonkey »

I'm currently using MSI at 4mhz (the thing in the previous post is a typo) and 4mhz unable to handle 115220 communication of just 25 bytes without doing anything else is what is making me wonder... this shouldn't be the case. I think I'll go for the UART driver directly when I get to optimization stage.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serrial communication massive data loss

Post by Giovanni »

You may try to make the serial driver ISR handler thinner by removing events generation and see if that helps (I could make it an option if it does).

Giovanni
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: Serrial communication massive data loss

Post by DrunkenDonkey »

Sorry for being stupid, but how do I gracefully stop the events? Setting CH_USE_EVENTS to FALSE results in no less than a billion errors and removing chIOAddFlagsI in sdIncomingDataI and sdRequestDataI in serial.c ranges from no effect to total loss of communication...
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serrial communication massive data loss

Post by Giovanni »

I meant to physically remove calls to events from the ISR code path, this also means inlining the call of sdRequestDataI() in the ISR without the events code, the ISR would become:

Code: Select all

static void serve_interrupt(SerialDriver *sdp) {
  USART_TypeDef *u = sdp->usart;
  uint16_t cr1 = u->CR1;
  uint16_t sr = u->SR;  /* SR reset step 1.*/
  uint16_t dr = u->DR;  /* SR reset step 2.*/

  /* Error condition detection.*/
  if (sr & (USART_SR_ORE | USART_SR_NE | USART_SR_FE  | USART_SR_PE))
    set_error(sdp, sr);
  /* Special case, LIN break detection.*/
  if (sr & USART_SR_LBD) {
    u->SR &= ~USART_SR_LBD;
  }
  /* Data available.*/
  if (sr & USART_SR_RXNE) {
    chSysLockFromIsr();
    chIQPutI(&sdp->iqueue, (uint8_t)dr);
    chSysUnlockFromIsr();
  }
  /* Transmission buffer empty.*/
  if ((cr1 & USART_CR1_TXEIE) && (sr & USART_SR_TXE)) {
    msg_t b;
    chSysLockFromIsr();
    b = chOQGetI(&sdp->oqueue);
    if (b < Q_OK) {
      u->CR1 = (cr1 & ~USART_CR1_TXEIE) | USART_CR1_TCIE;
    }
    else
      u->DR = b;
    chSysUnlockFromIsr();
  }
  /* Physical transmission end.*/
  if (sr & USART_SR_TC) {
    u->CR1 = cr1 & ~USART_CR1_TCIE;
    u->SR &= ~USART_SR_TC;
  }
}


It should become a little faster but I didn't test it. Probably it would be possible to rewite this code to make it more efficient at high interrupt rates by enclosing the check in a loop, this way it would not return from the ISR until there is still pending data in the hw buffer.

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

Re: Serrial communication massive data loss

Post by Giovanni »

Ah, I just remembered :-)

Try enabling the port option CORTEX_SIMPLIFIED_PRIORITY, that will buy you some more speed.

Giovanni
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: Serrial communication massive data loss

Post by DrunkenDonkey »

Sorry Giovanni, didn't make any change, switched back and forth to see anything, even broke it just to check I'm modding the right function, but it is all the same. Will have to try it with the UART later to see if there will be any sort of a difference too, what's the possibility of the MSI being too inaccurate and screwing up the reading?
Post Reply