Updated memory address #defines to avoid clashes with other libraries and f411re board defines
MCP23S17.h@0:e43a0fe24907, 2011-11-02 (annotated)
- Committer:
- stjo2809
- Date:
- Wed Nov 02 20:51:30 2011 +0000
- Revision:
- 0:e43a0fe24907
- Child:
- 1:f93b811965d8
1st. Release
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 | |
stjo2809 | 0:e43a0fe24907 | 32 | #define IODIRA 0x00 // Controls the direction of the data I/O on Port A |
stjo2809 | 0:e43a0fe24907 | 33 | #define IODIRB 0x01 // Controls the direction of the data I/O on Port B |
stjo2809 | 0:e43a0fe24907 | 34 | #define IPOLA 0x02 // Configure the polarity on the corresponding GPIO (Port A) |
stjo2809 | 0:e43a0fe24907 | 35 | #define IPOLB 0x03 // Configure the polarity on the corresponding GPIO (Port B) |
stjo2809 | 0:e43a0fe24907 | 36 | #define GPINTENA 0x04 // Controls the interrupt-on change feature for each pin for Port A |
stjo2809 | 0:e43a0fe24907 | 37 | #define GPINTENB 0x05 // Controls the interrupt-on change feature for each pin for Port B |
stjo2809 | 0:e43a0fe24907 | 38 | #define DEFVALA 0x06 // The default comparison value if the INTCONA is set to "1" for Port A |
stjo2809 | 0:e43a0fe24907 | 39 | #define DEFVALB 0x07 // The default comparison value if the INTCONA is set to "1" for Port B |
stjo2809 | 0:e43a0fe24907 | 40 | #define INTCONA 0x08 // Controls how the associated pin value is compared for the interrupt-on-change feature for Port A |
stjo2809 | 0:e43a0fe24907 | 41 | #define INTCONB 0x09 // Controls how the associated pin value is compared for the interrupt-on-change feature for Port B |
stjo2809 | 0:e43a0fe24907 | 42 | #define IOCON 0x0A // Contains several bits for configuring the device |
stjo2809 | 0:e43a0fe24907 | 43 | #define GPPUA 0x0C // Controls the pull-up resistors for the port pins for port A |
stjo2809 | 0:e43a0fe24907 | 44 | #define GPPUB 0x0D // Controls the pull-up resistors for the port pins for port B |
stjo2809 | 0:e43a0fe24907 | 45 | #define INTFA 0x0E // READ ONLY // reflects the interrupt condition on port A pins of any pin that is enabled for interrupts via the GPINTEN register. |
stjo2809 | 0:e43a0fe24907 | 46 | #define INTFB 0x0F // READ ONLY // reflects the interrupt condition on port B pins of any pin that is enabled for interrupts via the GPINTEN register. |
stjo2809 | 0:e43a0fe24907 | 47 | #define INTCAPA 0x10 // READ ONLY // captures the GPIO port A value at the time the interrupt occurred |
stjo2809 | 0:e43a0fe24907 | 48 | #define INTCAPB 0x11 // READ ONLY // captures the GPIO port B value at the time the interrupt occurred |
stjo2809 | 0:e43a0fe24907 | 49 | #define GPIOA 0x12 // Reflects the value on the port A (doing write function it only read input) |
stjo2809 | 0:e43a0fe24907 | 50 | #define GPIOB 0x13 // Reflects the value on the port B (doing write function it only read input) |
stjo2809 | 0:e43a0fe24907 | 51 | #define OLATA 0x14 // A write to this register modifies the output latches that modifies the pins configured as outputs for Port A |
stjo2809 | 0:e43a0fe24907 | 52 | #define 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 | 0:e43a0fe24907 | 116 | |
stjo2809 | 0:e43a0fe24907 | 117 | /** Configure the control setting (IOCON register). |
stjo2809 | 0:e43a0fe24907 | 118 | * |
stjo2809 | 0:e43a0fe24907 | 119 | * @param data The 8 bits to write to IOCON register. |
stjo2809 | 0:e43a0fe24907 | 120 | */ |
stjo2809 | 0:e43a0fe24907 | 121 | void config_control_register(char data); |
stjo2809 | 0:e43a0fe24907 | 122 | |
stjo2809 | 0:e43a0fe24907 | 123 | /** Configure if the port pins has Pull ups or not (GPPUn register). |
stjo2809 | 0:e43a0fe24907 | 124 | * |
stjo2809 | 0:e43a0fe24907 | 125 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 126 | * @param data The 8 bits to write to GPPUn register. |
stjo2809 | 0:e43a0fe24907 | 127 | */ |
stjo2809 | 0:e43a0fe24907 | 128 | void config_pullups(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 129 | |
stjo2809 | 0:e43a0fe24907 | 130 | /** Configure the direction of the port pins (IODIRn 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 IODIRn register. |
stjo2809 | 0:e43a0fe24907 | 134 | */ |
stjo2809 | 0:e43a0fe24907 | 135 | void config_direction(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 136 | |
stjo2809 | 0:e43a0fe24907 | 137 | /** Configure the input polarity of the port pins (IPOLn 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 IPOLn register. |
stjo2809 | 0:e43a0fe24907 | 141 | */ |
stjo2809 | 0:e43a0fe24907 | 142 | void config_polarity(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 143 | |
stjo2809 | 0:e43a0fe24907 | 144 | /** Configure if the port pins is interruptable or not (GPINTENn 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 GPINTENn register. |
stjo2809 | 0:e43a0fe24907 | 148 | */ |
stjo2809 | 0:e43a0fe24907 | 149 | void config_interrupt_enable(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 150 | |
stjo2809 | 0:e43a0fe24907 | 151 | /** Configure if the interrupts is compared to default value register or against the previous value (INTCONn 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 INTCONn register. |
stjo2809 | 0:e43a0fe24907 | 155 | */ |
stjo2809 | 0:e43a0fe24907 | 156 | void config_interrupt_control(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 157 | |
stjo2809 | 0:e43a0fe24907 | 158 | /** Configure if the interrupts pins is connected internally or separated into intA and intB (default = separated). |
stjo2809 | 0:e43a0fe24907 | 159 | * |
stjo2809 | 0:e43a0fe24907 | 160 | * @param mirror Write True or False (True = interconnected). |
stjo2809 | 0:e43a0fe24907 | 161 | */ |
stjo2809 | 0:e43a0fe24907 | 162 | void config_mirror_interrupt(bool mirror); |
stjo2809 | 0:e43a0fe24907 | 163 | |
stjo2809 | 0:e43a0fe24907 | 164 | /** Configure the default value that the interrupt value is compared to if it setup for use (DEFVALn register). |
stjo2809 | 0:e43a0fe24907 | 165 | * |
stjo2809 | 0:e43a0fe24907 | 166 | * @param Port The selected port to write to. |
stjo2809 | 0:e43a0fe24907 | 167 | * @param data The 8 bits to write to DEFVALn register. |
stjo2809 | 0:e43a0fe24907 | 168 | */ |
stjo2809 | 0:e43a0fe24907 | 169 | void config_defaultvalue(Port port, char data); |
stjo2809 | 0:e43a0fe24907 | 170 | |
stjo2809 | 0:e43a0fe24907 | 171 | /** Configure if the interrupts is active high or active low. |
stjo2809 | 0:e43a0fe24907 | 172 | * |
stjo2809 | 0:e43a0fe24907 | 173 | * @param polarity Write ACTIVE_LOW or ACTIVE_HIGH. |
stjo2809 | 0:e43a0fe24907 | 174 | */ |
stjo2809 | 0:e43a0fe24907 | 175 | void config_interrupt_polarity(Polarity polarity); |
stjo2809 | 0:e43a0fe24907 | 176 | |
stjo2809 | 0:e43a0fe24907 | 177 | /** Read the interrupt flags for the port. |
stjo2809 | 0:e43a0fe24907 | 178 | * |
stjo2809 | 0:e43a0fe24907 | 179 | * @param Port The selected port to read from. |
stjo2809 | 0:e43a0fe24907 | 180 | * @return The 8 bits read, but only value for the inputs (outputs is read as "0"). |
stjo2809 | 0:e43a0fe24907 | 181 | */ |
stjo2809 | 0:e43a0fe24907 | 182 | char read_interrupt_flag(Port port); |
stjo2809 | 0:e43a0fe24907 | 183 | |
stjo2809 | 0:e43a0fe24907 | 184 | /** Read the value of the port captured the time the interrupt occurred (use this is for fast changing inputs). |
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_capture(Port port); |
stjo2809 | 0:e43a0fe24907 | 190 | |
stjo2809 | 0:e43a0fe24907 | 191 | private: |
stjo2809 | 0:e43a0fe24907 | 192 | SPI& _spi; |
stjo2809 | 0:e43a0fe24907 | 193 | DigitalOut _nCs; |
stjo2809 | 0:e43a0fe24907 | 194 | DigitalOut _nReset; |
stjo2809 | 0:e43a0fe24907 | 195 | int _hardwareaddress; |
stjo2809 | 0:e43a0fe24907 | 196 | char _writeopcode; |
stjo2809 | 0:e43a0fe24907 | 197 | char _readopcode; |
stjo2809 | 0:e43a0fe24907 | 198 | void _initialization(); |
stjo2809 | 0:e43a0fe24907 | 199 | void _make_opcode(int _hardwareaddress); |
stjo2809 | 0:e43a0fe24907 | 200 | char _read(char address); // _read function is overloaded |
stjo2809 | 0:e43a0fe24907 | 201 | char _read(Port port, char address); |
stjo2809 | 0:e43a0fe24907 | 202 | void _write(char address, char data); // _write function is overloaded |
stjo2809 | 0:e43a0fe24907 | 203 | void _write(Port port, char address, char data); |
stjo2809 | 0:e43a0fe24907 | 204 | |
stjo2809 | 0:e43a0fe24907 | 205 | }; |
stjo2809 | 0:e43a0fe24907 | 206 | |
stjo2809 | 0:e43a0fe24907 | 207 | #endif |
stjo2809 | 0:e43a0fe24907 | 208 |