A device driver for the Freescale MPR121 capactive touch IC. Not optimized for any particular system, just a starting point to get the chip up in running in no time. Changes to registers init() method will tailor the library for end system use.

Dependents:   Seeed_Grove_I2C_Touch_Example MPR121_HelloWorld mbed_petbottle_holder_shikake test_DEV-10508 ... more

Datasheet:

http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf

Information

Must add pull-ups to the I2C bus!!

Committer:
sam_grove
Date:
Mon Apr 07 16:05:30 2014 +0000
Revision:
4:c1bc00c5e8e5
Parent:
3:828260f21de6
Child:
6:b6bb38744edd
corrected typos

Who changed what in which revision?

UserRevisionLine numberNew 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 0:42add775212a 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 0:42add775212a 33
sam_grove 0:42add775212a 34 return;
sam_grove 0:42add775212a 35 }
sam_grove 0:42add775212a 36
sam_grove 0:42add775212a 37 void MPR121::init(void)
sam_grove 0:42add775212a 38 {
sam_grove 0:42add775212a 39 // set the i2c speed
sam_grove 0:42add775212a 40 _i2c->frequency(400000);
sam_grove 0:42add775212a 41 // irq is open-collector and active-low
sam_grove 0:42add775212a 42 _irq->mode(PullUp);
sam_grove 0:42add775212a 43
sam_grove 0:42add775212a 44 // setup and registers - start with POR values (must be in stop mode)
sam_grove 0:42add775212a 45 MPR121::writeRegister(SRST, 0x63); //REG 0x80
sam_grove 0:42add775212a 46
sam_grove 0:42add775212a 47 // Baseline Filtering Control Register (changes response sensitivity)
sam_grove 0:42add775212a 48 // http://cache.freescale.com/files/sensors/doc/app_note/AN3891.pdf
sam_grove 0:42add775212a 49 MPR121::writeRegister(MHDR, 0x1); //REG 0x2B
sam_grove 0:42add775212a 50 MPR121::writeRegister(NHDR, 0x1); //REG 0x2C
sam_grove 0:42add775212a 51 MPR121::writeRegister(NCLR, 0x0); //REG 0x2D
sam_grove 0:42add775212a 52 MPR121::writeRegister(FDLR, 0x0); //REG 0x2E
sam_grove 0:42add775212a 53 MPR121::writeRegister(MHDF, 0x1); //REG 0x2F
sam_grove 0:42add775212a 54 MPR121::writeRegister(NHDF, 0x1); //REG 0x30
sam_grove 0:42add775212a 55 MPR121::writeRegister(NCLF, 0xFF); //REG 0x31
sam_grove 0:42add775212a 56 MPR121::writeRegister(FDLF, 0x2); //REG 0x32
sam_grove 0:42add775212a 57
sam_grove 0:42add775212a 58 // Touch / Release Threshold
sam_grove 0:42add775212a 59 // cache.freescale.com/files/sensors/doc/app_note/AN3892.pdf
sam_grove 3:828260f21de6 60 for(int i=0; i<(12*2); i+=2) // touch
sam_grove 0:42add775212a 61 {
sam_grove 3:828260f21de6 62 MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); //REG 0x41...0x58 odd
sam_grove 0:42add775212a 63 }
sam_grove 3:828260f21de6 64 for(int i=0; i<(12*2); i+=2) // release
sam_grove 0:42add775212a 65 {
sam_grove 3:828260f21de6 66 MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); //REG 0x41...0x58 even
sam_grove 0:42add775212a 67 }
sam_grove 0:42add775212a 68
sam_grove 0:42add775212a 69 // Debounce Register DR=b6...4, DT=b2...0
sam_grove 0:42add775212a 70 MPR121::writeRegister(DT_DR, 0x11); //REG 0x5B
sam_grove 0:42add775212a 71
sam_grove 0:42add775212a 72 // Filter and Global CDC CDT Configuration (sample time, charge current)
sam_grove 0:42add775212a 73 MPR121::writeRegister(CDC_CONFIG, 0x10); //REG 0x5C default 10
sam_grove 0:42add775212a 74 MPR121::writeRegister(CDT_CONFIG, 0x20); //REG 0x5D default 24
sam_grove 0:42add775212a 75
sam_grove 0:42add775212a 76 // Auto-Configuration Registers
sam_grove 0:42add775212a 77 // http://cache.freescale.com/files/sensors/doc/app_note/AN3889.pdf
sam_grove 0:42add775212a 78 MPR121::writeRegister(AUTO_CFG0, 0x33); // REG 0x7B
sam_grove 0:42add775212a 79 MPR121::writeRegister(AUTO_CFG1, 0x07); // REG 0x7C
sam_grove 0:42add775212a 80 MPR121::writeRegister(USL, 0xc9); // REG 0x7D((3.3-.07)/3.3) * 256
sam_grove 0:42add775212a 81 MPR121::writeRegister(LSL, 0x83); // REG 0x7E((3.3-.07)/3.3) * 256 * 0.65f
sam_grove 0:42add775212a 82 MPR121::writeRegister(TL, 0xb5); // REG 0x7F((3.3-.07)/3.3) * 256 * 0.9f
sam_grove 0:42add775212a 83 // 255 > USL > TL > LSL > 0
sam_grove 0:42add775212a 84
sam_grove 0:42add775212a 85 // Electrode Configuration Register - enable all 12 and start
sam_grove 0:42add775212a 86 MPR121::writeRegister(ECR, 0x8f);
sam_grove 0:42add775212a 87
sam_grove 0:42add775212a 88 return;
sam_grove 0:42add775212a 89 }
sam_grove 0:42add775212a 90
sam_grove 0:42add775212a 91 void MPR121::enable(void)
sam_grove 0:42add775212a 92 {
sam_grove 0:42add775212a 93 _button = 0;
sam_grove 0:42add775212a 94 _button_has_changed = 0;
sam_grove 1:cee45334b36a 95 // enable the 12 electrodes - allow disable to put device into
sam_grove 1:cee45334b36a 96 // lower current consumption mode
sam_grove 1:cee45334b36a 97 MPR121::writeRegister(ECR, 0x8f);
sam_grove 1:cee45334b36a 98 // and attach the interrupt handler
sam_grove 0:42add775212a 99 _irq->fall(this, &MPR121::handler);
sam_grove 0:42add775212a 100
sam_grove 0:42add775212a 101 return;
sam_grove 0:42add775212a 102 }
sam_grove 0:42add775212a 103
sam_grove 0:42add775212a 104 void MPR121::disable(void)
sam_grove 0:42add775212a 105 {
sam_grove 1:cee45334b36a 106 // detach the interrupt handler
sam_grove 0:42add775212a 107 _irq->fall(NULL);
sam_grove 0:42add775212a 108 _button = 0;
sam_grove 0:42add775212a 109 _button_has_changed = 0;
sam_grove 1:cee45334b36a 110 // put the device in low current consumption mode - dont re-init registers
sam_grove 1:cee45334b36a 111 MPR121::writeRegister(ECR, 0x0);
sam_grove 1:cee45334b36a 112 MPR121::writeRegister(AUTO_CFG0, 0x0); // REG 0x7B
sam_grove 1:cee45334b36a 113 MPR121::writeRegister(AUTO_CFG1, 0x0); // REG 0x7C
sam_grove 0:42add775212a 114
sam_grove 0:42add775212a 115 return;
sam_grove 0:42add775212a 116 }
sam_grove 0:42add775212a 117
sam_grove 0:42add775212a 118 uint32_t MPR121::isPressed(void)
sam_grove 0:42add775212a 119 {
sam_grove 0:42add775212a 120 return _button_has_changed;
sam_grove 0:42add775212a 121 }
sam_grove 0:42add775212a 122
sam_grove 0:42add775212a 123 uint16_t MPR121::buttonPressed(void)
sam_grove 0:42add775212a 124 {
sam_grove 0:42add775212a 125 _button_has_changed = 0;
sam_grove 0:42add775212a 126 return _button;
sam_grove 0:42add775212a 127 }
sam_grove 0:42add775212a 128
sam_grove 3:828260f21de6 129 void MPR121::registerDump(Serial &obj) const
sam_grove 0:42add775212a 130 {
sam_grove 0:42add775212a 131 uint8_t reg_val = 0;
sam_grove 0:42add775212a 132
sam_grove 0:42add775212a 133 for(int i=0; i<0x80; i++)
sam_grove 0:42add775212a 134 {
sam_grove 3:828260f21de6 135 reg_val = MPR121::readRegister(static_cast<MPR121_REGISTER>(i));
sam_grove 3:828260f21de6 136 obj.printf("Reg 0x%02x: 0x%02x \n", i, reg_val);
sam_grove 0:42add775212a 137 }
sam_grove 0:42add775212a 138
sam_grove 0:42add775212a 139 return;
sam_grove 0:42add775212a 140 }
sam_grove 0:42add775212a 141
sam_grove 0:42add775212a 142 void MPR121::handler(void)
sam_grove 0:42add775212a 143 {
sam_grove 0:42add775212a 144 uint16_t reg_val = 0, oor_val = 0;
sam_grove 0:42add775212a 145 // read register 0 and 1
sam_grove 0:42add775212a 146 reg_val = MPR121::readRegister(ELE0_7_STAT);
sam_grove 0:42add775212a 147 reg_val |= MPR121::readRegister(ELE8_11_STAT) << 8;
sam_grove 0:42add775212a 148 // 2 and 3
sam_grove 0:42add775212a 149 oor_val = MPR121::readRegister(ELE0_7_OOR_STAT);
sam_grove 0:42add775212a 150 oor_val |= MPR121::readRegister(ELE8_11_OOR_STAT) << 8;
sam_grove 0:42add775212a 151
sam_grove 3:828260f21de6 152 // debugging stuff and errors - if OOR fails someone was touching the pad during auto-config
sam_grove 3:828260f21de6 153 // Just reboot until they're not doing this
sam_grove 3:828260f21de6 154 if((0 != oor_val) && DEBUG)
sam_grove 0:42add775212a 155 {
sam_grove 3:828260f21de6 156 debug("MPR121 OOR failure - 0x%04x\n", oor_val);
sam_grove 3:828260f21de6 157 wait(0.1f);
sam_grove 3:828260f21de6 158 NVIC_SystemReset();
sam_grove 0:42add775212a 159 }
sam_grove 0:42add775212a 160
sam_grove 0:42add775212a 161 _button = reg_val;
sam_grove 0:42add775212a 162 _button_has_changed = 1;
sam_grove 0:42add775212a 163
sam_grove 0:42add775212a 164 return;
sam_grove 0:42add775212a 165 }
sam_grove 0:42add775212a 166
sam_grove 3:828260f21de6 167 void MPR121::writeRegister(MPR121_REGISTER const reg, uint8_t const data) const
sam_grove 0:42add775212a 168 {
sam_grove 0:42add775212a 169 char buf[2] = {reg, data};
sam_grove 0:42add775212a 170 uint8_t result = 0;
sam_grove 0:42add775212a 171
sam_grove 0:42add775212a 172 result = _i2c->write(_i2c_addr, buf, 2);
sam_grove 0:42add775212a 173
sam_grove 3:828260f21de6 174 if(result && DEBUG)
sam_grove 0:42add775212a 175 {
sam_grove 4:c1bc00c5e8e5 176 debug("I2C write failed\n");
sam_grove 0:42add775212a 177 }
sam_grove 0:42add775212a 178
sam_grove 0:42add775212a 179 return;
sam_grove 0:42add775212a 180 }
sam_grove 0:42add775212a 181
sam_grove 3:828260f21de6 182 uint8_t MPR121::readRegister(MPR121_REGISTER const reg) const
sam_grove 0:42add775212a 183 {
sam_grove 3:828260f21de6 184 char buf[1] = {reg}, data = 0;
sam_grove 3:828260f21de6 185 uint8_t result = 1;
sam_grove 2:4c0d4b90a3ed 186
sam_grove 3:828260f21de6 187 result &= _i2c->write(_i2c_addr, buf, 1, true);
sam_grove 3:828260f21de6 188 result &= _i2c->read(_i2c_addr, &data, 1);
sam_grove 0:42add775212a 189
sam_grove 3:828260f21de6 190 if(result && DEBUG)
sam_grove 0:42add775212a 191 {
sam_grove 4:c1bc00c5e8e5 192 debug("I2C read failed\n");
sam_grove 0:42add775212a 193 }
sam_grove 0:42add775212a 194
sam_grove 0:42add775212a 195 return data;
sam_grove 0:42add775212a 196 }