PX4 Firmware
PX4 Autopilot Software http://px4.io
lps25h_i2c.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016 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 LPS25H_I2C.cpp
36  *
37  * I2C interface for LPS25H
38  */
39 
40 #include "lps25h.h"
41 
42 #define LPS25H_ADDRESS 0x5D
43 
45 
46 class LPS25H_I2C : public device::I2C
47 {
48 public:
49  LPS25H_I2C(int bus);
50  virtual ~LPS25H_I2C() = default;
51 
52  virtual int read(unsigned address, void *data, unsigned count);
53  virtual int write(unsigned address, void *data, unsigned count);
54 
55  virtual int ioctl(unsigned operation, unsigned &arg);
56 
57 protected:
58  virtual int probe();
59 
60 };
61 
64 {
65  return new LPS25H_I2C(bus);
66 }
67 
69  I2C("LPS25H_I2C", nullptr, bus, LPS25H_ADDRESS, 400000)
70 {
71 }
72 
73 int
74 LPS25H_I2C::ioctl(unsigned operation, unsigned &arg)
75 {
76  int ret;
77 
78  switch (operation) {
79 
80  case DEVIOCGDEVICEID:
81  return CDev::ioctl(nullptr, operation, arg);
82 
83  default:
84  ret = -EINVAL;
85  }
86 
87  return ret;
88 }
89 
90 int
92 {
93  uint8_t id;
94 
95  _retries = 10;
96 
97  if (read(ADDR_WHO_AM_I, &id, 1)) {
98  DEVICE_DEBUG("read_reg fail");
99  return -EIO;
100  }
101 
102  _retries = 2;
103 
104  if (id != ID_WHO_AM_I) {
105  DEVICE_DEBUG("ID byte mismatch (%02x != %02x)", ID_WHO_AM_I, id);
106  return -EIO;
107  }
108 
109  return OK;
110 }
111 
112 int
113 LPS25H_I2C::write(unsigned address, void *data, unsigned count)
114 {
115  uint8_t buf[32];
116 
117  if (sizeof(buf) < (count + 1)) {
118  return -EIO;
119  }
120 
121  buf[0] = address;
122  memcpy(&buf[1], data, count);
123 
124  return transfer(&buf[0], count + 1, nullptr, 0);
125 }
126 
127 int
128 LPS25H_I2C::read(unsigned address, void *data, unsigned count)
129 {
130  uint8_t cmd = address;
131  return transfer(&cmd, 1, (uint8_t *)data, count);
132 }
Shared defines for the lps25h driver.
LPS25H_I2C(int bus)
Definition: lps25h_i2c.cpp:68
device::Device * LPS25H_I2C_interface(int bus)
Definition: lps25h_i2c.cpp:63
#define ADDR_WHO_AM_I
Definition: lps25h.cpp:52
virtual int write(unsigned address, void *data, unsigned count)
Definition: lps25h_i2c.cpp:113
#define ID_WHO_AM_I
Definition: lps25h.h:59
#define LPS25H_ADDRESS
Definition: lps25h_i2c.cpp:42
virtual int read(unsigned address, void *data, unsigned count)
Definition: lps25h_i2c.cpp:128
uint8_t * data
Definition: dataman.cpp:149
virtual int ioctl(unsigned operation, unsigned &arg)
Definition: lps25h_i2c.cpp:74
virtual ~LPS25H_I2C()=default
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
#define OK
Definition: uavcan_main.cpp:71
#define DEVICE_DEBUG(FMT,...)
Definition: Device.hpp:52
virtual int probe()
Definition: lps25h_i2c.cpp:91