2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2008-2017 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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-12-21 21:55:31 -08:00
|
|
|
#include "HAL/I2C.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "I2C.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2016-05-22 21:41:22 -07:00
|
|
|
#include "HAL/HAL.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Constructor.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param port The I2C port to which the device is connected.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param deviceAddress The address of the device on the I2C bus.
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
2016-07-09 01:12:37 -07:00
|
|
|
I2C::~I2C() { HAL_CloseI2C(m_port); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generic transaction.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* This is a lower-level interface to the I2C hardware giving you more control
|
|
|
|
|
* over each transaction.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param dataToSend Buffer of data to send as part of the transaction.
|
|
|
|
|
* @param sendSize Number of bytes to send as part of the transaction.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param dataReceived Buffer to read data into.
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param receiveSize Number of bytes to read from the device.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Transfer Aborted... false for success, true for aborted.
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempt to address a device on the I2C bus.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This allows you to figure out if there is a device on the I2C bus that
|
|
|
|
|
* responds to the address specified in the constructor.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Transfer Aborted... false for success, true for aborted.
|
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a write transaction with the device.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* Write a single byte to a register on a device and wait until the
|
|
|
|
|
* transaction is complete.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param registerAddress The address of the register on the device to be
|
2016-05-20 17:30:37 -07:00
|
|
|
* written.
|
|
|
|
|
* @param data The byte to write to the register on the device.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Transfer Aborted... false for success, true for aborted.
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a bulk write transaction with the device.
|
|
|
|
|
*
|
|
|
|
|
* Write multiple bytes to a device and wait until the
|
|
|
|
|
* transaction is complete.
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param data The data to write to the register on the device.
|
2014-06-18 15:38:02 -04:00
|
|
|
* @param count The number of bytes to be written.
|
|
|
|
|
* @return Transfer Aborted... false for success, true for aborted.
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a read transaction with the device.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2015-07-09 15:36:02 -04:00
|
|
|
* Read bytes from a device.
|
|
|
|
|
* Most I2C devices will auto-increment the register pointer internally allowing
|
2016-05-20 17:30:37 -07:00
|
|
|
* you to read consecutive registers on a device in a single transaction.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param registerAddress The register to read first in the transaction.
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param count The number of bytes to read in the transaction.
|
|
|
|
|
* @param buffer A pointer to the array of bytes to store the data
|
|
|
|
|
* read from the device.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Transfer Aborted... false for success, true for aborted.
|
|
|
|
|
*/
|
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
|
|
|
|
2014-06-18 15:38:02 -04:00
|
|
|
/**
|
|
|
|
|
* Execute a read only transaction with the device.
|
|
|
|
|
*
|
2015-07-09 15:36:02 -04:00
|
|
|
* Read bytes from a device. This method does not write any data to prompt the
|
|
|
|
|
* device.
|
2014-06-18 15:38:02 -04:00
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param buffer A pointer to the array of bytes to store the data read from
|
|
|
|
|
* the device.
|
|
|
|
|
* @param count The number of bytes to read in the transaction.
|
2014-06-18 15:38:02 -04:00
|
|
|
* @return Transfer Aborted... false for success, true for aborted.
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a broadcast write to all devices on the I2C bus.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This is not currently implemented!
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param registerAddress The register to write on all devices on the bus.
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param data The value to write to the devices.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-12-23 11:20:13 -08:00
|
|
|
// [[gnu::warning("I2C::Broadcast() is not implemented.")]] void I2C::Broadcast(
|
|
|
|
|
// int registerAddress, uint8_t data) {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify that a device's registers contain expected values.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* Most devices will have a set of registers that contain a known value that
|
2016-05-20 17:30:37 -07:00
|
|
|
* can be used to identify them. This allows an I2C device driver to easily
|
|
|
|
|
* verify that the device contains the expected value.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @pre The device must support and be configured to use register
|
|
|
|
|
* auto-increment.
|
2014-07-21 16:32:36 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param registerAddress The base register to start reading from the device.
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param count The size of the field to be verified.
|
|
|
|
|
* @param expected A buffer containing the values expected from the
|
|
|
|
|
* device.
|
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
|
|
|
}
|