Adafruit 9DOF - L3GD20H Driver

Dependents:   Galileo_HoverBoardRider projetinterface

Committer:
julioefajardo
Date:
Sat Apr 09 04:27:11 2016 +0000
Revision:
0:81b60e58e9e1
Adafruit 9 DOF - L3GD20H Driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
julioefajardo 0:81b60e58e9e1 1 /**
julioefajardo 0:81b60e58e9e1 2 * Copyright (c) 2011 Pololu Corporation. For more information, see
julioefajardo 0:81b60e58e9e1 3 *
julioefajardo 0:81b60e58e9e1 4 * http://www.pololu.com/
julioefajardo 0:81b60e58e9e1 5 * http://forum.pololu.com/
julioefajardo 0:81b60e58e9e1 6 *
julioefajardo 0:81b60e58e9e1 7 * Permission is hereby granted, free of charge, to any person
julioefajardo 0:81b60e58e9e1 8 * obtaining a copy of this software and associated documentation
julioefajardo 0:81b60e58e9e1 9 * files (the "Software"), to deal in the Software without
julioefajardo 0:81b60e58e9e1 10 * restriction, including without limitation the rights to use,
julioefajardo 0:81b60e58e9e1 11 * copy, modify, merge, publish, distribute, sublicense, and/or sell
julioefajardo 0:81b60e58e9e1 12 * copies of the Software, and to permit persons to whom the
julioefajardo 0:81b60e58e9e1 13 * Software is furnished to do so, subject to the following
julioefajardo 0:81b60e58e9e1 14 * conditions:
julioefajardo 0:81b60e58e9e1 15 *
julioefajardo 0:81b60e58e9e1 16 * The above copyright notice and this permission notice shall be
julioefajardo 0:81b60e58e9e1 17 * included in all copies or substantial portions of the Software.
julioefajardo 0:81b60e58e9e1 18 *
julioefajardo 0:81b60e58e9e1 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
julioefajardo 0:81b60e58e9e1 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
julioefajardo 0:81b60e58e9e1 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
julioefajardo 0:81b60e58e9e1 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
julioefajardo 0:81b60e58e9e1 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
julioefajardo 0:81b60e58e9e1 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
julioefajardo 0:81b60e58e9e1 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
julioefajardo 0:81b60e58e9e1 26 * OTHER DEALINGS IN THE SOFTWARE.
julioefajardo 0:81b60e58e9e1 27 */
julioefajardo 0:81b60e58e9e1 28
julioefajardo 0:81b60e58e9e1 29 #include "mbed.h"
julioefajardo 0:81b60e58e9e1 30 #include "L3GD20H.h"
julioefajardo 0:81b60e58e9e1 31
julioefajardo 0:81b60e58e9e1 32
julioefajardo 0:81b60e58e9e1 33 // Defines ////////////////////////////////////////////////////////////////
julioefajardo 0:81b60e58e9e1 34
julioefajardo 0:81b60e58e9e1 35 // The Arduino two-wire interface uses a 7-bit number for the address,
julioefajardo 0:81b60e58e9e1 36 // and sets the last bit correctly based on reads and writes
julioefajardo 0:81b60e58e9e1 37 // mbed I2C libraries take the 7-bit address shifted left 1 bit
julioefajardo 0:81b60e58e9e1 38 // #define GYR_ADDRESS (0xD2 >> 1)
julioefajardo 0:81b60e58e9e1 39 #define GYR_ADDRESS 0xD6
julioefajardo 0:81b60e58e9e1 40
julioefajardo 0:81b60e58e9e1 41
julioefajardo 0:81b60e58e9e1 42 // Public Methods //////////////////////////////////////////////////////////////
julioefajardo 0:81b60e58e9e1 43
julioefajardo 0:81b60e58e9e1 44 // Constructor
julioefajardo 0:81b60e58e9e1 45 L3GD20H::L3GD20H(PinName sda, PinName scl):
julioefajardo 0:81b60e58e9e1 46 _L3GD20H(sda, scl)
julioefajardo 0:81b60e58e9e1 47 {
julioefajardo 0:81b60e58e9e1 48 char reg_v;
julioefajardo 0:81b60e58e9e1 49 _L3GD20H.frequency(100000);
julioefajardo 0:81b60e58e9e1 50
julioefajardo 0:81b60e58e9e1 51 // 0x0F = 0b00001111
julioefajardo 0:81b60e58e9e1 52 // Normal power mode, all axes enabled
julioefajardo 0:81b60e58e9e1 53 reg_v = 0;
julioefajardo 0:81b60e58e9e1 54 reg_v |= 0x0F;
julioefajardo 0:81b60e58e9e1 55 write_reg(GYR_ADDRESS,L3GD20_CTRL_REG1,reg_v);
julioefajardo 0:81b60e58e9e1 56
julioefajardo 0:81b60e58e9e1 57
julioefajardo 0:81b60e58e9e1 58
julioefajardo 0:81b60e58e9e1 59 }
julioefajardo 0:81b60e58e9e1 60
julioefajardo 0:81b60e58e9e1 61
julioefajardo 0:81b60e58e9e1 62
julioefajardo 0:81b60e58e9e1 63 bool L3GD20H::read(short g[3]) {
julioefajardo 0:81b60e58e9e1 64 char gyr[6];
julioefajardo 0:81b60e58e9e1 65
julioefajardo 0:81b60e58e9e1 66 if (recv(GYR_ADDRESS, L3GD20_OUT_X_L, gyr, 6)) {
julioefajardo 0:81b60e58e9e1 67 //scale is 1 dps/digit
julioefajardo 0:81b60e58e9e1 68 g[0] = (gyr[1] << 8 | gyr[0]);
julioefajardo 0:81b60e58e9e1 69 g[1] = (gyr[3] << 8 | gyr[2]);
julioefajardo 0:81b60e58e9e1 70 g[2] = (gyr[5] << 8 | gyr[4]);
julioefajardo 0:81b60e58e9e1 71
julioefajardo 0:81b60e58e9e1 72
julioefajardo 0:81b60e58e9e1 73 return true;
julioefajardo 0:81b60e58e9e1 74 }
julioefajardo 0:81b60e58e9e1 75
julioefajardo 0:81b60e58e9e1 76 return false;
julioefajardo 0:81b60e58e9e1 77 }
julioefajardo 0:81b60e58e9e1 78
julioefajardo 0:81b60e58e9e1 79
julioefajardo 0:81b60e58e9e1 80
julioefajardo 0:81b60e58e9e1 81
julioefajardo 0:81b60e58e9e1 82 bool L3GD20H::write_reg(int addr_i2c,int addr_reg, char v)
julioefajardo 0:81b60e58e9e1 83 {
julioefajardo 0:81b60e58e9e1 84 char data[2] = {addr_reg, v};
julioefajardo 0:81b60e58e9e1 85 return L3GD20H::_L3GD20H.write(addr_i2c, data, 2) == 0;
julioefajardo 0:81b60e58e9e1 86 }
julioefajardo 0:81b60e58e9e1 87
julioefajardo 0:81b60e58e9e1 88 bool L3GD20H::read_reg(int addr_i2c,int addr_reg, char *v)
julioefajardo 0:81b60e58e9e1 89 {
julioefajardo 0:81b60e58e9e1 90 char data = addr_reg;
julioefajardo 0:81b60e58e9e1 91 bool result = false;
julioefajardo 0:81b60e58e9e1 92
julioefajardo 0:81b60e58e9e1 93 __disable_irq();
julioefajardo 0:81b60e58e9e1 94 if ((_L3GD20H.write(addr_i2c, &data, 1) == 0) && (_L3GD20H.read(addr_i2c, &data, 1) == 0)){
julioefajardo 0:81b60e58e9e1 95 *v = data;
julioefajardo 0:81b60e58e9e1 96 result = true;
julioefajardo 0:81b60e58e9e1 97 }
julioefajardo 0:81b60e58e9e1 98 __enable_irq();
julioefajardo 0:81b60e58e9e1 99 return result;
julioefajardo 0:81b60e58e9e1 100 }
julioefajardo 0:81b60e58e9e1 101
julioefajardo 0:81b60e58e9e1 102
julioefajardo 0:81b60e58e9e1 103 bool L3GD20H::recv(char sad, char sub, char *buf, int length) {
julioefajardo 0:81b60e58e9e1 104 if (length > 1) sub |= 0x80;
julioefajardo 0:81b60e58e9e1 105
julioefajardo 0:81b60e58e9e1 106 return _L3GD20H.write(sad, &sub, 1, true) == 0 && _L3GD20H.read(sad, buf, length) == 0;
julioefajardo 0:81b60e58e9e1 107 }