fish

Dependencies:   mbed

Committer:
tzxl10000
Date:
Wed Jun 14 20:22:45 2017 +0000
Revision:
0:561f7672eaad
fish;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tzxl10000 0:561f7672eaad 1 /** LSM303DLHC Interface Library
tzxl10000 0:561f7672eaad 2 *
tzxl10000 0:561f7672eaad 3 * base on code by Michael Shimniok http://bot-thoughts.com
tzxl10000 0:561f7672eaad 4 *
tzxl10000 0:561f7672eaad 5 * and on test program by @tosihisa and
tzxl10000 0:561f7672eaad 6 *
tzxl10000 0:561f7672eaad 7 * and on Pololu sample library for LSM303DLHC breakout by ryantm:
tzxl10000 0:561f7672eaad 8 *
tzxl10000 0:561f7672eaad 9 * Copyright (c) 2011 Pololu Corporation. For more information, see
tzxl10000 0:561f7672eaad 10 *
tzxl10000 0:561f7672eaad 11 * http://www.pololu.com/
tzxl10000 0:561f7672eaad 12 * http://forum.pololu.com/
tzxl10000 0:561f7672eaad 13 *
tzxl10000 0:561f7672eaad 14 * Permission is hereby granted, free of charge, to any person
tzxl10000 0:561f7672eaad 15 * obtaining a copy of this software and associated documentation
tzxl10000 0:561f7672eaad 16 * files (the "Software"), to deal in the Software without
tzxl10000 0:561f7672eaad 17 * restriction, including without limitation the rights to use,
tzxl10000 0:561f7672eaad 18 * copy, modify, merge, publish, distribute, sublicense, and/or sell
tzxl10000 0:561f7672eaad 19 * copies of the Software, and to permit persons to whom the
tzxl10000 0:561f7672eaad 20 * Software is furnished to do so, subject to the following
tzxl10000 0:561f7672eaad 21 * conditions:
tzxl10000 0:561f7672eaad 22 *
tzxl10000 0:561f7672eaad 23 * The above copyright notice and this permission notice shall be
tzxl10000 0:561f7672eaad 24 * included in all copies or substantial portions of the Software.
tzxl10000 0:561f7672eaad 25 *
tzxl10000 0:561f7672eaad 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
tzxl10000 0:561f7672eaad 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
tzxl10000 0:561f7672eaad 28 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
tzxl10000 0:561f7672eaad 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
tzxl10000 0:561f7672eaad 30 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
tzxl10000 0:561f7672eaad 31 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
tzxl10000 0:561f7672eaad 32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tzxl10000 0:561f7672eaad 33 * OTHER DEALINGS IN THE SOFTWARE.
tzxl10000 0:561f7672eaad 34 */
tzxl10000 0:561f7672eaad 35 #include "mbed.h"
tzxl10000 0:561f7672eaad 36 #include "LSM303DLHC.h"
tzxl10000 0:561f7672eaad 37
tzxl10000 0:561f7672eaad 38
tzxl10000 0:561f7672eaad 39 const int addr_acc = 0x32;
tzxl10000 0:561f7672eaad 40 const int addr_mag = 0x3c;
tzxl10000 0:561f7672eaad 41
tzxl10000 0:561f7672eaad 42 enum REG_ADDRS {
tzxl10000 0:561f7672eaad 43 /* --- Mag --- */
tzxl10000 0:561f7672eaad 44 CRA_REG_M = 0x00,
tzxl10000 0:561f7672eaad 45 CRB_REG_M = 0x01,
tzxl10000 0:561f7672eaad 46 MR_REG_M = 0x02,
tzxl10000 0:561f7672eaad 47 OUT_X_M = 0x03,
tzxl10000 0:561f7672eaad 48 OUT_Y_M = 0x05,
tzxl10000 0:561f7672eaad 49 OUT_Z_M = 0x07,
tzxl10000 0:561f7672eaad 50 /* --- Acc --- */
tzxl10000 0:561f7672eaad 51 CTRL_REG1_A = 0x20,
tzxl10000 0:561f7672eaad 52 CTRL_REG4_A = 0x23,
tzxl10000 0:561f7672eaad 53 OUT_X_A = 0x28,
tzxl10000 0:561f7672eaad 54 OUT_Y_A = 0x2A,
tzxl10000 0:561f7672eaad 55 OUT_Z_A = 0x2C,
tzxl10000 0:561f7672eaad 56 };
tzxl10000 0:561f7672eaad 57
tzxl10000 0:561f7672eaad 58 bool LSM303DLHC::write_reg(int addr_i2c,int addr_reg, char v)
tzxl10000 0:561f7672eaad 59 {
tzxl10000 0:561f7672eaad 60 char data[2] = {addr_reg, v};
tzxl10000 0:561f7672eaad 61 return LSM303DLHC::_LSM303.write(addr_i2c, data, 2) == 0;
tzxl10000 0:561f7672eaad 62 }
tzxl10000 0:561f7672eaad 63
tzxl10000 0:561f7672eaad 64 bool LSM303DLHC::read_reg(int addr_i2c,int addr_reg, char *v)
tzxl10000 0:561f7672eaad 65 {
tzxl10000 0:561f7672eaad 66 char data = addr_reg;
tzxl10000 0:561f7672eaad 67 bool result = false;
tzxl10000 0:561f7672eaad 68
tzxl10000 0:561f7672eaad 69 __disable_irq();
tzxl10000 0:561f7672eaad 70 if ((_LSM303.write(addr_i2c, &data, 1) == 0) && (_LSM303.read(addr_i2c, &data, 1) == 0)){
tzxl10000 0:561f7672eaad 71 *v = data;
tzxl10000 0:561f7672eaad 72 result = true;
tzxl10000 0:561f7672eaad 73 }
tzxl10000 0:561f7672eaad 74 __enable_irq();
tzxl10000 0:561f7672eaad 75 return result;
tzxl10000 0:561f7672eaad 76 }
tzxl10000 0:561f7672eaad 77
tzxl10000 0:561f7672eaad 78
tzxl10000 0:561f7672eaad 79 LSM303DLHC::LSM303DLHC(PinName sda, PinName scl):
tzxl10000 0:561f7672eaad 80 _LSM303(sda, scl)
tzxl10000 0:561f7672eaad 81 {
tzxl10000 0:561f7672eaad 82 char reg_v;
tzxl10000 0:561f7672eaad 83 _LSM303.frequency(100000);
tzxl10000 0:561f7672eaad 84
tzxl10000 0:561f7672eaad 85 reg_v = 0;
tzxl10000 0:561f7672eaad 86
tzxl10000 0:561f7672eaad 87 reg_v |= 0x27; /* X/Y/Z axis enable. */
tzxl10000 0:561f7672eaad 88 write_reg(addr_acc,CTRL_REG1_A,reg_v);
tzxl10000 0:561f7672eaad 89
tzxl10000 0:561f7672eaad 90 reg_v = 0;
tzxl10000 0:561f7672eaad 91 // reg_v |= 0x01 << 6; /* 1: data MSB @ lower address */
tzxl10000 0:561f7672eaad 92 reg_v = 0x01 << 4; /* +/- 4g */
tzxl10000 0:561f7672eaad 93 write_reg(addr_acc,CTRL_REG4_A,reg_v);
tzxl10000 0:561f7672eaad 94
tzxl10000 0:561f7672eaad 95 /* -- mag --- */
tzxl10000 0:561f7672eaad 96 reg_v = 0;
tzxl10000 0:561f7672eaad 97 reg_v |= 0x04 << 2; /* Minimum data output rate = 15Hz */
tzxl10000 0:561f7672eaad 98 write_reg(addr_mag,CRA_REG_M,reg_v);
tzxl10000 0:561f7672eaad 99
tzxl10000 0:561f7672eaad 100 reg_v = 0;
tzxl10000 0:561f7672eaad 101 reg_v |= 0x01 << 5; /* +-1.3Gauss */
tzxl10000 0:561f7672eaad 102 //reg_v |= 0x07 << 5; /* +-8.1Gauss */
tzxl10000 0:561f7672eaad 103 write_reg(addr_mag,CRB_REG_M,reg_v);
tzxl10000 0:561f7672eaad 104
tzxl10000 0:561f7672eaad 105 reg_v = 0; /* Continuous-conversion mode */
tzxl10000 0:561f7672eaad 106 write_reg(addr_mag,MR_REG_M,reg_v);
tzxl10000 0:561f7672eaad 107 }
tzxl10000 0:561f7672eaad 108
tzxl10000 0:561f7672eaad 109
tzxl10000 0:561f7672eaad 110 bool LSM303DLHC::read(float *ax, float *ay, float *az, float *mx, float *my, float *mz) {
tzxl10000 0:561f7672eaad 111 char acc[6], mag[6];
tzxl10000 0:561f7672eaad 112
tzxl10000 0:561f7672eaad 113 if (recv(addr_acc, OUT_X_A, acc, 6) && recv(addr_mag, OUT_X_M, mag, 6)) {
tzxl10000 0:561f7672eaad 114 *ax = float(short(acc[1] << 8 | acc[0]))/8192; //32768/4=8192
tzxl10000 0:561f7672eaad 115 *ay = float(short(acc[3] << 8 | acc[2]))/8192;
tzxl10000 0:561f7672eaad 116 *az = float(short(acc[5] << 8 | acc[4]))/8192;
tzxl10000 0:561f7672eaad 117 //full scale magnetic readings are from -2048 to 2047
tzxl10000 0:561f7672eaad 118 //gain is x,y =1100; z = 980 LSB/gauss
tzxl10000 0:561f7672eaad 119 *mx = float(short(mag[0] << 8 | mag[1]))/1100;
tzxl10000 0:561f7672eaad 120 *mz = float(short(mag[2] << 8 | mag[3]))/980;
tzxl10000 0:561f7672eaad 121 *my = float(short(mag[4] << 8 | mag[5]))/1100;
tzxl10000 0:561f7672eaad 122
tzxl10000 0:561f7672eaad 123 return true;
tzxl10000 0:561f7672eaad 124 }
tzxl10000 0:561f7672eaad 125
tzxl10000 0:561f7672eaad 126 return false;
tzxl10000 0:561f7672eaad 127 }
tzxl10000 0:561f7672eaad 128
tzxl10000 0:561f7672eaad 129
tzxl10000 0:561f7672eaad 130 bool LSM303DLHC::recv(char sad, char sub, char *buf, int length) {
tzxl10000 0:561f7672eaad 131 if (length > 1) sub |= 0x80;
tzxl10000 0:561f7672eaad 132
tzxl10000 0:561f7672eaad 133 return _LSM303.write(sad, &sub, 1, true) == 0 && _LSM303.read(sad, buf, length) == 0;
tzxl10000 0:561f7672eaad 134 }