Hi,
I have a synchronization problem. There are two tasks, say A and B, which both use the same UART for receiving and transmitting to a bluetooth module. Particularly, task A only transmits information, task B receives and transmits. The bluetooth module is meant to connect to a remote device, i.e. a tablet. So task B receive some commands from the tablet, as an ack transmit their echo, then changes the state of task A by properly setting a variable, say taskSts, to a given value. Following, task A is supposed to send other information to the tablet as a consequence of the new value of taskSts. Task A collects these information from another task, say task C, via a queue.
Since task A and task B access the same UART, in order to avoid collisions, I have protected each I/O operation related to task A and B with a mutex. That is, in task A there is
:::c
osMutexWait(uartMutexId, osWaitForever);
HAL_UART_Transmit_IT(&UartHandle, (uint8_t *)&btTx, sizeof(btTx));
osMutexRelease(uartMutexId);
where
:::c
osWaitForever = 0xFFFFFFFF,
and in task B there is something like
:::c
HAL_UART_Receive_IT(&UartHandle, &btRx, 5);
/* some code... */
if(buffCmp("START", &btRxBuffer))
{
osMutexWait(uartMutexId, osWaitForever);
HAL_UART_Transmit_IT(&UartHandle, (uint8_t *)"STARTn", sizeof("STARTn"));
/* some other code... */
osMutexRelease(uartMutexId);
btTaskSts = RUNNING;
}.
Notice that I am using the CMSIS-RTOS API
:::c
osMutexWait(mutex_id, ticks),
and
:::c
osMutexRelease(mutex_id),
provided by ST within STM32Cube. Such APIs wrap
:::c
xSemaphoreTake(mutex_id, ticks),
and
:::c
xSemaphoreGive(mutex_id),
respectively.
It happens that when I increase the speed which task C sends the information to task A to, task B never manages to take the mutex, waiting for it to be released by task A.
The first solution which comes in my mind is not to “wait forever”. But I am not aware of all the things that such choice would imply. Since both task A and B have, at this stage, the same priority, another solutions would be making task B priority higher than that of task A. However I am not an expert so I am wondering what is the best choice.
Thank you and regards,
vm