Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
l3g4200.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2011 Gautier Hattenberger <gautier.hattenberger@enac.fr>
4  * 2013 Felix Ruess <felix.ruess@gmail.com>
5  * 2013 Eduardo Lavratti <agressiva@hotmail.com>
6  *
7  * This file is part of paparazzi.
8  *
9  * paparazzi is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * paparazzi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with paparazzi; see the file COPYING. If not, write to
21  * the Free Software Foundation, 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24 
31 #include "peripherals/l3g4200.h"
32 #include "std.h"
33 
37 }
38 
46 void l3g4200_init(struct L3g4200 *l3g, struct i2c_periph *i2c_p, uint8_t addr)
47 {
48  /* set i2c_peripheral */
49  l3g->i2c_p = i2c_p;
50  /* set i2c address */
51  l3g->i2c_trans.slave_addr = addr;
53  /* set default config options */
55  l3g->initialized = FALSE;
57 }
58 
59 static void l3g4200_i2c_tx_reg(struct L3g4200 *l3g, uint8_t reg, uint8_t val)
60 {
61  l3g->i2c_trans.buf[0] = reg;
62  l3g->i2c_trans.buf[1] = val;
63  i2c_transmit(l3g->i2c_p, &(l3g->i2c_trans), l3g->i2c_trans.slave_addr, 2);
64 }
65 
66 // Configuration function called once before normal use
67 static void l3g4200_send_config(struct L3g4200 *l3g)
68 {
69  switch (l3g->init_status) {
70  case L3G_CONF_REG1:
72  l3g->init_status++;
73  break;
74  case L3G_CONF_REG5:
76  l3g->init_status++;
77  break;
78  case L3G_CONF_DONE:
79  l3g->initialized = TRUE;
81  break;
82  default:
83  break;
84  }
85 }
86 
87 // Configure
89 {
90  if (l3g->init_status == L3G_CONF_UNINIT) {
91  l3g->init_status++;
94  }
95  }
96 }
97 
98 // Normal reading
99 void l3g4200_read(struct L3g4200 *l3g)
100 {
101  if (l3g->initialized && l3g->i2c_trans.status == I2CTransDone) {
103  i2c_transceive(l3g->i2c_p, &(l3g->i2c_trans), l3g->i2c_trans.slave_addr, 1, 9);
104  }
105 }
106 
107 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
108 
109 void l3g4200_event(struct L3g4200 *l3g)
110 {
111  if (l3g->initialized) {
112  if (l3g->i2c_trans.status == I2CTransFailed) {
114  }
115  else if (l3g->i2c_trans.status == I2CTransSuccess) {
116  // Successfull reading and new data available
117  if (l3g->i2c_trans.buf[0] & 0x01) { // ver oque é o sinal antes do &
118  // New data available
119  l3g->data.rates.p = Int16FromBuf(l3g->i2c_trans.buf,3);
120  l3g->data.rates.q = Int16FromBuf(l3g->i2c_trans.buf,5);
121  l3g->data.rates.r = Int16FromBuf(l3g->i2c_trans.buf,7);
122  l3g->data_available = TRUE;
123  }
125  }
126  }
127  else if (l3g->init_status != L3G_CONF_UNINIT) { // Configuring but not yet initialized
128  if (l3g->i2c_trans.status == I2CTransSuccess || l3g->i2c_trans.status == I2CTransDone) {
130  l3g4200_send_config(l3g);
131  }
132  if (l3g->i2c_trans.status == I2CTransFailed) {
133  l3g->init_status--;
135  l3g4200_send_config(l3g); // Retry config (TODO max retry)
136  }
137  }
138 }
void l3g4200_set_default_config(struct L3g4200Config *c)
Definition: l3g4200.c:34
void l3g4200_event(struct L3g4200 *l3g)
Definition: l3g4200.c:109
int32_t p
in rad/s^2 with INT32_RATE_FRAC
#define L3G4200_REG_STATUS_REG
Definition: l3g4200_regs.h:44
#define Int16FromBuf(_buf, _idx)
Definition: l3g4200.c:107
struct Int32Rates rates
data as angular rates in gyro coordinate system
Definition: l3g4200.h:71
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Definition: i2c.c:105
void l3g4200_read(struct L3g4200 *l3g)
Definition: l3g4200.c:99
uint8_t slave_addr
Definition: i2c.h:79
#define FALSE
Definition: imu_chimu.h:141
bool_t initialized
config done flag
Definition: l3g4200.h:67
struct i2c_periph * i2c_p
Definition: l3g4200.h:65
uint8_t ctrl_reg1
Definition: l3g4200.h:52
#define L3G4200_REG_CTRL_REG5
Definition: l3g4200_regs.h:41
#define L3G4200_REG_CTRL_REG1
Definition: l3g4200_regs.h:37
union L3g4200::@22 data
#define L3G4200_DEFAULT_CTRL_REG5
Definition: l3g4200.h:49
uint16_t val[TCOUPLE_NB]
bool_t i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Definition: i2c.c:85
volatile bool_t data_available
data ready flag
Definition: l3g4200.h:69
static void l3g4200_i2c_tx_reg(struct L3g4200 *l3g, uint8_t reg, uint8_t val)
Definition: l3g4200.c:59
enum I2CTransactionStatus status
Definition: i2c.h:83
void l3g4200_init(struct L3g4200 *l3g, struct i2c_periph *i2c_p, uint8_t addr)
Initialize L3g4200 struct and set default config options.
Definition: l3g4200.c:46
uint8_t ctrl_reg5
Definition: l3g4200.h:53
static void l3g4200_send_config(struct L3g4200 *l3g)
Definition: l3g4200.c:67
#define TRUE
Definition: imu_chimu.h:144
void l3g4200_start_configure(struct L3g4200 *l3g)
Definition: l3g4200.c:88
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
struct L3g4200Config config
Definition: l3g4200.h:74
unsigned char uint8_t
Definition: types.h:14
enum L3g4200ConfStatus init_status
init status
Definition: l3g4200.h:68
Definition: i2c.h:90
int32_t q
in rad/s^2 with INT32_RATE_FRAC
int32_t r
in rad/s^2 with INT32_RATE_FRAC
static struct point c
Definition: discsurvey.c:39
struct i2c_transaction i2c_trans
Definition: l3g4200.h:66
#define L3G4200_DEFAULT_CTRL_REG1
Definition: l3g4200.h:48
Driver for the gyro L3G4200 From ST.