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
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 
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
void lps25h_i2c_init(struct Lps25h_I2c *lps, struct i2c_periph *i2c_p, uint8_t addr)
Definition: lps25h_i2c.c:33
void lps25h_i2c_event(struct Lps25h_I2c *lps)
Definition: lps25h_i2c.c:110
void lps25h_i2c_start_configure(struct Lps25h_I2c *lps)
Definition: lps25h_i2c.c:76
transmit and receive transaction
Definition: i2c.h:49
struct Lps25hConfig config
Definition: lps25h_i2c.h:45
I2C interface for LPS25H barometer.
enum Lps25hConfStatus init_status
init status
Definition: lps25h_i2c.h:41
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
#define Int32FromBuf(buf, idx)
Definition: lps25h_i2c.c:98
float pressureToAltMeters(float pressure_mbar, float altimeter_setting_mbar)
Definition: lps25h_i2c.c:106
transaction set to done by user level
Definition: i2c.h:59
uint16_t val[TCOUPLE_NB]
transaction failed
Definition: i2c.h:58
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define LPS25H_REG_OUT_XL
Definition: lps25h_regs.h:37
signed long int32_t
Definition: types.h:19
bool initialized
config done flag
Definition: lps25h_i2c.h:42
uint8_t ctrl1
Definition: lps25h.h:43
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
unsigned char uint8_t
Definition: types.h:14
static void lps25h_i2c_tx_reg(struct Lps25h_I2c *lps, uint8_t reg, uint8_t val)
Definition: lps25h_i2c.c:48
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
static void lps25h_set_default_config(struct Lps25hConfig *c)
Definition: lps25h.h:46
transmit only transaction
Definition: i2c.h:47
#define LPS25H_CTRL_REG1
Definition: lps25h_regs.h:35
void lps25h_i2c_read(struct Lps25h_I2c *lps)
Definition: lps25h_i2c.c:87
float lps25h_readPressureMillibars(int32_t press)
Definition: lps25h_i2c.c:101
struct i2c_periph * i2c_p
Definition: lps25h_i2c.h:39
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
int32_t data
Definition: lps25h_i2c.h:44
volatile bool data_available
data ready flag
Definition: lps25h_i2c.h:43
static void lps25h_i2c_send_config(struct Lps25h_I2c *lps)
Definition: lps25h_i2c.c:59
struct i2c_transaction i2c_trans
Definition: lps25h_i2c.h:40