Paparazzi UAS  v5.18.0_stable
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 {
35  c->ctrl_reg1 = L3G4200_DEFAULT_CTRL_REG1;
36  c->ctrl_reg4 = L3G4200_DEFAULT_CTRL_REG4;
37  c->ctrl_reg5 = L3G4200_DEFAULT_CTRL_REG5;
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 }
c
VIC slots used for the LPC2148 define name e g gps UART1_VIC_SLOT e g modem SPI1_VIC_SLOT SPI1 in mcu_periph spi_arch c or spi_slave_hs_arch c(and some others not using the SPI peripheral yet..) I2C0_VIC_SLOT 8 mcu_periph/i2c_arch.c I2C1_VIC_SLOT 9 mcu_periph/i2c_arch.c USB_VIC_SLOT 10 usb
L3G4200_DEFAULT_CTRL_REG1
#define L3G4200_DEFAULT_CTRL_REG1
Definition: l3g4200.h:48
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
L3G_CONF_DONE
@ L3G_CONF_DONE
Definition: l3g4200.h:64
val
uint16_t val[TCOUPLE_NB]
Definition: temp_tcouple_adc.c:49
L3G4200_REG_STATUS_REG
#define L3G4200_REG_STATUS_REG
Definition: l3g4200_regs.h:44
L3G_CONF_UNINIT
@ L3G_CONF_UNINIT
Definition: l3g4200.h:60
L3G_CONF_REG5
@ L3G_CONF_REG5
Definition: l3g4200.h:63
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
L3G4200_REG_CTRL_REG5
#define L3G4200_REG_CTRL_REG5
Definition: l3g4200_regs.h:41
L3G4200_REG_CTRL_REG1
#define L3G4200_REG_CTRL_REG1
Definition: l3g4200_regs.h:37
L3g4200::config
struct L3g4200Config config
Definition: l3g4200.h:77
L3G4200_REG_CTRL_REG4
#define L3G4200_REG_CTRL_REG4
Definition: l3g4200_regs.h:40
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: l3g4200.c:112
L3G_CONF_REG1
@ L3G_CONF_REG1
Definition: l3g4200.h:61
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
std.h
i2c_transmit
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
L3g4200::initialized
bool initialized
config done flag
Definition: l3g4200.h:70
l3g4200_read
void l3g4200_read(struct L3g4200 *l3g)
Definition: l3g4200.c:104
l3g4200_init
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
L3g4200Config::ctrl_reg4
uint8_t ctrl_reg4
Definition: l3g4200.h:54
l3g4200_send_config
static void l3g4200_send_config(struct L3g4200 *l3g)
Definition: l3g4200.c:68
L3G4200_DEFAULT_CTRL_REG5
#define L3G4200_DEFAULT_CTRL_REG5
Definition: l3g4200.h:50
i2c_transceive
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
l3g4200_i2c_tx_reg
static void l3g4200_i2c_tx_reg(struct L3g4200 *l3g, uint8_t reg, uint8_t val)
Definition: l3g4200.c:60
L3G_CONF_REG4
@ L3G_CONF_REG4
Definition: l3g4200.h:62
L3g4200Config::ctrl_reg1
uint8_t ctrl_reg1
Definition: l3g4200.h:53
uint8_t
unsigned char uint8_t
Definition: types.h:14
L3g4200
Definition: l3g4200.h:67
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
L3g4200Config::ctrl_reg5
uint8_t ctrl_reg5
Definition: l3g4200.h:55
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
L3g4200::init_status
enum L3g4200ConfStatus init_status
init status
Definition: l3g4200.h:71
l3g4200.h
L3G4200_DEFAULT_CTRL_REG4
#define L3G4200_DEFAULT_CTRL_REG4
Definition: l3g4200.h:49
l3g4200_start_configure
void l3g4200_start_configure(struct L3g4200 *l3g)
Definition: l3g4200.c:93
L3g4200::data_available
volatile bool data_available
data ready flag
Definition: l3g4200.h:72
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
L3g4200::i2c_trans
struct i2c_transaction i2c_trans
Definition: l3g4200.h:69
i2c_periph
Definition: i2c.h:144
L3g4200::data
union L3g4200::@320 data
L3g4200Config
Definition: l3g4200.h:52
L3g4200::i2c_p
struct i2c_periph * i2c_p
Definition: l3g4200.h:68
l3g4200_set_default_config
void l3g4200_set_default_config(struct L3g4200Config *c)
Definition: l3g4200.c:33
l3g4200_event
void l3g4200_event(struct L3g4200 *l3g)
Definition: l3g4200.c:114