PlatformIO Community

Infinite loop when using UART interrupts

Hi there,
I’m new to STM32 and I’ve been struggling with this for days and can’t find a solution. In the UART configuration, I miss something to activate the receiving of a character through interrupts. I am using the code generated by CubeMX in Platformio IDE (arduino framework). UART works fine in blocking mode, but when trying to receive a char through interrupts, it runs into infinite loop. Trying to debug and check registers but it’s difficult to analyze for me…
Thank you for any help.

main.cpp:

#include "main.h"
#include <Arduino.h>

#include <cubemx_uart.h>  // konfiguracja UART
#include <cubemx_system.h>  // konfiguracja ogólna

unsigned long previousMillis = 0;  // do odliczania czasu

static void MX_NVIC_Init(void)
{
	HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);  // UART1
	HAL_NVIC_EnableIRQ(USART1_IRQn);  // UART1
}

void setup()
{
	HAL_Init();
	SystemClock_Config();

	MX_GPIO_Init();  // GPIO

	MX_NVIC_Init();
	MX_USART1_UART_Init();  // UART
	HAL_UART_MspInit(&huart1);
	HAL_UART_Receive_IT(&huart1, UART1_Rx_data, 1);
}

void loop()
{
	unsigned long currentMillis = millis();

	if (currentMillis - previousMillis > 1000) // odmierz czas 1 sekundę
	{
		previousMillis = currentMillis;

		uint8_t Test[] = "Test";
		HAL_UART_Transmit(&huart1, Test, sizeof(Test), 10);
	}
}

cubemx_uart.cpp:

#include <Arduino.h>
#include <cubemx_uart.h>

UART_HandleTypeDef huart1;
uint8_t UART1_Rx_data[10];

void MX_USART1_UART_Init(void)
{
	huart1.Instance = USART1;
	huart1.Init.BaudRate = 115200;
	huart1.Init.WordLength = UART_WORDLENGTH_8B;
	huart1.Init.StopBits = UART_STOPBITS_1;
	huart1.Init.Parity = UART_PARITY_NONE;
	huart1.Init.Mode = UART_MODE_TX_RX;
	huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart1.Init.OverSampling = UART_OVERSAMPLING_16;
	huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
	huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
	huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
	huart1.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;

	if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); }
	if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); }
	if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); }
	if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) { Error_Handler(); }

	__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_ERR);
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXFF);
}

void HAL_MspInit(void)
{
	__HAL_RCC_SYSCFG_CLK_ENABLE();
	__HAL_RCC_PWR_CLK_ENABLE();
}

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

	if (huart -> Instance == USART1)
	{
		PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
		PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;

		if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); }

		__HAL_RCC_USART1_CLK_ENABLE();
		__HAL_RCC_GPIOC_CLK_ENABLE();  // PC4 -> USART1_TX, PC5 -> USART1_RX

		GPIO_InitStruct.Pin = UART1_TX_Pin|UART1_RX_Pin;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
		GPIO_InitStruct.Alternate = GPIO_AF1_USART1;

		HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

		USART1->CR1 |= (1<<2);  // (1<<RE)  Receive Enable
	}
	else if (huart -> Instance == USART2)
	{
	}
}

void USART1_IRQHandler(void)  // obsługa przerwania
{
	led = true;

	HAL_UART_IRQHandler(&huart1);
	HAL_UART_Receive_IT(&huart1, UART1_Rx_data, 1);  // rozpocznij nasłuch
}

cubemx_system.cpp:

void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /* Configure the main internal regulator output voltage */
    HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

    /* Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef structure. */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.LSIState = RCC_LSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); }

    /* Initializes the CPU, AHB and APB buses clocks */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); }

    HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_LSI, RCC_MCODIV_1);
}