cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot get UART interrupt to fire with standard peripheral lib

Alexander Hoffman
Associate III
Posted on February 14, 2018 at 16:41

Hi all,

I am working on a project based around an old comms stack that uses the standard peripheral library, which i'm not that familiar with. I have been trying for a good couple of days now to get the UART rx interrupt to fire without luck. I have been basing my work off of the example here 

http://pandafruits.com/stm32_primer/stm32_primer_uart.php

 . I am able to send over UART but cannot seem to receive via interrupt.

I am initilizing USART1 and 3, 3 being part of the exsisting comms stack I am trying to implement in my project, my code for initilizing USART is as follow

void serial_init_1(unsigned int baud)

{

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

// Enable USART1 and GPIOA clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

/* Configure USART1 Tx (PA.09) as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Rx (PA.10) as input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = baud;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure);

/* Enable the USART1 Receive interrupt: this interrupt is generated when the

USART1 receive data register is not empty */

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

/* Enable USART1 */

USART_Cmd(USART1, ENABLE);

// NVIC_EnableIRQ(USART1_IRQn);

}

Then the NVIC as follows

nvic_initialisierung(USART1_IRQn | USART3_IRQn);

void nvic_initialisierung(uint16_t IRQ)

{

NVIC_InitTypeDef NVIC_InitStructure; //create NVIC structure

NVIC_InitStructure.NVIC_IRQChannel = IRQ;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

}

I have then defined 

void USART1_IRQHandler(void)

{

}

But it is never reached.

Any advice and or links to solid examples would be appreciated. I have tried many slight variations of what is above but nothing seems to work.

Cheers

#stm32 #standard-peripheral-library #uart
7 REPLIES 7
Posted on February 14, 2018 at 17:32

If using a .CPP file (or C++ compilation) use

extern 'C' void USART1_IRQHandler(void)

{

  // need code here, lest processor stops executing

}

What STM32 part?

Does the USART work in polling mode, ie you transmit a loop of 'U' chars, or you echo back received data

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
T J
Lead
Posted on February 15, 2018 at 03:34

I have mastered the Uart dma stuff now, do you know about the cube ?

I use the Cube to initialise 3 serial ports and many other periherials,

in main.c every thing is already done,

you just need to adjust some callbacks.

Posted on February 16, 2018 at 09:59

Thanks for the responses. Unfortunately I need to use the standard peripheral libraries and not the HAL libraries that I normally work with. UART in HAL is so much easier.

Turvey.Clive.002

it's not a cpp file and I have not tried polling because I need to get it working with interrupts. I know that the UART physically works as I originally tried porting the project into HAL but that proved to be a much more challenging process, none the less the UART worked in HAL no problem.

I am not sure what I am missing.

Posted on February 16, 2018 at 10:17

Make sure the USART isn't flagging any errors (parity, framing, etc) which will need to be cleared.

Not going to get interrupts if USART isn't receiving anything, the one-minute sanity check is to have a tight polling loop echoing data back to a terminal.

Its not a bitvector, ORing an index number isn't likely to get a valid number for either interrupt.

nvic_initialisierung(USART1_IRQn | USART3_IRQn);

nvic_initialisierung(USART1_IRQn);

nvic_initialisierung(USART3_IRQn);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AvaTar
Lead
Posted on February 21, 2018 at 10:51

// Enable USART1 and GPIOA clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

Are you sure about that ?

For a F303 example I have, GPIO is on AHB, not APBx:

    /* UART1 on PC4,PC5 */

    /* Enable GPIO clock */

    RCC_AHBPeriphClockCmd (RCC_AHBPeriph_GPIOC, ENABLE);

    /* Enable USART clock */

    RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1, ENABLE);
Posted on February 21, 2018 at 10:04

Thanks I'll give this a try and check back.

Cheers
henry.dick
Senior II
Posted on February 21, 2018 at 12:50

You didn't enable uart1 nvic - you did uart3 instead.

Interrupts on those parts involve the flag, the enable bits, the nvic specific to that peripheral and the global irq.

You are almost there.