blob: 4b18f73e71653df481f515a1c0ef50c0369d0b45 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
3/* 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
12#include <hal/HAL.h>
13#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);
23 // wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
24
25 HAL_Report(HALUsageReporting::kResourceType_I2C, deviceAddress);
26}
27
28I2C::~I2C() { HAL_CloseI2C(m_port); }
29
30I2C::I2C(I2C&& rhs)
31 : ErrorBase(std::move(rhs)),
32 m_deviceAddress(std::move(rhs.m_deviceAddress)) {
33 std::swap(m_port, rhs.m_port);
34}
35
36I2C& I2C::operator=(I2C&& rhs) {
37 ErrorBase::operator=(std::move(rhs));
38
39 std::swap(m_port, rhs.m_port);
40 m_deviceAddress = std::move(rhs.m_deviceAddress);
41
42 return *this;
43}
44
45bool I2C::Transaction(uint8_t* dataToSend, int sendSize, uint8_t* dataReceived,
46 int receiveSize) {
47 int32_t status = 0;
48 status = HAL_TransactionI2C(m_port, m_deviceAddress, dataToSend, sendSize,
49 dataReceived, receiveSize);
50 // wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
51 return status < 0;
52}
53
54bool I2C::AddressOnly() { return Transaction(nullptr, 0, nullptr, 0); }
55
56bool I2C::Write(int registerAddress, uint8_t data) {
57 uint8_t buffer[2];
58 buffer[0] = registerAddress;
59 buffer[1] = data;
60 int32_t status = 0;
61 status = HAL_WriteI2C(m_port, m_deviceAddress, buffer, sizeof(buffer));
62 return status < 0;
63}
64
65bool I2C::WriteBulk(uint8_t* data, int count) {
66 int32_t status = 0;
67 status = HAL_WriteI2C(m_port, m_deviceAddress, data, count);
68 return status < 0;
69}
70
71bool I2C::Read(int registerAddress, int count, uint8_t* buffer) {
72 if (count < 1) {
73 wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
74 return true;
75 }
76 if (buffer == nullptr) {
77 wpi_setWPIErrorWithContext(NullParameter, "buffer");
78 return true;
79 }
80 uint8_t regAddr = registerAddress;
81 return Transaction(&regAddr, sizeof(regAddr), buffer, count);
82}
83
84bool I2C::ReadOnly(int count, uint8_t* buffer) {
85 if (count < 1) {
86 wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
87 return true;
88 }
89 if (buffer == nullptr) {
90 wpi_setWPIErrorWithContext(NullParameter, "buffer");
91 return true;
92 }
93 return HAL_ReadI2C(m_port, m_deviceAddress, buffer, count) < 0;
94}
95
96bool I2C::VerifySensor(int registerAddress, int count,
97 const uint8_t* expected) {
98 // TODO: Make use of all 7 read bytes
99 uint8_t deviceData[4];
100 for (int i = 0, curRegisterAddress = registerAddress; i < count;
101 i += 4, curRegisterAddress += 4) {
102 int toRead = count - i < 4 ? count - i : 4;
103 // Read the chunk of data. Return false if the sensor does not respond.
104 if (Read(curRegisterAddress, toRead, deviceData)) return false;
105
106 for (int j = 0; j < toRead; j++) {
107 if (deviceData[j] != expected[i + j]) return false;
108 }
109 }
110 return true;
111}