Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
lsm6ds33_i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Alexis Cornard <alexiscornard@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
29 #include "std.h"
30 #include <stdio.h>
31 
32 
33 void lsm6_i2c_init(struct Lsm6_I2c *lsm, struct i2c_periph *i2c_p, uint8_t addr)
34 {
35  /* set i2c_peripheral */
36  lsm->i2c_p = i2c_p;
37  /* set i2c address */
38  lsm->i2c_trans.slave_addr = addr;
40  /* set default config options */
42  lsm->initialized = false;
43  lsm->data_available = false;
45 }
46 
47 
48 static void lsm6_i2c_tx_reg(struct Lsm6_I2c *lsm, uint8_t reg, uint8_t val)
49 {
50  lsm->i2c_trans.type = I2CTransTx;
51  lsm->i2c_trans.buf[0] = reg;
52  lsm->i2c_trans.buf[1] = val;
53  lsm->i2c_trans.len_r = 0;
54  lsm->i2c_trans.len_w = 2;
55  i2c_submit(lsm->i2c_p, &(lsm->i2c_trans));
56 }
57 
58 // Configuration function called once before normal use
59 static void lsm6_i2c_send_config(struct Lsm6_I2c *lsm)
60 {
61  switch (lsm->init_status) {
62  case LSM6_CONF_CTRL1_XL:
64  lsm->init_status++;
65  break;
66  case LSM6_CONF_CTRL2_G:
68  lsm->init_status++;
69  break;
70  case LSM6_CONF_CTRL3_C:
72  lsm->init_status++;
73  break;
76  lsm->init_status++;
77  break;
78  case LSM6_CONF_DONE:
79  lsm->initialized = true;
81  break;
82  default:
83  break;
84  }
85 }
86 
87 // Start configuration if not already done
89 {
90  if (lsm->init_status == LSM6_CONF_UNINIT) {
91  lsm->init_status++;
94  }
95  }
96 }
97 
98 // Normal reading
99 void lsm6_i2c_read(struct Lsm6_I2c *lsm)
100 {
101  if (lsm->initialized && lsm->i2c_trans.status == I2CTransDone) {
102  lsm->i2c_trans.buf[0] = LSM6_REG_OUTX_L_G;
103  lsm->i2c_trans.type = I2CTransTxRx;
104  lsm->i2c_trans.len_r = 12;
105  lsm->i2c_trans.len_w = 1;
106  i2c_submit(lsm->i2c_p, &(lsm->i2c_trans));
107  }
108 }
109 
110 
111 
112 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx+1]<<8) | _buf[_idx]))
113 
114 void lsm6_i2c_event(struct Lsm6_I2c *lsm)
115 {
116  if (lsm->initialized) {
117  if (lsm->i2c_trans.status == I2CTransFailed) {
119  } else if (lsm->i2c_trans.status == I2CTransSuccess) {
120  // Successfull reading
121  lsm->data_xl.vect.x = Int16FromBuf(lsm->i2c_trans.buf, 6);
122  lsm->data_xl.vect.y = Int16FromBuf(lsm->i2c_trans.buf, 8);
123  lsm->data_xl.vect.z = Int16FromBuf(lsm->i2c_trans.buf, 10);
124  lsm->data_g.rates.p = Int16FromBuf(lsm->i2c_trans.buf, 0);
125  lsm->data_g.rates.q = Int16FromBuf(lsm->i2c_trans.buf, 2);
126  lsm->data_g.rates.r = Int16FromBuf(lsm->i2c_trans.buf, 4);
127  lsm->data_available = true;
129  }
130  } else if (lsm->init_status != LSM6_CONF_UNINIT) { // Configuring but not yet initialized
131  switch(lsm->i2c_trans.status){
132  case I2CTransFailed:
133  lsm->init_status--;
134  case I2CTransSuccess:
135  case I2CTransDone:
137  if(lsm->initialized)
139  break;
140  default:
141  break;
142  }
143  }
144 }
145 
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: lsm6ds33_i2c.c:112
val
uint16_t val[TCOUPLE_NB]
Definition: temp_tcouple_adc.c:49
i2c_transaction::len_r
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
lsm6_set_default_config
static void lsm6_set_default_config(struct Lsm6Config *c)
Definition: lsm6ds33.h:119
LSM6_CONF_CTRL3_C
@ LSM6_CONF_CTRL3_C
Definition: lsm6ds33.h:106
lsm6_i2c_init
void lsm6_i2c_init(struct Lsm6_I2c *lsm, struct i2c_periph *i2c_p, uint8_t addr)
Definition: lsm6ds33_i2c.c:33
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
Lsm6_I2c
Definition: lsm6ds33_i2c.h:38
LSM6_REG_CTRL2_G
#define LSM6_REG_CTRL2_G
Definition: lsm6ds33_regs.h:50
LSM6_REG_CTRL1_XL
#define LSM6_REG_CTRL1_XL
Definition: lsm6ds33_regs.h:49
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
LSM6_REG_ORIENT_CFG_G
#define LSM6_REG_ORIENT_CFG_G
Definition: lsm6ds33_regs.h:44
LSM6_CONF_CTRL3_ORIENT
@ LSM6_CONF_CTRL3_ORIENT
Definition: lsm6ds33.h:107
LSM6_REG_OUTX_L_G
#define LSM6_REG_OUTX_L_G
Definition: lsm6ds33_regs.h:67
Lsm6Config::c
uint8_t c
Definition: lsm6ds33.h:114
Lsm6_I2c::config
struct Lsm6Config config
Definition: lsm6ds33_i2c.h:52
Lsm6_I2c::data_xl
union Lsm6_I2c::@329 data_xl
i2c_transaction::len_w
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
lsm6_i2c_start_configure
void lsm6_i2c_start_configure(struct Lsm6_I2c *lsm)
Definition: lsm6ds33_i2c.c:88
std.h
LSM6_CONF_CTRL1_XL
@ LSM6_CONF_CTRL1_XL
Definition: lsm6ds33.h:104
uint8_t
unsigned char uint8_t
Definition: types.h:14
Lsm6_I2c::initialized
bool initialized
config done flag
Definition: lsm6ds33_i2c.h:42
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
LSM6_CONF_DONE
@ LSM6_CONF_DONE
Definition: lsm6ds33.h:108
I2CTransTxRx
@ I2CTransTxRx
transmit and receive transaction
Definition: i2c.h:49
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
Lsm6_I2c::data_g
union Lsm6_I2c::@330 data_g
LSM6_CONF_CTRL2_G
@ LSM6_CONF_CTRL2_G
Definition: lsm6ds33.h:105
Lsm6Config::orient
uint8_t orient
Definition: lsm6ds33.h:115
lsm6_i2c_tx_reg
static void lsm6_i2c_tx_reg(struct Lsm6_I2c *lsm, uint8_t reg, uint8_t val)
Definition: lsm6ds33_i2c.c:48
lsm6_i2c_send_config
static void lsm6_i2c_send_config(struct Lsm6_I2c *lsm)
Definition: lsm6ds33_i2c.c:59
Lsm6_I2c::data_available
volatile bool data_available
data ready flag
Definition: lsm6ds33_i2c.h:43
lsm6_i2c_read
void lsm6_i2c_read(struct Lsm6_I2c *lsm)
Definition: lsm6ds33_i2c.c:99
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
i2c_submit
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
Lsm6_I2c::i2c_trans
struct i2c_transaction i2c_trans
Definition: lsm6ds33_i2c.h:40
lsm6_i2c_event
void lsm6_i2c_event(struct Lsm6_I2c *lsm)
Definition: lsm6ds33_i2c.c:114
LSM6_REG_CTRL3_C
#define LSM6_REG_CTRL3_C
Definition: lsm6ds33_regs.h:51
i2c_periph
Definition: i2c.h:144
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
Lsm6Config::xl
uint8_t xl
Definition: lsm6ds33.h:112
Lsm6Config::g
uint8_t g
Definition: lsm6ds33.h:113
Lsm6_I2c::i2c_p
struct i2c_periph * i2c_p
Definition: lsm6ds33_i2c.h:39
lsm6ds33_i2c.h
Lsm6_I2c::init_status
enum Lsm6ConfStatus init_status
init status
Definition: lsm6ds33_i2c.h:41
LSM6_CONF_UNINIT
@ LSM6_CONF_UNINIT
Definition: lsm6ds33.h:103