This is the test firmware for the interfaceBoard for the DIY 3D Printable Raspberry Pi Raman Spectrometer For more details visit http://hackaday.io/project/1279

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