Page 1 of 1

event driven serial, event is late

Posted: Sat Apr 30, 2016 9:56 am
by zamek
Hi All,

I would like to use event driven serialdriver. I made a test environment:
hw: I connected TxD to RxD to receive my sent characters.
sw: I sent only one 73 bytes length package with a console command. (7 bytes framing, 64 bytes data with incremental contents and 2 bytes of crc.)
HAL configuration is:
#define SERIAL_BUFFERS_SIZE 64

Here is my event listener code:

Code: Select all

 event_listener_t serial_listener;
    chEvtRegisterMaskWithFlags(chnGetEventSource(&SD_DRIVER), &serial_listener,
                                 EVENT_MASK(1),
                         CHN_INPUT_AVAILABLE|SD_PARITY_ERROR|SD_FRAMING_ERROR|SD_OVERRUN_ERROR|SD_NOISE_ERROR|SD_BREAK_DETECTED);
    while(TRUE) {
        palSetPad(GPIOD, GPIOD_LED5);
        eventmask_t evt = chEvtWaitAny(ALL_EVENTS);
       if (evt & EVENT_MASK(1)) {
          eventflags_t flags = chEvtGetAndClearFlags(&serial_listener);
          if (flags&CHN_INPUT_AVAILABLE)
             handle_serial_data(&motorola);

          if (flags&SD_FRAMING_ERROR)
             ++motorola.receive_framing_error;


          if (flags&SD_OVERRUN_ERROR)
             ++motorola.receive_overrun;

          if (flags&SD_NOISE_ERROR)
             ++motorola.receive_noise_error;
       }

       chThdSleepMicroseconds(SERIAL_SLEEP_TIME_US);

    }


I send data with chnWrite(&SD1, buffer, bufferlen)

An interest thing is that, when I sent a packet which is 73 bytes length I cannot get any event. I can get an event after I send 12 packets (which is 876bytes!). Of course the receive buffer is mixed to an unusable data content.

When event subsystem have to send an event, when the input queue filled?
Maybe I made a mistake in event configuration?

thx
Zamek

Re: event driven serial, event is late

Posted: Sat Apr 30, 2016 1:45 pm
by Giovanni
Hi,

The CHN_INPUT_AVAILABLE event is signaled when the input queues goes from empty to non-empty. If you don't read all data from the queue then no more events are signaled.

Giovanni

Re: event driven serial, event is late

Posted: Sun May 01, 2016 7:52 am
by zamek
Hi Giovanni,
Thx for your reply. I made some debugging and I saw the following:

When I send a packet I get event CHN_INPUT_AVAILABLE. I call the handle_serial function. This function calls

Code: Select all

int bytes_read = chnRead(&SD1, read_buffer, READ_BUFFER_SIZE);

this function is blocked.

read_buffer is a global 1024 bytes array.

I don't understand that, if I get an event of CHN_INPUT_AVAILABLE, why chnRead is blocked?

thx,
Zamek

Re: event driven serial, event is late

Posted: Sun May 01, 2016 8:52 am
by Giovanni
Because it tries to read 1024 bytes but there are less than that in the buffer, so it waits.

Use the read function with timeout set to TIME_IMMEDIATE, this is the non-blocking variant.

Giovanni

Re: event driven serial, event is late

Posted: Mon May 02, 2016 7:05 am
by zamek
Yes, you are right, it works with TIME_IMMEDIATE.
Unfortunately there is a new problem:

I sent 64 byte ordered data. When I get a CHN_AVAILABLE event I can read about 0x18-0x1a bytes from serial port. It is coming correct order. After I finished the first part of data reading, I get the next event and I can read the remaining part of data, where the second character of data is missed. For example the first part finished with 0x1a, the remaining part beginning with 0x1b, 0x1d and so on. Everything are right order except that the second character is missed of remaining part. Maybe it is a synchronization problem?

thx
Zamek

Re: event driven serial, event is late

Posted: Mon May 02, 2016 8:16 am
by Giovanni
Make sure to read all data from the queue when you get an event. I cannot possibly imagine what the problem is without hands on a debugger.

Queues are very old code, I find unlikely a problem there.

Giovanni

Re: event driven serial, event is late

Posted: Tue May 03, 2016 7:29 pm
by zamek
Hi Giovanni,

I found my mistake, everything is working well:)

Thank you for your patient,

Zamek