Engduino v2.1
EngduinoMagnetometer.h
Go to the documentation of this file.
00001 /**
00002 * \defgroup EngduinoMagnetometer Driver for Engduino Magnetometer
00003 * 
00004 * @{
00005 */
00006 
00007 /**
00008 * \file 
00009 *               Engduino Magnetometer driver
00010 * \author
00011 *               Engduino team: support@engduino.org
00012 */
00013 
00014 #ifndef __ENGDUINOMAGNETOMETER_H__
00015 #define __ENGDUINOMAGNETOMETER_H__
00016 
00017 #include <stdio.h>
00018 #include <Arduino.h>
00019 #include <Engduino.h>
00020 
00021 /***********************************************************************************************\
00022 * Public macros
00023 \***********************************************************************************************/
00024 
00025 // 7 Bit I2C Device Address
00026 #define FXMS3110_IIC_ADDRESS       (0x0E) 
00027 
00028 /***********************************************************************************************
00029 **
00030 **  FXMS3110Q Sensor Internal Registers
00031 */
00032 enum
00033 {
00034   FXMS3110_DR_STATUS = 0,          // 0x00
00035   FXMS3110_OUT_X_MSB,              // 0x01
00036   FXMS3110_OUT_X_LSB,              // 0x02
00037   FXMS3110_OUT_Y_MSB,              // 0x03
00038   FXMS3110_OUT_Y_lSB,              // 0x04
00039   FXMS3110_OUT_Z_MSB,              // 0x05
00040   FXMS3110_OUT_Z_LSB,              // 0x06
00041   FXMS3110_WHO_AM_I,               // 0x07
00042   FXMS3110_SYSMOD,                 // 0x08
00043   FXMS3110_OFF_X_MSB,              // 0x09
00044   FXMS3110_OFF_X_LSB,              // 0x0A
00045   FXMS3110_OFF_Y_MSB,              // 0x0B
00046   FXMS3110_OFF_Y_LSB,              // 0x0C
00047   FXMS3110_OFF_Z_MSB,              // 0x0D
00048   FXMS3110_OFF_Z_LSB,              // 0x0E
00049   FXMS3110_DIE_TEMP,               // 0x0F
00050   FXMS3110_CTRL_REG1,              // 0x10
00051   FXMS3110_CTRL_REG2               // 0x11
00052 };
00053 
00054 /*
00055 **  DR_STATUS Register
00056 */
00057 #define DR_STATUS_REG         0x00
00058 //
00059 #define ZYXOW_BIT             Bit._7
00060 #define ZOW_BIT               Bit._6
00061 #define YOW_BIT               Bit._5
00062 #define XOW_BIT               Bit._4
00063 #define ZYXDR_BIT             Bit._3
00064 #define ZDR_BIT               Bit._2
00065 #define YDR_BIT               Bit._1
00066 #define XDR_BIT               Bit._0
00067 //
00068 #define ZYXOW_MASK            0x80
00069 #define ZOW_MASK              0x40
00070 #define YOW_MASK              0x20
00071 #define XOW_MASK              0x10
00072 #define ZYXDR_MASK            0x08
00073 #define ZDR_MASK              0x04
00074 #define YDR_MASK              0x02
00075 #define XDR_MASK              0x01
00076 
00077 
00078 /*
00079 **  XYZ Data Registers
00080 */
00081 #define OUT_X_MSB_REG         0x01
00082 #define OUT_X_LSB_REG         0x02
00083 #define OUT_Y_MSB_REG         0x03
00084 #define OUT_Y_LSB_REG         0x04
00085 #define OUT_Z_MSB_REG         0x05
00086 #define OUT_Z_LSB_REG         0x06
00087 
00088 /*
00089 **  WHO_AM_I Device ID Register
00090 */
00091 #define WHO_AM_I_REG          0x07
00092 //
00093 #define FXMS3110              0xC4
00094 
00095 
00096 /*
00097 **  SYSMOD System Mode Register
00098 **  MMA8652 only
00099 */
00100 #define SYSMOD_REG            0x08
00101 //
00102 #define SYSMOD1_BIT           Bit._1
00103 #define SYSMOD0_BIT           Bit._0
00104 //
00105 #define SYSMOD1_MASK          0x02
00106 #define SYSMOD0_MASK          0x01
00107 #define SYSMOD_MASK           0x03
00108 //
00109 #define SYSMOD_STANDBY        0x00
00110 #define SYSMOD_ACTIVE_RAW     (SYSMOD0_MASK)
00111 #define SYSMOD_ACTIVE_USER    (SYSMOD1_MASK)
00112 
00113 /*
00114 **  XYZ Offset Correction Registers
00115 */
00116 #define OFF_X_MSB_REG         0x09
00117 #define OFF_X_LSB_REG         0x0A
00118 #define OFF_Y_MSB_REG         0x0B
00119 #define OFF_Y_LSB_REG         0x0C
00120 #define OFF_Z_MSB_REG         0x0D
00121 #define OFF_Z_LSB_REG         0x0E
00122 
00123 /*
00124 **  Die Temperature Register
00125 */
00126 #define OFF_X_MSB_REG         0x0F
00127 
00128 /*
00129 **  CTRL_REG1 System Control 1 Register
00130 */
00131 #define CTRL_REG1             0x10
00132 //
00133 #define DR2_BIT               Bit._7
00134 #define DR1_BIT               Bit._6
00135 #define DR0_BIT               Bit._5
00136 #define OS1_BIT               Bit._4
00137 #define OS0_BIT               Bit._3
00138 #define FREAD_BIT             Bit._2
00139 #define TM_BIT                Bit._1
00140 #define ACTIVE_BIT            Bit._0
00141 //
00142 #define DR2_MASK              0x80
00143 #define DR1_MASK              0x40
00144 #define DR0_MASK              0x20
00145 #define OS1_MASK              0x10
00146 #define OS0_MASK              0x08
00147 #define FREAD_MASK            0x04
00148 #define TM_MASK               0x02
00149 #define ACTIVE_MASK           0x01
00150 #define DR_MASK                           (DR2_MASK+DR1_MASK+DR0_MASK)
00151 #define OS_MASK                           (OS1_MASK+OS0_MASK)
00152 // The values are formed as ADCrate_Oversampling 
00153 #define DATA_RATE_1280_16     0x00
00154 #define DATA_RATE_1280_32     (OS0_MASK)
00155 #define DATA_RATE_1280_64     (OS1_MASK)
00156 #define DATA_RATE_1280_128    (OS0_MASK+OS1_MASK)
00157 #define DATA_RATE_640_16      (DR0)
00158 #define DATA_RATE_640_32      (DR0+OS0_MASK)
00159 #define DATA_RATE_640_64      (DR0+OS1_MASK)
00160 #define DATA_RATE_640_128     (DR0+OS0_MASK+OS1_MASK)
00161 #define DATA_RATE_320_16      (DR1)
00162 #define DATA_RATE_320_32      (DR1+OS0_MASK)
00163 #define DATA_RATE_320_64      (DR1+OS1_MASK)
00164 #define DATA_RATE_320_128     (DR1+OS0_MASK+OS1_MASK)
00165 #define DATA_RATE_160_16      (DR0+DR1)
00166 #define DATA_RATE_160_32      (DR0+DR1+OS0_MASK)
00167 #define DATA_RATE_160_64      (DR0+DR1+OS1_MASK)
00168 #define DATA_RATE_160_128     (DR0+DR1+OS0_MASK+OS1_MASK)
00169 #define DATA_RATE_80_16       (DR2)
00170 #define DATA_RATE_80_32       (DR2+OS0_MASK)
00171 #define DATA_RATE_80_64       (DR2+OS1_MASK)
00172 #define DATA_RATE_80_128      (DR2+OS0_MASK+OS1_MASK)
00173 // There are others, but no easy way of naming them directly because of overloading.
00174 // See table 32 in the data sheet
00175 //
00176 #define TRIGGER_OFF           0x00
00177 #define TRIGGER_ON            (ACTIVE_MASK)
00178 #define ACTIVE                (ACTIVE_MASK)
00179 #define STANDBY               0x00
00180 
00181 /*
00182 **  CTRL_REG2 System Control 2 Register
00183 */
00184 #define CTRL_REG2             0x11
00185 //
00186 #define AUTO_MRST_EN_BIT      Bit._7
00187 #define RAW_BIT               Bit._5
00188 #define MAG_RST_BIT           Bit._4
00189 //
00190 #define AUTO_MRST_EN_MASK     0x80
00191 #define RAW_MASK                  0x20
00192 #define MAG_RST_MASK          0x10
00193 //
00194 
00195 /***********************************************************************************************\
00196 * Public type definitions
00197 \***********************************************************************************************/
00198 
00199 /***********************************************************************************************\
00200 * Public memory declarations
00201 \***********************************************************************************************/
00202 
00203 /***********************************************************************************************\
00204 * Public prototypes
00205 \***********************************************************************************************/
00206 
00207 class EngduinoMagnetometerClass 
00208 {
00209         private:
00210                 void activate();
00211                 void standby();
00212                 void writeReg(int firstReg, const uint8_t *buf, uint8_t nRegs=1);
00213                 void readReg(int firstReg, uint8_t *buf, uint8_t nRegs=1);
00214         
00215         public:
00216                 EngduinoMagnetometerClass();
00217                 void   begin();
00218                 void   end();
00219                 void   xyz(float buf[3]);
00220                 int8_t temperature();
00221 
00222 };
00223 
00224 extern EngduinoMagnetometerClass EngduinoMagnetometer;
00225 
00226 #endif
00227 
00228 /** @} */