title: STM32F407 UART gives garbage on terminal
body: <p>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. </p>
  
  <p>I managed to write the following code in order to use USART1 from the board </p>
  
  <pre><code>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, &amp;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 ,&amp;USART_InitStructure);
  USART_Cmd(USART1 , ENABLE);
  
  
  while(1)
  {
      while (USART_GetFlagStatus(USART1 , USART_FLAG_TXE) == RESET);
      USART1 -&gt;DR = (uint16_t)(45 &amp; 0x01FF);
  
      Delay(0x3FFFFF);
  }
  </code></pre>
  
  <p>}    </p>
  
  <p>Also I have made sure clock is configured correctly by setting HSE and other clock details </p>
  
  <pre><code>    #if !defined  (HSE_VALUE)
    #define HSE_VALUE    ((uint32_t)8000000) /*!&lt; Value of the External oscillator in Hz */
  #endif /* HSE_VALUE */
  </code></pre>
  
  <p>And also </p>
  
  <pre><code>    /************************* 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
  
  /******************************************************************************/
  </code></pre>
  
  <p>In console I just receive garbage and I have made the following to make sure the setup is correct : 
  - Checked USB-RS232 converter 
  - Switched between converter and STM32 board 
  - Switched converter 
  - Generated code using STM32CubeMX to compare it and it seems consistent (logic wise of course :) ) </p>
  
  <p>From my beginner perspective it is kinda "showstopper" :( I cannnot seem to find a root cause of this </p>
  
  <p>Any help would be appreciated</p>
  
  <hr>
  
  <p>UPDATE 1 : In order to find root cause I have decided to try one of the other USART modules. Below is my code for USART3 and this one <strong>WORKS OUT OF THE BOX</strong> which would lead me to incorrect init of USART1 in the initial question ? </p>
  
  <pre><code>int main(void)
  </code></pre>
  
  <p>{</p>
  
  <pre><code>  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, &amp;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, &amp;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, &amp;USART_InitStructure);
  
    /* Enable USART */
    USART_Cmd(USART3, ENABLE);
  
    SysTick_Config(SystemCoreClock / 1000);
  
    while(1)
    {
  
  
      for (int var = 45; var &lt; 128; var++)
      {
          USART_SendData(USART3, var);
  
          Delay_SysTick(500); // 1 sek
      }
  
  
  
    }
  </code></pre>
  
  <p>}</p>
  
  <hr>
  
  <p>Resolution: After a lot of digging and trying multiple things it appears that USART1 would collide with USB components and there is capacitor which collides with the transmission <a href="https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/Help%20with%20GPIO%20on%20STM32F4%20discovery&amp;FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&amp;currentviews=956" rel="nofollow">link to answer on ST forums</a></p>
  
  <p>I wish I would have found this somewhere in documentation before. </p>
  
  <p>Hope this helps someone and thanks everyone for your assistance</p>