Serial driver on 2.5.x is not working properly

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

Moderators: RoccoMarco, lbednarz, tfAteba

trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Serial driver on 2.5.x is not working properly

Post by trunet »

Hi,

I'm porting my code to chibios 2.5.1 and I'm having issues dealing with Serial driver.

Everything worked perfectly on 2.4.x however this code get serial event errors that never occurred on 2.4.x. I'm trying to debug but I have no success... Event flag on 2.5.x is 736 decimal(tons of event errors). But I didn't receive this on 2.4.x. Note that I'm dealing with a non-trivial serial device: it's 8E2 100000bps. SerialConfig is:

Code: Select all

const SerialConfig sbusPortConfig = {
    100000,
    (1 << 10),
    USART_CR2_STOP2_BITS,
    0
};


2.5.x not working code:

Code: Select all

static WORKING_AREA(waThreadRadio, 256);
static msg_t ThreadRadio(void *arg) {
    (void)arg;
    chRegSetThreadName("radio");

    EventListener elRadio;
    chEvtRegisterMask((EventSource *)chnGetEventSource(&SD1), &elRadio, 1);
    while (TRUE) {
        flagsmask_t flags;
        chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
        flags = chEvtGetAndClearFlags(&elRadio);
        if (flags & CHN_INPUT_AVAILABLE) {
            char c;
            c = chnGetTimeout(&SD1, TIME_IMMEDIATE);
            // do my stuff with "c"
        }
    }
}


2.4.x working properly code:

Code: Select all

static WORKING_AREA(waThreadRadio, 256);
static msg_t ThreadRadio(void *arg) {
    (void)arg;
    chRegSetThreadName("radio");

    EventListener elRadio;
    chEvtRegister(chIOGetEventSource(&SD1), &elRadio, 1);
    while (TRUE) {
        ioflags_t flags;
        chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
        flags = chIOGetAndClearFlags(&SD1);
        if (flags & IO_INPUT_AVAILABLE) {
            char c;
            c = chIOGetTimeout(&SD1, TIME_IMMEDIATE);
            // do my stuff with "c"
        }
    }
}


Thank you very much.

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

Re: Serial driver on 2.5.x is not working properly

Post by Giovanni »

Hi,

Not sure about the errors, those are HW related: SD_BREAK_DETECTED, SD_OVERRUN_ERROR, SD_FRAMING_ERROR.

About your code, I can see a problem there that could explain at least the overrun. You wait for an event but you assume that a single char is available in the queue. There could be no chars (if it was an error event) or more than one char.
If you consume only one char then the queue could be filled and finally overrun.

The correct way of handling a serial event is:
1) Wait for an event.
2) Call chEvtGetAndClearFlags().
3) If some error flag is set then handle it (see serial.h).
4) If CHN_INPUT_AVAILABLE is specified then read all chars from the queue using TIME_IMMEDIATE until the queue is empty.
5) Repeat from 1.

Giovanni
trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Re: Serial driver on 2.5.x is not working properly

Post by trunet »

How to check if serial buffer is empty?
trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Re: Serial driver on 2.5.x is not working properly

Post by trunet »

It's working perfectly now:

Code: Select all

int32_t c = 0;
while (c != Q_TIMEOUT) {
    c = chnGetTimeout(radioData.serialPort, TIME_IMMEDIATE);
}


The hardware errors I'm getting is because I'm using a transistor to invert signal and I'm losing some timing doing it on transistions. I just ordered an hex inverter.
goeck
Posts: 92
Joined: Mon Feb 11, 2013 12:00 pm

Re: Serial driver on 2.5.x is not working properly

Post by goeck »

Hey everyone,

sorry to make this thread alive after 3/4 of a year, but I keep having a serious issue with reading from a serial port.
My project involves communication with a BLE112 Bluetooth Low Energy module which can be controlled over UART.
Everything works nice except, I can't read and process the callback messages of the module, which actually shouldn't be that much of a thread, right?

I use a uart event listener thread and the serial driver. (I did some tests using the uart driver, but no luck at all...)
Here's the thread code

Code: Select all


// UART event listener
static WORKING_AREA(waThread_4, 128);
static msg_t Thread4(void *arg)
{
   (void)arg;
   chRegSetThreadName("BLEData");

   unsigned char data[256];
   struct ble_header hdr;
   uint8_t* pRxBuffer = rxBuffer;
   static uint8_t counter=0;

   EventListener elBLEData;
   flagsmask_t flags;
   chEvtRegisterMask((EventSource *)chnGetEventSource(&SD1), &elBLEData, EVENT_MASK(1));

   while (TRUE)
   {
      chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
      chSysLock();
      flags = chEvtGetAndClearFlags(&elBLEData);
      chSysUnlock();

      if (flags & CHN_INPUT_AVAILABLE)
      {
         palSetPad(GPIOC, GPIOC_LED3);
         palSetPad(GPIOC, GPIOC_LED4);

         //read all there is from buffer
//         msg_t charbuf;
//         do{
//            charbuf = chnGetTimeout(&SD1, TIME_INFINITE);
//            if ( charbuf != Q_TIMEOUT )
//               *pRxBuffer++ = (uint8_t)charbuf;
//         }
//         while (charbuf != Q_TIMEOUT);

//     //reset moving pointer
//      pRxBuffer = rxBuffer;

         sdReadTimeout(&SD1, &hdr, sizeof(hdr), TIME_IMMEDIATE);
         if(hdr.lolen)
            sdReadTimeout(&SD1, data, hdr.lolen, TIME_IMMEDIATE);

      hdr.type_hilen = rxBuffer[0];
      hdr.lolen = rxBuffer[1];
      hdr.cls = rxBuffer[2];
      hdr.command = rxBuffer[3];

      chSequentialStreamWrite(&SD1, &hdr, sizeof(hdr));
//      memmove(&hdr, &rxBuffer[0], sizeof(hdr));
//      if (hdr.lolen)
//         memmove(&data[0], &rxBuffer[4], hdr.lolen);

//      chSequentialStreamWrite(&SD1, &data[0], hdr.lolen);
//      //         if (hdr.lolen)
//      //            sdReadTimeout(&SD1, data, hdr.lolen, TIME_IMMEDIATE);
//

//         chSequentialStreamRead(&SD1, (uint8_t*)&hdr, sizeof(hdr));
//         if(hdr.lolen)
//            chSequentialStreamRead(&SD1, data, hdr.lolen);
//
          // Processing data
          const struct ble_msg *msg = ble_get_msg_hdr(hdr);
         msg->handler(data);

          palClearPad(GPIOC, GPIOC_LED3);
          palClearPad(GPIOC, GPIOC_LED4);
      }
   }
}


Basically almost all of this code is copied from user EsEblog in the thread called "Reading serial port with event handler".
Using this code I can catch all chars and resend them over the UART lines, but I am not able to put them in two different arrays and process them. The MCU halts.
As you can see, I tried some things because I read that one can read the whole buffer using sdRead(), but maybe I missed something...

I really really hope that you guys have a hint for me, I get nuts...

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

Re: Serial driver on 2.5.x is not working properly

Post by Giovanni »

Hi,

You seem to assume that sdReadTimeout(&SD1, &hdr, sizeof(hdr), TIME_IMMEDIATE) reads exactly sizeof(hdr) bytes. Because TIME_IMMEDIATE that function just returns what is already in the RX buffer without stopping internally, it can be anything between zero and sizeof(hdr).

You have to wait for data and call sdReadTimeout() or sdGetTimeout() until all the data you expect to receive has been received OR an error occurred.

Something like this.

Code: Select all

my buffer = empty;
while (1) {
  wait for event;
  if event.error then {
    handle error;
    break;
  }
  read data until the RX buffer is empty OR my buffer is filled;
  if my buffer is filled {
    handle message;
    break;
  }
}


Giovanni
goeck
Posts: 92
Joined: Mon Feb 11, 2013 12:00 pm

Re: Serial driver on 2.5.x is not working properly

Post by goeck »

Thanks for the help, Giovanni.
Actually I don't get this far with it...

Should this work then if I want to populate my hdr and the attached data (hdr.lolen actually tells how many payload bytes are attached, which should be read into data[])?

Code: Select all

// UART event listener
static WORKING_AREA(waThread_4, 128);
static msg_t Thread4(void *arg)
{
   (void)arg;
   chRegSetThreadName("BLEData");

   unsigned char data[256];
   struct ble_header hdr;
   uint8_t* pRxBuffer = rxBuffer;
   static uint8_t counter=0;

   EventListener elBLEData;
   flagsmask_t flags;
   chEvtRegisterMask((EventSource *)chnGetEventSource(&SD1), &elBLEData, EVENT_MASK(1));

   while (TRUE)
   {
      chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
      chSysLock();
      flags = chEvtGetAndClearFlags(&elBLEData);
      chSysUnlock();

      if (flags & CHN_INPUT_AVAILABLE)
      {
         sdReadTimeout(&SD1, &hdr, sizeof(hdr), TIME_INFINITE);
         if(hdr.lolen)
            sdReadTimeout(&SD1, data, hdr.lolen, TIME_INFINITE);

         chSequentialStreamWrite(&SD1, &hdr, sizeof(hdr));
                  if(hdr.lolen)
         chSequentialStreamWrite(&SD1, data, hdr.lolen);
     }
   }
}

MCU halts...

*crying*


--EDIT ---------
Hang on, I know what happened.
Actually everytime when I used the data array the MCU would halt as soon when the thread space was declared and initialised using

Code: Select all

tpBLEData      = chThdCreateStatic(waThread_4, sizeof(waThread_4), NORMALPRIO, Thread4, NULL);

So, the working area had only been given 128 Byte to live and the data array was declared to be 256 entries long...
After giving the thread 512 byte to live all went just fine.

Thanks anyway for the great support, 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: Serial driver on 2.5.x is not working properly

Post by Giovanni »

:)

Buffers... better do them static.

Giovanni
goeck
Posts: 92
Joined: Mon Feb 11, 2013 12:00 pm

Re: Serial driver on 2.5.x is not working properly

Post by goeck »

Hey Giovanni,

now I have the handlers called twice most often (but I can see that the message on UART comes only once physically)... Is there an obvious bug that I don't see?
I used 10ms as timeout because handling the messages would take way to long to be performant.
I will play around with this value to ensure it's not coming from there.
Will keep you posted.

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

Re: Serial driver on 2.5.x is not working properly

Post by Giovanni »

The above code still assumes that sdReadTimeout(&SD1, &hdr, sizeof(hdr), TIME_INFINITE) actually reads sizeof(hdr) bytes, it does not work that way. It reads the data ALREADY in the RX buffer and up to sizeof(hdr) bytes, the actual number of bytes read is returned.

The function will most likely return a number of bytes < sizeof(hdr).

Giovanni
Post Reply