cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F10x Std. Periph. Lib: USART baud rate bug?

davidkoscheck9
Associate II
Posted on August 18, 2016 at 10:28

Hello, I disovered some weird behaviour with STM32F10x standard peripheral lib V.3.5.0: When I try to set a UART baud rate of 215,000 B/s via USART_Init() function, it results in a baud rate of 250,000 B/s instead (BRR = 0x60 at 24MHz). When I choose 214,000 or 216,000 everything works fine.

Is that a known bug?

#stm32-stdperiph_lib
2 REPLIES 2
Posted on August 18, 2016 at 23:27
  1. Is that a known bug?The math is definitely broken, and unnecessarily complicated, there have been other complaints

    C:\MSVC\STM32>usartf1

96 60 250000
112 70 214285
111 6F 216216

#include <windows.h> #include <stdio.h> typedef unsigned long uint32_t; typedef unsigned char uint8_t; void baud(uint32_t apbclock, uint32_t baudrate) { uint32_t tmpreg = 0x00; uint32_t integerdivider = 0x00; uint32_t fractionaldivider = 0x00; int over8 = 0; /* Determine the integer part */ if (over8) { /* Integer part computing in case Oversampling mode is 8 Samples */ integerdivider = ((25 * apbclock) / (2 * baudrate)); } else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */ { /* Integer part computing in case Oversampling mode is 16 Samples */ integerdivider = ((25 * apbclock) / (4 * baudrate)); } tmpreg = (integerdivider / 100) << 4; /* Determine the fractional part */ fractionaldivider = integerdivider - (100 * (tmpreg >> 4)); /* Implement the fractional part in the register */ if (over8) { tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07); } else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */ { tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F); } /* Write to USART BRR */ // USARTx->BRR = (uint16_t)tmpreg; printf('%3d %02X %10d ', tmpreg, tmpreg, apbclock / tmpreg); } int main(int argc, char **argv) { baud(24000000, 215000); baud(24000000, 214000); baud(24000000, 216000); return(1); }
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rugyi
Associate
Posted on November 09, 2016 at 04:57

I've posted a new [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/STSW-STM32054%20-%20STM32F10x%20standard%20peripheral%20library%20USART%20bug&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21]thread about the same issue, including the fix.

clive1 is absolutely right.