2021-02-19 01:53 AM
I am using STM32F072RB.
I have enable the FreeRTOS CMSIS1.
I have created two tasks which uses the same UART for transmitting data.
I want to use the semaphore concept to use this shared resource between two task.
The task as belows:
TASK 1 High priority
void StartTask02(void const * argument)
{
/* USER CODE BEGIN StartTask02 */
/* Infinite loop */
for(;;)
{
uint8_t data[1];
data[0] = 65;
HAL_UART_Transmit_IT(&huart1, &data, 1);
}
/* USER CODE END StartTask02 */
}
Task 2 Low priority
void StartTask03(void const * argument)
{
/* USER CODE BEGIN StartTask03 */
/* Infinite loop */
for(;;)
{
osSemaphoreWait (myBinarySem01Handle, 100);
uint8_t data[1];
data[0] = 66;
HAL_UART_Transmit_IT(&huart1, &data, 1);
osSemaphoreRelease (myBinarySem01Handle);
}
/* USER CODE END StartTask03 */
}
I tried to use the semaphore, but didnt get the proper result. I continuously getting data from task1 even if I used semaphore in task 2.
Kindly correct me if anything is wrong....
2021-02-19 02:49 AM
There are few problems in your code. First you have to check semaphore has been well acquired before you continue. So check the return of Wait function if it was OK, otherwise try to acquire it again.
Then, HAL_UART_Transmit_IT is a non-blocking function, means that when function returns, data haven't been yet transmitted. You should unlock your semaphore in the HAL TX completed callback or add a thread delay before unlocking it.
HAL_UART_Transmit_IT accepts array pointer, which is not the case what you are passing. (&data is not address of first element).
Best regards, TIlen