Paparazzi UAS v7.0_unstable
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
41
42
49void 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;
55 itg->i2c_trans.status = I2CTransDone;
56 /* set default config options */
58 itg->initialized = false;
59 itg->init_status = ITG_CONF_UNINIT;
60}
61
62static 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
73static 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:
81 itg3200_i2c_tx_reg(itg, ITG3200_REG_DLPF_FS, (itg->config.fs_sel << 3) | (itg->config.dlpf_cfg));
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;
94 itg->i2c_trans.status = I2CTransDone;
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) {
108 }
109 }
110}
111
112// Normal reading
114{
115 if (itg->initialized && itg->i2c_trans.status == I2CTransDone) {
116 itg->i2c_trans.buf[0] = ITG3200_REG_INT_STATUS;
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
127{
128 if (itg->initialized) {
129 if (itg->i2c_trans.status == I2CTransFailed) {
130 itg->i2c_trans.status = I2CTransDone;
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 }
140 itg->i2c_trans.status = I2CTransDone;
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) {
144 itg->i2c_trans.status = I2CTransDone;
146 }
147 if (itg->i2c_trans.status == I2CTransFailed) {
148 itg->init_status--;
149 itg->i2c_trans.status = I2CTransDone;
150 itg3200_send_config(itg); // Retry config (TODO max retry)
151 }
152 }
153}
154
enum I2CStatus status
Definition i2c.h:155
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition i2c.h:266
@ 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
@ I2CTransTx
transmit only transaction
Definition i2c.h:47
@ I2CTransTxRx
transmit and receive transaction
Definition i2c.h:49
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
void itg3200_start_configure(struct Itg3200 *itg)
Definition itg3200.c:102
static void itg3200_i2c_tx_reg(struct Itg3200 *itg, uint8_t reg, uint8_t val)
Definition itg3200.c:62
void itg3200_event(struct Itg3200 *itg)
Definition itg3200.c:126
#define Int16FromBuf(_buf, _idx)
Definition itg3200.c:124
void itg3200_set_default_config(struct Itg3200Config *c)
Definition itg3200.c:33
void itg3200_read(struct Itg3200 *itg)
Definition itg3200.c:113
static void itg3200_send_config(struct Itg3200 *itg)
Definition itg3200.c:73
Driver for the gyro ITG3200 from InvenSense.
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition itg3200.h:55
#define ITG3200_DEFAULT_INT_CFG
Default interrupt config: RAW_RDY_EN.
Definition itg3200.h:47
#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
#define ITG3200_DEFAULT_SMPLRT_DIV
Default sample rate divider.
Definition itg3200.h:41
#define ITG3200_DEFAULT_CLK_SEL
Default clock: PLL with X gyro reference.
Definition itg3200.h:49
uint8_t int_cfg
Interrupt config.
Definition itg3200.h:56
#define ITG3200_DEFAULT_DLPF_CFG
Default internal sampling (1kHz, 42Hz LP Bandwidth)
Definition itg3200.h:45
uint8_t fs_sel
Full scale range.
Definition itg3200.h:54
uint8_t clk_sel
Clock select.
Definition itg3200.h:57
@ ITG_CONF_INT
Definition itg3200.h:65
@ ITG_CONF_SD
Definition itg3200.h:63
@ ITG_CONF_PWR
Definition itg3200.h:66
@ ITG_CONF_UNINIT
Definition itg3200.h:62
@ ITG_CONF_DONE
Definition itg3200.h:67
@ ITG_CONF_DF
Definition itg3200.h:64
#define ITG3200_REG_INT_CFG
#define ITG3200_REG_DLPF_FS
#define ITG3200_REG_PWR_MGM
#define ITG3200_REG_SMPLRT_DIV
#define ITG3200_REG_INT_STATUS
uint16_t foo
Definition main_demo5.c:58
uint16_t val[TCOUPLE_NB]
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.