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 #if !defined(CONFIG_I2C)
48 # error I2C support requires CONFIG_I2C
49 #endif
50 
51 namespace device __EXPORT
52 {
53 
54 /**
55  * Abstract class for character device on I2C
56  */
57 class __EXPORT I2C : public CDev
58 {
59 
60 public:
61 
62  // no copy, assignment, move, move assignment
63  I2C(const I2C &) = delete;
64  I2C &operator=(const I2C &) = delete;
65  I2C(I2C &&) = delete;
66  I2C &operator=(I2C &&) = delete;
67 
68  virtual int init() override;
69 
70  static int set_bus_clock(unsigned bus, unsigned clock_hz);
71 
72  static unsigned int _bus_clocks[BOARD_NUMBER_I2C_BUSES];
73 
74 protected:
75  /**
76  * The number of times a read or write operation will be retried on
77  * error.
78  */
79  uint8_t _retries{0};
80 
81  /**
82  * @ Constructor
83  *
84  * @param name Driver name
85  * @param devname Device node name
86  * @param bus I2C bus on which the device lives
87  * @param address I2C bus address, or zero if set_address will be used
88  * @param frequency I2C bus frequency for the device (currently not used)
89  */
90  I2C(const char *name, const char *devname, const int bus, const uint16_t address, const uint32_t frequency);
91  virtual ~I2C();
92 
93  /**
94  * Check for the presence of the device on the bus.
95  */
96  virtual int probe() { return PX4_OK; }
97 
98  /**
99  * Perform an I2C transaction to the device.
100  *
101  * At least one of send_len and recv_len must be non-zero.
102  *
103  * @param send Pointer to bytes to send.
104  * @param send_len Number of bytes to send.
105  * @param recv Pointer to buffer for bytes received.
106  * @param recv_len Number of bytes to receive.
107  * @return OK if the transfer was successful, -errno
108  * otherwise.
109  */
110  int transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const unsigned recv_len);
111 
112  virtual bool external() const override { return px4_i2c_bus_external(_device_id.devid_s.bus); }
113 
114 private:
115  uint32_t _frequency{0};
116  px4_i2c_dev_t *_dev{nullptr};
117 
118 };
119 
120 } // namespace device
121 
122 #endif /* _DEVICE_I2C_H */
Abstract class for character device on I2C.
Definition: I2C.hpp:57
virtual bool external() const override
Definition: I2C.hpp:112
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:96
void init()
Activates/configures the hardware registers.
const char * name
Definition: tests_main.c:58