Device driver for TCA9554A, which is I2C GPIO expander IC.
tca9554a.h@0:402147fa55f6, 2016-05-04 (annotated)
- Committer:
- coisme
- Date:
- Wed May 04 00:40:25 2016 +0000
- Revision:
- 0:402147fa55f6
- Child:
- 1:e02d9e33b9f3
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
coisme | 0:402147fa55f6 | 1 | #ifndef __TCA9554A_H__ |
coisme | 0:402147fa55f6 | 2 | #define __TCA9554A_H__ |
coisme | 0:402147fa55f6 | 3 | |
coisme | 0:402147fa55f6 | 4 | #include "mbed.h" |
coisme | 0:402147fa55f6 | 5 | |
coisme | 0:402147fa55f6 | 6 | class TCA9554A |
coisme | 0:402147fa55f6 | 7 | { |
coisme | 0:402147fa55f6 | 8 | public: |
coisme | 0:402147fa55f6 | 9 | /** |
coisme | 0:402147fa55f6 | 10 | * Device's slave address. |
coisme | 0:402147fa55f6 | 11 | */ |
coisme | 0:402147fa55f6 | 12 | typedef enum { |
coisme | 0:402147fa55f6 | 13 | SLAVE_ADDRESS_38H = 0x38, /**< Slave address 0x38, when (A2)(A1)(A0) = 000. */ |
coisme | 0:402147fa55f6 | 14 | SLAVE_ADDRESS_39H = 0x39, /**< Slave address 0x39, when (A2)(A1)(A0) = 001. */ |
coisme | 0:402147fa55f6 | 15 | SLAVE_ADDRESS_3AH = 0x3A, /**< Slave address 0x3A, when (A2)(A1)(A0) = 010. */ |
coisme | 0:402147fa55f6 | 16 | SLAVE_ADDRESS_3BH = 0x3B, /**< Slave address 0x3B, when (A2)(A1)(A0) = 011. */ |
coisme | 0:402147fa55f6 | 17 | SLAVE_ADDRESS_3CH = 0x3C, /**< Slave address 0x3C, when (A2)(A1)(A0) = 100. */ |
coisme | 0:402147fa55f6 | 18 | SLAVE_ADDRESS_3DH = 0x3D, /**< Slave address 0x3D, when (A2)(A1)(A0) = 101. */ |
coisme | 0:402147fa55f6 | 19 | SLAVE_ADDRESS_3EH = 0x3E, /**< Slave address 0x3E, when (A2)(A1)(A0) = 110. */ |
coisme | 0:402147fa55f6 | 20 | SLAVE_ADDRESS_3FH = 0x3F, /**< Slave address 0x3F, when (A2)(A1)(A0) = 111. */ |
coisme | 0:402147fa55f6 | 21 | } SlaveAddress; |
coisme | 0:402147fa55f6 | 22 | |
coisme | 0:402147fa55f6 | 23 | /** |
coisme | 0:402147fa55f6 | 24 | * Status of function. |
coisme | 0:402147fa55f6 | 25 | */ |
coisme | 0:402147fa55f6 | 26 | typedef enum { |
coisme | 0:402147fa55f6 | 27 | SUCCESS, /**< The function processed successfully. */ |
coisme | 0:402147fa55f6 | 28 | ERROR_I2C_READ, /**< Error related to I2C read. */ |
coisme | 0:402147fa55f6 | 29 | ERROR_I2C_WRITE, /**< Error related to I2C write. */ |
coisme | 0:402147fa55f6 | 30 | ERROR, /**< General Error */ |
coisme | 0:402147fa55f6 | 31 | } Status; |
coisme | 0:402147fa55f6 | 32 | |
coisme | 0:402147fa55f6 | 33 | /** |
coisme | 0:402147fa55f6 | 34 | * Port number. |
coisme | 0:402147fa55f6 | 35 | */ |
coisme | 0:402147fa55f6 | 36 | typedef enum { |
coisme | 0:402147fa55f6 | 37 | PORT_0 = 0x01, /**< Port 0. */ |
coisme | 0:402147fa55f6 | 38 | PORT_1 = 0x02, /**< Port 1. */ |
coisme | 0:402147fa55f6 | 39 | PORT_2 = 0x04, /**< Port 2. */ |
coisme | 0:402147fa55f6 | 40 | PORT_3 = 0x08, /**< Port 3. */ |
coisme | 0:402147fa55f6 | 41 | PORT_4 = 0x10, /**< Port 4. */ |
coisme | 0:402147fa55f6 | 42 | PORT_5 = 0x20, /**< Port 5. */ |
coisme | 0:402147fa55f6 | 43 | PORT_6 = 0x40, /**< Port 6. */ |
coisme | 0:402147fa55f6 | 44 | PORT_7 = 0x80, /**< Port 7. */ |
coisme | 0:402147fa55f6 | 45 | } Port; |
coisme | 0:402147fa55f6 | 46 | |
coisme | 0:402147fa55f6 | 47 | /** |
coisme | 0:402147fa55f6 | 48 | * Port value. |
coisme | 0:402147fa55f6 | 49 | */ |
coisme | 0:402147fa55f6 | 50 | typedef enum { |
coisme | 0:402147fa55f6 | 51 | LOW = 0, |
coisme | 0:402147fa55f6 | 52 | HIGH = 1, |
coisme | 0:402147fa55f6 | 53 | } LogicLevel; |
coisme | 0:402147fa55f6 | 54 | |
coisme | 0:402147fa55f6 | 55 | /** |
coisme | 0:402147fa55f6 | 56 | * Direction of a port. |
coisme | 0:402147fa55f6 | 57 | */ |
coisme | 0:402147fa55f6 | 58 | typedef enum { |
coisme | 0:402147fa55f6 | 59 | DIR_OUTPUT = 0x00, |
coisme | 0:402147fa55f6 | 60 | DIR_INPUT = 0x01, |
coisme | 0:402147fa55f6 | 61 | } Direction; |
coisme | 0:402147fa55f6 | 62 | |
coisme | 0:402147fa55f6 | 63 | /** |
coisme | 0:402147fa55f6 | 64 | * Flag for enabling input polarity inversion. |
coisme | 0:402147fa55f6 | 65 | */ |
coisme | 0:402147fa55f6 | 66 | typedef enum { |
coisme | 0:402147fa55f6 | 67 | NON_INVERTING = 0x00, |
coisme | 0:402147fa55f6 | 68 | INVERTING = 0x01, |
coisme | 0:402147fa55f6 | 69 | } PolarityInversion; |
coisme | 0:402147fa55f6 | 70 | |
coisme | 0:402147fa55f6 | 71 | /** |
coisme | 0:402147fa55f6 | 72 | * Constructor. In default, all the ports are set as input. |
coisme | 0:402147fa55f6 | 73 | */ |
coisme | 0:402147fa55f6 | 74 | TCA9554A(I2C *conn, SlaveAddress addr); |
coisme | 0:402147fa55f6 | 75 | |
coisme | 0:402147fa55f6 | 76 | |
coisme | 0:402147fa55f6 | 77 | /** |
coisme | 0:402147fa55f6 | 78 | * Sets port properties. |
coisme | 0:402147fa55f6 | 79 | * @param port Port number to be configured. |
coisme | 0:402147fa55f6 | 80 | * @param dir Direction to be set for the specified port. |
coisme | 0:402147fa55f6 | 81 | * @param pol Polarity inversion to be set for the specified port. |
coisme | 0:402147fa55f6 | 82 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:402147fa55f6 | 83 | */ |
coisme | 0:402147fa55f6 | 84 | Status configurePort(Port port, Direction dir, PolarityInversion pol = NON_INVERTING); |
coisme | 0:402147fa55f6 | 85 | |
coisme | 0:402147fa55f6 | 86 | /** |
coisme | 0:402147fa55f6 | 87 | * Gets the logic level from the specified port. If the polarity inversion is enabled at the port, |
coisme | 0:402147fa55f6 | 88 | * this function returns the inverted value. For example, the input voltage of a port is high level |
coisme | 0:402147fa55f6 | 89 | * and polarity inversion setting for the port is enabled, then LOW will be obtained from this function. |
coisme | 0:402147fa55f6 | 90 | * @param val Logic level of the specified port. |
coisme | 0:402147fa55f6 | 91 | * @param port Port to be read. |
coisme | 0:402147fa55f6 | 92 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:402147fa55f6 | 93 | */ |
coisme | 0:402147fa55f6 | 94 | Status getPortLevel(LogicLevel *val, Port port); |
coisme | 0:402147fa55f6 | 95 | |
coisme | 0:402147fa55f6 | 96 | /** |
coisme | 0:402147fa55f6 | 97 | * Sets output level of the specified port. If the direction of the specified port is input, |
coisme | 0:402147fa55f6 | 98 | * this operation has no effect. |
coisme | 0:402147fa55f6 | 99 | * @param port Port to be set. |
coisme | 0:402147fa55f6 | 100 | * @param val Logic level to be set. |
coisme | 0:402147fa55f6 | 101 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:402147fa55f6 | 102 | */ |
coisme | 0:402147fa55f6 | 103 | Status setPortLevel(Port port, LogicLevel val); |
coisme | 0:402147fa55f6 | 104 | |
coisme | 0:402147fa55f6 | 105 | /** |
coisme | 0:402147fa55f6 | 106 | * Gets port direction of the specified port. |
coisme | 0:402147fa55f6 | 107 | * @param dir Pointer to the buffer direction to be stored. |
coisme | 0:402147fa55f6 | 108 | * @param port Port to be checked direction. |
coisme | 0:402147fa55f6 | 109 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:402147fa55f6 | 110 | */ |
coisme | 0:402147fa55f6 | 111 | Status getPortDirection(Direction *dir, Port port); |
coisme | 0:402147fa55f6 | 112 | |
coisme | 0:402147fa55f6 | 113 | private: |
coisme | 0:402147fa55f6 | 114 | I2C *connection; /**< Pointer to an I2C object. */ |
coisme | 0:402147fa55f6 | 115 | uint8_t slaveAddress; /**< Holds the device's slave address. */ |
coisme | 0:402147fa55f6 | 116 | |
coisme | 0:402147fa55f6 | 117 | typedef enum { |
coisme | 0:402147fa55f6 | 118 | REG_ADDR_INPUT = 0x00, |
coisme | 0:402147fa55f6 | 119 | REG_ADDR_OUTPUT = 0x01, |
coisme | 0:402147fa55f6 | 120 | REG_ADDR_POLARITY = 0x02, |
coisme | 0:402147fa55f6 | 121 | REG_ADDR_CONFIG = 0x03, |
coisme | 0:402147fa55f6 | 122 | } RegisterAddress; |
coisme | 0:402147fa55f6 | 123 | |
coisme | 0:402147fa55f6 | 124 | /** |
coisme | 0:402147fa55f6 | 125 | * Reads one byte from the specified register address. |
coisme | 0:402147fa55f6 | 126 | * @param addr Register address to be read. |
coisme | 0:402147fa55f6 | 127 | * @param buf Buffer to store the read value. |
coisme | 0:402147fa55f6 | 128 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:402147fa55f6 | 129 | */ |
coisme | 0:402147fa55f6 | 130 | Status read(RegisterAddress addr, uint8_t *buf); |
coisme | 0:402147fa55f6 | 131 | |
coisme | 0:402147fa55f6 | 132 | /** |
coisme | 0:402147fa55f6 | 133 | * Write one byte to the specified register address. |
coisme | 0:402147fa55f6 | 134 | * @param addr Register address to be read. |
coisme | 0:402147fa55f6 | 135 | * @param buf Value to be written into the specified register. |
coisme | 0:402147fa55f6 | 136 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:402147fa55f6 | 137 | */ |
coisme | 0:402147fa55f6 | 138 | Status write(RegisterAddress addr, uint8_t val); |
coisme | 0:402147fa55f6 | 139 | }; |
coisme | 0:402147fa55f6 | 140 | |
coisme | 0:402147fa55f6 | 141 | #endif |