Skip to content

I2C HAL ideas #401

Description

@RREE

I think the hal-i2c.ads lacks some comments on how to use it. Here are some ideas for modifications:

package HAL.I2C is

   type I2C_Status is
     (Ok,
      Error,         -- Err_Error,
      Timeout,       -- Err_Timeout,
      No_Connection, -- ?
      Busy);         -- ?

   subtype I2C_Data is UInt8_Array;

   -- when requesting data from the device, do we (the master) send
   -- one (Memory_Size_8b) or two bytes to the device.
   -- type I2C_Memory_Address_Size is
   --   (Memory_Size_8b,    -- single command or address
   --    Memory_Size_16b);  -- two bytes, BE or LE?


   -- subtype I2C_Address is UInt10;
   -- 7 and 10-bit addressess must be distinct types
   subtype I2C_7bit_Address is UInt7 range 8 .. 16#77#;
   subtype I2C_10bit_Address is Uint10;
   -- do we really support 10bit addresses? If yes, we have to
   -- overload all routines with 7 and 10 bit or add a parameter that
   -- tells us, if we have to handle the given value as a 7 or 10 bit
   -- address.  Honestly I had never seen a real 10bit device. I
   -- propose to only support 7bit addresses

   type I2C_Port is limited interface;

   type Any_I2C_Port is access all I2C_Port'Class;

   -- Create I2C start condition.  Send the Data to the device (slave)
   -- at I2C_Address through the port This.  The number of bytes is
   -- determined by the length of Data.  Ends with I2C stop condition.
   -- Success or failure is reported in Status.
   procedure Master_Transmit
     (This    : in out I2C_Port;
      Addr    : I2C_7bit_Address;
      Data    : I2C_Data;
      Status  : out I2C_Status;
      Timeout : Natural := 1000) is abstract;
   procedure Master_Transmit
     (This    : in out I2C_Port;
      Addr    : I2C_7bit_Address;
      Data    : UInt8; -- avoid tedious wrapping in (1 => ...)
      Status  : out I2C_Status;
      Timeout : Natural := 1000) is abstract;

   --
   --  Is Master_Receive really useful? I cannot imagine a situation
   --  or a device where you can receive data without prior
   --  information to the device.
   --

   -- Create I2C start condition.  Receive Data from the device
   -- (slave) at I2C_Address through the port This.  The number of
   -- expected bytes is determined by the length of Data.  Ends with
   -- I2C stop condition.  Success or failure is reported in Status.
   procedure Master_Receive
     (This    : in out I2C_Port;
      Addr    : I2C_7bit_Address;
      Data    : out I2C_Data;
      Status  : out I2C_Status;
      Timeout : Natural := 1000) is abstract;

   -- Create I2C start condition.  Send the Send_Data to the device
   -- (slave) at I2C_Address through the port This.  The number of
   -- bytes is determined by the length of Send_Data.  Without
   -- intermediate stop request Recv_Data'Length bytes from the same
   -- client.  Ends with I2C stop condition.  Success or failure is
   -- reported in Status.
   procedure Master_Transmit_And_Receive
     (This      : in out I2C_Port;
      Addr      : I2C_7bit_Address;
      Send_Data : I2C_Data;
      Recv_Data : out I2_Data;
      Status    : out I2C_Status;
      Timeout   : Natural := 1000) is abstract;
   procedure Master_Transmit_And_Receive
     (This      : in out I2C_Port;
      Addr      : I2C_7bit_Address;
      Send_Data : UInt8;
      Recv_Data : out I2_Data;
      Status    : out I2C_Status;
      Timeout   : Natural := 1000) is abstract;

   --  See if there is a device at the address Addr. Report presence
   --  (Status = Ok) or absence (Status = No_Connection). Detects the ACK after sending the address.
   procedure Detect_Connection
     (This      : in out I2C_Port;
      Addr      : I2C_7bit_Address;
      Status    : out I2C_Status) is abstract;


   --
   --  In my opinion we don't need these routines. We can keep them
   --  for compatibility with the existing interface
   --

   --  -- Same as Master_Transmit.  Depending on Mem_Addr_Size one or two
   --  -- bytes from Mem_Addr are prepened before sending Data. Ends with
   --  -- I2C stop condition.
   --  procedure Mem_Write
   --    (This          : in out I2C_Port;
   --     Addr          : I2C_7bit_Address;
   --     Mem_Addr      : UInt16;
   --     Mem_Addr_Size : I2C_Memory_Address_Size;
   --     Data          : I2C_Data;
   --     Status        : out I2C_Status;
   --     Timeout       : Natural := 1000) is abstract;

   --  -- Depending on Mem_Addr_Size first send one or two bytes from
   --  -- Mem_Addr to the slave at address I2C_Address. Then receive
   --  -- Data'Length bytes in Data. Ends with I2C stop condition.
   --  procedure Mem_Read
   --    (This          : in out I2C_Port;
   --     Addr          : I2C_7bit_Address;
   --     Mem_Addr      : UInt16;
   --     Mem_Addr_Size : I2C_Memory_Address_Size;
   --     Data          : out I2C_Data;
   --     Status        : out I2C_Status;
   --     Timeout       : Natural := 1000) is abstract;

end HAL.I2C;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions