cancel
Showing results for 
Search instead for 
Did you mean: 

wrong baud rate (yet another thread...)

jogerh
Associate II
Posted on June 23, 2015 at 17:36

Dear community,

I try to use the NUCLEO F302R8T6 board which I initialized using a start-up.c – file which was generatedby the STM32F30x_Clock_Configuration_V1.0.0.xls tool. I changed the NUCLEO board configuration accordingly to the UM1724 User manual for STM32 Nucleo boards page 22 5.7.1 -> MCO from STLINK to use the 8 MHZ produced by the on board quartz. The system_stm32f30x.c file (which I renamed into “system_stm32f3xx.c�? to make it compatible with the KEIL CMISS package) contains the following definition:

//Definition

*============================================================================= 
* Supported STM32F30x device 
*----------------------------------------------------------------------------- 
* System Clock source | HSE 
*----------------------------------------------------------------------------- 
* SYSCLK(Hz) | 8000000 
*----------------------------------------------------------------------------- 
* HCLK(Hz) | 72000000 
*----------------------------------------------------------------------------- 
* AHB Prescaler | 1 
*----------------------------------------------------------------------------- 
* APB2 Prescaler | 1 
*----------------------------------------------------------------------------- 
* APB1 Prescaler | 2 
*----------------------------------------------------------------------------- 
* HSE Frequency(Hz) | 8000000 
*---------------------------------------------------------------------------- 
* PLLMUL | 2 
*----------------------------------------------------------------------------- 
* PREDIV | 2 
*----------------------------------------------------------------------------- 
* USB Clock | DISABLE 
*----------------------------------------------------------------------------- 
* Flash Latency(WS) | 2 
*----------------------------------------------------------------------------- 
* Prefetch Buffer | OFF 
*----------------------------------------------------------------------------- 
*=============================================================================

I initialized the USART using the following routine:

void COM_USART1_Init() { //COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct) 
GPIO_InitTypeDef GPIO_InitStructure1; //GPIO STruct 
GPIO_InitTypeDef GPIO_InitStructure2; //GPIO STruct 
USART_InitTypeDef USART_InitStructure; //USART Struct 
NVIC_InitTypeDef NVIC_InitStructure; //for USART 
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //GPIOA_AHBPeriph_CLOCK 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //ENABLE USART3 CLK 
// Configure the GPIO_struct USART Tx as alternate function push-pull 
//PA10 ->RX 
//PA9 ->TX 
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_9; 
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP; //PushPull (Gegentakt) 
GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_UP; //NO Pull 
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz; 
// Configure the GPIO_struct USART Rx as alternate function push-pull 
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_10; 
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz; 
//Init GpioStruct 
GPIO_Init(GPIOA, &GPIO_InitStructure1); 
GPIO_Init(GPIOA, &GPIO_InitStructure2); 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7); //Connect the PINS and USART BUS 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7); 
//NVIC for USARR 
// Enable the USARTx Interrupt 
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // one level lower than TIm !! 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 
// USART 1 
// USARTx configured as follow: 
// - BaudRate = 115200 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 = 115200;//9600;//115200; //is just the half of 115200 57600; 
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(USART1, &USART_InitStructure); 
// Enable USART 
USART_Cmd(USART1, ENABLE); 
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1- receive interrupt 
} 

Accordingly to CLIVE1’s post I set the HSE_VALUE to 8000000

(

/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/wrong%20usart%20baudrate%20after%20change%20of%20crystal&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=89

)

However the baud rate of the USART is twice as fast as defined (to get the desired baud rate I have to initialize the USART using 0.5* desired baudrate).

I would be very happy if someone could point me into the right direction how to set the clock speed of the cpu correctly.

5 REPLIES 5
Posted on June 23, 2015 at 18:27

Your PREMUL/PREDIV seems to be non-sense for getting an 8 MHz clock to 72 MHz, review some actual examples in the library, and don't use the Excel sheet/tool.

You might need to enable the HSE Bypass mode.

Check the bit clock you see on the USART output by outputting a continuous stream of 0x55 characters.

Check the internal clocks of the F3 by outputting via it's MCO PA8 pin.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jogerh
Associate II
Posted on June 24, 2015 at 10:28

Dear community,

Thank you very much for responding to my question.

I forgot to select “PLL && HSE� in the excel- worksheet. The new start- up.c file configures the clock @ 72 MHz which I double-checked at the MCO pin using an oscilloscope.

(http://webcache.googleusercontent.com/search?q=cache:iaiPKKT9sg8J:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/MCO1%2520output%2520doesn't%2520output%2520anything+&cd=3&hl=de&ct=clnk&gl=de)

Unfortunately the prescaler option of the RCC_MCOConfig command in combination with RCC_MCOSource_PLLCLK has no effect on the output frequency (therefore an oscilloscope with a high sample- frequency is required).

Regarding CLIVE1’s recommendations:

A: Check the bit clock you see on the USART output by outputting a continuous stream of 0x55 characters.

-> I did so and found that the baud- rate is twice as high as defined.

A: Check the internal clocks of the F3 by outputting via it's MCO PA8 pin.

-> I did so and found that the clock speed is 36*2 Mhz , such as defined

However my USART – issue is still present: The USART speed is twice as high as defined in  USART_InitStructure.USART_BaudRate

I will check whether the ''HSE bypass mode'' makes some sense to solve this problem...

jogerh
Associate II
Posted on June 24, 2015 at 11:47

Dear community,

I did a few experiments by changing the startup.c configuration regarding the APB1 & APP2 prescaler values.

I summarized my result in the following table:

APB1    APB2    Multiplier X  (real baudrate = x* baudrate defined)   

2             1             0.5

2             4             2

2             2             1

4             8             2

4             2             0.5

4             4             1

Looks like that both APB1 and APB2 must have the same prescaler to get the defined baudrate. I read the reference and also the datasheet but I didn’t found neither an explanation for the obtained result. I would be very happy if someone would explain those findings.

Posted on June 24, 2015 at 18:12

Sounds like whatever firmware/library you are using is not correctly associating the USART and APB.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jogerh
Associate II
Posted on June 26, 2015 at 14:22

To make sure no strange files of different library’s (e.g. older versions from keil and newer version from SPL) are mixed together   I made a new project containing only such header files which come together with the standard peripheral library (latest version). No effect. Then I checked the EXCEL clock configuration tool and found that it produced in the wizard mode at least for f30x and f37x controller’s wrong entries for the rcc Registers (see the table in my first post PLLMUL must be 9 and PREDIV must be 1 to reach 72 MHz). In the expert mode it seems to produce more consistent parametrization however I in the case of the f302 it is still as shown in the table of my previous post. Probably it is necessary to check each register entry using reference manual.