use mbed os
Dependents: Seeed_Grove_I2C_Touch_Example
Fork of MPR121 by
MPR121.cpp@0:42add775212a, 2013-03-07 (annotated)
- Committer:
- sam_grove
- Date:
- Thu Mar 07 19:25:08 2013 +0000
- Revision:
- 0:42add775212a
- Child:
- 1:cee45334b36a
Working baseline example
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 | 0:42add775212a | 24 | |
sam_grove | 0:42add775212a | 25 | MPR121::MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr) |
sam_grove | 0:42add775212a | 26 | { |
sam_grove | 0:42add775212a | 27 | _i2c = &i2c; |
sam_grove | 0:42add775212a | 28 | _irq = &pin; |
sam_grove | 0:42add775212a | 29 | _i2c_addr = (i2c_addr << 1); |
sam_grove | 0:42add775212a | 30 | |
sam_grove | 0:42add775212a | 31 | return; |
sam_grove | 0:42add775212a | 32 | } |
sam_grove | 0:42add775212a | 33 | |
sam_grove | 0:42add775212a | 34 | void MPR121::init(void) |
sam_grove | 0:42add775212a | 35 | { |
sam_grove | 0:42add775212a | 36 | uint8_t i = 0; |
sam_grove | 0:42add775212a | 37 | // set the i2c speed |
sam_grove | 0:42add775212a | 38 | _i2c->frequency(400000); |
sam_grove | 0:42add775212a | 39 | // irq is open-collector and active-low |
sam_grove | 0:42add775212a | 40 | _irq->mode(PullUp); |
sam_grove | 0:42add775212a | 41 | |
sam_grove | 0:42add775212a | 42 | // setup and registers - start with POR values (must be in stop mode) |
sam_grove | 0:42add775212a | 43 | MPR121::writeRegister(SRST, 0x63); //REG 0x80 |
sam_grove | 0:42add775212a | 44 | |
sam_grove | 0:42add775212a | 45 | // baseline < filtered data = touch (set by auto-config) |
sam_grove | 0:42add775212a | 46 | // for(i=0; i<12; i++) |
sam_grove | 0:42add775212a | 47 | // { |
sam_grove | 0:42add775212a | 48 | // MPR121::writeRegister(E0BV+i, 0x10); //REG 0x1E...0x29 |
sam_grove | 0:42add775212a | 49 | // } |
sam_grove | 0:42add775212a | 50 | |
sam_grove | 0:42add775212a | 51 | // Baseline Filtering Control Register (changes response sensitivity) |
sam_grove | 0:42add775212a | 52 | // http://cache.freescale.com/files/sensors/doc/app_note/AN3891.pdf |
sam_grove | 0:42add775212a | 53 | MPR121::writeRegister(MHDR, 0x1); //REG 0x2B |
sam_grove | 0:42add775212a | 54 | MPR121::writeRegister(NHDR, 0x1); //REG 0x2C |
sam_grove | 0:42add775212a | 55 | MPR121::writeRegister(NCLR, 0x0); //REG 0x2D |
sam_grove | 0:42add775212a | 56 | MPR121::writeRegister(FDLR, 0x0); //REG 0x2E |
sam_grove | 0:42add775212a | 57 | MPR121::writeRegister(MHDF, 0x1); //REG 0x2F |
sam_grove | 0:42add775212a | 58 | MPR121::writeRegister(NHDF, 0x1); //REG 0x30 |
sam_grove | 0:42add775212a | 59 | MPR121::writeRegister(NCLF, 0xFF); //REG 0x31 |
sam_grove | 0:42add775212a | 60 | MPR121::writeRegister(FDLF, 0x2); //REG 0x32 |
sam_grove | 0:42add775212a | 61 | |
sam_grove | 0:42add775212a | 62 | |
sam_grove | 0:42add775212a | 63 | // Touch / Release Threshold |
sam_grove | 0:42add775212a | 64 | // cache.freescale.com/files/sensors/doc/app_note/AN3892.pdf |
sam_grove | 0:42add775212a | 65 | for(i=0; i<(12*2); i+=2) // touch |
sam_grove | 0:42add775212a | 66 | { |
sam_grove | 0:42add775212a | 67 | MPR121::writeRegister(E0TTH+i, 0x20); //REG 0x41...0x58 odd |
sam_grove | 0:42add775212a | 68 | } |
sam_grove | 0:42add775212a | 69 | for(i=0; i<(12*2); i+=2) // release |
sam_grove | 0:42add775212a | 70 | { |
sam_grove | 0:42add775212a | 71 | MPR121::writeRegister(E0RTH+i, 0x10); //REG 0x41...0x58 even |
sam_grove | 0:42add775212a | 72 | } |
sam_grove | 0:42add775212a | 73 | |
sam_grove | 0:42add775212a | 74 | // Debounce Register DR=b6...4, DT=b2...0 |
sam_grove | 0:42add775212a | 75 | MPR121::writeRegister(DT_DR, 0x11); //REG 0x5B |
sam_grove | 0:42add775212a | 76 | |
sam_grove | 0:42add775212a | 77 | // Filter and Global CDC CDT Configuration (sample time, charge current) |
sam_grove | 0:42add775212a | 78 | MPR121::writeRegister(CDC_CONFIG, 0x10); //REG 0x5C default 10 |
sam_grove | 0:42add775212a | 79 | MPR121::writeRegister(CDT_CONFIG, 0x20); //REG 0x5D default 24 |
sam_grove | 0:42add775212a | 80 | |
sam_grove | 0:42add775212a | 81 | // Electrode Charge Current Register - uses CDC_STAT if 0 |
sam_grove | 0:42add775212a | 82 | // for(i=0; i<12; i++) // current |
sam_grove | 0:42add775212a | 83 | // { |
sam_grove | 0:42add775212a | 84 | // MPR121::writeRegister(CDC0+i, 0x10); //REG 0x5F ... 0x6B |
sam_grove | 0:42add775212a | 85 | // } |
sam_grove | 0:42add775212a | 86 | // for(i=0; i<6; i++) // time |
sam_grove | 0:42add775212a | 87 | // { |
sam_grove | 0:42add775212a | 88 | // MPR121::writeRegister(CDT0_CDT1+i, 0x11); //REG 0x6C ... 0x72 |
sam_grove | 0:42add775212a | 89 | // } |
sam_grove | 0:42add775212a | 90 | |
sam_grove | 0:42add775212a | 91 | // Auto-Configuration Registers |
sam_grove | 0:42add775212a | 92 | // http://cache.freescale.com/files/sensors/doc/app_note/AN3889.pdf |
sam_grove | 0:42add775212a | 93 | MPR121::writeRegister(AUTO_CFG0, 0x33); // REG 0x7B |
sam_grove | 0:42add775212a | 94 | MPR121::writeRegister(AUTO_CFG1, 0x07); // REG 0x7C |
sam_grove | 0:42add775212a | 95 | MPR121::writeRegister(USL, 0xc9); // REG 0x7D((3.3-.07)/3.3) * 256 |
sam_grove | 0:42add775212a | 96 | MPR121::writeRegister(LSL, 0x83); // REG 0x7E((3.3-.07)/3.3) * 256 * 0.65f |
sam_grove | 0:42add775212a | 97 | MPR121::writeRegister(TL, 0xb5); // REG 0x7F((3.3-.07)/3.3) * 256 * 0.9f |
sam_grove | 0:42add775212a | 98 | // 255 > USL > TL > LSL > 0 |
sam_grove | 0:42add775212a | 99 | |
sam_grove | 0:42add775212a | 100 | // MPR121::registerDump(); |
sam_grove | 0:42add775212a | 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 | 0:42add775212a | 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 | 0:42add775212a | 112 | _irq->fall(this, &MPR121::handler); |
sam_grove | 0:42add775212a | 113 | |
sam_grove | 0:42add775212a | 114 | return; |
sam_grove | 0:42add775212a | 115 | } |
sam_grove | 0:42add775212a | 116 | |
sam_grove | 0:42add775212a | 117 | void MPR121::disable(void) |
sam_grove | 0:42add775212a | 118 | { |
sam_grove | 0:42add775212a | 119 | _irq->fall(NULL); |
sam_grove | 0:42add775212a | 120 | _button = 0; |
sam_grove | 0:42add775212a | 121 | _button_has_changed = 0; |
sam_grove | 0:42add775212a | 122 | |
sam_grove | 0:42add775212a | 123 | return; |
sam_grove | 0:42add775212a | 124 | } |
sam_grove | 0:42add775212a | 125 | |
sam_grove | 0:42add775212a | 126 | uint32_t MPR121::isPressed(void) |
sam_grove | 0:42add775212a | 127 | { |
sam_grove | 0:42add775212a | 128 | return _button_has_changed; |
sam_grove | 0:42add775212a | 129 | } |
sam_grove | 0:42add775212a | 130 | |
sam_grove | 0:42add775212a | 131 | uint16_t MPR121::buttonPressed(void) |
sam_grove | 0:42add775212a | 132 | { |
sam_grove | 0:42add775212a | 133 | _button_has_changed = 0; |
sam_grove | 0:42add775212a | 134 | return _button; |
sam_grove | 0:42add775212a | 135 | } |
sam_grove | 0:42add775212a | 136 | |
sam_grove | 0:42add775212a | 137 | void MPR121::registerDump(void) |
sam_grove | 0:42add775212a | 138 | { |
sam_grove | 0:42add775212a | 139 | uint8_t reg_val = 0; |
sam_grove | 0:42add775212a | 140 | |
sam_grove | 0:42add775212a | 141 | for(int i=0; i<0x80; i++) |
sam_grove | 0:42add775212a | 142 | { |
sam_grove | 0:42add775212a | 143 | reg_val = MPR121::readRegister(i); |
sam_grove | 0:42add775212a | 144 | printf("Reg 0x%02x: 0x%02x \n", i, reg_val); |
sam_grove | 0:42add775212a | 145 | } |
sam_grove | 0:42add775212a | 146 | |
sam_grove | 0:42add775212a | 147 | return; |
sam_grove | 0:42add775212a | 148 | } |
sam_grove | 0:42add775212a | 149 | |
sam_grove | 0:42add775212a | 150 | void MPR121::handler(void) |
sam_grove | 0:42add775212a | 151 | { |
sam_grove | 0:42add775212a | 152 | uint16_t reg_val = 0, oor_val = 0; |
sam_grove | 0:42add775212a | 153 | // read register 0 and 1 |
sam_grove | 0:42add775212a | 154 | reg_val = MPR121::readRegister(ELE0_7_STAT); |
sam_grove | 0:42add775212a | 155 | reg_val |= MPR121::readRegister(ELE8_11_STAT) << 8; |
sam_grove | 0:42add775212a | 156 | // 2 and 3 |
sam_grove | 0:42add775212a | 157 | oor_val = MPR121::readRegister(ELE0_7_OOR_STAT); |
sam_grove | 0:42add775212a | 158 | oor_val |= MPR121::readRegister(ELE8_11_OOR_STAT) << 8; |
sam_grove | 0:42add775212a | 159 | |
sam_grove | 0:42add775212a | 160 | if(0 != oor_val) |
sam_grove | 0:42add775212a | 161 | { |
sam_grove | 0:42add775212a | 162 | error("%s %d: MPR121 OOR failure - 0x%04x\n", __FILE__, __LINE__, oor_val); |
sam_grove | 0:42add775212a | 163 | } |
sam_grove | 0:42add775212a | 164 | |
sam_grove | 0:42add775212a | 165 | _button = reg_val; |
sam_grove | 0:42add775212a | 166 | _button_has_changed = 1; |
sam_grove | 0:42add775212a | 167 | |
sam_grove | 0:42add775212a | 168 | return; |
sam_grove | 0:42add775212a | 169 | } |
sam_grove | 0:42add775212a | 170 | |
sam_grove | 0:42add775212a | 171 | void MPR121::writeRegister(uint8_t reg, uint8_t data) |
sam_grove | 0:42add775212a | 172 | { |
sam_grove | 0:42add775212a | 173 | char buf[2] = {reg, data}; |
sam_grove | 0:42add775212a | 174 | uint8_t result = 0; |
sam_grove | 0:42add775212a | 175 | |
sam_grove | 0:42add775212a | 176 | __disable_irq(); // Tickers and other timebase events can jack up the I2C bus |
sam_grove | 0:42add775212a | 177 | result = _i2c->write(_i2c_addr, buf, 2); |
sam_grove | 0:42add775212a | 178 | __enable_irq(); // Just need to block during the transaction |
sam_grove | 0:42add775212a | 179 | |
sam_grove | 0:42add775212a | 180 | if(0 != result) |
sam_grove | 0:42add775212a | 181 | { |
sam_grove | 0:42add775212a | 182 | error("%s %d: I2c write failed\n", __FILE__, __LINE__); |
sam_grove | 0:42add775212a | 183 | } |
sam_grove | 0:42add775212a | 184 | |
sam_grove | 0:42add775212a | 185 | return; |
sam_grove | 0:42add775212a | 186 | } |
sam_grove | 0:42add775212a | 187 | |
sam_grove | 0:42add775212a | 188 | uint8_t MPR121::readRegister(uint8_t reg) |
sam_grove | 0:42add775212a | 189 | { |
sam_grove | 0:42add775212a | 190 | uint8_t result = 1, data = 0; |
sam_grove | 0:42add775212a | 191 | |
sam_grove | 0:42add775212a | 192 | __disable_irq(); // Tickers and other timebase events can jack up the I2C bus |
sam_grove | 0:42add775212a | 193 | _i2c->start(); |
sam_grove | 0:42add775212a | 194 | result &= _i2c->write(_i2c_addr); |
sam_grove | 0:42add775212a | 195 | result &= _i2c->write(reg); |
sam_grove | 0:42add775212a | 196 | // issue a repeated start... |
sam_grove | 0:42add775212a | 197 | _i2c->start(); |
sam_grove | 0:42add775212a | 198 | result &= _i2c->write(_i2c_addr | 0x01); |
sam_grove | 0:42add775212a | 199 | // read with nak |
sam_grove | 0:42add775212a | 200 | data = _i2c->read(0); |
sam_grove | 0:42add775212a | 201 | _i2c->stop(); |
sam_grove | 0:42add775212a | 202 | __enable_irq(); // Just need to block during the transaction |
sam_grove | 0:42add775212a | 203 | |
sam_grove | 0:42add775212a | 204 | if(1 != result) |
sam_grove | 0:42add775212a | 205 | { |
sam_grove | 0:42add775212a | 206 | error("%s %d: I2C read failed\n", __FILE__, __LINE__); |
sam_grove | 0:42add775212a | 207 | } |
sam_grove | 0:42add775212a | 208 | |
sam_grove | 0:42add775212a | 209 | return data; |
sam_grove | 0:42add775212a | 210 | } |