Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
lps25h_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 publpshed 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 
28 #include "peripherals/lps25h_i2c.h"
29 #include "std.h"
30 #include <stdio.h>
31 #include <math.h>
32 
33 void lps25h_i2c_init(struct Lps25h_I2c *lps, struct i2c_periph *i2c_p, uint8_t addr)
34 {
35  /* set i2c_peripheral */
36  lps->i2c_p = i2c_p;
37  /* set i2c address */
38  lps->i2c_trans.slave_addr = addr;
40  /* set default config options */
42  lps->initialized = false;
43  lps->data_available = false;
45 }
46 
47 
48 static void lps25h_i2c_tx_reg(struct Lps25h_I2c *lps, uint8_t reg, uint8_t val)
49 {
50  lps->i2c_trans.type = I2CTransTx;
51  lps->i2c_trans.buf[0] = reg;
52  lps->i2c_trans.buf[1] = val;
53  lps->i2c_trans.len_r = 0;
54  lps->i2c_trans.len_w = 2;
55  i2c_submit(lps->i2c_p, &(lps->i2c_trans));
56 }
57 
58 // Configuration function called once before normal use
59 static void lps25h_i2c_send_config(struct Lps25h_I2c *lps)
60 {
61  switch (lps->init_status) {
62  case LPS25H_CONF_CTRL1:
64  lps->init_status++;
65  break;
66  case LPS25H_CONF_DONE:
67  lps->initialized = true;
69  break;
70  default:
71  break;
72  }
73 }
74 
75 // Start configuration if not already done
77 {
78  if (lps->init_status == LPS25H_CONF_UNINIT) {
79  lps->init_status++;
82  }
83  }
84 }
85 
86 // Normal reading
87 void lps25h_i2c_read(struct Lps25h_I2c *lps)
88 {
89  if (lps->initialized && lps->i2c_trans.status == I2CTransDone) {
90  lps->i2c_trans.buf[0] = LPS25H_REG_OUT_XL | (1 << 7);
92  lps->i2c_trans.len_r = 3;
93  lps->i2c_trans.len_w = 1;
94  i2c_submit(lps->i2c_p, &(lps->i2c_trans));
95  }
96 }
97 
98 #define Int32FromBuf(buf, idx) (int32_t)(int8_t)buf[idx+2] << 16 | (uint16_t)buf[idx+1] << 8 | buf[idx];
99 
100 // The two following functions can be used to check data
102 {
103  return (float)press / 4096;
104 }
105 
106 float pressureToAltMeters(float pressure_mbar, float altimeter_setting_mbar){
107  return (1-pow((pressure_mbar/altimeter_setting_mbar), 0.190263)) * 4430.8;
108 }
109 
110 void lps25h_i2c_event(struct Lps25h_I2c *lps)
111 {
112  if (lps->initialized) {
113  if (lps->i2c_trans.status == I2CTransFailed) {
115  } else if (lps->i2c_trans.status == I2CTransSuccess) {
116  lps->data = Int32FromBuf(lps->i2c_trans.buf, 0)
117  lps->data_available = true;
119  }
120  } else if (lps->init_status != LPS25H_CONF_UNINIT) { // Configuring but not yet initialized
121  switch(lps->i2c_trans.status){
122  case I2CTransFailed:
123  lps->init_status--;
124  case I2CTransSuccess:
125  case I2CTransDone:
127  if(lps->initialized)
129  break;
130  default:
131  break;
132  }
133  }
134 }
135 
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
i2c_transaction::len_r
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
Lps25hConfig::ctrl1
uint8_t ctrl1
Definition: lps25h.h:43
lps25h_i2c_read
void lps25h_i2c_read(struct Lps25h_I2c *lps)
Definition: lps25h_i2c.c:87
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
Int32FromBuf
#define Int32FromBuf(buf, idx)
Definition: lps25h_i2c.c:98
Lps25h_I2c::initialized
bool initialized
config done flag
Definition: lps25h_i2c.h:42
Lps25h_I2c::data_available
volatile bool data_available
data ready flag
Definition: lps25h_i2c.h:43
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
lps25h_i2c.h
lps25h_i2c_event
void lps25h_i2c_event(struct Lps25h_I2c *lps)
Definition: lps25h_i2c.c:110
lps25h_i2c_start_configure
void lps25h_i2c_start_configure(struct Lps25h_I2c *lps)
Definition: lps25h_i2c.c:76
LPS25H_CONF_DONE
@ LPS25H_CONF_DONE
Definition: lps25h.h:39
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
std.h
Lps25h_I2c::init_status
enum Lps25hConfStatus init_status
init status
Definition: lps25h_i2c.h:41
LPS25H_CONF_UNINIT
@ LPS25H_CONF_UNINIT
Definition: lps25h.h:37
Lps25h_I2c::i2c_trans
struct i2c_transaction i2c_trans
Definition: lps25h_i2c.h:40
uint8_t
unsigned char uint8_t
Definition: types.h:14
Lps25h_I2c::data
int32_t data
Definition: lps25h_i2c.h:44
pressureToAltMeters
float pressureToAltMeters(float pressure_mbar, float altimeter_setting_mbar)
Definition: lps25h_i2c.c:106
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
I2CTransTxRx
@ I2CTransTxRx
transmit and receive transaction
Definition: i2c.h:49
lps25h_i2c_send_config
static void lps25h_i2c_send_config(struct Lps25h_I2c *lps)
Definition: lps25h_i2c.c:59
LPS25H_CTRL_REG1
#define LPS25H_CTRL_REG1
Definition: lps25h_regs.h:35
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
Lps25h_I2c::config
struct Lps25hConfig config
Definition: lps25h_i2c.h:45
lps25h_i2c_init
void lps25h_i2c_init(struct Lps25h_I2c *lps, struct i2c_periph *i2c_p, uint8_t addr)
Definition: lps25h_i2c.c:33
Lps25h_I2c::i2c_p
struct i2c_periph * i2c_p
Definition: lps25h_i2c.h:39
int32_t
signed long int32_t
Definition: types.h:19
LPS25H_CONF_CTRL1
@ LPS25H_CONF_CTRL1
Definition: lps25h.h:38
LPS25H_REG_OUT_XL
#define LPS25H_REG_OUT_XL
Definition: lps25h_regs.h:37
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
lps25h_set_default_config
static void lps25h_set_default_config(struct Lps25hConfig *c)
Definition: lps25h.h:46
i2c_submit
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
i2c_periph
Definition: i2c.h:144
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
lps25h_readPressureMillibars
float lps25h_readPressureMillibars(int32_t press)
Definition: lps25h_i2c.c:101
lps25h_i2c_tx_reg
static void lps25h_i2c_tx_reg(struct Lps25h_I2c *lps, uint8_t reg, uint8_t val)
Definition: lps25h_i2c.c:48
Lps25h_I2c
Definition: lps25h_i2c.h:38