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
MPR121.cpp
00001 /** 00002 * @file MPR121.cpp 00003 * @brief Device driver - MPR121 capactiive touch IC 00004 * @author sam grove 00005 * @version 1.0 00006 * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf 00007 * 00008 * Copyright (c) 2013 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); 00011 * you may not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, 00018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 */ 00022 00023 #include "MPR121.h" 00024 #include "mbed_debug.h" 00025 00026 #define DEBUG 1 00027 00028 MPR121::MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr) 00029 { 00030 _i2c = &i2c; 00031 _irq = &pin; 00032 _i2c_addr = (i2c_addr << 1); 00033 00034 return; 00035 } 00036 00037 void MPR121::init(void) 00038 { 00039 // set the i2c speed 00040 _i2c->frequency(400000); 00041 // irq is open-collector and active-low 00042 _irq->mode(PullUp); 00043 00044 // setup and registers - start with POR values (must be in stop mode) 00045 MPR121::writeRegister(SRST, 0x63); //REG 0x80 00046 00047 // Baseline Filtering Control Register (changes response sensitivity) 00048 // http://cache.freescale.com/files/sensors/doc/app_note/AN3891.pdf 00049 MPR121::writeRegister(MHDR, 0x1); //REG 0x2B 00050 MPR121::writeRegister(NHDR, 0x1); //REG 0x2C 00051 MPR121::writeRegister(NCLR, 0x0); //REG 0x2D 00052 MPR121::writeRegister(FDLR, 0x0); //REG 0x2E 00053 MPR121::writeRegister(MHDF, 0x1); //REG 0x2F 00054 MPR121::writeRegister(NHDF, 0x1); //REG 0x30 00055 MPR121::writeRegister(NCLF, 0xFF); //REG 0x31 00056 MPR121::writeRegister(FDLF, 0x2); //REG 0x32 00057 00058 // Touch / Release Threshold 00059 // cache.freescale.com/files/sensors/doc/app_note/AN3892.pdf 00060 for(int i=0; i<(12*2); i+=2) // touch 00061 { 00062 MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); //REG 0x41...0x58 odd 00063 } 00064 for(int i=0; i<(12*2); i+=2) // release 00065 { 00066 MPR121::writeRegister(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); //REG 0x41...0x58 even 00067 } 00068 00069 // Debounce Register DR=b6...4, DT=b2...0 00070 MPR121::writeRegister(DT_DR, 0x11); //REG 0x5B 00071 00072 // Filter and Global CDC CDT Configuration (sample time, charge current) 00073 MPR121::writeRegister(CDC_CONFIG, 0x10); //REG 0x5C default 10 00074 MPR121::writeRegister(CDT_CONFIG, 0x20); //REG 0x5D default 24 00075 00076 // Auto-Configuration Registers 00077 // http://cache.freescale.com/files/sensors/doc/app_note/AN3889.pdf 00078 MPR121::writeRegister(AUTO_CFG0, 0x33); // REG 0x7B 00079 MPR121::writeRegister(AUTO_CFG1, 0x07); // REG 0x7C 00080 MPR121::writeRegister(USL, 0xc9); // REG 0x7D((3.3-.07)/3.3) * 256 00081 MPR121::writeRegister(LSL, 0x83); // REG 0x7E((3.3-.07)/3.3) * 256 * 0.65f 00082 MPR121::writeRegister(TL, 0xb5); // REG 0x7F((3.3-.07)/3.3) * 256 * 0.9f 00083 // 255 > USL > TL > LSL > 0 00084 00085 // Electrode Configuration Register - enable all 12 and start 00086 MPR121::writeRegister(ECR, 0x8f); 00087 00088 return; 00089 } 00090 00091 void MPR121::enable(void) 00092 { 00093 _button = 0; 00094 _button_has_changed = 0; 00095 // enable the 12 electrodes - allow disable to put device into 00096 // lower current consumption mode 00097 MPR121::writeRegister(ECR, 0x8f); 00098 // and attach the interrupt handler 00099 _irq->fall(this, &MPR121::handler); 00100 00101 return; 00102 } 00103 00104 void MPR121::disable(void) 00105 { 00106 // detach the interrupt handler 00107 _irq->fall(NULL); 00108 _button = 0; 00109 _button_has_changed = 0; 00110 // put the device in low current consumption mode - dont re-init registers 00111 MPR121::writeRegister(ECR, 0x0); 00112 MPR121::writeRegister(AUTO_CFG0, 0x0); // REG 0x7B 00113 MPR121::writeRegister(AUTO_CFG1, 0x0); // REG 0x7C 00114 00115 return; 00116 } 00117 00118 uint32_t MPR121::isPressed(void) 00119 { 00120 return _button_has_changed; 00121 } 00122 00123 uint16_t MPR121::buttonPressed(void) 00124 { 00125 _button_has_changed = 0; 00126 return _button; 00127 } 00128 00129 void MPR121::registerDump(Serial &obj) const 00130 { 00131 uint8_t reg_val = 0; 00132 00133 for(int i=0; i<0x80; i++) 00134 { 00135 reg_val = MPR121::readRegister(static_cast<MPR121_REGISTER>(i)); 00136 obj.printf("Reg 0x%02x: 0x%02x \n", i, reg_val); 00137 } 00138 00139 return; 00140 } 00141 00142 void MPR121::handler(void) 00143 { 00144 uint16_t reg_val = 0, oor_val = 0; 00145 // read register 0 and 1 00146 reg_val = MPR121::readRegister(ELE0_7_STAT); 00147 reg_val |= MPR121::readRegister(ELE8_11_STAT) << 8; 00148 // 2 and 3 00149 oor_val = MPR121::readRegister(ELE0_7_OOR_STAT); 00150 oor_val |= MPR121::readRegister(ELE8_11_OOR_STAT) << 8; 00151 00152 // debugging stuff and errors - if OOR fails someone was touching the pad during auto-config 00153 // Just reboot until they're not doing this 00154 if((0 != oor_val) && DEBUG) 00155 { 00156 debug("MPR121 OOR failure - 0x%04x\n", oor_val); 00157 wait(0.1f); 00158 NVIC_SystemReset(); 00159 } 00160 00161 _button = reg_val; 00162 _button_has_changed = 1; 00163 00164 return; 00165 } 00166 00167 void MPR121::writeRegister(MPR121_REGISTER const reg, uint8_t const data) const 00168 { 00169 char buf[2] = {reg, data}; 00170 uint8_t result = 0; 00171 00172 result = _i2c->write(_i2c_addr, buf, 2); 00173 00174 if(result && DEBUG) 00175 { 00176 debug("I2c write failed\n"); 00177 } 00178 00179 return; 00180 } 00181 00182 uint8_t MPR121::readRegister(MPR121_REGISTER const reg) const 00183 { 00184 char buf[1] = {reg}, data = 0; 00185 uint8_t result = 1; 00186 00187 result &= _i2c->write(_i2c_addr, buf, 1, true); 00188 result &= _i2c->read(_i2c_addr, &data, 1); 00189 00190 if(result && DEBUG) 00191 { 00192 debug("I2c read failed\n"); 00193 } 00194 00195 return data; 00196 }
Generated on Wed Jul 13 2022 12:22:31 by 1.7.2