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
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
31void 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;
41 mpl->req_trans.status = I2CTransDone;
42 mpl->initialized = false;
43 mpl->init_status = MPL_CONF_UNINIT;
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
55static void mpl3115_send_config(struct Mpl3115 *mpl)
56{
57 switch (mpl->init_status) {
59 mpl->trans.buf[0] = MPL3115_REG_PT_DATA_CFG;
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:
65 mpl->trans.buf[0] = MPL3115_REG_CTRL_REG1;
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
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
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) {
99 mpl->req_trans.buf[0] = MPL3115_REG_CTRL_REG1;
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
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;
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 }
148 if (mpl->req_trans.status == I2CTransSuccess || mpl->req_trans.status == I2CTransFailed) {
149 mpl->req_trans.status = I2CTransDone;
150 }
151}
152
154{
155 if (mpl->initialized) {
157 } else {
159 }
160}
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
Definition i2c.h:151
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:202
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:222
@ 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
uint16_t foo
Definition main_demo5.c:58
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)
#define MPL3115_OST_BIT
Definition mpl3115.h:52
@ 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
#define MPL3115_OVERSAMPLING
Definition mpl3115.h:59
#define MPL3115_REG_CTRL_REG1
Definition mpl3115.h:46
#define MPL3115_PT_DATA_CFG
Definition mpl3115.h:56
#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.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.