Search code examples
interruptstm32uartfreertosstm32-hal

UART Transmit failing after UART Receive thread starts in STM32 HAL Library


I am using STM32F1 (STM32F103C8T6) in order to develop a project using FreeRTOS.

The following is my GPIO and USART1 interface configuration:

    __GPIOA_CLK_ENABLE();
    __USART1_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600;//115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  HAL_UART_Init(&huart1);

  HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART1_IRQn);

The question is: Why does UART transmit work before threads start but not after threads started or from threads? I want to transmit data from threads. i.e

int main(void)
{
     Initializations();

     //THIS WORKS!!
     uart_transmit_buffer[0] = 'H';
     uart_transmit_buffer[1] = 'R';
     uart_transmit_buffer[2] = '#';
     uint8_t nums_in_tr_buf = 0;
     nums_in_tr_buf = sizeof(uart_transmit_buffer)/sizeof(uint8_t);
     state = HAL_UART_Transmit(&huart1, uart_transmit_buffer, nums_in_tr_buf, 5000);

     StartAllThreads();
     osKernelStart();

     for (;;);
}

static void A_Random_Thread(void const *argument)
{
      for(;;)
      {
         if (conditionsMet())  //Executed once when a proper response received.
         {
            //BUT NOT THIS :(!!
            uart_transmit_buffer[0] = 'H';
            uart_transmit_buffer[1] = 'R';
            uart_transmit_buffer[2] = '#';
            uint8_t nums_in_tr_buf = 0;
            nums_in_tr_buf = sizeof(uart_transmit_buffer)/sizeof(uint8_t);
            state = HAL_UART_Transmit(&huart1, uart_transmit_buffer, nums_in_tr_buf, 5000);
        }
      }
}

I have made sure no thread is in deadlock. The problem is UART_HAL_Transmit gives HAL_BUSY state.

Furthermore, I have dedicated one thread to receiving and parsing information from UART RX and I suspect this might be a cause of the problem. The following is the code:

static void UART_Receive_Thread(void const *argument)
{
  uint32_t count;
  (void) argument;
    int j = 0, word_length = 0;

  for (;;)
  {
            if (uart_line_ready == 0)
            {
                    HAL_UART_Receive(&huart1, uart_receive_buffer, UART_RX_BUFFER_SIZE, 0xFFFF);
                    if (uart_receive_buffer[0] != 0)
                    {
                            if (uart_receive_buffer[0] != END_OF_WORD_CHAR)
                            {
                                    uart_line_buffer[k] = uart_receive_buffer[0];
                                    uart_receive_buffer[0] = 0;
                                    k++;
                            }
                            else
                            {
                                    uart_receive_buffer[0] = 0;
                                    uart_line_ready = 1;
                                    word_length = k;
                                    k = 0;
                            }
                    }
            }
            if (uart_line_ready == 1)
            {
                    //osThreadSuspend(OLEDThreadHandle);
                    for (j = 0; j <= word_length; j++)
                    {
                            UART_RECEIVED_COMMAND[j] = uart_line_buffer[j];
                    }
                    for (j = 0; j <= word_length; j++)
                    {
                            uart_line_buffer[j] = 0;
                    }
                    uart_line_ready = 0;

                    RECEIVED_COMMAND = ParseReceivedCommand(UART_RECEIVED_COMMAND);
                    if (RECEIVED_COMMAND != _ID_)
                    {
                            AssignReceivedData (word_length);  //Results in uint8_t * RECEIVED_DATA
                    }
                    //osThreadResume(OLEDThreadHandle);
            }
          //Should be no delay in order not to miss any data..
  }
}

Another cause to the problem I suspect could be related to interrupts of the system (Also please notice initialization part, I configured NVIC):

void USART1_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart1);
}

Any help or guidance to this issue would be highly appreciated. Thanks in advance.


Solution

  • The problem turned out to be something to do with blocking statements. Since UART_Receive_Thread has HAL_UART_Receive inside and that is blocking the thread until something is received, that results in a busy HAL (hence, the HAL_BUSY state).

    The solution was using non-blocking statements without changing anything else. i.e. using HAL_UART_Receive_IT and HAL_UART_Transmit_IT at the same time and ignoring blocking statements worked.

    Thanks for all suggestions that lead to this solution.