Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 12 | #include <hal/FRCUsageReporting.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 13 | #include <hal/I2C.h> |
| 14 | |
| 15 | #include "frc/WPIErrors.h" |
| 16 | |
| 17 | using namespace frc; |
| 18 | |
| 19 | I2C::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 Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 23 | // wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 24 | |
| 25 | HAL_Report(HALUsageReporting::kResourceType_I2C, deviceAddress); |
| 26 | } |
| 27 | |
| 28 | I2C::~I2C() { HAL_CloseI2C(m_port); } |
| 29 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 30 | bool 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 Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 35 | // wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 36 | return status < 0; |
| 37 | } |
| 38 | |
| 39 | bool I2C::AddressOnly() { return Transaction(nullptr, 0, nullptr, 0); } |
| 40 | |
| 41 | bool 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 | |
| 50 | bool 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 | |
| 56 | bool 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(®Addr, sizeof(regAddr), buffer, count); |
| 67 | } |
| 68 | |
| 69 | bool 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 | |
| 81 | bool 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 | } |