2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/I2C.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/I2C.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
I2C::I2C(Port port, int deviceAddress)
|
2017-05-09 12:12:46 -07:00
|
|
|
: m_port(static_cast<HAL_I2CPort>(port)), m_deviceAddress(deviceAddress) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 01:12:37 -07:00
|
|
|
HAL_InitializeI2C(m_port, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", port);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_I2C, deviceAddress);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
I2C::~I2C() {
|
|
|
|
|
HAL_CloseI2C(m_port);
|
|
|
|
|
}
|
2021-08-21 02:19:59 -04:00
|
|
|
|
|
|
|
|
I2C::Port I2C::GetPort() const {
|
|
|
|
|
return static_cast<Port>(static_cast<int>(m_port));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int I2C::GetDeviceAddress() const {
|
|
|
|
|
return m_deviceAddress;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
bool I2C::Transaction(uint8_t* dataToSend, int sendSize, uint8_t* dataReceived,
|
|
|
|
|
int receiveSize) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 01:12:37 -07:00
|
|
|
status = HAL_TransactionI2C(m_port, m_deviceAddress, dataToSend, sendSize,
|
2016-07-09 00:24:26 -07:00
|
|
|
dataReceived, receiveSize);
|
2017-02-17 00:05:54 -08:00
|
|
|
return status < 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool I2C::AddressOnly() {
|
|
|
|
|
return Transaction(nullptr, 0, nullptr, 0);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
bool I2C::Write(int registerAddress, uint8_t data) {
|
2015-06-25 15:07:55 -04:00
|
|
|
uint8_t buffer[2];
|
|
|
|
|
buffer[0] = registerAddress;
|
|
|
|
|
buffer[1] = data;
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 01:12:37 -07:00
|
|
|
status = HAL_WriteI2C(m_port, m_deviceAddress, buffer, sizeof(buffer));
|
2017-02-17 00:05:54 -08:00
|
|
|
return status < 0;
|
2014-06-18 15:38:02 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
bool I2C::WriteBulk(uint8_t* data, int count) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 01:12:37 -07:00
|
|
|
status = HAL_WriteI2C(m_port, m_deviceAddress, data, count);
|
2017-02-17 00:05:54 -08:00
|
|
|
return status < 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
bool I2C::Read(int registerAddress, int count, uint8_t* buffer) {
|
2015-07-09 15:36:02 -04:00
|
|
|
if (count < 1) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::ParameterOutOfRange, "count {}", count);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!buffer) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "buffer");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-02-10 15:21:59 -08:00
|
|
|
uint8_t regAddr = registerAddress;
|
|
|
|
|
return Transaction(®Addr, sizeof(regAddr), buffer, count);
|
2014-06-18 15:38:02 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
bool I2C::ReadOnly(int count, uint8_t* buffer) {
|
2015-07-09 15:36:02 -04:00
|
|
|
if (count < 1) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::ParameterOutOfRange, "count {}", count);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!buffer) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "buffer");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-02-17 00:05:54 -08:00
|
|
|
return HAL_ReadI2C(m_port, m_deviceAddress, buffer, count) < 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
bool I2C::VerifySensor(int registerAddress, int count,
|
2016-05-20 17:30:37 -07:00
|
|
|
const uint8_t* expected) {
|
2015-06-25 15:07:55 -04:00
|
|
|
// TODO: Make use of all 7 read bytes
|
|
|
|
|
uint8_t deviceData[4];
|
2016-09-06 00:01:45 -07:00
|
|
|
for (int i = 0, curRegisterAddress = registerAddress; i < count;
|
2015-06-25 15:07:55 -04:00
|
|
|
i += 4, curRegisterAddress += 4) {
|
2016-09-06 00:01:45 -07:00
|
|
|
int toRead = count - i < 4 ? count - i : 4;
|
2015-06-25 15:07:55 -04:00
|
|
|
// Read the chunk of data. Return false if the sensor does not respond.
|
2020-12-28 12:58:06 -08:00
|
|
|
if (Read(curRegisterAddress, toRead, deviceData)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
for (int j = 0; j < toRead; j++) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (deviceData[j] != expected[i + j]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|