How can I use both usart1 (PA.09, PA.10) and usart2 (PA.02,PA.03) in a same time?
I can send and receive data via Usart1 but when I enable usart2 , no data could not be sent by usart1 and usart2!
I use stm32f103c8t6.
Code:
int main(void)
{
GPIO_CONFIG();
/* Configure the USART1 */
USART_Configuration();
printf("\r\nWELCOME usart1\r\n");// data received by terminal 1
/* Configure the USART2 */
USART2_Configuration();
USART2_Send("Hello usart2",9);// data not received by terminal 2
printf("\r\nWELCOME usart1\r\n");// data not received by terminal 1
while(1){}
}
usart1 and usart2 configurations are the same, and I use 2 terminal for each usart separately.
Edit:
Both usart1 and 2 configuration are the same,
usart2 configs in 'usart2.c' :
void USART2_IRQHandler(void)
{
uint8_t receivedChar;
if ((USART2->SR & USART_FLAG_RXNE) != (u16)RESET)
{
receivedChar = USART_ReceiveData(USART2);
if(USART2_Rx_BufferIndex == Max_Rx_Buffer_Size || USART2_Rx_BufferIndex==USART2_Desired_Buffer_Size || receivedChar=='\n')
{
USART2_RxBuffer[USART2_Rx_BufferIndex] = receivedChar;
USART2_flag = 1;
}
else
{
USART2_RxBuffer[USART2_Rx_BufferIndex++] = receivedChar;
}
USART2_RxBuffer[USART2_Rx_BufferIndex] = '\0';
}
}
void USART2_Recieve(uint16_t size)
{
if(size < Max_Rx_Buffer_Size)
{
USART2_Desired_Buffer_Size = size;
}
else
{
USART2_Desired_Buffer_Size = Max_Rx_Buffer_Size;
}
USART2_flag = 0;
while(USART2_flag == 0);
USART2_recievedData = USART2_RxBuffer;
USART2_recievedData[USART2_Rx_BufferIndex+1] ='\0';
USART2_Rx_BufferIndex=0;
}
void USART2_RecieveFix(uint16_t size)
{
if(size < Max_Rx_Buffer_Size)
{
USART2_Desired_Buffer_Size = size;
}
else
{
USART2_Desired_Buffer_Size = Max_Rx_Buffer_Size;
}
USART2_Rx_BufferIndex = 0;
while(USART2_Rx_BufferIndex < size)
{
USART2_flag = 0;
while(USART2_flag ==0);
USART2_Rx_BufferIndex++;
}
USART2_recievedData = USART2_RxBuffer;
USART2_recievedData[USART2_Rx_BufferIndex+1] ='\0';
USART2_Rx_BufferIndex=0;
}
char *USART2_GetReceivedData(void)
{
return USART2_recievedData;
}
void USART2_NVIC_Configuration(void){
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USARTx 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);
}
void USART2_Configuration(void){
USART_InitTypeDef USART_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_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
USART2_NVIC_Configuration();
/* Enable the USART2 Receive interrupt: this interrupt is generated when the
USART2 receive data register is not empty */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
void USART2_Send(unsigned char *pucBuffer, unsigned long ulCount)
{
//
// Loop while there are more characters to send.
//
while(ulCount--)
{
USART_SendData(USART2, *pucBuffer++);// Last Version USART_SendData(USART2,(uint16_t) *pucBuffer++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{
}
}
}
and in 'main.c'
void GPIO_CONFIG(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/* Configure USART1 Tx (PA.09) and UARTT2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2;
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) and USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
Best Answer
The problem was in the
GPIO_CONFIG(void)
function, I have to useRCC_APB2PeriphClockCmd
function forusart1
andRCC_APB1PeriphClockCmd
forusart2
. Sinceusart1
belongs toAPB2PeriphClock
andusart2
belongs toRCC_APB1PeriphClock
.So the final solution is :