Skip to main content
NJP.1
Associate III
May 3, 2022
Question

Interupt driven uart reception not working

  • May 3, 2022
  • 10 replies
  • 3919 views

Hi all iam working in stm32 f051c8tx microcontroller im trying to recieve a byte and if i recieve a byte in intrupt im printing "hello world " via tx of the same uart, the issue is im not able to recieve any char the below is the code any thoughts please let me know.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
 // HAL_UART_Transmit(&huart1, UART1_rxBuffer, 12, 100);
	INTERUPT_RX_1 = rx_data[0];
 HAL_UART_Receive_IT(&huart1, rx_data, 1);
}
 
 
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();
 
 memset(SSD1306_Buffer,0,strlen(SSD1306_Buffer));
 /* USER CODE BEGIN SysInit */
 
 /* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 
 MX_GPIO_Init();
 
 MX_I2C2_Init();
 
 MX_SPI1_Init();
 // MX_SPI2_Init();
 MX_TIM2_Init();
 MX_TIM3_Init();
 MX_USART1_UART_Init();
 MX_TIM6_Init();
 MX_ADC_Init();
 MX_TIM14_Init();
 
 fsm_sens_init();
 
 RetargetInit(&huart1);
 
 
 
 
 sensor_init(&sensor_cfg);
 
 HAL_UART_Receive_IT (&huart1, rx_data, 1);
 
 while(1)
 {
 
	 // printf("while \r\n");
	 if(INTERUPT_RX_1 != 0)
	 {
	 printf("while");
	 INTERUPT_RX_1 = 0;
	 }
 }
 
}
 
 
 
 
static void MX_USART1_UART_Init(void)
{
 
 /* USER CODE BEGIN USART1_Init 0 */
 
 /* USER CODE END USART1_Init 0 */
 
 /* USER CODE BEGIN USART1_Init 1 */
 
 /* USER CODE END USART1_Init 1 */
 huart1.Instance = USART1;
 huart1.Init.BaudRate =19200;
 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.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 if (HAL_UART_Init(&huart1) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN USART1_Init 2 */
 
 /* USER CODE END USART1_Init 2 */
 
}

This topic has been closed for replies.

10 replies

waclawek.jan
Super User
May 3, 2022

Have you enabled the interrupt in NVIC?

Are the Rx pin set correctly in GPIO?

Read out and check/post content of USART and relevant GPIO and perhaps NVIC registers.

Some general interrupt-does-not-fire hints here.

JW

NJP.1
NJP.1Author
Associate III
May 4, 2022

Hi Waclawek thanks for the reply

Have you enabled the interrupt in NVIC?

yes i have enabled the interrupt in Nvic

Are the Rx pin set correctly in GPIO?

yes it is correctly set in the gpio.

Thankyou for the reference

Bob S
Super User
May 5, 2022

Probably not related to your problem, but this line isn't correct:

 memset(SSD1306_Buffer,0,strlen(SSD1306_Buffer));

use sizeof() not strlen(). If the buffer is a global variable, it is likely already zero filled from the "C" startup code, and if so, strlen() will be zero. If the buffer is declared as a local variable in a function, it will contain uncertain data and strlen() may return a number much larger than the actual size of the buffer - them memset() will overwrite OTHER variables on the stack and bad things will happen.

NJP.1
NJP.1Author
Associate III
May 10, 2022

thankyou i will change it

NJP.1
NJP.1Author
Associate III
May 10, 2022

may i know how to enable the global interupts in the code by using code only not the cube ide code generation.

waclawek.jan
Super User
May 10, 2022

In Cortex-Mx, interrupts are enabled after reset globally.

To disable/enable interrupts globally, use

__disable_irq();

__enable_irq();

JW

NJP.1
NJP.1Author
Associate III
May 11, 2022

thankyou i enabled the global interrupts even then the interrupt driven reception is not working

Slh
Senior
May 10, 2022

Try this after UART Configuration:

__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); 

NJP.1
NJP.1Author
Associate III
May 11, 2022

Thankyou even i tried adding this even then the reception is not happening.

Slh
Senior
May 11, 2022

Check your hardware if Pin connection is correctly or maybe shared with other functions.