Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
mcp23x08.hpp
1 /*
2  Title --- driver/mcp23x08.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_DRIVER_MCP23X08_HPP_
23 #define _RPI_HW_DRIVER_MCP23X08_HPP_
24 
25 #include <rpi-hw/rpi.hpp>
26 
27 #include <rpi-hw/consts.hpp>
28 #include <rpi-hw/types.hpp>
29 #include <rpi-hw/exception.hpp>
30 #include <rpi-hw/utils.hpp>
31 
32 #include <rpi-hw/driver/io-expander.hpp>
33 
34 namespace rpihw { // Begin main namespace
35 
36 namespace driver { // Begin drivers namespace
37 
38 /*!
39  @class mcp23x08
40  @brief 8-bit I/O Expander with SPI/I2C (skeleton).
41 */
42 class mcp23x08 : public io_expander {
43 
44 public:
45 
46  //! The controller command set.
47  enum Commands {
48 
49  WRITE = 0x40, //!< Writes on the register.
50  READ = 0x41 //!< Reads from the register.
51  };
52 
53  //! Device registers.
54  enum Registers {
55 
56  IODIR = 0x00, //!< Controls the direction of the data I/O.
57  IPOL = 0x02, //!< Configures the polarity on the corresponding GPIO port bits.
58  GPINTEN = 0x04, //!< Controls the interrupt-on-change feature for each pin
59  DEFVAL = 0x06, //!< Sets the default comparison value.
60  INTCON = 0x08, //!< Controls how the associated pin value is compared for the interrupt-on-change feature.
61  IOCON = 0x0a, //!< Configures the device.
62  GPPU = 0x0c, //!< Controls the pull-up resistors for the port pins.
63  INTF = 0x0e, //!< Reflects the interrupt condition on the port pins of any pin that is enabled for interrupts.
64  INTCAP = 0x10, //!< Captures the GPIO port value at the time the interrupt occurred.
65  GPIO = 0x12, //!< Reflects the value on the port.
66  OLAT = 0x14, //!< Provides access to the output latches.
67  };
68 
69  //! The controller configuration set (bit flags).
70  enum Settings {
71 
72  INTPOL = 0x02, //!< Sets the polarity of the INT output pin
73  ODR = 0x04, //!< Configures the INT pin as an open-drain output.
74  HAEN = 0x08, //!< Hardware address enable.
75  DISSLW = 0x10, //!< Slew Rate control bit for SDA output.
76  SEQOP = 0x20, //!< Sequential operation mode.
77  MIRROR = 0x40, //!< Pins mirror.
78  BANK = 0x80 //!< Controls how the registers are addressed.
79  };
80 
81  /*!
82  @brief Constructor method.
83  @param[in] dev_path The device path.
84  */
85  mcp23x08( const std::string &dev_path );
86 
87  //! Destructor method.
88  virtual ~mcp23x08();
89 
90  //! Initialize the expander.
91  void init();
92 
93  /*!
94  @brief Sets the mode of a GPIO pin.
95  @param[in] pin The GPIO pin.
96  @param[in] mode The GPIO mode.
97  @param[in] pull_mode The pull resistor mode.
98  */
99  virtual void setup( uint8_t pin, uint8_t mode, uint8_t pull_mode = PULL_OFF );
100 
101  /*!
102  @brief Sets the value of a output pin.
103  @param[in] pin The output pin.
104  @param[in] value The value of output pin.
105  */
106  virtual void write( uint8_t pin, bool value );
107 
108  /*!
109  @brief Returns the value of a input pin.
110  @param[in] pin The input pin.
111  @return The value of input pin.
112  */
113  virtual bool read( uint8_t pin );
114 
115  /*!
116  @brief Enables/disables the pull-up control on a GPIO pin.
117  @param[in] pin The GPIO pin.
118  @param[in] mode The pull resistor mode.
119  */
120  virtual void setPullUpDown( uint8_t pin, uint8_t mode );
121 
122 protected:
123 
124  //! Local registers.
125  uint8_t m_states[4];
126 
127  //! Device path.
128  std::string m_dev_path;
129 
130  //! Sends data to the device.
131  virtual void send( uint8_t reg, uint8_t data ) = 0;
132 
133  //! Receives data from the device.
134  virtual uint8_t receive( uint8_t reg ) = 0;
135 };
136 
137 } // End of drivers namespace
138 
139 } // End of main namespace
140 
141 
142 // Include inline methods
143 #include <rpi-hw/driver/mcp23x08-inl.hpp>
144 
145 #endif /* _RPI_HW_DRIVER_MCP23X08_HPP_ */
Reads from the register.
Definition: mcp23x08.hpp:50
8-bit I/O Expander with SPI/I2C (skeleton).
Definition: mcp23x08.hpp:42
Controls the interrupt-on-change feature for each pin.
Definition: mcp23x08.hpp:58
Controls how the registers are addressed.
Definition: mcp23x08.hpp:78
Captures the GPIO port value at the time the interrupt occurred.
Definition: mcp23x08.hpp:64
Writes on the register.
Definition: mcp23x08.hpp:49
Sequential operation mode.
Definition: mcp23x08.hpp:76
std::string m_dev_path
Device path.
Definition: mcp23x08.hpp:128
void init()
Initialize the expander.
Controls the direction of the data I/O.
Definition: mcp23x08.hpp:56
virtual ~mcp23x08()
Destructor method.
Hardware address enable.
Definition: mcp23x08.hpp:74
Controls the pull-up resistors for the port pins.
Definition: mcp23x08.hpp:62
Commands
The controller command set.
Definition: mcp23x08.hpp:47
Configures the INT pin as an open-drain output.
Definition: mcp23x08.hpp:73
Provides access to the output latches.
Definition: mcp23x08.hpp:66
virtual uint8_t receive(uint8_t reg)=0
Receives data from the device.
Controls how the associated pin value is compared for the interrupt-on-change feature.
Definition: mcp23x08.hpp:60
Reflects the value on the port.
Definition: mcp23x08.hpp:65
Configures the device.
Definition: mcp23x08.hpp:61
Reflects the interrupt condition on the port pins of any pin that is enabled for interrupts.
Definition: mcp23x08.hpp:63
mcp23x08(const std::string &dev_path)
Constructor method.
Sets the default comparison value.
Definition: mcp23x08.hpp:59
uint8_t m_states[4]
Local registers.
Definition: mcp23x08.hpp:125
Slew Rate control bit for SDA output.
Definition: mcp23x08.hpp:75
virtual void send(uint8_t reg, uint8_t data)=0
Sends data to the device.
virtual void write(uint8_t pin, bool value)
Sets the value of a output pin.
Registers
Device registers.
Definition: mcp23x08.hpp:54
Settings
The controller configuration set (bit flags).
Definition: mcp23x08.hpp:70
Sets the polarity of the INT output pin.
Definition: mcp23x08.hpp:72
virtual void setup(uint8_t pin, uint8_t mode, uint8_t pull_mode=PULL_OFF)
Sets the mode of a GPIO pin.
virtual void setPullUpDown(uint8_t pin, uint8_t mode)
Enables/disables the pull-up control on a GPIO pin.
virtual bool read(uint8_t pin)
Returns the value of a input pin.
Configures the polarity on the corresponding GPIO port bits.
Definition: mcp23x08.hpp:57