Updated memory address #defines to avoid clashes with other libraries and f411re board defines

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?

UserRevisionLine numberNew 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 #include "MCP23S17.h"
stjo2809 0:e43a0fe24907 25
stjo2809 0:e43a0fe24907 26 //=============================================================================
stjo2809 0:e43a0fe24907 27 // Public functions
stjo2809 0:e43a0fe24907 28 //=============================================================================
stjo2809 0:e43a0fe24907 29
stjo2809 0:e43a0fe24907 30 MCP23S17::MCP23S17(int hardwareaddress, SPI& spi, PinName nCs, PinName nReset) : _spi(spi), _nCs(nCs), _nReset(nReset)
stjo2809 0:e43a0fe24907 31 {
stjo2809 0:e43a0fe24907 32 _hardwareaddress = hardwareaddress;
stjo2809 0:e43a0fe24907 33 _make_opcode(_hardwareaddress);
stjo2809 0:e43a0fe24907 34 _initialization();
stjo2809 0:e43a0fe24907 35 }
stjo2809 0:e43a0fe24907 36
stjo2809 0:e43a0fe24907 37 char MCP23S17::read(Port port)
stjo2809 0:e43a0fe24907 38 {
dave93cab 2:69ce30406dc8 39 return _read(port, MCP23S17_INTCAPA );
stjo2809 0:e43a0fe24907 40 }
stjo2809 0:e43a0fe24907 41
stjo2809 0:e43a0fe24907 42 void MCP23S17::write(Port port, char data)
stjo2809 0:e43a0fe24907 43 {
dave93cab 2:69ce30406dc8 44 _write(port, MCP23S17_OLATA, data);
stjo2809 0:e43a0fe24907 45 }
stjo2809 0:e43a0fe24907 46
stjo2809 0:e43a0fe24907 47 void MCP23S17::reset()
stjo2809 0:e43a0fe24907 48 {
stjo2809 0:e43a0fe24907 49 _nReset = 0;
stjo2809 0:e43a0fe24907 50 wait_us(5);
stjo2809 0:e43a0fe24907 51 _nReset = 1;
stjo2809 0:e43a0fe24907 52 _initialization();
stjo2809 0:e43a0fe24907 53 }
stjo2809 1:f93b811965d8 54
stjo2809 1:f93b811965d8 55 char MCP23S17::read_register(char address)
stjo2809 1:f93b811965d8 56 {
stjo2809 1:f93b811965d8 57 return _read(address);
stjo2809 1:f93b811965d8 58 }
stjo2809 0:e43a0fe24907 59
stjo2809 0:e43a0fe24907 60 void MCP23S17::config_control_register(char data)
stjo2809 0:e43a0fe24907 61 {
dave93cab 2:69ce30406dc8 62 _write(MCP23S17_IOCON, data);
stjo2809 0:e43a0fe24907 63 }
stjo2809 0:e43a0fe24907 64
stjo2809 0:e43a0fe24907 65 void MCP23S17::config_pullups(Port port, char data)
stjo2809 0:e43a0fe24907 66 {
dave93cab 2:69ce30406dc8 67 _write(port, MCP23S17_GPPUA, data);
stjo2809 0:e43a0fe24907 68 }
stjo2809 0:e43a0fe24907 69
stjo2809 0:e43a0fe24907 70 void MCP23S17::config_direction(Port port, char data)
stjo2809 0:e43a0fe24907 71 {
dave93cab 2:69ce30406dc8 72 _write(port, MCP23S17_IODIRA, data);
stjo2809 0:e43a0fe24907 73 }
stjo2809 0:e43a0fe24907 74
stjo2809 0:e43a0fe24907 75 void MCP23S17::config_polarity(Port port, char data)
stjo2809 0:e43a0fe24907 76 {
dave93cab 2:69ce30406dc8 77 _write(port, MCP23S17_IPOLA, data);
stjo2809 0:e43a0fe24907 78 }
stjo2809 0:e43a0fe24907 79
stjo2809 0:e43a0fe24907 80 void MCP23S17::config_interrupt_enable(Port port, char data)
stjo2809 0:e43a0fe24907 81 {
dave93cab 2:69ce30406dc8 82 _write(port, MCP23S17_GPINTENA, data);
stjo2809 0:e43a0fe24907 83 }
stjo2809 0:e43a0fe24907 84
stjo2809 0:e43a0fe24907 85 void MCP23S17::config_interrupt_control(Port port, char data)
stjo2809 0:e43a0fe24907 86 {
dave93cab 2:69ce30406dc8 87 _write(port, MCP23S17_INTCONA , data);
stjo2809 0:e43a0fe24907 88 }
stjo2809 0:e43a0fe24907 89
stjo2809 0:e43a0fe24907 90 void MCP23S17::config_mirror_interrupt(bool mirror)
stjo2809 0:e43a0fe24907 91 {
dave93cab 2:69ce30406dc8 92 char kopi_iocon = _read(MCP23S17_IOCON);
stjo2809 0:e43a0fe24907 93 if (mirror)
stjo2809 0:e43a0fe24907 94 {
stjo2809 0:e43a0fe24907 95 kopi_iocon = kopi_iocon | INTERRUPT_MIRROR_BIT;
stjo2809 0:e43a0fe24907 96 }
stjo2809 0:e43a0fe24907 97 else
stjo2809 0:e43a0fe24907 98 {
stjo2809 0:e43a0fe24907 99 kopi_iocon = kopi_iocon & ~INTERRUPT_MIRROR_BIT;
stjo2809 0:e43a0fe24907 100 }
dave93cab 2:69ce30406dc8 101 _write(MCP23S17_IOCON, kopi_iocon);
stjo2809 0:e43a0fe24907 102 }
stjo2809 0:e43a0fe24907 103
stjo2809 0:e43a0fe24907 104 void MCP23S17::config_defaultvalue(Port port, char data)
stjo2809 0:e43a0fe24907 105 {
dave93cab 2:69ce30406dc8 106 _write(port, MCP23S17_DEFVALA, data);
stjo2809 0:e43a0fe24907 107 }
stjo2809 0:e43a0fe24907 108
stjo2809 0:e43a0fe24907 109 void MCP23S17::config_interrupt_polarity(Polarity polarity)
stjo2809 0:e43a0fe24907 110 {
dave93cab 2:69ce30406dc8 111 char kopi_iocon = _read(MCP23S17_IOCON);
stjo2809 0:e43a0fe24907 112 if (polarity == ACTIVE_LOW)
stjo2809 0:e43a0fe24907 113 {
stjo2809 0:e43a0fe24907 114 kopi_iocon = kopi_iocon | INTERRUPT_POLARITY_BIT;
stjo2809 0:e43a0fe24907 115 }
stjo2809 0:e43a0fe24907 116 else
stjo2809 0:e43a0fe24907 117 {
stjo2809 0:e43a0fe24907 118 kopi_iocon = kopi_iocon & ~INTERRUPT_POLARITY_BIT;
stjo2809 0:e43a0fe24907 119 }
dave93cab 2:69ce30406dc8 120 _write(MCP23S17_IOCON, kopi_iocon);
stjo2809 0:e43a0fe24907 121 }
stjo2809 0:e43a0fe24907 122
stjo2809 0:e43a0fe24907 123 char MCP23S17::read_interrupt_flag(Port port)
stjo2809 0:e43a0fe24907 124 {
dave93cab 2:69ce30406dc8 125 return _read(port, MCP23S17_INTFA);
stjo2809 0:e43a0fe24907 126 }
stjo2809 0:e43a0fe24907 127
stjo2809 0:e43a0fe24907 128 char MCP23S17::read_interrupt_capture(Port port)
stjo2809 0:e43a0fe24907 129 {
dave93cab 2:69ce30406dc8 130 return _read(port, MCP23S17_INTCAPA );
stjo2809 0:e43a0fe24907 131 }
stjo2809 0:e43a0fe24907 132
stjo2809 0:e43a0fe24907 133 //=============================================================================
stjo2809 0:e43a0fe24907 134 // Private functions
stjo2809 0:e43a0fe24907 135 //=============================================================================
stjo2809 0:e43a0fe24907 136
stjo2809 0:e43a0fe24907 137 void MCP23S17::_initialization()
stjo2809 0:e43a0fe24907 138 {
dave93cab 2:69ce30406dc8 139 _write(MCP23S17_IOCON, 0x2A); // setup af control register (BANK = 0, MIRROR = 0, SEQOP = 1, DISSLW = 0, HAEN = 1, ODR = 0, INTPOL = 1, NC = 0)
stjo2809 0:e43a0fe24907 140 }
stjo2809 0:e43a0fe24907 141
stjo2809 0:e43a0fe24907 142 void MCP23S17::_make_opcode(int _hardwareaddress)
stjo2809 0:e43a0fe24907 143 {
stjo2809 0:e43a0fe24907 144 switch(_hardwareaddress)
stjo2809 0:e43a0fe24907 145 {
stjo2809 0:e43a0fe24907 146 case 0:
stjo2809 0:e43a0fe24907 147 _writeopcode = 0x40;
stjo2809 0:e43a0fe24907 148 _readopcode = 0x41;
stjo2809 0:e43a0fe24907 149 break;
stjo2809 0:e43a0fe24907 150
stjo2809 0:e43a0fe24907 151 case 1:
stjo2809 0:e43a0fe24907 152 _writeopcode = 0x42;
stjo2809 0:e43a0fe24907 153 _readopcode = 0x43;
stjo2809 0:e43a0fe24907 154 break;
stjo2809 0:e43a0fe24907 155
stjo2809 0:e43a0fe24907 156 case 2:
stjo2809 0:e43a0fe24907 157 _writeopcode = 0x44;
stjo2809 0:e43a0fe24907 158 _readopcode = 0x45;
stjo2809 0:e43a0fe24907 159 break;
stjo2809 0:e43a0fe24907 160
stjo2809 0:e43a0fe24907 161 case 3:
stjo2809 0:e43a0fe24907 162 _writeopcode = 0x46;
stjo2809 0:e43a0fe24907 163 _readopcode = 0x47;
stjo2809 0:e43a0fe24907 164 break;
stjo2809 0:e43a0fe24907 165
stjo2809 0:e43a0fe24907 166 case 4:
stjo2809 0:e43a0fe24907 167 _writeopcode = 0x48;
stjo2809 0:e43a0fe24907 168 _readopcode = 0x49;
stjo2809 0:e43a0fe24907 169 break;
stjo2809 0:e43a0fe24907 170
stjo2809 0:e43a0fe24907 171 case 5:
stjo2809 0:e43a0fe24907 172 _writeopcode = 0x4A;
stjo2809 0:e43a0fe24907 173 _readopcode = 0x4B;
stjo2809 0:e43a0fe24907 174 break;
stjo2809 0:e43a0fe24907 175
stjo2809 0:e43a0fe24907 176 case 6:
stjo2809 0:e43a0fe24907 177 _writeopcode = 0x4C;
stjo2809 0:e43a0fe24907 178 _readopcode = 0x4D;
stjo2809 0:e43a0fe24907 179 break;
stjo2809 0:e43a0fe24907 180
stjo2809 0:e43a0fe24907 181 case 7:
stjo2809 0:e43a0fe24907 182 _writeopcode = 0x4E;
stjo2809 0:e43a0fe24907 183 _readopcode = 0x4F;
stjo2809 0:e43a0fe24907 184 break;
stjo2809 0:e43a0fe24907 185 }
stjo2809 0:e43a0fe24907 186 }
stjo2809 0:e43a0fe24907 187
stjo2809 0:e43a0fe24907 188 char MCP23S17::_read(char address) // _read function is overloaded
stjo2809 0:e43a0fe24907 189 {
stjo2809 0:e43a0fe24907 190 _nCs = 0;
stjo2809 0:e43a0fe24907 191 _spi.write(_readopcode);
stjo2809 0:e43a0fe24907 192 _spi.write(address);
stjo2809 0:e43a0fe24907 193 char response = _spi.write(0xFF); // 0xFF data to get response
stjo2809 0:e43a0fe24907 194 _nCs = 1;
stjo2809 0:e43a0fe24907 195 return response;
stjo2809 0:e43a0fe24907 196 }
stjo2809 0:e43a0fe24907 197
stjo2809 0:e43a0fe24907 198 char MCP23S17::_read(Port port, char address) // _read function is overloaded
stjo2809 0:e43a0fe24907 199 {
stjo2809 0:e43a0fe24907 200 return _read(address + (char) port);
stjo2809 0:e43a0fe24907 201 }
stjo2809 0:e43a0fe24907 202
stjo2809 0:e43a0fe24907 203 void MCP23S17::_write(char address, char data) // _write function is overloaded
stjo2809 0:e43a0fe24907 204 {
stjo2809 0:e43a0fe24907 205 _nCs = 0;
stjo2809 0:e43a0fe24907 206 _spi.write(_writeopcode);
stjo2809 0:e43a0fe24907 207 _spi.write(address);
stjo2809 0:e43a0fe24907 208 _spi.write(data);
stjo2809 0:e43a0fe24907 209 _nCs = 1;
stjo2809 0:e43a0fe24907 210 }
stjo2809 0:e43a0fe24907 211
stjo2809 0:e43a0fe24907 212 void MCP23S17::_write(Port port, char address, char data) // _write function is overloaded
stjo2809 0:e43a0fe24907 213 {
stjo2809 0:e43a0fe24907 214 return _write(address + (char) port, data);
stjo2809 0:e43a0fe24907 215 }
stjo2809 0:e43a0fe24907 216