PX4 Firmware
PX4 Autopilot Software http://px4.io
mpl3115a2_i2c.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file mpl3115a2_i2c.cpp
36  *
37  * I2C interface for MPL3115A2
38  */
39 
40 #include "mpl3115a2.h"
41 
42 #define MPL3115A2_ADDRESS 0x60
43 
45 
46 class MPL3115A2_I2C : public device::I2C
47 {
48 public:
49  MPL3115A2_I2C(uint8_t bus);
50  virtual ~MPL3115A2_I2C() = default;
51 
52  virtual int init();
53  virtual int read(unsigned offset, void *data, unsigned count);
54  virtual int ioctl(unsigned operation, unsigned &arg);
55 
56 protected:
57  virtual int probe();
58 
59 private:
60 
61  /**
62  * Send a measure command to the MPL3115A2.
63  *
64  * @param addr Which address to use for the measure operation.
65  */
66  int _measure(unsigned addr);
67 
68  int reg_read(uint8_t reg, void *data, unsigned count = 1);
69  int reg_write(uint8_t reg, uint8_t data);
70  int reset();
71 
72 };
73 
75 MPL3115A2_i2c_interface(uint8_t busnum)
76 {
77  return new MPL3115A2_I2C(busnum);
78 }
79 
81  I2C("MPL3115A2_I2C", nullptr, bus, 0, 400000)
82 {
83 }
84 
85 int
87 {
88  /* this will call probe() */
89  set_device_address(MPL3115A2_ADDRESS);
90  return I2C::init();
91 }
92 
93 int
95 {
96  int max = 10;
98  int rv = CTRL_REG1_RST;
99  int ret = 1;
100 
101  while (ret == 1 && (rv & CTRL_REG1_RST) && max--) {
102  usleep(4000);
103  ret = reg_read(MPL3115A2_CTRL_REG1, &rv);
104  }
105 
106  return ret == 1 ? PX4_OK : ret;
107 }
108 
109 int
110 MPL3115A2_I2C::read(unsigned offset, void *data, unsigned count)
111 {
112 
113  int ret = -EINVAL;
114 
115  switch (offset) {
116  case MPL3115A2_CTRL_REG1:
117  ret = reg_read(offset, data, count);
118  break;
119 
120  case OUT_P_MSB: {
121  union _cvt {
122  MPL3115A2_data_t reading;
123  } *cvt = (_cvt *)data;
124 
125  /* read the most recent measurement
126  * 3 Pressure and 2 temprtture
127  */
128  uint8_t b[3 + 2];
129  uint8_t reg = (uint8_t) offset;
130 
131  ret = transfer(&reg, 1, &b[0], sizeof(b));
132 
133  if (ret == PX4_OK) {
134  cvt->reading.pressure.q = ((uint32_t)b[0]) << 18 | ((uint32_t) b[1]) << 10 | (((uint32_t)b[2]) & 0xc0) << 2 | ((
135  b[2] & 0x30) >> 4);
136  cvt->reading.temperature.w = ((uint16_t) b[3]) << 8 | (b[4] >> 4);
137 
138  }
139  }
140  break;
141  }
142 
143  return ret;
144 }
145 
146 int
147 MPL3115A2_I2C::ioctl(unsigned operation, unsigned &arg)
148 {
149  int ret;
150 
151  switch (operation) {
152  case IOCTL_RESET:
153  ret = reset();
154  break;
155 
156  case IOCTL_MEASURE:
157  ret = _measure(arg);
158  break;
159 
160  default:
161  ret = EINVAL;
162  }
163 
164  return ret;
165 }
166 
167 int
169 {
170  _retries = 10;
171  uint8_t whoami = 0;
172 
173  if ((reg_read(MPL3115A2_REG_WHO_AM_I, &whoami) > 0) && (whoami == MPL3115A2_WHO_AM_I)) {
174  /*
175  * Disable retries; we may enable them selectively in some cases,
176  * but the device gets confused if we retry some of the commands.
177  */
178  _retries = 0;
179  return PX4_OK;
180  }
181 
182  return -EIO;
183 }
184 
185 int
187 {
188  /*
189  * Disable retries on this command; we can't know whether failure
190  * means the device did or did not see the command.
191  */
192  _retries = 0;
193  return reg_write((addr >> 8) & 0xff, addr & 0xff);
194 }
195 
196 
197 int MPL3115A2_I2C::reg_read(uint8_t reg, void *data, unsigned count)
198 {
199  uint8_t cmd = reg;
200  int ret = transfer(&cmd, 1, (uint8_t *)data, count);
201  return ret == PX4_OK ? count : ret;
202 }
203 
204 int MPL3115A2_I2C::reg_write(uint8_t reg, uint8_t data)
205 {
206  uint8_t buf[2] = { reg, data};
207  int ret = transfer(buf, sizeof(buf), NULL, 0);
208  return ret == PX4_OK ? 2 : ret;
209 }
#define CTRL_REG1_RST
Definition: mpl3115a2.h:87
virtual int probe()
int reg_read(uint8_t reg, void *data, unsigned count=1)
#define MPL3115A2_WHO_AM_I
Definition: mpl3115a2.h:77
virtual int ioctl(unsigned operation, unsigned &arg)
Shared defines for the mpl3115a2 driver.
MPL3115A2_I2C(uint8_t bus)
int _measure(unsigned addr)
Send a measure command to the MPL3115A2.
virtual int read(unsigned offset, void *data, unsigned count)
#define MPL3115A2_REG_WHO_AM_I
Definition: mpl3115a2.h:76
void init()
Activates/configures the hardware registers.
#define MPL3115A2_CTRL_REG1
Definition: mpl3115a2.h:81
uint8_t * data
Definition: dataman.cpp:149
#define MPL3115A2_ADDRESS
int reg_write(uint8_t reg, uint8_t data)
device::Device * MPL3115A2_i2c_interface(uint8_t busnum)
#define IOCTL_MEASURE
Definition: mpl3115a2.h:93
constexpr _Tp max(_Tp a, _Tp b)
Definition: Limits.hpp:60
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define OUT_P_MSB
Definition: mpl3115a2.h:79
virtual int init()
virtual ~MPL3115A2_I2C()=default
#define IOCTL_RESET
Definition: mpl3115a2.h:92