2014-01-06 01:15 AM
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 } }}2014-01-06 03:20 AM
The code looks oddly familiar...
Ok, so perhaps the hardware doesn't see anything, may be the wrong baud rate or the connectivity isn't what it needs to be. Implement a simpler echo back test, and try that. Evaluate your hardware with a scope, and check the pins don't clash with other functionality.2014-01-07 03:19 AM
Yes Clive, it's your example!
Thanks for the advises, have you a echo back test example?regards,2014-01-07 03:27 AM
Hi
I suspect in the same example, there should be code for transmitting on RS232. Where you have commented out ''process_line()'' replace the process_line with sending the recieved line.2014-01-07 05:15 AM
// STM32 USART2 LOOP (Tx PA.2, Rx PA.3) STM32F3xx - sourcer32@gmail.com
#include ''stm32f30x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Enable USART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Connect PA2 to USART2_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
/* Connect PA3 to USART2_Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************************************/
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
/* USART 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 configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART2, ENABLE);
}
/**************************************************************************************/
void OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, *s++);
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART2_Configuration();
OutString(''This is an echo back test for USART2
'');
while(1) // Don't want to exit
{
uint16_t ch;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char
ch = USART_ReceiveData(USART2); // Collect Char
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, ch); // Echo Char
}
}
/**************************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d
'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
2014-01-07 07:14 AM
Thank you Clive.
I'll try and come back with feedback!2014-01-12 12:55 PM
Hi all,
now it works fine! The problem is a poor misunderstanding: I use USB To RS232 TTL UART PL2303HX for STM to PC connection. Datasheet says Green cable to TX and White to RX, but I've forgotten to invert TX and RX! The correct connection is Green cable to PA3 and white cable to PA2. Thank you Clive for your help. Good night to everyone.