Files
allwpilib/wpilibc/wpilibC++Devices/src/I2C.cpp

213 lines
6.8 KiB
C++
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.
*/
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "I2C.h"
#include "HAL/HAL.hpp"
#include "HAL/Digital.hpp"
#include "WPIErrors.h"
/**
* Constructor.
*
* @param port The I2C port to which the device is connected.
* @param deviceAddress The address of the device on the I2C bus.
*/
I2C::I2C(Port port, uint8_t deviceAddress)
: m_port(port), m_deviceAddress(deviceAddress) {
int32_t status = 0;
i2CInitialize(m_port, &status);
// wpi_setErrorWithContext(status, getHALErrorMessage(status));
HALReport(HALUsageReporting::kResourceType_I2C, deviceAddress);
}
/**
* Destructor.
*/
I2C::~I2C() { i2CClose(m_port); }
/**
* Generic transaction.
*
* This is a lower-level interface to the I2C hardware giving you more control
* over each transaction.
*
* @param dataToSend Buffer of data to send as part of the transaction.
* @param sendSize Number of bytes to send as part of the transaction. [0..6]
* @param dataReceived Buffer to read data into.
* @param receiveSize Number of bytes to read from the device. [0..7]
* @return Transfer Aborted... false for success, true for aborted.
*/
bool I2C::Transaction(uint8_t *dataToSend, uint8_t sendSize,
uint8_t *dataReceived, uint8_t receiveSize) {
if (sendSize > 6) // Optional, provides better error message.
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "sendSize");
return true;
}
if (receiveSize > 7) // Optional, provides better error message.
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "receiveSize");
return true;
}
int32_t status = 0;
status = i2CTransaction(m_port, m_deviceAddress, dataToSend, sendSize,
dataReceived, receiveSize);
// wpi_setErrorWithContext(status, getHALErrorMessage(status));
return status < 0;
}
/**
* Attempt to address a device on the I2C bus.
*
* This allows you to figure out if there is a device on the I2C bus that
* responds to the address specified in the constructor.
*
* @return Transfer Aborted... false for success, true for aborted.
*/
bool I2C::AddressOnly() {
int32_t status = 0;
status = Transaction(nullptr, 0, nullptr, 0);
return status < 0;
}
/**
* Execute a write transaction with the device.
*
* Write a single byte to a register on a device and wait until the
* transaction is complete.
*
* @param registerAddress The address of the register on the device to be
* written.
* @param data The byte to write to the register on the device.
* @return Transfer Aborted... false for success, true for aborted.
*/
bool I2C::Write(uint8_t registerAddress, uint8_t data) {
uint8_t buffer[2];
buffer[0] = registerAddress;
buffer[1] = data;
int32_t status = 0;
status = i2CWrite(m_port, m_deviceAddress, buffer, sizeof(buffer));
return status < 0;
}
/**
* Execute a bulk write transaction with the device.
*
* Write multiple bytes to a device and wait until the
* transaction is complete.
*
* @param data The data to write to the register on the device.
* @param count The number of bytes to be written.
* @return Transfer Aborted... false for success, true for aborted.
*/
bool I2C::WriteBulk(uint8_t *data, uint8_t count) {
int32_t status = 0;
status = i2CWrite(m_port, m_deviceAddress, data, count);
return status < 0;
}
/**
* Execute a read transaction with the device.
*
* Read 1 to 7 bytes from a device.
* Most I2C devices will auto-increment the register pointer internally
* allowing you to read up to 7 consecutive registers on a device in a
* single transaction.
*
* @param registerAddress The register to read first in the transaction.
* @param count The number of bytes to read in the transaction. [1..7]
* @param buffer A pointer to the array of bytes to store the data read from the
* device.
* @return Transfer Aborted... false for success, true for aborted.
*/
bool I2C::Read(uint8_t registerAddress, uint8_t count, uint8_t *buffer) {
if (count < 1 || count > 7) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
return true;
}
if (buffer == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "buffer");
return true;
}
int32_t status = 0;
status =
Transaction(&registerAddress, sizeof(registerAddress), buffer, count);
return status < 0;
}
/**
* Execute a read only transaction with the device.
*
* Read 1 to 7 bytes from a device. This method does not write any data to
* prompt
* the device.
*
* @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. [1..7]
* @return Transfer Aborted... false for success, true for aborted.
*/
bool I2C::ReadOnly(uint8_t count, uint8_t *buffer) {
if (count < 1 || count > 7) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
return true;
}
if (buffer == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "buffer");
return true;
}
int32_t status = 0;
status = i2CRead(m_port, m_deviceAddress, buffer, count);
return status < 0;
}
/**
* Send a broadcast write to all devices on the I2C bus.
*
* This is not currently implemented!
*
* @param registerAddress The register to write on all devices on the bus.
* @param data The value to write to the devices.
*/
artf4154: Get rid of raw pointers in C++. This deals with the majority of the user-facing code in wpilibC++Devices and a substantial portion of it in wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests are untouched except where it is necessary to make them work with the rest of the libraries. There is still a lot to do in the following areas: -The HAL (which we may not want to touch at all). -The I2C, Serial, and SPI interfaces in wpilibC++Devices, which I haven't gotten around to doing yet. -Most wpilibC++Devices classes have void* pointers for interacting with the HAL. -InterruptableSensorBase passes a void *params for the interrupt handler. -I haven't converted all the const char* to std::strings. -There are plenty of other cases of raw pointers still existing. -This doesn't fall directly under raw pointer stuff, but move syntax and rvalue references could be introduced in many places. -I haven't touched vision code. -The Resource classes conflict (one is in the hal, the other in wpilibC++). Someone should figure out a more permanent fix (eg, just renaming them), then doing what I did (making a new namespace for one of them, essentially the same as renaming it). A few other things: -I created a NullDeleter class which is marked as deprecated. What this does is it can be passed as the deleter to a std::shared_ptr so that when you are converting raw pointers to shared_ptrs the shared_ptr doesn't do any deletion if someone else owns the raw pointer. This should only be used in making old raw pointer UIs. -I had to alter the build.gradle so that it did not emit errors when deprecated functions called deprecated functions. Unfortunately, gradle doesn't appear to be actually printing out gcc warnigns for some reason. The best way I have found to fix this is to patch the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff) so that a deprecated function calling a deprecated function is fine but a non-deprecated function calling a deprecated function will throw a warning (which we then elevate with -Werror). I believe that clang deals with this properly, although I have not tried it myself. Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
[[gnu::warning("I2C::Broadcast() is not implemented.")]]
void I2C::Broadcast(uint8_t registerAddress, uint8_t data) {}
/**
* Verify that a device's registers contain expected values.
*
* Most devices will have a set of registers that contain a known value that
* can be used to identify them. This allows an I2C device driver to easily
* verify that the device contains the expected value.
*
* @pre The device must support and be configured to use register
* auto-increment.
*
* @param registerAddress The base register to start reading from the device.
* @param count The size of the field to be verified.
* @param expected A buffer containing the values expected from the device.
*/
bool I2C::VerifySensor(uint8_t registerAddress, uint8_t count,
const uint8_t *expected) {
// TODO: Make use of all 7 read bytes
uint8_t deviceData[4];
for (uint8_t i = 0, curRegisterAddress = registerAddress; i < count;
i += 4, curRegisterAddress += 4) {
uint8_t toRead = count - i < 4 ? count - i : 4;
// Read the chunk of data. Return false if the sensor does not respond.
if (Read(curRegisterAddress, toRead, deviceData)) return false;
for (uint8_t j = 0; j < toRead; j++) {
if (deviceData[j] != expected[i + j]) return false;
}
}
return true;
}