PX4 Firmware
PX4 Autopilot Software http://px4.io
LPS22HB_SPI.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_SPI.cpp
36  *
37  * SPI interface for LPS22HB
38  */
39 
40 #include "LPS22HB.hpp"
41 
42 #ifdef PX4_SPIDEV_LPS22HB
43 
44 /* SPI protocol address bits */
45 #define DIR_READ (1<<7)
46 #define DIR_WRITE (0<<7)
47 
49 
50 class LPS22HB_SPI : public device::SPI
51 {
52 public:
53  LPS22HB_SPI(int bus, uint32_t device);
54  virtual ~LPS22HB_SPI() = default;
55 
56  virtual int init();
57  virtual int read(unsigned address, void *data, unsigned count);
58  virtual int write(unsigned address, void *data, unsigned count);
59 
60 };
61 
63 LPS22HB_SPI_interface(int bus)
64 {
65  return new LPS22HB_SPI(bus, PX4_SPIDEV_LPS22HB);
66 }
67 
68 LPS22HB_SPI::LPS22HB_SPI(int bus, uint32_t device) : SPI("LPS22HB_SPI", nullptr, bus, device, SPIDEV_MODE3, 10000000)
69 {
70  _device_id.devid_s.devtype = DRV_BARO_DEVTYPE_LPS22HB;
71 }
72 
73 int
75 {
76  int ret = SPI::init();
77 
78  if (ret != OK) {
79  DEVICE_DEBUG("SPI init failed");
80  return -EIO;
81  }
82 
83  // read WHO_AM_I value
84  uint8_t id = 0;
85 
86  if (read(WHO_AM_I, &id, 1)) {
87  DEVICE_DEBUG("read_reg fail");
88  return -EIO;
89  }
90 
91  if (id != LPS22HB_ID_WHO_AM_I) {
92  DEVICE_DEBUG("ID byte mismatch (%02x != %02x)", LPS22HB_ID_WHO_AM_I, id);
93  return -EIO;
94  }
95 
96  return OK;
97 }
98 
99 int
100 LPS22HB_SPI::write(unsigned address, void *data, unsigned count)
101 {
102  uint8_t buf[32];
103 
104  if (sizeof(buf) < (count + 1)) {
105  return -EIO;
106  }
107 
108  buf[0] = address | DIR_WRITE;
109  memcpy(&buf[1], data, count);
110 
111  return transfer(&buf[0], &buf[0], count + 1);
112 }
113 
114 int
115 LPS22HB_SPI::read(unsigned address, void *data, unsigned count)
116 {
117  uint8_t buf[32];
118 
119  if (sizeof(buf) < (count + 1)) {
120  return -EIO;
121  }
122 
123  buf[0] = address | DIR_READ;
124 
125  int ret = transfer(&buf[0], &buf[0], count + 1);
126  memcpy(data, &buf[1], count);
127  return ret;
128 }
129 
130 #endif /* PX4_SPIDEV_LPS22HB */
static constexpr uint8_t LPS22HB_ID_WHO_AM_I
Definition: LPS22HB.hpp:51
#define DRV_BARO_DEVTYPE_LPS22HB
Definition: drv_sensor.h:109
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
#define DIR_READ
Definition: bmp388_spi.cpp:46
static void read(bootloader_app_shared_t *pshared)
#define WHO_AM_I
Definition: FXAS21002C.cpp:83
void init()
Activates/configures the hardware registers.
uint8_t * data
Definition: dataman.cpp:149
device::Device * LPS22HB_SPI_interface(int bus)
#define DIR_WRITE
Definition: bmp388_spi.cpp:47
static void write(bootloader_app_shared_t *pshared)
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