Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
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  * Copyright (C) 2011 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3  * 2013 Felix Ruess <felix.ruess@gmail.com>
4  * 2013 Eduardo Lavratti <agressiva@hotmail.com>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
30 #include "peripherals/l3g4200.h"
31 #include "std.h"
32 
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_REG4:
76  l3g->init_status++;
77  break;
78  case L3G_CONF_REG5:
80  l3g->init_status++;
81  break;
82  case L3G_CONF_DONE:
83  l3g->initialized = TRUE;
85  break;
86  default:
87  break;
88  }
89 }
90 
91 // Configure
93 {
94  if (l3g->init_status == L3G_CONF_UNINIT) {
95  l3g->init_status++;
98  }
99  }
100 }
101 
102 // Normal reading
103 void l3g4200_read(struct L3g4200 *l3g)
104 {
105  if (l3g->initialized && l3g->i2c_trans.status == I2CTransDone) {
106  l3g->i2c_trans.buf[0] = 0x80 | L3G4200_REG_STATUS_REG;
107  i2c_transceive(l3g->i2c_p, &(l3g->i2c_trans), l3g->i2c_trans.slave_addr, 1, 7);
108  }
109 }
110 
111 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx+1]<<8) | _buf[_idx]))
112 
113 void l3g4200_event(struct L3g4200 *l3g)
114 {
115  if (l3g->initialized) {
116  if (l3g->i2c_trans.status == I2CTransFailed) {
118  }
119  else if (l3g->i2c_trans.status == I2CTransSuccess) {
120  // Successfull reading and new data available
121  if (l3g->i2c_trans.buf[0] & 0x08) {
122  // New data available
123  l3g->data.rates.p = Int16FromBuf(l3g->i2c_trans.buf,1);
124  l3g->data.rates.q = Int16FromBuf(l3g->i2c_trans.buf,3);
125  l3g->data.rates.r = Int16FromBuf(l3g->i2c_trans.buf,5);
126  l3g->data_available = TRUE;
127  }
129  }
130  }
131  else if (l3g->init_status != L3G_CONF_UNINIT) { // Configuring but not yet initialized
132  if (l3g->i2c_trans.status == I2CTransSuccess || l3g->i2c_trans.status == I2CTransDone) {
134  l3g4200_send_config(l3g);
135  }
136  if (l3g->i2c_trans.status == I2CTransFailed) {
137  l3g->init_status--;
139  l3g4200_send_config(l3g); // Retry config (TODO max retry)
140  }
141  }
142 }
void l3g4200_set_default_config(struct L3g4200Config *c)
Definition: l3g4200.c:33
void l3g4200_event(struct L3g4200 *l3g)
Definition: l3g4200.c:113
transaction successfully finished by I2C driver
Definition: i2c.h:57
union L3g4200::@31 data
int32_t p
in rad/s with INT32_RATE_FRAC
#define L3G4200_REG_STATUS_REG
Definition: l3g4200_regs.h:44
#define Int16FromBuf(_buf, _idx)
Definition: l3g4200.c:111
struct Int32Rates rates
data as angular rates in gyro coordinate system
Definition: l3g4200.h:74
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:258
void l3g4200_read(struct L3g4200 *l3g)
Definition: l3g4200.c:103
uint8_t ctrl_reg4
Definition: l3g4200.h:54
#define L3G4200_REG_CTRL_REG4
Definition: l3g4200_regs.h:40
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
#define FALSE
Definition: imu_chimu.h:141
bool_t initialized
config done flag
Definition: l3g4200.h:70
struct i2c_periph * i2c_p
Definition: l3g4200.h:68
uint8_t ctrl_reg1
Definition: l3g4200.h:53
#define L3G4200_REG_CTRL_REG5
Definition: l3g4200_regs.h:41
#define L3G4200_REG_CTRL_REG1
Definition: l3g4200_regs.h:37
#define L3G4200_DEFAULT_CTRL_REG5
Definition: l3g4200.h:50
transaction set to done by user level
Definition: i2c.h:59
uint16_t val[TCOUPLE_NB]
bool_t i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:238
volatile bool_t data_available
data ready flag
Definition: l3g4200.h:72
static void l3g4200_i2c_tx_reg(struct L3g4200 *l3g, uint8_t reg, uint8_t val)
Definition: l3g4200.c:59
transaction failed
Definition: i2c.h:58
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
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:55
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:92
#define L3G4200_DEFAULT_CTRL_REG4
Definition: l3g4200.h:49
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
struct L3g4200Config config
Definition: l3g4200.h:77
unsigned char uint8_t
Definition: types.h:14
enum L3g4200ConfStatus init_status
init status
Definition: l3g4200.h:71
I2C peripheral structure.
Definition: i2c.h:138
int32_t q
in rad/s with INT32_RATE_FRAC
int32_t r
in rad/s with INT32_RATE_FRAC
struct i2c_transaction i2c_trans
Definition: l3g4200.h:69
#define L3G4200_DEFAULT_CTRL_REG1
Definition: l3g4200.h:48
Driver for the gyro L3G4200 From ST.