diff --git a/components/src/io_expander/pcf8574/pcf8574.adb b/components/src/io_expander/pcf8574/pcf8574.adb new file mode 100644 index 000000000..8d94f770c --- /dev/null +++ b/components/src/io_expander/pcf8574/pcf8574.adb @@ -0,0 +1,39 @@ +-- +-- Copyright 2022 (C) Rolf Ebert +-- +-- SPDX-License-Identifier: BSD-3-Clause +-- + + +package body PCF8574 is + + ----------- + -- Get -- + ----------- + + function Get (This : PCF8574_Module) return UInt8 + is + Val : I2C_Data (1 .. 1); + Status : I2C_Status; + begin + This.Port.Master_Receive (This.Addr, Val, Status); + return Val (1); + end Get; + + procedure Get (This : PCF8574_Module; Data : out UInt8) + is begin + Data := Get (This); + end Get; + + ----------- + -- Set -- + ----------- + + procedure Set (This : PCF8574_Module; Data : UInt8) + is + Status : I2C_Status; + begin + 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 new file mode 100644 index 000000000..6cdcbeca0 --- /dev/null +++ b/components/src/io_expander/pcf8574/pcf8574.ads @@ -0,0 +1,38 @@ +-- +-- Copyright 2022 (C) Rolf Ebert +-- +-- SPDX-License-Identifier: BSD-3-Clause +-- + +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 + +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 limited private; + + type Any_PCF8574_Module is access all PCF8574_Module'Class; + + + procedure Set (This : PCF8574_Module; Data : 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: + -- M.Set (16#FF#); + -- Keys := M.Get; + +private + + type PCF8574_Module (Port : not null Any_I2C_Port; + Addr : I2C_Address) is tagged limited null record; + +end PCF8574;