Paparazzi UAS  v5.15_devel-88-gb3ad7fe
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
itg3200.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  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
30 #include "peripherals/itg3200.h"
31 #include "std.h"
32 
34 {
40 }
41 
42 
49 void itg3200_init(struct Itg3200 *itg, struct i2c_periph *i2c_p, uint8_t addr)
50 {
51  /* set i2c_peripheral */
52  itg->i2c_p = i2c_p;
53  /* set i2c address */
54  itg->i2c_trans.slave_addr = addr;
56  /* set default config options */
58  itg->initialized = false;
60 }
61 
62 static void itg3200_i2c_tx_reg(struct Itg3200 *itg, uint8_t reg, uint8_t val)
63 {
64  itg->i2c_trans.type = I2CTransTx;
65  itg->i2c_trans.buf[0] = reg;
66  itg->i2c_trans.buf[1] = val;
67  itg->i2c_trans.len_r = 0;
68  itg->i2c_trans.len_w = 2;
69  i2c_submit(itg->i2c_p, &(itg->i2c_trans));
70 }
71 
72 // Configuration function called once before normal use
73 static void itg3200_send_config(struct Itg3200 *itg)
74 {
75  switch (itg->init_status) {
76  case ITG_CONF_SD:
78  itg->init_status++;
79  break;
80  case ITG_CONF_DF:
82  itg->init_status++;
83  break;
84  case ITG_CONF_INT:
86  itg->init_status++;
87  break;
88  case ITG_CONF_PWR:
90  itg->init_status++;
91  break;
92  case ITG_CONF_DONE:
93  itg->initialized = true;
95  break;
96  default:
97  break;
98  }
99 }
100 
101 // Configure
103 {
104  if (itg->init_status == ITG_CONF_UNINIT) {
105  itg->init_status++;
106  if (itg->i2c_trans.status == I2CTransSuccess || itg->i2c_trans.status == I2CTransDone) {
107  itg3200_send_config(itg);
108  }
109  }
110 }
111 
112 // Normal reading
113 void itg3200_read(struct Itg3200 *itg)
114 {
115  if (itg->initialized && itg->i2c_trans.status == I2CTransDone) {
117  itg->i2c_trans.type = I2CTransTxRx;
118  itg->i2c_trans.len_r = 9;
119  itg->i2c_trans.len_w = 1;
120  i2c_submit(itg->i2c_p, &(itg->i2c_trans));
121  }
122 }
123 
124 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
125 
126 void itg3200_event(struct Itg3200 *itg)
127 {
128  if (itg->initialized) {
129  if (itg->i2c_trans.status == I2CTransFailed) {
131  } else if (itg->i2c_trans.status == I2CTransSuccess) {
132  // Successfull reading and new data available
133  if (itg->i2c_trans.buf[0] & 0x01) {
134  // New data available
135  itg->data.rates.p = Int16FromBuf(itg->i2c_trans.buf, 3);
136  itg->data.rates.q = Int16FromBuf(itg->i2c_trans.buf, 5);
137  itg->data.rates.r = Int16FromBuf(itg->i2c_trans.buf, 7);
138  itg->data_available = true;
139  }
141  }
142  } else if (itg->init_status != ITG_CONF_UNINIT) { // Configuring but not yet initialized
143  if (itg->i2c_trans.status == I2CTransSuccess || itg->i2c_trans.status == I2CTransDone) {
145  itg3200_send_config(itg);
146  }
147  if (itg->i2c_trans.status == I2CTransFailed) {
148  itg->init_status--;
150  itg3200_send_config(itg); // Retry config (TODO max retry)
151  }
152  }
153 }
154 
#define ITG3200_DEFAULT_FS_SEL
Default full scale range +- 2000°/s.
Definition: itg3200.h:43
uint8_t smplrt_div
Sample rate divider.
Definition: itg3200.h:53
void itg3200_init(struct Itg3200 *itg, struct i2c_periph *i2c_p, uint8_t addr)
Initialize Itg3200 struct and set default config options.
Definition: itg3200.c:49
#define ITG3200_REG_DLPF_FS
Definition: itg3200_regs.h:37
union Itg3200::@310 data
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
transaction successfully finished by I2C driver
Definition: i2c.h:57
uint8_t int_cfg
Interrupt config.
Definition: itg3200.h:56
transmit and receive transaction
Definition: i2c.h:49
uint8_t fs_sel
Full scale range.
Definition: itg3200.h:54
struct i2c_periph * i2c_p
Definition: itg3200.h:71
static void itg3200_i2c_tx_reg(struct Itg3200 *itg, uint8_t reg, uint8_t val)
Definition: itg3200.c:62
#define Int16FromBuf(_buf, _idx)
Definition: itg3200.c:124
#define ITG3200_DEFAULT_DLPF_CFG
Default internal sampling (1kHz, 42Hz LP Bandwidth)
Definition: itg3200.h:45
struct i2c_transaction i2c_trans
Definition: itg3200.h:72
#define ITG3200_DEFAULT_SMPLRT_DIV
Default sample rate divider.
Definition: itg3200.h:41
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
enum Itg3200ConfStatus init_status
init status
Definition: itg3200.h:74
#define ITG3200_DEFAULT_CLK_SEL
Default clock: PLL with X gyro reference.
Definition: itg3200.h:49
Driver for the gyro ITG3200 from InvenSense.
transaction set to done by user level
Definition: i2c.h:59
uint16_t val[TCOUPLE_NB]
#define ITG3200_DEFAULT_INT_CFG
Default interrupt config: RAW_RDY_EN.
Definition: itg3200.h:47
uint8_t clk_sel
Clock select.
Definition: itg3200.h:57
bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
i2c_submit() function
Definition: i2c_arch.c:433
transaction failed
Definition: i2c.h:58
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define ITG3200_REG_PWR_MGM
Definition: itg3200_regs.h:48
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
I2C peripheral structure.
Definition: i2c.h:138
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: itg3200.h:55
unsigned char uint8_t
Definition: types.h:14
#define ITG3200_REG_INT_CFG
Definition: itg3200_regs.h:38
#define ITG3200_REG_INT_STATUS
Definition: itg3200_regs.h:39
transmit only transaction
Definition: i2c.h:47
void itg3200_event(struct Itg3200 *itg)
Definition: itg3200.c:126
static void itg3200_send_config(struct Itg3200 *itg)
Definition: itg3200.c:73
void itg3200_read(struct Itg3200 *itg)
Definition: itg3200.c:113
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
void itg3200_start_configure(struct Itg3200 *itg)
Definition: itg3200.c:102
void itg3200_set_default_config(struct Itg3200Config *c)
Definition: itg3200.c:33
volatile bool data_available
data ready flag
Definition: itg3200.h:75
struct Itg3200Config config
Definition: itg3200.h:80
bool initialized
config done flag
Definition: itg3200.h:73
#define ITG3200_REG_SMPLRT_DIV
Definition: itg3200_regs.h:36