ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mcp23017.h Source File

mcp23017.h

00001 
00002 /**
00003  * MCP23017 のアドレスモードについて:
00004  *
00005  * Byte Mode: アドレスポインタの自動インクリメントがオフ(デフォルト)
00006  *     特定のGPIOを連続して監視するときに便利
00007  *
00008  *     IOCON.BANK=0: GPIOA と GPIOB が読み出すたびにトグルする (デフォルト)
00009  *       読み出すと1つアドレスをすすめる(またはアドレスを1つ戻す)
00010  *       常に 16bit 分の GPIO を読み書きするとき便利
00011  *
00012  *     IOCON.BANK=1: GPIOA と GPIOB が完全に別れたアドレスになる
00013  *
00014  * Sequential mode: アドレスポインタの自動インクリメントがオン
00015  *     I2C eeprom みたいな挙動になる
00016  *
00017  */
00018 class MCP23017 {
00019     I2C& i2c;
00020     uint8_t address;
00021 
00022 public:
00023     // BANK=1
00024     enum RegisterAddress {
00025         IODIRA_BANK = 0x00,
00026         IPOLA_BANK = 0x01,
00027         GPINTENA_BANK = 0x02,
00028         DEFVALA_BANK = 0x03,
00029         INTCONA_BANK = 0x04,
00030         IOCON_BANK = 0x05,
00031         GPPUA_BANK = 0x06,
00032         INTFA_BANK = 0x07,
00033         INTCAPA_BANK = 0x08,
00034         GPIOA_BANK = 0x09,
00035         OLATA_BANK = 0x0a,
00036         IODIRB_BANK = 0x10,
00037         IPOLB_BANK = 0x11,
00038         GPINTENB_BANK = 0x12,
00039         DEFVALB_BANK = 0x13,
00040         INTCONB_BANK = 0x14,
00041         GPPUB_BANK = 0x16,
00042         INTFB_BANK = 0x17,
00043         INTCAPB_BANK = 0x18,
00044         GPIOB_BANK = 0x19,
00045         OLATB_BANK = 0x1a,
00046 
00047         // BANK=0
00048         IODIRA = 0x00,
00049         IODIRB = 0x01,
00050         IPOLA = 0x02,
00051         IPOLB = 0x03,
00052         GPINTENA = 0x04,
00053         GPINTENB = 0x05,
00054         DEFVALA = 0x06,
00055         DEFVALB = 0x07,
00056         INTCONA = 0x08,
00057         INTCONB = 0x09,
00058         IOCON = 0x0a,
00059         GPPUA = 0x0c,
00060         GPPUB = 0x0d,
00061         INTFA = 0x0e,
00062         INTFB = 0x0f,
00063         INTCAPA = 0x10,
00064         INTCAPB = 0x11,
00065         GPIOA = 0x12,
00066         GPIOB = 0x13,
00067         OLATA = 0x14,
00068         OLATB = 0x15,
00069     };
00070 
00071     static const uint8_t BANK = 7;
00072     static const uint8_t MIRROR = 6;
00073     static const uint8_t SEQOP = 5;
00074     static const uint8_t DISSLW = 4;
00075     static const uint8_t HAEN = 3;
00076     static const uint8_t ODR = 2;
00077     static const uint8_t INTPOL = 1;
00078     
00079     // i2c.write(byte) returns 1 with success
00080     // i2c.write(int, const char*, int, bool) returns 0 with success.
00081     static const int I2C_WRITE_MULTIBYTES_SUCCESS = 0;
00082 
00083     /**
00084      * _i2c is instance of I2C
00085      * _address is 7-bit slave address of MCP23017
00086      */
00087 
00088     MCP23017(
00089         I2C& _i2c,
00090         uint8_t _address
00091     ) :
00092         i2c(_i2c),
00093         address(_address<<1)
00094     {
00095     }
00096 
00097     uint8_t read8(const RegisterAddress reg, int& error) const {
00098         char data[1];
00099         data[0] = reg;
00100         i2c.write(address, data, 1, true);
00101         error = i2c.read(address, data, 1, false);
00102         return data[0];
00103     }
00104 
00105     uint16_t read16(const RegisterAddress reg, int& error) const {
00106         char data[2];
00107         data[0] = reg;
00108         i2c.write(address, data, 1, true);
00109         error = i2c.read(address, data, 2, false);
00110         return (static_cast<uint16_t>(data[0]) << 8) | static_cast<uint16_t>(data[1]);
00111     }
00112 
00113     int write8(const RegisterAddress reg, uint8_t data) const {
00114         char d[2];
00115         d[0] = reg;
00116         d[1] = data;
00117         return i2c.write(address, d, 2, false) == I2C_WRITE_MULTIBYTES_SUCCESS;
00118     }
00119 
00120     int write16(const RegisterAddress reg, uint16_t data) const {
00121         char d[3];
00122         d[0] = reg;
00123         d[1] = data >> 8;
00124         d[2] = data & 0xff;
00125         return i2c.write(address, d, 3, false) == I2C_WRITE_MULTIBYTES_SUCCESS;
00126     }
00127 };
00128