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.
Dependencies: FXOS8700CQ HIH6130 IS31SE5000 MAG3110 MAX44000 MAX44005 MAX44008 MMA8451Q MMA8452Q MPL3115A2 VEML6040 VEML6075 mbed vt100 LM75B FXAS21002 MAX30101 VCNL4020 VCNL4100
MSU.cpp
00001 #include "mbed.h" 00002 #include "vt100.h" 00003 #include "MSS.h" 00004 #include "MSU.h" 00005 #include "MMA8451Q.h" 00006 #include "MMA8452Q.h" 00007 #include "MAG3110.h" 00008 #include "HIH6130.h" 00009 #include "FXOS8700CQ.h" 00010 #include "MAX44000.h" 00011 #include "MAX44005.h" 00012 #include "MAX44008.h" 00013 #include "MPL3115A2.h" 00014 #include "VEML6040.h" 00015 #include "VEML6075.h" 00016 #include "LM75B.h" 00017 #include "FXAS21002.h" 00018 #include "S11059.h" 00019 #include "MAX30101.h" 00020 #include "VCNL4100.h" 00021 #include "AK9752.h" 00022 #include "AK9753.h" 00023 #include "AK09970N.h" 00024 00025 extern int test_loop ; 00026 extern int interval ; /* wait interval ms */ 00027 00028 extern vt100 *tty ; 00029 00030 i2c_sensor_type i2c_sensor[] = { 00031 {MSU_MMA8451Q_ADDRESS, "MMA8451Q", testMMA8451Q}, 00032 {MSU_MMA8452Q_ADDRESS, "MMA8452Q", testMMA8452Q}, 00033 {MSU_MAG3110_ADDRESS, "MAG3110", testMAG3110}, 00034 {MSU_HIH6130_ADDRESS, "HIH6130", testHIH6130}, 00035 {MSU_FXOS8700CQ_ADDRESS, "FXOS8700CQ", testFXOS8700CQ}, 00036 {MSU_MAX44000_ADDRESS, "MAX44000", testMAX44000}, 00037 {MSU_MAX44005_ADDRESS, "MAX44005", testMAX44005}, 00038 {MSU_MAX44008_ADDRESS, "MAX44008", testMAX44008}, 00039 {MSU_MAX30101_ADDRESS, "MAX30101", testMAX30101}, 00040 {MSU_MPL3115A2_ADDRESS, "MPL3115A2", testMPL3115A2}, 00041 {MSU_IS31SE5000_ADDRESS, "IS31SE5000", testIS31SE5000}, 00042 // {MSU_VCNL4020_ADDRESS, "VCNL4020", testVCNL4020"}, 00043 {MSU_VEML6040A_ADDRESS, "VEML6040A", testVEML6040A}, 00044 {MSU_VEML6075_ADDRESS, "VEML6075", testVEML6075}, 00045 {MSU_LM75B_ADDRESS, "LM75B", testLM75B}, 00046 {MSU_FXAS21002_ADDRESS, "FXAS21002", testFXAS21002}, 00047 {MSU_S11059_ADDRESS, "S11059", testS11059}, 00048 {MSU_VCNL4100_I2C_ADDRESS, "VCNL4100", testVCNL4100}, 00049 {MSU_AK9752_I2C_ADDRESS, "AK9752", testAK9752}, 00050 {MSU_AK9753_I2C_ADDRESS, "AK9753", testAK9753}, 00051 {MSU_AK09970N_I2C_ADDRESS, "AK09970N", testAK09970N}, 00052 {0x00, 0, 0} 00053 } ; 00054 00055 MAX30101 *max30101 = 0 ; 00056 #ifndef FIFO_DEPTH 00057 #define FIFO_DEPTH 32 00058 #endif 00059 00060 void doDemo(void) 00061 { 00062 int i ; 00063 float humidity = 0 ; 00064 float temperature = 0 ; 00065 uint8_t mode ; 00066 uint8_t txdata, rxdata ; 00067 uint16_t als ; 00068 uint8_t prox, overflow ; 00069 uint32_t data, alt, bar, tmp ; 00070 uint16_t sample_time ; 00071 00072 HIH6130 *hih6130 = new HIH6130(PIN_SDA, PIN_SCL, MSU_HIH6130_ADDRESS) ; 00073 MMA8452Q *mma8452q = new MMA8452Q(PIN_SDA, PIN_SCL, MSU_MMA8452Q_ADDRESS) ; 00074 MAX44000 *max44000 = new MAX44000(PIN_SDA, PIN_SCL, MSU_MAX44000_ADDRESS) ; 00075 rxdata = 0xF1 ; 00076 max44000->setRxConfig(rxdata) ; 00077 txdata = 0x0F ; /* light LED! */ 00078 max44000->setTxConfig(txdata) ; 00079 mode = 0x30 ; // ALS & PROX mode 00080 max44000->setMainConfig(mode) ; 00081 00082 MPL3115A2 *mpl3115a2 = new MPL3115A2(PIN_SDA, PIN_SCL, MSU_MPL3115A2_ADDRESS) ; 00083 tty->cls() ; 00084 tty->locate(10, 1) ; 00085 printf("=== Multi Sensor Demo (%s) ===\n", BOARD_NAME) ; 00086 tty->putStr(2, 3, "MMA8451Q") ; 00087 tty->putStr(2, 10, "MAX44000") ; 00088 tty->putStr(40, 3, "HIH6130") ; 00089 tty->putStr(40, 10, "MPL3115A2") ; 00090 for (i = 0 ; i < test_loop ; i++ ) { 00091 00092 /* MMA8451Q */ 00093 tty->locate(2, 5) ; 00094 printf("X:%.2f Y:%.2f Z:%.2f", 00095 mma8452q->getAccX(), 00096 mma8452q->getAccY(), 00097 mma8452q->getAccZ()) ; 00098 00099 if ((i % 10) == 0) { 00100 /* HIH6130 */ 00101 tty->locate(40, 5) ; 00102 hih6130->getValue(&humidity, &temperature) ; 00103 printf("Temp %.1f C", temperature) ; 00104 tty->locate(40, 7) ; 00105 printf("Humidity %.1f %%\n", humidity) ; 00106 /* MAX44000 */ 00107 tty->locate(2, 12) ; 00108 max44000->getADC(&overflow, &als, &prox) ; 00109 printf("ALS[ %04X ]", als) ; 00110 if (overflow) { 00111 printf(" overflow ") ; 00112 } else { 00113 printf(" ") ; 00114 } 00115 tty->locate(2, 14) ; 00116 printf("PRX[ %02X ]\n", prox) ; 00117 /* MPL3115A2 */ 00118 00119 mpl3115a2->modeAlt() ; 00120 sample_time = mpl3115a2->OneShot() ; 00121 wait(((double)sample_time)/1000.0) ; 00122 alt = mpl3115a2->getAltitude() ; 00123 mpl3115a2->modeBar() ; 00124 sample_time = mpl3115a2->OneShot() ; 00125 wait(((double)sample_time)/1000.0) ; 00126 bar = mpl3115a2->getPressure() ; 00127 tmp = mpl3115a2->getTemperature() ; 00128 tty->locate(40, 11) ; 00129 printf("Altitude: %d", (alt >> 16)&0xFFFF) ; 00130 tty->locate(40, 13) ; 00131 printf("Pressure: %d", (bar >> 6)) ; 00132 tty->locate(40, 15) ; 00133 printf("Temperature: %d", (tmp >> 8) ) ; 00134 } 00135 } 00136 txdata = 0x00 ; /* dim LED! */ 00137 max44000->setTxConfig(txdata) ; 00138 tty->locate(1, 20) ; 00139 delete mpl3115a2 ; 00140 delete max44000 ; 00141 delete mma8452q ; 00142 delete hih6130 ; 00143 } 00144 00145 void testLM75B(void) 00146 { 00147 int8_t itemp = 0 ; 00148 float ftemp = 0.0 ; 00149 printf("test LM75B\n") ; 00150 LM75B *lm75b = new LM75B(PIN_SDA, PIN_SCL, MSU_LM75B_ADDRESS) ; 00151 for (int i = 0 ; i < test_loop ; i++ ) { 00152 itemp = lm75b->temp() ; 00153 lm75b->getTemp(&ftemp) ; 00154 printf("Temp = %d C degree, %.3f C degree\n", itemp, ftemp) ; 00155 wait_ms(interval) ; 00156 } 00157 delete lm75b ; 00158 } 00159 00160 void testFXAS21002(void) 00161 { 00162 int16_t data[3] ; 00163 00164 printf("test FXAS21002\n") ; 00165 FXAS21002 *fxas = new FXAS21002(PIN_SDA, PIN_SCL, MSU_FXAS21002_ADDRESS) ; 00166 fxas->activate(true) ; 00167 00168 for (int i = 0 ; i < test_loop ; i++ ) { 00169 data[0] = fxas->getX() ; 00170 data[1] = fxas->getY() ; 00171 data[2] = fxas->getZ() ; 00172 printf("X[%6d], Y[%6d], Z[%6d]\n", 00173 data[0], data[1], data[2]) ; 00174 wait_ms(interval) ; 00175 } 00176 delete fxas ; 00177 } 00178 00179 void testMMA8451Q(void) 00180 { 00181 uint16_t ix, iy, iz ; 00182 float fx, fy, fz ; 00183 00184 printf("test MMA8451Q\n") ; 00185 MMA8451Q *acc = new MMA8451Q(PIN_SDA, PIN_SCL, MSU_MMA8451Q_ADDRESS) ; 00186 for(int i = 0 ; i < test_loop ; i++) { 00187 ix = acc->getRawX() ; 00188 iy = acc->getRawY() ; 00189 iz = acc->getRawZ() ; 00190 fx = acc->getAccX() ; 00191 fy = acc->getAccY() ; 00192 fz = acc->getAccZ() ; 00193 printf("X = %d [ %.2f ], Y = %d [ %.2f ], Z = %d [ %.2f ]\n", 00194 ix, fx, iy, fy, iz, fz ) ; 00195 wait_ms(interval) ; 00196 } 00197 delete acc ; 00198 } 00199 00200 void testMMA8452Q(void) 00201 { 00202 float x, y, z ; 00203 printf("test MMA8452Q\n") ; 00204 MMA8452Q *acc = new MMA8452Q(PIN_SDA, PIN_SCL, MSU_MMA8452Q_ADDRESS) ; 00205 for(int i = 0 ; i < test_loop ; i++) { 00206 x = acc->getAccX() ; 00207 y = acc->getAccY() ; 00208 z = acc->getAccZ() ; 00209 printf("X[%.2f] Y[%.2f] Z[%.2f]\n",x, y, z) ; 00210 wait_ms(interval) ; 00211 } 00212 delete acc ; 00213 } 00214 00215 void testMAG3110(void) 00216 { 00217 uint8_t status = 0 ; 00218 int16_t mX, mY, mZ ; 00219 float fx, fy, fz ; 00220 int x = 10 ; 00221 int y = 5 ; 00222 MAG3110 *mag = new MAG3110(PIN_SDA, PIN_SCL, MSU_MAG3110_ADDRESS) ; 00223 mag->activate() ; 00224 tty->cls() ; 00225 printf("test MAG3110\n") ; 00226 for (int i = 0 ; i < test_loop ; i++) { 00227 wait_ms(interval) ; 00228 status = mag->getStatus() ; 00229 while((status & 0x80) == 0) { // wait for ZYXOW 00230 wait(0.01) ; 00231 status = mag->getStatus() ; 00232 } 00233 00234 tty->locate(x+5, y) ; 00235 printf("--- MAG3110 ---") ; 00236 tty->locate(x, y+4) ; 00237 printf("Temperature: %02X",mag->getTemp()) ; 00238 tty->locate(x, y+5) ; 00239 printf("status: 0x%02X",mag->getStatus()) ; 00240 tty->locate(x, y+6) ; 00241 printf("ctrl reg1: 0x%02X ", mag->getCtrlReg1()) ; 00242 printf("reg2: 0x%02X", mag->getCtrlReg2()) ; 00243 mX = mag->getX() ; // here reset takes place! 00244 mY = mag->getY() ; 00245 mZ = mag->getZ() ; 00246 fx = 10000 * (float)mX / (float)0x7FFF ; 00247 fy = 10000 * (float)mY / (float)0x7FFF ; 00248 fz = 10000 * (float)mZ / (float)0x7FFF ; 00249 tty->locate(x, y+1) ; 00250 printf("X : ") ; 00251 if (fx >= 0) { printf(" ") ; } 00252 printf("%.2f uT : 0x%08X ", fx, mX) ; 00253 00254 tty->locate(x, y+2) ; 00255 printf("Y : ") ; 00256 if (fy >= 0) { printf(" ") ; } 00257 printf("%.2f uT : 0x%08X ", fy, mY) ; 00258 00259 tty->locate(x, y+3) ; 00260 printf("Z : ") ; 00261 if (fz >= 0) { printf(" ") ; } 00262 printf("%.2f uT : 0x%08X ", fz, mZ) ; 00263 } 00264 tty->locate(0, 20) ; 00265 mag->standby() ; 00266 delete mag ; 00267 } 00268 00269 void testHIH6130(void) 00270 { 00271 uint16_t result = 0 ; 00272 float humidity = 0 ; 00273 float temperature = 0 ; 00274 HIH6130 *hih = new HIH6130(PIN_SDA, PIN_SCL, MSU_HIH6130_ADDRESS) ; 00275 printf("test HIH6130\n") ; 00276 for (int i = 0 ; i < test_loop ; i++) { 00277 result = hih->getValue(&humidity, &temperature) ; 00278 printf("Temp %.1f C Humidity %.1f %%\n", temperature, humidity) ; 00279 wait_ms(interval) ; 00280 } 00281 } 00282 00283 void testFXOS8700CQ(void) 00284 { 00285 int16_t accData[3], mgnData[3] ; 00286 00287 printf("test FXOS8700CQ\n") ; 00288 FXOS8700CQ *fxos = new FXOS8700CQ(PIN_SDA, PIN_SCL, MSU_FXOS8700CQ_ADDRESS) ; 00289 fxos->init() ; 00290 00291 for (int i = 0 ; i < test_loop ; i++) { 00292 fxos->readAccMgnData(accData, mgnData) ; 00293 printf("ACC X[%d], Y[%d], Z[%d], MGN X[%d], Y[%d], Z[%d]\n", 00294 accData[0], accData[1], accData[2], 00295 mgnData[0], mgnData[1], mgnData[2]) ; 00296 wait_ms(interval) ; 00297 } 00298 delete fxos ; 00299 } 00300 00301 void testMAX44000(void) 00302 { 00303 uint8_t mode ; 00304 uint8_t txdata, rxdata ; 00305 uint16_t als ; 00306 uint8_t prox, overflow ; 00307 int x = 10 ; 00308 int y = 5 ; 00309 00310 tty->cls() ; 00311 printf("test MAX44000\n") ; 00312 MAX44000 *max44000 = new MAX44000(PIN_SDA, PIN_SCL, MSU_MAX44000_ADDRESS) ; 00313 00314 rxdata = 0xF1 ; 00315 max44000->setRxConfig(rxdata) ; 00316 txdata = 0x0F ; /* light LED! */ 00317 max44000->setTxConfig(txdata) ; 00318 mode = 0x30 ; // ALS & PROX mode 00319 max44000->setMainConfig(mode) ; 00320 00321 for (int i = 0 ; i < test_loop ; i++ ) { 00322 max44000->getADC(&overflow, &als, &prox) ; 00323 printf("ALS[ %04X ]", als) ; 00324 if (overflow) { 00325 printf(" overflow ") ; 00326 } else { 00327 printf(" ") ; 00328 } 00329 printf("PRX[ %02X ]\n", prox) ; 00330 wait_ms(interval) ; 00331 } 00332 txdata = 0x00 ; /* dim LED! */ 00333 max44000->setTxConfig(txdata) ; 00334 delete max44000 ; 00335 } 00336 00337 void testMAX44005(void) 00338 { 00339 uint16_t amb_c, amb_r, amb_g, amb_b, amb_ir, amb_ircomp, amb_temp ; 00340 MAX44005 *max44005 = new MAX44005(PIN_SDA, PIN_SCL, MSU_MAX44005_ADDRESS) ; 00341 max44005->enableTEMP() ; 00342 max44005->setMode(0x04) ; 00343 printf("test MAX44005\n") ; 00344 00345 for (int i = 0 ; i < test_loop ; i++ ) { 00346 amb_c = max44005->getAMB_CLEAR() ; 00347 amb_r = max44005->getAMB_RED() ; 00348 amb_g = max44005->getAMB_GREEN() ; 00349 amb_b = max44005->getAMB_BLUE() ; 00350 amb_ir = max44005->getIR() ; 00351 amb_ircomp = max44005->getIRCOMP() ; 00352 amb_temp = max44005->getTEMP() ; 00353 printf("--- MAX44005 ---\n") ; 00354 printf(" C[ %04X ]\n", amb_c) ; 00355 printf("R[ %04X ] G[ %04X ] B[ %04X ]\n",amb_r, amb_g, amb_b) ; 00356 printf("IR[ %04X ] IRCOMP [ %04X ]\n",amb_ir, amb_ircomp) ; 00357 printf(" TEMP [ %04X ]\n",amb_temp) ; 00358 printf("\n") ; 00359 wait_ms(interval) ; 00360 } 00361 delete max44005 ; 00362 } 00363 00364 #define MODE_CLEAR_RGB_IR 0x02 00365 void testMAX44008(void) 00366 { 00367 uint16_t amb_c, amb_r, amb_g, amb_b, amb_ir, amb_ircomp, amb_temp ; 00368 00369 MAX44008 *max44008 = new MAX44008(PIN_SDA, PIN_SCL, MSU_MAX44008_ADDRESS) ; 00370 max44008->setMode(MODE_CLEAR_RGB_IR) ; 00371 printf("test MAX44008\n") ; 00372 00373 for (int i = 0 ; i < test_loop ; i++ ) { 00374 amb_c = max44008->getAMB_CLEAR() ; 00375 amb_r = max44008->getAMB_RED() ; 00376 amb_g = max44008->getAMB_GREEN() ; 00377 amb_b = max44008->getAMB_BLUE() ; 00378 amb_ir = max44008->getIR() ; 00379 amb_ircomp = max44008->getIRCOMP() ; 00380 amb_temp = max44008->getTEMP() ; 00381 printf("--- MAX44008 ---\n") ; 00382 printf(" C[ %04X ]\n", amb_c) ; 00383 printf("R[ %04X ] G[ %04X ] B[ %04X ]\n",amb_r, amb_g, amb_b) ; 00384 printf("IR[ %04X ] IRCOMP [ %04X ]\n",amb_ir, amb_ircomp) ; 00385 printf(" TEMP [ %04X ]\n",amb_temp) ; 00386 printf("\n") ; 00387 wait_ms(interval) ; 00388 } 00389 delete max44008 ; 00390 } 00391 00392 void testMPL3115A2(void) 00393 { 00394 uint32_t data, alt, bar, tmp ; 00395 uint16_t sample_time ; 00396 MPL3115A2 *mpl = new MPL3115A2(PIN_SDA, PIN_SCL, MSU_MPL3115A2_ADDRESS) ; 00397 00398 printf("test MPL3115A2\n") ; 00399 for (int i = 0 ; i < test_loop ; i++) { 00400 mpl->modeAlt() ; 00401 sample_time = mpl->OneShot() ; 00402 wait(((double)sample_time)/1000.0) ; 00403 alt = mpl->getAltitude() ; 00404 mpl->modeBar() ; 00405 sample_time = mpl->OneShot() ; 00406 wait(((double)sample_time)/1000.0) ; 00407 bar = mpl->getPressure() ; 00408 tmp = mpl->getTemperature() ; 00409 printf("Altitude: %d, Pressure: %d, Temperature: %d\n", 00410 (alt >> 16)&0xFFFF, 00411 (bar >> 6), 00412 (tmp >> 8) ) ; 00413 wait_ms(interval) ; 00414 } 00415 delete mpl ; 00416 } 00417 00418 void testIS31SE5000(void) 00419 { 00420 printf("test ISI31SE5000\n") ; 00421 printf("Sorry not implemented yet\n") ; 00422 } 00423 00424 void testVEML6040A(void) 00425 { 00426 uint16_t uR, uG, uB, uW ; 00427 VEML6040 *veml = new VEML6040(PIN_SDA, PIN_SCL, MSU_VEML6040A_ADDRESS) ; 00428 00429 for (int i = 0 ; i < test_loop ; i++ ) { 00430 veml->setCOLORConf(0x04) ; /* one time trigger, other wise write 0 */ 00431 wait(0.1) ; 00432 veml->getRData(&uR) ; 00433 veml->getGData(&uG) ; 00434 veml->getBData(&uB) ; 00435 veml->getWData(&uW) ; 00436 printf("VEML6040A R[%d], G[%d], B[%d], W[%d]\n", uR, uG, uB, uW) ; 00437 wait_ms(interval) ; 00438 } 00439 delete veml ; 00440 } 00441 00442 void testVEML6075(void) 00443 { 00444 float uvi ; 00445 float uva, uvb, uva_cie, uvb_cie ; 00446 VEML6075 *veml = new VEML6075(PIN_SDA, PIN_SCL, MSU_VEML6075_ADDRESS) ; 00447 00448 veml->setUVConf(0x00) ; 00449 for (int i = 0 ; i < test_loop ; i++ ) { 00450 uvi = veml->UVI() ; 00451 uva = veml->getUVA() ; 00452 uva_cie = veml->getUVA_CIE() ; 00453 uvb = veml->getUVB() ; 00454 uvb_cie = veml->getUVB_CIE() ; 00455 00456 printf("VEML6075 UVI[%.4f] UVA[%.4f] UVA_CIE[%.4f] UVB[%.4f] UVB_CIE[%.4f]\n", 00457 uvi, uva, uva_cie, uvb, uvb_cie) ; 00458 wait_ms(interval) ; 00459 } 00460 delete veml ; 00461 } 00462 00463 void testVCNL4100(void) 00464 { 00465 uint8_t psData = 0x00 ; 00466 uint16_t alsData = 0x00 ; 00467 VCNL4100 *vcnl4100 = 0 ; 00468 00469 vcnl4100 = new VCNL4100(PIN_SDA, PIN_SCL, MSU_VCNL4100_I2C_ADDRESS) ; 00470 00471 printf("=== test VCNL4100 for %s (%s)\n", BOARD_NAME, __DATE__) ; 00472 printf("Proximity, Ambient Light\n") ; 00473 00474 vcnl4100->setAlsConf(0x00) ; 00475 vcnl4100->setPsConf12(0x0000) ; 00476 vcnl4100->setSpo(0xA0) ; 00477 wait(0.1) ; 00478 00479 for (int i = 0 ; i < test_loop ; i++ ) { 00480 psData = vcnl4100->getPsData() ; 00481 alsData = vcnl4100->getAlsData() ; 00482 printf("0x%02X, 0x%04X\n", psData, alsData) ; 00483 wait(1); 00484 } 00485 delete vcnl4100 ; 00486 } 00487 00488 00489 void testS11059(void) 00490 { 00491 uint16_t uR, uG, uB, uIR ; 00492 uint8_t ctrl ; 00493 S11059 *s11059 = new S11059(PIN_SDA, PIN_SCL, MSU_S11059_ADDRESS) ; 00494 00495 for (int i = 0 ; i < test_loop ; i++ ) { 00496 ctrl = 0x89 ; /* ADC reset, High Gain, integration time 1.4ms */ 00497 s11059->setControl(ctrl) ; 00498 ctrl = 0x09 ; /* Release ADC reset, High Gain, integration time 1.4ms */ 00499 s11059->setControl(ctrl) ; /* start measure */ 00500 wait(0.02) ; 00501 s11059->getRData(&uR) ; 00502 s11059->getGData(&uG) ; 00503 s11059->getBData(&uB) ; 00504 s11059->getIRData(&uIR) ; 00505 printf("S11059 R[%d], G[%d], B[%d], IR[%d]\n", uR, uG, uB, uIR) ; 00506 wait_ms(interval) ; 00507 } 00508 delete s11059 ; 00509 } 00510 00511 void init_max30101(void) 00512 { 00513 uint32_t slot_config = 0 ; 00514 uint8_t mode ; 00515 00516 max30101 = new MAX30101(PIN_SDA, PIN_SCL, MAX30101_I2C_ADDRESS) ; 00517 max30101->reset() ; 00518 00519 /* config for Mode Configuration (0x09) */ 00520 00521 mode = 0x07 ; /* Green, Red, and/or IR */ 00522 // mode = 0x03 ; /* SpO2 mode */ 00523 max30101->setMODE_CONFIG(mode) ; 00524 00525 /* config for FIFO Configuration (0x08) */ 00526 mode = 00527 // (0x02 << 5) /* SMP_AVE = 2 -> avarage of 4 data */ 00528 (0x05 << 5) /* SMP_AVE = 5 -> avarage of 32 data */ 00529 // | (0x01 << 4) /* FIFO_ROLLOVER_EN = 1 */ 00530 | (0x00 << 4) /* FIFO_ROLLOVER_EN = 0 */ 00531 | (0x0C) /* FIFO ALMOST FULL at 12 (0x0C) */ 00532 ; 00533 max30101->setFIFO_CONFIG(mode) ; 00534 00535 max30101->setLED1_PA(0x80) ; 00536 max30101->setLED2_PA(0x80) ; 00537 // max30101->setLED3_PA(0x80) ; 00538 // max30101->setPILOT_PA(0x80) ; 00539 #if 0 00540 slot_config = 00541 (0x02 << 24) /* SLOT2 LED2(IR) */ 00542 | (0x01 << 16) /* SLOT1 LED1(RED) */ 00543 | (0x00 << 8) /* SLOT4 none */ 00544 | (0x03) /* SLOT3 LED3(GREEN) */ 00545 ; 00546 max30101->setSLOT(slot_config) ; 00547 #endif 00548 mode = 0x01 ; 00549 max30101->setPROX_INT_THR(mode) ; 00550 } 00551 00552 void readSPO2_FIFO(void) 00553 { 00554 uint32_t data[2] ; 00555 uint8_t fifo_wr_ptr, fifo_rd_ptr ; 00556 int num_available_samples = 0 ; 00557 int num_samples_to_read = 6 ; 00558 int i ; 00559 00560 fifo_wr_ptr = max30101->getFIFO_WR_PTR() ; 00561 fifo_rd_ptr = max30101->getFIFO_RD_PTR() ; 00562 num_available_samples = (fifo_wr_ptr + FIFO_DEPTH - fifo_rd_ptr) % FIFO_DEPTH ; 00563 00564 if (num_available_samples < num_samples_to_read) { 00565 num_samples_to_read = num_available_samples / 2 ; 00566 } else { 00567 num_samples_to_read = num_available_samples ; 00568 } 00569 printf("=== %d data %d to read===\n", 00570 num_available_samples, num_samples_to_read) ; 00571 for (i = 0 ; i < num_samples_to_read ; i++ ) { 00572 data[0] = max30101->readFIFO() ; /* LED1 */ 00573 data[1] = max30101->readFIFO() ; /* IR */ 00574 // printf("LED1: 0x%05X, IR: 0x%05X\n", data[0], data[1]) ; 00575 } 00576 // max30101->setFIFO_WR_PTR(0) ; 00577 // max30101->setFIFO_RD_PTR(0) ; 00578 } 00579 00580 void SPO2_isr(void) 00581 { 00582 uint16_t flag, config ; 00583 float temp ; 00584 int temp_int, temp_frac ; 00585 00586 flag = max30101->getIntStatus() ; 00587 config = max30101->getIntEnable() ; 00588 // printf("Int: 0x%04X Enable: 0x%04X\n", flag, config) ; 00589 if (flag & INT_ST_A_FULL) { /* A_FULL_EN */ 00590 // printf("FIFO almost full!\n") ; 00591 readSPO2_FIFO() ; 00592 } 00593 if (flag & INT_ST_PPG_RGY) { /* New FIFO Data Ready */ 00594 printf("New FIFO Data Ready\n") ; 00595 } 00596 if (flag & INT_ST_ALC_OVF) { /* Ambient Light Cancellaration Overflow */ 00597 printf("Ambient Light Cancellaration Overflow\n") ; 00598 } 00599 if (flag & INT_ST_PROX_INT) {/* Proximity Threshold Triggered */ 00600 printf("Proximity Threshold Triggered\n") ; 00601 } 00602 if (flag & INT_ST_PWR_RDY) {/* Power Ready Flag */ 00603 printf("Power Ready!\n") ; 00604 } 00605 if (flag & INT_ST_DIE_TEMP_RDY) {/* Internal Temperature Ready Flag */ 00606 printf("DIE Temperature Ready!\n") ; 00607 temp_int = max30101->getTEMP_INT() ; 00608 temp_frac = max30101->getTEMP_FRAC() ; 00609 temp = ((float)temp_int)+(((float)temp_frac)/16.0) ; 00610 printf("Temp: %.2f\n", temp) ; 00611 } 00612 } 00613 00614 void initSPO2(void) 00615 { 00616 uint8_t config_byte = 0 ; 00617 uint16_t slot_config = 0 ; 00618 00619 /* config for SPO2 (0x0A) */ 00620 config_byte = 00621 (0x01 << 5) /* SPO2_ADC_RGE */ 00622 // | (0x05 << 3) /* SPO2_SR 101 = 1000 samples/sec */ 00623 | (0x00 << 3) /* SPO2_SR 000 = 50 samples/sec */ 00624 | (0x00) /* LED_PW 00 = 69us */ 00625 ; 00626 max30101->setSPO2_CONFIG(config_byte) ; 00627 00628 /* slot configuration */ 00629 slot_config = 00630 (0x02 << 12) /* SLOT2 LED2(IR) */ 00631 | (0x01 << 8) /* SLOT1 LED1(RED) */ 00632 | (0x00 << 4) /* SLOT4 none */ 00633 | (0x00) /* SLOT3 none */ 00634 ; 00635 max30101->setSLOT(slot_config) ; 00636 00637 /* Inititalize FIFO */ 00638 /* config for FIFO Configuration (0x08) */ 00639 config_byte = 00640 (0x02 << 5) /* SMP_AVE = 2 -> avarage of 4 data */ 00641 | (0x01 << 4) /* FIFO_ROLLOVER_EN = 1 */ 00642 // | (0x0C) /* FIFO ALMOST FULL at 12 (0x0C) */ 00643 | (0x09) /* FIFO ALMOST FULL at 9 */ 00644 ; 00645 max30101->setFIFO_CONFIG(config_byte) ; 00646 00647 00648 } 00649 00650 void testMAX30101(void) 00651 { 00652 uint16_t int_config = 0 ; 00653 uint8_t flag = 0 ; 00654 float temp = 0.0 ; 00655 uint8_t id, rev ; 00656 InterruptIn *int0 ; 00657 MAX30101 *max30101 ; 00658 00659 int0 = new InterruptIn(PIN_INT1) ; 00660 00661 max30101 = new MAX30101(PIN_SDA, PIN_SCL, MSU_MAX30101_ADDRESS) ; 00662 00663 init_max30101() ; 00664 id = max30101->getID() ; 00665 rev = max30101->getRev() ; 00666 printf("MAX30101 ID: 0x%02X, Rev: 0x%02X\n", id, rev) ; 00667 00668 initSPO2() ; 00669 int0->fall(&SPO2_isr) ; 00670 max30101->setMODE_CONFIG(MODE_SPO2) ; 00671 max30101->setIntEnable( INT_EN_A_FULL | INT_EN_DIE_TEMP_RDY ) ; 00672 int_config = max30101->getIntEnable() ; 00673 printf("Int enable: 0x%04X\n", int_config) ; 00674 flag = max30101->getIntStatus() ; 00675 00676 for (int i = 0 ; i < test_loop ; i++ ) { 00677 max30101->setTEMP_EN() ; /* trigger temperature read */ 00678 temp = max30101->getTEMP() ; 00679 flag = max30101->getIntStatus() ; 00680 printf("Temp = %.2f interrupt flags = 0x%02X\n", temp, flag) ; 00681 wait(1) ; 00682 } 00683 00684 delete max30101 ; 00685 delete int0 ; 00686 } 00687 00688 void testAK9752(void) 00689 { 00690 int companyID, deviceID ; 00691 float temp, ir ; 00692 AK9752 *ak9752 = 0 ; 00693 ak9752 = new AK9752(PIN_SDA, PIN_SCL, MSU_AK9752_I2C_ADDRESS) ; 00694 companyID = ak9752->getCompanyCode() ; 00695 deviceID = ak9752->getDeviceID() ; 00696 printf("=== AK9752 ===\n") ; 00697 printf("Company Code = 0x%02X [should be 0x48]\n", companyID) ; 00698 printf("Device ID = 0x%02X [should be 0x14]\n", deviceID) ; 00699 printf("Temp IR\n") ; 00700 for (int i = 0 ; i < test_loop ; i++ ) { 00701 ak9752->setCNTL2(0xFE) ; /* single shot */ 00702 while(ak9752->dataReady() != true) { 00703 ; 00704 } 00705 ir = ak9752->getIR() ; 00706 temp = ak9752->getTMP() ; 00707 ak9752->dataOverRun() ; /* clear data ready */ 00708 printf("%.2f %.2f\n", temp, ir) ; 00709 wait(0.1) ; 00710 } 00711 delete ak9752 ; 00712 } 00713 00714 void testAK9753(void) 00715 { 00716 int companyID, deviceID ; 00717 float temp, ir[4] ; 00718 AK9753 *ak9753 = 0 ; 00719 ak9753 = new AK9753(PIN_SDA, PIN_SCL, MSU_AK9753_I2C_ADDRESS) ; 00720 ak9753->software_reset() ; 00721 companyID = ak9753->getCompanyCode() ; 00722 deviceID = ak9753->getDeviceID() ; 00723 printf("=== AK9753 ===\n") ; 00724 printf("Company Code = 0x%02X [should be 0x48]\n", companyID) ; 00725 printf("Device ID = 0x%02X [should be 0x13]\n", deviceID) ; 00726 printf("Temp IR1 IR2 IR3 IR4\n") ; 00727 ak9753->setEINTEN(0x01) ; 00728 for (int i = 0 ; i < test_loop ; i++ ) { 00729 ak9753->setECNTL1(0xAA) ; /* single shot */ 00730 while(ak9753->dataReady() != true) { 00731 ; 00732 } 00733 temp = ak9753->getTMP() ; 00734 ir[0] = ak9753->getIR1() ; 00735 ir[1] = ak9753->getIR2() ; 00736 ir[2] = ak9753->getIR3() ; 00737 ir[3] = ak9753->getIR4() ; 00738 ak9753->dataOverRun() ; /* clear data ready */ 00739 printf("%.2f %.2f %.2f %.2f %.2f\n", 00740 temp, ir[0], ir[1], ir[2], ir[3]) ; 00741 wait(0.2) ; 00742 } 00743 delete ak9753 ; 00744 } 00745 00746 void testAK09970N(void) 00747 { 00748 uint16_t companyID, deviceID ; 00749 uint16_t status ; 00750 float x, y, z ; 00751 int result ; 00752 00753 AK09970N *ak09970n = 0 ; 00754 ak09970n = new AK09970N(PIN_SDA, PIN_SCL, MSU_AK09970N_I2C_ADDRESS) ; 00755 ak09970n->software_reset() ; 00756 ak09970n->getID(&companyID, &deviceID) ; 00757 printf("=== AK09970N ===\n") ; 00758 printf("Company ID = 0x%02X (expected 0x48)\n", companyID) ; 00759 printf("Device ID = 0x%02X (expected 0xC0)\n", deviceID) ; 00760 printf(" X Y Z\n") ; 00761 // status = ak09970n->getStatus() ; 00762 // printf("Status = 0x%04X\n", status) ; 00763 00764 for (int i = 0 ; i < test_loop ; i++ ) { 00765 // result = ak09970n->setConfig(0x08) ; /* 10Hz continuous mode */ 00766 result = ak09970n->singleShot() ; 00767 status = ak09970n->getStatus() ; 00768 while((status & 0x01) != 0x01) { /* wait for data ready */ 00769 wait_ms(10) ; 00770 status = ak09970n->getStatus() ; 00771 } 00772 ak09970n->getX_Y_Z(&status, &x, &y, &z) ; 00773 printf("%2.4f %2.4f %2.4f\n", x, y, z) ; 00774 wait_ms(100) ; 00775 } 00776 delete ak09970n ; 00777 }
Generated on Fri Jul 15 2022 06:46:16 by
1.7.2