fl@c@ Johnson / Mbed 2 deprecated ramanSpectrometer_interfaceBoard Featured

Dependencies:   SDFileSystem SPI_TFT_ILI9341 mbed

Committer:
flatcat
Date:
Fri Aug 15 10:28:21 2014 +0000
Revision:
0:b513017ee369
http://hackaday.io/project/1279

Who changed what in which revision?

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