0
\$\begingroup\$

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);
 }
\$\endgroup\$
  • \$\begingroup\$ Your code tells us nothing. \$\endgroup\$ – Bence Kaulics Jul 31 '16 at 20:01
  • \$\begingroup\$ @BenceKaulics, more details added \$\endgroup\$ – Hamid Jul 31 '16 at 20:14
  • \$\begingroup\$ It baffles me why you did not show the code you are having problems with. You say that USART_Configuration works and still you paste the code for that. \$\endgroup\$ – pipe Jul 31 '16 at 22:05
  • \$\begingroup\$ @Hamid I'm not even going to look at your code, are you using DMA? If not, you run the risk that the data will get overwritten before you get it out of the buffer. google.com/… \$\endgroup\$ – Voltage Spike Aug 1 '16 at 16:20
  • \$\begingroup\$ @laptop2d, No , I am not using DMA \$\endgroup\$ – Hamid Aug 1 '16 at 18:32
1
\$\begingroup\$

The only place I see you enabling peripheral clocks is this line in GPIO_CONFIG, which only enables USART1 and GPIOA:

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

USART2 won't work unless you enable its clock as well. Make sure you enable it on the right bus.

\$\endgroup\$
  • \$\begingroup\$ Why would usart 1 stop working? \$\endgroup\$ – Scott Seidman Aug 1 '16 at 2:19
  • \$\begingroup\$ @ScottSeidman I have to use RCC_APB2PeriphClockCmd function for usart1 and RCC_APB1PeriphClockCmd for usart2. but I used a RCC_APB2PeriphClockCmd function fot both of them! \$\endgroup\$ – Hamid Aug 1 '16 at 18:14
  • \$\begingroup\$ @duskwuff No, don't use RCC_APB2PeriphClockCmd for both usart1 and usart2, this acts for usart1, but for usart2 you should use RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); \$\endgroup\$ – Hamid Aug 1 '16 at 18:30
  • \$\begingroup\$ @Hamid The point is you're not enabling the USART2 clock anywhere. Yes, you have to enable the peripheral clock on the correct bus. \$\endgroup\$ – duskwuff -inactive- Aug 1 '16 at 18:38
1
\$\begingroup\$

The problem was in the GPIO_CONFIG(void) function, I have to use RCC_APB2PeriphClockCmd function for usart1 and RCC_APB1PeriphClockCmd for usart2. Since usart1 belongs to APB2PeriphClock and usart2 belongs to RCC_APB1PeriphClock.So the final solution is :

void GPIO_CONFIG(void)
 {
   GPIO_InitTypeDef GPIO_InitStructure;    
   // enable uart 1 preph clock mode
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA ,     ENABLE);

    // enable uart 2 preph clock mode
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,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);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.