cancel
Showing results for 
Search instead for 
Did you mean: 

USART problem

agilmore19
Associate II
Posted on November 16, 2010 at 21:41

Hi, I've managed to get the example program working which transfers a buffer of text from usart2 to usart3. When I try and output on usart2 to a PC however it seems as if the baud rate is wrong. I just get garbage.

The GPIO config is

 /* Configure USARTy Rx as input floating */

  GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);

 

   /* Configure USARTy Tx as push-pull */

  GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);

 The RCC config is

 RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK, ENABLE);

 RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE);

The defines are

 #define USARTy                   USART2

  #define USARTy_GPIO              GPIOA

  #define USARTy_CLK               RCC_APB1Periph_USART2

  #define USARTy_GPIO_CLK          RCC_APB2Periph_GPIOA

  #define USARTy_RxPin             GPIO_Pin_3

  #define USARTy_TxPin             GPIO_Pin_2

and the code is

  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_Even;

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 

  /* Configure USARTy */

  USART_Init(USARTy, &USART_InitStructure);

 

  /* Enable the USARTy */

  USART_Cmd(USARTy, ENABLE)

  do{

  while(TxCounter < TxBufferSize)

  {  

    /* Send one byte from USARTy  */

    USART_SendData(USARTy, TxBuffer[TxCounter++]);

    /* Loop until USARTy DR register is empty */

    while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)

    {

    }

  }

Any help would be appreciated

Alastair

5 REPLIES 5
jepjoh2
Associate II
Posted on November 16, 2010 at 22:13

I think you need to enable clock to AFIO too

Posted on November 16, 2010 at 22:44

I think you need to enable clock to AFIO too

 

Nothing is being remapped. GPIOA and USART2 should suffice.

Would start by confirming you can see data on the PA.2 pin with a scope. Measure the bit timing to confirm the baud rate. If the bit timing is wrong check the clock and the PLL settings.

Then checking your CMOS-to-RS232 voltage conversion circuit.

PA.2/PA.3 are not 5V tolerant pins, so verify any USB or RS232 serial conversion is using a supply in common with the STM32VL

Send out repetitive characters in a tight loop so the burst length is not limited to your output string while debugging.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
agilmore19
Associate II
Posted on November 17, 2010 at 10:29

Posted on November 17, 2010 at 15:32

Can you answer something else for me. The peripheral routines have the GPIO clock set at 50MHZ but the board is running at 24MHz.

 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 

The defines in the GPIO header file only have 50, 10 and 2. What effect does this have?

It is slew rate control, ie how strongly/rapidly it drives the lines high and low. 50 MHz will provide very fast/sharp edges, and more overshoot/undershoot and ringing, depending on how they are terminated. For a USART output, 2 MHz would more than suffice.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
agilmore19
Associate II
Posted on November 17, 2010 at 21:55