blob: 21d92f243b231622fb9e2b9ad28d43a8452437e0 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc/I2C.h"
9
10#include <utility>
11
James Kuszmaul4b81d302019-12-14 20:53:14 -080012#include <hal/FRCUsageReporting.h>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080013#include <hal/I2C.h>
14
15#include "frc/WPIErrors.h"
16
17using namespace frc;
18
19I2C::I2C(Port port, int deviceAddress)
20 : m_port(static_cast<HAL_I2CPort>(port)), m_deviceAddress(deviceAddress) {
21 int32_t status = 0;
22 HAL_InitializeI2C(m_port, &status);
James Kuszmaul4b81d302019-12-14 20:53:14 -080023 // wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080024
25 HAL_Report(HALUsageReporting::kResourceType_I2C, deviceAddress);
26}
27
28I2C::~I2C() { HAL_CloseI2C(m_port); }
29
Brian Silverman41cdd3e2019-01-19 19:48:58 -080030bool I2C::Transaction(uint8_t* dataToSend, int sendSize, uint8_t* dataReceived,
31 int receiveSize) {
32 int32_t status = 0;
33 status = HAL_TransactionI2C(m_port, m_deviceAddress, dataToSend, sendSize,
34 dataReceived, receiveSize);
James Kuszmaul4b81d302019-12-14 20:53:14 -080035 // wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080036 return status < 0;
37}
38
39bool I2C::AddressOnly() { return Transaction(nullptr, 0, nullptr, 0); }
40
41bool I2C::Write(int registerAddress, uint8_t data) {
42 uint8_t buffer[2];
43 buffer[0] = registerAddress;
44 buffer[1] = data;
45 int32_t status = 0;
46 status = HAL_WriteI2C(m_port, m_deviceAddress, buffer, sizeof(buffer));
47 return status < 0;
48}
49
50bool I2C::WriteBulk(uint8_t* data, int count) {
51 int32_t status = 0;
52 status = HAL_WriteI2C(m_port, m_deviceAddress, data, count);
53 return status < 0;
54}
55
56bool I2C::Read(int registerAddress, int count, uint8_t* buffer) {
57 if (count < 1) {
58 wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
59 return true;
60 }
61 if (buffer == nullptr) {
62 wpi_setWPIErrorWithContext(NullParameter, "buffer");
63 return true;
64 }
65 uint8_t regAddr = registerAddress;
66 return Transaction(&regAddr, sizeof(regAddr), buffer, count);
67}
68
69bool I2C::ReadOnly(int count, uint8_t* buffer) {
70 if (count < 1) {
71 wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
72 return true;
73 }
74 if (buffer == nullptr) {
75 wpi_setWPIErrorWithContext(NullParameter, "buffer");
76 return true;
77 }
78 return HAL_ReadI2C(m_port, m_deviceAddress, buffer, count) < 0;
79}
80
81bool I2C::VerifySensor(int registerAddress, int count,
82 const uint8_t* expected) {
83 // TODO: Make use of all 7 read bytes
84 uint8_t deviceData[4];
85 for (int i = 0, curRegisterAddress = registerAddress; i < count;
86 i += 4, curRegisterAddress += 4) {
87 int toRead = count - i < 4 ? count - i : 4;
88 // Read the chunk of data. Return false if the sensor does not respond.
89 if (Read(curRegisterAddress, toRead, deviceData)) return false;
90
91 for (int j = 0; j < toRead; j++) {
92 if (deviceData[j] != expected[i + j]) return false;
93 }
94 }
95 return true;
96}