Mingcheng Lin / Mbed 2 deprecated TEST

Dependencies:   mbed

Committer:
Humberttt
Date:
Tue Jun 11 08:57:23 2019 +0000
Revision:
2:eca01caa037f
Parent:
0:3d3331775af2
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mrweilun 0:3d3331775af2 1 /* MCP23017 library for Arduino
mrweilun 0:3d3331775af2 2 Copyright (C) 2009 David Pye <davidmpye@gmail.com
mrweilun 0:3d3331775af2 3 Modified for use on the MBED ARM platform
mrweilun 0:3d3331775af2 4
mrweilun 0:3d3331775af2 5 This program is free software: you can redistribute it and/or modify
mrweilun 0:3d3331775af2 6 it under the terms of the GNU General Public License as published by
mrweilun 0:3d3331775af2 7 the Free Software Foundation, either version 3 of the License, or
mrweilun 0:3d3331775af2 8 (at your option) any later version.
mrweilun 0:3d3331775af2 9
mrweilun 0:3d3331775af2 10 This program is distributed in the hope that it will be useful,
mrweilun 0:3d3331775af2 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
mrweilun 0:3d3331775af2 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mrweilun 0:3d3331775af2 13 GNU General Public License for more details.
mrweilun 0:3d3331775af2 14
mrweilun 0:3d3331775af2 15 You should have received a copy of the GNU General Public License
mrweilun 0:3d3331775af2 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
mrweilun 0:3d3331775af2 17 */
mrweilun 0:3d3331775af2 18
mrweilun 0:3d3331775af2 19 #ifndef MBED_MCP23017_H
mrweilun 0:3d3331775af2 20 #define MBED_MCP23017_H
mrweilun 0:3d3331775af2 21
mrweilun 0:3d3331775af2 22 #include "mbed.h"
mrweilun 0:3d3331775af2 23
mrweilun 0:3d3331775af2 24 //
mrweilun 0:3d3331775af2 25 // Register defines from data sheet - we set IOCON.BANK to 0
mrweilun 0:3d3331775af2 26 // as it is easier to manage the registers sequentially.
mrweilun 0:3d3331775af2 27 //
mrweilun 0:3d3331775af2 28 #define IODIR 0x00
mrweilun 0:3d3331775af2 29 #define IPOL 0x02
mrweilun 0:3d3331775af2 30 #define GPINTEN 0x04
mrweilun 0:3d3331775af2 31 #define DEFVAL 0x06
mrweilun 0:3d3331775af2 32 #define INTCON 0x08
mrweilun 0:3d3331775af2 33 #define IOCON 0x0A
mrweilun 0:3d3331775af2 34 #define GPPU 0x0C
mrweilun 0:3d3331775af2 35 #define INTF 0x0E
mrweilun 0:3d3331775af2 36 #define INTCAP 0x10
mrweilun 0:3d3331775af2 37 #define GPIO 0x12
mrweilun 0:3d3331775af2 38 #define OLAT 0x14
mrweilun 0:3d3331775af2 39
mrweilun 0:3d3331775af2 40 #define I2C_BASE_ADDRESS 0x40
mrweilun 0:3d3331775af2 41
mrweilun 0:3d3331775af2 42 #define DIR_OUTPUT 0
mrweilun 0:3d3331775af2 43 #define DIR_INPUT 1
mrweilun 0:3d3331775af2 44
mrweilun 0:3d3331775af2 45 /** MCP23017 class
mrweilun 0:3d3331775af2 46 *
mrweilun 0:3d3331775af2 47 * Allow access to an I2C connected MCP23017 16-bit I/O extender chip
mrweilun 0:3d3331775af2 48 * Example:
mrweilun 0:3d3331775af2 49 * @code
mrweilun 0:3d3331775af2 50 * MCP23017 *par_port;
mrweilun 0:3d3331775af2 51 * @endcode
mrweilun 0:3d3331775af2 52 *
mrweilun 0:3d3331775af2 53 */
mrweilun 0:3d3331775af2 54 class MCP23017 {
mrweilun 0:3d3331775af2 55 public:
mrweilun 0:3d3331775af2 56 /** Constructor for the MCP23017 connected to specified I2C pins at a specific address
mrweilun 0:3d3331775af2 57 *
mrweilun 0:3d3331775af2 58 * 16-bit I/O expander with I2C interface
mrweilun 0:3d3331775af2 59 *
mrweilun 0:3d3331775af2 60 * @param sda I2C data pin
mrweilun 0:3d3331775af2 61 * @param scl I2C clock pin
mrweilun 0:3d3331775af2 62 * @param i2cAddress I2C address
mrweilun 0:3d3331775af2 63 */
mrweilun 0:3d3331775af2 64 MCP23017(PinName sda, PinName scl, int i2cAddress);
mrweilun 0:3d3331775af2 65
mrweilun 0:3d3331775af2 66 /** Reset MCP23017 device to its power-on state
mrweilun 0:3d3331775af2 67 */
mrweilun 0:3d3331775af2 68 void reset(void);
mrweilun 0:3d3331775af2 69
mrweilun 0:3d3331775af2 70 /** Write a 0/1 value to an output bit
mrweilun 0:3d3331775af2 71 *
mrweilun 0:3d3331775af2 72 * @param value 0 or 1
mrweilun 0:3d3331775af2 73 * @param bit_number bit number range 0 --> 15
mrweilun 0:3d3331775af2 74 */
mrweilun 0:3d3331775af2 75 void write_bit(int value, int bit_number);
mrweilun 0:3d3331775af2 76
mrweilun 0:3d3331775af2 77 /** Write a masked 16-bit value to the device
mrweilun 0:3d3331775af2 78 *
mrweilun 0:3d3331775af2 79 * @param data 16-bit data value
mrweilun 0:3d3331775af2 80 * @param mask 16-bit mask value
mrweilun 0:3d3331775af2 81 */
mrweilun 0:3d3331775af2 82 void write_mask(unsigned short data, unsigned short mask);
mrweilun 0:3d3331775af2 83
mrweilun 0:3d3331775af2 84 /** Read a 0/1 value from an input bit
mrweilun 0:3d3331775af2 85 *
mrweilun 0:3d3331775af2 86 * @param bit_number bit number range 0 --> 15
mrweilun 0:3d3331775af2 87 * @return 0/1 value read
mrweilun 0:3d3331775af2 88 */
mrweilun 0:3d3331775af2 89 int read_bit(int bit_number);
mrweilun 0:3d3331775af2 90
mrweilun 0:3d3331775af2 91 /** Read a 16-bit value from the device and apply mask
mrweilun 0:3d3331775af2 92 *
mrweilun 0:3d3331775af2 93 * @param mask 16-bit mask value
mrweilun 0:3d3331775af2 94 * @return 16-bit data with mask applied
mrweilun 0:3d3331775af2 95 */
mrweilun 0:3d3331775af2 96 int read_mask(unsigned short mask);
mrweilun 0:3d3331775af2 97
mrweilun 0:3d3331775af2 98 /** Configure an MCP23017 device
mrweilun 0:3d3331775af2 99 *
mrweilun 0:3d3331775af2 100 * @param dir_config data direction value (1 = input, 0 = output)
mrweilun 0:3d3331775af2 101 * @param pullup_config 100k pullup value (1 = enabled, 0 = disabled)
mrweilun 0:3d3331775af2 102 * @param polarity_config polarity value (1 = flip, 0 = normal)
mrweilun 0:3d3331775af2 103 */
mrweilun 0:3d3331775af2 104 void config(unsigned short dir_config, unsigned short pullup_config, unsigned short polarity_config);
mrweilun 0:3d3331775af2 105
mrweilun 0:3d3331775af2 106 void writeRegister(int regAddress, unsigned char val);
mrweilun 0:3d3331775af2 107 void writeRegister(int regAddress, unsigned short val);
mrweilun 0:3d3331775af2 108 int readRegister(int regAddress);
mrweilun 0:3d3331775af2 109
mrweilun 0:3d3331775af2 110 /*-----------------------------------------------------------------------------
mrweilun 0:3d3331775af2 111 * pinmode
mrweilun 0:3d3331775af2 112 * Set units to sequential, bank0 mode
mrweilun 0:3d3331775af2 113 */
mrweilun 0:3d3331775af2 114 void pinMode(int pin, int mode);
mrweilun 0:3d3331775af2 115 void digitalWrite(int pin, int val);
mrweilun 0:3d3331775af2 116 int digitalRead(int pin);
mrweilun 0:3d3331775af2 117
mrweilun 0:3d3331775af2 118 // These provide a more advanced mapping of the chip functionality
mrweilun 0:3d3331775af2 119 // See the data sheet for more information on what they do
mrweilun 0:3d3331775af2 120
mrweilun 0:3d3331775af2 121 //Returns a word with the current pin states (ie contents of the GPIO register)
mrweilun 0:3d3331775af2 122 unsigned short digitalWordRead();
mrweilun 0:3d3331775af2 123 // Allows you to write a word to the GPIO register
mrweilun 0:3d3331775af2 124 void digitalWordWrite(unsigned short w);
mrweilun 0:3d3331775af2 125 // Sets up the polarity mask that the MCP23017 supports
mrweilun 0:3d3331775af2 126 // if set to 1, it will flip the actual pin value.
mrweilun 0:3d3331775af2 127 void inputPolarityMask(unsigned short mask);
mrweilun 0:3d3331775af2 128 //Sets which pins are inputs or outputs (1 = input, 0 = output) NB Opposite to arduino's
mrweilun 0:3d3331775af2 129 //definition for these
mrweilun 0:3d3331775af2 130 void inputOutputMask(unsigned short mask);
mrweilun 0:3d3331775af2 131 // Allows enabling of the internal 100k pullup resisters (1 = enabled, 0 = disabled)
mrweilun 0:3d3331775af2 132 void internalPullupMask(unsigned short mask);
mrweilun 0:3d3331775af2 133 int read(void);
mrweilun 0:3d3331775af2 134 void write(int data);
mrweilun 0:3d3331775af2 135
mrweilun 0:3d3331775af2 136 protected:
mrweilun 0:3d3331775af2 137 I2C _i2c;
mrweilun 0:3d3331775af2 138 int MCP23017_i2cAddress; // physical I2C address
mrweilun 0:3d3331775af2 139 unsigned short shadow_GPIO, shadow_IODIR, shadow_GPPU, shadow_IPOL; // Cached copies of the register values
mrweilun 0:3d3331775af2 140
mrweilun 0:3d3331775af2 141 };
mrweilun 0:3d3331775af2 142
mrweilun 0:3d3331775af2 143 #endif