Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
void lsm6_i2c_event(struct Lsm6_I2c *lsm)
Definition: lsm6ds33_i2c.c:114
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
transaction successfully finished by I2C driver
Definition: i2c.h:57
enum Lsm6ConfStatus init_status
init status
Definition: lsm6ds33_i2c.h:41
transmit and receive transaction
Definition: i2c.h:49
struct Lsm6Config config
Definition: lsm6ds33_i2c.h:52
uint8_t c
Definition: lsm6ds33.h:114
void lsm6_i2c_start_configure(struct Lsm6_I2c *lsm)
Definition: lsm6ds33_i2c.c:88
void lsm6_i2c_init(struct Lsm6_I2c *lsm, struct i2c_periph *i2c_p, uint8_t addr)
Definition: lsm6ds33_i2c.c:33
#define LSM6_REG_CTRL3_C
Definition: lsm6ds33_regs.h:51
union Lsm6_I2c::@328 data_g
uint8_t g
Definition: lsm6ds33.h:113
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
#define LSM6_REG_CTRL1_XL
Definition: lsm6ds33_regs.h:49
transaction set to done by user level
Definition: i2c.h:59
union Lsm6_I2c::@327 data_xl
uint8_t xl
Definition: lsm6ds33.h:112
#define LSM6_REG_CTRL2_G
Definition: lsm6ds33_regs.h:50
uint16_t val[TCOUPLE_NB]
uint8_t orient
Definition: lsm6ds33.h:115
transaction failed
Definition: i2c.h:58
bool initialized
config done flag
Definition: lsm6ds33_i2c.h:42
volatile bool data_available
data ready flag
Definition: lsm6ds33_i2c.h:43
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
Driver for the accelerometer and gyrometer LSM6DS33.
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
static void lsm6_i2c_tx_reg(struct Lsm6_I2c *lsm, uint8_t reg, uint8_t val)
Definition: lsm6ds33_i2c.c:48
unsigned char uint8_t
Definition: types.h:14
#define LSM6_REG_ORIENT_CFG_G
Definition: lsm6ds33_regs.h:44
#define LSM6_REG_OUTX_L_G
Definition: lsm6ds33_regs.h:67
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
transmit only transaction
Definition: i2c.h:47
struct i2c_transaction i2c_trans
Definition: lsm6ds33_i2c.h:40
static void lsm6_set_default_config(struct Lsm6Config *c)
Definition: lsm6ds33.h:119
void lsm6_i2c_read(struct Lsm6_I2c *lsm)
Definition: lsm6ds33_i2c.c:99
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
struct i2c_periph * i2c_p
Definition: lsm6ds33_i2c.h:39
static void lsm6_i2c_send_config(struct Lsm6_I2c *lsm)
Definition: lsm6ds33_i2c.c:59
#define Int16FromBuf(_buf, _idx)
Definition: lsm6ds33_i2c.c:112