AnsweredAssumed Answered

F4 usart clock problem USART1 timing slightly off Workaround Found

Question asked by Kallberg.Mikael on Aug 10, 2013
Latest reply on Aug 13, 2013 by Kallberg.Mikael
Hi, i really hope a can get some help with my problem, been working with this for 2 days now.

I have worked with F1 and L1 for a few years now and thy are great, i have tested the F4 series before, but without using the usart.

Now i have created a project with STM32F406RG and i get some weird USART results.

The systick works at the right speed so i don' t know what to think.
I have tested this on two different HW boards, and made two new code projects to test my code.

The crystal is 8Mhz and the signal is checked and working fine.
I have edit stm32f4xx.h so it´s set to 8MHz.
The System_stm32f4 clock config is also set to 8MHz,

So i made this code so i could ask you guys if you can spot the error.
the code comes more or less from the standard perih uart examples.
uart send two values (0x55,0x02) every 20ms based on systick counter.

USART3 is used with USART_BaudRate = 9600

USART3 differers the most, with a frame timing at 3255uS instead of the right time frame = 1024uS
i have also tested USART1, and it gives me a timeframe = 1124uS which is slight of, but still gives a huge Error.


when i don't call the SystemInit(); then the usart timing is working fine, but not the systick.
when it is OKAY the |T1-T2| should be ~1024uS
working.jpg



and when i do call the SystemInit(); then the usart timing fails, but systick works fine 
when it is OKAY the |T1-T2| should be ~1024uS
fail.jpg


the code

Main.c
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_conf.h"
#include "system_stm32f4xx.h"
 
 
/* Private function prototypes -----------------------------------------------*/
static void USART_Config(void);
static void SysTickConfig(void);
   
/* Private functions ---------------------------------------------------------*/
 
volatile unsigned long SysTickCount;
 
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  // Only used to check the osc reference values while debug so they are correct
  uint32_t T1 = HSE_VALUE;
  uint32_t T2 = HSI_VALUE;
 
   
  /* System Clocks Configuration **********************************************/
  SystemInit();
 
  /* USART configuration -----------------------------------------------------*/
  USART_Config();
   
  /* SysTick configuration ---------------------------------------------------*/
  SysTickConfig();
 
  
 
  while (1)
  {
          // Send char U
          USART_SendData(USARTx, 0x55);
 
          // Wait until end of transmit
          while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET){}
 
          /* The software must wait until TC=1. The TC flag remains cleared during all data
             transfers and it is set by hardware at the last frame’s end of transmission*/
          while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)
          {}
          // Send one extra char so we can see the stopbit negative flank
          USART_SendData(USARTx, 0x02);
 
 
          // Main Delay "Wait" (1ms * 20) = 20ms    ********************************** /
          while( SysTickCount < 20)
          {}
          SysTickCount = 0;
  }
}
 
/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
    SysTickCount++;
}
 
/**
  * @brief  Configures the USART Peripheral.
  * @param  None
  * @retval None
  */
static void USART_Config(void)
{
  USART_InitTypeDef USART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
   
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE);
   
  /* Enable USART clock */
  USARTx_CLK_INIT(USARTx_CLK, ENABLE);
   
  /* Connect USART pins to AF7 */
  GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF);
  GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF);
   
  /* Configure USART Tx and Rx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN;
  GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure);
   
  GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN;
  GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure);
 
  /* Disable the USART OverSampling by 8 */
  USART_OverSampling8Cmd(USARTx, DISABLE); 
 
  /* USARTx configuration ----------------------------------------------------*/
  /* USARTx configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - one Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  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_Init(USARTx, &USART_InitStructure);
   
  /* NVIC configuration */
  /* Configure the Priority Group to 2 bits */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
   
  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
   
  /* Enable USART */
  USART_Cmd(USARTx, ENABLE);
}
 
/**
  * @brief  Configures the SysTick Base time to 10 ms.
  * @param  None
  * @retval None
  */
static void SysTickConfig(void)
{
  /* Setup SysTick Timer for 1ms interrupts  */
  if (SysTick_Config(SystemCoreClock / 1000))
  {
    /* Capture error */
    while (1);
  }
  /* Configure the SysTick handler priority */
  NVIC_SetPriority(SysTick_IRQn, 0x0);
}

Main.h
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
 
 
 
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
 
 
#define USARTx                           USART3
#define USARTx_CLK                       RCC_APB1Periph_USART3
#define USARTx_CLK_INIT                  RCC_APB1PeriphClockCmd
#define USARTx_IRQn                      USART3_IRQn
#define USARTx_IRQHandler                USART3_IRQHandler
 
#define USARTx_TX_PIN                    GPIO_Pin_10               
#define USARTx_TX_GPIO_PORT              GPIOC                      
#define USARTx_TX_GPIO_CLK               RCC_AHB1Periph_GPIOC
#define USARTx_TX_SOURCE                 GPIO_PinSource10
#define USARTx_TX_AF                     GPIO_AF_USART3
 
#define USARTx_RX_PIN                    GPIO_Pin_11               
#define USARTx_RX_GPIO_PORT              GPIOC                   
#define USARTx_RX_GPIO_CLK               RCC_AHB1Periph_GPIOC
#define USARTx_RX_SOURCE                 GPIO_PinSource11
#define USARTx_RX_AF                     GPIO_AF_USART3
 
 
 
 
#endif /* __MAIN_H */

System_stm32f4 settings from STM32F4xx_Clock_Configuration_V1.1.0.xls
*=============================================================================
*        Supported STM32F40xx/41xx/427x/437x devices
*-----------------------------------------------------------------------------
*        System Clock source                    | PLL (HSE)
*-----------------------------------------------------------------------------
*        SYSCLK(Hz)                             | 168000000
*-----------------------------------------------------------------------------
*        HCLK(Hz)                               | 168000000
*-----------------------------------------------------------------------------
*        AHB Prescaler                          | 1
*-----------------------------------------------------------------------------
*        APB1 Prescaler                         | 4
*-----------------------------------------------------------------------------
*        APB2 Prescaler                         | 2
*-----------------------------------------------------------------------------
*        HSE Frequency(Hz)                      | 8000000
*-----------------------------------------------------------------------------
*        PLL_M                                  | 8
*-----------------------------------------------------------------------------
*        PLL_N                                  | 336
*-----------------------------------------------------------------------------
*        PLL_P                                  | 2
*-----------------------------------------------------------------------------
*        PLL_Q                                  | 7
*-----------------------------------------------------------------------------
*        PLLI2S_N                               | NA
*-----------------------------------------------------------------------------
*        PLLI2S_R                               | NA
*-----------------------------------------------------------------------------
*        I2S input clock                        | NA
*-----------------------------------------------------------------------------
*        VDD(V)                                 | 3.3
*-----------------------------------------------------------------------------
*        Main regulator output voltage          | Scale1 mode
*-----------------------------------------------------------------------------
*        Flash Latency(WS)                      | 5
*-----------------------------------------------------------------------------
*        Prefetch Buffer                        | OFF
*-----------------------------------------------------------------------------
*        Instruction cache                      | ON
*-----------------------------------------------------------------------------
*        Data cache                             | ON
*-----------------------------------------------------------------------------
*        Require 48MHz for USB OTG FS,          | Disabled
*        SDIO and RNG clock                     |
*-----------------------------------------------------------------------------


The project can be downloaded www.lizerd.se/images/wysiwyg/Kab/error/F4_UART.rar

I would be very happy if some one would check the code and see if i did something wrong

Outcomes