Device driver for TCA9554A, which is I2C GPIO expander IC.
Diff: tca9554a.h
- Revision:
- 1:e02d9e33b9f3
- Parent:
- 0:402147fa55f6
diff -r 402147fa55f6 -r e02d9e33b9f3 tca9554a.h --- a/tca9554a.h Wed May 04 00:40:25 2016 +0000 +++ b/tca9554a.h Thu May 05 17:55:38 2016 +0000 @@ -3,6 +3,51 @@ #include "mbed.h" +/** + * Device driver for TCA9554A. + * @note TCA9554A is a GPIO expander with I2C interface. + * Example: + * @code + * #include "mbed.h" + * #include "tca9554a.h" + * + * #define I2C_SPEED_100KHZ 100000 + * #define I2C_SPEED_400KHZ 400000 + * + * int main(void) { + * // Instanciate I2C + * I2C i2c(I2C_SDA0, I2C_SCL0); + * i2c.frequency(I2C_SPEED_400KHZ); + * + * // Instanciate TCA9554A + * // Suppose that the slave address of TCA9554A on your board is 38H. + * TCA9554A tca9554a(&i2c, TCA9554A::SLAVE_ADDRESS_38H); + * + * // Configure the GPIO ports. + * // Default direction of the ports are input and polarity inversion is disabled. + * // Here, sets port 3 to output and port 5 to input with polarity inversion. + * if (tca9554a.configurePort(TCA9554A::PORT_3, TCA9554A::DIR_OUTPUT)!= TCA9554A::SUCCESS) { + * // @TODO: error handling + * } + * if (tca9554a.configurePort(TCA9554A::PORT_5, TCA9554A::DIR_INPUT, TCA9554A::INVERTING) != TCA9554A::SUCCESS) { + * // @TODO: error handling + * } + * + * while(true) { + * LogicLevel val = TCA9554A::LOW; + * // Reads the value from port 5 and sets the read value to port 3. + * // Note that the read value is inverted from the actual input level, + * // because the port 5 is configured as INVERTING at the initialization process above. + * if (tca9554a.getPortLevel(&val, TCA9554A::PORT_5) != SUCCESS) { + * // @TODO: error handling + * } + * if (tca9554a.setPortLevel(TCA9554A::PORT_3, val) != SUCCESS) { + * // @TODO: error handling + * } + * } + * } + * @endcode + */ class TCA9554A { public: @@ -21,7 +66,7 @@ } SlaveAddress; /** - * Status of function. + * Result status of function execution. */ typedef enum { SUCCESS, /**< The function processed successfully. */ @@ -31,7 +76,7 @@ } Status; /** - * Port number. + * GPIO port number. */ typedef enum { PORT_0 = 0x01, /**< Port 0. */ @@ -48,28 +93,30 @@ * Port value. */ typedef enum { - LOW = 0, - HIGH = 1, + LOW = 0, /**< Indicates low level. */ + HIGH = 1, /**< Indicates high level. */ } LogicLevel; /** * Direction of a port. */ typedef enum { - DIR_OUTPUT = 0x00, - DIR_INPUT = 0x01, + DIR_OUTPUT = 0x00, /**< Output. */ + DIR_INPUT = 0x01, /**< Input. */ } Direction; /** * Flag for enabling input polarity inversion. */ typedef enum { - NON_INVERTING = 0x00, - INVERTING = 0x01, + NON_INVERTING = 0x00, /**< Input polarity is not inverted. */ + INVERTING = 0x01, /**< Input polarity is inverted. */ } PolarityInversion; /** * Constructor. In default, all the ports are set as input. + * @param conn Pointer to an instance of I2C. + * @param addr Slave address of this device. */ TCA9554A(I2C *conn, SlaveAddress addr);