From f2fd11b7d0f98bca942b29dda0723f4527b19b87 Mon Sep 17 00:00:00 2001 From: RREE Date: Sun, 10 Apr 2022 14:28:21 +0200 Subject: [PATCH 1/4] new component: the old pcf8574 8bit io-expander. --- .../src/io_expander/pcf8574/pcf8574.adb | 51 +++++++++++++++++++ .../src/io_expander/pcf8574/pcf8574.ads | 41 +++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 components/src/io_expander/pcf8574/pcf8574.adb create mode 100644 components/src/io_expander/pcf8574/pcf8574.ads diff --git a/components/src/io_expander/pcf8574/pcf8574.adb b/components/src/io_expander/pcf8574/pcf8574.adb new file mode 100644 index 000000000..7e2f24480 --- /dev/null +++ b/components/src/io_expander/pcf8574/pcf8574.adb @@ -0,0 +1,51 @@ +-- +-- Copyright 2022 (C) Rolf Ebert +-- +-- SPDX-License-Identifier: BSD-3-Clause +-- + + +package body PCF8574 is + + ----------------- + -- Configure -- + ----------------- + + procedure Configure (This : in out Module; + Port : Any_I2C_Master_Port; + Addr : Module_Address) + is begin + This.Port := Port; + This.Addr := Addr; + end Configure; + + ----------- + -- Get -- + ----------- + + function Get (This : Module) return UInt8 + is + Val : I2C_Data (1 .. 1); + Status : I2C_Status; + begin + This.Port.Receive (This.Addr, Val, Status); + return Val(1); + end Get; + + procedure Get (This : Module; Data : out UInt8) + is begin + Data := Get (This); + end Get; + + ----------- + -- Set -- + ----------- + + procedure Set (This : Module; Data : UInt8) + is + Status : I2C_Status; + begin + This.Port.Transmit (This.Addr, (1=>Data), Status); + end Set; + +end PCF8574; diff --git a/components/src/io_expander/pcf8574/pcf8574.ads b/components/src/io_expander/pcf8574/pcf8574.ads new file mode 100644 index 000000000..5080fcd19 --- /dev/null +++ b/components/src/io_expander/pcf8574/pcf8574.ads @@ -0,0 +1,41 @@ +-- +-- Copyright 2022 (C) Rolf Ebert +-- +-- SPDX-License-Identifier: BSD-3-Clause +-- + +with HAL; use HAL; +with HAL.I2C; use HAL.I2C; +with HAL.I2C.Master; use HAL.I2C.Master; + +-- I2C 8-bit IO expander with quasi bidirectional I/O, no data +-- direction, no latch + +package PCF8574 is + + subtype Module_Address is I2C_7bit_Address range 16#20# .. 16#2F#; + + type Module is tagged private; + type Any_Module is access all Module'Class; + + procedure Configure (This : in out Module; + Port : Any_I2C_Master_Port; + Addr : Module_Address); + + procedure Set (This : Module; Data : UInt8); + + function Get (This : Module) return UInt8; + procedure Get (This : Module; Data : out UInt8); + -- when reading the input from keys (buttons) carefully read the + -- datasheet. The input line should be set high before reading. + -- E.g. if all lines are key input: + -- M.Set (16#FF#); + -- Keys := M.Get; + +private + type Module is tagged record + Port : Any_I2C_Master_Port; + Addr : Module_Address; + end record; + +end PCF8574; From eaaaa1a6d50cbdb293cfc1edd39e75efc9d49cf9 Mon Sep 17 00:00:00 2001 From: RREE Date: Sun, 1 May 2022 17:31:03 +0200 Subject: [PATCH 2/4] correct interface to current HAL (v0.3) --- .../src/io_expander/pcf8574/pcf8574.adb | 10 ++++---- .../src/io_expander/pcf8574/pcf8574.ads | 25 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/components/src/io_expander/pcf8574/pcf8574.adb b/components/src/io_expander/pcf8574/pcf8574.adb index 7e2f24480..7163d2173 100644 --- a/components/src/io_expander/pcf8574/pcf8574.adb +++ b/components/src/io_expander/pcf8574/pcf8574.adb @@ -11,9 +11,9 @@ package body PCF8574 is -- Configure -- ----------------- - procedure Configure (This : in out Module; - Port : Any_I2C_Master_Port; - Addr : Module_Address) + procedure Configure (This : in out PCF8574_Module; + Port : Any_I2C_Port; + Addr : PCF8574_Address) is begin This.Port := Port; This.Addr := Addr; @@ -23,7 +23,7 @@ package body PCF8574 is -- Get -- ----------- - function Get (This : Module) return UInt8 + function Get (This : PCF8574_Module) return UInt8 is Val : I2C_Data (1 .. 1); Status : I2C_Status; @@ -41,7 +41,7 @@ package body PCF8574 is -- Set -- ----------- - procedure Set (This : Module; Data : UInt8) + procedure Set (This : PCF8574_Module; Data : UInt8) is Status : I2C_Status; begin diff --git a/components/src/io_expander/pcf8574/pcf8574.ads b/components/src/io_expander/pcf8574/pcf8574.ads index 5080fcd19..0cd78f650 100644 --- a/components/src/io_expander/pcf8574/pcf8574.ads +++ b/components/src/io_expander/pcf8574/pcf8574.ads @@ -6,26 +6,25 @@ with HAL; use HAL; with HAL.I2C; use HAL.I2C; -with HAL.I2C.Master; use HAL.I2C.Master; -- I2C 8-bit IO expander with quasi bidirectional I/O, no data -- direction, no latch package PCF8574 is - subtype Module_Address is I2C_7bit_Address range 16#20# .. 16#2F#; + subtype PCF8574_Address is I2C_Address range 16#40# .. 16#5F#; - type Module is tagged private; - type Any_Module is access all Module'Class; + type PCF8574_Module is tagged private; + type Any_PCF8574_Module is access all PCF8574_Module'Class; - procedure Configure (This : in out Module; - Port : Any_I2C_Master_Port; - Addr : Module_Address); + procedure Configure (This : in out PCF8574_Module; + Port : Any_I2C_Port; + Addr : PCF8574_Address); - procedure Set (This : Module; Data : UInt8); + procedure Set (This : PCF8574_Module; Data : UInt8); - function Get (This : Module) return UInt8; - procedure Get (This : Module; Data : out UInt8); + function Get (This : PCF8574_Module) return UInt8; + procedure Get (This : PCF8574_Module; Data : out UInt8); -- when reading the input from keys (buttons) carefully read the -- datasheet. The input line should be set high before reading. -- E.g. if all lines are key input: @@ -33,9 +32,9 @@ package PCF8574 is -- Keys := M.Get; private - type Module is tagged record - Port : Any_I2C_Master_Port; - Addr : Module_Address; + type PCF8574_Module is tagged record + Port : Any_I2C_Port; + Addr : PCF8574_Address; end record; end PCF8574; From df80b9038a492c08328463488580b0198b0a6e87 Mon Sep 17 00:00:00 2001 From: RREE Date: Mon, 2 May 2022 21:19:22 +0200 Subject: [PATCH 3/4] make address and I2C_Port discriminants, so that variables cannot be declared without initialization --- .../src/io_expander/pcf8574/pcf8574.adb | 19 ++++--------------- .../src/io_expander/pcf8574/pcf8574.ads | 14 ++++++-------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/components/src/io_expander/pcf8574/pcf8574.adb b/components/src/io_expander/pcf8574/pcf8574.adb index 7163d2173..2a26e88a5 100644 --- a/components/src/io_expander/pcf8574/pcf8574.adb +++ b/components/src/io_expander/pcf8574/pcf8574.adb @@ -7,18 +7,6 @@ package body PCF8574 is - ----------------- - -- Configure -- - ----------------- - - procedure Configure (This : in out PCF8574_Module; - Port : Any_I2C_Port; - Addr : PCF8574_Address) - is begin - This.Port := Port; - This.Addr := Addr; - end Configure; - ----------- -- Get -- ----------- @@ -28,11 +16,12 @@ package body PCF8574 is Val : I2C_Data (1 .. 1); Status : I2C_Status; begin - This.Port.Receive (This.Addr, Val, Status); + -- if not This.Is_Initialized then raise Program_Error; end if; + This.Port.Master_Receive (This.Addr, Val, Status); return Val(1); end Get; - procedure Get (This : Module; Data : out UInt8) + procedure Get (This : PCF8574_Module; Data : out UInt8) is begin Data := Get (This); end Get; @@ -45,7 +34,7 @@ package body PCF8574 is is Status : I2C_Status; begin - This.Port.Transmit (This.Addr, (1=>Data), Status); + This.Port.Master_Transmit (This.Addr, (1=>Data), Status); end Set; end PCF8574; diff --git a/components/src/io_expander/pcf8574/pcf8574.ads b/components/src/io_expander/pcf8574/pcf8574.ads index 0cd78f650..587083f70 100644 --- a/components/src/io_expander/pcf8574/pcf8574.ads +++ b/components/src/io_expander/pcf8574/pcf8574.ads @@ -14,12 +14,11 @@ package PCF8574 is subtype PCF8574_Address is I2C_Address range 16#40# .. 16#5F#; - type PCF8574_Module is tagged private; + type PCF8574_Module (Port : not null Any_I2C_Port; + Addr : I2C_Address) is tagged private; + type Any_PCF8574_Module is access all PCF8574_Module'Class; - procedure Configure (This : in out PCF8574_Module; - Port : Any_I2C_Port; - Addr : PCF8574_Address); procedure Set (This : PCF8574_Module; Data : UInt8); @@ -32,9 +31,8 @@ package PCF8574 is -- Keys := M.Get; private - type PCF8574_Module is tagged record - Port : Any_I2C_Port; - Addr : PCF8574_Address; - end record; + + type PCF8574_Module (Port : not null Any_I2C_Port; + Addr : I2C_Address) is tagged null record; end PCF8574; From 2228fc370f619dfb39041a8b9301110092742fe8 Mon Sep 17 00:00:00 2001 From: RREE Date: Tue, 3 May 2022 12:34:57 +0200 Subject: [PATCH 4/4] cleared style issues (missing blanks) and made the type limited --- components/src/io_expander/pcf8574/pcf8574.adb | 5 ++--- components/src/io_expander/pcf8574/pcf8574.ads | 18 +++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/components/src/io_expander/pcf8574/pcf8574.adb b/components/src/io_expander/pcf8574/pcf8574.adb index 2a26e88a5..8d94f770c 100644 --- a/components/src/io_expander/pcf8574/pcf8574.adb +++ b/components/src/io_expander/pcf8574/pcf8574.adb @@ -16,9 +16,8 @@ package body PCF8574 is Val : I2C_Data (1 .. 1); Status : I2C_Status; begin - -- if not This.Is_Initialized then raise Program_Error; end if; This.Port.Master_Receive (This.Addr, Val, Status); - return Val(1); + return Val (1); end Get; procedure Get (This : PCF8574_Module; Data : out UInt8) @@ -34,7 +33,7 @@ package body PCF8574 is is Status : I2C_Status; begin - This.Port.Master_Transmit (This.Addr, (1=>Data), Status); + This.Port.Master_Transmit (This.Addr, (1 => Data), Status); end Set; end PCF8574; diff --git a/components/src/io_expander/pcf8574/pcf8574.ads b/components/src/io_expander/pcf8574/pcf8574.ads index 587083f70..6cdcbeca0 100644 --- a/components/src/io_expander/pcf8574/pcf8574.ads +++ b/components/src/io_expander/pcf8574/pcf8574.ads @@ -7,15 +7,15 @@ with HAL; use HAL; with HAL.I2C; use HAL.I2C; --- I2C 8-bit IO expander with quasi bidirectional I/O, no data --- direction, no latch +-- I2C 8-bit IO expander with quasi bidirectional I/O, no data +-- direction, no latch package PCF8574 is subtype PCF8574_Address is I2C_Address range 16#40# .. 16#5F#; type PCF8574_Module (Port : not null Any_I2C_Port; - Addr : I2C_Address) is tagged private; + Addr : I2C_Address) is tagged limited private; type Any_PCF8574_Module is access all PCF8574_Module'Class; @@ -24,15 +24,15 @@ package PCF8574 is function Get (This : PCF8574_Module) return UInt8; procedure Get (This : PCF8574_Module; Data : out UInt8); - -- when reading the input from keys (buttons) carefully read the - -- datasheet. The input line should be set high before reading. - -- E.g. if all lines are key input: - -- M.Set (16#FF#); - -- Keys := M.Get; + -- when reading the input from keys (buttons) carefully read the + -- datasheet. The input line should be set high before reading. + -- E.g. if all lines are key input: + -- M.Set (16#FF#); + -- Keys := M.Get; private type PCF8574_Module (Port : not null Any_I2C_Port; - Addr : I2C_Address) is tagged null record; + Addr : I2C_Address) is tagged limited null record; end PCF8574;