2017-02-06 12:21 PM
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
voidMX_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?
2017-02-06 12:54 PM
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; } } }}�?�?�?�?�?�?�?
2017-02-06 01:02 PM
This might be useful:
http://embedded.fm/blog/2016/11/29/discovery-uarts-part-1
http://embedded.fm/blog/2016/12/6/discovery-uarts-part-2-connections
http://embedded.fm/blog/2016/12/13/discovery-uarts-part-3-the-final-step
2017-02-06 01:10 PM
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?
2017-11-04 02:44 PM
I am also facing the same problem, do u have the solution?
2017-11-04 05:24 PM
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.
2018-04-17 08:16 AM
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.2018-04-17 11:55 PM
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!!!
2018-04-19 04:49 PM
Are there examples not just turning on a LED?