Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
34 {
38 }
39 
47 void l3g4200_init(struct L3g4200 *l3g, struct i2c_periph *i2c_p, uint8_t addr)
48 {
49  /* set i2c_peripheral */
50  l3g->i2c_p = i2c_p;
51  /* set i2c address */
52  l3g->i2c_trans.slave_addr = addr;
54  /* set default config options */
56  l3g->initialized = false;
58 }
59 
60 static void l3g4200_i2c_tx_reg(struct L3g4200 *l3g, uint8_t reg, uint8_t val)
61 {
62  l3g->i2c_trans.buf[0] = reg;
63  l3g->i2c_trans.buf[1] = val;
64  i2c_transmit(l3g->i2c_p, &(l3g->i2c_trans), l3g->i2c_trans.slave_addr, 2);
65 }
66 
67 // Configuration function called once before normal use
68 static void l3g4200_send_config(struct L3g4200 *l3g)
69 {
70  switch (l3g->init_status) {
71  case L3G_CONF_REG1:
73  l3g->init_status++;
74  break;
75  case L3G_CONF_REG4:
77  l3g->init_status++;
78  break;
79  case L3G_CONF_REG5:
81  l3g->init_status++;
82  break;
83  case L3G_CONF_DONE:
84  l3g->initialized = true;
86  break;
87  default:
88  break;
89  }
90 }
91 
92 // Configure
94 {
95  if (l3g->init_status == L3G_CONF_UNINIT) {
96  l3g->init_status++;
99  }
100  }
101 }
102 
103 // Normal reading
104 void l3g4200_read(struct L3g4200 *l3g)
105 {
106  if (l3g->initialized && l3g->i2c_trans.status == I2CTransDone) {
107  l3g->i2c_trans.buf[0] = 0x80 | L3G4200_REG_STATUS_REG;
108  i2c_transceive(l3g->i2c_p, &(l3g->i2c_trans), l3g->i2c_trans.slave_addr, 1, 7);
109  }
110 }
111 
112 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx+1]<<8) | _buf[_idx]))
113 
114 void l3g4200_event(struct L3g4200 *l3g)
115 {
116  if (l3g->initialized) {
117  if (l3g->i2c_trans.status == I2CTransFailed) {
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  } else if (l3g->init_status != L3G_CONF_UNINIT) { // Configuring but not yet initialized
131  if (l3g->i2c_trans.status == I2CTransSuccess || l3g->i2c_trans.status == I2CTransDone) {
133  l3g4200_send_config(l3g);
134  }
135  if (l3g->i2c_trans.status == I2CTransFailed) {
136  l3g->init_status--;
138  l3g4200_send_config(l3g); // Retry config (TODO max retry)
139  }
140  }
141 }
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
bool 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:324
bool 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:344
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
void l3g4200_start_configure(struct L3g4200 *l3g)
Definition: l3g4200.c:93
void l3g4200_set_default_config(struct L3g4200Config *c)
Definition: l3g4200.c:33
static void l3g4200_i2c_tx_reg(struct L3g4200 *l3g, uint8_t reg, uint8_t val)
Definition: l3g4200.c:60
void l3g4200_read(struct L3g4200 *l3g)
Definition: l3g4200.c:104
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:47
static void l3g4200_send_config(struct L3g4200 *l3g)
Definition: l3g4200.c:68
#define Int16FromBuf(_buf, _idx)
Definition: l3g4200.c:112
void l3g4200_event(struct L3g4200 *l3g)
Definition: l3g4200.c:114
Driver for the gyro L3G4200 From ST.
#define L3G4200_DEFAULT_CTRL_REG4
Definition: l3g4200.h:49
uint8_t ctrl_reg5
Definition: l3g4200.h:55
bool initialized
config done flag
Definition: l3g4200.h:70
volatile bool data_available
data ready flag
Definition: l3g4200.h:72
uint8_t ctrl_reg4
Definition: l3g4200.h:54
struct i2c_periph * i2c_p
Definition: l3g4200.h:68
struct i2c_transaction i2c_trans
Definition: l3g4200.h:69
uint8_t ctrl_reg1
Definition: l3g4200.h:53
struct L3g4200Config config
Definition: l3g4200.h:77
#define L3G4200_DEFAULT_CTRL_REG5
Definition: l3g4200.h:50
enum L3g4200ConfStatus init_status
init status
Definition: l3g4200.h:71
union L3g4200::@328 data
@ L3G_CONF_REG1
Definition: l3g4200.h:61
@ L3G_CONF_REG4
Definition: l3g4200.h:62
@ L3G_CONF_UNINIT
Definition: l3g4200.h:60
@ L3G_CONF_DONE
Definition: l3g4200.h:64
@ L3G_CONF_REG5
Definition: l3g4200.h:63
#define L3G4200_DEFAULT_CTRL_REG1
Definition: l3g4200.h:48
#define L3G4200_REG_CTRL_REG5
Definition: l3g4200_regs.h:41
#define L3G4200_REG_STATUS_REG
Definition: l3g4200_regs.h:44
#define L3G4200_REG_CTRL_REG4
Definition: l3g4200_regs.h:40
#define L3G4200_REG_CTRL_REG1
Definition: l3g4200_regs.h:37
uint16_t val[TCOUPLE_NB]
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98