Start of a microbit mpr121 library

Dependents:   microbitmpr121-example

Committer:
owenbrotherwood
Date:
Mon Jan 16 23:02:19 2017 +0000
Revision:
8:cda72c6b04c0
Parent:
6:103a5a2ca571
ok;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
owenbrotherwood 4:f63476855239 1 /*
owenbrotherwood 4:f63476855239 2 The MIT License (MIT)
owenbrotherwood 1:f6fed00a3ff2 3
owenbrotherwood 4:f63476855239 4 Copyright (c) 2016 British Broadcasting Corporation.
owenbrotherwood 4:f63476855239 5 This software is provided by Lancaster University by arrangement with the BBC.
owenbrotherwood 4:f63476855239 6
owenbrotherwood 4:f63476855239 7 Permission is hereby granted, free of charge, to any person obtaining a
owenbrotherwood 4:f63476855239 8 copy of this software and associated documentation files (the "Software"),
owenbrotherwood 4:f63476855239 9 to deal in the Software without restriction, including without limitation
owenbrotherwood 4:f63476855239 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
owenbrotherwood 4:f63476855239 11 and/or sell copies of the Software, and to permit persons to whom the
owenbrotherwood 4:f63476855239 12 Software is furnished to do so, subject to the following conditions:
owenbrotherwood 4:f63476855239 13
owenbrotherwood 4:f63476855239 14 The above copyright notice and this permission notice shall be included in
owenbrotherwood 4:f63476855239 15 all copies or substantial portions of the Software.
owenbrotherwood 0:fb4572fc4901 16
owenbrotherwood 4:f63476855239 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
owenbrotherwood 4:f63476855239 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
owenbrotherwood 4:f63476855239 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
owenbrotherwood 4:f63476855239 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
owenbrotherwood 4:f63476855239 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
owenbrotherwood 4:f63476855239 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
owenbrotherwood 4:f63476855239 23 DEALINGS IN THE SOFTWARE.
owenbrotherwood 4:f63476855239 24 */
owenbrotherwood 4:f63476855239 25
owenbrotherwood 4:f63476855239 26 #ifndef MICROBIT_MPR121_H
owenbrotherwood 4:f63476855239 27 #define MICROBIT_MPR121_H
owenbrotherwood 4:f63476855239 28
owenbrotherwood 4:f63476855239 29 #include "mbed.h"
owenbrotherwood 4:f63476855239 30 #include "MicroBitConfig.h"
owenbrotherwood 4:f63476855239 31 #include "MicroBitPin.h"
owenbrotherwood 4:f63476855239 32 #include "MicroBitComponent.h"
owenbrotherwood 4:f63476855239 33 #include "MicroBitI2C.h"
owenbrotherwood 1:f6fed00a3ff2 34
owenbrotherwood 4:f63476855239 35 /**
owenbrotherwood 4:f63476855239 36 * Relevant pin assignments
owenbrotherwood 4:f63476855239 37 */
owenbrotherwood 4:f63476855239 38 #define MPR121_DEFAULT_INT MICROBIT_PIN_P16
owenbrotherwood 4:f63476855239 39
owenbrotherwood 4:f63476855239 40 /**
owenbrotherwood 4:f63476855239 41 * I2C constants
owenbrotherwood 4:f63476855239 42 */
owenbrotherwood 5:4a8384331ca7 43 #define MPR121_DEFAULT_ADDR 0x5A
owenbrotherwood 4:f63476855239 44
owenbrotherwood 4:f63476855239 45
owenbrotherwood 4:f63476855239 46 /**
owenbrotherwood 4:f63476855239 47 * Status Bits
owenbrotherwood 4:f63476855239 48 */
owenbrotherwood 4:f63476855239 49
owenbrotherwood 1:f6fed00a3ff2 50
owenbrotherwood 4:f63476855239 51 /**
owenbrotherwood 4:f63476855239 52 * MPR121 MAGIC ID value
owenbrotherwood 4:f63476855239 53 * Returned from the MPR121_WHO_AM_I register for ID purposes.
owenbrotherwood 4:f63476855239 54 */
owenbrotherwood 5:4a8384331ca7 55 #define MPR121_WHOAMI_VAL 0X9A //TODO
owenbrotherwood 4:f63476855239 56
owenbrotherwood 4:f63476855239 57 /**
owenbrotherwood 4:f63476855239 58 * Class definition for MicroBit Mpr121.
owenbrotherwood 4:f63476855239 59 *
owenbrotherwood 4:f63476855239 60 * Represents an implementation of the Mpr121.
owenbrotherwood 4:f63476855239 61 * Also includes ...
owenbrotherwood 4:f63476855239 62 */
owenbrotherwood 4:f63476855239 63 class MicroBitMpr121 : public MicroBitComponent
owenbrotherwood 4:f63476855239 64 {
owenbrotherwood 4:f63476855239 65 uint16_t address; // I2C address of the magnetmometer.
owenbrotherwood 4:f63476855239 66
owenbrotherwood 4:f63476855239 67 DigitalIn int1; // Data ready interrupt.
owenbrotherwood 4:f63476855239 68 MicroBitI2C& i2c; // The I2C interface the sensor is connected to.
owenbrotherwood 0:fb4572fc4901 69
owenbrotherwood 6:103a5a2ca571 70 volatile uint16_t _button;
owenbrotherwood 6:103a5a2ca571 71 volatile uint32_t _button_has_changed;
owenbrotherwood 6:103a5a2ca571 72
owenbrotherwood 6:103a5a2ca571 73 /** The interrupt handler for the IRQ pin
owenbrotherwood 6:103a5a2ca571 74 */
owenbrotherwood 6:103a5a2ca571 75 void handler(void);
owenbrotherwood 6:103a5a2ca571 76
owenbrotherwood 0:fb4572fc4901 77 public:
owenbrotherwood 1:f6fed00a3ff2 78
owenbrotherwood 0:fb4572fc4901 79 /**
owenbrotherwood 6:103a5a2ca571 80 * @enum MPR121_ADDR
owenbrotherwood 6:103a5a2ca571 81 * @brief Possible terminations for the ADDR pin
owenbrotherwood 6:103a5a2ca571 82 */
owenbrotherwood 6:103a5a2ca571 83 enum MPR121_ADDR {
owenbrotherwood 6:103a5a2ca571 84 ADDR_VSS = 0x5A, /*!< ADDR connected to VSS */
owenbrotherwood 6:103a5a2ca571 85 ADDR_VDD, /*!< ADDR connected to VDD */
owenbrotherwood 6:103a5a2ca571 86 ADDR_SCL, /*!< ADDR connected to SDA */
owenbrotherwood 6:103a5a2ca571 87 ADDR_SDA /*!< ADDR connected to SCL */
owenbrotherwood 6:103a5a2ca571 88 };
owenbrotherwood 6:103a5a2ca571 89
owenbrotherwood 6:103a5a2ca571 90 /**
owenbrotherwood 6:103a5a2ca571 91 * @enum MPR121_REGISTER
owenbrotherwood 6:103a5a2ca571 92 * @brief The device register map
owenbrotherwood 6:103a5a2ca571 93 */
owenbrotherwood 6:103a5a2ca571 94 enum MPR121_REGISTER {
owenbrotherwood 6:103a5a2ca571 95 ELE0_7_STAT = 0x00,
owenbrotherwood 6:103a5a2ca571 96 ELE8_11_STAT, ELE0_7_OOR_STAT, ELE8_11_OOR_STAT, EFD0LB, EFD0HB,
owenbrotherwood 6:103a5a2ca571 97 EFD1LB, EFD1HB, EFD2LB, EFD2HB, EFD3LB, EFD3HB, EFD4LB, EFD4HB, EFD5LB, EFD5HB,
owenbrotherwood 6:103a5a2ca571 98
owenbrotherwood 6:103a5a2ca571 99 EFD6LB = 0x10,
owenbrotherwood 6:103a5a2ca571 100 EFD6HB, EFD7LB, EFD7HB, EFD8LB, EFD8HB, EFD9LB, EFD9HB, EFD10LB,
owenbrotherwood 6:103a5a2ca571 101 EFD10HB, EFD11LB, EFD11HB, EFDPROXLB, EFDPROXHB, E0BV, E1BV,
owenbrotherwood 6:103a5a2ca571 102
owenbrotherwood 6:103a5a2ca571 103 E2BV = 0x20,
owenbrotherwood 6:103a5a2ca571 104 E3BV, E4BV, E5BV, E6BV, E7BV, E8BV, E9BV, E10BV, E11BV, EPROXBV,
owenbrotherwood 6:103a5a2ca571 105 MHDR, NHDR, NCLR, FDLR, MHDF,
owenbrotherwood 6:103a5a2ca571 106
owenbrotherwood 6:103a5a2ca571 107 NHDF = 0x30,
owenbrotherwood 6:103a5a2ca571 108 NCLF, FDLF, NHDT, NCLT, FDLT, MHDPROXR, NHDPROXR, NCLPROXR,
owenbrotherwood 6:103a5a2ca571 109 FDLPROXR, MHDPROXF, NHDPROXF, NCLPROXF, FDLPROXF, NHDPROXT, NCLPROXT,
owenbrotherwood 6:103a5a2ca571 110
owenbrotherwood 6:103a5a2ca571 111 FDLPROXT = 0x40,
owenbrotherwood 6:103a5a2ca571 112 E0TTH, E0RTH, E1TTH, E1RTH, E2TTH, E2RTH, E3TTH, E3RTH,
owenbrotherwood 6:103a5a2ca571 113 E4TTH, E4RTH, E5TTH, E5RTH, E6TTH, E6RTH, E7TTH,
owenbrotherwood 6:103a5a2ca571 114
owenbrotherwood 6:103a5a2ca571 115 E7RTH = 0x50,
owenbrotherwood 6:103a5a2ca571 116 E8TTH, E8RTH, E9TTH, E9RTH, E10TTH, E10RTH, E11TTH, E11RTH,
owenbrotherwood 6:103a5a2ca571 117 EPROXTTH, EPROXRTH, DT_DR, CDC_CONFIG, CDT_CONFIG, ECR, CDC0,
owenbrotherwood 6:103a5a2ca571 118
owenbrotherwood 6:103a5a2ca571 119 CDC1 = 0x60,
owenbrotherwood 6:103a5a2ca571 120 CDC2, CDC3, CDC4, CDC5, CDC6, CDC7, CDC8, CDC9, CDC10, CDC11, CDCPROX, CDT0_CDT1,
owenbrotherwood 6:103a5a2ca571 121 CDT2_CDT3, CDT4_CDT5, CDT6_CDT7,
owenbrotherwood 6:103a5a2ca571 122
owenbrotherwood 6:103a5a2ca571 123 CDT8_CDT9 = 0x70,
owenbrotherwood 6:103a5a2ca571 124 CDT10_CDT11, CDTPROX, GPIO_CTRL0, GPIO_CTRL1, GPIO_DATA, GPIO_DIR, GPIO_EN, GPIO_SET,
owenbrotherwood 6:103a5a2ca571 125 GPIO_CLR, GPIO_TOGGLE, AUTO_CFG0, AUTO_CFG1, USL, LSL, TL,
owenbrotherwood 6:103a5a2ca571 126
owenbrotherwood 6:103a5a2ca571 127 SRST = 0x80
owenbrotherwood 6:103a5a2ca571 128 };
owenbrotherwood 6:103a5a2ca571 129
owenbrotherwood 6:103a5a2ca571 130 /**
owenbrotherwood 4:f63476855239 131 * Constructor.
owenbrotherwood 4:f63476855239 132 * Create a software representation of a mpr121.
owenbrotherwood 4:f63476855239 133 *
owenbrotherwood 4:f63476855239 134 * @param _i2c an instance of i2c, which the mpr121 is accessible from.
owenbrotherwood 4:f63476855239 135 *
owenbrotherwood 4:f63476855239 136 * @param address the address for the mpr121 register on the i2c bus. Defaults to MPR121_DEFAULT_ADDR.
owenbrotherwood 4:f63476855239 137 *
owenbrotherwood 4:f63476855239 138 * @param id the ID of the new MicroBitMpr121 object. Defaults to MPR121_DEFAULT_ADDR.
owenbrotherwood 4:f63476855239 139 *
owenbrotherwood 4:f63476855239 140 * @code
owenbrotherwood 4:f63476855239 141 * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
owenbrotherwood 4:f63476855239 142 *
owenbrotherwood 4:f63476855239 143 * MicroBitMpr121 mpr121(i2c);
owenbrotherwood 4:f63476855239 144 * @endcode
owenbrotherwood 4:f63476855239 145 */
owenbrotherwood 5:4a8384331ca7 146 MicroBitMpr121(MicroBitI2C& _i2c, uint16_t address = MPR121_DEFAULT_ADDR, DigitalIn interupt = MICROBIT_PIN_P16, uint16_t id = MPR121_DEFAULT_ADDR);
owenbrotherwood 4:f63476855239 147
owenbrotherwood 4:f63476855239 148 /**
owenbrotherwood 6:103a5a2ca571 149 * Attempts to read the 8 bit ID for validation purposes. TODO IF RELEVANT
owenbrotherwood 4:f63476855239 150 *
owenbrotherwood 6:103a5a2ca571 151 * @return the 8 bit ID, or MICROBIT_I2C_ERROR if the request fails.
owenbrotherwood 4:f63476855239 152 *
owenbrotherwood 4:f63476855239 153 * @code
owenbrotherwood 6:103a5a2ca571 154 * mrp121.whoAmI();
owenbrotherwood 4:f63476855239 155 * @endcode
owenbrotherwood 4:f63476855239 156 */
owenbrotherwood 4:f63476855239 157 int whoAmI();
owenbrotherwood 4:f63476855239 158
owenbrotherwood 4:f63476855239 159 /**
owenbrotherwood 6:103a5a2ca571 160 * Allow the IC to run and collect user input
owenbrotherwood 4:f63476855239 161 */
owenbrotherwood 6:103a5a2ca571 162 void enable(void);
owenbrotherwood 6:103a5a2ca571 163
owenbrotherwood 6:103a5a2ca571 164 /** print the register map and values to the console
owenbrotherwood 6:103a5a2ca571 165 */
owenbrotherwood 8:cda72c6b04c0 166 void registerDump();
owenbrotherwood 1:f6fed00a3ff2 167
owenbrotherwood 0:fb4572fc4901 168 /**
owenbrotherwood 6:103a5a2ca571 169 * Stop the IC and put into low power mode
owenbrotherwood 4:f63476855239 170 */
owenbrotherwood 6:103a5a2ca571 171 void disable(void);
owenbrotherwood 1:f6fed00a3ff2 172
owenbrotherwood 4:f63476855239 173 /**
owenbrotherwood 4:f63476855239 174 * Periodic callback from MicroBit idle thread.
owenbrotherwood 4:f63476855239 175 *
owenbrotherwood 4:f63476855239 176 * Calls updateSample().
owenbrotherwood 4:f63476855239 177 */
owenbrotherwood 4:f63476855239 178 virtual void idleTick();
owenbrotherwood 1:f6fed00a3ff2 179
owenbrotherwood 1:f6fed00a3ff2 180
owenbrotherwood 4:f63476855239 181 /**
owenbrotherwood 4:f63476855239 182 * Destructor for MicroBitMpr121, where we deregister this instance from the array of fiber components.
owenbrotherwood 4:f63476855239 183 */
owenbrotherwood 4:f63476855239 184 ~MicroBitMpr121();
owenbrotherwood 1:f6fed00a3ff2 185
owenbrotherwood 4:f63476855239 186 private:
owenbrotherwood 2:4e130924a398 187
owenbrotherwood 4:f63476855239 188 /**
owenbrotherwood 4:f63476855239 189 * Issues a standard, 2 byte I2C command write to the accelerometer.
owenbrotherwood 4:f63476855239 190 *
owenbrotherwood 4:f63476855239 191 * Blocks the calling thread until complete.
owenbrotherwood 4:f63476855239 192 *
owenbrotherwood 4:f63476855239 193 * @param reg The address of the register to write to.
owenbrotherwood 4:f63476855239 194 *
owenbrotherwood 4:f63476855239 195 * @param value The value to write.
owenbrotherwood 4:f63476855239 196 *
owenbrotherwood 4:f63476855239 197 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the the write request failed.
owenbrotherwood 4:f63476855239 198 */
owenbrotherwood 4:f63476855239 199 int writeCommand(uint8_t reg, uint8_t value);
owenbrotherwood 2:4e130924a398 200
owenbrotherwood 4:f63476855239 201 /**
owenbrotherwood 4:f63476855239 202 * Issues a read command, copying data into the specified buffer.
owenbrotherwood 4:f63476855239 203 *
owenbrotherwood 4:f63476855239 204 * Blocks the calling thread until complete.
owenbrotherwood 4:f63476855239 205 *
owenbrotherwood 4:f63476855239 206 * @param reg The address of the register to access.
owenbrotherwood 4:f63476855239 207 *
owenbrotherwood 4:f63476855239 208 * @param buffer Memory area to read the data into.
owenbrotherwood 4:f63476855239 209 *
owenbrotherwood 4:f63476855239 210 * @param length The number of bytes to read.
owenbrotherwood 4:f63476855239 211 *
owenbrotherwood 4:f63476855239 212 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the the read request failed.
owenbrotherwood 4:f63476855239 213 */
owenbrotherwood 4:f63476855239 214 int readCommand(uint8_t reg, uint8_t* buffer, int length);
owenbrotherwood 1:f6fed00a3ff2 215
owenbrotherwood 4:f63476855239 216 /**
owenbrotherwood 4:f63476855239 217 * Issues a read of a given address, and returns the value.
owenbrotherwood 4:f63476855239 218 *
owenbrotherwood 4:f63476855239 219 * Blocks the calling thread until complete.
owenbrotherwood 4:f63476855239 220 *
owenbrotherwood 4:f63476855239 221 * @param reg The address of the 16 bit register to access.
owenbrotherwood 4:f63476855239 222 *
owenbrotherwood 6:103a5a2ca571 223 * @return The register value, interpreted as a 16 but signed value, or MICROBIT_I2C_ERROR.
owenbrotherwood 4:f63476855239 224 */
owenbrotherwood 4:f63476855239 225 int read16(uint8_t reg);
owenbrotherwood 1:f6fed00a3ff2 226
owenbrotherwood 4:f63476855239 227 /**
owenbrotherwood 4:f63476855239 228 * Issues a read of a given address, and returns the value.
owenbrotherwood 4:f63476855239 229 *
owenbrotherwood 4:f63476855239 230 * Blocks the calling thread until complete.
owenbrotherwood 4:f63476855239 231 *
owenbrotherwood 4:f63476855239 232 * @param reg The address of the 16 bit register to access.
owenbrotherwood 4:f63476855239 233 *
owenbrotherwood 6:103a5a2ca571 234 * @return The register value, interpreted as a 8 bit unsigned value, or MICROBIT_I2C_ERROR if the read request failed.
owenbrotherwood 4:f63476855239 235 */
owenbrotherwood 4:f63476855239 236 int read8(uint8_t reg);
owenbrotherwood 1:f6fed00a3ff2 237
owenbrotherwood 4:f63476855239 238 /**
owenbrotherwood 6:103a5a2ca571 239 * An initialisation member function.
owenbrotherwood 6:103a5a2ca571 240 *
owenbrotherwood 6:103a5a2ca571 241 * @param id the unique identifier for this instance.
owenbrotherwood 4:f63476855239 242 *
owenbrotherwood 6:103a5a2ca571 243 * @param address the base address on the i2c bus.
owenbrotherwood 4:f63476855239 244 *
owenbrotherwood 6:103a5a2ca571 245 * @param intPin the pin used for interrupt.
owenbrotherwood 4:f63476855239 246 */
owenbrotherwood 4:f63476855239 247 void init(uint16_t id, uint16_t address);
owenbrotherwood 0:fb4572fc4901 248 };
owenbrotherwood 0:fb4572fc4901 249
owenbrotherwood 4:f63476855239 250 #endif