Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 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. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/I2C.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <hal/FRCUsageReporting.h> |
| 10 | #include <hal/I2C.h> |
| 11 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 12 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | |
| 14 | using namespace frc; |
| 15 | |
| 16 | I2C::I2C(Port port, int deviceAddress) |
| 17 | : m_port(static_cast<HAL_I2CPort>(port)), m_deviceAddress(deviceAddress) { |
| 18 | int32_t status = 0; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 19 | |
| 20 | if (port == I2C::Port::kOnboard) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 21 | FRC_ReportError(warn::Warning, |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 22 | "Onboard I2C port is subject to system lockups. See Known " |
| 23 | "Issues page for " |
| 24 | "details"); |
| 25 | } |
| 26 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | HAL_InitializeI2C(m_port, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 28 | FRC_CheckErrorStatus(status, "Port {}", static_cast<int>(port)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | |
| 30 | HAL_Report(HALUsageReporting::kResourceType_I2C, deviceAddress); |
| 31 | } |
| 32 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 33 | I2C::~I2C() { |
| 34 | HAL_CloseI2C(m_port); |
| 35 | } |
| 36 | |
| 37 | I2C::Port I2C::GetPort() const { |
| 38 | return static_cast<Port>(static_cast<int>(m_port)); |
| 39 | } |
| 40 | |
| 41 | int I2C::GetDeviceAddress() const { |
| 42 | return m_deviceAddress; |
| 43 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 44 | |
| 45 | bool 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); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | return status < 0; |
| 51 | } |
| 52 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 53 | bool I2C::AddressOnly() { |
| 54 | return Transaction(nullptr, 0, nullptr, 0); |
| 55 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | |
| 57 | bool I2C::Write(int registerAddress, uint8_t data) { |
| 58 | uint8_t buffer[2]; |
| 59 | buffer[0] = registerAddress; |
| 60 | buffer[1] = data; |
| 61 | int32_t status = 0; |
| 62 | status = HAL_WriteI2C(m_port, m_deviceAddress, buffer, sizeof(buffer)); |
| 63 | return status < 0; |
| 64 | } |
| 65 | |
| 66 | bool I2C::WriteBulk(uint8_t* data, int count) { |
| 67 | int32_t status = 0; |
| 68 | status = HAL_WriteI2C(m_port, m_deviceAddress, data, count); |
| 69 | return status < 0; |
| 70 | } |
| 71 | |
| 72 | bool I2C::Read(int registerAddress, int count, uint8_t* buffer) { |
| 73 | if (count < 1) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 74 | throw FRC_MakeError(err::ParameterOutOfRange, "count {}", count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 75 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 76 | if (!buffer) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 77 | throw FRC_MakeError(err::NullParameter, "buffer"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | } |
| 79 | uint8_t regAddr = registerAddress; |
| 80 | return Transaction(®Addr, sizeof(regAddr), buffer, count); |
| 81 | } |
| 82 | |
| 83 | bool I2C::ReadOnly(int count, uint8_t* buffer) { |
| 84 | if (count < 1) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 85 | throw FRC_MakeError(err::ParameterOutOfRange, "count {}", count); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 86 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 87 | if (!buffer) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 88 | throw FRC_MakeError(err::NullParameter, "buffer"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | } |
| 90 | return HAL_ReadI2C(m_port, m_deviceAddress, buffer, count) < 0; |
| 91 | } |
| 92 | |
| 93 | bool I2C::VerifySensor(int registerAddress, int count, |
| 94 | const uint8_t* expected) { |
| 95 | // TODO: Make use of all 7 read bytes |
| 96 | uint8_t deviceData[4]; |
| 97 | for (int i = 0, curRegisterAddress = registerAddress; i < count; |
| 98 | i += 4, curRegisterAddress += 4) { |
| 99 | int toRead = count - i < 4 ? count - i : 4; |
| 100 | // Read the chunk of data. Return false if the sensor does not respond. |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 101 | if (Read(curRegisterAddress, toRead, deviceData)) { |
| 102 | return false; |
| 103 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 104 | |
| 105 | for (int j = 0; j < toRead; j++) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 106 | if (deviceData[j] != expected[i + j]) { |
| 107 | return false; |
| 108 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | return true; |
| 112 | } |