AnsweredAssumed Answered

Bug in the STM32F4xx mbed library

Question asked by chernikov.vasily on Oct 8, 2015
Latest reply on Oct 8, 2015 by Nesrine M
Hello. There's an issue in MBED's spi_api.c file. Function

void spi_frequency(spi_t *obj, int hz)

line

baud_rate = __builtin_ffs(divisor) - 1;

The problem is this line makes a hardware divisor value (baud_rate) to become 1 step larger than it should be, resulting in 2x lower communication frequency than it is possible under given in HZ desired limitation. Let's say I'm going to have a power-of-2-casted divisor of 0x80. Then __builtin_ffs() returns "8" as it should; baud_rate then becomes "7", which is 0b111 - and that value corresponds to f_PCLK/256 according to STM32F4xx refman, not f_PCLK/128!

What we need is harware divisor values equal to 0b000 for 0,1 and 2 division rates, and "baud_rate-2" for all other cases. So i suggest the following code:

lsbIndex = __builtin_ffs(divisor);
baud_rate = lsbIndex>1 ? lsbIndex-2 : 0;

where lsbIndex is a new uint32_t variable.

Outcomes