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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPR121.h Source File

MPR121.h

Go to the documentation of this file.
00001 /**
00002  * @file    MPR121.h
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 #ifndef MPR121_H
00024 #define MPR121_H
00025 
00026 #include "mbed.h"
00027 
00028 /** Using the Sparkfun SEN-10250 BoB
00029  *
00030  * Example:
00031  * @code
00032  *  #include "mbed.h"
00033  *  #include "MPR121.h"
00034  *  
00035  *  Serial pc(USBTX, USBRX);
00036  *  DigitalOut myled(LED1);
00037  *  
00038  *  #if defined TARGET_LPC1768 || TARGET_LPC11U24
00039  *    I2C i2c(p28, p27);
00040  *    InterruptIn irq(p26);
00041  *    MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
00042  *  
00043  *  #elif defined TARGET_KL25Z
00044  *    I2C i2c(PTC9, PTC8);
00045  *    InterruptIn irq(PTA5);
00046  *    MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
00047  *  
00048  *  #else
00049  *    #error TARGET NOT TESTED
00050  *  #endif
00051  *  
00052  *  int main()  
00053  *  {       
00054  *      touch_pad.init();
00055  *      touch_pad.enable();
00056  *      
00057  *      while(1)
00058  *      {
00059  *          if(touch_pad.isPressed())
00060  *          {
00061  *              uint16_t button_val = touch_pad.buttonPressed();
00062  *              printf("button = 0x%04x\n", button_val);
00063  *              myled = (button_val>0) ? 1 : 0;
00064  *          }            
00065  *      }
00066  *  }
00067  * @endcode
00068  */
00069 
00070 /**
00071  *  @class MPR121
00072  *  @brief API for the MPR121 capacitive touch IC
00073  */ 
00074 class MPR121
00075 {
00076 private:
00077     
00078     I2C         *_i2c;
00079     InterruptIn *_irq;
00080     uint8_t      _i2c_addr;
00081     volatile uint16_t _button;
00082     volatile uint32_t _button_has_changed;
00083     
00084     /** The interrupt handler for the IRQ pin
00085      */
00086     void handler(void);
00087 
00088 public:
00089     
00090     /**
00091      *  @enum MPR121_ADDR
00092      *  @brief Possible terminations for the ADDR pin
00093      */ 
00094     enum MPR121_ADDR
00095     { 
00096         ADDR_VSS  = 0x5A, /*!< ADDR connected to VSS */
00097         ADDR_VDD ,    /*!< ADDR connected to VDD */
00098         ADDR_SCL ,    /*!< ADDR connected to SDA */
00099         ADDR_SDA      /*!< ADDR connected to SCL */
00100     };
00101     
00102     /**
00103      *  @enum MPR121_REGISTER
00104      *  @brief The device register map
00105      */
00106     enum MPR121_REGISTER
00107     {
00108         ELE0_7_STAT = 0x00,
00109         ELE8_11_STAT, ELE0_7_OOR_STAT, ELE8_11_OOR_STAT, EFD0LB, EFD0HB, 
00110         EFD1LB, EFD1HB, EFD2LB, EFD2HB, EFD3LB, EFD3HB, EFD4LB, EFD4HB, EFD5LB, EFD5HB,
00111         
00112         EFD6LB = 0x10,
00113         EFD6HB, EFD7LB, EFD7HB, EFD8LB, EFD8HB, EFD9LB, EFD9HB, EFD10LB,
00114         EFD10HB, EFD11LB, EFD11HB, EFDPROXLB, EFDPROXHB, E0BV, E1BV,
00115         
00116         E2BV = 0x20,
00117         E3BV, E4BV, E5BV, E6BV, E7BV, E8BV, E9BV, E10BV, E11BV, EPROXBV,
00118         MHDR, NHDR, NCLR, FDLR, MHDF, 
00119         
00120         NHDF = 0x30,
00121         NCLF, FDLF, NHDT, NCLT, FDLT, MHDPROXR, NHDPROXR, NCLPROXR,
00122         FDLPROXR, MHDPROXF, NHDPROXF, NCLPROXF, FDLPROXF, NHDPROXT, NCLPROXT,
00123         
00124         FDLPROXT = 0x40,
00125         E0TTH, E0RTH, E1TTH, E1RTH, E2TTH, E2RTH, E3TTH, E3RTH,
00126         E4TTH, E4RTH, E5TTH, E5RTH, E6TTH, E6RTH, E7TTH,
00127         
00128         E7RTH = 0x50,
00129         E8TTH, E8RTH, E9TTH, E9RTH, E10TTH, E10RTH, E11TTH, E11RTH,
00130         EPROXTTH, EPROXRTH, DT_DR, CDC_CONFIG, CDT_CONFIG, ECR, CDC0,
00131         
00132         CDC1 = 0x60,
00133         CDC2, CDC3, CDC4, CDC5, CDC6, CDC7, CDC8, CDC9, CDC10, CDC11, CDCPROX, CDT0_CDT1,
00134         CDT2_CDT3, CDT4_CDT5, CDT6_CDT7, 
00135         
00136         CDT8_CDT9 = 0x70,
00137         CDT10_CDT11, CDTPROX, GPIO_CTRL0, GPIO_CTRL1, GPIO_DATA, GPIO_DIR, GPIO_EN, GPIO_SET,
00138         GPIO_CLR, GPIO_TOGGLE, AUTO_CFG0, AUTO_CFG1, USL, LSL, TL,
00139         
00140         SRST = 0x80
00141     };
00142     
00143     /** Create the MPR121 object
00144      *  @param i2c - A defined I2C object
00145      *  @param pin - A defined InterruptIn object
00146      *  @param i2c_addr - Connection of the address line
00147      */    
00148     MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr);
00149     
00150     /** Clear state variables and initilize the dependant objects
00151      */
00152     void init(void);
00153     
00154     /** Allow the IC to run and collect user input
00155      */
00156     void enable(void);
00157     
00158     /** Stop the IC and put into low power mode
00159      */
00160     void disable(void);
00161     
00162     /** Determine if a new button press event occured
00163      *  Upon calling the state is cleared until another press is detected
00164      *  @return 1 if a press has been detected since the last call, 0 otherwise
00165      */
00166     uint32_t isPressed(void);
00167     
00168     /** Get the electrode status (ELE12 ... ELE0 -> b15 xxx b11 ... b0
00169      *  The buttons are bit mapped. ELE0 = b0 ... ELE11 = b11 b12 ... b15 undefined
00170      *  @return The state of all buttons
00171      */
00172     uint16_t buttonPressed(void);
00173     
00174     /** print the register map and values to the console
00175      *  @param obj - a Serial object that prints to a console
00176      */
00177     void registerDump(Serial &obj) const;
00178     
00179     /** Write to a register (exposed for debugging reasons)
00180      *  Note: most writes are only valid in stop mode
00181      *  @param reg - The register to be written
00182      *  @param data - The data to be written
00183      */
00184     void writeRegister(MPR121_REGISTER const reg, uint8_t const data) const;
00185     
00186     /** Read from a register (exposed for debugging reasons)
00187      *  @param reg - The register to read from
00188      *  @return The register contents
00189      */
00190     uint8_t readRegister(MPR121_REGISTER const reg) const;
00191     
00192 };
00193 
00194 #endif
00195