2018-02-14 7:41 AM
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 #uart2018-02-14 8:32 AM
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
2018-02-14 6:34 PM
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.
2018-02-16 1:59 AM
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.2018-02-16 2:17 AM
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);
2018-02-21 1:51 AM
// 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);2018-02-21 2:04 AM
Thanks I'll give this a try and check back.
Cheers2018-02-21 3:50 AM
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.
We’re moving the ST Community to a new platform to give you a better and more reliable community experience.