* no need in that scattered port declaration. Let's define then with GPIO.Ports generics!

This commit is contained in:
Vovanium 2024-03-23 23:51:45 +03:00
parent c39c2981d1
commit 3ec6b5a5af
8 changed files with 43 additions and 51 deletions

View File

@ -1,11 +1,9 @@
with STM32.GPIO, STM32.Reset_and_Clock;
use STM32.GPIO, STM32.Reset_and_Clock;
with STM32.GPIO.Ports, STM32.Reset_and_Clock;
use STM32.GPIO, STM32.Reset_and_Clock;
package Board is
LED_Port : GPIO_Registers renames GPIOC;
LED_Bit : constant Port_Bit_Number := 13;
LED_On : constant Boolean := False;
package LED is new Ports.GPIO_Port_Boolean (GPIOC, 13, Invert => True);
LED_RCC_EN : Boolean renames RCC.AHB1ENR (Index.GPIOC);

View File

@ -1,11 +1,9 @@
with STM32.GPIO, STM32.Reset_and_Clock;
use STM32.GPIO, STM32.Reset_and_Clock;
with STM32.GPIO.Ports, STM32.Reset_and_Clock;
use STM32.GPIO, STM32.Reset_and_Clock;
package Board is
LED_Port : GPIO_Registers renames GPIOD;
LED_Bit : constant Port_Bit_Number := 3;
LED_On : constant Boolean := False;
package LED is new Ports.GPIO_Port_Boolean (GPIOD, 3, Invert => True);
LED_RCC_EN : Boolean renames RCC.AHB1ENR (Index.GPIOD);

View File

@ -1,19 +1,15 @@
with STM32.GPIO, STM32.Reset_and_Clock;
use STM32.GPIO, STM32.Reset_and_Clock;
with STM32.GPIO.Ports, STM32.Reset_and_Clock;
use STM32.GPIO, STM32.Reset_and_Clock;
package Board is
-- LED
LED_Port : GPIO_Registers renames GPIOG;
LED_Bit : constant Port_Bit_Number := 13;
LED_On : constant Boolean := True;
package LED is new Ports.GPIO_Port_Boolean (GPIOG, 13, Invert => False);
LED_RCC_EN : Boolean renames RCC.AHB1ENR (Index.GPIOG);
LED_2_Port : GPIO_Registers renames GPIOG;
LED_2_Bit : constant Port_Bit_Number := 14;
LED_2_On : constant Boolean := True;
package LED_2 is new Ports.GPIO_Port_Boolean (GPIOG, 14, Invert => False);
LED_2_RCC_EN : Boolean renames RCC.AHB1ENR (Index.GPIOG);

View File

@ -1,16 +1,15 @@
with Board; use Board;
with STM32.Reset_and_Clock; use STM32.Reset_and_Clock;
with STM32.GPIO.Ports;
use STM32.GPIO.Ports, STM32.GPIO;
with STM32.GPIO;
use STM32.GPIO;
with Ada.Real_Time; use Ada.Real_Time;
procedure LED_Flasher is
Period: constant Time_Span := Milliseconds(250);
On_Time: constant Time_Span := Milliseconds(10);
Now: Time := Clock;
package LED is new GPIO_Port_Boolean(LED_Port, LED_Bit);
Period : constant Time_Span := Milliseconds(250);
On_Time : constant Time_Span := Milliseconds(10);
Now : Time := Clock;
begin
LED_RCC_EN := True;
@ -19,9 +18,9 @@ begin
LED.Set_OSPEEDR (Very_High_Speed);
LED.Set_PUPDR (No_Pull);
loop
LED.Set (LED_On);
LED.Set (True);
delay until Now + On_Time;
LED.Set (not LED_On);
LED.Set (False);
Now := Now + Period;
delay until Now;
end loop;

View File

@ -1,12 +1,11 @@
with Board; use Board;
with STM32.Reset_and_Clock; use STM32.Reset_and_Clock;
with STM32.GPIO.Ports;
use STM32.GPIO.Ports, STM32.GPIO;
with STM32.GPIO;
use STM32.GPIO;
procedure LED_Flasher is
package LED is new GPIO_Port_Boolean(LED_Port, LED_Bit);
procedure Dly (X : Positive) is
begin
@ -22,9 +21,9 @@ begin
LED.Set_OSPEEDR (Very_High_Speed);
LED.Set_PUPDR (No_Pull);
loop
LED.Set (LED_On);
LED.Set (True);
Dly (1000000);
LED.Set (not LED_On);
LED.Set (False);
Dly (3000000);
end loop;
end Led_Flasher;

View File

@ -6,7 +6,7 @@ package body STM32.GPIO.Ports is
begin
Register.BSRR := (
BR => 2**Bit,
BS => 2**Bit * Boolean'Pos(Value)
BS => 2**Bit * Boolean'Pos(Value xor Invert)
);
end;
@ -44,20 +44,20 @@ package body STM32.GPIO.Ports is
package body GPIO_Port_Modular is
Size: constant Positive := 1 + Last_Bit - First_Bit;
Mask: constant Unsigned_16 := 2**(Last_Bit + 1) - 2**First_Bit;
Size : constant Positive := 1 + Last_Bit - First_Bit;
Mask : constant Unsigned_16 := 2**(Last_Bit + 1) - 2**First_Bit;
procedure Set(Value: Value_Type) is
procedure Set(Value : Value_Type) is
begin
Register.BSRR := (
BR => Mask,
BS => 2**First_Bit * Unsigned_16(Value)
BS => 2**First_Bit * Unsigned_16(Value xor Invert)
);
end;
function Value return Value_Type is
begin
return Value_Type((Register.IDR / 2**First_Bit) and (2**Size - 1));
return Value_Type((Register.IDR / 2**First_Bit) and (2**Size - 1)) xor Invert;
end;
procedure Set_MODER (Mode : Port_Mode) is

View File

@ -2,8 +2,9 @@ package STM32.GPIO.Ports is
-- Single GPIO pin
generic
Register : in out GPIO_Registers;
Bit : Port_Bit_Number;
Register : in out GPIO_Registers;
Bit : Port_Bit_Number;
Invert : Boolean := False;
package GPIO_Port_Boolean is
procedure Set (Value : Boolean) with Inline; -- Output to port
function Value return Boolean with Inline; -- Read port
@ -19,9 +20,10 @@ package STM32.GPIO.Ports is
-- Multi-pin GPIO
generic
type Value_Type is mod <>;
Register: in out GPIO_Registers;
First_Bit: Port_Bit_Number;
Last_Bit: Port_Bit_Number;
Register : in out GPIO_Registers;
First_Bit : Port_Bit_Number;
Last_Bit : Port_Bit_Number;
Invert : Value_Type := 0;
package GPIO_Port_Modular is
procedure Set(Value: Value_Type) with Inline;
function Value return Value_Type with Inline;

View File

@ -175,15 +175,15 @@ package STM32.GPIO is
with Pack, Size => 64;
type GPIO_Registers is record
MODER: Port_Mode_Register;
OTYPER: Output_Type_Register;
OSPEEDR: Output_Speed_Register;
PUPDR: Port_Pull_Register;
IDR: Interfaces.Unsigned_32;
ODR: Interfaces.Unsigned_32;
BSRR: Bit_Set_Reset_Register;
LCKR: Configuration_Lock_Register;
AFR: Alternate_Function_Register;
MODER : Port_Mode_Register;
OTYPER : Output_Type_Register;
OSPEEDR : Output_Speed_Register;
PUPDR : Port_Pull_Register;
IDR : Interfaces.Unsigned_32;
ODR : Interfaces.Unsigned_32;
BSRR : Bit_Set_Reset_Register;
LCKR : Configuration_Lock_Register;
AFR : Alternate_Function_Register;
end record with Volatile;
for GPIO_Registers use record