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