cancel
Showing results for 
Search instead for 
Did you mean: 

FreeRTOS waiting flag in task

ismail fatih iltar
Associate II
Posted on June 20, 2018 at 10:08

Hi, i have uart comm. task. Its priority is osPriorityRealtime(+3)  in task i send datas with interrupt method and in txcallback i change the flag. in data_send im waiting for flag with while. But my program hanging on while. In debug it enteres 

HAL_UART_TxCpltCallback function and sets flag to TRUE but data_send function still wait in while. When i changed

while(

flag_u8

== FALSE); 

with osDelay(10); it works. Is it about rtos , task priorty or something like that? 

UINT8

flag_u8

= FALSE;

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

      if(huart == &huart1)

      {

            flag_u8= TRUE;

      }

}

void data_send(uint8_t*

main_buffer

, test_t* test, uint16_t size)

{

      flag_u8

= FALSE;

      MX_USART1_UART_Init2();                                                   //change baudrate to 90

      HAL_UART_Transmit_IT(&huart1,test->test_buffer,1);

      while(

flag_u8

== FALSE);

      

flag_u8

= FALSE;

      MX_USART1_UART_Init();                                                   

//change baudrate  to 250

      HAL_UART_Transmit_IT(&huart1, main_buffer,size+1);

      while(

flag_u8

== FALSE);

}

#freertos #stm32-uart-interrupts #uart-callback
1 ACCEPTED SOLUTION

Accepted Solutions
aa bb_2
Associate II
Posted on June 20, 2018 at 10:29

Change your flag declaration from: 

UINT8

flag_u8

= FALSE;

to: 

volatile UINT8

flag_u8

= FALSE;

View solution in original post

3 REPLIES 3
aa bb_2
Associate II
Posted on June 20, 2018 at 10:29

Change your flag declaration from: 

UINT8

flag_u8

= FALSE;

to: 

volatile UINT8

flag_u8

= FALSE;

Posted on June 20, 2018 at 10:37

Thank you so much. It works

Tilen MAJERLE
ST Employee
Posted on June 20, 2018 at 13:44

Hello,

instead of using while loop and waste CPU resources, check for semaphores. When you want to send data over UART, lock the semaphore, start transfer and wait for semaphore to be unlocked. Simply unlock semaphore in TX complete callback.

You will benefit in CPU resources and your system will be able to take care of other tasks while you are sending data (this is not the case now), in case you use preemptive approach. While you are waiting for semaphore, thread will go to blocked state and other threads will get executed in the meantime.

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
 if (huart == &huart1) {
 osSemaphoreRelease(uart1_sem);
 }
}
 
void data_send(uint8_t* main_buffer, test_t* test, uint16_t size)
{
 //At this point I assume you have uart1_sem valid semaphore returned by osSemaphoreCreate with single token (binary semaphore!)
 osSemaphoreWait(uart1_sem, 1000); //Lock semaphore
 MX_USART1_UART_Init2(); //change baudrate to 90
 HAL_UART_Transmit_IT(&huart1,test->test_buffer,1);
 osSemaphoreWait(uart1_sem, 1000); //Lock it again
 MX_USART1_UART_Init(); //change baudrate to 250
 HAL_UART_Transmit_IT(&huart1, main_buffer,size+1);
 osSemaphoreWait(uart1_sem, 1000); //Try to lock, it will block until data received
 osSemaphoreRelease(uart1_sem); //Manually release sem.
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Best regards,

Tilen