A fix contributed by James Ward to fix the restriction on the read/write sizes in I2C.

Change-Id: Iddc48dc55ff9cea37d8dcfaba5e63f0d14e4c7d0
This commit is contained in:
jmanning
2015-07-09 15:36:02 -04:00
committed by Brad Miller (WPI)
parent 3635299fde
commit c21c47f546
3 changed files with 38 additions and 76 deletions

View File

@@ -644,7 +644,6 @@ void remapDigitalSource(bool analogTrigger, uint32_t &pin, uint8_t &module) {
}
}
/**
* Set the source object that causes the counter to count up.
* Set the up counting DigitalSource.
@@ -1360,9 +1359,9 @@ void i2CInitialize(uint8_t port, int32_t *status) {
* 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 sendSize Number of bytes to send as part of the transaction.
* @param dataReceived Buffer to read data into.
* @param receiveSize Number of bytes to read from the device. [0..7]
* @param receiveSize Number of bytes to read from the device.
* @return Transfer Aborted... false for success, true for aborted.
*/
int32_t i2CTransaction(uint8_t port, uint8_t deviceAddress, uint8_t *dataToSend, uint8_t sendSize, uint8_t *dataReceived, uint8_t receiveSize)
@@ -1371,16 +1370,7 @@ int32_t i2CTransaction(uint8_t port, uint8_t deviceAddress, uint8_t *dataToSend,
//Set port out of range error here
return -1;
}
/*if (sendSize > 6) // Optional, provides better error message. TODO: Are these limits still right? Implement error. Check for null buffer
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "sendSize");
return true;
}
if (receiveSize > 7) // Optional, provides better error message.
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "receiveSize");
return true;
}*/
int32_t handle = port == 0 ? i2COnBoardHandle:i2CMXPHandle;
priority_recursive_mutex &lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex;
@@ -1406,11 +1396,7 @@ int32_t i2CWrite(uint8_t port, uint8_t deviceAddress, uint8_t* dataToSend, uint8
//Set port out of range error here
return -1;
}
/*if (sendSize > 6) // Optional, provides better error message. TODO: Are these limits still right? Implement error. Check for null buffer
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "sendSize");
return true;
}*/
int32_t handle = port == 0 ? i2COnBoardHandle:i2CMXPHandle;
priority_recursive_mutex &lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex;
{
@@ -1422,13 +1408,12 @@ int32_t i2CWrite(uint8_t port, uint8_t deviceAddress, uint8_t* dataToSend, uint8
/**
* 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.
* Read bytes from a device.
* Most I2C devices will auto-increment the register pointer internally allowing
* you to read 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 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.
* @return Transfer Aborted... false for success, true for aborted.
*/
@@ -1438,16 +1423,7 @@ int32_t i2CRead(uint8_t port, uint8_t deviceAddress, uint8_t *buffer, uint8_t co
//Set port out of range error here
return -1;
}
/* if (count < 1 || count > 7) Todo: Are these limits still right? Implement error
{
wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
return true;
}
if (buffer == NULL)
{
wpi_setWPIErrorWithContext(NullParameter, "buffer");
return true;
}*/
int32_t handle = port == 0 ? i2COnBoardHandle:i2CMXPHandle;
priority_recursive_mutex &lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex;
{

View File

@@ -37,24 +37,13 @@ I2C::~I2C() { i2CClose(m_port); }
* 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 sendSize Number of bytes to send as part of the transaction.
* @param dataReceived Buffer to read data into.
* @param receiveSize Number of bytes to read from the device. [0..7]
* @param receiveSize Number of bytes to read from the device.
* @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);
@@ -115,19 +104,18 @@ bool I2C::WriteBulk(uint8_t *data, uint8_t count) {
/**
* 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.
* Read bytes from a device.
* Most I2C devices will auto-increment the register pointer internally allowing
* you to read 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 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.
* @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) {
if (count < 1) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
return true;
}
@@ -144,19 +132,18 @@ bool I2C::Read(uint8_t registerAddress, uint8_t count, uint8_t *buffer) {
/**
* 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.
* Read 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]
The number of bytes to read in the transaction.
* @return Transfer Aborted... false for success, true for aborted.
*/
bool I2C::ReadOnly(uint8_t count, uint8_t *buffer) {
if (count < 1 || count > 7) {
if (count < 1) {
wpi_setWPIErrorWithContext(ParameterOutOfRange, "count");
return true;
}

View File

@@ -72,9 +72,9 @@ public class I2C extends SensorBase {
* 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 sendSize Number of bytes to send as part of the transaction.
* @param dataReceived Buffer to read data into.
* @param receiveSize Number of bytes to read from the device. [0..7]
* @param receiveSize Number of bytes to read from the device.
* @return Transfer Aborted... false for success, true for aborted.
*/
public synchronized boolean transaction(byte[] dataToSend, int sendSize, byte[] dataReceived,
@@ -88,15 +88,6 @@ public class I2C extends SensorBase {
aborted =
I2CJNI.i2CTransaction((byte) m_port.getValue(), (byte) m_deviceAddress, dataToSendBuffer,
(byte) sendSize, dataReceivedBuffer, (byte) receiveSize) != 0;
/*
* if (status.get() == HALUtil.PARAMETER_OUT_OF_RANGE) { if (sendSize > 6) {
* throw new BoundaryException(BoundaryException.getMessage( sendSize, 0,
* 6)); } else if (receiveSize > 7) { throw new
* BoundaryException(BoundaryException.getMessage( receiveSize, 0, 7)); }
* else { throw new RuntimeException(
* HALLibrary.PARAMETER_OUT_OF_RANGE_MESSAGE); } }
* HALUtil.checkStatus(status);
*/
if (receiveSize > 0 && dataReceived != null) {
dataReceivedBuffer.get(dataReceived);
}
@@ -156,18 +147,22 @@ public class I2C extends SensorBase {
/**
* 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
* Read bytes from a device. Most I2C devices will auto-increment the
* register pointer internally allowing you to read 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 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.
* @return Transfer Aborted... false for success, true for aborted.
*/
public boolean read(int registerAddress, int count, byte[] buffer) {
BoundaryException.assertWithinBounds(count, 1, 7);
if (count < 1) {
throw new BoundaryException("Value must be at least 1, " + count +
" given");
}
if (buffer == null) {
throw new NullPointerException("Null return buffer was given");
}
@@ -180,16 +175,20 @@ public class I2C extends SensorBase {
/**
* 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.
* Read 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]
* @param count The number of bytes to read in the transaction.
* @return Transfer Aborted... false for success, true for aborted.
*/
public boolean readOnly(byte[] buffer, int count) {
BoundaryException.assertWithinBounds(count, 1, 7);
if (count < 1) {
throw new BoundaryException("Value must be at least 1, " + count +
" given");
}
if (buffer == null) {
throw new NullPointerException("Null return buffer was given");
}