use mbed os

Dependents:   Seeed_Grove_I2C_Touch_Example

Fork of MPR121 by Sam Grove

Committer:
Nathan Yonkee
Date:
Sun Jul 02 11:10:26 2017 -0600
Revision:
11:ad26c0810f02
Parent:
10:fb2d2454fea4
Parent:
9:8cb5ce483be3
Child:
12:22d36da8df91
merge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nathan Yonkee 11:ad26c0810f02 1 <<<<<<< working copy
sam_grove 0:42add775212a 2 /**
sam_grove 0:42add775212a 3 * @file MPR121.cpp
sam_grove 0:42add775212a 4 * @brief Device driver - MPR121 capactiive touch IC
sam_grove 0:42add775212a 5 * @author sam grove
sam_grove 0:42add775212a 6 * @version 1.0
sam_grove 0:42add775212a 7 * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf
sam_grove 0:42add775212a 8 *
sam_grove 0:42add775212a 9 * Copyright (c) 2013
sam_grove 0:42add775212a 10 *
sam_grove 0:42add775212a 11 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:42add775212a 12 * you may not use this file except in compliance with the License.
sam_grove 0:42add775212a 13 * You may obtain a copy of the License at
sam_grove 0:42add775212a 14 *
sam_grove 0:42add775212a 15 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:42add775212a 16 *
sam_grove 0:42add775212a 17 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:42add775212a 18 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:42add775212a 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:42add775212a 20 * See the License for the specific language governing permissions and
sam_grove 0:42add775212a 21 * limitations under the License.
sam_grove 0:42add775212a 22 */
sam_grove 0:42add775212a 23
sam_grove 0:42add775212a 24 #include "MPR121.h"
sam_grove 3:828260f21de6 25 #include "mbed_debug.h"
sam_grove 3:828260f21de6 26
sam_grove 3:828260f21de6 27 #define DEBUG 1
sam_grove 5:3934358ec2b7 28
sam_grove 0:42add775212a 29 MPR121::MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr)
sam_grove 0:42add775212a 30 {
sam_grove 0:42add775212a 31 _i2c = &i2c;
sam_grove 0:42add775212a 32 _irq = &pin;
sam_grove 0:42add775212a 33 _i2c_addr = (i2c_addr << 1);
sam_grove 5:3934358ec2b7 34
sam_grove 5:3934358ec2b7 35 return;
sam_grove 5:3934358ec2b7 36 }
sam_grove 5:3934358ec2b7 37
Nathan Yonkee 10:fb2d2454fea4 38 MPR121::MPR121(I2C &i2c, MPR121_ADDR fake)
sam_grove 5:3934358ec2b7 39 {
sam_grove 5:3934358ec2b7 40 _i2c = &i2c;
sam_grove 5:3934358ec2b7 41 _irq = NULL;
Nathan Yonkee 10:fb2d2454fea4 42 _i2c_addr = (0x1b << 1);
Nathan Yonkee 10:fb2d2454fea4 43 char reg[1] = { 0x03 };
Nathan Yonkee 10:fb2d2454fea4 44 int result=0;
Nathan Yonkee 10:fb2d2454fea4 45 char data = 10;
Nathan Yonkee 10:fb2d2454fea4 46 result = _i2c->write(_i2c_addr, reg, 1, true);
Nathan Yonkee 10:fb2d2454fea4 47 if(result == 0 ) debug("result is 0");
Nathan Yonkee 10:fb2d2454fea4 48 result = _i2c->read(_i2c_addr, &data, 1);
Nathan Yonkee 10:fb2d2454fea4 49 if(result == 0 ) debug("result is 0");
sam_grove 5:3934358ec2b7 50
sam_grove 0:42add775212a 51 return;
sam_grove 0:42add775212a 52 }
sam_grove 0:42add775212a 53
sam_grove 0:42add775212a 54 void MPR121::init(void)
sam_grove 0:42add775212a 55 {
sam_grove 0:42add775212a 56 // set the i2c speed
Nathan Yonkee 10:fb2d2454fea4 57 /* _i2c->frequency(400000); */
sam_grove 0:42add775212a 58 // irq is open-collector and active-low
sam_grove 5:3934358ec2b7 59 if(_irq != NULL) {
sam_grove 5:3934358ec2b7 60 _irq->mode(PullUp);
sam_grove 5:3934358ec2b7 61 }
sam_grove 5:3934358ec2b7 62
sam_grove 0:42add775212a 63 // setup and registers - start with POR values (must be in stop mode)
sam_grove 0:42add775212a 64 MPR121::writeRegister(SRST, 0x63); //REG 0x80
sam_grove 5:3934358ec2b7 65
sam_grove 0:42add775212a 66 // Baseline Filtering Control Register (changes response sensitivity)
sam_grove 0:42add775212a 67 // http://cache.freescale.com/files/sensors/doc/app_note/AN3891.pdf
sam_grove 0:42add775212a 68 MPR121::writeRegister(MHDR, 0x1); //REG 0x2B
sam_grove 0:42add775212a 69 MPR121::writeRegister(NHDR, 0x1); //REG 0x2C
sam_grove 0:42add775212a 70 MPR121::writeRegister(NCLR, 0x0); //REG 0x2D
sam_grove 0:42add775212a 71 MPR121::writeRegister(FDLR, 0x0); //REG 0x2E
sam_grove 0:42add775212a 72 MPR121::writeRegister(MHDF, 0x1); //REG 0x2F
sam_grove 0:42add775212a 73 MPR121::writeRegister(NHDF, 0x1); //REG 0x30
sam_grove 0:42add775212a 74 MPR121::writeRegister(NCLF, 0xFF); //REG 0x31
sam_grove 0:42add775212a 75 MPR121::writeRegister(FDLF, 0x2); //REG 0x32
sam_grove 5:3934358ec2b7 76
sam_grove 0:42add775212a 77 // Touch / Release Threshold
sam_grove 0:42add775212a 78 // cache.freescale.com/files/sensors/doc/app_note/AN3892.pdf
sam_grove 5:3934358ec2b7 79 for(int i=0; i<(12*2); i+=2) { // touch
sam_grove 3:828260f21de6 80 MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); //REG 0x41...0x58 odd
sam_grove 0:42add775212a 81 }
sam_grove 5:3934358ec2b7 82 for(int i=0; i<(12*2); i+=2) { // release
sam_grove 3:828260f21de6 83 MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); //REG 0x41...0x58 even
sam_grove 0:42add775212a 84 }
sam_grove 5:3934358ec2b7 85
sam_grove 0:42add775212a 86 // Debounce Register DR=b6...4, DT=b2...0
sam_grove 0:42add775212a 87 MPR121::writeRegister(DT_DR, 0x11); //REG 0x5B
sam_grove 5:3934358ec2b7 88
sam_grove 0:42add775212a 89 // Filter and Global CDC CDT Configuration (sample time, charge current)
sam_grove 0:42add775212a 90 MPR121::writeRegister(CDC_CONFIG, 0x10); //REG 0x5C default 10
sam_grove 0:42add775212a 91 MPR121::writeRegister(CDT_CONFIG, 0x20); //REG 0x5D default 24
sam_grove 5:3934358ec2b7 92
sam_grove 0:42add775212a 93 // Auto-Configuration Registers
sam_grove 0:42add775212a 94 // http://cache.freescale.com/files/sensors/doc/app_note/AN3889.pdf
sam_grove 0:42add775212a 95 MPR121::writeRegister(AUTO_CFG0, 0x33); // REG 0x7B
sam_grove 0:42add775212a 96 MPR121::writeRegister(AUTO_CFG1, 0x07); // REG 0x7C
sam_grove 0:42add775212a 97 MPR121::writeRegister(USL, 0xc9); // REG 0x7D((3.3-.07)/3.3) * 256
sam_grove 0:42add775212a 98 MPR121::writeRegister(LSL, 0x83); // REG 0x7E((3.3-.07)/3.3) * 256 * 0.65f
sam_grove 0:42add775212a 99 MPR121::writeRegister(TL, 0xb5); // REG 0x7F((3.3-.07)/3.3) * 256 * 0.9f
sam_grove 0:42add775212a 100 // 255 > USL > TL > LSL > 0
sam_grove 5:3934358ec2b7 101
sam_grove 0:42add775212a 102 // Electrode Configuration Register - enable all 12 and start
sam_grove 0:42add775212a 103 MPR121::writeRegister(ECR, 0x8f);
sam_grove 5:3934358ec2b7 104
sam_grove 0:42add775212a 105 return;
sam_grove 0:42add775212a 106 }
sam_grove 0:42add775212a 107
sam_grove 0:42add775212a 108 void MPR121::enable(void)
sam_grove 0:42add775212a 109 {
sam_grove 0:42add775212a 110 _button = 0;
sam_grove 0:42add775212a 111 _button_has_changed = 0;
sam_grove 1:cee45334b36a 112 // enable the 12 electrodes - allow disable to put device into
sam_grove 1:cee45334b36a 113 // lower current consumption mode
sam_grove 1:cee45334b36a 114 MPR121::writeRegister(ECR, 0x8f);
sam_grove 1:cee45334b36a 115 // and attach the interrupt handler
sam_grove 5:3934358ec2b7 116 if(_irq != NULL) {
sam_grove 5:3934358ec2b7 117 _irq->fall(this, &MPR121::handler);
sam_grove 5:3934358ec2b7 118 }
sam_grove 5:3934358ec2b7 119
sam_grove 0:42add775212a 120 return;
sam_grove 0:42add775212a 121 }
sam_grove 0:42add775212a 122
sam_grove 0:42add775212a 123 void MPR121::disable(void)
sam_grove 0:42add775212a 124 {
sam_grove 1:cee45334b36a 125 // detach the interrupt handler
sam_grove 0:42add775212a 126 _irq->fall(NULL);
sam_grove 0:42add775212a 127 _button = 0;
sam_grove 0:42add775212a 128 _button_has_changed = 0;
sam_grove 1:cee45334b36a 129 // put the device in low current consumption mode - dont re-init registers
sam_grove 1:cee45334b36a 130 MPR121::writeRegister(ECR, 0x0);
sam_grove 1:cee45334b36a 131 MPR121::writeRegister(AUTO_CFG0, 0x0); // REG 0x7B
sam_grove 1:cee45334b36a 132 MPR121::writeRegister(AUTO_CFG1, 0x0); // REG 0x7C
sam_grove 5:3934358ec2b7 133
sam_grove 0:42add775212a 134 return;
sam_grove 0:42add775212a 135 }
sam_grove 0:42add775212a 136
sam_grove 0:42add775212a 137 uint32_t MPR121::isPressed(void)
sam_grove 0:42add775212a 138 {
Nathan Yonkee 10:fb2d2454fea4 139
Nathan Yonkee 10:fb2d2454fea4 140 char reg[1] = { 0x03 };
Nathan Yonkee 10:fb2d2454fea4 141 int result=0;
Nathan Yonkee 10:fb2d2454fea4 142 char data = -1;
Nathan Yonkee 10:fb2d2454fea4 143 result = _i2c->write(_i2c_addr, reg, 1, true);
Nathan Yonkee 10:fb2d2454fea4 144 if(result == 0 ) debug("result is 0");
Nathan Yonkee 10:fb2d2454fea4 145 result = _i2c->read(_i2c_addr, &data, 1);
Nathan Yonkee 10:fb2d2454fea4 146 if(result == 0 ) debug("result is 0");
Nathan Yonkee 10:fb2d2454fea4 147 if(data > 0) data = true;
Nathan Yonkee 10:fb2d2454fea4 148 return data;
Nathan Yonkee 10:fb2d2454fea4 149 /* return _button_has_changed; */
sam_grove 0:42add775212a 150 }
sam_grove 0:42add775212a 151
sam_grove 0:42add775212a 152 uint16_t MPR121::buttonPressed(void)
sam_grove 0:42add775212a 153 {
Nathan Yonkee 10:fb2d2454fea4 154 char reg[1] = { 0x03 };
Nathan Yonkee 10:fb2d2454fea4 155 int result=0;
Nathan Yonkee 10:fb2d2454fea4 156 char data = -1;
Nathan Yonkee 10:fb2d2454fea4 157 result = _i2c->write(_i2c_addr, reg, 1, true);
Nathan Yonkee 10:fb2d2454fea4 158 if(result == 0 ) debug("result is 0");
Nathan Yonkee 10:fb2d2454fea4 159 result = _i2c->read(_i2c_addr, &data, 1);
Nathan Yonkee 10:fb2d2454fea4 160 if(result == 0 ) debug("result is 0");
Nathan Yonkee 10:fb2d2454fea4 161 if(data == 4) data = 3;
Nathan Yonkee 10:fb2d2454fea4 162 if(data == 2) data = 2;
Nathan Yonkee 10:fb2d2454fea4 163 if(data == 1) data = 1;
Nathan Yonkee 10:fb2d2454fea4 164 return data;
sam_grove 0:42add775212a 165 }
sam_grove 0:42add775212a 166
sam_grove 3:828260f21de6 167 void MPR121::registerDump(Serial &obj) const
sam_grove 0:42add775212a 168 {
sam_grove 0:42add775212a 169 uint8_t reg_val = 0;
sam_grove 5:3934358ec2b7 170
sam_grove 5:3934358ec2b7 171 for(int i=0; i<0x80; i++) {
sam_grove 3:828260f21de6 172 reg_val = MPR121::readRegister(static_cast<MPR121_REGISTER>(i));
sam_grove 3:828260f21de6 173 obj.printf("Reg 0x%02x: 0x%02x \n", i, reg_val);
sam_grove 0:42add775212a 174 }
sam_grove 5:3934358ec2b7 175
sam_grove 5:3934358ec2b7 176 return;
sam_grove 5:3934358ec2b7 177 }
sam_grove 5:3934358ec2b7 178
sam_grove 5:3934358ec2b7 179 void MPR121::registerDump(void) const
sam_grove 5:3934358ec2b7 180 {
sam_grove 5:3934358ec2b7 181 uint8_t reg_val = 0;
sam_grove 5:3934358ec2b7 182
sam_grove 5:3934358ec2b7 183 for(int i=0; i<0x80; i++) {
sam_grove 5:3934358ec2b7 184 reg_val = MPR121::readRegister(static_cast<MPR121_REGISTER>(i));
sam_grove 5:3934358ec2b7 185 printf("Reg 0x%02x: 0x%02x \n", i, reg_val);
sam_grove 5:3934358ec2b7 186 }
sam_grove 5:3934358ec2b7 187
sam_grove 0:42add775212a 188 return;
sam_grove 0:42add775212a 189 }
sam_grove 0:42add775212a 190
sam_grove 0:42add775212a 191 void MPR121::handler(void)
sam_grove 0:42add775212a 192 {
sam_grove 0:42add775212a 193 uint16_t reg_val = 0, oor_val = 0;
sam_grove 0:42add775212a 194 // read register 0 and 1
sam_grove 0:42add775212a 195 reg_val = MPR121::readRegister(ELE0_7_STAT);
sam_grove 0:42add775212a 196 reg_val |= MPR121::readRegister(ELE8_11_STAT) << 8;
sam_grove 0:42add775212a 197 // 2 and 3
sam_grove 0:42add775212a 198 oor_val = MPR121::readRegister(ELE0_7_OOR_STAT);
sam_grove 0:42add775212a 199 oor_val |= MPR121::readRegister(ELE8_11_OOR_STAT) << 8;
sam_grove 5:3934358ec2b7 200
sam_grove 3:828260f21de6 201 // debugging stuff and errors - if OOR fails someone was touching the pad during auto-config
sam_grove 3:828260f21de6 202 // Just reboot until they're not doing this
sam_grove 5:3934358ec2b7 203 if((0 != oor_val) && DEBUG) {
sam_grove 3:828260f21de6 204 debug("MPR121 OOR failure - 0x%04x\n", oor_val);
sam_grove 3:828260f21de6 205 wait(0.1f);
sam_grove 3:828260f21de6 206 NVIC_SystemReset();
sam_grove 0:42add775212a 207 }
sam_grove 5:3934358ec2b7 208
sam_grove 0:42add775212a 209 _button = reg_val;
sam_grove 0:42add775212a 210 _button_has_changed = 1;
sam_grove 5:3934358ec2b7 211
sam_grove 0:42add775212a 212 return;
sam_grove 0:42add775212a 213 }
sam_grove 0:42add775212a 214
sam_grove 3:828260f21de6 215 void MPR121::writeRegister(MPR121_REGISTER const reg, uint8_t const data) const
sam_grove 0:42add775212a 216 {
sam_grove 0:42add775212a 217 char buf[2] = {reg, data};
sam_grove 0:42add775212a 218 uint8_t result = 0;
sam_grove 5:3934358ec2b7 219
sam_grove 0:42add775212a 220 result = _i2c->write(_i2c_addr, buf, 2);
sam_grove 3:828260f21de6 221 if(result && DEBUG)
sam_grove 0:42add775212a 222 {
sam_grove 4:c1bc00c5e8e5 223 debug("I2C write failed\n");
sam_grove 0:42add775212a 224 }
sam_grove 5:3934358ec2b7 225
sam_grove 0:42add775212a 226 return;
sam_grove 0:42add775212a 227 }
sam_grove 0:42add775212a 228
sam_grove 3:828260f21de6 229 uint8_t MPR121::readRegister(MPR121_REGISTER const reg) const
sam_grove 0:42add775212a 230 {
sam_grove 3:828260f21de6 231 char buf[1] = {reg}, data = 0;
sam_grove 3:828260f21de6 232 uint8_t result = 1;
sam_grove 5:3934358ec2b7 233
sam_grove 3:828260f21de6 234 result &= _i2c->write(_i2c_addr, buf, 1, true);
sam_grove 3:828260f21de6 235 result &= _i2c->read(_i2c_addr, &data, 1);
sam_grove 0:42add775212a 236
sam_grove 3:828260f21de6 237 if(result && DEBUG)
sam_grove 0:42add775212a 238 {
sam_grove 4:c1bc00c5e8e5 239 debug("I2C read failed\n");
sam_grove 0:42add775212a 240 }
sam_grove 5:3934358ec2b7 241
sam_grove 0:42add775212a 242 return data;
sam_grove 0:42add775212a 243 }
Nathan Yonkee 11:ad26c0810f02 244 =======
Nate Yonkee 9:8cb5ce483be3 245 /**
Nate Yonkee 9:8cb5ce483be3 246 * @file MPR121.cpp
Nate Yonkee 9:8cb5ce483be3 247 * @brief Device driver - MPR121 capactiive touch IC
Nate Yonkee 9:8cb5ce483be3 248 * @author sam grove
Nate Yonkee 9:8cb5ce483be3 249 * @version 1.0
Nate Yonkee 9:8cb5ce483be3 250 * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf
Nate Yonkee 9:8cb5ce483be3 251 *
Nate Yonkee 9:8cb5ce483be3 252 * Copyright (c) 2013
Nate Yonkee 9:8cb5ce483be3 253 *
Nate Yonkee 9:8cb5ce483be3 254 * Licensed under the Apache License, Version 2.0 (the "License");
Nate Yonkee 9:8cb5ce483be3 255 * you may not use this file except in compliance with the License.
Nate Yonkee 9:8cb5ce483be3 256 * You may obtain a copy of the License at
Nate Yonkee 9:8cb5ce483be3 257 *
Nate Yonkee 9:8cb5ce483be3 258 * http://www.apache.org/licenses/LICENSE-2.0
Nate Yonkee 9:8cb5ce483be3 259 *
Nate Yonkee 9:8cb5ce483be3 260 * Unless required by applicable law or agreed to in writing, software
Nate Yonkee 9:8cb5ce483be3 261 * distributed under the License is distributed on an "AS IS" BASIS,
Nate Yonkee 9:8cb5ce483be3 262 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Nate Yonkee 9:8cb5ce483be3 263 * See the License for the specific language governing permissions and
Nate Yonkee 9:8cb5ce483be3 264 * limitations under the License.
Nate Yonkee 9:8cb5ce483be3 265 */
Nate Yonkee 9:8cb5ce483be3 266
Nate Yonkee 9:8cb5ce483be3 267 #include "MPR121.h"
Nate Yonkee 9:8cb5ce483be3 268 #include "mbed_debug.h"
Nate Yonkee 9:8cb5ce483be3 269
Nate Yonkee 9:8cb5ce483be3 270 #define DEBUG 1
Nate Yonkee 9:8cb5ce483be3 271
Nate Yonkee 9:8cb5ce483be3 272 MPR121::MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr)
Nate Yonkee 9:8cb5ce483be3 273 {
Nate Yonkee 9:8cb5ce483be3 274 _i2c = &i2c;
Nate Yonkee 9:8cb5ce483be3 275 _irq = &pin;
Nate Yonkee 9:8cb5ce483be3 276 _i2c_addr = (i2c_addr << 1);
Nate Yonkee 9:8cb5ce483be3 277
Nate Yonkee 9:8cb5ce483be3 278 return;
Nate Yonkee 9:8cb5ce483be3 279 }
Nate Yonkee 9:8cb5ce483be3 280
Nate Yonkee 9:8cb5ce483be3 281 MPR121::MPR121(I2C &i2c, MPR121_ADDR i2c_addr)
Nate Yonkee 9:8cb5ce483be3 282 {
Nate Yonkee 9:8cb5ce483be3 283 _i2c = &i2c;
Nate Yonkee 9:8cb5ce483be3 284 _irq = NULL;
Nate Yonkee 9:8cb5ce483be3 285 _i2c_addr = (i2c_addr << 1);
Nate Yonkee 9:8cb5ce483be3 286
Nate Yonkee 9:8cb5ce483be3 287 return;
Nate Yonkee 9:8cb5ce483be3 288 }
Nate Yonkee 9:8cb5ce483be3 289
Nate Yonkee 9:8cb5ce483be3 290 void MPR121::init(void)
Nate Yonkee 9:8cb5ce483be3 291 {
Nate Yonkee 9:8cb5ce483be3 292 // set the i2c speed
Nate Yonkee 9:8cb5ce483be3 293 _i2c->frequency(400000);
Nate Yonkee 9:8cb5ce483be3 294 // irq is open-collector and active-low
Nate Yonkee 9:8cb5ce483be3 295 if(_irq != NULL) {
Nate Yonkee 9:8cb5ce483be3 296 _irq->mode(PullUp);
Nate Yonkee 9:8cb5ce483be3 297 }
Nate Yonkee 9:8cb5ce483be3 298
Nate Yonkee 9:8cb5ce483be3 299 // setup and registers - start with POR values (must be in stop mode)
Nate Yonkee 9:8cb5ce483be3 300 MPR121::writeRegister(SRST, 0x63); //REG 0x80
Nate Yonkee 9:8cb5ce483be3 301
Nate Yonkee 9:8cb5ce483be3 302 // Baseline Filtering Control Register (changes response sensitivity)
Nate Yonkee 9:8cb5ce483be3 303 // http://cache.freescale.com/files/sensors/doc/app_note/AN3891.pdf
Nate Yonkee 9:8cb5ce483be3 304 MPR121::writeRegister(MHDR, 0x1); //REG 0x2B
Nate Yonkee 9:8cb5ce483be3 305 MPR121::writeRegister(NHDR, 0x1); //REG 0x2C
Nate Yonkee 9:8cb5ce483be3 306 MPR121::writeRegister(NCLR, 0x0); //REG 0x2D
Nate Yonkee 9:8cb5ce483be3 307 MPR121::writeRegister(FDLR, 0x0); //REG 0x2E
Nate Yonkee 9:8cb5ce483be3 308 MPR121::writeRegister(MHDF, 0x1); //REG 0x2F
Nate Yonkee 9:8cb5ce483be3 309 MPR121::writeRegister(NHDF, 0x1); //REG 0x30
Nate Yonkee 9:8cb5ce483be3 310 MPR121::writeRegister(NCLF, 0xFF); //REG 0x31
Nate Yonkee 9:8cb5ce483be3 311 MPR121::writeRegister(FDLF, 0x2); //REG 0x32
Nate Yonkee 9:8cb5ce483be3 312
Nate Yonkee 9:8cb5ce483be3 313 // Touch / Release Threshold
Nate Yonkee 9:8cb5ce483be3 314 // cache.freescale.com/files/sensors/doc/app_note/AN3892.pdf
Nate Yonkee 9:8cb5ce483be3 315 for(int i=0; i<(12*2); i+=2) { // touch
Nate Yonkee 9:8cb5ce483be3 316 MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); //REG 0x41...0x58 odd
Nate Yonkee 9:8cb5ce483be3 317 }
Nate Yonkee 9:8cb5ce483be3 318 for(int i=0; i<(12*2); i+=2) { // release
Nate Yonkee 9:8cb5ce483be3 319 MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); //REG 0x41...0x58 even
Nate Yonkee 9:8cb5ce483be3 320 }
Nate Yonkee 9:8cb5ce483be3 321
Nate Yonkee 9:8cb5ce483be3 322 // Debounce Register DR=b6...4, DT=b2...0
Nate Yonkee 9:8cb5ce483be3 323 MPR121::writeRegister(DT_DR, 0x11); //REG 0x5B
Nate Yonkee 9:8cb5ce483be3 324
Nate Yonkee 9:8cb5ce483be3 325 // Filter and Global CDC CDT Configuration (sample time, charge current)
Nate Yonkee 9:8cb5ce483be3 326 MPR121::writeRegister(CDC_CONFIG, 0x10); //REG 0x5C default 10
Nate Yonkee 9:8cb5ce483be3 327 MPR121::writeRegister(CDT_CONFIG, 0x20); //REG 0x5D default 24
Nate Yonkee 9:8cb5ce483be3 328
Nate Yonkee 9:8cb5ce483be3 329 // Auto-Configuration Registers
Nate Yonkee 9:8cb5ce483be3 330 // http://cache.freescale.com/files/sensors/doc/app_note/AN3889.pdf
Nate Yonkee 9:8cb5ce483be3 331 MPR121::writeRegister(AUTO_CFG0, 0x33); // REG 0x7B
Nate Yonkee 9:8cb5ce483be3 332 MPR121::writeRegister(AUTO_CFG1, 0x07); // REG 0x7C
Nate Yonkee 9:8cb5ce483be3 333 MPR121::writeRegister(USL, 0xc9); // REG 0x7D((3.3-.07)/3.3) * 256
Nate Yonkee 9:8cb5ce483be3 334 MPR121::writeRegister(LSL, 0x83); // REG 0x7E((3.3-.07)/3.3) * 256 * 0.65f
Nate Yonkee 9:8cb5ce483be3 335 MPR121::writeRegister(TL, 0xb5); // REG 0x7F((3.3-.07)/3.3) * 256 * 0.9f
Nate Yonkee 9:8cb5ce483be3 336 // 255 > USL > TL > LSL > 0
Nate Yonkee 9:8cb5ce483be3 337
Nate Yonkee 9:8cb5ce483be3 338 // Electrode Configuration Register - enable all 12 and start
Nate Yonkee 9:8cb5ce483be3 339 MPR121::writeRegister(ECR, 0x8f);
Nate Yonkee 9:8cb5ce483be3 340
Nate Yonkee 9:8cb5ce483be3 341 return;
Nate Yonkee 9:8cb5ce483be3 342 }
Nate Yonkee 9:8cb5ce483be3 343
Nate Yonkee 9:8cb5ce483be3 344 void MPR121::enable(void)
Nate Yonkee 9:8cb5ce483be3 345 {
Nate Yonkee 9:8cb5ce483be3 346 _button = 0;
Nate Yonkee 9:8cb5ce483be3 347 _button_has_changed = 0;
Nate Yonkee 9:8cb5ce483be3 348 // enable the 12 electrodes - allow disable to put device into
Nate Yonkee 9:8cb5ce483be3 349 // lower current consumption mode
Nate Yonkee 9:8cb5ce483be3 350 MPR121::writeRegister(ECR, 0x8f);
Nate Yonkee 9:8cb5ce483be3 351 // and attach the interrupt handler
Nate Yonkee 9:8cb5ce483be3 352 if(_irq != NULL) {
Nate Yonkee 9:8cb5ce483be3 353 _irq->fall(this, &MPR121::handler);
Nate Yonkee 9:8cb5ce483be3 354 }
Nate Yonkee 9:8cb5ce483be3 355
Nate Yonkee 9:8cb5ce483be3 356 return;
Nate Yonkee 9:8cb5ce483be3 357 }
Nate Yonkee 9:8cb5ce483be3 358
Nate Yonkee 9:8cb5ce483be3 359 void MPR121::disable(void)
Nate Yonkee 9:8cb5ce483be3 360 {
Nate Yonkee 9:8cb5ce483be3 361 // detach the interrupt handler
Nate Yonkee 9:8cb5ce483be3 362 _irq->fall(NULL);
Nate Yonkee 9:8cb5ce483be3 363 _button = 0;
Nate Yonkee 9:8cb5ce483be3 364 _button_has_changed = 0;
Nate Yonkee 9:8cb5ce483be3 365 // put the device in low current consumption mode - dont re-init registers
Nate Yonkee 9:8cb5ce483be3 366 MPR121::writeRegister(ECR, 0x0);
Nate Yonkee 9:8cb5ce483be3 367 MPR121::writeRegister(AUTO_CFG0, 0x0); // REG 0x7B
Nate Yonkee 9:8cb5ce483be3 368 MPR121::writeRegister(AUTO_CFG1, 0x0); // REG 0x7C
Nate Yonkee 9:8cb5ce483be3 369
Nate Yonkee 9:8cb5ce483be3 370 return;
Nate Yonkee 9:8cb5ce483be3 371 }
Nate Yonkee 9:8cb5ce483be3 372
Nate Yonkee 9:8cb5ce483be3 373 uint32_t MPR121::isPressed(void)
Nate Yonkee 9:8cb5ce483be3 374 {
Nate Yonkee 9:8cb5ce483be3 375 return _button_has_changed;
Nate Yonkee 9:8cb5ce483be3 376 }
Nate Yonkee 9:8cb5ce483be3 377
Nate Yonkee 9:8cb5ce483be3 378 uint16_t MPR121::buttonPressed(void)
Nate Yonkee 9:8cb5ce483be3 379 {
Nate Yonkee 9:8cb5ce483be3 380 if(_irq == NULL) {
Nate Yonkee 9:8cb5ce483be3 381 handler();
Nate Yonkee 9:8cb5ce483be3 382 }
Nate Yonkee 9:8cb5ce483be3 383 _button_has_changed = 0;
Nate Yonkee 9:8cb5ce483be3 384 return _button;
Nate Yonkee 9:8cb5ce483be3 385 }
Nate Yonkee 9:8cb5ce483be3 386
Nate Yonkee 9:8cb5ce483be3 387 void MPR121::registerDump(Serial &obj) const
Nate Yonkee 9:8cb5ce483be3 388 {
Nate Yonkee 9:8cb5ce483be3 389 uint8_t reg_val = 0;
Nate Yonkee 9:8cb5ce483be3 390
Nate Yonkee 9:8cb5ce483be3 391 for(int i=0; i<0x80; i++) {
Nate Yonkee 9:8cb5ce483be3 392 reg_val = MPR121::readRegister(static_cast<MPR121_REGISTER>(i));
Nate Yonkee 9:8cb5ce483be3 393 obj.printf("Reg 0x%02x: 0x%02x \n", i, reg_val);
Nate Yonkee 9:8cb5ce483be3 394 }
Nate Yonkee 9:8cb5ce483be3 395
Nate Yonkee 9:8cb5ce483be3 396 return;
Nate Yonkee 9:8cb5ce483be3 397 }
Nate Yonkee 9:8cb5ce483be3 398
Nate Yonkee 9:8cb5ce483be3 399 void MPR121::registerDump(void) const
Nate Yonkee 9:8cb5ce483be3 400 {
Nate Yonkee 9:8cb5ce483be3 401 uint8_t reg_val = 0;
Nate Yonkee 9:8cb5ce483be3 402
Nate Yonkee 9:8cb5ce483be3 403 for(int i=0; i<0x80; i++) {
Nate Yonkee 9:8cb5ce483be3 404 reg_val = MPR121::readRegister(static_cast<MPR121_REGISTER>(i));
Nate Yonkee 9:8cb5ce483be3 405 printf("Reg 0x%02x: 0x%02x \n", i, reg_val);
Nate Yonkee 9:8cb5ce483be3 406 }
Nate Yonkee 9:8cb5ce483be3 407
Nate Yonkee 9:8cb5ce483be3 408 return;
Nate Yonkee 9:8cb5ce483be3 409 }
Nate Yonkee 9:8cb5ce483be3 410
Nate Yonkee 9:8cb5ce483be3 411 void MPR121::handler(void)
Nate Yonkee 9:8cb5ce483be3 412 {
Nate Yonkee 9:8cb5ce483be3 413 uint16_t reg_val = 0, oor_val = 0;
Nate Yonkee 9:8cb5ce483be3 414 // read register 0 and 1
Nate Yonkee 9:8cb5ce483be3 415 reg_val = MPR121::readRegister(ELE0_7_STAT);
Nate Yonkee 9:8cb5ce483be3 416 reg_val |= MPR121::readRegister(ELE8_11_STAT) << 8;
Nate Yonkee 9:8cb5ce483be3 417 // 2 and 3
Nate Yonkee 9:8cb5ce483be3 418 oor_val = MPR121::readRegister(ELE0_7_OOR_STAT);
Nate Yonkee 9:8cb5ce483be3 419 oor_val |= MPR121::readRegister(ELE8_11_OOR_STAT) << 8;
Nate Yonkee 9:8cb5ce483be3 420
Nate Yonkee 9:8cb5ce483be3 421 // debugging stuff and errors - if OOR fails someone was touching the pad during auto-config
Nate Yonkee 9:8cb5ce483be3 422 // Just reboot until they're not doing this
Nate Yonkee 9:8cb5ce483be3 423 if((0 != oor_val) && DEBUG) {
Nate Yonkee 9:8cb5ce483be3 424 debug("MPR121 OOR failure - 0x%04x\n", oor_val);
Nate Yonkee 9:8cb5ce483be3 425 wait(0.1f);
Nate Yonkee 9:8cb5ce483be3 426 NVIC_SystemReset();
Nate Yonkee 9:8cb5ce483be3 427 }
Nate Yonkee 9:8cb5ce483be3 428
Nate Yonkee 9:8cb5ce483be3 429 _button = reg_val;
Nate Yonkee 9:8cb5ce483be3 430 _button_has_changed = 1;
Nate Yonkee 9:8cb5ce483be3 431
Nate Yonkee 9:8cb5ce483be3 432 return;
Nate Yonkee 9:8cb5ce483be3 433 }
Nate Yonkee 9:8cb5ce483be3 434
Nate Yonkee 9:8cb5ce483be3 435 void MPR121::writeRegister(MPR121_REGISTER const reg, uint8_t const data) const
Nate Yonkee 9:8cb5ce483be3 436 {
Nate Yonkee 9:8cb5ce483be3 437 char buf[2] = {reg, data};
Nate Yonkee 9:8cb5ce483be3 438 uint8_t result = 0;
Nate Yonkee 9:8cb5ce483be3 439
Nate Yonkee 9:8cb5ce483be3 440 result = _i2c->write(_i2c_addr, buf, 2);
Nate Yonkee 9:8cb5ce483be3 441 if(result && DEBUG)
Nate Yonkee 9:8cb5ce483be3 442 {
Nate Yonkee 9:8cb5ce483be3 443 debug("I2C write failed\n");
Nate Yonkee 9:8cb5ce483be3 444 }
Nate Yonkee 9:8cb5ce483be3 445
Nate Yonkee 9:8cb5ce483be3 446 return;
Nate Yonkee 9:8cb5ce483be3 447 }
Nate Yonkee 9:8cb5ce483be3 448
Nate Yonkee 9:8cb5ce483be3 449 uint8_t MPR121::readRegister(MPR121_REGISTER const reg) const
Nate Yonkee 9:8cb5ce483be3 450 {
Nate Yonkee 9:8cb5ce483be3 451 char buf[1] = {reg}, data = 0;
Nate Yonkee 9:8cb5ce483be3 452 uint8_t result = 1;
Nate Yonkee 9:8cb5ce483be3 453
Nate Yonkee 9:8cb5ce483be3 454 result &= _i2c->write(_i2c_addr, buf, 1, true);
Nate Yonkee 9:8cb5ce483be3 455 result &= _i2c->read(_i2c_addr, &data, 1);
Nate Yonkee 9:8cb5ce483be3 456
Nate Yonkee 9:8cb5ce483be3 457 if(result && DEBUG)
Nate Yonkee 9:8cb5ce483be3 458 {
Nate Yonkee 9:8cb5ce483be3 459 debug("I2C read failed\n");
Nate Yonkee 9:8cb5ce483be3 460 }
Nate Yonkee 9:8cb5ce483be3 461
Nate Yonkee 9:8cb5ce483be3 462 return data;
Nate Yonkee 9:8cb5ce483be3 463 }
Nathan Yonkee 11:ad26c0810f02 464 >>>>>>> merge rev