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
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
29#include "std.h"
30#include <stdio.h>
31#include <math.h>
32
33void 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;
39 lps->i2c_trans.status = I2CTransDone;
40 /* set default config options */
42 lps->initialized = false;
43 lps->data_available = false;
44 lps->init_status = LPS25H_CONF_UNINIT;
45}
46
47
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
60{
61 switch (lps->init_status) {
64 lps->init_status++;
65 break;
67 lps->initialized = true;
68 lps->i2c_trans.status = I2CTransDone;
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++;
80 if (lps->i2c_trans.status == I2CTransSuccess || lps->i2c_trans.status == I2CTransDone) {
82 }
83 }
84}
85
86// Normal reading
88{
89 if (lps->initialized && lps->i2c_trans.status == I2CTransDone) {
90 lps->i2c_trans.buf[0] = LPS25H_REG_OUT_XL | (1 << 7);
91 lps->i2c_trans.type = I2CTransTxRx;
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
107 return (1-pow((pressure_mbar/altimeter_setting_mbar), 0.190263)) * 4430.8;
108}
109
111{
112 if (lps->initialized) {
113 if (lps->i2c_trans.status == I2CTransFailed) {
114 lps->i2c_trans.status = I2CTransDone;
115 } else if (lps->i2c_trans.status == I2CTransSuccess) {
116 lps->data = Int32FromBuf(lps->i2c_trans.buf, 0)
117 lps->data_available = true;
118 lps->i2c_trans.status = I2CTransDone;
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)
128 lps->i2c_trans.status = I2CTransDone;
129 break;
130 default:
131 break;
132 }
133 }
134}
135
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
@ LPS25H_CONF_DONE
Definition lps25h.h:39
@ LPS25H_CONF_UNINIT
Definition lps25h.h:37
@ LPS25H_CONF_CTRL1
Definition lps25h.h:38
static void lps25h_set_default_config(struct Lps25hConfig *c)
Definition lps25h.h:46
float lps25h_readPressureMillibars(int32_t press)
Definition lps25h_i2c.c:101
#define Int32FromBuf(buf, idx)
Definition lps25h_i2c.c:98
static void lps25h_i2c_send_config(struct Lps25h_I2c *lps)
Definition lps25h_i2c.c:59
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
void lps25h_i2c_init(struct Lps25h_I2c *lps, struct i2c_periph *i2c_p, uint8_t addr)
Definition lps25h_i2c.c:33
void lps25h_i2c_read(struct Lps25h_I2c *lps)
Definition lps25h_i2c.c:87
static void lps25h_i2c_tx_reg(struct Lps25h_I2c *lps, uint8_t reg, uint8_t val)
Definition lps25h_i2c.c:48
float pressureToAltMeters(float pressure_mbar, float altimeter_setting_mbar)
Definition lps25h_i2c.c:106
I2C interface for LPS25H barometer.
#define LPS25H_REG_OUT_XL
Definition lps25h_regs.h:37
#define LPS25H_CTRL_REG1
Definition lps25h_regs.h:35
uint16_t foo
Definition main_demo5.c:58
uint16_t val[TCOUPLE_NB]
int int32_t
Typedef defining 32 bit int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.