Semaphore

k3nt00 wrote on Thursday, May 21, 2015:

Hello,

I am running FreeRTOS 8.2.1 on an STM32F103 with the newest STM32F1 Hal Driver

I had been having some issues with the interrupt UART Tx beginning a new transmission before the one before it completed. I call the interrupt transmit and then wait on a semaphore from the callback to be given.


if(HAL_UART_Transmit_IT(&huart1, (uint8_t*) outBuffer, dataSize) != HAL_OK)
{
    Error_Handler();
}
   
xSemaphoreTake(waitUntilSendDone, portMAX_DELAY);

The Callback:


void HAL_UART_TxCpltCallback (UART_HandleTypeDef *UartHandle)
{
    xHigherPriorityTaskWoken = pdFALSE;
    xSemaphoreGiveFromISR(waitUntilSendDone, &xHigherPriorityTaskWoken);
}

The problem is, the callback was being called after only 2 bytes had been transmitted, which allowed for the second transmit buffer to be sent prematurely.

The ST Examples use the following:


if(HAL_UART_Transmit_IT(&huart1, (uint8_t*) outBuffer, dataSize) != HAL_OK)
{
    Error_Handler();
}
   
   while (UartReady != SET)
   {
   }

   /* Reset transmission flag */
   UartReady = RESET;

And the Callback:


void HAL_UART_TxCpltCallback (UART_HandleTypeDef *UartHandle)
{
   /* Set transmission flag: transfer complete */
   UartReady = SET;
}

This solution solved the problem. The callback wasn’t called until the first buffer was completely transmitted. I do not understand why this is? Why is the method of waiting effecting the callback?

Any insight would be much appreciated.

Thanks

PS Posted this prematurely without proper title. Apologies.

heinbali01 wrote on Friday, May 22, 2015:

Hello,

Your HAL_UART_TxCpltCallback() is calling xSemaphoreGiveFromISR() but what I miss is a call to portYIELD_FROM_ISR().

In other words: the semaphore will be set, the task waiting for the semaphore becomes runnable but it wakes up much later than expected.

It is a pity that the entire ISR handling is done within the STM32 library:

void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
{
    ...
    /* Transfer complete callback */
    hdma->XferCpltCallback(hdma);
    ....
}

which calls:

static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
{
    HAL_UART_TxCpltCallback(huart);
}

Once I made a minor change in the STM32 library. I had the same problem with the SD-card: once a transfer is ready I wanted to set a semaphore and unblock some process.

/* A global variable that you can set from within a TX call-back */
BaseType_t xDMA_IRQ_HigherPriorityTaskWoken;

void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
{
    xDMA_IRQ_HigherPriorityTaskWoken = pdFALSE;
    ...
    /* Transfer complete callback */
    hdma->XferCpltCallback(hdma);
    ....
    if( xDMA_IRQ_HigherPriorityTaskWoken != pdFALSE )
    {
        portYIELD_FROM_ISR( xDMA_IRQ_HigherPriorityTaskWoken );
    }
}

PS. here was an earlier post about using ‘portYIELD_FROM_ISR()’ :

https://sourceforge.net/p/freertos/discussion/382005/thread/7217ae20

Regards.