WPILibC++ 2024.1.1-beta-4
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 enum Port { kOnboard = 0, kMXP };
26
27 /**
28 * Constructor.
29 *
30 * @param port The I2C port to which the device is connected.
31 * @param deviceAddress The address of the device on the I2C bus.
32 */
33 I2C(Port port, int deviceAddress);
34
36
37 I2C(I2C&&) = default;
38 I2C& operator=(I2C&&) = default;
39
40 Port GetPort() const;
41 int GetDeviceAddress() const;
42
43 /**
44 * Generic transaction.
45 *
46 * This is a lower-level interface to the I2C hardware giving you more control
47 * over each transaction. If you intend to write multiple bytes in the same
48 * transaction and do not plan to receive anything back, use writeBulk()
49 * instead. Calling this with a receiveSize of 0 will result in an error.
50 *
51 * @param dataToSend Buffer of data to send as part of the transaction.
52 * @param sendSize Number of bytes to send as part of the transaction.
53 * @param dataReceived Buffer to read data into.
54 * @param receiveSize Number of bytes to read from the device.
55 * @return Transfer Aborted... false for success, true for aborted.
56 */
57 bool Transaction(uint8_t* dataToSend, int sendSize, uint8_t* dataReceived,
58 int receiveSize);
59
60 /**
61 * Attempt to address a device on the I2C bus.
62 *
63 * This allows you to figure out if there is a device on the I2C bus that
64 * responds to the address specified in the constructor.
65 *
66 * @return Transfer Aborted... false for success, true for aborted.
67 */
69
70 /**
71 * Execute a write transaction with the device.
72 *
73 * Write a single byte to a register on a device and wait until the
74 * transaction is complete.
75 *
76 * @param registerAddress The address of the register on the device to be
77 * written.
78 * @param data The byte to write to the register on the device.
79 * @return Transfer Aborted... false for success, true for aborted.
80 */
81 bool Write(int registerAddress, uint8_t data);
82
83 /**
84 * Execute a bulk write transaction with the device.
85 *
86 * Write multiple bytes to a device and wait until the
87 * transaction is complete.
88 *
89 * @param data The data to write to the register on the device.
90 * @param count The number of bytes to be written.
91 * @return Transfer Aborted... false for success, true for aborted.
92 */
93 bool WriteBulk(uint8_t* data, int count);
94
95 /**
96 * Execute a read transaction with the device.
97 *
98 * Read bytes from a device.
99 * Most I2C devices will auto-increment the register pointer internally
100 * allowing you to read consecutive registers on a device in a single
101 * transaction.
102 *
103 * @param registerAddress The register to read first in the transaction.
104 * @param count The number of bytes to read in the transaction.
105 * @param data A pointer to the array of bytes to store the data
106 * read from the device.
107 * @return Transfer Aborted... false for success, true for aborted.
108 */
109 bool Read(int registerAddress, int count, uint8_t* data);
110
111 /**
112 * Execute a read only transaction with the device.
113 *
114 * Read bytes from a device. This method does not write any data to prompt the
115 * device.
116 *
117 * @param buffer A pointer to the array of bytes to store the data read from
118 * the device.
119 * @param count The number of bytes to read in the transaction.
120 * @return Transfer Aborted... false for success, true for aborted.
121 */
122 bool ReadOnly(int count, uint8_t* buffer);
123
124 /**
125 * Verify that a device's registers contain expected values.
126 *
127 * Most devices will have a set of registers that contain a known value that
128 * can be used to identify them. This allows an I2C device driver to easily
129 * verify that the device contains the expected value.
130 *
131 * @pre The device must support and be configured to use register
132 * auto-increment.
133 *
134 * @param registerAddress The base register to start reading from the device.
135 * @param count The size of the field to be verified.
136 * @param expected A buffer containing the values expected from the
137 * device.
138 */
139 bool VerifySensor(int registerAddress, int count, const uint8_t* expected);
140
141 private:
142 hal::I2CPort m_port;
143 int m_deviceAddress;
144};
145
146} // 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
Port GetPort() const
bool AddressOnly()
Attempt to address a device on the I2C bus.
Port
Definition: I2C.h:25
@ kOnboard
Definition: I2C.h:25
@ kMXP
Definition: I2C.h:25
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