cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive versus HAL_UART_Receive_IT

mvonahnen
Associate III
Posted on February 06, 2017 at 21:21

I am using a Nucleo-L476RG board and I am trying to use the HAL_UART_Receive_IT function to receive a byte at a time from a RS-232 source (4800 Baud) on UART 4.  I need to use the interrupt because I am transmitting out on UART3 console data and I miss characters when I transmit long strings.  But I always get a framing error when I call the my receive function:

UART4_status = HAL_UART_Receive_IT(&huart4,(uint8_t *)rx_data, 1);

I don't see any framing errors when I poll for the data:

UART4_status = HAL_UART_Receive(&huart4,(

uint8_t

*)rx_data, 1, 1000);

Here the initialization of the UART generated by CubeMX:

/* UART4

init

function */

static

void

MX_UART4_Init(

void

)

{

huart4.

Instance

= UART4;

huart4.

Init

.

BaudRate

= 4800;

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();

}

}

Before I try to receive data, I enable the RXNE interrupt:

__HAL_UART_CLEAR_OREFLAG(&huart4);

__HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE);

I have attached the main.c.

Is there something I am missing on the interrupt initialization?

8 REPLIES 8
T J
Lead
Posted on February 06, 2017 at 21:54

Are you using RS485 half duplex ?

this works very well for TX with DMA

void CheckTxDMABufferProgress(void){ if ( DMABufHasData ){ char uartState = HAL_UART_GetState(&huart1); if ((uartState == HAL_UART_STATE_READY) || (uartState == HAL_UART_STATE_BUSY_RX)) { DMABufHasData = false; // sending now if (HAL_UART_Transmit_DMA(&huart1, (uint8_t *)Usart1TxDMABuffer + U1TxBufferPtrOUT ,U1TxBufferPtrIN - U1TxBufferPtrOUT) == HAL_OK){ HAL_UART_Transmit_DMA_Status = UartDMAsuccess; U1TxBufferPtrOUT = U1TxBufferPtrIN; } else{ Error_Handler(); /* Transfer error in transmission process */ HAL_UART_Transmit_DMA_Status = UartDMAfailed; } } }}�?�?�?�?�?�?�?

mvonahnen
Associate III
Posted on February 06, 2017 at 22:10

Thanks but I did not see any references to the interrupt routines in these web pages.  If you saw something in particular that might help could you indicate it?

Abhishek Kumar
Associate III
Posted on November 04, 2017 at 22:44

I am also facing the same problem, do u have the solution?

Joerg Wagner
Senior III
Posted on November 05, 2017 at 01:24

Hello.

Did you read the User Manual of the HAL drivers?

Receive an amount of data in non blocking mode using HAL_UART_Receive_IT().

At reception end of transfer HAL_UART_RxCpltCallback is executed and user can

add his own code by customization of function pointer HAL_UART_RxCpltCallback.

Your intention is to use the interrupt mechanism but you use a poll function to read.

Instead, define the Callback function i.e.:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

    if (huart==&huart4) {

       // do something with rx_data

      

       HAL_UART_Receive_IT(&huart4, &rx_data, 1);   // trigger the next read

    }

}

and call it once in main():

        HAL_UART_Receive_IT(&huart4, &rx_data, 1);

Everytime a character is available the callback function is called automatically.

Posted on April 17, 2018 at 15:16

Are you sure that we need place another “

HAL_UART_Receive_IT

� in the callback function? I don't see them in the examples given by ST.
Huzaifah Limbada
Associate II
Posted on April 18, 2018 at 08:55

To receive full string as you transfer you have to made changes into the register level code...here i have mention in which function or you can just do copy and paste with that function.

    static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)

    {

    uint16_t uhMask = huart->Mask;

    uint16_t uhdata;

    /* Check that a Rx process is ongoing */

    if(huart->RxState == HAL_UART_STATE_BUSY_RX)

    {

    uhdata = (uint16_t) READ_REG(huart->Instance->RDR);

    *huart->pRxBuffPtr++ = (uint8_t)(uhdata & (uint8_t)uhMask);

    HAL_UART_RxCpltCallback(huart);

    return HAL_OK;

    }

    else

    {

    /* Clear RXNE interrupt flag */

    __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);

    return HAL_BUSY;

    }

    }

You can find this function in to the 'stm32l0xx_hal_uart.c'

Hope you'll get your solution by this!!!

Posted on April 19, 2018 at 23:49

Are there examples not just turning on a LED?