blob: 79767b3bec159a433e4c02be378afa5bad2d4527 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7
8#include "I2C.h"
9#include "HAL/HAL.hpp"
10#include "HAL/Digital.hpp"
11#include "WPIErrors.h"
12
13/**
14 * Constructor.
15 *
16 * @param port The I2C port to which the device is connected.
17 * @param deviceAddress The address of the device on the I2C bus.
18 */
19I2C::I2C(Port port, uint8_t deviceAddress)
20 : m_port(port), m_deviceAddress(deviceAddress) {
21 int32_t status = 0;
22 i2CInitialize(m_port, &status);
23 // wpi_setErrorWithContext(status, getHALErrorMessage(status));
24
25 HALReport(HALUsageReporting::kResourceType_I2C, deviceAddress);
26}
27
28/**
29 * Destructor.
30 */
31I2C::~I2C() { i2CClose(m_port); }
32
33/**
34 * Generic transaction.
35 *
36 * This is a lower-level interface to the I2C hardware giving you more control
37 * over each transaction.
38 *
39 * @param dataToSend Buffer of data to send as part of the transaction.
40 * @param sendSize Number of bytes to send as part of the transaction.
41 * @param dataReceived Buffer to read data into.
42 * @param receiveSize Number of bytes to read from the device.
43 * @return Transfer Aborted... false for success, true for aborted.
44 */
45bool I2C::Transaction(uint8_t *dataToSend, uint8_t sendSize,
46 uint8_t *dataReceived, uint8_t receiveSize) {
47 int32_t status = 0;
48 status = i2CTransaction(m_port, m_deviceAddress, dataToSend, sendSize,
49 dataReceived, receiveSize);
50 // wpi_setErrorWithContext(status, getHALErrorMessage(status));
51 return status < 0;
52}
53
54/**
55 * Attempt to address a device on the I2C bus.
56 *
57 * This allows you to figure out if there is a device on the I2C bus that
58 * responds to the address specified in the constructor.
59 *
60 * @return Transfer Aborted... false for success, true for aborted.
61 */
62bool I2C::AddressOnly() {
63 int32_t status = 0;
64 status = Transaction(nullptr, 0, nullptr, 0);
65 return status < 0;
66}
67
68/**
69 * Execute a write transaction with the device.
70 *
71 * Write a single byte to a register on a device and wait until the
72 * transaction is complete.
73 *
74 * @param registerAddress The address of the register on the device to be
75 * written.
76 * @param data The byte to write to the register on the device.
77 * @return Transfer Aborted... false for success, true for aborted.
78 */
79bool I2C::Write(uint8_t registerAddress, uint8_t data) {
80 uint8_t buffer[2];
81 buffer[0] = registerAddress;
82 buffer[1] = data;
83 int32_t status = 0;
84 status = i2CWrite(m_port, m_deviceAddress, buffer, sizeof(buffer));
85 return status < 0;
86}
87
88/**
89 * Execute a bulk write transaction with the device.
90 *
91 * Write multiple bytes to a device and wait until the
92 * transaction is complete.
93 *
94 * @param data The data to write to the register on the device.
95 * @param count The number of bytes to be written.
96 * @return Transfer Aborted... false for success, true for aborted.
97 */
98bool I2C::WriteBulk(uint8_t *data, uint8_t count) {
99 int32_t status = 0;
100 status = i2CWrite(m_port, m_deviceAddress, data, count);
101 return status < 0;
102}
103
104/**
105 * Execute a read transaction with the device.
106 *
107 * Read bytes from a device.
108 * Most I2C devices will auto-increment the register pointer internally allowing
109 * you to read consecutive registers on a device in a single transaction.
110 *
111 * @param registerAddress The register to read first in the transaction.
112 * @param count The number of bytes to read in the transaction.
113 * @param buffer A pointer to the array of bytes to store the data read from the
114 * device.
115 * @return Transfer Aborted... false for success, true for aborted.
116 */
117bool I2C::Read(uint8_t registerAddress, uint8_t count, uint8_t *buffer) {
118 if (count < 1) {
119 wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
120 return true;
121 }
122 if (buffer == nullptr) {
123 wpi_setWPIErrorWithContext(NullParameter, "buffer");
124 return true;
125 }
126 int32_t status = 0;
127 status =
128 Transaction(&registerAddress, sizeof(registerAddress), buffer, count);
129 return status < 0;
130}
131
132/**
133 * Execute a read only transaction with the device.
134 *
135 * Read bytes from a device. This method does not write any data to prompt the
136 * device.
137 *
138 * @param buffer
139 * A pointer to the array of bytes to store the data read from
140 * the device.
141 * @param count
142 The number of bytes to read in the transaction.
143 * @return Transfer Aborted... false for success, true for aborted.
144 */
145bool I2C::ReadOnly(uint8_t count, uint8_t *buffer) {
146 if (count < 1) {
147 wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
148 return true;
149 }
150 if (buffer == nullptr) {
151 wpi_setWPIErrorWithContext(NullParameter, "buffer");
152 return true;
153 }
154 int32_t status = 0;
155 status = i2CRead(m_port, m_deviceAddress, buffer, count);
156 return status < 0;
157}
158
159/**
160 * Send a broadcast write to all devices on the I2C bus.
161 *
162 * This is not currently implemented!
163 *
164 * @param registerAddress The register to write on all devices on the bus.
165 * @param data The value to write to the devices.
166 */
167[[gnu::warning("I2C::Broadcast() is not implemented.")]]
168void I2C::Broadcast(uint8_t registerAddress, uint8_t data) {}
169
170/**
171 * Verify that a device's registers contain expected values.
172 *
173 * Most devices will have a set of registers that contain a known value that
174 * can be used to identify them. This allows an I2C device driver to easily
175 * verify that the device contains the expected value.
176 *
177 * @pre The device must support and be configured to use register
178 * auto-increment.
179 *
180 * @param registerAddress The base register to start reading from the device.
181 * @param count The size of the field to be verified.
182 * @param expected A buffer containing the values expected from the device.
183 */
184bool I2C::VerifySensor(uint8_t registerAddress, uint8_t count,
185 const uint8_t *expected) {
186 // TODO: Make use of all 7 read bytes
187 uint8_t deviceData[4];
188 for (uint8_t i = 0, curRegisterAddress = registerAddress; i < count;
189 i += 4, curRegisterAddress += 4) {
190 uint8_t toRead = count - i < 4 ? count - i : 4;
191 // Read the chunk of data. Return false if the sensor does not respond.
192 if (Read(curRegisterAddress, toRead, deviceData)) return false;
193
194 for (uint8_t j = 0; j < toRead; j++) {
195 if (deviceData[j] != expected[i + j]) return false;
196 }
197 }
198 return true;
199}