Skip to main content
tommaso
Associate II
Feb 10, 2016 8:40 AM UTC
Question

STM32F4, HAL Lib, USART IRQ

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.
    This topic has been closed for replies.

    10 replies

    pratik
    Associate
    Feb 10, 2016 9:52 AM UTC
    Posted on February 10, 2016 at 10:52

    Hi, 

    try setting priority and enabling interrupt before calling ''

    HAL_USART_Receive_IT

    ''

    tommaso
    tommasoAuthor
    Associate II
    Feb 10, 2016 11:21 AM UTC
    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
    tommasoAuthor
    Associate II
    Feb 10, 2016 3:43 PM UTC
    Posted on February 10, 2016 at 16:43

    Have anyone an example for this problem?

    Tesla DeLorean
    Guru
    Feb 10, 2016 4:05 PM UTC
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Amel NASRI
    ST Technical Moderator
    Feb 10, 2016 4:06 PM UTC
    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 "Best Answer" on the reply which solved your issue or answered your question.
    tommaso
    tommasoAuthor
    Associate II
    Feb 10, 2016 4:17 PM UTC
    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 Technical Moderator
    Feb 10, 2016 4:51 PM UTC
    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 "Best Answer" on the reply which solved your issue or answered your question.
    tommaso
    tommasoAuthor
    Associate II
    Feb 12, 2016 1:56 PM UTC
    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
    tommasoAuthor
    Associate II
    Feb 16, 2016 2:48 PM UTC
    Posted on February 16, 2016 at 15:48

    Can anyone tell me if my IRQ handler is correct?

    Thanks.

    Amel NASRI
    ST Technical Moderator
    Feb 18, 2016 9:48 AM UTC
    Posted on February 18, 2016 at 10:48

    Hi crazy_code,

    There is no need to call 

    HAL_NVIC_ClearPendingIRQ in the IRQ handler.

    -Mayla-

    To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.