initial

Dependents:   Mt05_MtSense03

Committer:
johnathanlyu
Date:
Fri Apr 27 09:55:36 2018 +0000
Revision:
0:4f5c0b7acaa8
update library flow

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnathanlyu 0:4f5c0b7acaa8 1 /* Copyright (c) 2016 MtM Technology Corporation, MIT License
johnathanlyu 0:4f5c0b7acaa8 2 *
johnathanlyu 0:4f5c0b7acaa8 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
johnathanlyu 0:4f5c0b7acaa8 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
johnathanlyu 0:4f5c0b7acaa8 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
johnathanlyu 0:4f5c0b7acaa8 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
johnathanlyu 0:4f5c0b7acaa8 7 * furnished to do so, subject to the following conditions:
johnathanlyu 0:4f5c0b7acaa8 8 *
johnathanlyu 0:4f5c0b7acaa8 9 * The above copyright notice and this permission notice shall be included in all copies or
johnathanlyu 0:4f5c0b7acaa8 10 * substantial portions of the Software.
johnathanlyu 0:4f5c0b7acaa8 11 *
johnathanlyu 0:4f5c0b7acaa8 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
johnathanlyu 0:4f5c0b7acaa8 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
johnathanlyu 0:4f5c0b7acaa8 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
johnathanlyu 0:4f5c0b7acaa8 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
johnathanlyu 0:4f5c0b7acaa8 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
johnathanlyu 0:4f5c0b7acaa8 17 */
johnathanlyu 0:4f5c0b7acaa8 18 #include "BMG160.h"
johnathanlyu 0:4f5c0b7acaa8 19
johnathanlyu 0:4f5c0b7acaa8 20 /*******************************
johnathanlyu 0:4f5c0b7acaa8 21 * Public methods
johnathanlyu 0:4f5c0b7acaa8 22 *******************************/
johnathanlyu 0:4f5c0b7acaa8 23 BMG160::BMG160(PinName i2c_sda, PinName i2c_scl, PinName interrupt1_pinname, PinName interrupt2_pinname,uint8_t range, uint8_t bandwith)
johnathanlyu 0:4f5c0b7acaa8 24 : i2c_(i2c_sda, i2c_scl),
johnathanlyu 0:4f5c0b7acaa8 25 interrupt1_pinname_(interrupt1_pinname),
johnathanlyu 0:4f5c0b7acaa8 26 interrupt1_(interrupt1_pinname),
johnathanlyu 0:4f5c0b7acaa8 27 interrupt2_pinname_(interrupt2_pinname),
johnathanlyu 0:4f5c0b7acaa8 28 interrupt2_(interrupt2_pinname),
johnathanlyu 0:4f5c0b7acaa8 29 range_(range), bandwith_(bandwith) {
johnathanlyu 0:4f5c0b7acaa8 30
johnathanlyu 0:4f5c0b7acaa8 31 /* Basic */
johnathanlyu 0:4f5c0b7acaa8 32 RegWrite(0x10, bandwith_); // bandwidth
johnathanlyu 0:4f5c0b7acaa8 33 RegWrite(0x0F, range_); // range
johnathanlyu 0:4f5c0b7acaa8 34 RegWrite(0x1A, 0x20); // slow_offset_unfilt = 1
johnathanlyu 0:4f5c0b7acaa8 35 RegWrite(0x31, 0x07); // SOC_REG slow_offset_en_x/y/z = 1
johnathanlyu 0:4f5c0b7acaa8 36
johnathanlyu 0:4f5c0b7acaa8 37 }
johnathanlyu 0:4f5c0b7acaa8 38
johnathanlyu 0:4f5c0b7acaa8 39 void BMG160::ReadXYZ(int16_t *xyz) {
johnathanlyu 0:4f5c0b7acaa8 40 char val[6];
johnathanlyu 0:4f5c0b7acaa8 41
johnathanlyu 0:4f5c0b7acaa8 42 /* Read raw data */
johnathanlyu 0:4f5c0b7acaa8 43 RegRead(0x02, val, sizeof(val));
johnathanlyu 0:4f5c0b7acaa8 44 xyz[0] = ((int16_t)val[1] << 8) | (val[0] & 0xC0);
johnathanlyu 0:4f5c0b7acaa8 45 xyz[1] = ((int16_t)val[3] << 8) | (val[2] & 0xC0);
johnathanlyu 0:4f5c0b7acaa8 46 xyz[2] = ((int16_t)val[5] << 8) | (val[4] & 0xC0);
johnathanlyu 0:4f5c0b7acaa8 47
johnathanlyu 0:4f5c0b7acaa8 48 /* Align right */
johnathanlyu 0:4f5c0b7acaa8 49 xyz[0] >>= 6;
johnathanlyu 0:4f5c0b7acaa8 50 xyz[1] >>= 6;
johnathanlyu 0:4f5c0b7acaa8 51 xyz[2] >>= 6;
johnathanlyu 0:4f5c0b7acaa8 52 }
johnathanlyu 0:4f5c0b7acaa8 53 /*******************************
johnathanlyu 0:4f5c0b7acaa8 54 * Private methods
johnathanlyu 0:4f5c0b7acaa8 55 *******************************/
johnathanlyu 0:4f5c0b7acaa8 56 void BMG160::RegWrite(char reg, char val) {
johnathanlyu 0:4f5c0b7acaa8 57 char data[2];
johnathanlyu 0:4f5c0b7acaa8 58 data[0] = reg;
johnathanlyu 0:4f5c0b7acaa8 59 data[1] = val;
johnathanlyu 0:4f5c0b7acaa8 60 i2c_.write(BMG160_SLAVE_ADDR, data, 2, 0);
johnathanlyu 0:4f5c0b7acaa8 61 }
johnathanlyu 0:4f5c0b7acaa8 62
johnathanlyu 0:4f5c0b7acaa8 63 void BMG160::RegRead(char reg, char *val, int len) {
johnathanlyu 0:4f5c0b7acaa8 64 i2c_.write(BMG160_SLAVE_ADDR, &reg, 1, 0);
johnathanlyu 0:4f5c0b7acaa8 65 i2c_.read (BMG160_SLAVE_ADDR, val, len);
johnathanlyu 0:4f5c0b7acaa8 66 }