cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4Nucleo UART Rx interrupt doesn't work

peterharrison9158
Associate
Posted on July 13, 2015 at 22:15

Hi

I have been working with UART Rx interrupt but still not working. If someone can help me to figure out the problem I will appreciate it. I am using - STM32F411RE Nucleo Bd - STM32CubeMX - IAR I am thinking 2 UARTs interrupt mode (UART1 for a GPS module and UART2 for a PC). And I have finished some basic testings such as... - Button, B1 (GPIOC 13) - LED LD2 (GPIOA 5) - UART2(PA2, PA3) echo test without using the interrupt - UART1(PA9, PA10) Tx test without using the interrupt - I am using 2 differnt computers for UART1 and UART2 respectively. It seems to me like UART IRQ handler is never called. My codes are like this. I edited only ''main.c'' and ''stm32f4xx_it.c'' files in the ''User'' folder


/*main.c*/



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();


/* Configure the system clock */

SystemClock_Config();


/* System interrupt init*/

/* Sets the priority grouping field */

HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 15);


/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_TIM3_Init();

MX_USART1_UART_Init();

MX_USART2_UART_Init();


/* USER CODE BEGIN 2 */

char msg[15];

HAL_Delay(5000); 

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

sprintf(msg, ''>> UART2 (ST-Link) Init \n''); 

HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 0x12c);

HAL_Delay(5000);

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

sprintf(msg, ''>> UART1 (GPS) Init\n''); 

HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), 0x12c);

/* USER CODE END 2 */


/* USER CODE BEGIN 3 */

/* Infinite loop */

while (1)

{

if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == (uint32_t)GPIO_PIN_RESET) // Push Button B1

{

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); //LED, LD2

}


}

/* USER CODE END 3 */


}


/** System Clock Configuration

*/

void SystemClock_Config(void)

{


RCC_OscInitTypeDef RCC_OscInitStruct;


__PWR_CLK_ENABLE();


__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);


RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = 6;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

HAL_RCC_OscConfig(&RCC_OscInitStruct);


}


/* TIM3 init function */

void MX_TIM3_Init(void)

{


TIM_ClockConfigTypeDef sClockSourceConfig;

TIM_MasterConfigTypeDef sMasterConfig;


htim3.Instance = TIM3;

htim3.Init.Prescaler = 0;

htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

htim3.Init.Period = 0;

htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

HAL_TIM_Base_Init(&htim3);


sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);


sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);


}


/* USART1 init function */

void MX_USART1_UART_Init(void)

{


huart1.Instance = USART1;

huart1.Init.BaudRate = 9600;

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;

HAL_UART_Init(&huart1);


}


/* USART2 init function */

void MX_USART2_UART_Init(void)

{


huart2.Instance = USART2;

huart2.Init.BaudRate = 115200;

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;

HAL_UART_Init(&huart2);


}


/** Configure pins as 

* Analog 

* Input 

* Output

* EVENT_OUT

* EXTI

* Free pins are configured automatically as Analog (this feature is enabled through 

* the Code Generation settings)

*/

void MX_GPIO_Init(void)

{


GPIO_InitTypeDef GPIO_InitStruct;


/* GPIO Ports Clock Enable */

__GPIOC_CLK_ENABLE();

__GPIOH_CLK_ENABLE();

__GPIOA_CLK_ENABLE();

__GPIOB_CLK_ENABLE();

__GPIOD_CLK_ENABLE();


/*Configure GPIO pin : PC13 */

GPIO_InitStruct.Pin = GPIO_PIN_13;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_PULLUP;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);


/*Configure GPIO pins : PC14 PC15 PC0 PC1 

PC2 PC3 PC4 PC5 

PC6 PC7 PC8 PC9 

PC10 PC11 PC12 */

GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1 

|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5 

|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9 

|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);


/*Configure GPIO pins : PA0 PA1 PA4 PA6 

PA7 PA8 PA11 PA12 

PA15 */

GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_6 

|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_11|GPIO_PIN_12 

|GPIO_PIN_15;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


/*Configure GPIO pin : PA5 */

GPIO_InitStruct.Pin = GPIO_PIN_5;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


/*Configure GPIO pins : PB0 PB1 PB2 PB10 

PB12 PB13 PB14 PB15 

PB3 PB4 PB5 PB6 

PB7 PB8 PB9 */

GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10 

|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15 

|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6 

|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


/*Configure GPIO pin : PD2 */

GPIO_InitStruct.Pin = GPIO_PIN_2;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);


}

/*stm32f4xx_it.c*/



void SysTick_Handler(void)

{

HAL_IncTick();

HAL_SYSTICK_IRQHandler();

}


/**

* @brief This function handles USART2 global interrupt.

*/

void USART2_IRQHandler(void)

{

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

HAL_NVIC_ClearPendingIRQ(USART2_IRQn);

HAL_UART_IRQHandler(&huart2);

}


/**

* @brief This function handles USART1 global interrupt.

*/

void USART1_IRQHandler(void)

{

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

HAL_NVIC_ClearPendingIRQ(USART1_IRQn);

HAL_UART_IRQHandler(&huart1);

}

/*stm32f4xx_hal_msp.c*/



void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{


GPIO_InitTypeDef GPIO_InitStruct;

if(huart->Instance==USART1)

{

/* Peripheral clock enable */

__USART1_CLK_ENABLE();


/**USART1 GPIO Configuration 

PA9 ------> USART1_TX

PA10 ------> USART1_RX 

*/

GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

GPIO_InitStruct.Alternate = GPIO_AF7_USART1;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


/* Peripheral interrupt init*/

/* Sets the priority grouping field */

HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);

HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART1_IRQn);

}

else if(huart->Instance==USART2)

{

/* Peripheral clock enable */

__USART2_CLK_ENABLE();


/**USART2 GPIO Configuration 

PA2 ------> USART2_TX

PA3 ------> USART2_RX 

*/

GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


/* Peripheral interrupt init*/

/* Sets the priority grouping field */

HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);

HAL_NVIC_SetPriority(USART2_IRQn, 0, 4);

HAL_NVIC_EnableIRQ(USART2_IRQn);

}


}

Thank you in advance #uart #stm32f411re #interrupt
0 REPLIES 0