PX4 Firmware
PX4 Autopilot Software http://px4.io
LPS22HB_I2C.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 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 lps22hb_i2c.cpp
36  *
37  * I2C interface for lps22hb
38  */
39 
40 #include "LPS22HB.hpp"
41 
42 #define LPS22HB_ADDRESS 0x5D
43 
45 
46 class LPS22HB_I2C : public device::I2C
47 {
48 public:
49  LPS22HB_I2C(int bus);
50  virtual ~LPS22HB_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 protected:
56  virtual int probe();
57 
58 };
59 
62 {
63  return new LPS22HB_I2C(bus);
64 }
65 
67  I2C("LPS22HB_I2C", nullptr, bus, LPS22HB_ADDRESS, 400000)
68 {
69 }
70 
71 int
73 {
74  uint8_t id;
75 
76  _retries = 10;
77 
78  if (read(WHO_AM_I, &id, 1)) {
79  DEVICE_DEBUG("read_reg fail");
80  return -EIO;
81  }
82 
83  _retries = 2;
84 
85  if (id != LPS22HB_ID_WHO_AM_I) {
86  DEVICE_DEBUG("ID byte mismatch (%02x != %02x)", LPS22HB_ID_WHO_AM_I, id);
87  return -EIO;
88  }
89 
90  return OK;
91 }
92 
93 int
94 LPS22HB_I2C::write(unsigned address, void *data, unsigned count)
95 {
96  uint8_t buf[32];
97 
98  if (sizeof(buf) < (count + 1)) {
99  return -EIO;
100  }
101 
102  buf[0] = address;
103  memcpy(&buf[1], data, count);
104 
105  return transfer(&buf[0], count + 1, nullptr, 0);
106 }
107 
108 int
109 LPS22HB_I2C::read(unsigned address, void *data, unsigned count)
110 {
111  uint8_t cmd = address;
112  return transfer(&cmd, 1, (uint8_t *)data, count);
113 }
static constexpr uint8_t LPS22HB_ID_WHO_AM_I
Definition: LPS22HB.hpp:51
device::Device * LPS22HB_I2C_interface(int bus)
Definition: LPS22HB_I2C.cpp:61
virtual int probe()
Definition: LPS22HB_I2C.cpp:72
virtual int write(unsigned address, void *data, unsigned count)
Definition: LPS22HB_I2C.cpp:94
LPS22HB_I2C(int bus)
Definition: LPS22HB_I2C.cpp:66
#define WHO_AM_I
Definition: FXAS21002C.cpp:83
uint8_t * data
Definition: dataman.cpp:149
#define LPS22HB_ADDRESS
Definition: LPS22HB_I2C.cpp:42
virtual int read(unsigned address, void *data, unsigned count)
virtual ~LPS22HB_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