16-Bit I/O Expander with Serial Interface (SPI)

Dependents:   relaekort_til_motorstyring Nucleo_MCP23S17_Test BSM02 POT_V_1_1

Committer:
stjo2809
Date:
Wed Jan 20 10:49:02 2016 +0000
Revision:
3:b2a44e1e54b8
Parent:
2:b74bf9a31168
Version 2 (tested)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stjo2809 2:b74bf9a31168 1 /* mbed MCP23S17 Library, for driving the MCP23S17 16-Bit I/O Expander with Serial Interface (SPI)
stjo2809 2:b74bf9a31168 2 * Copyright (c) 2015, Created by Steen Joergensen (stjo2809) inspired by Romilly Cocking MCP23S17 library
stjo2809 2:b74bf9a31168 3 *
stjo2809 2:b74bf9a31168 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
stjo2809 2:b74bf9a31168 5 * of this software and associated documentation files (the "Software"), to deal
stjo2809 2:b74bf9a31168 6 * in the Software without restriction, including without limitation the rights
stjo2809 2:b74bf9a31168 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
stjo2809 2:b74bf9a31168 8 * copies of the Software, and to permit persons to whom the Software is
stjo2809 2:b74bf9a31168 9 * furnished to do so, subject to the following conditions:
stjo2809 2:b74bf9a31168 10 *
stjo2809 2:b74bf9a31168 11 * The above copyright notice and this permission notice shall be included in
stjo2809 2:b74bf9a31168 12 * all copies or substantial portions of the Software.
stjo2809 2:b74bf9a31168 13 *
stjo2809 2:b74bf9a31168 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
stjo2809 2:b74bf9a31168 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
stjo2809 2:b74bf9a31168 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
stjo2809 2:b74bf9a31168 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
stjo2809 2:b74bf9a31168 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
stjo2809 2:b74bf9a31168 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
stjo2809 2:b74bf9a31168 20 * THE SOFTWARE.
stjo2809 2:b74bf9a31168 21 */
stjo2809 2:b74bf9a31168 22
stjo2809 2:b74bf9a31168 23 #include "mbed.h"
stjo2809 2:b74bf9a31168 24
stjo2809 2:b74bf9a31168 25 #ifndef MBED_MCP23S17_H
stjo2809 2:b74bf9a31168 26 #define MBED_MCP23S17_H
stjo2809 2:b74bf9a31168 27
stjo2809 2:b74bf9a31168 28 //=============================================================================
stjo2809 2:b74bf9a31168 29 // All Registers and there Address if BANK = 0
stjo2809 2:b74bf9a31168 30 //=============================================================================
stjo2809 2:b74bf9a31168 31
stjo2809 2:b74bf9a31168 32 #define IODIRA_ADDR 0x00 // Controls the direction of the data I/O on Port A
stjo2809 2:b74bf9a31168 33 #define IODIRB_ADDR 0x01 // Controls the direction of the data I/O on Port B
stjo2809 2:b74bf9a31168 34 #define IPOLA_ADDR 0x02 // Configure the polarity on the corresponding GPIO (Port A)
stjo2809 2:b74bf9a31168 35 #define IPOLB_ADDR 0x03 // Configure the polarity on the corresponding GPIO (Port B)
stjo2809 2:b74bf9a31168 36 #define GPINTENA_ADDR 0x04 // Controls the interrupt-on change feature for each pin for Port A
stjo2809 2:b74bf9a31168 37 #define GPINTENB_ADDR 0x05 // Controls the interrupt-on change feature for each pin for Port B
stjo2809 2:b74bf9a31168 38 #define DEFVALA_ADDR 0x06 // The default comparison value if the INTCONA is set to "1" for Port A
stjo2809 2:b74bf9a31168 39 #define DEFVALB_ADDR 0x07 // The default comparison value if the INTCONA is set to "1" for Port B
stjo2809 2:b74bf9a31168 40 #define INTCONA_ADDR 0x08 // Controls how the associated pin value is compared for the interrupt-on-change feature for Port A
stjo2809 2:b74bf9a31168 41 #define INTCONB_ADDR 0x09 // Controls how the associated pin value is compared for the interrupt-on-change feature for Port B
stjo2809 2:b74bf9a31168 42 #define IOCON_ADDR 0x0A // Contains several bits for configuring the device
stjo2809 2:b74bf9a31168 43 #define GPPUA_ADDR 0x0C // Controls the pull-up resistors for the port pins for port A
stjo2809 2:b74bf9a31168 44 #define GPPUB_ADDR 0x0D // Controls the pull-up resistors for the port pins for port B
stjo2809 2:b74bf9a31168 45 #define INTFA_ADDR 0x0E // READ ONLY // reflects the interrupt condition on port A pins of any pin that is enabled for interrupts via the GPINTEN register.
stjo2809 2:b74bf9a31168 46 #define INTFB_ADDR 0x0F // READ ONLY // reflects the interrupt condition on port B pins of any pin that is enabled for interrupts via the GPINTEN register.
stjo2809 2:b74bf9a31168 47 #define INTCAPA_ADDR 0x10 // READ ONLY // captures the GPIO port A value at the time the interrupt occurred
stjo2809 2:b74bf9a31168 48 #define INTCAPB_ADDR 0x11 // READ ONLY // captures the GPIO port B value at the time the interrupt occurred
stjo2809 2:b74bf9a31168 49 #define GPIOA_ADDR 0x12 // Reflects the value on the port A (doing write function it only read input)
stjo2809 2:b74bf9a31168 50 #define GPIOB_ADDR 0x13 // Reflects the value on the port B (doing write function it only read input)
stjo2809 2:b74bf9a31168 51 #define OLATA_ADDR 0x14 // A write to this register modifies the output latches that modifies the pins configured as outputs for Port A
stjo2809 2:b74bf9a31168 52 #define OLATB_ADDR 0x15 // A write to this register modifies the output latches that modifies the pins configured as outputs for Port B
stjo2809 2:b74bf9a31168 53
stjo2809 2:b74bf9a31168 54 //=============================================================================
stjo2809 2:b74bf9a31168 55 // Declaration of variables & custom #defines
stjo2809 2:b74bf9a31168 56 //=============================================================================
stjo2809 2:b74bf9a31168 57
stjo2809 2:b74bf9a31168 58 #define INTERRUPT_MIRROR_BIT 0x40
stjo2809 2:b74bf9a31168 59 #define INTERRUPT_POLARITY_BIT 0x02
stjo2809 2:b74bf9a31168 60
stjo2809 2:b74bf9a31168 61
stjo2809 2:b74bf9a31168 62 //=============================================================================
stjo2809 2:b74bf9a31168 63 // Functions Declaration
stjo2809 2:b74bf9a31168 64 //=============================================================================
stjo2809 2:b74bf9a31168 65
stjo2809 2:b74bf9a31168 66 /** Interface to the MCP23S17 16-Bit I/O Expander with Serial Interface (SPI)
stjo2809 2:b74bf9a31168 67 *
stjo2809 2:b74bf9a31168 68 * Using the driver:
stjo2809 2:b74bf9a31168 69 * - remenber to setup SPI in main routine.
stjo2809 2:b74bf9a31168 70 * - remenber to setup interrupt pin or pins in main routine (if you are using interrupts).
stjo2809 2:b74bf9a31168 71 *
stjo2809 2:b74bf9a31168 72 * Defaults in this driver:
stjo2809 2:b74bf9a31168 73 * - as default is hardware adressing "On" and if disable use "0" in hardwareaddress when creating the instance.
stjo2809 2:b74bf9a31168 74 * - as default is interrupt pins "Active High".
stjo2809 2:b74bf9a31168 75 * - as default is INTA is associated with PortA and INTB is associated with PortB.
stjo2809 2:b74bf9a31168 76 *
stjo2809 2:b74bf9a31168 77 * Limitations of using this driver:
stjo2809 2:b74bf9a31168 78 * - can't use Open-Drain output.
stjo2809 2:b74bf9a31168 79 * - can't use Sequential Operation mode bit.
stjo2809 2:b74bf9a31168 80 * - can't use BANK 1 addressing.
stjo2809 2:b74bf9a31168 81 *
stjo2809 2:b74bf9a31168 82 */
stjo2809 2:b74bf9a31168 83 class MCP23S17 {
stjo2809 2:b74bf9a31168 84 public:
stjo2809 2:b74bf9a31168 85 /** Create an instance of the MCP23S17 connected via specfied SPI instance, with the specified address.
stjo2809 2:b74bf9a31168 86 *
stjo2809 2:b74bf9a31168 87 * @param hardwareaddress The SPI hardware address 0-7 for this MCP23S17.
stjo2809 2:b74bf9a31168 88 * @param spi The mbed SPI instance (make in main routine)
stjo2809 2:b74bf9a31168 89 * @param nCs The SPI chip select pin.
stjo2809 2:b74bf9a31168 90 * @param nReset The Hardware reset pin.
stjo2809 2:b74bf9a31168 91 */
stjo2809 2:b74bf9a31168 92 MCP23S17(int hardwareaddress, SPI& spi, PinName nCs, PinName nReset);
stjo2809 2:b74bf9a31168 93
stjo2809 2:b74bf9a31168 94 /** Create an instance of the MCP23S17 connected via specfied SPI instance, with the specified address, and Global reset only.
stjo2809 2:b74bf9a31168 95 *
stjo2809 2:b74bf9a31168 96 * @param hardwareaddress The SPI hardware address 0-7 for this MCP23S17.
stjo2809 2:b74bf9a31168 97 * @param spi The mbed SPI instance (make in main routine)
stjo2809 2:b74bf9a31168 98 * @param nCs The SPI chip select pin.
stjo2809 2:b74bf9a31168 99 */
stjo2809 2:b74bf9a31168 100 MCP23S17(int hardwareaddress, SPI& spi, PinName nCs);
stjo2809 2:b74bf9a31168 101
stjo2809 2:b74bf9a31168 102
stjo2809 2:b74bf9a31168 103 /** Read an Register address.
stjo2809 2:b74bf9a31168 104 *
stjo2809 2:b74bf9a31168 105 * @param reg_address The selected register to read from.
stjo2809 2:b74bf9a31168 106 * @return The 8 bits read, but if GPIO register only the value of the inputs (outputs is read as "0").
stjo2809 2:b74bf9a31168 107 */
stjo2809 2:b74bf9a31168 108 char read(char reg_address);
stjo2809 2:b74bf9a31168 109
stjo2809 2:b74bf9a31168 110 /** Write to Register address.
stjo2809 2:b74bf9a31168 111 *
stjo2809 2:b74bf9a31168 112 * @param reg_adress The selected register to write to.
stjo2809 2:b74bf9a31168 113 * @param data The 8 bits to write to the register, but if GPIO only the output will change.
stjo2809 2:b74bf9a31168 114 */
stjo2809 2:b74bf9a31168 115 void write(char reg_address, char data);
stjo2809 2:b74bf9a31168 116
stjo2809 2:b74bf9a31168 117 /** Write to Bit in a register.
stjo2809 2:b74bf9a31168 118 *
stjo2809 2:b74bf9a31168 119 * @param reg_adress The selected register to write to.
stjo2809 2:b74bf9a31168 120 * @param bit The bit with to write in, values from 1 to 8
stjo2809 2:b74bf9a31168 121 * @param high_low The value to write the bit True = '1' and False = '0'.
stjo2809 2:b74bf9a31168 122 */
stjo2809 2:b74bf9a31168 123 void bit(char reg_address, int bitnumber, bool high_low);
stjo2809 2:b74bf9a31168 124
stjo2809 2:b74bf9a31168 125 /** Resetting the MCP23S17.
stjo2809 2:b74bf9a31168 126 *
stjo2809 2:b74bf9a31168 127 * Reset has to be pull down for min. 1uS to insure correct reset.
stjo2809 2:b74bf9a31168 128 * This function pull down the reset pin for 5uS.
stjo2809 2:b74bf9a31168 129 */
stjo2809 2:b74bf9a31168 130 void reset();
stjo2809 2:b74bf9a31168 131
stjo2809 2:b74bf9a31168 132 /** Read IODIRA.
stjo2809 2:b74bf9a31168 133 *
stjo2809 2:b74bf9a31168 134 * I/O DIRECTION REGISTER
stjo2809 2:b74bf9a31168 135 * Controls the direction of the data I/O.
stjo2809 2:b74bf9a31168 136 *
stjo2809 2:b74bf9a31168 137 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 138 */
stjo2809 2:b74bf9a31168 139 char iodira();
stjo2809 2:b74bf9a31168 140
stjo2809 2:b74bf9a31168 141 /** Write to IODIRA.
stjo2809 2:b74bf9a31168 142 *
stjo2809 2:b74bf9a31168 143 * I/O DIRECTION REGISTER
stjo2809 2:b74bf9a31168 144 * Controls the direction of the data I/O.
stjo2809 2:b74bf9a31168 145 *
stjo2809 2:b74bf9a31168 146 * @param data The 8 bits to write to IODIRA register.
stjo2809 2:b74bf9a31168 147 */
stjo2809 2:b74bf9a31168 148 void iodira(char data);
stjo2809 2:b74bf9a31168 149
stjo2809 2:b74bf9a31168 150 /** Read IODIRB.
stjo2809 2:b74bf9a31168 151 *
stjo2809 2:b74bf9a31168 152 * I/O DIRECTION REGISTER
stjo2809 2:b74bf9a31168 153 * Controls the direction of the data I/O.
stjo2809 2:b74bf9a31168 154 *
stjo2809 2:b74bf9a31168 155 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 156 */
stjo2809 2:b74bf9a31168 157 char iodirb();
stjo2809 2:b74bf9a31168 158
stjo2809 2:b74bf9a31168 159 /** Write to IODIRB.
stjo2809 2:b74bf9a31168 160 *
stjo2809 2:b74bf9a31168 161 * I/O DIRECTION REGISTER
stjo2809 2:b74bf9a31168 162 * Controls the direction of the data I/O.
stjo2809 2:b74bf9a31168 163 *
stjo2809 2:b74bf9a31168 164 * @param data The 8 bits to write to IODIRB register.
stjo2809 2:b74bf9a31168 165 */
stjo2809 2:b74bf9a31168 166 void iodirb(char data);
stjo2809 2:b74bf9a31168 167
stjo2809 2:b74bf9a31168 168 /** Read IPOLA.
stjo2809 2:b74bf9a31168 169 *
stjo2809 2:b74bf9a31168 170 * INPUT POLARITY REGISTER
stjo2809 2:b74bf9a31168 171 * This register allows the user to configure the polarity on the corresponding GPIO port bits.
stjo2809 2:b74bf9a31168 172 *
stjo2809 2:b74bf9a31168 173 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 174 */
stjo2809 2:b74bf9a31168 175 char ipola();
stjo2809 2:b74bf9a31168 176
stjo2809 2:b74bf9a31168 177 /** Write to IPOLA.
stjo2809 2:b74bf9a31168 178 *
stjo2809 2:b74bf9a31168 179 * INPUT POLARITY REGISTER
stjo2809 2:b74bf9a31168 180 * This register allows the user to configure the polarity on the corresponding GPIO port bits.
stjo2809 2:b74bf9a31168 181 *
stjo2809 2:b74bf9a31168 182 * @param data The 8 bits to write to IPOLA register.
stjo2809 2:b74bf9a31168 183 */
stjo2809 2:b74bf9a31168 184 void ipola(char data);
stjo2809 2:b74bf9a31168 185
stjo2809 2:b74bf9a31168 186 /** Read IPOLB.
stjo2809 2:b74bf9a31168 187 *
stjo2809 2:b74bf9a31168 188 * INPUT POLARITY REGISTER
stjo2809 2:b74bf9a31168 189 * This register allows the user to configure the polarity on the corresponding GPIO port bits.
stjo2809 2:b74bf9a31168 190 *
stjo2809 2:b74bf9a31168 191 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 192 */
stjo2809 2:b74bf9a31168 193 char ipolb();
stjo2809 2:b74bf9a31168 194
stjo2809 2:b74bf9a31168 195 /** Write to IPOLB.
stjo2809 2:b74bf9a31168 196 *
stjo2809 2:b74bf9a31168 197 * INPUT POLARITY REGISTER
stjo2809 2:b74bf9a31168 198 * This register allows the user to configure the polarity on the corresponding GPIO port bits.
stjo2809 2:b74bf9a31168 199 *
stjo2809 2:b74bf9a31168 200 * @param data The 8 bits to write to IPOLB register.
stjo2809 2:b74bf9a31168 201 */
stjo2809 2:b74bf9a31168 202 void ipolb(char data);
stjo2809 2:b74bf9a31168 203
stjo2809 2:b74bf9a31168 204 /** Read GPINTENA.
stjo2809 2:b74bf9a31168 205 *
stjo2809 2:b74bf9a31168 206 * INTERRUPT-ON-CHANGE CONTROL REGISTER
stjo2809 2:b74bf9a31168 207 * The GPINTEN register controls the interrupt-onchange feature for each pin.
stjo2809 2:b74bf9a31168 208 *
stjo2809 2:b74bf9a31168 209 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 210 */
stjo2809 2:b74bf9a31168 211 char gpintena();
stjo2809 2:b74bf9a31168 212
stjo2809 2:b74bf9a31168 213 /** Write to GPINTENA.
stjo2809 2:b74bf9a31168 214 *
stjo2809 2:b74bf9a31168 215 * INTERRUPT-ON-CHANGE CONTROL REGISTER
stjo2809 2:b74bf9a31168 216 * The GPINTEN register controls the interrupt-onchange feature for each pin.
stjo2809 2:b74bf9a31168 217 *
stjo2809 2:b74bf9a31168 218 * @param data The 8 bits to write to GPINTENA register.
stjo2809 2:b74bf9a31168 219 */
stjo2809 2:b74bf9a31168 220 void gpintena(char data);
stjo2809 2:b74bf9a31168 221
stjo2809 2:b74bf9a31168 222 /** Read GPINTENB.
stjo2809 2:b74bf9a31168 223 *
stjo2809 2:b74bf9a31168 224 * INTERRUPT-ON-CHANGE CONTROL REGISTER
stjo2809 2:b74bf9a31168 225 * The GPINTEN register controls the interrupt-onchange feature for each pin.
stjo2809 2:b74bf9a31168 226 *
stjo2809 2:b74bf9a31168 227 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 228 */
stjo2809 2:b74bf9a31168 229 char gpintenb();
stjo2809 2:b74bf9a31168 230
stjo2809 2:b74bf9a31168 231 /** Write to GPINTENB.
stjo2809 2:b74bf9a31168 232 *
stjo2809 2:b74bf9a31168 233 * INTERRUPT-ON-CHANGE CONTROL REGISTER
stjo2809 2:b74bf9a31168 234 * The GPINTEN register controls the interrupt-onchange feature for each pin.
stjo2809 2:b74bf9a31168 235 *
stjo2809 2:b74bf9a31168 236 * @param data The 8 bits to write to GPINTENB register.
stjo2809 2:b74bf9a31168 237 */
stjo2809 2:b74bf9a31168 238 void gpintenb(char data);
stjo2809 2:b74bf9a31168 239
stjo2809 2:b74bf9a31168 240 /** Read DEFVALA.
stjo2809 2:b74bf9a31168 241 *
stjo2809 2:b74bf9a31168 242 * DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE
stjo2809 2:b74bf9a31168 243 * The default comparison value is configured in the DEFVAL register, If enabled (via GPINTEN and INTCON).
stjo2809 2:b74bf9a31168 244 *
stjo2809 2:b74bf9a31168 245 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 246 */
stjo2809 2:b74bf9a31168 247 char defvala();
stjo2809 2:b74bf9a31168 248
stjo2809 2:b74bf9a31168 249 /** Write to DEFVALA.
stjo2809 2:b74bf9a31168 250 *
stjo2809 2:b74bf9a31168 251 * DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE
stjo2809 2:b74bf9a31168 252 * The default comparison value is configured in the DEFVAL register, If enabled (via GPINTEN and INTCON).
stjo2809 2:b74bf9a31168 253 *
stjo2809 2:b74bf9a31168 254 * @param data The 8 bits to write to DEVALA register.
stjo2809 2:b74bf9a31168 255 */
stjo2809 2:b74bf9a31168 256 void defvala(char data);
stjo2809 2:b74bf9a31168 257
stjo2809 2:b74bf9a31168 258 /** Read DEFVALB.
stjo2809 2:b74bf9a31168 259 *
stjo2809 2:b74bf9a31168 260 * DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE
stjo2809 2:b74bf9a31168 261 * The default comparison value is configured in the DEFVAL register, If enabled (via GPINTEN and INTCON).
stjo2809 2:b74bf9a31168 262 *
stjo2809 2:b74bf9a31168 263 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 264 */
stjo2809 2:b74bf9a31168 265 char defvalb();
stjo2809 2:b74bf9a31168 266
stjo2809 2:b74bf9a31168 267 /** Write to DEFVALB.
stjo2809 2:b74bf9a31168 268 *
stjo2809 2:b74bf9a31168 269 * DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE
stjo2809 2:b74bf9a31168 270 * The default comparison value is configured in the DEFVAL register, If enabled (via GPINTEN and INTCON).
stjo2809 2:b74bf9a31168 271 *
stjo2809 2:b74bf9a31168 272 * @param data The 8 bits to write to DEVALB register.
stjo2809 2:b74bf9a31168 273 */
stjo2809 2:b74bf9a31168 274 void defvalb(char data);
stjo2809 2:b74bf9a31168 275
stjo2809 2:b74bf9a31168 276 /** Read INTCONA.
stjo2809 2:b74bf9a31168 277 *
stjo2809 2:b74bf9a31168 278 * INTERRUPT CONTROL REGISTER
stjo2809 2:b74bf9a31168 279 * The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
stjo2809 2:b74bf9a31168 280 *
stjo2809 2:b74bf9a31168 281 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 282 */
stjo2809 2:b74bf9a31168 283 char intcona();
stjo2809 2:b74bf9a31168 284
stjo2809 2:b74bf9a31168 285 /** Write to INTCONA.
stjo2809 2:b74bf9a31168 286 *
stjo2809 2:b74bf9a31168 287 * INTERRUPT CONTROL REGISTER
stjo2809 2:b74bf9a31168 288 * The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
stjo2809 2:b74bf9a31168 289 *
stjo2809 2:b74bf9a31168 290 * @param data The 8 bits to write to INTCONA register.
stjo2809 2:b74bf9a31168 291 */
stjo2809 2:b74bf9a31168 292 void intcona(char data);
stjo2809 2:b74bf9a31168 293
stjo2809 2:b74bf9a31168 294 /** Read INTCONB.
stjo2809 2:b74bf9a31168 295 *
stjo2809 2:b74bf9a31168 296 * INTERRUPT CONTROL REGISTER
stjo2809 2:b74bf9a31168 297 * The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
stjo2809 2:b74bf9a31168 298 *
stjo2809 2:b74bf9a31168 299 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 300 */
stjo2809 2:b74bf9a31168 301 char intconb();
stjo2809 2:b74bf9a31168 302
stjo2809 2:b74bf9a31168 303 /** Write to INTCONB.
stjo2809 2:b74bf9a31168 304 *
stjo2809 2:b74bf9a31168 305 * INTERRUPT CONTROL REGISTER
stjo2809 2:b74bf9a31168 306 * The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
stjo2809 2:b74bf9a31168 307 *
stjo2809 2:b74bf9a31168 308 * @param data The 8 bits to write to INTCONB register.
stjo2809 2:b74bf9a31168 309 */
stjo2809 2:b74bf9a31168 310 void intconb(char data);
stjo2809 2:b74bf9a31168 311
stjo2809 2:b74bf9a31168 312 /** Read IOCON.
stjo2809 2:b74bf9a31168 313 *
stjo2809 2:b74bf9a31168 314 * CONFIGURATION REGISTER
stjo2809 2:b74bf9a31168 315 * The IOCON register contains several bits for configuring the device.
stjo2809 2:b74bf9a31168 316 *
stjo2809 2:b74bf9a31168 317 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 318 */
stjo2809 2:b74bf9a31168 319 char iocon();
stjo2809 2:b74bf9a31168 320
stjo2809 2:b74bf9a31168 321 /** Write to IOCON.
stjo2809 2:b74bf9a31168 322 *
stjo2809 2:b74bf9a31168 323 * CONFIGURATION REGISTER
stjo2809 2:b74bf9a31168 324 * The IOCON register contains several bits for configuring the device.
stjo2809 2:b74bf9a31168 325 *
stjo2809 2:b74bf9a31168 326 * @param data The 8 bits to write to IOCON register.
stjo2809 2:b74bf9a31168 327 */
stjo2809 2:b74bf9a31168 328 void iocon(char data);
stjo2809 2:b74bf9a31168 329
stjo2809 2:b74bf9a31168 330 /** Read GPPUA.
stjo2809 2:b74bf9a31168 331 *
stjo2809 2:b74bf9a31168 332 * PULL-UP RESISTOR CONFIGURATION REGISTER
stjo2809 2:b74bf9a31168 333 * The GPPU register controls the pull-up resistors for the port pins.
stjo2809 2:b74bf9a31168 334 *
stjo2809 2:b74bf9a31168 335 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 336 */
stjo2809 2:b74bf9a31168 337 char gppua();
stjo2809 2:b74bf9a31168 338
stjo2809 2:b74bf9a31168 339 /** Write to GPPUA.
stjo2809 2:b74bf9a31168 340 *
stjo2809 2:b74bf9a31168 341 * PULL-UP RESISTOR CONFIGURATION REGISTER
stjo2809 2:b74bf9a31168 342 * The GPPU register controls the pull-up resistors for the port pins.
stjo2809 2:b74bf9a31168 343 *
stjo2809 2:b74bf9a31168 344 * @param data The 8 bits to write to GPPUA register.
stjo2809 2:b74bf9a31168 345 */
stjo2809 2:b74bf9a31168 346 void gppua(char data);
stjo2809 2:b74bf9a31168 347
stjo2809 2:b74bf9a31168 348 /** Read GPPUB.
stjo2809 2:b74bf9a31168 349 *
stjo2809 2:b74bf9a31168 350 * PULL-UP RESISTOR CONFIGURATION REGISTER
stjo2809 2:b74bf9a31168 351 * The GPPU register controls the pull-up resistors for the port pins.
stjo2809 2:b74bf9a31168 352 *
stjo2809 2:b74bf9a31168 353 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 354 */
stjo2809 2:b74bf9a31168 355 char gppub();
stjo2809 2:b74bf9a31168 356
stjo2809 2:b74bf9a31168 357 /** Write to GPPUB.
stjo2809 2:b74bf9a31168 358 *
stjo2809 2:b74bf9a31168 359 * PULL-UP RESISTOR CONFIGURATION REGISTER
stjo2809 2:b74bf9a31168 360 * The GPPU register controls the pull-up resistors for the port pins.
stjo2809 2:b74bf9a31168 361 *
stjo2809 2:b74bf9a31168 362 * @param data The 8 bits to write to GPPUB register.
stjo2809 2:b74bf9a31168 363 */
stjo2809 2:b74bf9a31168 364 void gppub(char data);
stjo2809 2:b74bf9a31168 365
stjo2809 2:b74bf9a31168 366 /** Read INTFA.
stjo2809 2:b74bf9a31168 367 *
stjo2809 2:b74bf9a31168 368 * INTERRUPT FLAG REGISTER - READ ONLY
stjo2809 2:b74bf9a31168 369 * The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register.
stjo2809 2:b74bf9a31168 370 *
stjo2809 2:b74bf9a31168 371 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 372 */
stjo2809 2:b74bf9a31168 373 char intfa();
stjo2809 2:b74bf9a31168 374
stjo2809 2:b74bf9a31168 375 /** Read INTFB.
stjo2809 2:b74bf9a31168 376 *
stjo2809 2:b74bf9a31168 377 * INTERRUPT FLAG REGISTER - READ ONLY
stjo2809 2:b74bf9a31168 378 * The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register.
stjo2809 2:b74bf9a31168 379 *
stjo2809 2:b74bf9a31168 380 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 381 */
stjo2809 2:b74bf9a31168 382 char intfb();
stjo2809 2:b74bf9a31168 383
stjo2809 2:b74bf9a31168 384 /** Read INTCAPA.
stjo2809 2:b74bf9a31168 385 *
stjo2809 2:b74bf9a31168 386 * INTERRUPT CAPTURE REGISTER - READ ONLY
stjo2809 2:b74bf9a31168 387 * The INTCAP register captures the GPIO port value at the time the interrupt occurred. The register is ‘read only’ and is updated only when an interrupt occurs.
stjo2809 2:b74bf9a31168 388 *
stjo2809 2:b74bf9a31168 389 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 390 */
stjo2809 2:b74bf9a31168 391 char intcapa();
stjo2809 2:b74bf9a31168 392
stjo2809 2:b74bf9a31168 393 /** Read INTCAPB.
stjo2809 2:b74bf9a31168 394 *
stjo2809 2:b74bf9a31168 395 * INTERRUPT CAPTURE REGISTER - READ ONLY
stjo2809 2:b74bf9a31168 396 * The INTCAP register captures the GPIO port value at the time the interrupt occurred. The register is ‘read only’ and is updated only when an interrupt occurs.
stjo2809 2:b74bf9a31168 397 *
stjo2809 2:b74bf9a31168 398 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 399 */
stjo2809 2:b74bf9a31168 400 char intcapb();
stjo2809 2:b74bf9a31168 401
stjo2809 2:b74bf9a31168 402 /** Read GPIOA.
stjo2809 2:b74bf9a31168 403 *
stjo2809 2:b74bf9a31168 404 * PORT REGISTER
stjo2809 2:b74bf9a31168 405 * The GPIO register reflects the value on the port. Reading from this register reads the port. Writing to this register modifies the Output Latch (OLAT) register.
stjo2809 2:b74bf9a31168 406 *
stjo2809 2:b74bf9a31168 407 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 408 */
stjo2809 2:b74bf9a31168 409 char gpioa();
stjo2809 2:b74bf9a31168 410
stjo2809 2:b74bf9a31168 411 /** Write to GPIOA.
stjo2809 2:b74bf9a31168 412 *
stjo2809 2:b74bf9a31168 413 * PORT REGISTER
stjo2809 2:b74bf9a31168 414 * The GPIO register reflects the value on the port. Reading from this register reads the port. Writing to this register modifies the Output Latch (OLAT) register.
stjo2809 2:b74bf9a31168 415 *
stjo2809 2:b74bf9a31168 416 * @param data The 8 bits to write to GPIOA register.
stjo2809 2:b74bf9a31168 417 */
stjo2809 2:b74bf9a31168 418 void gpioa(char data);
stjo2809 2:b74bf9a31168 419
stjo2809 2:b74bf9a31168 420 /** Read GPIOB.
stjo2809 2:b74bf9a31168 421 *
stjo2809 2:b74bf9a31168 422 * PORT REGISTER
stjo2809 2:b74bf9a31168 423 * The GPIO register reflects the value on the port. Reading from this register reads the port. Writing to this register modifies the Output Latch (OLAT) register.
stjo2809 2:b74bf9a31168 424 *
stjo2809 2:b74bf9a31168 425 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 426 */
stjo2809 2:b74bf9a31168 427 char gpiob();
stjo2809 2:b74bf9a31168 428
stjo2809 2:b74bf9a31168 429 /** Write to GPIOB.
stjo2809 2:b74bf9a31168 430 *
stjo2809 2:b74bf9a31168 431 * PORT REGISTER
stjo2809 2:b74bf9a31168 432 * The GPIO register reflects the value on the port. Reading from this register reads the port. Writing to this register modifies the Output Latch (OLAT) register.
stjo2809 2:b74bf9a31168 433 *
stjo2809 2:b74bf9a31168 434 * @param data The 8 bits to write to GPIOB register.
stjo2809 2:b74bf9a31168 435 */
stjo2809 2:b74bf9a31168 436 void gpiob(char data);
stjo2809 2:b74bf9a31168 437
stjo2809 2:b74bf9a31168 438 /** Read OLATA.
stjo2809 2:b74bf9a31168 439 *
stjo2809 2:b74bf9a31168 440 * OUTPUT LATCH REGISTER
stjo2809 2:b74bf9a31168 441 * The OLAT register provides access to the output latches. A read from this register results in a read of the OLAT and not the port itself. A write to this register
stjo2809 2:b74bf9a31168 442 * modifies the output latches that modifies the pins configured as outputs.
stjo2809 2:b74bf9a31168 443 *
stjo2809 2:b74bf9a31168 444 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 445 */
stjo2809 2:b74bf9a31168 446 char olata();
stjo2809 2:b74bf9a31168 447
stjo2809 2:b74bf9a31168 448 /** Write to OLATA.
stjo2809 2:b74bf9a31168 449 *
stjo2809 2:b74bf9a31168 450 * OUTPUT LATCH REGISTER
stjo2809 2:b74bf9a31168 451 * The OLAT register provides access to the output latches. A read from this register results in a read of the OLAT and not the port itself. A write to this register
stjo2809 2:b74bf9a31168 452 * modifies the output latches that modifies the pins configured as outputs.
stjo2809 2:b74bf9a31168 453 *
stjo2809 2:b74bf9a31168 454 * @param data The 8 bits to write to OLATA register.
stjo2809 2:b74bf9a31168 455 */
stjo2809 2:b74bf9a31168 456 void olata(char data);
stjo2809 2:b74bf9a31168 457
stjo2809 2:b74bf9a31168 458 /** Read OLATB.
stjo2809 2:b74bf9a31168 459 *
stjo2809 2:b74bf9a31168 460 * OUTPUT LATCH REGISTER
stjo2809 2:b74bf9a31168 461 * The OLAT register provides access to the output latches. A read from this register results in a read of the OLAT and not the port itself. A write to this register
stjo2809 2:b74bf9a31168 462 * modifies the output latches that modifies the pins configured as outputs.
stjo2809 2:b74bf9a31168 463 *
stjo2809 2:b74bf9a31168 464 * @return The 8 bits read.
stjo2809 2:b74bf9a31168 465 */
stjo2809 2:b74bf9a31168 466 char olatb();
stjo2809 2:b74bf9a31168 467
stjo2809 2:b74bf9a31168 468 /** Write to OLATB.
stjo2809 2:b74bf9a31168 469 *
stjo2809 2:b74bf9a31168 470 * OUTPUT LATCH REGISTER
stjo2809 2:b74bf9a31168 471 * The OLAT register provides access to the output latches. A read from this register results in a read of the OLAT and not the port itself. A write to this register
stjo2809 2:b74bf9a31168 472 * modifies the output latches that modifies the pins configured as outputs.
stjo2809 2:b74bf9a31168 473 *
stjo2809 2:b74bf9a31168 474 * @param data The 8 bits to write to OLATB register.
stjo2809 2:b74bf9a31168 475 */
stjo2809 2:b74bf9a31168 476 void olatb(char data);
stjo2809 2:b74bf9a31168 477
stjo2809 2:b74bf9a31168 478 /** Write to IOCON.MIRROR
stjo2809 2:b74bf9a31168 479 *
stjo2809 2:b74bf9a31168 480 * IOCON REGISTER - INTERRUPT MIRROR BIT
stjo2809 2:b74bf9a31168 481 * 1 = The INT pins are internally connected
stjo2809 2:b74bf9a31168 482 * 0 = The INT pins are not connected. INTA is associated with PortA and INTB is associated with PortB
stjo2809 2:b74bf9a31168 483 *
stjo2809 2:b74bf9a31168 484 * @param mirror write true ('1') or false ('0').
stjo2809 2:b74bf9a31168 485 */
stjo2809 2:b74bf9a31168 486 void intmirror(bool mirror);
stjo2809 2:b74bf9a31168 487
stjo2809 2:b74bf9a31168 488 /** Write to IOCON.INTPOL
stjo2809 2:b74bf9a31168 489 *
stjo2809 2:b74bf9a31168 490 * IOCON REGISTER - INTERRUPT POLARITY BIT
stjo2809 2:b74bf9a31168 491 * This bit sets the polarity of the INT output pin.
stjo2809 2:b74bf9a31168 492 * 1 = Active-high.
stjo2809 2:b74bf9a31168 493 * 0 = Active-low.
stjo2809 2:b74bf9a31168 494 *
stjo2809 2:b74bf9a31168 495 * @param polarity write true ('1') or false ('0').
stjo2809 2:b74bf9a31168 496 */
stjo2809 2:b74bf9a31168 497 void intpol(bool polarity);
stjo2809 2:b74bf9a31168 498
stjo2809 2:b74bf9a31168 499 private:
stjo2809 2:b74bf9a31168 500 int _hardwareaddress;
stjo2809 2:b74bf9a31168 501 SPI& _spi;
stjo2809 2:b74bf9a31168 502 DigitalOut _nCs;
stjo2809 2:b74bf9a31168 503 DigitalOut _nReset;
stjo2809 2:b74bf9a31168 504 char _writeopcode;
stjo2809 2:b74bf9a31168 505 char _readopcode;
stjo2809 2:b74bf9a31168 506 void _initialization();
stjo2809 2:b74bf9a31168 507 void _make_opcode(int _hardwareaddress);
stjo2809 2:b74bf9a31168 508 char _read(char address);
stjo2809 2:b74bf9a31168 509 void _write(char address, char data);
stjo2809 2:b74bf9a31168 510
stjo2809 2:b74bf9a31168 511 };
stjo2809 2:b74bf9a31168 512
stjo2809 2:b74bf9a31168 513 #endif