I’ve got a simple RS232 library which I’m using for debug output but it doesn’t always do what I expect. I’ve created a function to initialise the port and within that I create a mutex to protect the resource. I have a number of threads, all of which have different priorities, and any of them are free to use the RS232 library calls. What I wanted to ensure was that if one thread is preempted by a higher priority thread whilst in the middle of sending a message, it’s able to finish its message before the higher priority one gets run.
Here is an over-simplified version of my code showing the mutex creation/calls…
static xSemaphoreHandle semRS232Tx = NULL; // mutex to protect the resource
// create the mutex which protects writes and ensures reentrancy
semRS232Tx = xSemaphoreCreateMutex();
if(NULL == semRS232Tx)
RS232_STATUS RS232WriteString(char *str)
if(pdFAIL == xSemaphoreTake(semRS232Tx, TIME_IN_MS(1000)))
// output character string here
Most of the time it works fine but occassionally I will get a higher priority thread message embedded in the middle of a low priority thread’s message and I don’t understand why. Can anybody see what I’ve done wrong?
P.S. Great little OS by the way. We looked at ThreadX which was going to cost us £4,000 just for the binaries and went with FreeRTOS in the end and it really has performed well :o)