PX4 Firmware
PX4 Autopilot Software http://px4.io
bmp388_spi.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_spi.cpp
36  *
37  * SPI interface for BMP388 (NOTE: untested!)
38  */
39 
40 #include <drivers/device/spi.h>
41 
42 #include "bmp388.h"
43 
44 
45 /* SPI protocol address bits */
46 #define DIR_READ (1<<7) //for set
47 #define DIR_WRITE ~(1<<7) //for clear
48 
49 #if defined(PX4_SPIDEV_BARO) || defined(PX4_SPIDEV_EXT_BARO)
50 
51 #pragma pack(push,1)
52 struct spi_data_s {
53  uint8_t addr;
54  struct data_s data;
55 };
56 
57 struct spi_calibration_s {
58  uint8_t addr;
59  struct calibration_s cal;
60 };
61 #pragma pack(pop)
62 
63 class BMP388_SPI: public device::SPI, public IBMP388
64 {
65 public:
66  BMP388_SPI(uint8_t bus, uint32_t device);
67  virtual ~BMP388_SPI() = default;
68 
69  int init();
70 
71  uint8_t get_reg(uint8_t addr);
72  int get_reg_buf(uint8_t addr, uint8_t *buf, uint8_t len);
73  int set_reg(uint8_t value, uint8_t addr);
74  data_s *get_data(uint8_t addr);
75  calibration_s *get_calibration(uint8_t addr);
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 
84 IBMP388 *bmp388_spi_interface(uint8_t busnum, uint32_t device)
85 {
86  return new BMP388_SPI(busnum, device);
87 }
88 
89 BMP388_SPI::BMP388_SPI(uint8_t bus, uint32_t device) :
90  SPI("BMP388_SPI", nullptr, bus, device, SPIDEV_MODE3, 10 * 1000 * 1000)
91 {
92 }
93 
94 int BMP388_SPI::init()
95 {
96  return SPI::init();
97 };
98 
99 uint8_t BMP388_SPI::get_reg(uint8_t addr)
100 {
101  uint8_t cmd[2] = { (uint8_t)(addr | DIR_READ), 0}; //set MSB bit
102  transfer(&cmd[0], &cmd[0], 2);
103 
104  return cmd[1];
105 }
106 
107 int BMP388_SPI::get_reg_buf(uint8_t addr, uint8_t *buf, uint8_t len)
108 {
109  uint8_t cmd[1] = {(uint8_t)(addr | DIR_READ)};
110  return transfer(&cmd[0], buf, len);
111 }
112 
113 int BMP388_SPI::set_reg(uint8_t value, uint8_t addr)
114 {
115  uint8_t cmd[2] = { (uint8_t)(addr & DIR_WRITE), value}; //clear MSB bit
116  return transfer(&cmd[0], nullptr, 2);
117 }
118 
119 data_s *BMP388_SPI::get_data(uint8_t addr)
120 {
121  _data.addr = (uint8_t)(addr | DIR_READ); //set MSB bit
122 
123  if (transfer((uint8_t *)&_data, (uint8_t *)&_data, sizeof(struct spi_data_s)) == OK) {
124  return &(_data.data);
125 
126  } else {
127  return nullptr;
128  }
129 }
130 
131 calibration_s *BMP388_SPI::get_calibration(uint8_t addr)
132 {
133  _cal.addr = addr | DIR_READ;
134 
135  if (transfer((uint8_t *)&_cal, (uint8_t *)&_cal, sizeof(struct spi_calibration_s)) == OK) {
136  return &(_cal.cal);
137 
138  } else {
139  return nullptr;
140  }
141 }
142 
143 #endif /* PX4_SPIDEV_BARO || PX4_SPIDEV_EXT_BARO */
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
#define DIR_READ
Definition: bmp388_spi.cpp:46
Definition: bmp388.h:198
void init()
Activates/configures the hardware registers.
#define DIR_WRITE
Definition: bmp388_spi.cpp:47
Shared defines for the bmp388 driver.
IBMP388 * bmp388_spi_interface(uint8_t busnum, uint32_t device)
#define OK
Definition: uavcan_main.cpp:71
static struct mpu9x50_data _data
IMU measurement data.