AnsweredAssumed Answered

USART High speed baud rate on STM32F0Discovery not working

Question asked by Michael Hart on May 26, 2017
Latest reply on May 27, 2017 by Michael Hart

I am having a strange error using the STM32 standard peripheral library while using the USART. I need to be able to transmit at 4M baud rate on USART2. Although the board is stated to support up to 6M baud rate, only 3M and below work correctly.

I have looked through the SPL document and the reference manual for the board and found that my clock is the default at 48MHz and the oversampling is set to 8, confirmed by stepping through code in the debugger. Why is it that 3M and below work for the standard initialisation, but 4M, 5M, and 6M do not?

static void hal_init(void){    GPIO_InitTypeDef port_init;    USART_InitTypeDef usart_init;     //GPIO init: USART2 PA2 as OUT, PA3 as IN    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );     /* Connect PA2 to USART2 tx, PA3 to rx */    GPIO_PinAFConfig(GPIOA,  GPIO_PinSource3, GPIO_AF_1);    GPIO_PinAFConfig(GPIOA,  GPIO_PinSource2, GPIO_AF_1);     port_init.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;    port_init.GPIO_Mode = GPIO_Mode_AF;    port_init.GPIO_Speed = GPIO_Speed_50MHz;    port_init.GPIO_OType = GPIO_OType_PP;    port_init.GPIO_PuPd = GPIO_PuPd_UP;    GPIO_Init( GPIOA, &port_init );     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);     /* Need oversampling set for higher baud rates */    USART_OverSampling8Cmd(USART2, ENABLE);     //USART init: USART2 500K 8n1    usart_init.USART_BaudRate = USART_BAUD_RATE; // Currently 4M    usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;    usart_init.USART_Parity = USART_Parity_No;    usart_init.USART_StopBits = USART_StopBits_1;    usart_init.USART_WordLength = USART_WordLength_8b;    usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    USART_Init(USART2, (USART_InitTypeDef*) &usart_init);     USART_Cmd(USART2, ENABLE);}

For reference, the board is also using FreeRTOS and I am building and debugging with IAR-EWARM.

The faulty behaviour is tested by telling the board to continuously transmit 0xA5 (randomly chosen), and then setting the host to receive 10 bytes. In all baud rates 3M or below, a series of bytes of value 0xA5 are received, as expected. For 4M, if the oversampling is set to 16 (OVER8=0), values 0x00 are received. If oversampling is set to 8 (OVER8=1), no values at all are received, either by my Python script or by PuTTy with correct settings (as lower baud rates are received).