SerialDriver protected by Semaphore output split

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

Moderator: RoccoMarco

Post Reply
enly1
Posts: 38
Joined: Wed Oct 09, 2013 10:21 pm

SerialDriver protected by Semaphore output split

Post by enly1 »

Hi All,

Anyone have any ideas why I'm getting this issue ?

Short issue statement:
When writing to SD3 from 2 threads (main+worker) ( with binarySemaphore protecting access to SD3) the last byte of the previous string gets delayed until after the complete string of the second write.

PS - Please note I mention BinarySemaphore here, but actually use normal Semaphores(chSemInit(&logSem,1);

Longer Details:
I've a worker thread thats busy reading serial input from one SerialDriver (SD2), scanning for CRLF and when found, claims a binarySemaphore, chprintfs the output to another SerialDriver (SD3) and then continues on as normal.
I have a loop running in main that every 100MS it also creates some content to stream out to SD3 after claiming the same binarySemaphore.

Now the logic works fine (I know its not the right way to do this in terms of blocking the reading while writing - but ignore that for now).

The issue I get is that the last byte of the worker thread output seems to be buffered until the main loop output has completed, then it gets sent. But given the worker thread issues chprintf while holding the semaphore surely this shouldn't happen as it should block or fill the outputqueue of the serialdriver before main is able to claim the Semaphore. Operating without the Semaphore gives random blended output as you would expect.

Thread Priority seems to have some impact on this happening as well, but i'm not sure why it should have any impact given the Semaphore is controlling the ability to queue data for the serial output.

The attached png shows the serial byte sequence received including the CR/LF being split by the $RC2 string output. You can clearly see the symptom if you look at the $RC2 line in the middle of the image - the CR from the pervious semaphore protected write is there, then the $RC2 string, then the delayed LF from the previous write ).

The code in the worker thread looks like this:

Code: Select all

chSemWait(&logSem);
chprintf((BaseSequentialStream *)&SD3,"%s",nmea_buf);
chSemSignal(&logSem);


The code in main looks like this:

Code: Select all

chSemWait(&logSem);
sendRC2Data((BaseSequentialStream*) &SD3, &sensorData, RC2_DT | RC2_ALL_DX | RC2_ALL_AX );
chSemSignal(&logSem);


The code in the function sendRC2Data is quite long, but the usart output command used at the end is

Code: Select all

// Add checksummarker
p+=chsnprintf((char *)&rc2Buffer[p], 7, "%02x\r\n", checksum);

// Stream rc2Buffer to stream (usually serial port)
chprintf(stream,"%s",(char *)&rc2Buffer[0]);


So - anybody any ideas why my serial data output isn't in sequence.

Thanks in advance. Probably some newby mistake but seems strange unless the IO queues are operating with priority, but then I would expect it to appear in the middle of strings as well, but it never does.
Attachments
Broken CRLF from semaphore proected chprintf calls
Broken CRLF from semaphore proected chprintf calls
serial.PNG (13.83 KiB) Viewed 3234 times
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SerialDriver protected by Semaphore output split

Post by Giovanni »

Are you using a version older than 2.6.5? if so you are probably hitting a bug in serial driver fixed in 2.6.5.
Alternatively, is your semaphore initialized to 1?

Giovanni
enly1
Posts: 38
Joined: Wed Oct 09, 2013 10:21 pm

Re: SerialDriver protected by Semaphore output split

Post by enly1 »

Hi - I'm using Chibistudio Preview - latest, so yes, 2.6.4 (but I think it may have been an idiot coding issue - not 100% sure just yet, but have managed to make it work :-)).
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SerialDriver protected by Semaphore output split

Post by Giovanni »

Please update to 2.6.5, just replace the chibios directory with the new one.

Giovanni
Post Reply