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