Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 {
35  c->smplrt_div = ITG3200_DEFAULT_SMPLRT_DIV;
36  c->fs_sel = ITG3200_DEFAULT_FS_SEL;
37  c->dlpf_cfg = ITG3200_DEFAULT_DLPF_CFG;
38  c->int_cfg = ITG3200_DEFAULT_INT_CFG;
39  c->clk_sel = ITG3200_DEFAULT_CLK_SEL;
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 
Itg3200Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: itg3200.h:53
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
itg3200_start_configure
void itg3200_start_configure(struct Itg3200 *itg)
Definition: itg3200.c:102
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
val
uint16_t val[TCOUPLE_NB]
Definition: temp_tcouple_adc.c:49
Itg3200::config
struct Itg3200Config config
Definition: itg3200.h:80
Itg3200Config::fs_sel
uint8_t fs_sel
Full scale range.
Definition: itg3200.h:54
i2c_transaction::len_r
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
itg3200_i2c_tx_reg
static void itg3200_i2c_tx_reg(struct Itg3200 *itg, uint8_t reg, uint8_t val)
Definition: itg3200.c:62
ITG3200_DEFAULT_FS_SEL
#define ITG3200_DEFAULT_FS_SEL
Default full scale range +- 2000°/s.
Definition: itg3200.h:43
itg3200_init
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
ITG3200_REG_SMPLRT_DIV
#define ITG3200_REG_SMPLRT_DIV
Definition: itg3200_regs.h:36
Itg3200::initialized
bool initialized
config done flag
Definition: itg3200.h:73
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
ITG_CONF_INT
@ ITG_CONF_INT
Definition: itg3200.h:65
Itg3200Config::int_cfg
uint8_t int_cfg
Interrupt config.
Definition: itg3200.h:56
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
ITG3200_DEFAULT_DLPF_CFG
#define ITG3200_DEFAULT_DLPF_CFG
Default internal sampling (1kHz, 42Hz LP Bandwidth)
Definition: itg3200.h:45
ITG_CONF_SD
@ ITG_CONF_SD
Definition: itg3200.h:63
i2c_transaction::len_w
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
Itg3200Config
Definition: itg3200.h:52
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
Itg3200::init_status
enum Itg3200ConfStatus init_status
init status
Definition: itg3200.h:74
std.h
ITG3200_DEFAULT_INT_CFG
#define ITG3200_DEFAULT_INT_CFG
Default interrupt config: RAW_RDY_EN.
Definition: itg3200.h:47
Itg3200::i2c_p
struct i2c_periph * i2c_p
Definition: itg3200.h:71
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: itg3200.c:124
ITG3200_DEFAULT_SMPLRT_DIV
#define ITG3200_DEFAULT_SMPLRT_DIV
Default sample rate divider.
Definition: itg3200.h:41
Itg3200::i2c_trans
struct i2c_transaction i2c_trans
Definition: itg3200.h:72
ITG3200_DEFAULT_CLK_SEL
#define ITG3200_DEFAULT_CLK_SEL
Default clock: PLL with X gyro reference.
Definition: itg3200.h:49
ITG3200_REG_PWR_MGM
#define ITG3200_REG_PWR_MGM
Definition: itg3200_regs.h:48
Itg3200Config::dlpf_cfg
enum Itg3200DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: itg3200.h:55
uint8_t
unsigned char uint8_t
Definition: types.h:14
ITG_CONF_UNINIT
@ ITG_CONF_UNINIT
Definition: itg3200.h:62
ITG_CONF_PWR
@ ITG_CONF_PWR
Definition: itg3200.h:66
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
Itg3200::data
union Itg3200::@319 data
ITG3200_REG_INT_CFG
#define ITG3200_REG_INT_CFG
Definition: itg3200_regs.h:38
I2CTransTxRx
@ I2CTransTxRx
transmit and receive transaction
Definition: i2c.h:49
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
Itg3200Config::clk_sel
uint8_t clk_sel
Clock select.
Definition: itg3200.h:57
ITG3200_REG_INT_STATUS
#define ITG3200_REG_INT_STATUS
Definition: itg3200_regs.h:39
ITG_CONF_DONE
@ ITG_CONF_DONE
Definition: itg3200.h:67
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
Itg3200
Definition: itg3200.h:70
i2c_submit
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
itg3200.h
i2c_periph
Definition: i2c.h:144
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
Itg3200::data_available
volatile bool data_available
data ready flag
Definition: itg3200.h:75
itg3200_set_default_config
void itg3200_set_default_config(struct Itg3200Config *c)
Definition: itg3200.c:33
ITG_CONF_DF
@ ITG_CONF_DF
Definition: itg3200.h:64
itg3200_read
void itg3200_read(struct Itg3200 *itg)
Definition: itg3200.c:113
ITG3200_REG_DLPF_FS
#define ITG3200_REG_DLPF_FS
Definition: itg3200_regs.h:37
itg3200_event
void itg3200_event(struct Itg3200 *itg)
Definition: itg3200.c:126
itg3200_send_config
static void itg3200_send_config(struct Itg3200 *itg)
Definition: itg3200.c:73