Skip to content

Commit 904d4e4

Browse files
committed
Make ticker computation use shift-by-0
Runtime code that analysed clock frequency to determine numerator and denominator for conversion to standard 1MHz failed to handle the case of either being 1 correctly. Although it would spot other values that could be performed as shifts, it failed to spot that 1 is "shift by 0", so would end up doing runtime multiply and/or divide by 1. The runtime divide by 1 could be slow on a Cortex-M0 device, increasing interrupt latency. UART character loss on STM32F0 devices has been traced to this incorrect code. Correct the `exact_log2` routine so that `exact_log2(1)` returns 0 to fix this. Original code had a single special no-multiply-or-divide case for hardware clock frequency being exactly 1MHz, as USTICKER is on STM32F0 - this code lacks that but has a more general special case that covers all shift-convertible frequencies like 500kHz or 8MHz, which should be similar speed as shifts are cheap.
1 parent 35b8e55 commit 904d4e4

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

hal/source/mbed_ticker_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static inline uint32_t gcd(uint32_t a, uint32_t b)
110110

111111
static int exact_log2(uint32_t n)
112112
{
113-
for (int i = 31; i > 0; --i) {
113+
for (int i = 31; i >= 0; --i) {
114114
if ((1U << i) == n) {
115115
return i;
116116
}

0 commit comments

Comments
 (0)