cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4, HAL Lib, USART IRQ

tommaso
Associate II
Posted on February 10, 2016 at 09:40

Hi all,

I'm working with STM32F4 mcu and keil uVision 5 IDE. I'm tryng to configure RS232 periph. but I have a problem. Transmission works fine but I'm not able to receive interrupt when something is received. Here my code:


/** Init Rs232 port

*/

void init_RS232(void){

GPIO_InitTypeDef GPIO_InitStruct;


/* GPIO Ports Clock Enable */

__GPIOC_CLK_ENABLE();

__USART3_CLK_ENABLE();


/**USART3 GPIO Configuration

PC10 ------> USART2_TX

PC11 ------> USART2_RX

*/


/*Configure GPIO pin : RS232 Pin */

GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11; 

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);


/* Configure USART3 parameters */ 

USART3_Handle.Instance = USART3;

USART3_Handle.Init.BaudRate = 9600;

USART3_Handle.Init.Mode = USART_MODE_TX_RX;

USART3_Handle.Init.Parity = USART_PARITY_NONE;

USART3_Handle.Init.StopBits = USART_STOPBITS_1;

USART3_Handle.Init.WordLength = USART_WORDLENGTH_8B;

HAL_USART_Init(&USART3_Handle);



HAL_USART_Receive_IT(&USART3_Handle, rx_buffer, 16); 

HAL_NVIC_SetPriority(USART3_IRQn, 3, 0);

HAL_NVIC_EnableIRQ(USART3_IRQn); 

}

I would to receive an interrupt every time a char is received and I would like to manage by myself the buffer. With old lib (standard periph lib) I was able to do this. With new HAL ones not. Can you help me? Thanks a lot.
10 REPLIES 10
pratik
Associate
Posted on February 10, 2016 at 10:52

Hi, 

try setting priority and enabling interrupt before calling ''

HAL_USART_Receive_IT

''

tommaso
Associate II
Posted on February 10, 2016 at 12:21

Hi,

I've tried your suggestion but it doesn't work. When I enable IRQ the transmission doesn't work too. The code fall continously in USART3_IRQHandler.

tommaso
Associate II
Posted on February 10, 2016 at 16:43

Have anyone an example for this problem?

Posted on February 10, 2016 at 17:05

Doesn't HAL/Cube have a load of examples it installs?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Amel NASRI
ST Employee
Posted on February 10, 2016 at 17:06

Hi crazy_code,

In the STM32CubeF4 package, there are several examples for USART/UART based on interrupt mode (Ex:STM32Cube_FW_F4_V1.11.0\Projects\STM32F4-Discovery\Examples\UART\UART_TwoBoards_ComIT).

You may take them as start point to develop your own application.

You can also generate your initialization code based on CubeMX, did you tried it?

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

tommaso
Associate II
Posted on February 10, 2016 at 17:17

Hi Mayla,

can you explain me how to generate an example that uses UART and interrupt?

I open CubeMX, select New Project, selct my mcu, add UART to periph and check NVIC global interrupt but in the code I cannot find IRQ configuration,

Thanks a lot.

Amel NASRI
ST Employee
Posted on February 10, 2016 at 17:51

You will find it in the file stm32f4xx_hal_msp.c.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

tommaso
Associate II
Posted on February 12, 2016 at 14:56

Hi,

UART now works. I can transmit and receive in polling. I still have sompe problem with receive interrupt. I receive only first interrupt. Following ones are not detected. Here my code:

void USART3_IRQHandler(void){
HAL_NVIC_ClearPendingIRQ(USART3_IRQn);
HAL_UART_IRQHandler(&USART3_Handle);
}

What's wrong?
tommaso
Associate II
Posted on February 16, 2016 at 15:48

Can anyone tell me if my IRQ handler is correct?

Thanks.