2017-05-26 01:43 PM
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).
#stm32f02017-05-26 02:34 PM
And what's the content of respective USART_BRR register? And/or other relevant USART registers?
JW
2017-05-26 02:50 PM
I haven't read them, but I have tested hardcoding the BRR register to the values suggested in the reference manual - that works for 3M or below, but not 4M or above. As for the other registers, I can look them all up, unless it's sufficient to say that they are only written to by the standard peripheral library, which I assumed was correct?
2017-05-26 02:57 PM
With high baud rates you tend to get into the granularity of the source clock, and the baud clock error becomes more dominant. For 4Mbaud consider running the machine a 32 MHz. Also check the APBx clocks.
Send an 0x55 pattern and confirm bit timing with a scope.
2017-05-27 04:01 AM
Thanks Clive, I've finally got my logic analyser working and found that the STM32 is sending bits at a steady 3.90625MHz. It appears I've been accusing the wrong end of the link.