PX4 Firmware
PX4 Autopilot Software http://px4.io
BMP280_I2C.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016-2019 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 bmp280_spi.cpp
36  *
37  * SPI interface for BMP280
38  */
39 
40 #include "bmp280.h"
41 
42 #include <px4_platform_common/px4_config.h>
43 #include <drivers/device/i2c.h>
44 
45 #if defined(PX4_I2C_OBDEV_BMP280) || defined(PX4_I2C_EXT_OBDEV_BMP280)
46 
47 class BMP280_I2C: public device::I2C, public bmp280::IBMP280
48 {
49 public:
50  BMP280_I2C(uint8_t bus, uint32_t device);
51  virtual ~BMP280_I2C() override = default;
52 
53  int init() override { return I2C::init(); }
54 
55  uint8_t get_reg(uint8_t addr) override;
56  int set_reg(uint8_t value, uint8_t addr) override;
57 
58  bmp280::data_s *get_data(uint8_t addr) override;
59  bmp280::calibration_s *get_calibration(uint8_t addr) override;
60 
61  uint32_t get_device_id() const override { return device::I2C::get_device_id(); }
62 
63 private:
64  bmp280::calibration_s _cal{};
66 };
67 
68 bmp280::IBMP280 *bmp280_i2c_interface(uint8_t busnum, uint32_t device)
69 {
70  return new BMP280_I2C(busnum, device);
71 }
72 
73 BMP280_I2C::BMP280_I2C(uint8_t bus, uint32_t device) :
74  I2C("BMP280_I2C", nullptr, bus, device, 100 * 1000)
75 {
76 }
77 
78 uint8_t
79 BMP280_I2C::get_reg(uint8_t addr)
80 {
81  uint8_t cmd[2] = { (uint8_t)(addr), 0};
82  transfer(&cmd[0], 1, &cmd[1], 1);
83 
84  return cmd[1];
85 }
86 
87 int
88 BMP280_I2C::set_reg(uint8_t value, uint8_t addr)
89 {
90  uint8_t cmd[2] = { (uint8_t)(addr), value};
91  return transfer(cmd, sizeof(cmd), nullptr, 0);
92 }
93 
95 BMP280_I2C::get_data(uint8_t addr)
96 {
97  const uint8_t cmd = addr;
98 
99  if (transfer(&cmd, sizeof(cmd), (uint8_t *)&_data, sizeof(bmp280::data_s)) == OK) {
100  return (&_data);
101 
102  } else {
103  return nullptr;
104  }
105 }
106 
108 BMP280_I2C::get_calibration(uint8_t addr)
109 {
110  const uint8_t cmd = addr;
111 
112  if (transfer(&cmd, sizeof(cmd), (uint8_t *)&_cal, sizeof(bmp280::calibration_s)) == OK) {
113  return &(_cal);
114 
115  } else {
116  return nullptr;
117  }
118 }
119 
120 #endif /* PX4_I2C_OBDEV_BMP280 || PX4_I2C_EXT_OBDEV_BMP280 */
virtual int set_reg(uint8_t value, uint8_t addr)=0
bmp280::calibration_s * _cal
Definition: BMP280.hpp:75
virtual bmp280::data_s * get_data(uint8_t addr)=0
Shared defines for the bmp280 driver.
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
virtual bmp280::calibration_s * get_calibration(uint8_t addr)=0
bmp280::IBMP280 * bmp280_i2c_interface(uint8_t busnum, uint32_t device)
virtual uint8_t get_reg(uint8_t addr)=0
void init()
Activates/configures the hardware registers.
virtual uint32_t get_device_id() const =0
virtual int init()=0
#define OK
Definition: uavcan_main.cpp:71
static struct mpu9x50_data _data
IMU measurement data.
Base class for devices connected via I2C.