Question
USART receiving with interrupts doesn't work
Posted on January 06, 2014 at 10:15
Hi all,
I'm trying to receive char using USART through interrupts but it seems doesn't work (it doesn't receive the interrupt when I prompt a char in the terminal).I use USB To RS232 TTL UART PL2303HX for connect stm32f3discovery to PC and Termite 2.9 on COM3 to emulate the hyperterminal.Here below the code. Any help is very appreciate. Thanks/* Includes ------------------------------------------------------------------*/
#include ''main.h''#include <stdio.h>#include <string.h> // memcpy() /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ void RCC_Configuration(void){/* --------------------------- System Clocks Configuration -----------------*//* USART2 clock enable */RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);/* GPIOA clock enable */RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);/* DMA1 clock enable */RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);}/**************************************************************************************/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure;/*-------------------------- GPIO Configuration ----------------------------*/GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);/* Connect USART pins to AF */ /* Configure Alternate Function pin muxing fabric to escape USART2 Rx and Tx */GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7); //USART2_TXGPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7); //USART2_RX}/**************************************************************************************/void USART2_Configuration(void){USART_InitTypeDef USART_InitStructure;/* USARTx configuration ------------------------------------------------------*//* USARTx configured as follow: - BaudRate = 4800 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */USART_InitStructure.USART_BaudRate = 4800;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(USART2, &USART_InitStructure);USART_Cmd(USART2, ENABLE);USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // Enable USART1 Receive Interrupt }/**************************************************************************************/#define LINEMAX 100 // Maximal allowed/expected line length volatile char line_buffer[LINEMAX + 1]; // Holding buffer with space for terminating NULvolatile int line_valid = 0; //************************************************************************************** void USART2_IRQHandler(void){ static char rx_buffer[LINEMAX]; // Local holding buffer to build line static int rx_index = 0; if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // Received character? { char rx = USART_ReceiveData(USART2); if ((rx == '\r') || (rx == '\n')) // Is this an end-of-line condition, either will suffice? { if (rx_index != 0) // Line has some content? { memcpy((void *)line_buffer, rx_buffer, rx_index); // Copy to static line buffer from dynamic receive buffer line_buffer[rx_index] = 0; // Add terminating NUL line_valid = 1; // flag new line valid for processing rx_index = 0; // Reset content pointer } } else { if (rx_index == LINEMAX) // If overflows pull back to start rx_index = 0; rx_buffer[rx_index++] = rx; // Copy to buffer and increment } }} /**************************************************************************************/void NVIC_Configuration(void){ NVIC_InitTypeDef NVIC_InitStructure;/* Configure the Priority Group to 2 bits */NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);/* Enable the USART2 RX DMA Interrupt */NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);}/**************************************************************************************/int main(void){RCC_Configuration();NVIC_Configuration();GPIO_Configuration();USART2_Configuration(); while(1) // Don't want to exit { __WFI(); // Wait for an interrupt rather than grind endlessly if (line_valid) // A new line has arrived { // ProcessLine(line_buffer); // Do something with the line line_valid = 0; // clear pending flag } }}