Library for MAX7300 GPIO Expander
max7300.cpp@0:350a850a7191, 2015-07-15 (annotated)
- Committer:
- j3
- Date:
- Wed Jul 15 15:10:04 2015 +0000
- Revision:
- 0:350a850a7191
- Child:
- 1:e1ee2549a047
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:350a850a7191 | 1 | /******************************************************************//** |
j3 | 0:350a850a7191 | 2 | * @file max7300.h |
j3 | 0:350a850a7191 | 3 | * |
j3 | 0:350a850a7191 | 4 | * @author Justin Jordan |
j3 | 0:350a850a7191 | 5 | * |
j3 | 0:350a850a7191 | 6 | * @version 0.0 |
j3 | 0:350a850a7191 | 7 | * |
j3 | 0:350a850a7191 | 8 | * Started: 14JUL15 |
j3 | 0:350a850a7191 | 9 | * |
j3 | 0:350a850a7191 | 10 | * Updated: |
j3 | 0:350a850a7191 | 11 | * |
j3 | 0:350a850a7191 | 12 | * @brief Source file for Max7300 class |
j3 | 0:350a850a7191 | 13 | *********************************************************************** |
j3 | 0:350a850a7191 | 14 | * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:350a850a7191 | 15 | * |
j3 | 0:350a850a7191 | 16 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:350a850a7191 | 17 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:350a850a7191 | 18 | * to deal in the Software without restriction, including without limitation |
j3 | 0:350a850a7191 | 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:350a850a7191 | 20 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:350a850a7191 | 21 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:350a850a7191 | 22 | * |
j3 | 0:350a850a7191 | 23 | * The above copyright notice and this permission notice shall be included |
j3 | 0:350a850a7191 | 24 | * in all copies or substantial portions of the Software. |
j3 | 0:350a850a7191 | 25 | * |
j3 | 0:350a850a7191 | 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:350a850a7191 | 27 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:350a850a7191 | 28 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:350a850a7191 | 29 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:350a850a7191 | 30 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:350a850a7191 | 31 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:350a850a7191 | 32 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:350a850a7191 | 33 | * |
j3 | 0:350a850a7191 | 34 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:350a850a7191 | 35 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:350a850a7191 | 36 | * Products, Inc. Branding Policy. |
j3 | 0:350a850a7191 | 37 | * |
j3 | 0:350a850a7191 | 38 | * The mere transfer of this software does not imply any licenses |
j3 | 0:350a850a7191 | 39 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:350a850a7191 | 40 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:350a850a7191 | 41 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:350a850a7191 | 42 | * ownership rights. |
j3 | 0:350a850a7191 | 43 | **********************************************************************/ |
j3 | 0:350a850a7191 | 44 | |
j3 | 0:350a850a7191 | 45 | |
j3 | 0:350a850a7191 | 46 | #include "max7300.h" |
j3 | 0:350a850a7191 | 47 | |
j3 | 0:350a850a7191 | 48 | |
j3 | 0:350a850a7191 | 49 | //********************************************************************* |
j3 | 0:350a850a7191 | 50 | Max7300::Max7300(I2C *i2c_bus, max7300_i2c_adrs_t i2c_adrs): _p_i2c(i2c_bus) |
j3 | 0:350a850a7191 | 51 | { |
j3 | 0:350a850a7191 | 52 | _i2c_owner = false; |
j3 | 0:350a850a7191 | 53 | |
j3 | 0:350a850a7191 | 54 | _r_adrs = ((i2c_adrs << 1) | 1); |
j3 | 0:350a850a7191 | 55 | _w_adrs = (i2c_adrs << 1); |
j3 | 0:350a850a7191 | 56 | } |
j3 | 0:350a850a7191 | 57 | |
j3 | 0:350a850a7191 | 58 | |
j3 | 0:350a850a7191 | 59 | //********************************************************************* |
j3 | 0:350a850a7191 | 60 | Max7300::Max7300(PinName sda, PinName scl, max7300_i2c_adrs_t i2c_adrs) |
j3 | 0:350a850a7191 | 61 | { |
j3 | 0:350a850a7191 | 62 | _p_i2c = new I2C(sda, scl); |
j3 | 0:350a850a7191 | 63 | _i2c_owner = true; |
j3 | 0:350a850a7191 | 64 | |
j3 | 0:350a850a7191 | 65 | _r_adrs = ((i2c_adrs << 1) | 1); |
j3 | 0:350a850a7191 | 66 | _w_adrs = (i2c_adrs << 1); |
j3 | 0:350a850a7191 | 67 | } |
j3 | 0:350a850a7191 | 68 | |
j3 | 0:350a850a7191 | 69 | |
j3 | 0:350a850a7191 | 70 | //********************************************************************* |
j3 | 0:350a850a7191 | 71 | Max7300::~Max7300() |
j3 | 0:350a850a7191 | 72 | { |
j3 | 0:350a850a7191 | 73 | if(_i2c_owner) |
j3 | 0:350a850a7191 | 74 | { |
j3 | 0:350a850a7191 | 75 | delete _p_i2c; |
j3 | 0:350a850a7191 | 76 | } |
j3 | 0:350a850a7191 | 77 | } |
j3 | 0:350a850a7191 | 78 | |
j3 | 0:350a850a7191 | 79 | |
j3 | 0:350a850a7191 | 80 | //********************************************************************* |
j3 | 0:350a850a7191 | 81 | int16_t Max7300::enable_ports(void) |
j3 | 0:350a850a7191 | 82 | { |
j3 | 0:350a850a7191 | 83 | int16_t result = -1; |
j3 | 0:350a850a7191 | 84 | char data[] = {MAX7300_CONFIGURATION, 0}; |
j3 | 0:350a850a7191 | 85 | |
j3 | 0:350a850a7191 | 86 | //set internal register pointer to configuration register |
j3 | 0:350a850a7191 | 87 | result = _p_i2c->write(_w_adrs, data, 1, true); |
j3 | 0:350a850a7191 | 88 | if(!result) |
j3 | 0:350a850a7191 | 89 | { |
j3 | 0:350a850a7191 | 90 | //get current configuration register |
j3 | 0:350a850a7191 | 91 | result = _p_i2c->read(_w_adrs, (data +1 ), 1, true); |
j3 | 0:350a850a7191 | 92 | |
j3 | 0:350a850a7191 | 93 | if(!result) |
j3 | 0:350a850a7191 | 94 | { |
j3 | 0:350a850a7191 | 95 | //set 'S' bit |
j3 | 0:350a850a7191 | 96 | data[1] |= 0x01; |
j3 | 0:350a850a7191 | 97 | |
j3 | 0:350a850a7191 | 98 | //write back to device |
j3 | 0:350a850a7191 | 99 | result = result = _p_i2c->write(_w_adrs, data, 2, false); |
j3 | 0:350a850a7191 | 100 | } |
j3 | 0:350a850a7191 | 101 | } |
j3 | 0:350a850a7191 | 102 | |
j3 | 0:350a850a7191 | 103 | return result; |
j3 | 0:350a850a7191 | 104 | } |
j3 | 0:350a850a7191 | 105 | |
j3 | 0:350a850a7191 | 106 | |
j3 | 0:350a850a7191 | 107 | //********************************************************************* |
j3 | 0:350a850a7191 | 108 | int16_t Max7300::disable_ports(void) |
j3 | 0:350a850a7191 | 109 | { |
j3 | 0:350a850a7191 | 110 | int16_t result = -1; |
j3 | 0:350a850a7191 | 111 | char data[] = {MAX7300_CONFIGURATION, 0}; |
j3 | 0:350a850a7191 | 112 | |
j3 | 0:350a850a7191 | 113 | //set internal register pointer to configuration register |
j3 | 0:350a850a7191 | 114 | result = _p_i2c->write(_w_adrs, data, 1, true); |
j3 | 0:350a850a7191 | 115 | if(!result) |
j3 | 0:350a850a7191 | 116 | { |
j3 | 0:350a850a7191 | 117 | //get current configuration register |
j3 | 0:350a850a7191 | 118 | result = _p_i2c->read(_w_adrs, (data +1 ), 1, true); |
j3 | 0:350a850a7191 | 119 | |
j3 | 0:350a850a7191 | 120 | if(!result) |
j3 | 0:350a850a7191 | 121 | { |
j3 | 0:350a850a7191 | 122 | //clear 'S' bit |
j3 | 0:350a850a7191 | 123 | data[1] &= ~0x01; |
j3 | 0:350a850a7191 | 124 | |
j3 | 0:350a850a7191 | 125 | //write back to device |
j3 | 0:350a850a7191 | 126 | result = result = _p_i2c->write(_w_adrs, data, 2, false); |
j3 | 0:350a850a7191 | 127 | } |
j3 | 0:350a850a7191 | 128 | } |
j3 | 0:350a850a7191 | 129 | |
j3 | 0:350a850a7191 | 130 | return result; |
j3 | 0:350a850a7191 | 131 | } |
j3 | 0:350a850a7191 | 132 | |
j3 | 0:350a850a7191 | 133 | |
j3 | 0:350a850a7191 | 134 | //********************************************************************* |
j3 | 0:350a850a7191 | 135 | int16_t Max7300::config_port(max7300_port_number_t port_num, max7300_port_type_t port_type) |
j3 | 0:350a850a7191 | 136 | { |
j3 | 0:350a850a7191 | 137 | int16_t result = -1; |
j3 | 0:350a850a7191 | 138 | char data[2]; |
j3 | 0:350a850a7191 | 139 | |
j3 | 0:350a850a7191 | 140 | //get address of port configuration register |
j3 | 0:350a850a7191 | 141 | data[0] = ((port_num/4) + 8); |
j3 | 0:350a850a7191 | 142 | |
j3 | 0:350a850a7191 | 143 | //get port config bits offset in that register |
j3 | 0:350a850a7191 | 144 | uint8_t offset = (port_num % 4); |
j3 | 0:350a850a7191 | 145 | |
j3 | 0:350a850a7191 | 146 | //set internal register pointer to port configuration register |
j3 | 0:350a850a7191 | 147 | result = _p_i2c->write(_w_adrs, data, 1, true); |
j3 | 0:350a850a7191 | 148 | if(!result) |
j3 | 0:350a850a7191 | 149 | { |
j3 | 0:350a850a7191 | 150 | //get current port configuration register |
j3 | 0:350a850a7191 | 151 | result = _p_i2c->read(_w_adrs, (data +1 ), 1, true); |
j3 | 0:350a850a7191 | 152 | |
j3 | 0:350a850a7191 | 153 | if(!result) |
j3 | 0:350a850a7191 | 154 | { |
j3 | 0:350a850a7191 | 155 | //clear old port configuration |
j3 | 0:350a850a7191 | 156 | data[1] &= ~(0x03 << (offset*2)); |
j3 | 0:350a850a7191 | 157 | //set port configuration bits |
j3 | 0:350a850a7191 | 158 | data[1] |= ((port_type & 0x03) << (offset*2)); |
j3 | 0:350a850a7191 | 159 | |
j3 | 0:350a850a7191 | 160 | //write back to device |
j3 | 0:350a850a7191 | 161 | result = result = _p_i2c->write(_w_adrs, data, 2, false); |
j3 | 0:350a850a7191 | 162 | } |
j3 | 0:350a850a7191 | 163 | } |
j3 | 0:350a850a7191 | 164 | |
j3 | 0:350a850a7191 | 165 | return result; |
j3 | 0:350a850a7191 | 166 | } |
j3 | 0:350a850a7191 | 167 | |
j3 | 0:350a850a7191 | 168 | |
j3 | 0:350a850a7191 | 169 | //********************************************************************* |
j3 | 0:350a850a7191 | 170 | int16_t Max7300::config_ports(max7300_registers_t reg, uint8_t data) |
j3 | 0:350a850a7191 | 171 | { |
j3 | 0:350a850a7191 | 172 | int16_t result = -1; |
j3 | 0:350a850a7191 | 173 | char local_data[] = {reg, data}; |
j3 | 0:350a850a7191 | 174 | |
j3 | 0:350a850a7191 | 175 | //no need for read, modify, write. |
j3 | 0:350a850a7191 | 176 | //Fx is intended to write whole register |
j3 | 0:350a850a7191 | 177 | result = _p_i2c->write(_w_adrs, local_data, 2, false); |
j3 | 0:350a850a7191 | 178 | |
j3 | 0:350a850a7191 | 179 | return result; |
j3 | 0:350a850a7191 | 180 | } |
j3 | 0:350a850a7191 | 181 | |
j3 | 0:350a850a7191 | 182 | |
j3 | 0:350a850a7191 | 183 | //********************************************************************* |
j3 | 0:350a850a7191 | 184 | int16_t Max7300::read_port(max7300_port_number_t port_num) |
j3 | 0:350a850a7191 | 185 | { |
j3 | 0:350a850a7191 | 186 | int16_t result = -1; |
j3 | 0:350a850a7191 | 187 | char data[2]; |
j3 | 0:350a850a7191 | 188 | |
j3 | 0:350a850a7191 | 189 | data[0] = (port_num + 0x20); |
j3 | 0:350a850a7191 | 190 | |
j3 | 0:350a850a7191 | 191 | //set internal register pointer to port data register |
j3 | 0:350a850a7191 | 192 | result = _p_i2c->write(_w_adrs, data, 1, true); |
j3 | 0:350a850a7191 | 193 | if(!result) |
j3 | 0:350a850a7191 | 194 | { |
j3 | 0:350a850a7191 | 195 | //get port data |
j3 | 0:350a850a7191 | 196 | result = _p_i2c->read(_w_adrs, (data +1 ), 1, false); |
j3 | 0:350a850a7191 | 197 | if(!result) |
j3 | 0:350a850a7191 | 198 | { |
j3 | 0:350a850a7191 | 199 | result = data[1]; |
j3 | 0:350a850a7191 | 200 | } |
j3 | 0:350a850a7191 | 201 | else |
j3 | 0:350a850a7191 | 202 | { |
j3 | 0:350a850a7191 | 203 | result = -1; |
j3 | 0:350a850a7191 | 204 | } |
j3 | 0:350a850a7191 | 205 | } |
j3 | 0:350a850a7191 | 206 | |
j3 | 0:350a850a7191 | 207 | return result; |
j3 | 0:350a850a7191 | 208 | } |
j3 | 0:350a850a7191 | 209 | |
j3 | 0:350a850a7191 | 210 | |
j3 | 0:350a850a7191 | 211 | //********************************************************************* |
j3 | 0:350a850a7191 | 212 | int16_t Max7300::write_port(max7300_port_number_t port_num, uint8_t data) |
j3 | 0:350a850a7191 | 213 | { |
j3 | 0:350a850a7191 | 214 | int16_t result = -1; |
j3 | 0:350a850a7191 | 215 | char local_data[] = {(port_num + 0x20), data}; |
j3 | 0:350a850a7191 | 216 | |
j3 | 0:350a850a7191 | 217 | //no need for read, modify, write. |
j3 | 0:350a850a7191 | 218 | //Fx is intended to write whole register |
j3 | 0:350a850a7191 | 219 | result = _p_i2c->write(_w_adrs, local_data, 2, false); |
j3 | 0:350a850a7191 | 220 | |
j3 | 0:350a850a7191 | 221 | return result; |
j3 | 0:350a850a7191 | 222 | } |
j3 | 0:350a850a7191 | 223 | |
j3 | 0:350a850a7191 | 224 | |
j3 | 0:350a850a7191 | 225 | //********************************************************************* |
j3 | 0:350a850a7191 | 226 | int16_t Max7300::read_eight_ports(max7300_registers_t reg) |
j3 | 0:350a850a7191 | 227 | { |
j3 | 0:350a850a7191 | 228 | int16_t result = -1; |
j3 | 0:350a850a7191 | 229 | char data[2]; |
j3 | 0:350a850a7191 | 230 | |
j3 | 0:350a850a7191 | 231 | data[0] = reg; |
j3 | 0:350a850a7191 | 232 | |
j3 | 0:350a850a7191 | 233 | //set internal register pointer to port data register |
j3 | 0:350a850a7191 | 234 | result = _p_i2c->write(_w_adrs, data, 1, true); |
j3 | 0:350a850a7191 | 235 | if(!result) |
j3 | 0:350a850a7191 | 236 | { |
j3 | 0:350a850a7191 | 237 | //get port data |
j3 | 0:350a850a7191 | 238 | result = _p_i2c->read(_w_adrs, (data +1 ), 1, false); |
j3 | 0:350a850a7191 | 239 | if(!result) |
j3 | 0:350a850a7191 | 240 | { |
j3 | 0:350a850a7191 | 241 | result = data[1]; |
j3 | 0:350a850a7191 | 242 | } |
j3 | 0:350a850a7191 | 243 | else |
j3 | 0:350a850a7191 | 244 | { |
j3 | 0:350a850a7191 | 245 | result = -1; |
j3 | 0:350a850a7191 | 246 | } |
j3 | 0:350a850a7191 | 247 | } |
j3 | 0:350a850a7191 | 248 | |
j3 | 0:350a850a7191 | 249 | return result; |
j3 | 0:350a850a7191 | 250 | } |
j3 | 0:350a850a7191 | 251 | |
j3 | 0:350a850a7191 | 252 | |
j3 | 0:350a850a7191 | 253 | //********************************************************************* |
j3 | 0:350a850a7191 | 254 | int16_t Max7300::write_eight_ports(max7300_registers_t reg, uint8_t data) |
j3 | 0:350a850a7191 | 255 | { |
j3 | 0:350a850a7191 | 256 | int16_t result = -1; |
j3 | 0:350a850a7191 | 257 | char local_data[] = {reg, data}; |
j3 | 0:350a850a7191 | 258 | |
j3 | 0:350a850a7191 | 259 | //no need for read, modify, write. |
j3 | 0:350a850a7191 | 260 | //Fx is intended to write whole register |
j3 | 0:350a850a7191 | 261 | result = _p_i2c->write(_w_adrs, local_data, 2, false); |
j3 | 0:350a850a7191 | 262 | |
j3 | 0:350a850a7191 | 263 | return result; |
j3 | 0:350a850a7191 | 264 | } |
j3 | 0:350a850a7191 | 265 | |
j3 | 0:350a850a7191 | 266 | |
j3 | 0:350a850a7191 | 267 |