From 7cf623aaf209cadf6c34726edca3d865dd333f21 Mon Sep 17 00:00:00 2001 From: Riccardo Binetti Date: Wed, 6 Jul 2022 23:57:23 +0200 Subject: [PATCH] stm32f407: use the correct bus frequency to calculate usartdiv (#57) USARTs 1 and 6 are clocked by APB2 --- src/modules/chips/stm32f407/stm32f407.zig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/modules/chips/stm32f407/stm32f407.zig b/src/modules/chips/stm32f407/stm32f407.zig index 724e410..b11a69a 100644 --- a/src/modules/chips/stm32f407/stm32f407.zig +++ b/src/modules/chips/stm32f407/stm32f407.zig @@ -294,7 +294,13 @@ pub fn Uart(comptime index: usize, comptime pins: micro.uart.Pins) type { // TODO: We assume the default OVER8=0 configuration above (i.e. 16x oversampling). // TODO: Do some checks to see if the baud rate is too high (or perhaps too low) // TODO: Do a rounding div, instead of a truncating div? - const usartdiv = @intCast(u16, @divTrunc(micro.clock.get().apb1, config.baud_rate)); + const clocks = micro.clock.get(); + const bus_frequency = switch (index) { + 1, 6 => clocks.apb2, + 2...5 => clocks.apb1, + else => unreachable, + }; + const usartdiv = @intCast(u16, @divTrunc(bus_frequency, config.baud_rate)); @field(regs, usart_name).BRR.raw = usartdiv; // enable USART, and its transmitter and receiver