Updated memory address #defines to avoid clashes with other libraries and f411re board defines
MCP23S17.h@2:69ce30406dc8, 2015-05-14 (annotated)
- Committer:
- dave93cab
- Date:
- Thu May 14 21:04:13 2015 +0000
- Revision:
- 2:69ce30406dc8
- Parent:
- 1:f93b811965d8
renamed memory address #defines to avoid clashes with other libraries and f411re board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stjo2809 | 0:e43a0fe24907 | 1 | /* mbed MCP23S17 Library, for driving the MCP23S17 16-Bit I/O Expander with Serial Interface (SPI) |
stjo2809 | 0:e43a0fe24907 | 2 | * Copyright (c) 2011, Created by Steen Joergensen (stjo2809) inspired by Romilly Cocking MCP23S17 library |
stjo2809 | 0:e43a0fe24907 | 3 | * |
stjo2809 | 0:e43a0fe24907 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
stjo2809 | 0:e43a0fe24907 | 5 | * of this software and associated documentation files (the "Software"), to deal |
stjo2809 | 0:e43a0fe24907 | 6 | * in the Software without restriction, including without limitation the rights |
stjo2809 | 0:e43a0fe24907 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
stjo2809 | 0:e43a0fe24907 | 8 | * copies of the Software, and to permit persons to whom the Software is |
stjo2809 | 0:e43a0fe24907 | 9 | * furnished to do so, subject to the following conditions: |
stjo2809 | 0:e43a0fe24907 | 10 | * |
stjo2809 | 0:e43a0fe24907 | 11 | * The above copyright notice and this permission notice shall be included in |
stjo2809 | 0:e43a0fe24907 | 12 | * all copies or substantial portions of the Software. |
stjo2809 | 0:e43a0fe24907 | 13 | * |
stjo2809 | 0:e43a0fe24907 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
stjo2809 | 0:e43a0fe24907 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
stjo2809 | 0:e43a0fe24907 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
stjo2809 | 0:e43a0fe24907 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
stjo2809 | 0:e43a0fe24907 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
stjo2809 | 0:e43a0fe24907 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
stjo2809 | 0:e43a0fe24907 | 20 | * THE SOFTWARE. |
stjo2809 | 0:e43a0fe24907 | 21 | */ |
stjo2809 | 0:e43a0fe24907 | 22 | |
stjo2809 | 0:e43a0fe24907 | 23 | #include "mbed.h" |
stjo2809 | 0:e43a0fe24907 | 24 | |
stjo2809 | 0:e43a0fe24907 | 25 | #ifndef MBED_MCP23S17_H |
stjo2809 | 0:e43a0fe24907 | 26 | #define MBED_MCP23S17_H |
stjo2809 | 0:e43a0fe24907 | 27 | |
stjo2809 | 0:e43a0fe24907 | 28 | //============================================================================= |
stjo2809 | 0:e43a0fe24907 | 29 | // All Registers and there Address if BANK = 0 |
stjo2809 | 0:e43a0fe24907 | 30 | //============================================================================= |
stjo2809 | 0:e43a0fe24907 | 31 | |
dave93cab | 2:69ce30406dc8 | 32 | #define MCP23S17_IODIRA 0x00 // Controls the direction of the data I/O on Port A |
dave93cab | 2:69ce30406dc8 | 33 | #define MCP23S17_IODIRB 0x01 // Controls the direction of the data I/O on Port B |
dave93cab | 2:69ce30406dc8 | 34 | #define MCP23S17_IPOLA 0x02 // Configure the polarity on the corresponding GPIO (Port A) |
dave93cab | 2:69ce30406dc8 | 35 | #define MCP23S17_IPOLB 0x03 // Configure the polarity on the corresponding GPIO (Port B) |
dave93cab | 2:69ce30406dc8 | 36 | #define MCP23S17_GPINTENA 0x04 // Controls the interrupt-on change feature for each pin for Port A |
dave93cab | 2:69ce30406dc8 | 37 | #define MCP23S17_GPINTENB 0x05 // Controls the interrupt-on change feature for each pin for Port B |
dave93cab | 2:69ce30406dc8 | 38 | #define MCP23S17_DEFVALA 0x06 // The default comparison value if the INTCONA is set to "1" for Port A |
dave93cab | 2:69ce30406dc8 | 39 | #define MCP23S17_DEFVALB 0x07 // The default comparison value if the INTCONA is set to "1" for Port B |
dave93cab | 2:69ce30406dc8 | 40 | #define MCP23S17_INTCONA 0x08 // Controls how the associated pin value is compared for the interrupt-on-change feature for Port A |
dave93cab | 2:69ce30406dc8 | 41 | #define MCP23S17_INTCONB 0x09 // Controls how the associated pin value is compared for the interrupt-on-change feature for Port B |
dave93cab | 2:69ce30406dc8 | 42 | #define MCP23S17_IOCON 0x0A // Contains several bits for configuring the device |
dave93cab | 2:69ce30406dc8 | 43 | #define MCP23S17_GPPUA 0x0C // Controls the pull-up resistors for the port pins for port A |
dave93cab | 2:69ce30406dc8 | 44 | #define MCP23S17_GPPUB 0x0D // Controls the pull-up resistors for the port pins for port B |
dave93cab | 2:69ce30406dc8 | 45 | #define MCP23S17_INTFA 0x0E // READ ONLY // reflects the interrupt condition on port A pins of any pin that is enabled for interrupts via the GPINTEN register. |
dave93cab | 2:69ce30406dc8 | 46 | #define MCP23S17_INTFB 0x0F // READ ONLY // reflects the interrupt condition on port B pins of any pin that is enabled for interrupts via the GPINTEN register. |
dave93cab | 2:69ce30406dc8 | 47 | #define MCP23S17_INTCAPA 0x10 // READ ONLY // captures the GPIO port A value at the time the interrupt occurred |
dave93cab | 2:69ce30406dc8 | 48 | #define MCP23S17_INTCAPB 0x11 // READ ONLY // captures the GPIO port B value at the time the interrupt occurred |
dave93cab | 2:69ce30406dc8 | 49 | #define MCP23S17_GPIOA 0x12 // Reflects the value on the port A (doing write function it only read input) |
dave93cab | 2:69ce30406dc8 | 50 | #define MCP23S17_GPIOB 0x13 // Reflects the value on the port B (doing write function it only read input) |
dave93cab | 2:69ce30406dc8 | 51 | #define MCP23S17_OLATA 0x14 // A write to this register modifies the output latches that modifies the pins configured as outputs for Port A |
dave93cab | 2:69ce30406dc8 | 52 | #define MCP23S17_OLATB 0x15 // A write to this register modifies the output latches that modifies the pins configured as outputs for Port B |
stjo2809 | 0:e43a0fe24907 | 53 | |
stjo2809 | 0:e43a0fe24907 | 54 | //============================================================================= |
stjo2809 | 0:e43a0fe24907 | 55 | // Declaration of variables & custom #defines |
stjo2809 | 0:e43a0fe24907 | 56 | //============================================================================= |
stjo2809 | 0:e43a0fe24907 | 57 | |
stjo2809 | 0:e43a0fe24907 | 58 | #define INTERRUPT_MIRROR_BIT 0x40 |
stjo2809 | 0:e43a0fe24907 | 59 | #define INTERRUPT_POLARITY_BIT 0x02 |
stjo2809 | 0:e43a0fe24907 | 60 | |
stjo2809 | 0:e43a0fe24907 | 61 | enum Port { PORT_A, PORT_B }; |
stjo2809 | 0:e43a0fe24907 | 62 | enum Polarity { ACTIVE_LOW, ACTIVE_HIGH }; |
stjo2809 | 0:e43a0fe24907 | 63 | |
stjo2809 | 0:e43a0fe24907 | 64 | //============================================================================= |
stjo2809 | 0:e43a0fe24907 | 65 | // Functions Declaration |
stjo2809 | 0:e43a0fe24907 | 66 | //============================================================================= |
stjo2809 | 0:e43a0fe24907 | 67 | |
stjo2809 | 0:e43a0fe24907 | 68 | /** Interface to the MCP23S17 16-Bit I/O Expander with Serial Interface (SPI) |
stjo2809 | 0:e43a0fe24907 | 69 | * |
stjo2809 | 0:e43a0fe24907 | 70 | * Using the driver: |
stjo2809 | 0:e43a0fe24907 | 71 | * - remenber to setup SPI in main routine. |
stjo2809 | 0:e43a0fe24907 | 72 | * - remenber to setup interrupt pin or pins in main routine (if you are using interrupts). |
stjo2809 | 0:e43a0fe24907 | 73 | * |
stjo2809 | 0:e43a0fe24907 | 74 | * Defaults in this driver: |
stjo2809 | 0:e43a0fe24907 | 75 | * - as default is hardware adressing "On" and if disable use "0" in hardwareaddress when creating the instance. |
stjo2809 | 0:e43a0fe24907 | 76 | * - as default is interrupt pins "Active High". |
stjo2809 | 0:e43a0fe24907 | 77 | * - as default is INTA is associated with PortA and INTB is associated with PortB. |
stjo2809 | 0:e43a0fe24907 | 78 | * |
stjo2809 | 0:e43a0fe24907 | 79 | * Limitations of using this driver: |
stjo2809 | 0:e43a0fe24907 | 80 | * - can't use Open-Drain output. |
stjo2809 | 0:e43a0fe24907 | 81 | * - can't use Sequential Operation mode bit. |
stjo2809 | 0:e43a0fe24907 | 82 | * - can't use BANK 1 addressing. |
stjo2809 | 0:e43a0fe24907 | 83 | * |
stjo2809 | 0:e43a0fe24907 | 84 | */ |
stjo2809 | 0:e43a0fe24907 | 85 | class MCP23S17 { |
stjo2809 | 0:e43a0fe24907 | 86 | public: |
stjo2809 | 0:e43a0fe24907 | 87 | /** Create an instance of the MCP23S17 connected to specfied SPI pins, with the specified address. |
stjo2809 | 0:e43a0fe24907 | 88 | * |
stjo2809 | 0:e43a0fe24907 | 89 | * @param hardwareaddress The SPI hardware address 0-7 for this MCP23S17. |
stjo2809 | 0:e43a0fe24907 | 90 | * @param spi The mbed SPI instance (make in main routine) |
stjo2809 | 0:e43a0fe24907 | 91 | * @param nCs The SPI chip select pin. |
stjo2809 | 0:e43a0fe24907 | 92 | * @param nReset The Hardware reset pin. |
stjo2809 | 0:e43a0fe24907 | 93 | */ |
stjo2809 | 0:e43a0fe24907 | 94 | MCP23S17(int hardwareaddress, SPI& spi, PinName nCs, PinName nReset); |
stjo2809 | 0:e43a0fe24907 | 95 | |
stjo2809 | 0:e43a0fe24907 | 96 | /** Read the port IO pins level. |
stjo2809 | 0:e43a0fe24907 | 97 | * |
stjo2809 | 0:e43a0fe24907 | 98 | * @param Port The selected port to read from. |
stjo2809 | 0:e43a0fe24907 | 99 | * @return The 8 bits read, but only the value of the inputs (outputs is read as "0"). |
stjo2809 | 0:e43a0fe24907 | 100 | */ |
stjo2809 | 0:e43a0fe24907 | 101 | char read(Port port); |
stjo2809 | 0:e43a0fe24907 | 102 | |
stjo2809 | 0:e43a0fe24907 | 103 | /** Write to the port IO pins. |
stjo2809 | 0:e43a0fe24907 | 104 | * |
stjo2809 | 0:e43a0fe24907 | 105 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 106 | * @param data The 8 bits to write to the IO port, but will only change the output. |
stjo2809 | 0:e43a0fe24907 | 107 | */ |
stjo2809 | 0:e43a0fe24907 | 108 | void write(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 109 | |
stjo2809 | 0:e43a0fe24907 | 110 | /** Resetting the MCP23S17. |
stjo2809 | 0:e43a0fe24907 | 111 | * |
stjo2809 | 0:e43a0fe24907 | 112 | * Reset has to be pull down for min. 1uS to insure correct reset. |
stjo2809 | 0:e43a0fe24907 | 113 | * This function pull down the reset pin for 5uS. |
stjo2809 | 0:e43a0fe24907 | 114 | */ |
stjo2809 | 0:e43a0fe24907 | 115 | void reset(); |
stjo2809 | 1:f93b811965d8 | 116 | |
stjo2809 | 1:f93b811965d8 | 117 | /** Read a register. |
stjo2809 | 1:f93b811965d8 | 118 | * |
stjo2809 | 1:f93b811965d8 | 119 | * @param address Enter register navn to read from. |
stjo2809 | 1:f93b811965d8 | 120 | * @return The 8 bits read, but only the value of the inputs (outputs is read as "0"). |
stjo2809 | 1:f93b811965d8 | 121 | */ |
stjo2809 | 1:f93b811965d8 | 122 | char read_register(char address); |
stjo2809 | 0:e43a0fe24907 | 123 | |
stjo2809 | 0:e43a0fe24907 | 124 | /** Configure the control setting (IOCON register). |
stjo2809 | 0:e43a0fe24907 | 125 | * |
stjo2809 | 0:e43a0fe24907 | 126 | * @param data The 8 bits to write to IOCON register. |
stjo2809 | 0:e43a0fe24907 | 127 | */ |
stjo2809 | 0:e43a0fe24907 | 128 | void config_control_register(char data); |
stjo2809 | 0:e43a0fe24907 | 129 | |
stjo2809 | 0:e43a0fe24907 | 130 | /** Configure if the port pins has Pull ups or not (GPPUn register). |
stjo2809 | 0:e43a0fe24907 | 131 | * |
stjo2809 | 0:e43a0fe24907 | 132 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 133 | * @param data The 8 bits to write to GPPUn register. |
stjo2809 | 0:e43a0fe24907 | 134 | */ |
stjo2809 | 0:e43a0fe24907 | 135 | void config_pullups(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 136 | |
stjo2809 | 0:e43a0fe24907 | 137 | /** Configure the direction of the port pins (IODIRn register). |
stjo2809 | 0:e43a0fe24907 | 138 | * |
stjo2809 | 0:e43a0fe24907 | 139 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 140 | * @param data The 8 bits to write to IODIRn register. |
stjo2809 | 0:e43a0fe24907 | 141 | */ |
stjo2809 | 0:e43a0fe24907 | 142 | void config_direction(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 143 | |
stjo2809 | 0:e43a0fe24907 | 144 | /** Configure the input polarity of the port pins (IPOLn register). |
stjo2809 | 0:e43a0fe24907 | 145 | * |
stjo2809 | 0:e43a0fe24907 | 146 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 147 | * @param data The 8 bits to write to IPOLn register. |
stjo2809 | 0:e43a0fe24907 | 148 | */ |
stjo2809 | 0:e43a0fe24907 | 149 | void config_polarity(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 150 | |
stjo2809 | 0:e43a0fe24907 | 151 | /** Configure if the port pins is interruptable or not (GPINTENn register). |
stjo2809 | 0:e43a0fe24907 | 152 | * |
stjo2809 | 0:e43a0fe24907 | 153 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 154 | * @param data The 8 bits to write to GPINTENn register. |
stjo2809 | 0:e43a0fe24907 | 155 | */ |
stjo2809 | 0:e43a0fe24907 | 156 | void config_interrupt_enable(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 157 | |
stjo2809 | 0:e43a0fe24907 | 158 | /** Configure if the interrupts is compared to default value register or against the previous value (INTCONn register). |
stjo2809 | 0:e43a0fe24907 | 159 | * |
stjo2809 | 0:e43a0fe24907 | 160 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 161 | * @param data The 8 bits to write to INTCONn register. |
stjo2809 | 0:e43a0fe24907 | 162 | */ |
stjo2809 | 0:e43a0fe24907 | 163 | void config_interrupt_control(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 164 | |
stjo2809 | 0:e43a0fe24907 | 165 | /** Configure if the interrupts pins is connected internally or separated into intA and intB (default = separated). |
stjo2809 | 0:e43a0fe24907 | 166 | * |
stjo2809 | 0:e43a0fe24907 | 167 | * @param mirror Write True or False (True = interconnected). |
stjo2809 | 0:e43a0fe24907 | 168 | */ |
stjo2809 | 0:e43a0fe24907 | 169 | void config_mirror_interrupt(bool mirror); |
stjo2809 | 0:e43a0fe24907 | 170 | |
stjo2809 | 0:e43a0fe24907 | 171 | /** Configure the default value that the interrupt value is compared to if it setup for use (DEFVALn register). |
stjo2809 | 0:e43a0fe24907 | 172 | * |
stjo2809 | 0:e43a0fe24907 | 173 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 174 | * @param data The 8 bits to write to DEFVALn register. |
stjo2809 | 0:e43a0fe24907 | 175 | */ |
stjo2809 | 0:e43a0fe24907 | 176 | void config_defaultvalue(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 177 | |
stjo2809 | 0:e43a0fe24907 | 178 | /** Configure if the interrupts is active high or active low. |
stjo2809 | 0:e43a0fe24907 | 179 | * |
stjo2809 | 0:e43a0fe24907 | 180 | * @param polarity Write ACTIVE_LOW or ACTIVE_HIGH. |
stjo2809 | 0:e43a0fe24907 | 181 | */ |
stjo2809 | 0:e43a0fe24907 | 182 | void config_interrupt_polarity(Polarity polarity); |
stjo2809 | 0:e43a0fe24907 | 183 | |
stjo2809 | 0:e43a0fe24907 | 184 | /** Read the interrupt flags for the port. |
stjo2809 | 0:e43a0fe24907 | 185 | * |
stjo2809 | 0:e43a0fe24907 | 186 | * @param Port The selected port to read from. |
stjo2809 | 0:e43a0fe24907 | 187 | * @return The 8 bits read, but only value for the inputs (outputs is read as "0"). |
stjo2809 | 0:e43a0fe24907 | 188 | */ |
stjo2809 | 0:e43a0fe24907 | 189 | char read_interrupt_flag(Port port); |
stjo2809 | 0:e43a0fe24907 | 190 | |
stjo2809 | 0:e43a0fe24907 | 191 | /** Read the value of the port captured the time the interrupt occurred (use this is for fast changing inputs). |
stjo2809 | 0:e43a0fe24907 | 192 | * |
stjo2809 | 0:e43a0fe24907 | 193 | * @param Port The selected port to read from. |
stjo2809 | 0:e43a0fe24907 | 194 | * @return The 8 bits read, but only value for the inputs (outputs is read as "0"). |
stjo2809 | 0:e43a0fe24907 | 195 | */ |
stjo2809 | 0:e43a0fe24907 | 196 | char read_interrupt_capture(Port port); |
stjo2809 | 0:e43a0fe24907 | 197 | |
stjo2809 | 0:e43a0fe24907 | 198 | private: |
stjo2809 | 0:e43a0fe24907 | 199 | SPI& _spi; |
stjo2809 | 0:e43a0fe24907 | 200 | DigitalOut _nCs; |
stjo2809 | 0:e43a0fe24907 | 201 | DigitalOut _nReset; |
stjo2809 | 0:e43a0fe24907 | 202 | int _hardwareaddress; |
stjo2809 | 0:e43a0fe24907 | 203 | char _writeopcode; |
stjo2809 | 0:e43a0fe24907 | 204 | char _readopcode; |
stjo2809 | 0:e43a0fe24907 | 205 | void _initialization(); |
stjo2809 | 0:e43a0fe24907 | 206 | void _make_opcode(int _hardwareaddress); |
stjo2809 | 0:e43a0fe24907 | 207 | char _read(char address); // _read function is overloaded |
stjo2809 | 0:e43a0fe24907 | 208 | char _read(Port port, char address); |
stjo2809 | 0:e43a0fe24907 | 209 | void _write(char address, char data); // _write function is overloaded |
stjo2809 | 0:e43a0fe24907 | 210 | void _write(Port port, char address, char data); |
stjo2809 | 0:e43a0fe24907 | 211 | |
stjo2809 | 0:e43a0fe24907 | 212 | }; |
stjo2809 | 0:e43a0fe24907 | 213 | |
stjo2809 | 0:e43a0fe24907 | 214 | #endif |
stjo2809 | 0:e43a0fe24907 | 215 |