WPILibC++ 2024.3.2
I2C.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <hal/I2CTypes.h>
10
11namespace frc {
12
13/**
14 * I2C bus interface class.
15 *
16 * This class is intended to be used by sensor (and other I2C device) drivers.
17 * It probably should not be used directly.
18 *
19 * The Onboard I2C port is subject to system lockups. See <a
20 * href="https://docs.wpilib.org/en/stable/docs/yearly-overview/known-issues.html#onboard-i2c-causing-system-lockups">
21 * WPILib Known Issues</a> page for details.
22 */
23class I2C {
24 public:
25 /**
26 * I2C connection ports.
27 */
28 enum Port {
29 /// Onboard I2C port.
31 /// MXP (roboRIO MXP) I2C port.
32 kMXP
33 };
34
35 /**
36 * Constructor.
37 *
38 * @param port The I2C port to which the device is connected.
39 * @param deviceAddress The address of the device on the I2C bus.
40 */
41 I2C(Port port, int deviceAddress);
42
44
45 I2C(I2C&&) = default;
46 I2C& operator=(I2C&&) = default;
47
48 /**
49 * Returns I2C port.
50 *
51 * @return I2C port.
52 */
53 Port GetPort() const;
54
55 /**
56 * Returns I2C device address.
57 *
58 * @return I2C device address.
59 */
60 int GetDeviceAddress() const;
61
62 /**
63 * Generic transaction.
64 *
65 * This is a lower-level interface to the I2C hardware giving you more control
66 * over each transaction. If you intend to write multiple bytes in the same
67 * transaction and do not plan to receive anything back, use writeBulk()
68 * instead. Calling this with a receiveSize of 0 will result in an error.
69 *
70 * @param dataToSend Buffer of data to send as part of the transaction.
71 * @param sendSize Number of bytes to send as part of the transaction.
72 * @param dataReceived Buffer to read data into.
73 * @param receiveSize Number of bytes to read from the device.
74 * @return Transfer Aborted... false for success, true for aborted.
75 */
76 bool Transaction(uint8_t* dataToSend, int sendSize, uint8_t* dataReceived,
77 int receiveSize);
78
79 /**
80 * Attempt to address a device on the I2C bus.
81 *
82 * This allows you to figure out if there is a device on the I2C bus that
83 * responds to the address specified in the constructor.
84 *
85 * @return Transfer Aborted... false for success, true for aborted.
86 */
88
89 /**
90 * Execute a write transaction with the device.
91 *
92 * Write a single byte to a register on a device and wait until the
93 * transaction is complete.
94 *
95 * @param registerAddress The address of the register on the device to be
96 * written.
97 * @param data The byte to write to the register on the device.
98 * @return Transfer Aborted... false for success, true for aborted.
99 */
100 bool Write(int registerAddress, uint8_t data);
101
102 /**
103 * Execute a bulk write transaction with the device.
104 *
105 * Write multiple bytes to a device and wait until the
106 * transaction is complete.
107 *
108 * @param data The data to write to the register on the device.
109 * @param count The number of bytes to be written.
110 * @return Transfer Aborted... false for success, true for aborted.
111 */
112 bool WriteBulk(uint8_t* data, int count);
113
114 /**
115 * Execute a read transaction with the device.
116 *
117 * Read bytes from a device.
118 * Most I2C devices will auto-increment the register pointer internally
119 * allowing you to read consecutive registers on a device in a single
120 * transaction.
121 *
122 * @param registerAddress The register to read first in the transaction.
123 * @param count The number of bytes to read in the transaction.
124 * @param data A pointer to the array of bytes to store the data
125 * read from the device.
126 * @return Transfer Aborted... false for success, true for aborted.
127 */
128 bool Read(int registerAddress, int count, uint8_t* data);
129
130 /**
131 * Execute a read only transaction with the device.
132 *
133 * Read bytes from a device. This method does not write any data to prompt the
134 * device.
135 *
136 * @param buffer A pointer to the array of bytes to store the data read from
137 * the device.
138 * @param count The number of bytes to read in the transaction.
139 * @return Transfer Aborted... false for success, true for aborted.
140 */
141 bool ReadOnly(int count, uint8_t* buffer);
142
143 /**
144 * Verify that a device's registers contain expected values.
145 *
146 * Most devices will have a set of registers that contain a known value that
147 * can be used to identify them. This allows an I2C device driver to easily
148 * verify that the device contains the expected value.
149 *
150 * @pre The device must support and be configured to use register
151 * auto-increment.
152 *
153 * @param registerAddress The base register to start reading from the device.
154 * @param count The size of the field to be verified.
155 * @param expected A buffer containing the values expected from the
156 * device.
157 */
158 bool VerifySensor(int registerAddress, int count, const uint8_t* expected);
159
160 private:
161 hal::I2CPort m_port;
162 int m_deviceAddress;
163};
164
165} // namespace frc
I2C bus interface class.
Definition: I2C.h:23
I2C(I2C &&)=default
I2C(Port port, int deviceAddress)
Constructor.
bool Read(int registerAddress, int count, uint8_t *data)
Execute a read transaction with the device.
bool ReadOnly(int count, uint8_t *buffer)
Execute a read only transaction with the device.
bool Transaction(uint8_t *dataToSend, int sendSize, uint8_t *dataReceived, int receiveSize)
Generic transaction.
bool Write(int registerAddress, uint8_t data)
Execute a write transaction with the device.
I2C & operator=(I2C &&)=default
bool WriteBulk(uint8_t *data, int count)
Execute a bulk write transaction with the device.
bool VerifySensor(int registerAddress, int count, const uint8_t *expected)
Verify that a device's registers contain expected values.
int GetDeviceAddress() const
Returns I2C device address.
Port GetPort() const
Returns I2C port.
bool AddressOnly()
Attempt to address a device on the I2C bus.
Port
I2C connection ports.
Definition: I2C.h:28
@ kOnboard
Onboard I2C port.
Definition: I2C.h:30
@ kMXP
MXP (roboRIO MXP) I2C port.
Definition: I2C.h:32
A move-only C++ wrapper around a HAL handle.
Definition: Types.h:96
constexpr auto count() -> size_t
Definition: core.h:1203
Definition: AprilTagPoseEstimator.h:15