A library with drivers for different peripherals on the LPC4088 QuickStart Board or related add-on boards.

Dependencies:   FATFileSystem

Dependents:   LPC4088test LPC4088test_ledonly LPC4088test_deleteall LPC4088_RAMtest ... more

Committer:
embeddedartists
Date:
Wed Jun 10 08:34:09 2015 +0000
Revision:
20:e1e36493f347
Parent:
12:15597e45eea0
Fixed compiler error in MCIFileSystem regarding us_ticker_api.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 12:15597e45eea0 1 /*
embeddedartists 12:15597e45eea0 2 * Copyright 2013 Embedded Artists AB
embeddedartists 12:15597e45eea0 3 *
embeddedartists 12:15597e45eea0 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 12:15597e45eea0 5 * you may not use this file except in compliance with the License.
embeddedartists 12:15597e45eea0 6 * You may obtain a copy of the License at
embeddedartists 12:15597e45eea0 7 *
embeddedartists 12:15597e45eea0 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 12:15597e45eea0 9 *
embeddedartists 12:15597e45eea0 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 12:15597e45eea0 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 12:15597e45eea0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 12:15597e45eea0 13 * See the License for the specific language governing permissions and
embeddedartists 12:15597e45eea0 14 * limitations under the License.
embeddedartists 12:15597e45eea0 15 */
embeddedartists 4:b32cf4ef45c5 16
embeddedartists 4:b32cf4ef45c5 17 /******************************************************************************
embeddedartists 4:b32cf4ef45c5 18 * Includes
embeddedartists 4:b32cf4ef45c5 19 *****************************************************************************/
embeddedartists 4:b32cf4ef45c5 20
embeddedartists 4:b32cf4ef45c5 21 #include "mbed.h"
embeddedartists 4:b32cf4ef45c5 22 #include "mbed_debug.h"
embeddedartists 4:b32cf4ef45c5 23
embeddedartists 4:b32cf4ef45c5 24 #include "MMA7455.h"
embeddedartists 4:b32cf4ef45c5 25
embeddedartists 4:b32cf4ef45c5 26 /******************************************************************************
embeddedartists 4:b32cf4ef45c5 27 * Defines and typedefs
embeddedartists 4:b32cf4ef45c5 28 *****************************************************************************/
embeddedartists 4:b32cf4ef45c5 29
embeddedartists 4:b32cf4ef45c5 30 #define MMA7455_I2C_ADDR (0x1D << 1)
embeddedartists 4:b32cf4ef45c5 31
embeddedartists 4:b32cf4ef45c5 32 #define MMA7455_ADDR_XOUTL 0x00
embeddedartists 4:b32cf4ef45c5 33 #define MMA7455_ADDR_XOUTH 0x01
embeddedartists 4:b32cf4ef45c5 34 #define MMA7455_ADDR_YOUTL 0x02
embeddedartists 4:b32cf4ef45c5 35 #define MMA7455_ADDR_YOUTH 0x03
embeddedartists 4:b32cf4ef45c5 36 #define MMA7455_ADDR_ZOUTL 0x04
embeddedartists 4:b32cf4ef45c5 37 #define MMA7455_ADDR_ZOUTH 0x05
embeddedartists 4:b32cf4ef45c5 38 #define MMA7455_ADDR_XOUT8 0x06
embeddedartists 4:b32cf4ef45c5 39 #define MMA7455_ADDR_YOUT8 0x07
embeddedartists 4:b32cf4ef45c5 40 #define MMA7455_ADDR_ZOUT8 0x08
embeddedartists 4:b32cf4ef45c5 41 #define MMA7455_ADDR_STATUS 0x09
embeddedartists 4:b32cf4ef45c5 42 #define MMA7455_ADDR_DETSRC 0x0A
embeddedartists 4:b32cf4ef45c5 43 #define MMA7455_ADDR_TOUT 0x0B
embeddedartists 4:b32cf4ef45c5 44 #define MMA7455_ADDR_I2CAD 0x0D
embeddedartists 4:b32cf4ef45c5 45 #define MMA7455_ADDR_USRINF 0x0E
embeddedartists 4:b32cf4ef45c5 46 #define MMA7455_ADDR_WHOAMI 0x0F
embeddedartists 4:b32cf4ef45c5 47 #define MMA7455_ADDR_XOFFL 0x10
embeddedartists 4:b32cf4ef45c5 48 #define MMA7455_ADDR_XOFFH 0x11
embeddedartists 4:b32cf4ef45c5 49 #define MMA7455_ADDR_YOFFL 0x12
embeddedartists 4:b32cf4ef45c5 50 #define MMA7455_ADDR_YOFFH 0x13
embeddedartists 4:b32cf4ef45c5 51 #define MMA7455_ADDR_ZOFFL 0x14
embeddedartists 4:b32cf4ef45c5 52 #define MMA7455_ADDR_ZOFFH 0x15
embeddedartists 4:b32cf4ef45c5 53 #define MMA7455_ADDR_MCTL 0x16
embeddedartists 4:b32cf4ef45c5 54 #define MMA7455_ADDR_INTRST 0x17
embeddedartists 4:b32cf4ef45c5 55 #define MMA7455_ADDR_CTL1 0x18
embeddedartists 4:b32cf4ef45c5 56 #define MMA7455_ADDR_CTL2 0x19
embeddedartists 4:b32cf4ef45c5 57 #define MMA7455_ADDR_LDTH 0x1A
embeddedartists 4:b32cf4ef45c5 58 #define MMA7455_ADDR_PDTH 0x1B
embeddedartists 4:b32cf4ef45c5 59 #define MMA7455_ADDR_PW 0x1C
embeddedartists 4:b32cf4ef45c5 60 #define MMA7455_ADDR_LT 0x1D
embeddedartists 4:b32cf4ef45c5 61 #define MMA7455_ADDR_TW 0x1E
embeddedartists 4:b32cf4ef45c5 62
embeddedartists 4:b32cf4ef45c5 63 #define MMA7455_MCTL_MODE(m) ((m) << 0)
embeddedartists 4:b32cf4ef45c5 64 #define MMA7455_MCTL_GLVL(g) ((g) << 2)
embeddedartists 4:b32cf4ef45c5 65
embeddedartists 4:b32cf4ef45c5 66 #define MMA7455_STATUS_DRDY (1 << 0)
embeddedartists 4:b32cf4ef45c5 67 #define MMA7455_STATUS_DOVR (1 << 1)
embeddedartists 4:b32cf4ef45c5 68 #define MMA7455_STATUS_PERR (1 << 2)
embeddedartists 4:b32cf4ef45c5 69
embeddedartists 4:b32cf4ef45c5 70
embeddedartists 4:b32cf4ef45c5 71 MMA7455::MMA7455(PinName sda, PinName scl) : _i2c(sda, scl)
embeddedartists 4:b32cf4ef45c5 72 {
embeddedartists 4:b32cf4ef45c5 73 _mode = ModeStandby;
embeddedartists 4:b32cf4ef45c5 74 _range = Range_8g;
embeddedartists 4:b32cf4ef45c5 75
embeddedartists 4:b32cf4ef45c5 76 _xOff = 0;
embeddedartists 4:b32cf4ef45c5 77 _yOff = 0;
embeddedartists 4:b32cf4ef45c5 78 _zOff = 0;
embeddedartists 4:b32cf4ef45c5 79 }
embeddedartists 4:b32cf4ef45c5 80
embeddedartists 4:b32cf4ef45c5 81 bool MMA7455::setMode(Mode mode) {
embeddedartists 4:b32cf4ef45c5 82 bool result = false;
embeddedartists 4:b32cf4ef45c5 83 int mCtrl = 0;
embeddedartists 4:b32cf4ef45c5 84
embeddedartists 4:b32cf4ef45c5 85 do {
embeddedartists 4:b32cf4ef45c5 86 mCtrl = getModeControl();
embeddedartists 4:b32cf4ef45c5 87 if (mCtrl < 0) break;
embeddedartists 4:b32cf4ef45c5 88
embeddedartists 4:b32cf4ef45c5 89 mCtrl &= ~(0x03 << 0);
embeddedartists 4:b32cf4ef45c5 90 mCtrl |= MMA7455_MCTL_MODE(mode);
embeddedartists 4:b32cf4ef45c5 91
embeddedartists 4:b32cf4ef45c5 92 if (setModeControl((uint8_t)mCtrl) < 0) {
embeddedartists 4:b32cf4ef45c5 93 break;
embeddedartists 4:b32cf4ef45c5 94 }
embeddedartists 4:b32cf4ef45c5 95
embeddedartists 4:b32cf4ef45c5 96 _mode = mode;
embeddedartists 4:b32cf4ef45c5 97 result = true;
embeddedartists 4:b32cf4ef45c5 98 } while(0);
embeddedartists 4:b32cf4ef45c5 99
embeddedartists 4:b32cf4ef45c5 100
embeddedartists 4:b32cf4ef45c5 101
embeddedartists 4:b32cf4ef45c5 102 return result;
embeddedartists 4:b32cf4ef45c5 103 }
embeddedartists 4:b32cf4ef45c5 104
embeddedartists 4:b32cf4ef45c5 105 bool MMA7455::setRange(Range range) {
embeddedartists 4:b32cf4ef45c5 106 bool result = false;
embeddedartists 4:b32cf4ef45c5 107 int mCtrl = 0;
embeddedartists 4:b32cf4ef45c5 108
embeddedartists 4:b32cf4ef45c5 109 do {
embeddedartists 4:b32cf4ef45c5 110 mCtrl = getModeControl();
embeddedartists 4:b32cf4ef45c5 111 if (mCtrl < 0) break;
embeddedartists 4:b32cf4ef45c5 112
embeddedartists 4:b32cf4ef45c5 113 mCtrl &= ~(0x03 << 2);
embeddedartists 4:b32cf4ef45c5 114 mCtrl |= MMA7455_MCTL_GLVL(range);
embeddedartists 4:b32cf4ef45c5 115
embeddedartists 4:b32cf4ef45c5 116 if (setModeControl((uint8_t)mCtrl) < 0) {
embeddedartists 4:b32cf4ef45c5 117 break;
embeddedartists 4:b32cf4ef45c5 118 }
embeddedartists 4:b32cf4ef45c5 119
embeddedartists 4:b32cf4ef45c5 120 _range = range;
embeddedartists 4:b32cf4ef45c5 121 result = true;
embeddedartists 4:b32cf4ef45c5 122 } while(0);
embeddedartists 4:b32cf4ef45c5 123
embeddedartists 4:b32cf4ef45c5 124
embeddedartists 4:b32cf4ef45c5 125
embeddedartists 4:b32cf4ef45c5 126 return result;
embeddedartists 4:b32cf4ef45c5 127
embeddedartists 4:b32cf4ef45c5 128 }
embeddedartists 4:b32cf4ef45c5 129
embeddedartists 4:b32cf4ef45c5 130 bool MMA7455::read(int32_t& x, int32_t& y, int32_t& z) {
embeddedartists 4:b32cf4ef45c5 131 bool result = false;
embeddedartists 4:b32cf4ef45c5 132
embeddedartists 4:b32cf4ef45c5 133
embeddedartists 4:b32cf4ef45c5 134 // nothing to read in standby mode
embeddedartists 4:b32cf4ef45c5 135 if (_mode == ModeStandby) return false;
embeddedartists 4:b32cf4ef45c5 136
embeddedartists 4:b32cf4ef45c5 137 // wait for ready flag
embeddedartists 4:b32cf4ef45c5 138 int status = 0;
embeddedartists 4:b32cf4ef45c5 139 do {
embeddedartists 4:b32cf4ef45c5 140 status = getStatus();
embeddedartists 4:b32cf4ef45c5 141 } while (status >= 0 && (status & MMA7455_STATUS_DRDY) == 0);
embeddedartists 4:b32cf4ef45c5 142
embeddedartists 4:b32cf4ef45c5 143
embeddedartists 4:b32cf4ef45c5 144 do {
embeddedartists 4:b32cf4ef45c5 145 if (status < 0) break;
embeddedartists 4:b32cf4ef45c5 146
embeddedartists 4:b32cf4ef45c5 147
embeddedartists 4:b32cf4ef45c5 148 char buf[6];
embeddedartists 4:b32cf4ef45c5 149 buf[0] = MMA7455_ADDR_XOUTL;
embeddedartists 4:b32cf4ef45c5 150 if (_i2c.write(MMA7455_I2C_ADDR, buf, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 151 if (_i2c.read(MMA7455_I2C_ADDR, buf, 6) != 0) break;
embeddedartists 4:b32cf4ef45c5 152
embeddedartists 4:b32cf4ef45c5 153 // check if second bit is set in high byte -> negative value
embeddedartists 4:b32cf4ef45c5 154 // expand negative value to full byte
embeddedartists 4:b32cf4ef45c5 155 if (buf[1] & 0x02) buf[1] |= 0xFC;
embeddedartists 4:b32cf4ef45c5 156 if (buf[3] & 0x02) buf[3] |= 0xFC;
embeddedartists 4:b32cf4ef45c5 157 if (buf[5] & 0x02) buf[5] |= 0xFC;
embeddedartists 4:b32cf4ef45c5 158
embeddedartists 4:b32cf4ef45c5 159 x = (int16_t)((buf[1] << 8) | buf[0]) + _xOff;
embeddedartists 4:b32cf4ef45c5 160 y = (int16_t)((buf[3] << 8) | buf[2]) + _yOff;
embeddedartists 4:b32cf4ef45c5 161 z = (int16_t)((buf[5] << 8) | buf[4]) + _zOff;
embeddedartists 4:b32cf4ef45c5 162
embeddedartists 4:b32cf4ef45c5 163
embeddedartists 4:b32cf4ef45c5 164 result = true;
embeddedartists 4:b32cf4ef45c5 165
embeddedartists 4:b32cf4ef45c5 166 } while(0);
embeddedartists 4:b32cf4ef45c5 167
embeddedartists 4:b32cf4ef45c5 168
embeddedartists 4:b32cf4ef45c5 169 return result;
embeddedartists 4:b32cf4ef45c5 170 }
embeddedartists 4:b32cf4ef45c5 171
embeddedartists 4:b32cf4ef45c5 172 bool MMA7455::calibrate() {
embeddedartists 4:b32cf4ef45c5 173 bool result = false;
embeddedartists 4:b32cf4ef45c5 174 bool failed = false;
embeddedartists 4:b32cf4ef45c5 175
embeddedartists 4:b32cf4ef45c5 176 int32_t x = 0;
embeddedartists 4:b32cf4ef45c5 177 int32_t y = 0;
embeddedartists 4:b32cf4ef45c5 178 int32_t z = 0;
embeddedartists 4:b32cf4ef45c5 179
embeddedartists 4:b32cf4ef45c5 180 int32_t xr = 0;
embeddedartists 4:b32cf4ef45c5 181 int32_t yr = 0;
embeddedartists 4:b32cf4ef45c5 182 int32_t zr = 0;
embeddedartists 4:b32cf4ef45c5 183
embeddedartists 4:b32cf4ef45c5 184 int xOff = 0;
embeddedartists 4:b32cf4ef45c5 185 int yOff = 0;
embeddedartists 4:b32cf4ef45c5 186 int zOff = 16;
embeddedartists 4:b32cf4ef45c5 187 if (_range == Range_2g) {
embeddedartists 4:b32cf4ef45c5 188 zOff = 64;
embeddedartists 4:b32cf4ef45c5 189 }
embeddedartists 4:b32cf4ef45c5 190 if (_range == Range_4g) {
embeddedartists 4:b32cf4ef45c5 191 zOff = 32;
embeddedartists 4:b32cf4ef45c5 192 }
embeddedartists 4:b32cf4ef45c5 193
embeddedartists 4:b32cf4ef45c5 194 do {
embeddedartists 4:b32cf4ef45c5 195
embeddedartists 4:b32cf4ef45c5 196 // get an average of 6 values
embeddedartists 4:b32cf4ef45c5 197 for (int i = 0; i < 6; i++) {
embeddedartists 4:b32cf4ef45c5 198 if (!read(xr, yr, zr)) {
embeddedartists 4:b32cf4ef45c5 199 failed = true;
embeddedartists 4:b32cf4ef45c5 200 break;
embeddedartists 4:b32cf4ef45c5 201 }
embeddedartists 4:b32cf4ef45c5 202 x += xr;
embeddedartists 4:b32cf4ef45c5 203 y += yr;
embeddedartists 4:b32cf4ef45c5 204 z += zr;
embeddedartists 4:b32cf4ef45c5 205
embeddedartists 4:b32cf4ef45c5 206 wait_ms(100);
embeddedartists 4:b32cf4ef45c5 207 }
embeddedartists 4:b32cf4ef45c5 208
embeddedartists 4:b32cf4ef45c5 209 if (failed) break;
embeddedartists 4:b32cf4ef45c5 210 x /= 6;
embeddedartists 4:b32cf4ef45c5 211 y /= 6;
embeddedartists 4:b32cf4ef45c5 212 z /= 6;
embeddedartists 4:b32cf4ef45c5 213
embeddedartists 4:b32cf4ef45c5 214 xOff -= x;
embeddedartists 4:b32cf4ef45c5 215 yOff -= y;
embeddedartists 4:b32cf4ef45c5 216 zOff -= z;
embeddedartists 4:b32cf4ef45c5 217
embeddedartists 4:b32cf4ef45c5 218 /*
embeddedartists 4:b32cf4ef45c5 219 * For some reason we have not got correct/reliable calibration
embeddedartists 4:b32cf4ef45c5 220 * by using the offset drift registers. Instead we are
embeddedartists 4:b32cf4ef45c5 221 * calculating the offsets and store them in member variables.
embeddedartists 4:b32cf4ef45c5 222 *
embeddedartists 4:b32cf4ef45c5 223 * These member variables are then used in the read() method
embeddedartists 4:b32cf4ef45c5 224 */
embeddedartists 4:b32cf4ef45c5 225
embeddedartists 4:b32cf4ef45c5 226 _xOff = xOff;
embeddedartists 4:b32cf4ef45c5 227 _yOff = yOff;
embeddedartists 4:b32cf4ef45c5 228 _zOff = zOff;
embeddedartists 4:b32cf4ef45c5 229
embeddedartists 4:b32cf4ef45c5 230
embeddedartists 4:b32cf4ef45c5 231 result = true;
embeddedartists 4:b32cf4ef45c5 232
embeddedartists 4:b32cf4ef45c5 233 } while (0);
embeddedartists 4:b32cf4ef45c5 234
embeddedartists 4:b32cf4ef45c5 235
embeddedartists 4:b32cf4ef45c5 236
embeddedartists 4:b32cf4ef45c5 237 return result;
embeddedartists 4:b32cf4ef45c5 238 }
embeddedartists 4:b32cf4ef45c5 239
embeddedartists 4:b32cf4ef45c5 240 bool MMA7455::setCalibrationOffsets(int32_t xOff, int32_t yOff, int32_t zOff) {
embeddedartists 4:b32cf4ef45c5 241 _xOff = xOff;
embeddedartists 4:b32cf4ef45c5 242 _yOff = yOff;
embeddedartists 4:b32cf4ef45c5 243 _zOff = zOff;
embeddedartists 4:b32cf4ef45c5 244
embeddedartists 4:b32cf4ef45c5 245 return true;
embeddedartists 4:b32cf4ef45c5 246 }
embeddedartists 4:b32cf4ef45c5 247
embeddedartists 4:b32cf4ef45c5 248 bool MMA7455::getCalibrationOffsets(int32_t& xOff, int32_t& yOff, int32_t& zOff) {
embeddedartists 4:b32cf4ef45c5 249 xOff = _xOff;
embeddedartists 4:b32cf4ef45c5 250 yOff = _yOff;
embeddedartists 4:b32cf4ef45c5 251 zOff = _zOff;
embeddedartists 4:b32cf4ef45c5 252
embeddedartists 4:b32cf4ef45c5 253 return true;
embeddedartists 4:b32cf4ef45c5 254 }
embeddedartists 4:b32cf4ef45c5 255
embeddedartists 4:b32cf4ef45c5 256 int MMA7455::getStatus() {
embeddedartists 4:b32cf4ef45c5 257 int result = -1;
embeddedartists 4:b32cf4ef45c5 258 char data[1];
embeddedartists 4:b32cf4ef45c5 259
embeddedartists 4:b32cf4ef45c5 260 do {
embeddedartists 4:b32cf4ef45c5 261 data[0] = MMA7455_ADDR_STATUS;
embeddedartists 4:b32cf4ef45c5 262 if (_i2c.write(MMA7455_I2C_ADDR, data, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 263
embeddedartists 4:b32cf4ef45c5 264 if (_i2c.read(MMA7455_I2C_ADDR, data, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 265
embeddedartists 4:b32cf4ef45c5 266 result = data[0];
embeddedartists 4:b32cf4ef45c5 267
embeddedartists 4:b32cf4ef45c5 268 } while (0);
embeddedartists 4:b32cf4ef45c5 269
embeddedartists 4:b32cf4ef45c5 270
embeddedartists 4:b32cf4ef45c5 271
embeddedartists 4:b32cf4ef45c5 272 return result;
embeddedartists 4:b32cf4ef45c5 273 }
embeddedartists 4:b32cf4ef45c5 274
embeddedartists 4:b32cf4ef45c5 275 int MMA7455::getModeControl() {
embeddedartists 4:b32cf4ef45c5 276
embeddedartists 4:b32cf4ef45c5 277 int result = -1;
embeddedartists 4:b32cf4ef45c5 278 char data[1];
embeddedartists 4:b32cf4ef45c5 279
embeddedartists 4:b32cf4ef45c5 280 do {
embeddedartists 4:b32cf4ef45c5 281 data[0] = MMA7455_ADDR_MCTL;
embeddedartists 4:b32cf4ef45c5 282 if (_i2c.write(MMA7455_I2C_ADDR, data, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 283
embeddedartists 4:b32cf4ef45c5 284 if (_i2c.read(MMA7455_I2C_ADDR, data, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 285
embeddedartists 4:b32cf4ef45c5 286 result = data[0];
embeddedartists 4:b32cf4ef45c5 287
embeddedartists 4:b32cf4ef45c5 288 } while (0);
embeddedartists 4:b32cf4ef45c5 289
embeddedartists 4:b32cf4ef45c5 290
embeddedartists 4:b32cf4ef45c5 291
embeddedartists 4:b32cf4ef45c5 292 return result;
embeddedartists 4:b32cf4ef45c5 293 }
embeddedartists 4:b32cf4ef45c5 294
embeddedartists 4:b32cf4ef45c5 295 int MMA7455::setModeControl(uint8_t mctl) {
embeddedartists 4:b32cf4ef45c5 296 int result = -1;
embeddedartists 4:b32cf4ef45c5 297 char data[2];
embeddedartists 4:b32cf4ef45c5 298
embeddedartists 4:b32cf4ef45c5 299 do {
embeddedartists 4:b32cf4ef45c5 300 data[0] = MMA7455_ADDR_MCTL;
embeddedartists 4:b32cf4ef45c5 301 data[1] = (char)mctl;
embeddedartists 4:b32cf4ef45c5 302 if (_i2c.write(MMA7455_I2C_ADDR, data, 2) != 0) break;
embeddedartists 4:b32cf4ef45c5 303
embeddedartists 4:b32cf4ef45c5 304 result = 0;
embeddedartists 4:b32cf4ef45c5 305
embeddedartists 4:b32cf4ef45c5 306 } while (0);
embeddedartists 4:b32cf4ef45c5 307
embeddedartists 4:b32cf4ef45c5 308
embeddedartists 4:b32cf4ef45c5 309
embeddedartists 4:b32cf4ef45c5 310 return result;
embeddedartists 4:b32cf4ef45c5 311 }
embeddedartists 4:b32cf4ef45c5 312
embeddedartists 4:b32cf4ef45c5 313