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:
Thu Mar 07 19:25:08 2013 +0000
Revision:
0:42add775212a
Child:
1:cee45334b36a
Working baseline example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:42add775212a 1 /**
sam_grove 0:42add775212a 2 * @file MPR121.h
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 #ifndef MPR121_H
sam_grove 0:42add775212a 24 #define MPR121_H
sam_grove 0:42add775212a 25
sam_grove 0:42add775212a 26 #include "mbed.h"
sam_grove 0:42add775212a 27
sam_grove 0:42add775212a 28 /** Using the Sparkfun SEN-10250
sam_grove 0:42add775212a 29 *
sam_grove 0:42add775212a 30 * Example:
sam_grove 0:42add775212a 31 * @code
sam_grove 0:42add775212a 32 * #include "mbed.h"
sam_grove 0:42add775212a 33 * #include "MPR121.h"
sam_grove 0:42add775212a 34 *
sam_grove 0:42add775212a 35 * DigitalOut myled(LED1);
sam_grove 0:42add775212a 36 * Serial pc(USBTX, USBRX);
sam_grove 0:42add775212a 37 *
sam_grove 0:42add775212a 38 * I2C i2c(p28, p27);
sam_grove 0:42add775212a 39 * InterruptIn irq(p26);
sam_grove 0:42add775212a 40 * MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
sam_grove 0:42add775212a 41 *
sam_grove 0:42add775212a 42 * int main()
sam_grove 0:42add775212a 43 * {
sam_grove 0:42add775212a 44 * pc.baud(921600); // boost the communication speed
sam_grove 0:42add775212a 45 * printf("\033[2J"); // clear the terminal
sam_grove 0:42add775212a 46 * printf("\033[1;1H");// and set the cursor to home
sam_grove 0:42add775212a 47 * wait(0.1f);
sam_grove 0:42add775212a 48 * printf("*******************************************************\n");
sam_grove 0:42add775212a 49 *
sam_grove 0:42add775212a 50 * touch_pad.init();
sam_grove 0:42add775212a 51 * touch_pad.enable();
sam_grove 0:42add775212a 52 *
sam_grove 0:42add775212a 53 * while(1)
sam_grove 0:42add775212a 54 * {
sam_grove 0:42add775212a 55 * if(touch_pad.isPressed())
sam_grove 0:42add775212a 56 * {
sam_grove 0:42add775212a 57 * uint16_t button_val = touch_pad.buttonPressed();
sam_grove 0:42add775212a 58 * printf("button = 0x%04x\n", button_val);
sam_grove 0:42add775212a 59 * myled = (button_val>0) ? 1 : 0;
sam_grove 0:42add775212a 60 * }
sam_grove 0:42add775212a 61 * }
sam_grove 0:42add775212a 62 * }
sam_grove 0:42add775212a 63 * @endcode
sam_grove 0:42add775212a 64 */
sam_grove 0:42add775212a 65
sam_grove 0:42add775212a 66 /**
sam_grove 0:42add775212a 67 * @class MPR121
sam_grove 0:42add775212a 68 * @brief API abstraction for the MPR121 capacitive touch IC
sam_grove 0:42add775212a 69 */
sam_grove 0:42add775212a 70 class MPR121
sam_grove 0:42add775212a 71 {
sam_grove 0:42add775212a 72 private:
sam_grove 0:42add775212a 73
sam_grove 0:42add775212a 74 I2C *_i2c;
sam_grove 0:42add775212a 75 InterruptIn *_irq;
sam_grove 0:42add775212a 76 uint8_t _i2c_addr;
sam_grove 0:42add775212a 77 volatile uint16_t _button;
sam_grove 0:42add775212a 78 volatile uint32_t _button_has_changed;
sam_grove 0:42add775212a 79
sam_grove 0:42add775212a 80 /** The interrupt handler for the IRQ pin
sam_grove 0:42add775212a 81 */
sam_grove 0:42add775212a 82 void handler(void);
sam_grove 0:42add775212a 83
sam_grove 0:42add775212a 84 public:
sam_grove 0:42add775212a 85
sam_grove 0:42add775212a 86 /**
sam_grove 0:42add775212a 87 * @enum MPR121_ADDR
sam_grove 0:42add775212a 88 * @brief Possible terminations for the ADDR pin
sam_grove 0:42add775212a 89 */
sam_grove 0:42add775212a 90 enum MPR121_ADDR
sam_grove 0:42add775212a 91 {
sam_grove 0:42add775212a 92 ADDR_VSS = 0x5A, /*!< ADDR connected to VSS */
sam_grove 0:42add775212a 93 ADDR_VDD, /*!< ADDR connected to VDD */
sam_grove 0:42add775212a 94 ADDR_SCL, /*!< ADDR connected to SDA */
sam_grove 0:42add775212a 95 ADDR_SDA /*!< ADDR connected to SCL */
sam_grove 0:42add775212a 96 };
sam_grove 0:42add775212a 97
sam_grove 0:42add775212a 98 /**
sam_grove 0:42add775212a 99 * @enum MPR121_REGISTER
sam_grove 0:42add775212a 100 * @brief The device register map
sam_grove 0:42add775212a 101 */
sam_grove 0:42add775212a 102 enum MPR121_REGISTER
sam_grove 0:42add775212a 103 {
sam_grove 0:42add775212a 104 ELE0_7_STAT = 0x00,
sam_grove 0:42add775212a 105 ELE8_11_STAT, ELE0_7_OOR_STAT, ELE8_11_OOR_STAT, EFD0LB, EFD0HB,
sam_grove 0:42add775212a 106 EFD1LB, EFD1HB, EFD2LB, EFD2HB, EFD3LB, EFD3HB, EFD4LB, EFD4HB, EFD5LB, EFD5HB,
sam_grove 0:42add775212a 107
sam_grove 0:42add775212a 108 EFD6LB = 0x10,
sam_grove 0:42add775212a 109 EFD6HB, EFD7LB, EFD7HB, EFD8LB, EFD8HB, EFD9LB, EFD9HB, EFD10LB,
sam_grove 0:42add775212a 110 EFD10HB, EFD11LB, EFD11HB, EFDPROXLB, EFDPROXHB, E0BV, E1BV,
sam_grove 0:42add775212a 111
sam_grove 0:42add775212a 112 E2BV = 0x20,
sam_grove 0:42add775212a 113 E3BV, E4BV, E5BV, E6BV, E7BV, E8BV, E9BV, E10BV, E11BV, EPROXBV,
sam_grove 0:42add775212a 114 MHDR, NHDR, NCLR, FDLR, MHDF,
sam_grove 0:42add775212a 115
sam_grove 0:42add775212a 116 NHDF = 0x30,
sam_grove 0:42add775212a 117 NCLF, FDLF, NHDT, NCLT, FDLT, MHDPROXR, NHDPROXR, NCLPROXR,
sam_grove 0:42add775212a 118 FDLPROXR, MHDPROXF, NHDPROXF, NCLPROXF, FDLPROXF, NHDPROXT, NCLPROXT,
sam_grove 0:42add775212a 119
sam_grove 0:42add775212a 120 FDLPROXT = 0x40,
sam_grove 0:42add775212a 121 E0TTH, E0RTH, E1TTH, E1RTH, E2TTH, E2RTH, E3TTH, E3RTH,
sam_grove 0:42add775212a 122 E4TTH, E4RTH, E5TTH, E5RTH, E6TTH, E6RTH, E7TTH,
sam_grove 0:42add775212a 123
sam_grove 0:42add775212a 124 E7RTH = 0x50,
sam_grove 0:42add775212a 125 E8TTH, E8RTH, E9TTH, E9RTH, E10TTH, E10RTH, E11TTH, E11RTH,
sam_grove 0:42add775212a 126 EPROXTTH, EPROXRTH, DT_DR, CDC_CONFIG, CDT_CONFIG, ECR, CDC0,
sam_grove 0:42add775212a 127
sam_grove 0:42add775212a 128 CDC1 = 0x60,
sam_grove 0:42add775212a 129 CDC2, CDC3, CDC4, CDC5, CDC6, CDC7, CDC8, CDC9, CDC10, CDC11, CDCPROX, CDT0_CDT1,
sam_grove 0:42add775212a 130 CDT2_CDT3, CDT4_CDT5, CDT6_CDT7,
sam_grove 0:42add775212a 131
sam_grove 0:42add775212a 132 CDT8_CDT9 = 0x70,
sam_grove 0:42add775212a 133 CDT10_CDT11, CDTPROX, GPIO_CTRL0, GPIO_CTRL1, GPIO_DATA, GPIO_DIR, GPIO_EN, GPIO_SET,
sam_grove 0:42add775212a 134 GPIO_CLR, GPIO_TOGGLE, AUTO_CFG0, AUTO_CFG1, USL, LSL, TL,
sam_grove 0:42add775212a 135
sam_grove 0:42add775212a 136 SRST = 0x80
sam_grove 0:42add775212a 137 };
sam_grove 0:42add775212a 138
sam_grove 0:42add775212a 139 /** Create the MPR121 object
sam_grove 0:42add775212a 140 * @param i2c A defined I2C object
sam_grove 0:42add775212a 141 * @param pin A defined InterruptIn object
sam_grove 0:42add775212a 142 * @param i2c_addr Connection of the address line
sam_grove 0:42add775212a 143 */
sam_grove 0:42add775212a 144 MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr);
sam_grove 0:42add775212a 145
sam_grove 0:42add775212a 146 /** Clear state vars and initilize the dependant objects
sam_grove 0:42add775212a 147 */
sam_grove 0:42add775212a 148 void init(void);
sam_grove 0:42add775212a 149
sam_grove 0:42add775212a 150 /** Allow the IC to run and collect user input
sam_grove 0:42add775212a 151 */
sam_grove 0:42add775212a 152 void enable(void);
sam_grove 0:42add775212a 153
sam_grove 0:42add775212a 154 /** Stop the IC and put into low power mode
sam_grove 0:42add775212a 155 */
sam_grove 0:42add775212a 156 void disable(void);
sam_grove 0:42add775212a 157
sam_grove 0:42add775212a 158 /** Determine if a new button press event occured
sam_grove 0:42add775212a 159 * Upon calling the state is cleared until another press is detected
sam_grove 0:42add775212a 160 * @return 1 if a press has been detected since the last call, 0 otherwise
sam_grove 0:42add775212a 161 */
sam_grove 0:42add775212a 162 uint32_t isPressed(void);
sam_grove 0:42add775212a 163
sam_grove 0:42add775212a 164 /** Get the electrode status (ELE12 ... ELE0 -> b15 xxx b11 ... b0
sam_grove 0:42add775212a 165 * The buttons are bit mapped. ELE0 = b0 ... ELE11 = b11 b12 ... b15 undefined
sam_grove 0:42add775212a 166 * @return The state of all buttons
sam_grove 0:42add775212a 167 */
sam_grove 0:42add775212a 168 uint16_t buttonPressed(void);
sam_grove 0:42add775212a 169
sam_grove 0:42add775212a 170 /** print the register map and values to the console
sam_grove 0:42add775212a 171 */
sam_grove 0:42add775212a 172 void registerDump(void);
sam_grove 0:42add775212a 173
sam_grove 0:42add775212a 174 /** Write to a register (exposed for debugging reasons)
sam_grove 0:42add775212a 175 * Note: most writes are only valid in stop mode
sam_grove 0:42add775212a 176 * @param reg The register to be written
sam_grove 0:42add775212a 177 * @param data The data to be written
sam_grove 0:42add775212a 178 */
sam_grove 0:42add775212a 179 void writeRegister(uint8_t reg, uint8_t data);
sam_grove 0:42add775212a 180
sam_grove 0:42add775212a 181 /** Read from a register (exposed for debugging reasons)
sam_grove 0:42add775212a 182 * @param reg The register to read from
sam_grove 0:42add775212a 183 * @return The register contents
sam_grove 0:42add775212a 184 */
sam_grove 0:42add775212a 185 uint8_t readRegister(uint8_t reg);
sam_grove 0:42add775212a 186
sam_grove 0:42add775212a 187 };
sam_grove 0:42add775212a 188
sam_grove 0:42add775212a 189 #endif