Device driver for TCA9554A, which is I2C GPIO expander IC.
Diff: tca9554a.h
- Revision:
- 0:402147fa55f6
- Child:
- 1:e02d9e33b9f3
diff -r 000000000000 -r 402147fa55f6 tca9554a.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tca9554a.h Wed May 04 00:40:25 2016 +0000 @@ -0,0 +1,141 @@ +#ifndef __TCA9554A_H__ +#define __TCA9554A_H__ + +#include "mbed.h" + +class TCA9554A +{ +public: + /** + * Device's slave address. + */ + typedef enum { + SLAVE_ADDRESS_38H = 0x38, /**< Slave address 0x38, when (A2)(A1)(A0) = 000. */ + SLAVE_ADDRESS_39H = 0x39, /**< Slave address 0x39, when (A2)(A1)(A0) = 001. */ + SLAVE_ADDRESS_3AH = 0x3A, /**< Slave address 0x3A, when (A2)(A1)(A0) = 010. */ + SLAVE_ADDRESS_3BH = 0x3B, /**< Slave address 0x3B, when (A2)(A1)(A0) = 011. */ + SLAVE_ADDRESS_3CH = 0x3C, /**< Slave address 0x3C, when (A2)(A1)(A0) = 100. */ + SLAVE_ADDRESS_3DH = 0x3D, /**< Slave address 0x3D, when (A2)(A1)(A0) = 101. */ + SLAVE_ADDRESS_3EH = 0x3E, /**< Slave address 0x3E, when (A2)(A1)(A0) = 110. */ + SLAVE_ADDRESS_3FH = 0x3F, /**< Slave address 0x3F, when (A2)(A1)(A0) = 111. */ + } SlaveAddress; + + /** + * Status of function. + */ + typedef enum { + SUCCESS, /**< The function processed successfully. */ + ERROR_I2C_READ, /**< Error related to I2C read. */ + ERROR_I2C_WRITE, /**< Error related to I2C write. */ + ERROR, /**< General Error */ + } Status; + + /** + * Port number. + */ + typedef enum { + PORT_0 = 0x01, /**< Port 0. */ + PORT_1 = 0x02, /**< Port 1. */ + PORT_2 = 0x04, /**< Port 2. */ + PORT_3 = 0x08, /**< Port 3. */ + PORT_4 = 0x10, /**< Port 4. */ + PORT_5 = 0x20, /**< Port 5. */ + PORT_6 = 0x40, /**< Port 6. */ + PORT_7 = 0x80, /**< Port 7. */ + } Port; + + /** + * Port value. + */ + typedef enum { + LOW = 0, + HIGH = 1, + } LogicLevel; + + /** + * Direction of a port. + */ + typedef enum { + DIR_OUTPUT = 0x00, + DIR_INPUT = 0x01, + } Direction; + + /** + * Flag for enabling input polarity inversion. + */ + typedef enum { + NON_INVERTING = 0x00, + INVERTING = 0x01, + } PolarityInversion; + + /** + * Constructor. In default, all the ports are set as input. + */ + TCA9554A(I2C *conn, SlaveAddress addr); + + + /** + * Sets port properties. + * @param port Port number to be configured. + * @param dir Direction to be set for the specified port. + * @param pol Polarity inversion to be set for the specified port. + * @return SUCCESS when succeeded. Other value will be returned when error. + */ + Status configurePort(Port port, Direction dir, PolarityInversion pol = NON_INVERTING); + + /** + * Gets the logic level from the specified port. If the polarity inversion is enabled at the port, + * this function returns the inverted value. For example, the input voltage of a port is high level + * and polarity inversion setting for the port is enabled, then LOW will be obtained from this function. + * @param val Logic level of the specified port. + * @param port Port to be read. + * @return SUCCESS when succeeded. Other value will be returned when error. + */ + Status getPortLevel(LogicLevel *val, Port port); + + /** + * Sets output level of the specified port. If the direction of the specified port is input, + * this operation has no effect. + * @param port Port to be set. + * @param val Logic level to be set. + * @return SUCCESS when succeeded. Other value will be returned when error. + */ + Status setPortLevel(Port port, LogicLevel val); + + /** + * Gets port direction of the specified port. + * @param dir Pointer to the buffer direction to be stored. + * @param port Port to be checked direction. + * @return SUCCESS when succeeded. Other value will be returned when error. + */ + Status getPortDirection(Direction *dir, Port port); + +private: + I2C *connection; /**< Pointer to an I2C object. */ + uint8_t slaveAddress; /**< Holds the device's slave address. */ + + typedef enum { + REG_ADDR_INPUT = 0x00, + REG_ADDR_OUTPUT = 0x01, + REG_ADDR_POLARITY = 0x02, + REG_ADDR_CONFIG = 0x03, + } RegisterAddress; + + /** + * Reads one byte from the specified register address. + * @param addr Register address to be read. + * @param buf Buffer to store the read value. + * @return SUCCESS when succeeded. Other value will be returned when error. + */ + Status read(RegisterAddress addr, uint8_t *buf); + + /** + * Write one byte to the specified register address. + * @param addr Register address to be read. + * @param buf Value to be written into the specified register. + * @return SUCCESS when succeeded. Other value will be returned when error. + */ + Status write(RegisterAddress addr, uint8_t val); +}; + +#endif \ No newline at end of file