Page 1 of 1
Issues with Serial and Event
Posted: Sun Dec 02, 2012 9:09 pm
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.
Re: Issues with Serial and Event
Posted: Sun Dec 02, 2012 9:19 pm
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
Re: Issues with Serial and Event
Posted: Sun Dec 02, 2012 9:31 pm
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.
Re: Issues with Serial and Event
Posted: Sun Dec 02, 2012 10:06 pm
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
Re: Issues with Serial and Event
Posted: Sun Dec 02, 2012 10:24 pm
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
Re: Issues with Serial and Event
Posted: Sun Dec 02, 2012 11:35 pm
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
Re: Issues with Serial and Event
Posted: Mon Dec 03, 2012 9:07 am
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