2014-07-14 06:14 AM
This is my first post here as I'm really stuck. I'm using a stm32F103 device but I cannot seem to get the usarts configured correctly to receive data. Transmit is fine, and on the receive side I do see characters. I want the user to enter a correct string terminated with a '\r' before the main app continues. However the uart only waits for me to type in some of the characters. It never waits to see in carraige return is pressed.
What I need is for the app to wait in the character receive loop until CR is pressed. If the key is invalid, then the user is prompted to re-enter the key. If not move on. Thanks in advance Dave void Usart1Init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /*-------------------------- GPIO Configuration ----------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 9600; 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; COMInit(COM1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { printf(''\r\nEnter Key \r\n''); res = Check_Key(); if (res == 1) { printf(''\r\nCorrect Key \r\n''); } } } uint8_t Check_Key(void) { char rxbyte; static uint8_t val = 0; static uint8_t char_count= 0; static char str_fr[20]; while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); rxbyte = USART_ReceiveData(USART1); str_fr[char_count] = (uint8_t)rxbyte; printf(''*''); char_count++; if (rxbyte == '\r') // Detect a newline character { if (!strcmp(str_fr, ''Hello+Welcome\r'')) { val = 1; } else { printf(''\n\rInvalid Key\n\r''); char_count= 0; } } return val; }2014-07-14 06:36 AM
Ok, have your interrupt receive one character at a time, and don't output multiple characters via printf(), etc.
Understand that it the time it takes to send a character you can also receive one, if you send a dozen and don't service the received data you will likely miss them.2014-07-14 07:32 AM
Hello Clive,
I thought I was receiving only one character at a time. Where in the code states otherwise? Removing all printfs make no difference. Should UART not wait in the receive loop until CR is pressed? Dave2014-07-14 08:08 AM
Clive,
I think I'm sorted on this one. Thanks for your help. Dave2014-07-14 08:15 AM
I scanned the code, the while loop seemed problematic, watch that the debugger doesn't clear the peripheral status.
Make sure you NUL terminate strings before using library functions against them.