2018-06-20 01:08 AM
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-callbackSolved! Go to Solution.
2018-06-20 01:29 AM
Change your flag declaration from:
UINT8
flag_u8
= FALSE;
to:
volatile UINT8
flag_u8
= FALSE;
2018-06-20 01:29 AM
Change your flag declaration from:
UINT8
flag_u8
= FALSE;
to:
volatile UINT8
flag_u8
= FALSE;
2018-06-20 03:37 AM
Thank you so much. It works
2018-06-20 04:44 AM
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