Issues with Serial and Event

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
belmondo2002us
Posts: 3
Joined: Fri Nov 30, 2012 5:02 am

Issues with Serial and Event

Post by belmondo2002us »

I am having issues with the serial buffer and event listening. There are not many examples on either topics. So I am not sure what is going wrong.

I am trying to monitor the events of the SerialDriver SD2. I have created a thread with the code below:

static msg_t Uart1EVT_Thread(void *p) {
char tk[80];
EventListener el1;

chEvtRegisterMask((EventSource *)&SD2, (EventListener *)&el1, ALL_EVENTS);
while (TRUE) {
if ( (m = chEvtWaitAnyTimeout(ALL_EVENTS,TIME_IMMEDIATE)) != 0 ) {
sprintf(tk,"UART Event %d", (uint16_t) m);
println(&SD2,tk);
}
}
}

But I am getting no event generated. I set up breakpoints in the serial_lld.c and it indeed stops in the set_error function and go through the chAddFlagsI routine but the event is not reaching my thread.

I must be doing something wrong in my thread but not sure what.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Issues with Serial and Event

Post by Giovanni »

Hi,

The problem is that SD2 is the structure representing the driver but it is not the actual event source, in order to get the event source do: chnGetEventSource(&SD2), no cast required.

Giovanni
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

Re: Issues with Serial and Event

Post by skute »

There are a few things going wrong. Most importantly is that you aren't listening to an event source, but rather the SerialDriver instance itself. You should try doing something like this:

Code: Select all

static msg_t Uart1EVT_Thread(void *p) {
   
    char tk[80];
    EventListener el1;
    flagmask_t flags;

    chEvtRegisterMask((EventSource *)chnGetEventSource(&SD2), &el1, 1);
    while (TRUE) {
        chEvtWaitOne(1);

        chSysLock();
        flags = chEvtGetAndClearFlagsI(&el1);
        chSysUnlock();

        sprintf(tk, "UART Event %d", (uint16_t)flags);
        println(&SD2, tk);
    }
}


The second "issue" is that you are polling for events and not taking advantage of the OS to suspend the thread and resume it when the event source signals.
belmondo2002us
Posts: 3
Joined: Fri Nov 30, 2012 5:02 am

Re: Issues with Serial and Event

Post by belmondo2002us »

Thank you both for your prompt response. I am using 2.5.1 and flagmask_t does not seem to be supported anymore.

So I have adjust the code to get the eventmask_t with

eventmask_t m = chEvtWaitOne(ALL_EVENTS);

Doing this, I get the UART sending me many events even what seems to be non documented events:

UART Event 1
UART Event 2
UART Event 4
UART Event 8
UART Event 16
UART Event 32
UART Event 64
UART Event 128
UART Event 256
UART Event 512
UART Event 1024
UART Event 2048
UART Event 4096
UART Event 8192
UART Event 16384
UART Event 32768
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 0
UART Event 1
UART Event 2
UART Event 4
UART Event 8
UART Event 16
UART Event 32
UART Event 64
UART Event 128
UART Event 256
UART Event 512

Is this normal ?

Also the UART get stuck at one point of time and block the main thread. It seems to be calling the set_error subroutine all the time with a flag of 0x01E0

Any idea what could be the issue ?

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

Re: Issues with Serial and Event

Post by Giovanni »

You are seeing a bit that is shifted to left one position each time and casted to 16 bits so you see those 16 zeros before it restarts from LSb.

I think it is not the serial driver doing this, there is something strange going on, could you post the complete code?

About the flagmask, it is no more part of the serial driver but now it is an integral part of events, if you do chEvtGetAndClearFlagsI() on the event listener connected to the serial driver then you get the same flags mask.

Giovanni
belmondo2002us
Posts: 3
Joined: Fri Nov 30, 2012 5:02 am

Re: Issues with Serial and Event

Post by belmondo2002us »

Hi Giovanni,

I seem to get the right events when using chEvtGetAndClearFlagsI - Thanks.

The issue I am having is that I am sending a lot of data over the serial port and it just stops working after a while. The code gets stuck when sending data (not receiving).

My code has some proprietary stuff in it. Is there a way I can send it to you only without posting it on the forum ?

Tx

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

Re: Issues with Serial and Event

Post by Giovanni »

You could send it via email but I would be able to run it without hardware.

About events, if you are operating a serial port in a event driven system please make sure to:
1) Waiting for events is the only wait you do in your listener thread.
2) When data becomes available make sure to empty the input queue using non blocking read functions (TIME_IMMEDIATE). If you don't empty the queue then some data may remain unread until the next event is broadcasted.
3) Serve all the possible flags from the serial driver.
4) Use timeouts in order to recover from protocol errors (received bytes can be corrupted or missing).

Giovanni
Post Reply