engineershrenik wrote on Wednesday, January 08, 2014:
Hi, I am using freertos demo for stm32f103x and using stm32f103vct6. Usart 1 is working fine but when i configured usart2 transmit function works fine but usart for receive function RXNE flags is setting falsely hence i am getting garbage data. This is not happening when i am programming two usarts without freertos in keil. Is there any configuration problem? Please help me to solve this issue.
I am posting my code too.
void USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//enable bus clocks
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//Set USART2 Tx (PA2) as AF push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Set USART2 Rx (PA3) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStructure);
//Enable USART2
USART_Cmd(USART2, ENABLE);
}
/* Usart functions --------------------------------- ------------------------/
/******************************************************************************
- Function Name : SerialPutChar
- Description : Print a character on the HyperTerminal
- Input : - c: The character to be printed
- Output : None
- Return : None
*******************************************************************************/
void SerialPutChar(u8 c)
{
USART_SendData(USART2, c);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
int GetChar (void)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
return USART_ReceiveData(USART2);
}
/*******************************************************************************
- Function Name : Serial_PutString
- Description : Print a string on the HyperTerminal
- Input : - s: The string to be printed
- Output : None
- Return : None
*******************************************************************************/
void Serial_PutString(u8 *s)
{
while (*s != ‘\0’)
{
SerialPutChar(*s);
s ++;
}
}
static portTASK_FUNCTION(vBTTask,pvParameters)
{
int data;
USART2_Init();
printf("Usart2 is initialized Press Key \r\n");
Serial_PutString("test usart string \r\n");
while(1)
{
data = GetChar();
SerialPutChar(data);
}
}
here data variable is getting continuously garbage data using GetChar function.
Please let me know. i guess its freertos related issue only.