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