event driven serial, event is late Topic is solved

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
zamek
Posts: 14
Joined: Sat Mar 23, 2013 6:39 pm

event driven serial, event is late

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: event driven serial, event is late

Post 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
zamek
Posts: 14
Joined: Sat Mar 23, 2013 6:39 pm

Re: event driven serial, event is late

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: event driven serial, event is late

Post 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
zamek
Posts: 14
Joined: Sat Mar 23, 2013 6:39 pm

Re: event driven serial, event is late

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: event driven serial, event is late

Post 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
zamek
Posts: 14
Joined: Sat Mar 23, 2013 6:39 pm

Re: event driven serial, event is late

Post by zamek »

Hi Giovanni,

I found my mistake, everything is working well:)

Thank you for your patient,

Zamek
Post Reply