AnsweredAssumed Answered

STSW-STM32054 - STM32F10x standard peripheral library USART bug

Question asked by lee.krump on Nov 7, 2016
I've found a bug in source: "stm32f10x_usart.c", in function "USART_Init", where it calculates the BRR register value. In version 3.5.0 it starts from line 245, till 275.

A few examples of good, and bad results, assuming a 72MHz clock (36MHz apb clock, 8x oversampling disabled):
Baud: 4839; Calculated tmpreg: 0x1d00; Baud rate with this BRR: 4849; Expected BRR: 0x1d10.
Baud: 225000; Calculated tmpreg: 0xa0; Baud rate with this BRR: 225000;
Baud 225001; Calculated tmpreg: 0x90; Baud rate with this BRR: 250000; Expected BRR: 0xa0.
Baud: 226415; Calculated tmpreg: 0x9f; Baud rate with this BRR: 226415;
Baud: 250001; Calculated tmpreg: 0x80; Baud rate with this BRR: 281250; Expected BRR: 0x90.

The whole calculation can be corrected, and simplyfied without needing any intermediate variable:

  tmpreg = (apbclock + (USART_InitStruct->USART_BaudRate >> 1)) / (USART_InitStruct->USART_BaudRate);
  if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
      tmpreg = ((tmpreg & ~7) << 1) | (tmpreg & 0x7);