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
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 
39 }
40 
41 
48 void itg3200_init(struct Itg3200 *itg, struct i2c_periph *i2c_p, uint8_t addr)
49 {
50  /* set i2c_peripheral */
51  itg->i2c_p = i2c_p;
52  /* set i2c address */
53  itg->i2c_trans.slave_addr = addr;
55  /* set default config options */
57  itg->initialized = FALSE;
59 }
60 
61 static void itg3200_i2c_tx_reg(struct Itg3200 *itg, uint8_t reg, uint8_t val)
62 {
63  itg->i2c_trans.type = I2CTransTx;
64  itg->i2c_trans.buf[0] = reg;
65  itg->i2c_trans.buf[1] = val;
66  itg->i2c_trans.len_r = 0;
67  itg->i2c_trans.len_w = 2;
68  i2c_submit(itg->i2c_p, &(itg->i2c_trans));
69 }
70 
71 // Configuration function called once before normal use
72 static void itg3200_send_config(struct Itg3200 *itg)
73 {
74  switch (itg->init_status) {
75  case ITG_CONF_SD:
77  itg->init_status++;
78  break;
79  case ITG_CONF_DF:
81  itg->init_status++;
82  break;
83  case ITG_CONF_INT:
85  itg->init_status++;
86  break;
87  case ITG_CONF_PWR:
89  itg->init_status++;
90  break;
91  case ITG_CONF_DONE:
92  itg->initialized = TRUE;
94  break;
95  default:
96  break;
97  }
98 }
99 
100 // Configure
102 {
103  if (itg->init_status == ITG_CONF_UNINIT) {
104  itg->init_status++;
105  if (itg->i2c_trans.status == I2CTransSuccess || itg->i2c_trans.status == I2CTransDone) {
106  itg3200_send_config(itg);
107  }
108  }
109 }
110 
111 // Normal reading
112 void itg3200_read(struct Itg3200 *itg)
113 {
114  if (itg->initialized && itg->i2c_trans.status == I2CTransDone) {
116  itg->i2c_trans.type = I2CTransTxRx;
117  itg->i2c_trans.len_r = 9;
118  itg->i2c_trans.len_w = 1;
119  i2c_submit(itg->i2c_p, &(itg->i2c_trans));
120  }
121 }
122 
123 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
124 
125 void itg3200_event(struct Itg3200 *itg)
126 {
127  if (itg->initialized) {
128  if (itg->i2c_trans.status == I2CTransFailed) {
130  }
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  }
143  else if (itg->init_status != ITG_CONF_UNINIT) { // Configuring but not yet initialized
144  if (itg->i2c_trans.status == I2CTransSuccess || itg->i2c_trans.status == I2CTransDone) {
146  itg3200_send_config(itg);
147  }
148  if (itg->i2c_trans.status == I2CTransFailed) {
149  itg->init_status--;
151  itg3200_send_config(itg); // Retry config (TODO max retry)
152  }
153  }
154 }
155 
#define ITG3200_DEFAULT_FS_SEL
Default full scale range +- 2000°/s.
Definition: itg3200.h:43
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c_arch.c:335
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:48
#define ITG3200_REG_DLPF_FS
Definition: itg3200_regs.h:37
transaction successfully finished by I2C driver
Definition: i2c.h:57
int32_t p
in rad/s with INT32_RATE_FRAC
transmit and receive transaction
Definition: i2c.h:49
static void itg3200_i2c_tx_reg(struct Itg3200 *itg, uint8_t reg, uint8_t val)
Definition: itg3200.c:61
struct Int32Rates rates
data as angular rates in gyro coordinate system
Definition: itg3200.h:77
#define Int16FromBuf(_buf, _idx)
Definition: itg3200.c:123
struct i2c_periph * i2c_p
Definition: itg3200.h:71
#define ITG3200_DEFAULT_DLPF_CFG
Default internal sampling (1kHz, 42Hz LP Bandwidth)
Definition: itg3200.h:45
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
#define FALSE
Definition: imu_chimu.h:141
uint8_t int_cfg
Interrupt config.
Definition: itg3200.h:56
union Itg3200::@30 data
#define ITG3200_DEFAULT_SMPLRT_DIV
Default sample rate divider.
Definition: itg3200.h:41
struct Itg3200Config config
Definition: itg3200.h:80
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: itg3200.h:55
#define ITG3200_DEFAULT_CLK_SEL
Default clock: PLL with X gyro reference.
Definition: itg3200.h:49
Driver for the gyro ITG3200 from InvenSense.
enum Itg3200ConfStatus init_status
init status
Definition: itg3200.h:74
transaction set to done by user level
Definition: i2c.h:59
uint16_t val[TCOUPLE_NB]
bool_t initialized
config done flag
Definition: itg3200.h:73
#define ITG3200_DEFAULT_INT_CFG
Default interrupt config: RAW_RDY_EN.
Definition: itg3200.h:47
transaction failed
Definition: i2c.h:58
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
struct i2c_transaction i2c_trans
Definition: itg3200.h:72
#define TRUE
Definition: imu_chimu.h:144
volatile bool_t data_available
data ready flag
Definition: itg3200.h:75
#define ITG3200_REG_PWR_MGM
Definition: itg3200_regs.h:48
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
unsigned char uint8_t
Definition: types.h:14
uint8_t fs_sel
Full scale range.
Definition: itg3200.h:54
#define ITG3200_REG_INT_CFG
Definition: itg3200_regs.h:38
#define ITG3200_REG_INT_STATUS
Definition: itg3200_regs.h:39
I2C peripheral structure.
Definition: i2c.h:138
transmit only transaction
Definition: i2c.h:47
void itg3200_event(struct Itg3200 *itg)
Definition: itg3200.c:125
static void itg3200_send_config(struct Itg3200 *itg)
Definition: itg3200.c:72
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
int32_t q
in rad/s with INT32_RATE_FRAC
void itg3200_read(struct Itg3200 *itg)
Definition: itg3200.c:112
uint8_t clk_sel
Clock select.
Definition: itg3200.h:57
uint8_t smplrt_div
Sample rate divider.
Definition: itg3200.h:53
int32_t r
in rad/s with INT32_RATE_FRAC
void itg3200_start_configure(struct Itg3200 *itg)
Definition: itg3200.c:101
void itg3200_set_default_config(struct Itg3200Config *c)
Definition: itg3200.c:33
#define ITG3200_REG_SMPLRT_DIV
Definition: itg3200_regs.h:36