Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LRAT-example-lorawan-REFACTOR-and-CLEAN-Branch
magnometer.cpp
00001 #include "Custom_LSM303/magnometer.h" 00002 00003 #define LSM303_ADR_MAG 0x3C 00004 00005 #define LSM303_REG_MAG_OFFSET_X_REG_L_M 0x45 00006 #define LSM303_REG_MAG_OFFSET_X_REG_H_M 0x46 00007 #define LSM303_REG_MAG_OFFSET_Y_REG_L_M 0x47 00008 #define LSM303_REG_MAG_OFFSET_Y_REG_H_M 0x48 00009 #define LSM303_REG_MAG_OFFSET_Z_REG_L_M 0x49 00010 #define LSM303_REG_MAG_OFFSET_Z_REG_H_M 0x4A 00011 //#define LSM303_REG_MAG_WHO_AM_I_M 0x4F 00012 //#define LSM303_REG_MAG_CFG_REG_A_M 0x60 00013 //#define LSM303_REG_MAG_CFG_REG_B_M 0x61 00014 //#define LSM303_REG_MAG_CFG_REG_C_M 0x62 00015 #define LSM303_REG_MAG_INT_CTRL_REG_M 0x63 00016 #define LSM303_REG_MAG_INT_SOURCE_REG_M 0x64 00017 #define LSM303_REG_MAG_INT_THS_L_REG_M 0x65 00018 #define LSM303_REG_MAG_INT_THS_H_REG_M 0x66 00019 //#define LSM303_REG_MAG_STATUS_REG_M 0x67 00020 //#define LSM303_REG_MAG_OUTX_L_REG_M 0x68 00021 //#define LSM303_REG_MAG_OUTX_H_REG_M 0x69 00022 //#define LSM303_REG_MAG_OUTY_L_REG_M 0x6A 00023 //#define LSM303_REG_MAG_OUTY_H_REG_M 0x6B 00024 //#define LSM303_REG_MAG_OUTZ_L_REG_M 0x6C 00025 //#define LSM303_REG_MAG_OUTZ_H_REG_M 0x6D 00026 00027 #if defined(TARGET_LRAT) 00028 00029 #define LSM303_REG_MAG_WHO_AM_I_M 0x0F 00030 #define LSM303_WHO_MAG 0x3D 00031 00032 #define LSM303_REG_MAG_CTRL_REG1_M 0x20 00033 #define LSM303_REG_MAG_CTRL_REG2_M 0x21 00034 #define LSM303_REG_MAG_CTRL_REG3_M 0x22 00035 #define LSM303_REG_MAG_CTRL_REG4_M 0x23 00036 #define LSM303_REG_MAG_CTRL_REG5_M 0x24 00037 #define LSM303_REG_MAG_STATUS_REG_M 0x27 00038 #define LSM303_REG_MAG_OUTX_L_REG_M 0x28 00039 #define LSM303_REG_MAG_OUTX_H_REG_M 0x29 00040 #define LSM303_REG_MAG_OUTY_L_REG_M 0x2A 00041 #define LSM303_REG_MAG_OUTY_H_REG_M 0x2B 00042 #define LSM303_REG_MAG_OUTZ_L_REG_M 0x2C 00043 #define LSM303_REG_MAG_OUTZ_H_REG_M 0x2D 00044 #define LSM303_REG_MAG_TEMP_L_M 0x2E 00045 #define LSM303_REG_MAG_TEMP_H_M 0x2F 00046 #define LSM303_REG_MAG_TEMP_CFG_REG_A 0x1F 00047 00048 #define CFG_MAG_ADR LSM303_REG_MAG_CTRL_REG1_M 00049 #define CFG_MAG_LEN 5 00050 00051 #else 00052 00053 // #define PIN_MAG PB_12 // Not really though - Pick dead pin here for time being 00054 00055 #define LSM303_REG_MAG_WHO_AM_I_M 0x4F 00056 #define LSM303_WHO_MAG 0x40 00057 00058 #define LSM303_REG_MAG_CFG_REG_A_M 0x60 00059 #define LSM303_REG_MAG_CFG_REG_B_M 0x61 00060 #define LSM303_REG_MAG_CFG_REG_C_M 0x62 00061 #define LSM303_REG_MAG_STATUS_REG_M 0x67 00062 #define LSM303_REG_MAG_OUTX_L_REG_M 0x68 00063 #define LSM303_REG_MAG_OUTX_H_REG_M 0x69 00064 #define LSM303_REG_MAG_OUTY_L_REG_M 0x6A 00065 #define LSM303_REG_MAG_OUTY_H_REG_M 0x6B 00066 #define LSM303_REG_MAG_OUTZ_L_REG_M 0x6C 00067 #define LSM303_REG_MAG_OUTZ_H_REG_M 0x6D 00068 00069 #define CFG_MAG_ADR LSM303_REG_MAG_CFG_REG_A_M 00070 #define CFG_MAG_LEN 3 00071 #endif 00072 00073 char sPass[26] = "[\u001b[32mPASS\u001b[0m]"; 00074 char sFail[26] = "[\u001b[31mFAIL\u001b[0m]"; 00075 00076 /** 00077 */ 00078 Magnometer::Magnometer() 00079 { 00080 00081 } 00082 00083 /** 00084 */ 00085 Magnometer::~Magnometer() 00086 { 00087 00088 } 00089 00090 /** 00091 */ 00092 void Magnometer::setters(I2C *_i2c, char *_buf) 00093 { 00094 i2c = _i2c; 00095 buf = _buf; 00096 } 00097 00098 /** 00099 */ 00100 void Magnometer::dumpCfg() 00101 { 00102 char cmd[2]; 00103 char start = CFG_MAG_ADR; 00104 00105 for (int i = 0; i < CFG_MAG_LEN; i++) 00106 { 00107 cmd[0] = start + i; 00108 i2c->write(LSM303_ADR_MAG, cmd, 1); 00109 i2c->read(LSM303_ADR_MAG, &buf[i], 1); 00110 } 00111 00112 printf("CFGMAG: | "); 00113 for (int i = 0; i < CFG_MAG_LEN; i++) 00114 { 00115 printf("%02X ", buf[i]); 00116 } 00117 printf("|\r\n"); 00118 } 00119 00120 /** 00121 */ 00122 void Magnometer::onIRQ() 00123 { 00124 // MAGNOMETER INTERRUPT FUNCTION 00125 } 00126 00127 /** 00128 */ 00129 void Magnometer::wipeIRQ() 00130 { 00131 char ret; 00132 char cmd[2]; 00133 cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M; 00134 00135 i2c->write(LSM303_ADR_MAG, cmd, 1); 00136 i2c->read(LSM303_ADR_MAG, &ret, 1); 00137 } 00138 00139 /** 00140 */ 00141 void Magnometer::init() 00142 { 00143 printf("In Magnometer Init Sequence...\r\n"); 00144 00145 char *res; 00146 char ret; 00147 00148 char cmd[2]; 00149 cmd[0] = LSM303_REG_MAG_WHO_AM_I_M; 00150 00151 i2c->write(LSM303_ADR_MAG, cmd, 1); 00152 i2c->read(LSM303_ADR_MAG, &ret, 1); 00153 res = (ret == LSM303_WHO_MAG ? sPass : sFail); 00154 printf("MAG WhoAmI: %02X %s\r\n", ret, res); 00155 00156 #if defined(TARGET_LRAT) 00157 cmd[0] = LSM303_REG_MAG_CTRL_REG1_M; 00158 cmd[1] = 0x70; // Ultra-High Performance Mode on XY axes, ODR=10Hz 00159 i2c->write(LSM303_ADR_MAG, cmd, 2); 00160 00161 cmd[0] = LSM303_REG_MAG_CTRL_REG3_M; 00162 cmd[1] = 0x00; // High Resolution? (Full-power), Continuous 00163 i2c->write(LSM303_ADR_MAG, cmd, 2); 00164 00165 cmd[0] = LSM303_REG_MAG_CTRL_REG4_M; 00166 cmd[1] = 0x0C; // Ultra-High Performance Mode on Z axis 00167 i2c->write(LSM303_ADR_MAG, cmd, 2); 00168 00169 #if defined(ENABLE_TMPSENSOR) 00170 // Enable Temp Sensor 00171 cmd[0] = LSM303_REG_MAG_CTRL_REG1_M; 00172 i2c->write(LSM303_ADR_MAG, cmd, 1); 00173 i2c->read(LSM303_ADR_MAG, &ret, 1); 00174 00175 cmd[0] = LSM303_REG_MAG_CTRL_REG1_M; 00176 cmd[1] = ret | 0x80; 00177 i2c->write(LSM303_ADR_MAG, cmd, 2); 00178 00179 #endif 00180 #else 00181 00182 cmd[0] = LSM303_REG_MAG_CFG_REG_A_M; 00183 cmd[1] = 0x00; // Mag = 10 Hz (high-resolution and continuous mode) 00184 i2c->write(LSM303_ADR_MAG, cmd, 2); 00185 00186 cmd[0] = LSM303_REG_MAG_CFG_REG_C_M; 00187 cmd[1] = 0x40; // Mag enable interrupt on pin 00188 i2c->write(LSM303_ADR_MAG, cmd, 2); 00189 00190 #endif 00191 00192 #if defined(TARGET_LRAT) 00193 // LRAT MAG IRQ SETUP GOES HERE (IF DIFFERENT) 00194 #else 00195 // MAG INTERRUPT SETUP 00196 cmd[0] = LSM303_REG_MAG_INT_THS_L_REG_M; 00197 cmd[1] = 0xE8; 00198 i2c->write(LSM303_ADR_MAG, cmd, 2); 00199 00200 cmd[0] = LSM303_REG_MAG_INT_THS_H_REG_M; 00201 cmd[1] = 0x03; 00202 i2c->write(LSM303_ADR_MAG, cmd, 2); 00203 00204 cmd[0] = LSM303_REG_MAG_INT_CTRL_REG_M; 00205 cmd[1] = 0xE7; 00206 i2c->write(LSM303_ADR_MAG, cmd, 2); 00207 magPin.rise(&onIRQ); 00208 #endif 00209 } 00210 00211 /** 00212 */ 00213 /* 00214 void Magnometer::read() 00215 { 00216 int16_t myMagX = 0; 00217 int16_t myMagY = 0; 00218 int16_t myMagZ = 0; 00219 00220 int16_t magMinX = 0; 00221 int16_t magMinY = 0; 00222 int16_t magMinZ = 0; 00223 int16_t magMaxX = 0; 00224 int16_t magMaxY = 0; 00225 int16_t magMaxZ = 0; 00226 00227 char rda = '\0'; 00228 char ret; 00229 00230 char cmd[2]; 00231 cmd[0] = LSM303_REG_MAG_STATUS_REG_M; 00232 00233 i2c->write(LSM303_ADR_MAG, cmd, 1); 00234 i2c->read(LSM303_ADR_MAG, &rda, 1); 00235 if (rda) 00236 { 00237 cmd[0] = LSM303_REG_MAG_OUTX_L_REG_M; 00238 i2c->write(LSM303_ADR_MAG, cmd, 1); 00239 i2c->read(LSM303_ADR_MAG, &buf[0], 1); 00240 00241 cmd[0] = LSM303_REG_MAG_OUTX_H_REG_M; 00242 i2c->write(LSM303_ADR_MAG, cmd, 1); 00243 i2c->read(LSM303_ADR_MAG, &buf[1], 1); 00244 00245 cmd[0] = LSM303_REG_MAG_OUTY_L_REG_M; 00246 i2c->write(LSM303_ADR_MAG, cmd, 1); 00247 i2c->read(LSM303_ADR_MAG, &buf[2], 1); 00248 00249 cmd[0] = LSM303_REG_MAG_OUTY_H_REG_M; 00250 i2c->write(LSM303_ADR_MAG, cmd, 1); 00251 i2c->read(LSM303_ADR_MAG, &buf[3], 1); 00252 00253 cmd[0] = LSM303_REG_MAG_OUTZ_L_REG_M; 00254 i2c->write(LSM303_ADR_MAG, cmd, 1); 00255 i2c->read(LSM303_ADR_MAG, &buf[4], 1); 00256 00257 cmd[0] = LSM303_REG_MAG_OUTZ_H_REG_M; 00258 i2c->write(LSM303_ADR_MAG, cmd, 1); 00259 i2c->read(LSM303_ADR_MAG, &buf[5], 1); 00260 00261 myMagX = (buf[0] | (buf[1] << 8)); 00262 myMagY = (buf[2] | (buf[3] << 8)); 00263 myMagZ = (buf[4] | (buf[5] << 8)); 00264 if (myMagX < magMinX) 00265 magMinX = myMagX; 00266 if (myMagY < magMinY) 00267 magMinY = myMagY; 00268 if (myMagZ < magMinZ) 00269 magMinZ = myMagZ; 00270 if (myMagX > magMaxX) 00271 magMaxX = myMagX; 00272 if (myMagY > magMaxY) 00273 magMaxY = myMagY; 00274 if (myMagZ > magMaxZ) 00275 magMaxZ = myMagZ; 00276 00277 cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M; 00278 i2c->write(LSM303_ADR_MAG, cmd, 1); 00279 i2c->read(LSM303_ADR_MAG, &ret, 1); 00280 00281 if (ret & 0x01 && magEvent == 0 && ret & 0xFC) 00282 { 00283 // magSFire++; 00284 // magEvent = 1; 00285 // magSHIRQ++; 00286 } 00287 else if (!(ret & 0x01) && magEvent == 1 && !(ret & 0xFC)) 00288 { 00289 // magSFire++; 00290 // magEvent = 0; 00291 // magSLIRQ++; 00292 } 00293 // printf("M|%02X|%02X %02X %02X %02X %02X %02X|%*d,%*d,%*d|%*d,%*d,%*d|%*d,%*d,%*d|%02X|%02X/%02X %02X/%02X\r\n", rda, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], 6, myMagX, 6, myMagY, 6, myMagZ, 6, magMinX, 6, magMinY, 6, magMinZ, 6, magMaxX, 6, magMaxY, 6, magMaxZ, ret, magSHIRQ, magSLIRQ, magSFire, magHFire); 00294 } 00295 } 00296 */
Generated on Wed Jul 13 2022 18:27:55 by
1.7.2