Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
mpl3115.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2011 Gautier Hattenberger
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
27 #include "peripherals/mpl3115.h"
28 #include "std.h"
29 
30 
31 void mpl3115_init(struct Mpl3115 *mpl, struct i2c_periph *i2c_p, uint8_t addr)
32 {
33 
34  /* set i2c_peripheral */
35  mpl->i2c_p = i2c_p;
36 
37  /* slave address */
38  mpl->trans.slave_addr = addr;
39 
40  mpl->trans.status = I2CTransDone;
42  mpl->initialized = false;
44 
45  /* by default disable raw mode and set pressure mode */
46  mpl->raw_mode = false;
47  mpl->alt_mode = false;
48 
49  mpl->pressure = 0;
50  mpl->temperature = 0;
51  mpl->altitude = 0.;
52 }
53 
54 // Configuration function called once before normal use
55 static void mpl3115_send_config(struct Mpl3115 *mpl)
56 {
57  switch (mpl->init_status) {
58  case MPL_CONF_PT_DATA:
60  mpl->trans.buf[1] = MPL3115_PT_DATA_CFG;
61  i2c_transmit(mpl->i2c_p, &mpl->trans, mpl->trans.slave_addr, 2);
62  mpl->init_status++;
63  break;
64  case MPL_CONF_CTRL1:
66  mpl->trans.buf[1] = ((MPL3115_OVERSAMPLING << 3) | (mpl->raw_mode << 6) |
67  (mpl->alt_mode << 7));
68  i2c_transmit(mpl->i2c_p, &mpl->trans, mpl->trans.slave_addr, 2);
69  mpl->init_status++;
70  break;
71  case MPL_CONF_DONE:
72  mpl->initialized = true;
73  mpl->trans.status = I2CTransDone;
74  break;
75  default:
76  break;
77  }
78 }
79 
80 // Configure
81 void mpl3115_configure(struct Mpl3115 *mpl)
82 {
83  if (mpl->init_status == MPL_CONF_UNINIT) {
84  mpl->init_status++;
85  if (mpl->trans.status == I2CTransSuccess || mpl->trans.status == I2CTransDone) {
87  }
88  }
89 }
90 
91 // Normal reading
92 void mpl3115_read(struct Mpl3115 *mpl)
93 {
94  // ask for a reading and then prepare next conversion
95  if (mpl->initialized && mpl->trans.status == I2CTransDone) {
96  mpl->trans.buf[0] = MPL3115_REG_STATUS;
97  i2c_transceive(mpl->i2c_p, &mpl->trans, mpl->trans.slave_addr, 1, 6);
98  if (mpl->req_trans.status == I2CTransDone) {
100  mpl->req_trans.buf[1] = ((MPL3115_OVERSAMPLING << 3) | (mpl->raw_mode << 6) |
101  (mpl->alt_mode << 7) | MPL3115_OST_BIT);
102  i2c_transmit(mpl->i2c_p, &mpl->req_trans, mpl->trans.slave_addr, 2);
103  }
104  }
105 }
106 
107 void mpl3115_event(struct Mpl3115 *mpl)
108 {
109  if (mpl->initialized) {
110  if (mpl->trans.status == I2CTransFailed) {
111  mpl->trans.status = I2CTransDone;
112  } else if (mpl->trans.status == I2CTransSuccess) {
113  // Successfull reading and new pressure data available
114  if (mpl->trans.buf[0] & (1 << 2)) {
115  if (mpl->raw_mode) {
116  // New data available
117  mpl->pressure = (((uint32_t)mpl->trans.buf[1] << 16) |
118  ((uint16_t)mpl->trans.buf[2] << 8) |
119  mpl->trans.buf[3]);
120  mpl->temperature = ((int16_t)mpl->trans.buf[4] << 8) | mpl->trans.buf[5];
121  } else { // not in raw mode
122  uint32_t tmp = (((uint32_t)mpl->trans.buf[1] << 16) |
123  ((uint16_t)mpl->trans.buf[2] << 8) |
124  mpl->trans.buf[3]);
125  if (mpl->alt_mode) {
126  mpl->altitude = (float)(tmp >> 4) / (1 << 4);
127  } else { // Pressure mode
128  mpl->pressure = (tmp >> 4);
129  }
130  tmp = ((int16_t)mpl->trans.buf[4] << 8) | mpl->trans.buf[5];
131  mpl->temperature = (tmp >> 4);
132  }
133  mpl->data_available = true;
134  }
135  mpl->trans.status = I2CTransDone;
136  }
137  } else if (!mpl->initialized && mpl->init_status != MPL_CONF_UNINIT) { // Configuring
138  if (mpl->trans.status == I2CTransSuccess || mpl->trans.status == I2CTransDone) {
139  mpl->trans.status = I2CTransDone;
140  mpl3115_send_config(mpl);
141  }
142  if (mpl->trans.status == I2CTransFailed) {
143  mpl->init_status--;
144  mpl->trans.status = I2CTransDone;
145  mpl3115_send_config(mpl); // Retry config (TODO max retry)
146  }
147  }
150  }
151 }
152 
153 void mpl3115_periodic(struct Mpl3115 *mpl)
154 {
155  if (mpl->initialized) {
156  mpl3115_read(mpl);
157  } else {
158  mpl3115_configure(mpl);
159  }
160 }
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:324
bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:344
@ 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
void mpl3115_read(struct Mpl3115 *mpl)
Definition: mpl3115.c:92
static void mpl3115_send_config(struct Mpl3115 *mpl)
Definition: mpl3115.c:55
void mpl3115_periodic(struct Mpl3115 *mpl)
Definition: mpl3115.c:153
void mpl3115_init(struct Mpl3115 *mpl, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpl3115.c:31
void mpl3115_configure(struct Mpl3115 *mpl)
Definition: mpl3115.c:81
void mpl3115_event(struct Mpl3115 *mpl)
Definition: mpl3115.c:107
Driver for the baro MPL3115A2 from Freescale (i2c)
uint32_t pressure
pressure in 1/4 Pascal
Definition: mpl3115.h:80
#define MPL3115_OST_BIT
Definition: mpl3115.h:52
bool alt_mode
set to TRUE to enable altitude output (otherwise pressure)
Definition: mpl3115.h:78
float altitude
altitude in meters
Definition: mpl3115.h:81
bool initialized
config done flag
Definition: mpl3115.h:75
@ MPL_CONF_UNINIT
Definition: mpl3115.h:64
@ MPL_CONF_PT_DATA
Definition: mpl3115.h:65
@ MPL_CONF_DONE
Definition: mpl3115.h:67
@ MPL_CONF_CTRL1
Definition: mpl3115.h:66
struct i2c_transaction req_trans
I2C transaction for conversion request.
Definition: mpl3115.h:73
#define MPL3115_OVERSAMPLING
Definition: mpl3115.h:59
#define MPL3115_REG_CTRL_REG1
Definition: mpl3115.h:46
bool raw_mode
set to TRUE to enable raw output
Definition: mpl3115.h:77
int16_t temperature
temperature in 1/16 degrees Celcius
Definition: mpl3115.h:79
struct i2c_periph * i2c_p
Definition: mpl3115.h:71
#define MPL3115_PT_DATA_CFG
Definition: mpl3115.h:56
enum Mpl3115Status init_status
Definition: mpl3115.h:74
struct i2c_transaction trans
I2C transaction for reading and configuring.
Definition: mpl3115.h:72
volatile bool data_available
data ready flag
Definition: mpl3115.h:76
#define MPL3115_REG_STATUS
Definition: mpl3115.h:38
#define MPL3115_REG_PT_DATA_CFG
Definition: mpl3115.h:45
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
short int16_t
Typedef defining 16 bit short type.
Definition: vl53l1_types.h:93
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98