+ Baud rate calculation function

This commit is contained in:
Vovanium 2021-07-15 19:39:51 +03:00
parent 37f1174946
commit 9bc2570d00
3 changed files with 28 additions and 5 deletions

View File

@ -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;
end UART_429Disco;

17
source/stm32-f4-usart.adb Normal file
View File

@ -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;

View File

@ -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;