AnsweredAssumed Answered

flow control uart1 -- odd behavior

Question asked by brown.geoffrey on May 10, 2011
Latest reply on Mar 5, 2014 by sanil.prem
I'm trying to get flow control working on Uart1 on the discovery board and am seeing some odd behavior.  I have a little test program that receives 10 characters, pauses, and then echos them back.   At the host (linux) side I'm using a cp2102 based usb/uart bridge with the flow control pins brought out.  The cp2102 isn't consistently stopping when the stm32 raises RTS.  When it continues to send, the stm32 lowers (enables) RTS and leaves RTS enabled until the next time the stm32 firmware reads a character from the uart buffer.

Obviously the cp2102 issue is outside the scope of this forum, but I'm surprised that the response of the stm32 uart is to enable RTS.   I've attached a screenshot from the logic analyzer.  I'm curious if anybody understands what's going on with the stm32 end of things.

Here is my UART initialization and the key test loop:


int  uart_open (uint8_t uart, uint32_t baud, uint32_t flags)
{
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructureTx;
  GPIO_InitTypeDef GPIO_InitStructureRx;

  if (uart == 1) {

    // Turn on clocks                                                                            

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA  |
                           RCC_APB2Periph_AFIO,
                           ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    // Configure TX pin                                                                          

    GPIO_InitStructureTx.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructureTx.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructureTx.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructureTx);

    // Configure RX pin                                                                          

    GPIO_InitStructureRx.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructureRx.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructureRx.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructureRx);

    // Configure CTS pin                                                                         

    GPIO_InitStructureRx.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructureRx.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructureRx.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructureRx);


    // Configure RTS pin                                                                         

    GPIO_InitStructureTx.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructureTx.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructureTx.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructureTx);

    // Configure the UART                                                                        

    USART_StructInit(&USART_InitStructure);
    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
    USART_Init(USART1,&USART_InitStructure);
    USART_Cmd(USART1, ENABLE);

    return 0;
  }
}

ssize_t uart_write(uint8_t uart, const void *buf, size_t nbyte)
{
  int i;
  for (i = 0; i < nbyte; i++)
    {
      while (!(USART1->SR & USART_FLAG_TXE));
      USART1->DR =  ((uint8_t *) buf)[i];
    }
}

ssize_t uart_read (uint8_t uart, void *buf, size_t nbyte)
{
  int i;
  for (i = 0; i < nbyte; i++)
    {
      while (!(USART1->SR & USART_FLAG_RXNE));
      ((uint8_t *) buf)[i] =  USART1->DR & 0xff;
    }
  return i;
}

 while(1)
  {
    Delay(20);
    uart_read(1, buf, 10);
    Delay(20);
    uart_write(1,buf,10);

  }

Outcomes