I am trying to read NMEA data from a GPS using serial driver 2 on the STM32F4 Discovery.
My event listener thread looks like this:
Code: Select all
tatic msg_t Thread6(void *arg)
{
(void)arg;
chRegSetThreadName("GPSdata");
static char charbuf; // character buffer
// Activates the Serial driver 2, PA2(TX) and PA3(RX) are routed to USART2, 9600 8N1.
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
sdStart(&SD2, NULL);
EventListener elGPSdata;
flagsmask_t flags;
chEvtRegisterMask((EventSource *)chnGetEventSource(&SD2), &elGPSdata, EVENT_MASK(1));
while (TRUE)
{
chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
chSysLock();
flags = chEvtGetAndClearFlags(&elGPSdata);
chSysUnlock();
if (flags & CHN_INPUT_AVAILABLE)
{
while ( charbuf != Q_TIMEOUT )
{
charbuf = chnGetTimeout(&SD2, TIME_IMMEDIATE);
chprintf((BaseSequentialStream*)&SDU1, "%c\n", charbuf);
}
}
}
return 0;
}
I have increased the serial buffer size to 560 bytes (longest sentence is 75 characters, 7 sentences) but when printing the flags variable I still get 128 in the flags, which I believe indicates an overrun error.
In another Thread Giovanni suggested reading the data like this:
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.
Where will CHN_INPUT_AVAILABLE be stored in? Still in the flags variable?
Additionally, which events does the event listener react to? Only when there is input data or also on errors?
Thank you!
Elia