PX4 Firmware
PX4 Autopilot Software http://px4.io
rm3100_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 rm3100_spi.cpp
36  *
37  * SPI interface for RM3100
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 
42 #include <assert.h>
43 #include <debug.h>
44 #include <errno.h>
45 #include <stdint.h>
46 #include <stdbool.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <unistd.h>
50 
51 #include <arch/board/board.h>
52 
53 #include <drivers/device/spi.h>
54 #include <drivers/drv_mag.h>
55 #include <drivers/drv_device.h>
56 
57 #include "board_config.h"
58 #include "rm3100.h"
59 
60 #if defined(PX4_SPIDEV_RM) || defined (PX4_SPIDEV_RM_EXT)
61 
62 /* SPI protocol address bits */
63 #define DIR_READ (1<<7)
64 #define DIR_WRITE (0<<7)
65 
66 class RM3100_SPI : public device::SPI
67 {
68 public:
69  RM3100_SPI(int bus, uint32_t device);
70  virtual ~RM3100_SPI() = default;
71 
72  virtual int init();
73  virtual int ioctl(unsigned operation, unsigned &arg);
74  virtual int read(unsigned address, void *data, unsigned count);
75  virtual int write(unsigned address, void *data, unsigned count);
76 };
77 
79 RM3100_SPI_interface(int bus);
80 
82 RM3100_SPI_interface(int bus)
83 {
84 #ifdef PX4_SPIDEV_RM_EXT
85  return new RM3100_SPI(bus, PX4_SPIDEV_RM_EXT);
86 #else
87  return new RM3100_SPI(bus, PX4_SPIDEV_RM);
88 #endif
89 }
90 
91 RM3100_SPI::RM3100_SPI(int bus, uint32_t device) :
92  SPI("RM3100_SPI", nullptr, bus, device, SPIDEV_MODE3, 1 * 1000 * 1000 /* */)
93 {
94  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_RM3100;
95 }
96 
97 int
99 {
100  int ret;
101 
102  ret = SPI::init();
103 
104  if (ret != OK) {
105  DEVICE_DEBUG("SPI init failed");
106  return -EIO;
107  }
108 
109  // Read REV_ID value
110  uint8_t data = 0;
111 
112  if (read(ADDR_REVID, &data, 1)) {
113  DEVICE_DEBUG("RM3100 read_reg fail");
114  }
115 
116  if (data != RM3100_REVID) {
117  DEVICE_DEBUG("RM3100 ID: %02x", data);
118  return -EIO;
119  }
120 
121  return OK;
122 }
123 
124 int
125 RM3100_SPI::ioctl(unsigned operation, unsigned &arg)
126 {
127  int ret;
128 
129  switch (operation) {
130 
131  case MAGIOCGEXTERNAL:
132  return external();
133 
134  case DEVIOCGDEVICEID:
135  return CDev::ioctl(nullptr, operation, arg);
136 
137  default: {
138  ret = -EINVAL;
139  }
140  }
141 
142  return ret;
143 }
144 
145 int
146 RM3100_SPI::read(unsigned address, void *data, unsigned count)
147 {
148  uint8_t buf[32];
149 
150  if (sizeof(buf) < (count + 1)) {
151  return -EIO;
152  }
153 
154  buf[0] = address | DIR_READ;
155 
156  int ret = transfer(&buf[0], &buf[0], count + 1);
157  memcpy(data, &buf[1], count);
158  return ret;
159 }
160 
161 int
162 RM3100_SPI::write(unsigned address, void *data, unsigned count)
163 {
164  uint8_t buf[32];
165 
166  if (sizeof(buf) < (count + 1)) {
167  return -EIO;
168  }
169 
170  buf[0] = address | DIR_WRITE;
171  memcpy(&buf[1], data, count);
172 
173  return transfer(&buf[0], &buf[0], count + 1);
174 }
175 
176 #endif /* PX4_SPIDEV_RM || PX4_SPIDEV_RM_EXT */
#define MAGIOCGEXTERNAL
determine if mag is external or onboard
Definition: drv_mag.h:88
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)
Generic device / sensor interface.
void init()
Activates/configures the hardware registers.
#define DRV_MAG_DEVTYPE_RM3100
Definition: drv_sensor.h:61
uint8_t * data
Definition: dataman.cpp:149
#define RM3100_REVID
Definition: rm3100.h:95
#define DIR_WRITE
Definition: bmp388_spi.cpp:47
static void write(bootloader_app_shared_t *pshared)
#define ADDR_REVID
Definition: rm3100.h:78
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
Shared defines for the RM3100 driver.
#define OK
Definition: uavcan_main.cpp:71
#define DEVICE_DEBUG(FMT,...)
Definition: Device.hpp:52
device::Device * RM3100_SPI_interface(int bus)