Firmware
rm3100.h
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 
40 #pragma once
41 
42 #include <float.h>
43 
44 #include <drivers/device/i2c.h>
46 #include <drivers/drv_hrt.h>
47 #include <drivers/drv_mag.h>
48 
50 
51 #include <perf/perf_counter.h>
52 #include <px4_defines.h>
53 #include <systemlib/err.h>
54 
55 #ifndef CONFIG_SCHED_WORKQUEUE
56 # error This requires CONFIG_SCHED_WORKQUEUE.
57 #endif
58 
63 /* At 146 Hz we encounter errors, 100 Hz is safer */
64 #define RM3100_CONVERSION_INTERVAL 10000 // Microseconds, corresponds to 100 Hz (cycle count 200 on 3 axis)
65 #define UTESLA_TO_GAUSS 100.0f
66 #define RM3100_SENSITIVITY 75.0f
67 
68 #define ADDR_POLL 0x00
69 #define ADDR_CMM 0x01
70 #define ADDR_CCX 0x04
71 #define ADDR_CCY 0x06
72 #define ADDR_CCZ 0x08
73 #define ADDR_TMRC 0x0B
74 #define ADDR_MX 0x24
75 #define ADDR_MY 0x27
76 #define ADDR_MZ 0x2A
77 #define ADDR_BIST 0x33
78 #define ADDR_STATUS 0x34
79 #define ADDR_HSHAKE 0x35
80 #define ADDR_REVID 0x36
81 
82 #define CCX_DEFAULT_MSB 0x00
83 #define CCX_DEFAULT_LSB 0xC8
84 #define CCY_DEFAULT_MSB CCX_DEFAULT_MSB
85 #define CCY_DEFAULT_LSB CCX_DEFAULT_LSB
86 #define CCZ_DEFAULT_MSB CCX_DEFAULT_MSB
87 #define CCZ_DEFAULT_LSB CCX_DEFAULT_LSB
88 #define CMM_DEFAULT 0x70 // No continuous mode
89 #define CONTINUOUS_MODE (1 << 0)
90 #define POLLING_MODE (0 << 0)
91 #define TMRC_DEFAULT 0x94
92 #define BIST_SELFTEST 0x8F
93 #define BIST_DEFAULT 0x00
94 #define BIST_XYZ_OK ((1 << 4) | (1 << 5) | (1 << 6))
95 #define STATUS_DRDY (1 << 7)
96 #define POLL_XYZ 0x70
97 #define RM3100_REVID 0x22
98 
99 #define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
100 
101 /* interface factories */
102 extern device::Device *RM3100_SPI_interface(int bus);
103 extern device::Device *RM3100_I2C_interface(int bus);
104 typedef device::Device *(*RM3100_constructor)(int);
105 
106 enum RM3100_BUS {
107  RM3100_BUS_ALL = 0,
108  RM3100_BUS_I2C_INTERNAL,
109  RM3100_BUS_I2C_EXTERNAL,
110  RM3100_BUS_SPI_INTERNAL,
111  RM3100_BUS_SPI_EXTERNAL
112 };
113 
114 enum OPERATING_MODE {
115  CONTINUOUS = 0,
116  SINGLE
117 };
118 
119 
120 class RM3100 : public device::CDev
121 {
122 public:
123  RM3100(device::Device *interface, const char *path, enum Rotation rotation);
124 
125  virtual ~RM3100();
126 
127  virtual int init();
128 
129  virtual int ioctl(struct file *file_pointer, int cmd, unsigned long arg);
130 
131  virtual int read(struct file *file_pointer, char *buffer, size_t buffer_len);
132 
136  void print_info();
137 
142 
146  void stop();
147 
148 protected:
149  Device *_interface;
150 
151 private:
152  work_s _work;
153 
154  ringbuffer::RingBuffer *_reports;
155 
156  struct mag_calibration_s _scale;
157 
158  struct mag_report _last_report {};
160  orb_advert_t _mag_topic;
161 
162  perf_counter_t _comms_errors;
163  perf_counter_t _conf_errors;
164  perf_counter_t _range_errors;
165  perf_counter_t _sample_perf;
166 
167  /* status reporting */
168  bool _calibrated;
169  bool _continuous_mode_set;
170 
171  enum OPERATING_MODE _mode;
172  enum Rotation _rotation;
173 
174  unsigned int _measure_ticks;
175 
176  int _class_instance;
177  int _orb_class_instance;
178 
179  float _range_scale;
180 
181  uint8_t _check_state_cnt;
182 
186  int collect();
187 
193  int self_test();
194 
200  int check_measurement();
201 
205  void convert_signed(int32_t *n);
206 
220  void cycle();
221 
228  static void cycle_trampoline(void *arg);
229 
235  int measure();
236 
240  int reset();
241 
248  void start();
249 
250  /* this class has pointer data members, do not allow copying it */
251  RM3100(const RM3100 &);
252 
253  RM3100 operator=(const RM3100 &);
254 }; // class RM3100
A flexible ringbuffer class.
void stop()
Stop the automatic measurement state machine.
Definition: rm3100.cpp:619
void print_info()
Diagnostics - print some basic information about the driver.
Definition: rm3100.cpp:495
mag scaling factors; Vout = (Vin * Vscale) + Voffset
Definition: drv_mag.h:56
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
Vector rotation library.
High-resolution timer with callouts and timekeeping.
Generally used magic defines.
Abstract class for any character device.
Definition: CDev.hpp:60
Header common to all counters.
Definition: perf_counter.cpp:65
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
Definition: rm3100.h:120
int set_default_register_values()
Configures the device with default register values.
Definition: rm3100.cpp:577
Definition: video_device.h:50
Performance measuring tools.