Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
adxl345_i2c.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 
31 #include "std.h"
32 
33 #define ADXL345_DATA_FORMAT ((adxl->config.int_invert<<5)|(adxl->config.full_res<<3)|(adxl->config.justify_msb<<2)|(adxl->config.range))
34 
35 
36 void adxl345_i2c_init(struct Adxl345_I2c *adxl, struct i2c_periph *i2c_p, uint8_t addr)
37 {
38  /* set i2c_peripheral */
39  adxl->i2c_p = i2c_p;
40  /* set i2c address */
41  adxl->i2c_trans.slave_addr = addr;
43  /* set default config options */
45  adxl->initialized = false;
47 }
48 
49 static void adxl345_i2c_tx_reg(struct Adxl345_I2c *adxl, uint8_t reg, uint8_t val)
50 {
51  adxl->i2c_trans.type = I2CTransTx;
52  adxl->i2c_trans.buf[0] = reg;
53  adxl->i2c_trans.buf[1] = val;
54  adxl->i2c_trans.len_r = 0;
55  adxl->i2c_trans.len_w = 2;
56  i2c_submit(adxl->i2c_p, &(adxl->i2c_trans));
57 }
58 
59 // Configuration function called once before normal use
60 static void adxl345_i2c_send_config(struct Adxl345_I2c *adxl)
61 {
62  switch (adxl->init_status) {
63  case ADXL_CONF_RATE:
65  adxl->init_status++;
66  break;
67  case ADXL_CONF_INT:
69  adxl->init_status++;
70  break;
71  case ADXL_CONF_FORMAT:
73  adxl->init_status++;
74  break;
75  case ADXL_CONF_ENABLE:
76  /* enable measurement, is in standby after power up */
77  adxl345_i2c_tx_reg(adxl, ADXL345_REG_POWER_CTL, (0x1 << 3));
78  adxl->init_status++;
79  break;
80  case ADXL_CONF_DONE:
81  adxl->initialized = true;
83  break;
84  default:
85  break;
86  }
87 }
88 
91 {
92  if (adxl->init_status == ADXL_CONF_UNINIT) {
93  adxl->init_status++;
94  if (adxl->i2c_trans.status == I2CTransSuccess || adxl->i2c_trans.status == I2CTransDone) {
96  }
97  }
98 }
99 
100 // Normal reading
101 void adxl345_i2c_read(struct Adxl345_I2c *adxl)
102 {
103  if (adxl->initialized && adxl->i2c_trans.status == I2CTransDone) {
104  adxl->i2c_trans.buf[0] = ADXL345_REG_DATA_X0;
105  adxl->i2c_trans.type = I2CTransTxRx;
106  adxl->i2c_trans.len_r = 6;
107  adxl->i2c_trans.len_w = 1;
108  i2c_submit(adxl->i2c_p, &(adxl->i2c_trans));
109  }
110 }
111 
112 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx+1]<<8) | _buf[_idx]))
113 
114 void adxl345_i2c_event(struct Adxl345_I2c *adxl)
115 {
116  if (adxl->initialized) {
117  if (adxl->i2c_trans.status == I2CTransFailed) {
118  adxl->i2c_trans.status = I2CTransDone;
119  } else if (adxl->i2c_trans.status == I2CTransSuccess) {
120  // Successfull reading
121  adxl->data.vect.x = Int16FromBuf(adxl->i2c_trans.buf, 0);
122  adxl->data.vect.y = Int16FromBuf(adxl->i2c_trans.buf, 2);
123  adxl->data.vect.z = Int16FromBuf(adxl->i2c_trans.buf, 4);
124  adxl->data_available = true;
125  adxl->i2c_trans.status = I2CTransDone;
126  }
127  } else if (adxl->init_status != ADXL_CONF_UNINIT) { // Configuring but not yet initialized
128  if (adxl->i2c_trans.status == I2CTransSuccess || adxl->i2c_trans.status == I2CTransDone) {
129  adxl->i2c_trans.status = I2CTransDone;
131  }
132  if (adxl->i2c_trans.status == I2CTransFailed) {
133  adxl->init_status--;
134  adxl->i2c_trans.status = I2CTransDone;
135  adxl345_i2c_send_config(adxl); // Retry config (TODO max retry)
136  }
137  }
138 }
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
adxl345_i2c_event
void adxl345_i2c_event(struct Adxl345_I2c *adxl)
Definition: adxl345_i2c.c:114
Adxl345_I2c
Definition: adxl345_i2c.h:39
i2c_transaction::len_r
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
Adxl345Config::drdy_int_enable
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: adxl345.h:46
ADXL_CONF_INT
@ ADXL_CONF_INT
Definition: adxl345.h:39
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
ADXL345_REG_INT_ENABLE
#define ADXL345_REG_INT_ENABLE
Definition: adxl345_regs.h:38
ADXL_CONF_FORMAT
@ ADXL_CONF_FORMAT
Definition: adxl345.h:40
Adxl345Config::rate
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
Adxl345_I2c::config
struct Adxl345Config config
Definition: adxl345_i2c.h:49
ADXL_CONF_DONE
@ ADXL_CONF_DONE
Definition: adxl345.h:42
adxl345_i2c_tx_reg
static void adxl345_i2c_tx_reg(struct Adxl345_I2c *adxl, uint8_t reg, uint8_t val)
Definition: adxl345_i2c.c:49
ADXL_CONF_ENABLE
@ ADXL_CONF_ENABLE
Definition: adxl345.h:41
ADXL345_DATA_FORMAT
#define ADXL345_DATA_FORMAT
Definition: adxl345_i2c.c:33
adxl345_i2c_init
void adxl345_i2c_init(struct Adxl345_I2c *adxl, struct i2c_periph *i2c_p, uint8_t addr)
Definition: adxl345_i2c.c:36
ADXL_CONF_UNINIT
@ ADXL_CONF_UNINIT
Definition: adxl345.h:37
i2c_transaction::len_w
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: adxl345_i2c.c:112
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
std.h
Adxl345_I2c::i2c_p
struct i2c_periph * i2c_p
Definition: adxl345_i2c.h:40
ADXL345_REG_DATA_FORMAT
#define ADXL345_REG_DATA_FORMAT
Definition: adxl345_regs.h:39
adxl345_i2c_start_configure
void adxl345_i2c_start_configure(struct Adxl345_I2c *adxl)
Start configuration if not already done.
Definition: adxl345_i2c.c:90
adxl345_i2c.h
ADXL345_REG_BW_RATE
#define ADXL345_REG_BW_RATE
Definition: adxl345_regs.h:36
Adxl345_I2c::initialized
bool initialized
config done flag
Definition: adxl345_i2c.h:43
uint8_t
unsigned char uint8_t
Definition: types.h:14
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
I2CTransTxRx
@ I2CTransTxRx
transmit and receive transaction
Definition: i2c.h:49
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
ADXL345_REG_POWER_CTL
#define ADXL345_REG_POWER_CTL
Definition: adxl345_regs.h:37
Adxl345_I2c::data
union Adxl345_I2c::@304 data
Adxl345_I2c::data_available
volatile bool data_available
data ready flag
Definition: adxl345_i2c.h:44
ADXL345_REG_DATA_X0
#define ADXL345_REG_DATA_X0
Definition: adxl345_regs.h:40
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
ADXL_CONF_RATE
@ ADXL_CONF_RATE
Definition: adxl345.h:38
i2c_submit
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
adxl345_i2c_send_config
static void adxl345_i2c_send_config(struct Adxl345_I2c *adxl)
Definition: adxl345_i2c.c:60
Adxl345_I2c::i2c_trans
struct i2c_transaction i2c_trans
Definition: adxl345_i2c.h:41
Adxl345_I2c::init_status
enum Adxl345ConfStatus init_status
init status
Definition: adxl345_i2c.h:42
adxl345_set_default_config
static void adxl345_set_default_config(struct Adxl345Config *c)
Definition: adxl345.h:56
i2c_periph
Definition: i2c.h:144
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
adxl345_i2c_read
void adxl345_i2c_read(struct Adxl345_I2c *adxl)
Definition: adxl345_i2c.c:101