diff --git a/examples/uart/source/uart_429disco.adb b/examples/uart/source/uart_429disco.adb index 5a2ee7c..bd200fa 100644 --- a/examples/uart/source/uart_429disco.adb +++ b/examples/uart/source/uart_429disco.adb @@ -1,7 +1,6 @@ with STM32.F429Z; use STM32.F429Z; use STM32.F429Z.Modules.GPIO; use STM32.F429Z.Modules.USART; -use STM32.F429Z.Modules.RCC; with Ada.Real_Time; use Ada.Real_Time; @@ -23,8 +22,8 @@ procedure UART_429Disco is end; APB2_Frequency : constant := 90_000_000; -- Set by board support - Baud_Rate : constant := 115_200; - Ratio : constant := (APB2_Frequency + Baud_Rate / 2) / Baud_Rate; + Baud : constant := 115_200; + --Ratio : constant := (APB2_Frequency + Baud_Rate / 2) / Baud_Rate; Period: constant Time_Span := Milliseconds(100); Now: Time := Clock; @@ -92,7 +91,8 @@ begin UART_Module.CR3 := R; end; - UART_Module.BRR := (DIV_Mantissa => Ratio / 16, DIV_Fraction => Ratio mod 16, others => 0); + --UART_Module.BRR := (DIV_Mantissa => Ratio / 16, DIV_Fraction => Ratio mod 16, others => 0); + UART_Module.BRR := Baud_Rate (Speed => Baud, OVER8 => False, Bus_Frequency => APB2_Frequency); declare R : Control_Register_1 := UART_Module.CR1; @@ -109,4 +109,4 @@ begin delay until Now; end loop; -end UART_429Disco; \ No newline at end of file +end UART_429Disco; diff --git a/source/stm32-f4-usart.adb b/source/stm32-f4-usart.adb new file mode 100644 index 0000000..10842a1 --- /dev/null +++ b/source/stm32-f4-usart.adb @@ -0,0 +1,17 @@ +package body STM32.F4.USART is + + function Baud_Rate ( + Speed: Positive; + OVER8: Boolean; + Bus_Frequency: Positive) + return Baud_Rate_Register + is + Ratio : Natural := (Bus_Frequency + Speed / 2) / Speed; + Oversampling : Positive := (if OVER8 then 8 else 16); + begin + return (DIV_Mantissa => Ratio / Oversampling, + DIV_Fraction => Ratio mod Oversampling, + others => 0); + end; + +end STM32.F4.USART; diff --git a/source/stm32-f4-usart.ads b/source/stm32-f4-usart.ads index 651edc9..2543ee4 100644 --- a/source/stm32-f4-usart.ads +++ b/source/stm32-f4-usart.ads @@ -251,4 +251,10 @@ package STM32.F4.USART is GTPR at 16#18# range 0 .. 31; end record; + function Baud_Rate ( + Speed: Positive; + OVER8: Boolean; + Bus_Frequency: Positive) + return Baud_Rate_Register; + end STM32.F4.USART;