PX4 Firmware
PX4 Autopilot Software http://px4.io
BMP280_SPI.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/spi.h>
44 
45 #if defined(PX4_SPIDEV_BARO) || defined(PX4_SPIDEV_EXT_BARO)
46 
47 /* SPI protocol address bits */
48 #define DIR_READ (1<<7) //for set
49 #define DIR_WRITE ~(1<<7) //for clear
50 
51 #pragma pack(push,1)
52 struct spi_data_s {
53  uint8_t addr;
54  struct bmp280::data_s data;
55 };
56 
57 struct spi_calibration_s {
58  uint8_t addr;
59  struct bmp280::calibration_s cal;
60 };
61 #pragma pack(pop)
62 
63 class BMP280_SPI: public device::SPI, public bmp280::IBMP280
64 {
65 public:
66  BMP280_SPI(uint8_t bus, uint32_t device);
67  virtual ~BMP280_SPI() override = default;
68 
69  int init() override { return SPI::init(); }
70 
71  uint8_t get_reg(uint8_t addr) override;
72  int set_reg(uint8_t value, uint8_t addr) override;
73 
74  bmp280::data_s *get_data(uint8_t addr) override;
75  bmp280::calibration_s *get_calibration(uint8_t addr) override;
76 
77  uint32_t get_device_id() const override { return device::SPI::get_device_id(); }
78 
79 private:
80  spi_calibration_s _cal{};
81  spi_data_s _data{};
82 };
83 
85 bmp280_spi_interface(uint8_t busnum, uint32_t device)
86 {
87  return new BMP280_SPI(busnum, device);
88 }
89 
90 BMP280_SPI::BMP280_SPI(uint8_t bus, uint32_t device) :
91  SPI("BMP280_SPI", nullptr, bus, device, SPIDEV_MODE3, 10 * 1000 * 1000)
92 {
93 }
94 
95 uint8_t
96 BMP280_SPI::get_reg(uint8_t addr)
97 {
98  uint8_t cmd[2] = { (uint8_t)(addr | DIR_READ), 0}; // set MSB bit
99  transfer(&cmd[0], &cmd[0], 2);
100 
101  return cmd[1];
102 }
103 
104 int
105 BMP280_SPI::set_reg(uint8_t value, uint8_t addr)
106 {
107  uint8_t cmd[2] = { (uint8_t)(addr & DIR_WRITE), value}; // clear MSB bit
108  return transfer(&cmd[0], nullptr, 2);
109 }
110 
112 BMP280_SPI::get_data(uint8_t addr)
113 {
114  _data.addr = (uint8_t)(addr | DIR_READ); // set MSB bit
115 
116  if (transfer((uint8_t *)&_data, (uint8_t *)&_data, sizeof(spi_data_s)) == OK) {
117  return &(_data.data);
118 
119  } else {
120  return nullptr;
121  }
122 }
123 
125 BMP280_SPI::get_calibration(uint8_t addr)
126 {
127  _cal.addr = addr | DIR_READ;
128 
129  if (transfer((uint8_t *)&_cal, (uint8_t *)&_cal, sizeof(spi_calibration_s)) == OK) {
130  return &(_cal.cal);
131 
132  } else {
133  return nullptr;
134  }
135 }
136 
137 #endif /* PX4_SPIDEV_BARO || PX4_SPIDEV_EXT_BARO */
bmp280::IBMP280 * bmp280_spi_interface(uint8_t busnum, uint32_t device)
Shared defines for the bmp280 driver.
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
#define DIR_READ
Definition: bmp388_spi.cpp:46
void init()
Activates/configures the hardware registers.
uint8_t * data
Definition: dataman.cpp:149
#define DIR_WRITE
Definition: bmp388_spi.cpp:47
#define OK
Definition: uavcan_main.cpp:71
static struct mpu9x50_data _data
IMU measurement data.