2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-08-25 18:42:00 -07:00
|
|
|
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the 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>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
|
|
|
|
#include <hal/I2C.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/WPIErrors.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);
|
2016-07-09 00:24:26 -07:00
|
|
|
// wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
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
|
|
|
}
|
|
|
|
|
|
2016-07-09 01:12:37 -07:00
|
|
|
I2C::~I2C() { HAL_CloseI2C(m_port); }
|
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);
|
|
|
|
|
// wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2017-02-17 00:05:54 -08:00
|
|
|
return status < 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07: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) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-06-23 04:49:51 -07:00
|
|
|
if (buffer == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(NullParameter, "buffer");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
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) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-06-23 04:49:51 -07:00
|
|
|
if (buffer == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(NullParameter, "buffer");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
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.
|
|
|
|
|
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++) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (deviceData[j] != expected[i + j]) return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|