Skip to content

Commit

Permalink
Move MCP23017 register address constants to private section per issue #8
Browse files Browse the repository at this point in the history
.  Add function to determine if a MCP23017 is present and update documentation.
  • Loading branch information
BrentSeidel committed Sep 11, 2024
1 parent 5c460fb commit 4e72cc5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 43 deletions.
Binary file modified doc/devices.pdf
Binary file not shown.
13 changes: 12 additions & 1 deletion doc/devices.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,17 @@ \subsection{\package{BBS.embed.i2c.MCP23017}}
\item $error$ - The error code from any I2C transactions.
\end{itemize}

\begin{lstlisting}
function present(port : i2c_interface;
addr : addr7) return boolean;
\end{lstlisting}
\indexfunc{present}
Check to see if a MCP23017 is present at the specified I2C address. First it checks if the address is in the range for the MCP23017 and then if a read of one of the device registers completes successfully.
\begin{itemize}
\item $port$ - The I2C bus object that the MCP23017 is connected to.
\item $addr$ - The I2C address of the device to check.
\end{itemize}

\begin{lstlisting}
procedure set_dir(self : MCP23017_record; dir : uint16;
error : out err_code);
Expand Down Expand Up @@ -3708,7 +3719,7 @@ \section{\package{BBS.embed.SPI.Due}}
\printindex[type]
\printindex[func]
%
% Add bibleography
% Add bibliography
%
\addcontentsline{toc}{chapter}{Bibliography}
\bibliographystyle{plain}
Expand Down
19 changes: 19 additions & 0 deletions src-common/bbs-embed-i2c-mcp23017.adb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ package body BBS.embed.i2c.MCP23017 is
self.address := addr;
end;
--
-- Check to see if the configured device is present.
--
function present(port : i2c_interface;
addr : addr7) return boolean is
err : err_code;
temp : uint8;
pragma unreferenced (temp); -- Needed for the read, but value is ignored
begin
--
-- First check to see if address is in range, then check if a device
-- responds at that address.
--
if (addr < addr_0) or (addr > addr_7) then
return False;
end if;
temp := port.read(addr, IOCON, err);
return err = NONE;
end;
--
-- Set the direction (read(0)/write(1)) for each of the output bits. The
-- direction bits are packed into a uint16.
--
Expand Down
89 changes: 47 additions & 42 deletions src-common/bbs-embed-i2c-mcp23017.ads
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,6 @@ package BBS.embed.i2c.MCP23017 is
addr_6 : constant addr7 := 16#26#;
addr_7 : constant addr7 := 16#27#;
--
-- Device registers. The MCP23017 can be configured to work as two separate
-- 8 bit ports or one 16 bit port. Depending on the setting of the BANK bit
-- in the IOCON register, the _A and _B registers are used or the register
-- with no suffix is used.
--
-- This application will configure the device to work as a single 16 bit I/O
-- port.
--
IODIR : constant uint8 := 16#00#; -- I/O Direction
IODIR_A : constant uint8 := 16#00#; -- I/O Direction
IODIR_B : constant uint8 := 16#10#; -- I/O Direction
IPOL : constant uint8 := 16#02#; -- Input polarity
IPOL_A : constant uint8 := 16#01#; -- Input polarity
IPOL_B : constant uint8 := 16#11#; -- Input polarity
GPINTEN : constant uint8 := 16#04#; -- Interrupt-on-change control
GPINTEN_A : constant uint8 := 16#02#; -- Interrupt-on-change control
GPINTEN_B : constant uint8 := 16#12#; -- Interrupt-on-change control
DEFVAL : constant uint8 := 16#06#; --Default compare
DEFVAL_A : constant uint8 := 16#03#; --Default compare
DEFVAL_B : constant uint8 := 16#13#; --Default compare
INTCON : constant uint8 := 16#08#; -- Interrupt control
INTCON_A : constant uint8 := 16#04#; -- Interrupt control
INTCON_B : constant uint8 := 16#14#; -- Interrupt control
IOCON : constant uint8 := 16#0A#; -- Control register
IOCON_A : constant uint8 := 16#05#; -- Control register
IOCON_B : constant uint8 := 16#15#; -- Control register
GPPU : constant uint8 := 16#0C#; -- Pull-up resistor configuration
GPPU_A : constant uint8 := 16#06#; -- Pull-up resistor configuration
GPPU_B : constant uint8 := 16#16#; -- Pull-up resistor configuration
INTF : constant uint8 := 16#0E#; -- Interrupt flag
INTF_A : constant uint8 := 16#07#; -- Interrupt flag
INTF_B : constant uint8 := 16#17#; -- Interrupt flag
INTCAP : constant uint8 := 16#10#; -- Interrupt capture
INTCAP_A : constant uint8 := 16#08#; -- Interrupt capture
INTCAP_B : constant uint8 := 16#18#; -- Interrupt capture
GPIO : constant uint8 := 16#12#; -- Port register
GPIO_A : constant uint8 := 16#09#; -- Port register
GPIO_B : constant uint8 := 16#19#; -- Port register
OLAT : constant uint8 := 16#14#; -- Output latch
OLAT_A : constant uint8 := 16#0A#; -- Output latch
OLAT_B : constant uint8 := 16#1A#; -- Output latch
--
all_write : constant uint8 := 16#00#;
all_read : constant uint8 := 16#FF#;
--
Expand All @@ -89,6 +47,11 @@ package BBS.embed.i2c.MCP23017 is
procedure configure(self : in out MCP23017_record; port : i2c_interface;
addr : addr7; error : out err_code);
--
-- Check to see if the configured device is present.
--
function present(port : i2c_interface;
addr : addr7) return boolean;
--
-- Set the direction (read(0)/write(1)) for each of the output bits. The
-- direction bits are packed into a uint16.
--
Expand Down Expand Up @@ -128,5 +91,47 @@ private
record
null;
end record;
--
-- Device registers. The MCP23017 can be configured to work as two separate
-- 8 bit ports or one 16 bit port. Depending on the setting of the BANK bit
-- in the IOCON register, the _A and _B registers are used or the register
-- with no suffix is used.
--
-- This application will configure the device to work as a single 16 bit I/O
-- port.
--
IODIR : constant uint8 := 16#00#; -- I/O Direction
IODIR_A : constant uint8 := 16#00#; -- I/O Direction
IODIR_B : constant uint8 := 16#10#; -- I/O Direction
IPOL : constant uint8 := 16#02#; -- Input polarity
IPOL_A : constant uint8 := 16#01#; -- Input polarity
IPOL_B : constant uint8 := 16#11#; -- Input polarity
GPINTEN : constant uint8 := 16#04#; -- Interrupt-on-change control
GPINTEN_A : constant uint8 := 16#02#; -- Interrupt-on-change control
GPINTEN_B : constant uint8 := 16#12#; -- Interrupt-on-change control
DEFVAL : constant uint8 := 16#06#; --Default compare
DEFVAL_A : constant uint8 := 16#03#; --Default compare
DEFVAL_B : constant uint8 := 16#13#; --Default compare
INTCON : constant uint8 := 16#08#; -- Interrupt control
INTCON_A : constant uint8 := 16#04#; -- Interrupt control
INTCON_B : constant uint8 := 16#14#; -- Interrupt control
IOCON : constant uint8 := 16#0A#; -- Control register
IOCON_A : constant uint8 := 16#05#; -- Control register
IOCON_B : constant uint8 := 16#15#; -- Control register
GPPU : constant uint8 := 16#0C#; -- Pull-up resistor configuration
GPPU_A : constant uint8 := 16#06#; -- Pull-up resistor configuration
GPPU_B : constant uint8 := 16#16#; -- Pull-up resistor configuration
INTF : constant uint8 := 16#0E#; -- Interrupt flag
INTF_A : constant uint8 := 16#07#; -- Interrupt flag
INTF_B : constant uint8 := 16#17#; -- Interrupt flag
INTCAP : constant uint8 := 16#10#; -- Interrupt capture
INTCAP_A : constant uint8 := 16#08#; -- Interrupt capture
INTCAP_B : constant uint8 := 16#18#; -- Interrupt capture
GPIO : constant uint8 := 16#12#; -- Port register
GPIO_A : constant uint8 := 16#09#; -- Port register
GPIO_B : constant uint8 := 16#19#; -- Port register
OLAT : constant uint8 := 16#14#; -- Output latch
OLAT_A : constant uint8 := 16#0A#; -- Output latch
OLAT_B : constant uint8 := 16#1A#; -- Output latch

end;

0 comments on commit 4e72cc5

Please sign in to comment.