Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system
Dependencies: FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem
Fork of AxedaGo-Freescal_FRDM-KL46Z by
MAG3110/MAG3110.h@0:65004368569c, 2014-07-01 (annotated)
- Committer:
- AxedaCorp
- Date:
- Tue Jul 01 21:31:54 2014 +0000
- Revision:
- 0:65004368569c
Made initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:65004368569c | 1 | /* |
AxedaCorp | 0:65004368569c | 2 | * MAG3110 Sensor Library for mbed |
AxedaCorp | 0:65004368569c | 3 | * TODO: Add proper header |
AxedaCorp | 0:65004368569c | 4 | */ |
AxedaCorp | 0:65004368569c | 5 | |
AxedaCorp | 0:65004368569c | 6 | #ifndef MAG3110_H |
AxedaCorp | 0:65004368569c | 7 | #define MAG3110_H |
AxedaCorp | 0:65004368569c | 8 | |
AxedaCorp | 0:65004368569c | 9 | #include "mbed.h" |
AxedaCorp | 0:65004368569c | 10 | |
AxedaCorp | 0:65004368569c | 11 | #define PI 3.14159265359 |
AxedaCorp | 0:65004368569c | 12 | |
AxedaCorp | 0:65004368569c | 13 | #define MAG_ADDR 0x1D |
AxedaCorp | 0:65004368569c | 14 | |
AxedaCorp | 0:65004368569c | 15 | // define registers |
AxedaCorp | 0:65004368569c | 16 | #define MAG_DR_STATUS 0x00 |
AxedaCorp | 0:65004368569c | 17 | #define MAG_OUT_X_MSB 0x01 |
AxedaCorp | 0:65004368569c | 18 | #define MAG_OUT_X_LSB 0x02 |
AxedaCorp | 0:65004368569c | 19 | #define MAG_OUT_Y_MSB 0x03 |
AxedaCorp | 0:65004368569c | 20 | #define MAG_OUT_Y_LSB 0x04 |
AxedaCorp | 0:65004368569c | 21 | #define MAG_OUT_Z_MSB 0x05 |
AxedaCorp | 0:65004368569c | 22 | #define MAG_OUT_Z_LSB 0x06 |
AxedaCorp | 0:65004368569c | 23 | #define MAG_WHO_AM_I 0x07 |
AxedaCorp | 0:65004368569c | 24 | #define MAG_SYSMOD 0x08 |
AxedaCorp | 0:65004368569c | 25 | #define MAG_OFF_X_MSB 0x09 |
AxedaCorp | 0:65004368569c | 26 | #define MAG_OFF_X_LSB 0x0A |
AxedaCorp | 0:65004368569c | 27 | #define MAG_OFF_Y_MSB 0x0B |
AxedaCorp | 0:65004368569c | 28 | #define MAG_OFF_Y_LSB 0x0C |
AxedaCorp | 0:65004368569c | 29 | #define MAG_OFF_Z_MSB 0x0D |
AxedaCorp | 0:65004368569c | 30 | #define MAG_OFF_Z_LSB 0x0E |
AxedaCorp | 0:65004368569c | 31 | #define MAG_DIE_TEMP 0x0F |
AxedaCorp | 0:65004368569c | 32 | #define MAG_CTRL_REG1 0x10 |
AxedaCorp | 0:65004368569c | 33 | #define MAG_CTRL_REG2 0x11 |
AxedaCorp | 0:65004368569c | 34 | |
AxedaCorp | 0:65004368569c | 35 | // what should WHO_AM_I return? |
AxedaCorp | 0:65004368569c | 36 | #define MAG_3110_WHO_AM_I_VALUE 0xC4 |
AxedaCorp | 0:65004368569c | 37 | |
AxedaCorp | 0:65004368569c | 38 | |
AxedaCorp | 0:65004368569c | 39 | // Fields in registers |
AxedaCorp | 0:65004368569c | 40 | // CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac |
AxedaCorp | 0:65004368569c | 41 | |
AxedaCorp | 0:65004368569c | 42 | // Sampling rate from 80Hz down to 0.625Hz |
AxedaCorp | 0:65004368569c | 43 | #define MAG_3110_SAMPLE80 0 |
AxedaCorp | 0:65004368569c | 44 | #define MAG_3110_SAMPLE40 0x20 |
AxedaCorp | 0:65004368569c | 45 | #define MAG_3110_SAMPLE20 0x40 |
AxedaCorp | 0:65004368569c | 46 | #define MAG_3110_SAMPLE10 0x60 |
AxedaCorp | 0:65004368569c | 47 | #define MAG_3110_SAMPLE5 0x80 |
AxedaCorp | 0:65004368569c | 48 | #define MAG_3110_SAMPLE2_5 0xA0 |
AxedaCorp | 0:65004368569c | 49 | #define MAG_3110_SAMPLE1_25 0xC0 |
AxedaCorp | 0:65004368569c | 50 | #define MAG_3110_SAMPLE0_625 0xE0 |
AxedaCorp | 0:65004368569c | 51 | |
AxedaCorp | 0:65004368569c | 52 | // How many samples to average (lowers data rate) |
AxedaCorp | 0:65004368569c | 53 | #define MAG_3110_OVERSAMPLE1 0 |
AxedaCorp | 0:65004368569c | 54 | #define MAG_3110_OVERSAMPLE2 0x08 |
AxedaCorp | 0:65004368569c | 55 | #define MAG_3110_OVERSAMPLE3 0x10 |
AxedaCorp | 0:65004368569c | 56 | #define MAG_3110_OVERSAMPLE4 0x18 |
AxedaCorp | 0:65004368569c | 57 | |
AxedaCorp | 0:65004368569c | 58 | // read only 1 byte per axis |
AxedaCorp | 0:65004368569c | 59 | #define MAG_3110_FASTREAD 0x04 |
AxedaCorp | 0:65004368569c | 60 | // do one measurement (even if in standby mode) |
AxedaCorp | 0:65004368569c | 61 | #define MAG_3110_TRIGGER 0x02 |
AxedaCorp | 0:65004368569c | 62 | // put in active mode |
AxedaCorp | 0:65004368569c | 63 | #define MAG_3110_ACTIVE 0x01 |
AxedaCorp | 0:65004368569c | 64 | |
AxedaCorp | 0:65004368569c | 65 | // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _ |
AxedaCorp | 0:65004368569c | 66 | // reset sensor after each reading |
AxedaCorp | 0:65004368569c | 67 | #define MAG_3110_AUTO_MRST_EN 0x80 |
AxedaCorp | 0:65004368569c | 68 | // don't subtract user offsets |
AxedaCorp | 0:65004368569c | 69 | #define MAG_3110_RAW 0x20 |
AxedaCorp | 0:65004368569c | 70 | // reset magnetic sensor after too-large field |
AxedaCorp | 0:65004368569c | 71 | #define MAG_3110_MAG_RST 0x10 |
AxedaCorp | 0:65004368569c | 72 | |
AxedaCorp | 0:65004368569c | 73 | // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR |
AxedaCorp | 0:65004368569c | 74 | #define MAG_3110_ZYXDR 0x08 |
AxedaCorp | 0:65004368569c | 75 | |
AxedaCorp | 0:65004368569c | 76 | /** |
AxedaCorp | 0:65004368569c | 77 | * MAG3110 Class to read X/Y/Z data from the magentometer |
AxedaCorp | 0:65004368569c | 78 | * |
AxedaCorp | 0:65004368569c | 79 | */ |
AxedaCorp | 0:65004368569c | 80 | class MAG3110 |
AxedaCorp | 0:65004368569c | 81 | { |
AxedaCorp | 0:65004368569c | 82 | public: |
AxedaCorp | 0:65004368569c | 83 | /** |
AxedaCorp | 0:65004368569c | 84 | * Main constructor |
AxedaCorp | 0:65004368569c | 85 | * @param sda SDA pin |
AxedaCorp | 0:65004368569c | 86 | * @param sdl SCL pin |
AxedaCorp | 0:65004368569c | 87 | * @param addr addr of the I2C peripheral |
AxedaCorp | 0:65004368569c | 88 | */ |
AxedaCorp | 0:65004368569c | 89 | MAG3110(PinName sda, PinName scl); |
AxedaCorp | 0:65004368569c | 90 | /** |
AxedaCorp | 0:65004368569c | 91 | * Debug version of constructor |
AxedaCorp | 0:65004368569c | 92 | * @param sda SDA pin |
AxedaCorp | 0:65004368569c | 93 | * @param sdl SCL pin |
AxedaCorp | 0:65004368569c | 94 | * @param addr Address of the I2C peripheral |
AxedaCorp | 0:65004368569c | 95 | * @param pc Serial object to output debug messages |
AxedaCorp | 0:65004368569c | 96 | */ |
AxedaCorp | 0:65004368569c | 97 | MAG3110(PinName sda, PinName scl, Serial *pc); //pass serial for debug |
AxedaCorp | 0:65004368569c | 98 | /** |
AxedaCorp | 0:65004368569c | 99 | * Setup the Magnetometer |
AxedaCorp | 0:65004368569c | 100 | * |
AxedaCorp | 0:65004368569c | 101 | */ |
AxedaCorp | 0:65004368569c | 102 | void begin(); |
AxedaCorp | 0:65004368569c | 103 | /** |
AxedaCorp | 0:65004368569c | 104 | * Read a register, return its value as int |
AxedaCorp | 0:65004368569c | 105 | * @param regAddr The address to read |
AxedaCorp | 0:65004368569c | 106 | * @return value in register |
AxedaCorp | 0:65004368569c | 107 | */ |
AxedaCorp | 0:65004368569c | 108 | int readReg(char regAddr); |
AxedaCorp | 0:65004368569c | 109 | /** |
AxedaCorp | 0:65004368569c | 110 | * Read a value from a pair of registers, return as int |
AxedaCorp | 0:65004368569c | 111 | * @param regAddr The address to read |
AxedaCorp | 0:65004368569c | 112 | * @return Value from 2 consecutive registers |
AxedaCorp | 0:65004368569c | 113 | */ |
AxedaCorp | 0:65004368569c | 114 | int readVal(char regAddr); |
AxedaCorp | 0:65004368569c | 115 | /** |
AxedaCorp | 0:65004368569c | 116 | * Calculate the heading |
AxedaCorp | 0:65004368569c | 117 | * @return heading in degrees |
AxedaCorp | 0:65004368569c | 118 | */ |
AxedaCorp | 0:65004368569c | 119 | float getHeading(); |
AxedaCorp | 0:65004368569c | 120 | /** |
AxedaCorp | 0:65004368569c | 121 | * Perform a read on the X, Y and Z values. |
AxedaCorp | 0:65004368569c | 122 | * @param xVal Pointer to X value |
AxedaCorp | 0:65004368569c | 123 | * @param yVal Pointer to Y value |
AxedaCorp | 0:65004368569c | 124 | * @param zVal Pointer to Z value |
AxedaCorp | 0:65004368569c | 125 | */ |
AxedaCorp | 0:65004368569c | 126 | void getValues(int *xVal, int *yVal, int *zVal); |
AxedaCorp | 0:65004368569c | 127 | /** |
AxedaCorp | 0:65004368569c | 128 | * Set the calibration parameters if required. |
AxedaCorp | 0:65004368569c | 129 | * @param minX Minimum value for X range |
AxedaCorp | 0:65004368569c | 130 | * @param maxX Maximum value for X range |
AxedaCorp | 0:65004368569c | 131 | * @param minY Minimum value for Y range |
AxedaCorp | 0:65004368569c | 132 | * @param maxY maximum value for Y range |
AxedaCorp | 0:65004368569c | 133 | */ |
AxedaCorp | 0:65004368569c | 134 | void setCalibration(int minX, int maxX, int minY, int maxY); |
AxedaCorp | 0:65004368569c | 135 | /** |
AxedaCorp | 0:65004368569c | 136 | * Perfrom the calibration process. |
AxedaCorp | 0:65004368569c | 137 | * @param calPin GPIO pinName to use for coordinating the board rotation |
AxedaCorp | 0:65004368569c | 138 | * @param activeValue the GPIO pin value when input asserted |
AxedaCorp | 0:65004368569c | 139 | */ |
AxedaCorp | 0:65004368569c | 140 | void calXY(PinName pin, int activeValue); |
AxedaCorp | 0:65004368569c | 141 | |
AxedaCorp | 0:65004368569c | 142 | private: |
AxedaCorp | 0:65004368569c | 143 | I2C _i2c; |
AxedaCorp | 0:65004368569c | 144 | int _i2c_address; |
AxedaCorp | 0:65004368569c | 145 | Serial *_pc; |
AxedaCorp | 0:65004368569c | 146 | bool _debug; |
AxedaCorp | 0:65004368569c | 147 | int _avgX, _avgY; |
AxedaCorp | 0:65004368569c | 148 | |
AxedaCorp | 0:65004368569c | 149 | }; |
AxedaCorp | 0:65004368569c | 150 | #endif |