BMC150 magnetometer and accelerometer library.

Dependents:   acd52832_BMC150_example acd52832_bmc150_test

Committer:
Dautor
Date:
Fri Sep 15 15:01:53 2017 +0000
Revision:
10:1d1123b3df76
Parent:
9:2d91d90f507d
Power consumption optimizations.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 1:f0d2ab09e3f8 1 /*
jurica238814 4:b1e0b3bd9c69 2 * Bosch BMC150 6-axis eCompass library.
jurica238814 4:b1e0b3bd9c69 3 * Developed by Jurica Resetar @ aconno, 2017
jurica238814 4:b1e0b3bd9c69 4 * More info @ aconno.de
jurica238814 4:b1e0b3bd9c69 5 * All rights reserved.
jurica238814 4:b1e0b3bd9c69 6 *
jurica238814 1:f0d2ab09e3f8 7 */
Dautor 0:3c8445053987 8
Dautor 0:3c8445053987 9 #include "bmc150.h"
jurica238814 2:9f106361d0fc 10 #include "utilities.h"
jurica238814 1:f0d2ab09e3f8 11 #include "bmc150_registers.h"
Dautor 0:3c8445053987 12
Dautor 8:49d185e97192 13 BMC150::BMC150(SPI spi, NRF52_DigitalOut cs) : spi(spi), cs(cs){
Dautor 0:3c8445053987 14 }
Dautor 0:3c8445053987 15
Dautor 6:842aec52497b 16 vector3_f BMC150::readAccelerometer_f(){
Dautor 6:842aec52497b 17 vector3_f result;
Dautor 6:842aec52497b 18 vector3_s data = readAccelerometer();
Dautor 6:842aec52497b 19 floatToBuffer((float)data.x*ACC_LSB_VALUE*0.001f, (uint8_t *)&result.x);
Dautor 6:842aec52497b 20 floatToBuffer((float)data.y*ACC_LSB_VALUE*0.001f, (uint8_t *)&result.y);
Dautor 6:842aec52497b 21 floatToBuffer((float)data.z*ACC_LSB_VALUE*0.001f, (uint8_t *)&result.z);
Dautor 6:842aec52497b 22 return result;
Dautor 6:842aec52497b 23 }
Dautor 6:842aec52497b 24
jurica238814 2:9f106361d0fc 25 bool BMC150::setRegister(uint8_t regAddress, uint8_t data){
jurica238814 4:b1e0b3bd9c69 26 cs = 0;
jurica238814 4:b1e0b3bd9c69 27 spi.write(regAddress & SPI_WRITE); // MSB bit has to be LOW to indicate Write command
jurica238814 4:b1e0b3bd9c69 28 spi.write(data);
jurica238814 4:b1e0b3bd9c69 29 cs = 1;
jurica238814 4:b1e0b3bd9c69 30 return 0;
jurica238814 2:9f106361d0fc 31 }
jurica238814 2:9f106361d0fc 32
jurica238814 3:a4e28930c2a9 33 uint8_t BMC150::getRegister(uint8_t regAddress){
jurica238814 4:b1e0b3bd9c69 34 uint8_t data;
jurica238814 4:b1e0b3bd9c69 35 cs = 0;
jurica238814 4:b1e0b3bd9c69 36 spi.write(regAddress | SPI_READ);
jurica238814 4:b1e0b3bd9c69 37 data = spi.write(SPI_DUMMY_BYTE | SPI_READ);
jurica238814 4:b1e0b3bd9c69 38 cs = 1;
jurica238814 4:b1e0b3bd9c69 39 return (data);
jurica238814 2:9f106361d0fc 40 }
jurica238814 2:9f106361d0fc 41
Dautor 0:3c8445053987 42 vector3_s BMC150::readAccelerometer(){
Dautor 0:3c8445053987 43 vector3_s result;
Dautor 0:3c8445053987 44 int8_t msb, lsb;
jurica238814 2:9f106361d0fc 45
jurica238814 3:a4e28930c2a9 46 lsb = getRegister(DATA_ACC_X_LSB);
jurica238814 3:a4e28930c2a9 47 msb = getRegister(DATA_ACC_X_MSB);
jurica238814 3:a4e28930c2a9 48 result.x = convert<4>(msb,lsb);
jurica238814 2:9f106361d0fc 49 lsb = getRegister(DATA_ACC_Y_LSB);
jurica238814 2:9f106361d0fc 50 msb = getRegister(DATA_ACC_Y_MSB);
jurica238814 3:a4e28930c2a9 51 result.y = convert<4>(msb,lsb);
jurica238814 3:a4e28930c2a9 52 lsb = getRegister(DATA_ACC_Z_LSB);
jurica238814 3:a4e28930c2a9 53 msb = getRegister(DATA_ACC_Z_MSB);
jurica238814 3:a4e28930c2a9 54 result.z = convert<4>(msb,lsb);
Dautor 0:3c8445053987 55 return result;
Dautor 0:3c8445053987 56 }
jurica238814 1:f0d2ab09e3f8 57
Dautor 0:3c8445053987 58 vector3_s BMC150::readMagnetometer(){
Dautor 0:3c8445053987 59 vector3_s result;
jurica238814 2:9f106361d0fc 60 int8_t msb, lsb;
jurica238814 2:9f106361d0fc 61
jurica238814 3:a4e28930c2a9 62 lsb = getRegister(DATA_MAG_X_LSB);
jurica238814 3:a4e28930c2a9 63 msb = getRegister(DATA_MAG_X_MSB);
jurica238814 3:a4e28930c2a9 64 result.x = convert<5>(msb,lsb);
jurica238814 3:a4e28930c2a9 65 lsb = getRegister(DATA_MAG_Y_LSB);
jurica238814 3:a4e28930c2a9 66 msb = getRegister(DATA_MAG_Y_MSB);
jurica238814 3:a4e28930c2a9 67 result.y = convert<5>(msb,lsb);
jurica238814 3:a4e28930c2a9 68 lsb = getRegister(DATA_MAG_Z_LSB);
jurica238814 3:a4e28930c2a9 69 msb = getRegister(DATA_MAG_Z_MSB);
jurica238814 3:a4e28930c2a9 70 result.z = convert<7>(msb,lsb);
jurica238814 2:9f106361d0fc 71
Dautor 0:3c8445053987 72 return result;
Dautor 0:3c8445053987 73 }
Dautor 0:3c8445053987 74
jurica238814 1:f0d2ab09e3f8 75 uint8_t BMC150::readTemperature(){
jurica238814 5:33eaee42ee94 76 uint8_t temperature;
jurica238814 5:33eaee42ee94 77 cs = 1;
jurica238814 5:33eaee42ee94 78 wait_ms(1);
jurica238814 5:33eaee42ee94 79 spi.write(0x08);
jurica238814 5:33eaee42ee94 80 wait_ms(1);
jurica238814 5:33eaee42ee94 81 temperature = spi.write(0x00);
jurica238814 5:33eaee42ee94 82 wait_ms(1);
jurica238814 5:33eaee42ee94 83 cs = 0;
jurica238814 1:f0d2ab09e3f8 84 return temperature;
jurica238814 1:f0d2ab09e3f8 85 }
jurica238814 1:f0d2ab09e3f8 86
Dautor 0:3c8445053987 87 void BMC150::calibrateAccelerometer(){
Dautor 0:3c8445053987 88 cs = 0;
Dautor 8:49d185e97192 89 // perform
Dautor 0:3c8445053987 90 cs = 1;
Dautor 0:3c8445053987 91 }
jurica238814 3:a4e28930c2a9 92 void BMC150::initMagnetometer(){
jurica238814 3:a4e28930c2a9 93 setRegister(0x4B, 0x01); // Set Power Control bit.
Dautor 9:2d91d90f507d 94 wait_ms(10);
jurica238814 3:a4e28930c2a9 95 setRegister(0x4C, 0x38); // Change magnetometer mode in Active mode
jurica238814 3:a4e28930c2a9 96 setRegister(0x4E, 0x07); // Enable magnetometer axes
Dautor 7:929be791ff09 97 setRegister(0x4D, 0xFF); // Enable interrupts for every axis and override
Dautor 10:1d1123b3df76 98 }