Working with Receive Timeout(RTOIE) and RXNEIE simultaneously
Hi,
Board details : NUCLEO64 - G071RBTX
I am trying to understand Receive timeout interrupt for implementation of Modbus RTU using USART2. As a start, to check the receive timeout interrupt, i want to check with a 2 seconds timeout interrupt. So configured the RTOR register accordingly below (Please guide if i configured wrong)
===
USART2 CONFIGURATIONS:
BAUD=19200
All other bits at default
===
Steps I used additionally after code generation (HAL) Below steps are written after HAL configures the USART2.
- Enabled RXNEIE interrupt
- Enabled RTOIE interrupt
- Enabled Receive timeout (USART2->RTOEN bit is set)
- Configured RTOR register with value 3840002 (To see interrupt of after 2 seconds)
Calculation for RTOR = (2000000/ 0.520833). I refered the calculation from here
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 19200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
//By default Receive interrupt is not enabled
//Enable Receive interrupt and receive timeout of USART2
USART2->CR1 |=(1<<26U) | (1<<5U);
USART2->CR2 |=(1<<23U); //RECEIVER TIMEOUT ENABLE
USART2->RTOR=3840002u;
/* USER CODE END USART2_Init 2 */
}Finally in USART2 IRQ handler below is the code:
One more thing in the IRQ handler, i checked with each bit. Is this the correct method?
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
if((USART2->ISR && 32u))
{
unsigned char test=USART2->RDR;
//USART2->ICR |=(1<<11u); //CLEAR RTOF BIT
}
else if ((USART2->ISR && 2048u))
{
//ENTERED RTOF
unsigned char tester;
tester=0u;
USART2->ICR |=(1<<11u); //CLEAR RTOF BIT
}
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
/* USER CODE END USART2_IRQn 1 */
}Interrupt does not occur after about 2 seconds. Please guide me.
