fastes

Dependencies:   mbed

Fork of mbed_test_fastest_rate by Bo-Jhang Ho

Committer:
mja054
Date:
Mon Feb 10 01:12:28 2014 +0000
Revision:
2:ca095bd51302
Parent:
0:459f1731fa6a
scheduler

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TimeString 0:459f1731fa6a 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
TimeString 0:459f1731fa6a 2 *
TimeString 0:459f1731fa6a 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
TimeString 0:459f1731fa6a 4 * and associated documentation files (the "Software"), to deal in the Software without
TimeString 0:459f1731fa6a 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
TimeString 0:459f1731fa6a 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
TimeString 0:459f1731fa6a 7 * Software is furnished to do so, subject to the following conditions:
TimeString 0:459f1731fa6a 8 *
TimeString 0:459f1731fa6a 9 * The above copyright notice and this permission notice shall be included in all copies or
TimeString 0:459f1731fa6a 10 * substantial portions of the Software.
TimeString 0:459f1731fa6a 11 *
TimeString 0:459f1731fa6a 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
TimeString 0:459f1731fa6a 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
TimeString 0:459f1731fa6a 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
TimeString 0:459f1731fa6a 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
TimeString 0:459f1731fa6a 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
TimeString 0:459f1731fa6a 17 */
TimeString 0:459f1731fa6a 18
TimeString 0:459f1731fa6a 19 #include "MMA8451Q.h"
TimeString 0:459f1731fa6a 20
TimeString 0:459f1731fa6a 21 #define INT_SOURCE 0x0C
TimeString 0:459f1731fa6a 22 #define REG_WHO_AM_I 0x0D
TimeString 0:459f1731fa6a 23 #define HP_FILTER_CUTOFF 0x0F
TimeString 0:459f1731fa6a 24 #define PULSE_CFG 0x21
TimeString 0:459f1731fa6a 25 #define PULSE_SRC 0x22
TimeString 0:459f1731fa6a 26 #define PULSE_THSX 0x23
TimeString 0:459f1731fa6a 27 #define PULSE_THSY 0x24
TimeString 0:459f1731fa6a 28 #define PULSE_THSZ 0x25
TimeString 0:459f1731fa6a 29 #define PULSE_TMLT 0x26
TimeString 0:459f1731fa6a 30 #define PULSE_LTCY 0x27
TimeString 0:459f1731fa6a 31 #define PULSE_WIND 0x28
TimeString 0:459f1731fa6a 32 #define REG_CTRL_REG_1 0x2A
TimeString 0:459f1731fa6a 33 #define CTRL_REG2 0x2B
TimeString 0:459f1731fa6a 34 #define CTRL_REG4 0x2D
TimeString 0:459f1731fa6a 35 #define CTRL_REG5 0x2E
TimeString 0:459f1731fa6a 36 #define REG_OUT_X_MSB 0x01
TimeString 0:459f1731fa6a 37 #define REG_OUT_Y_MSB 0x03
TimeString 0:459f1731fa6a 38 #define REG_OUT_Z_MSB 0x05
TimeString 0:459f1731fa6a 39
TimeString 0:459f1731fa6a 40 #define UINT14_MAX 16383
TimeString 0:459f1731fa6a 41
TimeString 0:459f1731fa6a 42 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
TimeString 0:459f1731fa6a 43 // activate the peripheral
TimeString 0:459f1731fa6a 44 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
TimeString 0:459f1731fa6a 45 writeRegs(data, 2);
TimeString 0:459f1731fa6a 46 }
TimeString 0:459f1731fa6a 47
TimeString 0:459f1731fa6a 48 MMA8451Q::~MMA8451Q() { }
TimeString 0:459f1731fa6a 49
TimeString 0:459f1731fa6a 50 uint8_t MMA8451Q::getWhoAmI() {
TimeString 0:459f1731fa6a 51 uint8_t who_am_i = 0;
TimeString 0:459f1731fa6a 52 readRegs(REG_WHO_AM_I, &who_am_i, 1);
TimeString 0:459f1731fa6a 53 return who_am_i;
TimeString 0:459f1731fa6a 54 }
TimeString 0:459f1731fa6a 55
TimeString 0:459f1731fa6a 56 float MMA8451Q::getAccX() {
TimeString 0:459f1731fa6a 57 //divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity)
TimeString 0:459f1731fa6a 58 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
TimeString 0:459f1731fa6a 59 }
TimeString 0:459f1731fa6a 60
TimeString 0:459f1731fa6a 61 float MMA8451Q::getAccY() {
TimeString 0:459f1731fa6a 62 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
TimeString 0:459f1731fa6a 63 }
TimeString 0:459f1731fa6a 64
TimeString 0:459f1731fa6a 65 float MMA8451Q::getAccZ() {
TimeString 0:459f1731fa6a 66 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
TimeString 0:459f1731fa6a 67 }
TimeString 0:459f1731fa6a 68
TimeString 0:459f1731fa6a 69 void MMA8451Q::getAccAllAxis(float * res) {
TimeString 0:459f1731fa6a 70 res[0] = getAccX();
TimeString 0:459f1731fa6a 71 res[1] = getAccY();
TimeString 0:459f1731fa6a 72 res[2] = getAccZ();
TimeString 0:459f1731fa6a 73 }
TimeString 0:459f1731fa6a 74
TimeString 0:459f1731fa6a 75 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
TimeString 0:459f1731fa6a 76 int16_t acc;
TimeString 0:459f1731fa6a 77 uint8_t res[2];
TimeString 0:459f1731fa6a 78 readRegs(addr, res, 2);
TimeString 0:459f1731fa6a 79
TimeString 0:459f1731fa6a 80 acc = (res[0] << 6) | (res[1] >> 2);
TimeString 0:459f1731fa6a 81 if (acc > UINT14_MAX/2)
TimeString 0:459f1731fa6a 82 acc -= UINT14_MAX;
TimeString 0:459f1731fa6a 83
TimeString 0:459f1731fa6a 84 return acc;
TimeString 0:459f1731fa6a 85 }
TimeString 0:459f1731fa6a 86
TimeString 0:459f1731fa6a 87 void MMA8451Q::setDoubleTap(void){
TimeString 0:459f1731fa6a 88 //Implemented directly from Freescale's AN4072
TimeString 0:459f1731fa6a 89 //Added to MMA8451Q lib
TimeString 0:459f1731fa6a 90
TimeString 0:459f1731fa6a 91 uint8_t CTRL_REG1_Data;
TimeString 0:459f1731fa6a 92 // int adds;
TimeString 0:459f1731fa6a 93 uint8_t data[2] = {REG_CTRL_REG_1, 0x08};
TimeString 0:459f1731fa6a 94
TimeString 0:459f1731fa6a 95 //400 Hz, Standby Mode
TimeString 0:459f1731fa6a 96 writeRegs(data,2);
TimeString 0:459f1731fa6a 97
TimeString 0:459f1731fa6a 98 //Enable X, Y and Z Double Pulse with DPA = 0 no double pulse abort
TimeString 0:459f1731fa6a 99 data[0]=PULSE_CFG;data[1]=0x2A;
TimeString 0:459f1731fa6a 100 writeRegs(data,2);
TimeString 0:459f1731fa6a 101
TimeString 0:459f1731fa6a 102 //SetThreshold 3g on X and Y and 5g on Z
TimeString 0:459f1731fa6a 103 //Note: Every step is 0.063g
TimeString 0:459f1731fa6a 104 //3 g/0.063g = 48 counts
TimeString 0:459f1731fa6a 105 //5g/0.063g = 79 counts
TimeString 0:459f1731fa6a 106 data[0]=PULSE_THSX;data[1]=0x30;
TimeString 0:459f1731fa6a 107 writeRegs(data,2);//Set X Threshold to 3g
TimeString 0:459f1731fa6a 108 data[0]=PULSE_THSY;data[1]=0x30;
TimeString 0:459f1731fa6a 109 writeRegs(data,2);//Set Y Threshold to 3g
TimeString 0:459f1731fa6a 110 data[0]=PULSE_THSZ;data[1]=0x4F;
TimeString 0:459f1731fa6a 111 writeRegs(data,2);//Set Z Threshold to 5g
TimeString 0:459f1731fa6a 112
TimeString 0:459f1731fa6a 113 //Set Time Limit for Tap Detection to 60 ms LP Mode
TimeString 0:459f1731fa6a 114 //Note: 400 Hz ODR, Time step is 1.25 ms per step
TimeString 0:459f1731fa6a 115 //60 ms/1.25 ms = 48 counts
TimeString 0:459f1731fa6a 116 data[0]=PULSE_TMLT;data[1]=0x30;
TimeString 0:459f1731fa6a 117 writeRegs(data,2);//60 ms
TimeString 0:459f1731fa6a 118
TimeString 0:459f1731fa6a 119 //Set Latency Time to 200 ms
TimeString 0:459f1731fa6a 120 //Note: 400 Hz ODR LPMode, Time step is 2.5 ms per step 00 ms/2.5 ms = 80 counts
TimeString 0:459f1731fa6a 121 data[0]=PULSE_LTCY;data[1]=0x50;
TimeString 0:459f1731fa6a 122 writeRegs(data,2);//200 ms
TimeString 0:459f1731fa6a 123
TimeString 0:459f1731fa6a 124 //Set Time Window for second tap to 300 ms
TimeString 0:459f1731fa6a 125 //Note: 400 Hz ODR LP Mode, Time step is 2.5 ms per step
TimeString 0:459f1731fa6a 126 //300 ms/2.5 ms = 120 counts
TimeString 0:459f1731fa6a 127 data[0]=PULSE_WIND;data[1]=0x78;
TimeString 0:459f1731fa6a 128 writeRegs(data,2);//300 ms
TimeString 0:459f1731fa6a 129
TimeString 0:459f1731fa6a 130 //Route INT1 to System Interrupt
TimeString 0:459f1731fa6a 131 data[0]=CTRL_REG4;data[1]=0x08;
TimeString 0:459f1731fa6a 132 writeRegs(data,2);//Enable Pulse Interrupt in System CTRL_REG4
TimeString 0:459f1731fa6a 133 data[0]=CTRL_REG5;data[1]=0x08;
TimeString 0:459f1731fa6a 134 writeRegs(data,2);//Route Pulse Interrupt to INT1 hardware Pin CTRL_REG5
TimeString 0:459f1731fa6a 135
TimeString 0:459f1731fa6a 136 //Set the device to Active Mode
TimeString 0:459f1731fa6a 137 readRegs(0x2A,&CTRL_REG1_Data,1);//Read out the contents of the register
TimeString 0:459f1731fa6a 138 CTRL_REG1_Data |= 0x01; //Change the value in the register to Active Mode.
TimeString 0:459f1731fa6a 139 data[0]=REG_CTRL_REG_1;
TimeString 0:459f1731fa6a 140 data[1]=CTRL_REG1_Data;
TimeString 0:459f1731fa6a 141 writeRegs(data,2);//Write in the updated value to put the device in Active Mode
TimeString 0:459f1731fa6a 142 }
TimeString 0:459f1731fa6a 143
TimeString 0:459f1731fa6a 144
TimeString 0:459f1731fa6a 145 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
TimeString 0:459f1731fa6a 146 char t[1] = {addr};
TimeString 0:459f1731fa6a 147 m_i2c.write(m_addr, t, 1, true);
TimeString 0:459f1731fa6a 148 m_i2c.read(m_addr, (char *)data, len);
TimeString 0:459f1731fa6a 149 }
TimeString 0:459f1731fa6a 150
TimeString 0:459f1731fa6a 151
TimeString 0:459f1731fa6a 152
TimeString 0:459f1731fa6a 153 void MMA8451Q::writeRegs(uint8_t * data, int len) {
TimeString 0:459f1731fa6a 154 m_i2c.write(m_addr, (char *)data, len);
TimeString 0:459f1731fa6a 155 }