2022-11-25 06:28 AM
Hi all i am working in interrupt driven uart uart2 which is connected to the usb port in nucleo -g431rb The interrupt handler is not been called I have shared my code any thoughts kindly share.
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
uint32_t isrflags = READ_REG(huart2.Instance->ISR);
uint32_t cr1its = READ_REG(huart2.Instance->CR1);
if (((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
{
ch = huart2.Instance->RDR;
HAL_UART_Transmit(&huart2, &ch, 1, 100);
}
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
/* USER CODE END USART2_IRQn 1 */
}i
nt main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_UART4_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint8_t data[50] = "interupt uart testing\r\n";
HAL_UART_Transmit (&huart2, data, sizeof (data), 100);
while (1)
{
// HAL_UART_Transmit (&huart1, data, sizeof (data), 10);
if(ch == 'a')
{
HAL_UART_Transmit (&huart2, data, sizeof (data), 100);
ch = 0;
}
// HAL_UART_Transmit (&huart3, data, sizeof (data), 10);
// HAL_UART_Transmit (&huart4, data, sizeof (data), 10);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
2022-11-25 09:55 AM
Have you enabled the RXNE interrupt in UART and UART interrupt in NVIC?
HAL_UART interrupt routine will interfere with your interrupt handling. Maybe you should simply not cal it at all.
sizeof(data) is 50. The string should be defined as const char str [], then its size would be sizeof(str) - 1. ch variable must be volatile.
2022-11-25 04:48 PM
Think why the HAL has functions ending with ***_IT() and ***_DMA().
And learn splitting thoughts into sentences...
2022-11-28 05:47 AM
Thankyou for the reply I got the solution I need to add the below line in the uart init funciton.
"USART2->CR1 |= (USART_CR1_TE | USART_CR1_RXNEIE | USART_CR1_RE | USART_CR1_UE);"
2022-11-28 05:47 AM
Thankyou for the reply..
2022-11-28 03:42 PM
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_UART4_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
EnableUART_Interrupt();
uint8_t data[50] = "interupt uart testing\r\n";
HAL_UART_Transmit (&huart2, data, sizeof (data), 100);
while (1)
{
if(GetUartEnErrorFlag())
{
ClearUartEnErrorFlag();
EnableUART_Interrupt();
}
// HAL_UART_Transmit (&huart1, data, sizeof (data), 10);
if(ch == 'a')
{
HAL_UART_Transmit (&huart2, data, sizeof (data), 100);
ch = 0;
}
// HAL_UART_Transmit (&huart3, data, sizeof (data), 10);
// HAL_UART_Transmit (&huart4, data, sizeof (data), 10);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void EnableUART_Interrupt(void)
{
if(HAL_UART_Receive_IT(&huart2, ch, 1) != HAL_OK)
{
UART_RxEnErrorFlag = true; // set error flag
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == huart2.Instance) // check if UART2 caused interrupt
{
...// do something with data in ch variable. Typically save byte to a ring buffer for polling in main. Also ch holds the data already so no need to copy from DR register to ch variable.
EnableUART_Interrupt(); // re-enable interrupt
}
}
bool GetUartEnErrorFlag(void)
{
return UART_RxEnErrorFlag;
}
void ClearUartEnErrorFlag(void)
{
UART_RxEnErrorFlag = false;
}
2022-11-28 08:56 PM
Thankyou for the effort you have put I will implement the missed thing in my code. Thankyou..
2022-11-29 08:17 AM
Also for anyone wondering, when you call HAL_UART_Receive_IT(&huart2, ch, 1), you're telling HAL driver to save the incoming data to the ch variable. When HAL_UART_RxCpltCallback is called, ch holds the new data.