PX4 Firmware
PX4 Autopilot Software http://px4.io
lis3mdl_i2c.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-2015 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 lis3mdl_i2c.cpp
36  *
37  * I2C interface for LIS3MDL
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 "lis3mdl.h"
59 
60 #if defined(PX4_I2C_BUS_ONBOARD) || defined(PX4_I2C_BUS_EXPANSION)
61 
62 #define LIS3MDLL_ADDRESS 0x1e
63 
64 class LIS3MDL_I2C : public device::I2C
65 {
66 public:
67  LIS3MDL_I2C(int bus);
68  virtual ~LIS3MDL_I2C() = default;
69 
70  virtual int ioctl(unsigned operation, unsigned &arg);
71  virtual int read(unsigned address, void *data, unsigned count);
72  virtual int write(unsigned address, void *data, unsigned count);
73 
74 protected:
75  virtual int probe();
76 
77 };
78 
80 LIS3MDL_I2C_interface(int bus);
81 
83 LIS3MDL_I2C_interface(int bus)
84 {
85  return new LIS3MDL_I2C(bus);
86 }
87 
88 LIS3MDL_I2C::LIS3MDL_I2C(int bus) :
89  I2C("LIS3MDL_I2C", nullptr, bus, LIS3MDLL_ADDRESS, 400000)
90 {
91  _device_id.devid_s.devtype = DRV_MAG_DEVTYPE_LIS3MDL;
92 }
93 
94 int
95 LIS3MDL_I2C::ioctl(unsigned operation, unsigned &arg)
96 {
97  switch (operation) {
98 
99  case MAGIOCGEXTERNAL:
100  return external();
101 
102  case DEVIOCGDEVICEID:
103  return CDev::ioctl(nullptr, operation, arg);
104 
105  default:
106  return -EINVAL;
107  }
108 }
109 
110 int
112 {
113  uint8_t data = 0;
114 
115  _retries = 10;
116 
117  if (read(ADDR_WHO_AM_I, &data, 1)) {
118  DEVICE_DEBUG("read_reg fail");
119  return -EIO;
120  }
121 
122  _retries = 2;
123 
124  if (data != ID_WHO_AM_I) {
125  DEVICE_DEBUG("LIS3MDL bad ID: %02x", data);
126  return -EIO;
127  }
128 
129  return OK;
130 }
131 
132 int
133 LIS3MDL_I2C::read(unsigned address, void *data, unsigned count)
134 {
135  uint8_t cmd = address;
136  return transfer(&cmd, 1, (uint8_t *)data, count);
137 }
138 
139 int
140 LIS3MDL_I2C::write(unsigned address, void *data, unsigned count)
141 {
142  uint8_t buf[32];
143 
144  if (sizeof(buf) < (count + 1)) {
145  return -EIO;
146  }
147 
148  buf[0] = address;
149  memcpy(&buf[1], data, count);
150 
151  return transfer(&buf[0], count + 1, nullptr, 0);
152 }
153 
154 #endif /* PX4_I2C_OBDEV_LIS3MDL */
#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: lis3mdl.cpp:632
#define ADDR_WHO_AM_I
Definition: lps25h.cpp:52
#define DRV_MAG_DEVTYPE_LIS3MDL
Definition: drv_sensor.h:59
static void read(bootloader_app_shared_t *pshared)
Generic device / sensor interface.
#define ID_WHO_AM_I
Definition: lps25h.h:59
uint8_t * data
Definition: dataman.cpp:149
Shared defines for the LIS3MDL driver.
virtual bool external() const
Definition: Device.hpp:237
static void write(bootloader_app_shared_t *pshared)
Fundamental base class for all physical drivers (I2C, SPI).
Definition: Device.hpp:65
device::Device * LIS3MDL_I2C_interface(int bus)
#define OK
Definition: uavcan_main.cpp:71
#define DEVICE_DEBUG(FMT,...)
Definition: Device.hpp:52
Base class for devices connected via I2C.