PX4 Firmware
PX4 Autopilot Software http://px4.io
I2C.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012 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 i2c.h
36  *
37  * Base class for devices connected via I2C.
38  */
39 
40 #ifndef _DEVICE_I2C_H
41 #define _DEVICE_I2C_H
42 
43 #include "../CDev.hpp"
44 
45 #include <px4_platform_common/i2c.h>
46 
47 #ifdef __PX4_LINUX
48 #include <linux/i2c.h>
49 #include <linux/i2c-dev.h>
50 #endif
51 
52 namespace device __EXPORT
53 {
54 
55 /**
56  * Abstract class for character device on I2C
57  */
58 class __EXPORT I2C : public CDev
59 {
60 
61 public:
62 
63  // no copy, assignment, move, move assignment
64  I2C(const I2C &) = delete;
65  I2C &operator=(const I2C &) = delete;
66  I2C(I2C &&) = delete;
67  I2C &operator=(I2C &&) = delete;
68 
69  virtual int init() override;
70 
71 protected:
72  /**
73  * The number of times a read or write operation will be retried on
74  * error.
75  */
76  uint8_t _retries{0};
77 
78  /**
79  * @ Constructor
80  *
81  * @param name Driver name
82  * @param devname Device node name
83  * @param bus I2C bus on which the device lives
84  * @param address I2C bus address, or zero if set_address will be used
85  * @param frequency I2C bus frequency for the device (currently not used)
86  */
87  I2C(const char *name, const char *devname, const int bus, const uint16_t address, const uint32_t frequency);
88  virtual ~I2C();
89 
90  /**
91  * Check for the presence of the device on the bus.
92  */
93  virtual int probe() { return PX4_OK; }
94 
95  /**
96  * Perform an I2C transaction to the device.
97  *
98  * At least one of send_len and recv_len must be non-zero.
99  *
100  * @param send Pointer to bytes to send.
101  * @param send_len Number of bytes to send.
102  * @param recv Pointer to buffer for bytes received.
103  * @param recv_len Number of bytes to receive.
104  * @return OK if the transfer was successful, -errno
105  * otherwise.
106  */
107  int transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const unsigned recv_len);
108 
109  virtual bool external() const override { return px4_i2c_bus_external(_device_id.devid_s.bus); }
110 
111 private:
112  int _fd{-1};
113 
114 };
115 
116 } // namespace device
117 
118 #endif /* _DEVICE_I2C_H */
virtual bool external() const override
Definition: I2C.hpp:109
void * send(void *data)
Definition: I2C.hpp:51
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
virtual int probe()
Check for the presence of the device on the bus.
Definition: I2C.hpp:93
void init()
Activates/configures the hardware registers.
const char * name
Definition: tests_main.c:58