PX4 Firmware
PX4 Autopilot Software http://px4.io
rm3100_i2c.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_i2c.cpp
36  *
37  * I2C 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/i2c.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_I2C_BUS_ONBOARD) || defined(PX4_I2C_BUS_EXPANSION)
61 
62 #define RM3100_ADDRESS 0x20
63 
64 class RM3100_I2C : public device::I2C
65 {
66 public:
67  RM3100_I2C(int bus);
68  virtual ~RM3100_I2C() = default;
69 
70  virtual int init();
71  virtual int ioctl(unsigned operation, unsigned &arg);
72  virtual int read(unsigned address, void *data, unsigned count);
73  virtual int write(unsigned address, void *data, unsigned count);
74 
75 protected:
76  virtual int probe();
77 
78 };
79 
81 RM3100_I2C_interface(int bus);
82 
84 RM3100_I2C_interface(int bus)
85 {
86  return new RM3100_I2C(bus);
87 }
88 
89 RM3100_I2C::RM3100_I2C(int bus) :
90  I2C("RM300_I2C", nullptr, bus, RM3100_ADDRESS, 400000)
91 {
92  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_RM3100;
93 }
94 
95 int
97 {
98  /* this will call probe() */
99  return I2C::init();
100 }
101 
102 int
103 RM3100_I2C::ioctl(unsigned operation, unsigned &arg)
104 {
105  switch (operation) {
106 
107  case MAGIOCGEXTERNAL:
108  return external();
109 
110  case DEVIOCGDEVICEID:
111  return CDev::ioctl(nullptr, operation, arg);
112 
113  default:
114  return -EINVAL;
115  }
116 }
117 
118 int
120 {
121  uint8_t data = 0;
122 
123  _retries = 10;
124 
125  if (read(ADDR_REVID, &data, 1)) {
126  DEVICE_DEBUG("RM3100 read_reg fail");
127  return -EIO;
128  }
129 
130  _retries = 2;
131 
132  if (data != RM3100_REVID) {
133  DEVICE_DEBUG("RM3100 bad ID: %02x", data);
134  return -EIO;
135  }
136 
137  return OK;
138 }
139 
140 int
141 RM3100_I2C::read(unsigned address, void *data, unsigned count)
142 {
143  uint8_t cmd = address;
144  int ret;
145 
146  /* We need a first transfer where we write the register to read */
147  ret = transfer(&cmd, 1, nullptr, 0);
148 
149  if (ret != OK) {
150  return ret;
151  }
152 
153  /* Now we read the previously selected register */
154  ret = transfer(nullptr, 0, (uint8_t *)data, count);
155 
156  return ret;
157 }
158 
159 int
160 RM3100_I2C::write(unsigned address, void *data, unsigned count)
161 {
162  uint8_t buf[32];
163 
164  if (sizeof(buf) < (count + 1)) {
165  return -EIO;
166  }
167 
168  buf[0] = address;
169  memcpy(&buf[1], data, count);
170 
171  return transfer(&buf[0], count + 1, nullptr, 0);
172 }
173 
174 #endif /* PX4_I2C_OBDEV_RM3100 */
#define MAGIOCGEXTERNAL
determine if mag is external or onboard
Definition: drv_mag.h:88
virtual int read(struct file *file_pointer, char *buffer, size_t buffer_len)
Definition: rm3100.cpp:501
device::Device * RM3100_I2C_interface(int bus)
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
virtual bool external() const
Definition: Device.hpp:237
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
Base class for devices connected via I2C.