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
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
33void 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;
39 lsm->i2c_trans.status = I2CTransDone;
40 /* set default config options */
41 lsm6_set_default_config(&(lsm->config));
42 lsm->initialized = false;
43 lsm->data_available = false;
44 lsm->init_status = LSM6_CONF_UNINIT;
45}
46
47
48static 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
59static void lsm6_i2c_send_config(struct Lsm6_I2c *lsm)
60{
61 switch (lsm->init_status) {
64 lsm->init_status++;
65 break;
68 lsm->init_status++;
69 break;
72 lsm->init_status++;
73 break;
76 lsm->init_status++;
77 break;
78 case LSM6_CONF_DONE:
79 lsm->initialized = true;
80 lsm->i2c_trans.status = I2CTransDone;
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++;
92 if (lsm->i2c_trans.status == I2CTransSuccess || lsm->i2c_trans.status == I2CTransDone) {
94 }
95 }
96}
97
98// Normal reading
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
115{
116 if (lsm->initialized) {
117 if (lsm->i2c_trans.status == I2CTransFailed) {
118 lsm->i2c_trans.status = I2CTransDone;
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;
128 lsm->i2c_trans.status = I2CTransDone;
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)
138 lsm->i2c_trans.status = I2CTransDone;
139 break;
140 default:
141 break;
142 }
143 }
144}
145
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
@ LSM6_CONF_UNINIT
Definition lsm6ds33.h:103
@ LSM6_CONF_CTRL3_C
Definition lsm6ds33.h:106
@ LSM6_CONF_DONE
Definition lsm6ds33.h:108
@ LSM6_CONF_CTRL1_XL
Definition lsm6ds33.h:104
@ LSM6_CONF_CTRL3_ORIENT
Definition lsm6ds33.h:107
@ LSM6_CONF_CTRL2_G
Definition lsm6ds33.h:105
static void lsm6_set_default_config(struct Lsm6Config *c)
Definition lsm6ds33.h:119
void lsm6_i2c_read(struct Lsm6_I2c *lsm)
void lsm6_i2c_start_configure(struct Lsm6_I2c *lsm)
static void lsm6_i2c_send_config(struct Lsm6_I2c *lsm)
void lsm6_i2c_init(struct Lsm6_I2c *lsm, struct i2c_periph *i2c_p, uint8_t addr)
static void lsm6_i2c_tx_reg(struct Lsm6_I2c *lsm, uint8_t reg, uint8_t val)
void lsm6_i2c_event(struct Lsm6_I2c *lsm)
#define Int16FromBuf(_buf, _idx)
Driver for the accelerometer and gyrometer LSM6DS33.
#define LSM6_REG_CTRL3_C
#define LSM6_REG_OUTX_L_G
#define LSM6_REG_CTRL2_G
#define LSM6_REG_ORIENT_CFG_G
#define LSM6_REG_CTRL1_XL
uint16_t foo
Definition main_demo5.c:58
uint16_t val[TCOUPLE_NB]
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.