AnsweredAssumed Answered

STM32F407 Discovery - Problem with UART1

Question asked by RafalPE on Jan 17, 2015
Latest reply on Jan 19, 2015 by childress.steve

I have just started my adventures with STM32f407 Discovery board. I'm using latest stable release of CoIDE and using up to date toolchain/libraries.

I managed to write the following code in order to use USART1 from the board

int main(void)
{
 
GPIO_InitTypeDef  GPIO_InitStructure;   // Definicja struktury do inicjalizacji PINOW
USART_InitTypeDef USART_InitStructure;
 
// Initialize pins as alternating function
GPIO_InitStructure.GPIO_Pin     = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType   = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd    = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed   = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
 
// Modify USART_InitStructure for non -default values , e.g.
// USART_InitStructure.USART_BaudRate = 38400;
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_No;
USART_InitStructure.USART_HardwareFlowControl   = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode                  = USART_Mode_Tx|USART_Mode_Rx;
USART_Init(USART1 ,&USART_InitStructure);
USART_Cmd(USART1 , ENABLE);
 
 
while(1)
{
    while (USART_GetFlagStatus(USART1 , USART_FLAG_TXE) == RESET);
    USART1 ->DR = (uint16_t)(45 & 0x01FF);
 
    Delay(0x3FFFFF);
}
 
}

cfa6a1396a41f34932751db887e3c66a.png

And as you can see on the attached picture I'm receiving incorrect data. Bascially if I change value sent the characters on the console are the same.

I have defined settings for clock configuration and confirmed it is working fine
#if !defined  (HSE_VALUE)
#define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
 
 
/************************* PLL Parameters *************************************/
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M      8
#define PLL_N      336
 
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P      2
 
/* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */
#define PLL_Q      7
 
/******************************************************************************/


Now in order to find the root cause I have tried to configure USART3 and this one works out of the box.

int main( void)
{
 
 GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
 
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
 
  /* Enable UART clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
 
  /* Connect PXx to USARTx_Tx*/
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
 
  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
 
  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
 
  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
 
  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_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
  /* USART configuration */
  USART_Init(USART3, &USART_InitStructure);
 
  /* Enable USART */
  USART_Cmd(USART3, ENABLE);
 
  SysTick_Config(SystemCoreClock / 1000);
 
  while(1)
  {
 
 
    for (int var = 45; var < 128; var++)
    {
        USART_SendData(USART3, var);
 
        Delay_SysTick(500); // 1 sek
    }
 
 
 
  }
 
 
}

Can anyone point me to some possible root causes ? I have already tried multiple baud rates on PC terminal / different USB<>RS232 (mostly FT232RL chips ) and slowly running out of any ideas.

Regards
Rafal

Outcomes