use mbed os
Dependents: Seeed_Grove_I2C_Touch_Example
Fork of MPR121 by
MPR121.cpp@6:b6bb38744edd, 2014-08-15 (annotated)
- Committer:
- sam_grove
- Date:
- Fri Aug 15 23:04:08 2014 +0000
- Revision:
- 6:b6bb38744edd
- Parent:
- 5:3934358ec2b7
- Parent:
- 4:c1bc00c5e8e5
- Child:
- 9:8cb5ce483be3
- Child:
- 10:fb2d2454fea4
Merged changes from branches
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:42add775212a | 1 | /** |
sam_grove | 0:42add775212a | 2 | * @file MPR121.cpp |
sam_grove | 0:42add775212a | 3 | * @brief Device driver - MPR121 capactiive touch IC |
sam_grove | 0:42add775212a | 4 | * @author sam grove |
sam_grove | 0:42add775212a | 5 | * @version 1.0 |
sam_grove | 0:42add775212a | 6 | * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf |
sam_grove | 0:42add775212a | 7 | * |
sam_grove | 0:42add775212a | 8 | * Copyright (c) 2013 |
sam_grove | 0:42add775212a | 9 | * |
sam_grove | 0:42add775212a | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 0:42add775212a | 11 | * you may not use this file except in compliance with the License. |
sam_grove | 0:42add775212a | 12 | * You may obtain a copy of the License at |
sam_grove | 0:42add775212a | 13 | * |
sam_grove | 0:42add775212a | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 0:42add775212a | 15 | * |
sam_grove | 0:42add775212a | 16 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 0:42add775212a | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 0:42add775212a | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 0:42add775212a | 19 | * See the License for the specific language governing permissions and |
sam_grove | 0:42add775212a | 20 | * limitations under the License. |
sam_grove | 0:42add775212a | 21 | */ |
sam_grove | 0:42add775212a | 22 | |
sam_grove | 0:42add775212a | 23 | #include "MPR121.h" |
sam_grove | 3:828260f21de6 | 24 | #include "mbed_debug.h" |
sam_grove | 3:828260f21de6 | 25 | |
sam_grove | 3:828260f21de6 | 26 | #define DEBUG 1 |
sam_grove | 5:3934358ec2b7 | 27 | |
sam_grove | 0:42add775212a | 28 | MPR121::MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr) |
sam_grove | 0:42add775212a | 29 | { |
sam_grove | 0:42add775212a | 30 | _i2c = &i2c; |
sam_grove | 0:42add775212a | 31 | _irq = &pin; |
sam_grove | 0:42add775212a | 32 | _i2c_addr = (i2c_addr << 1); |
sam_grove | 5:3934358ec2b7 | 33 | |
sam_grove | 5:3934358ec2b7 | 34 | return; |
sam_grove | 5:3934358ec2b7 | 35 | } |
sam_grove | 5:3934358ec2b7 | 36 | |
sam_grove | 5:3934358ec2b7 | 37 | MPR121::MPR121(I2C &i2c, MPR121_ADDR i2c_addr) |
sam_grove | 5:3934358ec2b7 | 38 | { |
sam_grove | 5:3934358ec2b7 | 39 | _i2c = &i2c; |
sam_grove | 5:3934358ec2b7 | 40 | _irq = NULL; |
sam_grove | 5:3934358ec2b7 | 41 | _i2c_addr = (i2c_addr << 1); |
sam_grove | 5:3934358ec2b7 | 42 | |
sam_grove | 0:42add775212a | 43 | return; |
sam_grove | 0:42add775212a | 44 | } |
sam_grove | 0:42add775212a | 45 | |
sam_grove | 0:42add775212a | 46 | void MPR121::init(void) |
sam_grove | 0:42add775212a | 47 | { |
sam_grove | 0:42add775212a | 48 | // set the i2c speed |
sam_grove | 0:42add775212a | 49 | _i2c->frequency(400000); |
sam_grove | 0:42add775212a | 50 | // irq is open-collector and active-low |
sam_grove | 5:3934358ec2b7 | 51 | if(_irq != NULL) { |
sam_grove | 5:3934358ec2b7 | 52 | _irq->mode(PullUp); |
sam_grove | 5:3934358ec2b7 | 53 | } |
sam_grove | 5:3934358ec2b7 | 54 | |
sam_grove | 0:42add775212a | 55 | // setup and registers - start with POR values (must be in stop mode) |
sam_grove | 0:42add775212a | 56 | MPR121::writeRegister(SRST, 0x63); //REG 0x80 |
sam_grove | 5:3934358ec2b7 | 57 | |
sam_grove | 0:42add775212a | 58 | // Baseline Filtering Control Register (changes response sensitivity) |
sam_grove | 0:42add775212a | 59 | // http://cache.freescale.com/files/sensors/doc/app_note/AN3891.pdf |
sam_grove | 0:42add775212a | 60 | MPR121::writeRegister(MHDR, 0x1); //REG 0x2B |
sam_grove | 0:42add775212a | 61 | MPR121::writeRegister(NHDR, 0x1); //REG 0x2C |
sam_grove | 0:42add775212a | 62 | MPR121::writeRegister(NCLR, 0x0); //REG 0x2D |
sam_grove | 0:42add775212a | 63 | MPR121::writeRegister(FDLR, 0x0); //REG 0x2E |
sam_grove | 0:42add775212a | 64 | MPR121::writeRegister(MHDF, 0x1); //REG 0x2F |
sam_grove | 0:42add775212a | 65 | MPR121::writeRegister(NHDF, 0x1); //REG 0x30 |
sam_grove | 0:42add775212a | 66 | MPR121::writeRegister(NCLF, 0xFF); //REG 0x31 |
sam_grove | 0:42add775212a | 67 | MPR121::writeRegister(FDLF, 0x2); //REG 0x32 |
sam_grove | 5:3934358ec2b7 | 68 | |
sam_grove | 0:42add775212a | 69 | // Touch / Release Threshold |
sam_grove | 0:42add775212a | 70 | // cache.freescale.com/files/sensors/doc/app_note/AN3892.pdf |
sam_grove | 5:3934358ec2b7 | 71 | for(int i=0; i<(12*2); i+=2) { // touch |
sam_grove | 3:828260f21de6 | 72 | MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); //REG 0x41...0x58 odd |
sam_grove | 0:42add775212a | 73 | } |
sam_grove | 5:3934358ec2b7 | 74 | for(int i=0; i<(12*2); i+=2) { // release |
sam_grove | 3:828260f21de6 | 75 | MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); //REG 0x41...0x58 even |
sam_grove | 0:42add775212a | 76 | } |
sam_grove | 5:3934358ec2b7 | 77 | |
sam_grove | 0:42add775212a | 78 | // Debounce Register DR=b6...4, DT=b2...0 |
sam_grove | 0:42add775212a | 79 | MPR121::writeRegister(DT_DR, 0x11); //REG 0x5B |
sam_grove | 5:3934358ec2b7 | 80 | |
sam_grove | 0:42add775212a | 81 | // Filter and Global CDC CDT Configuration (sample time, charge current) |
sam_grove | 0:42add775212a | 82 | MPR121::writeRegister(CDC_CONFIG, 0x10); //REG 0x5C default 10 |
sam_grove | 0:42add775212a | 83 | MPR121::writeRegister(CDT_CONFIG, 0x20); //REG 0x5D default 24 |
sam_grove | 5:3934358ec2b7 | 84 | |
sam_grove | 0:42add775212a | 85 | // Auto-Configuration Registers |
sam_grove | 0:42add775212a | 86 | // http://cache.freescale.com/files/sensors/doc/app_note/AN3889.pdf |
sam_grove | 0:42add775212a | 87 | MPR121::writeRegister(AUTO_CFG0, 0x33); // REG 0x7B |
sam_grove | 0:42add775212a | 88 | MPR121::writeRegister(AUTO_CFG1, 0x07); // REG 0x7C |
sam_grove | 0:42add775212a | 89 | MPR121::writeRegister(USL, 0xc9); // REG 0x7D((3.3-.07)/3.3) * 256 |
sam_grove | 0:42add775212a | 90 | MPR121::writeRegister(LSL, 0x83); // REG 0x7E((3.3-.07)/3.3) * 256 * 0.65f |
sam_grove | 0:42add775212a | 91 | MPR121::writeRegister(TL, 0xb5); // REG 0x7F((3.3-.07)/3.3) * 256 * 0.9f |
sam_grove | 0:42add775212a | 92 | // 255 > USL > TL > LSL > 0 |
sam_grove | 5:3934358ec2b7 | 93 | |
sam_grove | 0:42add775212a | 94 | // Electrode Configuration Register - enable all 12 and start |
sam_grove | 0:42add775212a | 95 | MPR121::writeRegister(ECR, 0x8f); |
sam_grove | 5:3934358ec2b7 | 96 | |
sam_grove | 0:42add775212a | 97 | return; |
sam_grove | 0:42add775212a | 98 | } |
sam_grove | 0:42add775212a | 99 | |
sam_grove | 0:42add775212a | 100 | void MPR121::enable(void) |
sam_grove | 0:42add775212a | 101 | { |
sam_grove | 0:42add775212a | 102 | _button = 0; |
sam_grove | 0:42add775212a | 103 | _button_has_changed = 0; |
sam_grove | 1:cee45334b36a | 104 | // enable the 12 electrodes - allow disable to put device into |
sam_grove | 1:cee45334b36a | 105 | // lower current consumption mode |
sam_grove | 1:cee45334b36a | 106 | MPR121::writeRegister(ECR, 0x8f); |
sam_grove | 1:cee45334b36a | 107 | // and attach the interrupt handler |
sam_grove | 5:3934358ec2b7 | 108 | if(_irq != NULL) { |
sam_grove | 5:3934358ec2b7 | 109 | _irq->fall(this, &MPR121::handler); |
sam_grove | 5:3934358ec2b7 | 110 | } |
sam_grove | 5:3934358ec2b7 | 111 | |
sam_grove | 0:42add775212a | 112 | return; |
sam_grove | 0:42add775212a | 113 | } |
sam_grove | 0:42add775212a | 114 | |
sam_grove | 0:42add775212a | 115 | void MPR121::disable(void) |
sam_grove | 0:42add775212a | 116 | { |
sam_grove | 1:cee45334b36a | 117 | // detach the interrupt handler |
sam_grove | 0:42add775212a | 118 | _irq->fall(NULL); |
sam_grove | 0:42add775212a | 119 | _button = 0; |
sam_grove | 0:42add775212a | 120 | _button_has_changed = 0; |
sam_grove | 1:cee45334b36a | 121 | // put the device in low current consumption mode - dont re-init registers |
sam_grove | 1:cee45334b36a | 122 | MPR121::writeRegister(ECR, 0x0); |
sam_grove | 1:cee45334b36a | 123 | MPR121::writeRegister(AUTO_CFG0, 0x0); // REG 0x7B |
sam_grove | 1:cee45334b36a | 124 | MPR121::writeRegister(AUTO_CFG1, 0x0); // REG 0x7C |
sam_grove | 5:3934358ec2b7 | 125 | |
sam_grove | 0:42add775212a | 126 | return; |
sam_grove | 0:42add775212a | 127 | } |
sam_grove | 0:42add775212a | 128 | |
sam_grove | 0:42add775212a | 129 | uint32_t MPR121::isPressed(void) |
sam_grove | 0:42add775212a | 130 | { |
sam_grove | 0:42add775212a | 131 | return _button_has_changed; |
sam_grove | 0:42add775212a | 132 | } |
sam_grove | 0:42add775212a | 133 | |
sam_grove | 0:42add775212a | 134 | uint16_t MPR121::buttonPressed(void) |
sam_grove | 0:42add775212a | 135 | { |
sam_grove | 5:3934358ec2b7 | 136 | if(_irq == NULL) { |
sam_grove | 5:3934358ec2b7 | 137 | handler(); |
sam_grove | 5:3934358ec2b7 | 138 | } |
sam_grove | 0:42add775212a | 139 | _button_has_changed = 0; |
sam_grove | 0:42add775212a | 140 | return _button; |
sam_grove | 0:42add775212a | 141 | } |
sam_grove | 0:42add775212a | 142 | |
sam_grove | 3:828260f21de6 | 143 | void MPR121::registerDump(Serial &obj) const |
sam_grove | 0:42add775212a | 144 | { |
sam_grove | 0:42add775212a | 145 | uint8_t reg_val = 0; |
sam_grove | 5:3934358ec2b7 | 146 | |
sam_grove | 5:3934358ec2b7 | 147 | for(int i=0; i<0x80; i++) { |
sam_grove | 3:828260f21de6 | 148 | reg_val = MPR121::readRegister(static_cast<MPR121_REGISTER>(i)); |
sam_grove | 3:828260f21de6 | 149 | obj.printf("Reg 0x%02x: 0x%02x \n", i, reg_val); |
sam_grove | 0:42add775212a | 150 | } |
sam_grove | 5:3934358ec2b7 | 151 | |
sam_grove | 5:3934358ec2b7 | 152 | return; |
sam_grove | 5:3934358ec2b7 | 153 | } |
sam_grove | 5:3934358ec2b7 | 154 | |
sam_grove | 5:3934358ec2b7 | 155 | void MPR121::registerDump(void) const |
sam_grove | 5:3934358ec2b7 | 156 | { |
sam_grove | 5:3934358ec2b7 | 157 | uint8_t reg_val = 0; |
sam_grove | 5:3934358ec2b7 | 158 | |
sam_grove | 5:3934358ec2b7 | 159 | for(int i=0; i<0x80; i++) { |
sam_grove | 5:3934358ec2b7 | 160 | reg_val = MPR121::readRegister(static_cast<MPR121_REGISTER>(i)); |
sam_grove | 5:3934358ec2b7 | 161 | printf("Reg 0x%02x: 0x%02x \n", i, reg_val); |
sam_grove | 5:3934358ec2b7 | 162 | } |
sam_grove | 5:3934358ec2b7 | 163 | |
sam_grove | 0:42add775212a | 164 | return; |
sam_grove | 0:42add775212a | 165 | } |
sam_grove | 0:42add775212a | 166 | |
sam_grove | 0:42add775212a | 167 | void MPR121::handler(void) |
sam_grove | 0:42add775212a | 168 | { |
sam_grove | 0:42add775212a | 169 | uint16_t reg_val = 0, oor_val = 0; |
sam_grove | 0:42add775212a | 170 | // read register 0 and 1 |
sam_grove | 0:42add775212a | 171 | reg_val = MPR121::readRegister(ELE0_7_STAT); |
sam_grove | 0:42add775212a | 172 | reg_val |= MPR121::readRegister(ELE8_11_STAT) << 8; |
sam_grove | 0:42add775212a | 173 | // 2 and 3 |
sam_grove | 0:42add775212a | 174 | oor_val = MPR121::readRegister(ELE0_7_OOR_STAT); |
sam_grove | 0:42add775212a | 175 | oor_val |= MPR121::readRegister(ELE8_11_OOR_STAT) << 8; |
sam_grove | 5:3934358ec2b7 | 176 | |
sam_grove | 3:828260f21de6 | 177 | // debugging stuff and errors - if OOR fails someone was touching the pad during auto-config |
sam_grove | 3:828260f21de6 | 178 | // Just reboot until they're not doing this |
sam_grove | 5:3934358ec2b7 | 179 | if((0 != oor_val) && DEBUG) { |
sam_grove | 3:828260f21de6 | 180 | debug("MPR121 OOR failure - 0x%04x\n", oor_val); |
sam_grove | 3:828260f21de6 | 181 | wait(0.1f); |
sam_grove | 3:828260f21de6 | 182 | NVIC_SystemReset(); |
sam_grove | 0:42add775212a | 183 | } |
sam_grove | 5:3934358ec2b7 | 184 | |
sam_grove | 0:42add775212a | 185 | _button = reg_val; |
sam_grove | 0:42add775212a | 186 | _button_has_changed = 1; |
sam_grove | 5:3934358ec2b7 | 187 | |
sam_grove | 0:42add775212a | 188 | return; |
sam_grove | 0:42add775212a | 189 | } |
sam_grove | 0:42add775212a | 190 | |
sam_grove | 3:828260f21de6 | 191 | void MPR121::writeRegister(MPR121_REGISTER const reg, uint8_t const data) const |
sam_grove | 0:42add775212a | 192 | { |
sam_grove | 0:42add775212a | 193 | char buf[2] = {reg, data}; |
sam_grove | 0:42add775212a | 194 | uint8_t result = 0; |
sam_grove | 5:3934358ec2b7 | 195 | |
sam_grove | 0:42add775212a | 196 | result = _i2c->write(_i2c_addr, buf, 2); |
sam_grove | 3:828260f21de6 | 197 | if(result && DEBUG) |
sam_grove | 0:42add775212a | 198 | { |
sam_grove | 4:c1bc00c5e8e5 | 199 | debug("I2C write failed\n"); |
sam_grove | 0:42add775212a | 200 | } |
sam_grove | 5:3934358ec2b7 | 201 | |
sam_grove | 0:42add775212a | 202 | return; |
sam_grove | 0:42add775212a | 203 | } |
sam_grove | 0:42add775212a | 204 | |
sam_grove | 3:828260f21de6 | 205 | uint8_t MPR121::readRegister(MPR121_REGISTER const reg) const |
sam_grove | 0:42add775212a | 206 | { |
sam_grove | 3:828260f21de6 | 207 | char buf[1] = {reg}, data = 0; |
sam_grove | 3:828260f21de6 | 208 | uint8_t result = 1; |
sam_grove | 5:3934358ec2b7 | 209 | |
sam_grove | 3:828260f21de6 | 210 | result &= _i2c->write(_i2c_addr, buf, 1, true); |
sam_grove | 3:828260f21de6 | 211 | result &= _i2c->read(_i2c_addr, &data, 1); |
sam_grove | 0:42add775212a | 212 | |
sam_grove | 3:828260f21de6 | 213 | if(result && DEBUG) |
sam_grove | 0:42add775212a | 214 | { |
sam_grove | 4:c1bc00c5e8e5 | 215 | debug("I2C read failed\n"); |
sam_grove | 0:42add775212a | 216 | } |
sam_grove | 5:3934358ec2b7 | 217 | |
sam_grove | 0:42add775212a | 218 | return data; |
sam_grove | 0:42add775212a | 219 | } |