Basic library for MCP23017 16-bit I/O expander using an I2C interface used in aconno ACD52832 dev board.

Dependents:   tof100 gaasgjdvas LED_GAME11 tof100

Fork of MCP23017 by jim herd

Committer:
Anunnaki
Date:
Mon Sep 26 10:08:33 2016 +0000
Revision:
14:1834cdf54c8f
Parent:
12:6d9d2b277f26
Added interrupt flags

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jimherd 0:a6a5d942b58f 1 /* MCP23017 library for Arduino
jimherd 0:a6a5d942b58f 2 Copyright (C) 2009 David Pye <davidmpye@gmail.com
jimherd 12:6d9d2b277f26 3 Modified for use on the MBED ARM platform
jimherd 0:a6a5d942b58f 4
jimherd 0:a6a5d942b58f 5 This program is free software: you can redistribute it and/or modify
jimherd 0:a6a5d942b58f 6 it under the terms of the GNU General Public License as published by
jimherd 0:a6a5d942b58f 7 the Free Software Foundation, either version 3 of the License, or
jimherd 0:a6a5d942b58f 8 (at your option) any later version.
jimherd 0:a6a5d942b58f 9
jimherd 0:a6a5d942b58f 10 This program is distributed in the hope that it will be useful,
jimherd 0:a6a5d942b58f 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
jimherd 0:a6a5d942b58f 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
jimherd 0:a6a5d942b58f 13 GNU General Public License for more details.
jimherd 0:a6a5d942b58f 14
jimherd 0:a6a5d942b58f 15 You should have received a copy of the GNU General Public License
jimherd 0:a6a5d942b58f 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
jimherd 0:a6a5d942b58f 17 */
jimherd 4:32fd6ee97dff 18
jimherd 4:32fd6ee97dff 19 #include "MCP23017.h"
jimherd 2:2a17f52c550a 20 #include "mbed.h"
jimherd 0:a6a5d942b58f 21
Anunnaki 14:1834cdf54c8f 22 bool interrupt = 0;
Anunnaki 14:1834cdf54c8f 23
Anunnaki 14:1834cdf54c8f 24 void setInt(){
Anunnaki 14:1834cdf54c8f 25 interrupt = 1;
Anunnaki 14:1834cdf54c8f 26 }
Anunnaki 14:1834cdf54c8f 27 void clearInt(){
Anunnaki 14:1834cdf54c8f 28 interrupt = 0;
Anunnaki 14:1834cdf54c8f 29 }
Anunnaki 14:1834cdf54c8f 30 bool getInt(){
Anunnaki 14:1834cdf54c8f 31 return interrupt;
Anunnaki 14:1834cdf54c8f 32 }
jimherd 0:a6a5d942b58f 33 union {
jimherd 0:a6a5d942b58f 34 uint8_t value8[2];
jimherd 0:a6a5d942b58f 35 uint16_t value16;
jimherd 0:a6a5d942b58f 36 } tmp_data;
jimherd 0:a6a5d942b58f 37
jimherd 12:6d9d2b277f26 38 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 39 *
jimherd 0:a6a5d942b58f 40 */
jimherd 0:a6a5d942b58f 41 MCP23017::MCP23017(PinName sda, PinName scl, int i2cAddress) : _i2c(sda, scl) {
jimherd 0:a6a5d942b58f 42 MCP23017_i2cAddress = i2cAddress;
jimherd 0:a6a5d942b58f 43 reset(); // initialise chip to power-on condition
jimherd 0:a6a5d942b58f 44 }
jimherd 0:a6a5d942b58f 45
jimherd 12:6d9d2b277f26 46 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 47 * reset
jimherd 0:a6a5d942b58f 48 * Set configuration (IOCON) and direction(IODIR) registers to initial state
jimherd 0:a6a5d942b58f 49 */
jimherd 0:a6a5d942b58f 50 void MCP23017::reset() {
jimherd 0:a6a5d942b58f 51 //
jimherd 0:a6a5d942b58f 52 // First make sure that the device is in BANK=0 mode
jimherd 0:a6a5d942b58f 53 //
jimherd 0:a6a5d942b58f 54 writeRegister(0x05, (unsigned char)0x00);
jimherd 0:a6a5d942b58f 55 //
jimherd 0:a6a5d942b58f 56 // set direction registers to inputs
jimherd 0:a6a5d942b58f 57 //
jimherd 0:a6a5d942b58f 58 writeRegister(IODIR, (unsigned short)0xFFFF);
jimherd 0:a6a5d942b58f 59 //
jimherd 9:e08c29541bc4 60 // set all other registers to zero (last of 10 registers is OLAT)
jimherd 0:a6a5d942b58f 61 //
jimherd 0:a6a5d942b58f 62 for (int reg_addr = 2 ; reg_addr <= OLAT ; reg_addr+=2) {
jimherd 0:a6a5d942b58f 63 writeRegister(reg_addr, (unsigned short)0x0000);
jimherd 0:a6a5d942b58f 64 }
jimherd 0:a6a5d942b58f 65 //
jimherd 0:a6a5d942b58f 66 // Set the shadow registers to power-on state
jimherd 0:a6a5d942b58f 67 //
jimherd 0:a6a5d942b58f 68 shadow_IODIR = 0xFFFF;
jimherd 0:a6a5d942b58f 69 shadow_GPIO = 0;
jimherd 0:a6a5d942b58f 70 shadow_GPPU = 0;
jimherd 0:a6a5d942b58f 71 shadow_IPOL = 0;
jimherd 0:a6a5d942b58f 72 }
jimherd 0:a6a5d942b58f 73
jimherd 12:6d9d2b277f26 74 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 75 * write_bit
jimherd 0:a6a5d942b58f 76 * Write a 1/0 to a single bit of the 16-bit port
jimherd 0:a6a5d942b58f 77 */
jimherd 0:a6a5d942b58f 78 void MCP23017::write_bit(int value, int bit_number) {
jimherd 0:a6a5d942b58f 79 if (value == 0) {
jimherd 0:a6a5d942b58f 80 shadow_GPIO &= ~(1 << bit_number);
jimherd 0:a6a5d942b58f 81 } else {
jimherd 0:a6a5d942b58f 82 shadow_GPIO |= 1 << bit_number;
jimherd 0:a6a5d942b58f 83 }
jimherd 0:a6a5d942b58f 84 writeRegister(GPIO, (unsigned short)shadow_GPIO);
jimherd 0:a6a5d942b58f 85 }
jimherd 0:a6a5d942b58f 86
jimherd 12:6d9d2b277f26 87 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 88 * Write a combination of bits to the 16-bit port
jimherd 0:a6a5d942b58f 89 */
jimherd 0:a6a5d942b58f 90 void MCP23017::write_mask(unsigned short data, unsigned short mask) {
jimherd 0:a6a5d942b58f 91 shadow_GPIO = (shadow_GPIO & ~mask) | data;
jimherd 0:a6a5d942b58f 92 writeRegister(GPIO, (unsigned short)shadow_GPIO);
jimherd 0:a6a5d942b58f 93 }
jimherd 0:a6a5d942b58f 94
jimherd 12:6d9d2b277f26 95 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 96 * read_bit
jimherd 0:a6a5d942b58f 97 * Read a single bit from the 16-bit port
jimherd 0:a6a5d942b58f 98 */
jimherd 0:a6a5d942b58f 99 int MCP23017::read_bit(int bit_number) {
jimherd 0:a6a5d942b58f 100 shadow_GPIO = readRegister(GPIO);
jimherd 0:a6a5d942b58f 101 return ((shadow_GPIO >> bit_number) & 0x0001);
jimherd 0:a6a5d942b58f 102 }
jimherd 0:a6a5d942b58f 103
jimherd 12:6d9d2b277f26 104 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 105 * read_mask
jimherd 0:a6a5d942b58f 106 */
jimherd 0:a6a5d942b58f 107 int MCP23017::read_mask(unsigned short mask) {
jimherd 0:a6a5d942b58f 108 shadow_GPIO = readRegister(GPIO);
jimherd 0:a6a5d942b58f 109 return (shadow_GPIO & mask);
jimherd 0:a6a5d942b58f 110 }
jimherd 0:a6a5d942b58f 111
jimherd 12:6d9d2b277f26 112 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 113 * Config
jimherd 0:a6a5d942b58f 114 * set direction and pull-up registers
jimherd 0:a6a5d942b58f 115 */
jimherd 0:a6a5d942b58f 116 void MCP23017::config(unsigned short dir_config, unsigned short pullup_config, unsigned short polarity_config) {
jimherd 0:a6a5d942b58f 117 shadow_IODIR = dir_config;
jimherd 0:a6a5d942b58f 118 writeRegister(IODIR, (unsigned short)shadow_IODIR);
jimherd 0:a6a5d942b58f 119 shadow_GPPU = pullup_config;
jimherd 0:a6a5d942b58f 120 writeRegister(GPPU, (unsigned short)shadow_GPPU);
jimherd 0:a6a5d942b58f 121 shadow_IPOL = polarity_config;
jimherd 0:a6a5d942b58f 122 writeRegister(IPOL, (unsigned short)shadow_IPOL);
jimherd 0:a6a5d942b58f 123 }
jimherd 0:a6a5d942b58f 124
jimherd 12:6d9d2b277f26 125 /*-----------------------------------------------------------------------------
jimherd 12:6d9d2b277f26 126 * writeRegister
jimherd 12:6d9d2b277f26 127 * write a byte
jimherd 12:6d9d2b277f26 128 */
jimherd 12:6d9d2b277f26 129 void MCP23017::writeRegister(int regAddress, unsigned char data) {
jimherd 12:6d9d2b277f26 130 char buffer[2];
jimherd 12:6d9d2b277f26 131
jimherd 12:6d9d2b277f26 132 buffer[0] = regAddress;
jimherd 12:6d9d2b277f26 133 buffer[1] = data;
jimherd 12:6d9d2b277f26 134 _i2c.write(MCP23017_i2cAddress, buffer, 2);
jimherd 12:6d9d2b277f26 135 }
jimherd 12:6d9d2b277f26 136
jimherd 12:6d9d2b277f26 137 /*----------------------------------------------------------------------------
jimherd 12:6d9d2b277f26 138 * write Register
jimherd 12:6d9d2b277f26 139 * write two bytes
jimherd 12:6d9d2b277f26 140 */
jimherd 12:6d9d2b277f26 141 void MCP23017::writeRegister(int regAddress, unsigned short data) {
jimherd 12:6d9d2b277f26 142 char buffer[3];
jimherd 12:6d9d2b277f26 143
jimherd 12:6d9d2b277f26 144 buffer[0] = regAddress;
jimherd 12:6d9d2b277f26 145 tmp_data.value16 = data;
jimherd 12:6d9d2b277f26 146 buffer[1] = tmp_data.value8[0];
jimherd 12:6d9d2b277f26 147 buffer[2] = tmp_data.value8[1];
jimherd 12:6d9d2b277f26 148
jimherd 12:6d9d2b277f26 149 _i2c.write(MCP23017_i2cAddress, buffer, 3);
jimherd 12:6d9d2b277f26 150 }
jimherd 12:6d9d2b277f26 151
jimherd 12:6d9d2b277f26 152 /*-----------------------------------------------------------------------------
jimherd 12:6d9d2b277f26 153 * readRegister
jimherd 12:6d9d2b277f26 154 */
jimherd 12:6d9d2b277f26 155 int MCP23017::readRegister(int regAddress) {
jimherd 12:6d9d2b277f26 156 char buffer[2];
jimherd 12:6d9d2b277f26 157
jimherd 12:6d9d2b277f26 158 buffer[0] = regAddress;
jimherd 12:6d9d2b277f26 159 _i2c.write(MCP23017_i2cAddress, buffer, 1);
jimherd 12:6d9d2b277f26 160 _i2c.read(MCP23017_i2cAddress, buffer, 2);
jimherd 12:6d9d2b277f26 161
jimherd 12:6d9d2b277f26 162 return ((int)(buffer[0] + (buffer[1]<<8)));
jimherd 12:6d9d2b277f26 163 }
jimherd 12:6d9d2b277f26 164
jimherd 12:6d9d2b277f26 165 /*-----------------------------------------------------------------------------
jimherd 12:6d9d2b277f26 166 * pinMode
jimherd 12:6d9d2b277f26 167 */
jimherd 12:6d9d2b277f26 168 void MCP23017::pinMode(int pin, int mode) {
jimherd 12:6d9d2b277f26 169 if (DIR_INPUT) {
jimherd 12:6d9d2b277f26 170 shadow_IODIR |= 1 << pin;
jimherd 12:6d9d2b277f26 171 } else {
jimherd 12:6d9d2b277f26 172 shadow_IODIR &= ~(1 << pin);
jimherd 12:6d9d2b277f26 173 }
jimherd 12:6d9d2b277f26 174 writeRegister(IODIR, (unsigned short)shadow_IODIR);
jimherd 12:6d9d2b277f26 175 }
jimherd 12:6d9d2b277f26 176
jimherd 12:6d9d2b277f26 177 /*-----------------------------------------------------------------------------
jimherd 12:6d9d2b277f26 178 * digitalRead
jimherd 12:6d9d2b277f26 179 */
jimherd 12:6d9d2b277f26 180 int MCP23017::digitalRead(int pin) {
jimherd 12:6d9d2b277f26 181 shadow_GPIO = readRegister(GPIO);
jimherd 12:6d9d2b277f26 182 if ( shadow_GPIO & (1 << pin)) {
jimherd 12:6d9d2b277f26 183 return 1;
jimherd 12:6d9d2b277f26 184 } else {
jimherd 12:6d9d2b277f26 185 return 0;
jimherd 12:6d9d2b277f26 186 }
jimherd 12:6d9d2b277f26 187 }
jimherd 12:6d9d2b277f26 188
jimherd 12:6d9d2b277f26 189 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 190 * digitalWrite
jimherd 0:a6a5d942b58f 191 */
jimherd 0:a6a5d942b58f 192 void MCP23017::digitalWrite(int pin, int val) {
jimherd 0:a6a5d942b58f 193 //If this pin is an INPUT pin, a write here will
jimherd 0:a6a5d942b58f 194 //enable the internal pullup
jimherd 0:a6a5d942b58f 195 //otherwise, it will set the OUTPUT voltage
jimherd 0:a6a5d942b58f 196 //as appropriate.
jimherd 0:a6a5d942b58f 197 bool isOutput = !(shadow_IODIR & 1<<pin);
jimherd 0:a6a5d942b58f 198
jimherd 0:a6a5d942b58f 199 if (isOutput) {
jimherd 0:a6a5d942b58f 200 //This is an output pin so just write the value
jimherd 0:a6a5d942b58f 201 if (val) shadow_GPIO |= 1 << pin;
jimherd 0:a6a5d942b58f 202 else shadow_GPIO &= ~(1 << pin);
jimherd 0:a6a5d942b58f 203 writeRegister(GPIO, (unsigned short)shadow_GPIO);
jimherd 0:a6a5d942b58f 204 } else {
jimherd 0:a6a5d942b58f 205 //This is an input pin, so we need to enable the pullup
jimherd 0:a6a5d942b58f 206 if (val) {
jimherd 0:a6a5d942b58f 207 shadow_GPPU |= 1 << pin;
jimherd 0:a6a5d942b58f 208 } else {
jimherd 0:a6a5d942b58f 209 shadow_GPPU &= ~(1 << pin);
jimherd 0:a6a5d942b58f 210 }
jimherd 0:a6a5d942b58f 211 writeRegister(GPPU, (unsigned short)shadow_GPPU);
jimherd 0:a6a5d942b58f 212 }
jimherd 0:a6a5d942b58f 213 }
jimherd 0:a6a5d942b58f 214
jimherd 12:6d9d2b277f26 215 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 216 * digitalWordRead
jimherd 0:a6a5d942b58f 217 */
jimherd 0:a6a5d942b58f 218 unsigned short MCP23017::digitalWordRead() {
jimherd 0:a6a5d942b58f 219 shadow_GPIO = readRegister(GPIO);
jimherd 0:a6a5d942b58f 220 return shadow_GPIO;
jimherd 0:a6a5d942b58f 221 }
jimherd 0:a6a5d942b58f 222
jimherd 12:6d9d2b277f26 223 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 224 * digitalWordWrite
jimherd 0:a6a5d942b58f 225 */
jimherd 0:a6a5d942b58f 226 void MCP23017::digitalWordWrite(unsigned short w) {
jimherd 0:a6a5d942b58f 227 shadow_GPIO = w;
jimherd 0:a6a5d942b58f 228 writeRegister(GPIO, (unsigned short)shadow_GPIO);
jimherd 0:a6a5d942b58f 229 }
jimherd 0:a6a5d942b58f 230
jimherd 12:6d9d2b277f26 231 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 232 * inputPolarityMask
jimherd 0:a6a5d942b58f 233 */
jimherd 0:a6a5d942b58f 234 void MCP23017::inputPolarityMask(unsigned short mask) {
jimherd 0:a6a5d942b58f 235 writeRegister(IPOL, mask);
jimherd 0:a6a5d942b58f 236 }
jimherd 0:a6a5d942b58f 237
jimherd 12:6d9d2b277f26 238 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 239 * inputoutputMask
jimherd 0:a6a5d942b58f 240 */
jimherd 0:a6a5d942b58f 241 void MCP23017::inputOutputMask(unsigned short mask) {
jimherd 0:a6a5d942b58f 242 shadow_IODIR = mask;
jimherd 0:a6a5d942b58f 243 writeRegister(IODIR, (unsigned short)shadow_IODIR);
jimherd 0:a6a5d942b58f 244 }
jimherd 0:a6a5d942b58f 245
jimherd 12:6d9d2b277f26 246 /*-----------------------------------------------------------------------------
jimherd 0:a6a5d942b58f 247 * internalPullupMask
jimherd 0:a6a5d942b58f 248 */
jimherd 0:a6a5d942b58f 249 void MCP23017::internalPullupMask(unsigned short mask) {
jimherd 0:a6a5d942b58f 250 shadow_GPPU = mask;
jimherd 0:a6a5d942b58f 251 writeRegister(GPPU, (unsigned short)shadow_GPPU);
jimherd 0:a6a5d942b58f 252 }
jimherd 0:a6a5d942b58f 253