err
err

Reputation: 75

STM32 HAL_UART_Transmit_IT never returns

I have created a project using cube mx and want to use the uart4 tx and rx to send and receive bytes in interrupt mode.

I have :

uint8_t buffer[] = "test\r\n";

if(HAL_UART_Transmit_IT(&huart4, (uint8_t*)buffer, strlen(buffer))!= HAL_OK)
{

}

The uart initialisation is

static void MX_UART4_Init(void)
{
    huart4.Instance = UART4;
    huart4.Init.BaudRate = 9600;
    huart4.Init.WordLength = UART_WORDLENGTH_8B;
    huart4.Init.StopBits = UART_STOPBITS_1;
    huart4.Init.Parity = UART_PARITY_NONE;
    huart4.Init.Mode = UART_MODE_TX_RX;
    huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart4.Init.OverSampling = UART_OVERSAMPLING_16;
    huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&huart4) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
}

The call to Transmit never returns and just sits there.

In the msp file I have

HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART4_IRQn);

and have in the it file

void UART4_IRQHandler(void)
{
    /* USER CODE BEGIN UART4_IRQn 0 */

    /* USER CODE END UART4_IRQn 0 */
    HAL_UART_IRQHandler(&huart4);
    /* USER CODE BEGIN UART4_IRQn 1 */

    /* USER CODE END UART4_IRQn 1 */
}

what am I missing?

Upvotes: 0

Views: 4498

Answers (2)

Alireza Niknam
Alireza Niknam

Reputation: 21

the problem is related to uint8_t buffer[] = "test\r\n"; the right side is char and the left side is uint8. So, you must do as follows

char buffer[] = "test\r\n";
if(HAL_UART_Transmit_IT(&huart4, (char*)buffer, strlen(buffer))!= HAL_OK)
{

}

or you can do this (use sizeof for int8_t not strlen)

unit8_t buffer[] = "test\r\n";
if(HAL_UART_Transmit_IT(&huart4, (uint8_t*)buffer, sizeof(buffer))!= HAL_OK)
{

}

please add "string.h" and in the first line of you code

Upvotes: 2

err
err

Reputation: 75

Adding a delay too allow the data to be sent fixed this problem. HAL_Delay(100)

Upvotes: 1

Related Questions