I2C: Provide byte[] JNI interfaces.

This avoids a direct byte buffer allocation on every read/write/transaction
on the byte[] variants.

Changes HAL I2C interfaces to use const for dataToSend.
This commit is contained in:
Peter Johnson
2017-11-15 21:41:58 -08:00
parent 6307d41002
commit 9021b37fd2
8 changed files with 161 additions and 58 deletions

View File

@@ -95,7 +95,7 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
* @return >= 0 on success or -1 on transfer abort.
*/
int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
uint8_t* dataToSend, int32_t sendSize,
const uint8_t* dataToSend, int32_t sendSize,
uint8_t* dataReceived, int32_t receiveSize) {
if (port > 1) {
// Set port out of range error here
@@ -106,7 +106,7 @@ int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
msgs[0].addr = deviceAddress;
msgs[0].flags = 0;
msgs[0].len = sendSize;
msgs[0].buf = dataToSend;
msgs[0].buf = const_cast<uint8_t*>(dataToSend);
msgs[1].addr = deviceAddress;
msgs[1].flags = I2C_M_RD;
msgs[1].len = receiveSize;
@@ -137,7 +137,7 @@ int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
* @return >= 0 on success or -1 on transfer abort.
*/
int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
uint8_t* dataToSend, int32_t sendSize) {
const uint8_t* dataToSend, int32_t sendSize) {
if (port > 1) {
// Set port out of range error here
return -1;
@@ -147,7 +147,7 @@ int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
msg.addr = deviceAddress;
msg.flags = 0;
msg.len = sendSize;
msg.buf = dataToSend;
msg.buf = const_cast<uint8_t*>(dataToSend);
struct i2c_rdwr_ioctl_data rdwr;
rdwr.msgs = &msg;

View File

@@ -17,10 +17,10 @@ extern "C" {
void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status);
int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
uint8_t* dataToSend, int32_t sendSize,
const uint8_t* dataToSend, int32_t sendSize,
uint8_t* dataReceived, int32_t receiveSize);
int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
uint8_t* dataToSend, int32_t sendSize);
const uint8_t* dataToSend, int32_t sendSize);
int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
int32_t count);
void HAL_CloseI2C(HAL_I2CPort port);

View File

@@ -16,14 +16,14 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
SimI2CData[port].SetInitialized(true);
}
int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
uint8_t* dataToSend, int32_t sendSize,
const uint8_t* dataToSend, int32_t sendSize,
uint8_t* dataReceived, int32_t receiveSize) {
SimI2CData[port].Write(deviceAddress, dataToSend, sendSize);
SimI2CData[port].Read(deviceAddress, dataReceived, receiveSize);
return 0;
}
int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
uint8_t* dataToSend, int32_t sendSize) {
const uint8_t* dataToSend, int32_t sendSize) {
SimI2CData[port].Write(deviceAddress, dataToSend, sendSize);
return 0;
}

View File

@@ -97,10 +97,11 @@ void I2CData::CancelWriteCallback(int32_t uid) {
m_writeCallbacks = CancelCallback(m_writeCallbacks, uid);
}
void I2CData::Write(int32_t deviceAddress, uint8_t* dataToSend,
void I2CData::Write(int32_t deviceAddress, const uint8_t* dataToSend,
int32_t sendSize) {
std::lock_guard<wpi::mutex> lock(m_dataMutex);
InvokeCallback(m_writeCallbacks, "Write", dataToSend, sendSize);
InvokeCallback(m_writeCallbacks, "Write", const_cast<uint8_t*>(dataToSend),
sendSize);
}
void I2CData::Read(int32_t deviceAddress, uint8_t* buffer, int32_t count) {
std::lock_guard<wpi::mutex> lock(m_dataMutex);

View File

@@ -35,7 +35,8 @@ class I2CData {
int32_t RegisterWriteCallback(HAL_BufferCallback callback, void* param);
void CancelWriteCallback(int32_t uid);
void Write(int32_t deviceAddress, uint8_t* dataToSend, int32_t sendSize);
void Write(int32_t deviceAddress, const uint8_t* dataToSend,
int32_t sendSize);
void Read(int32_t deviceAddress, uint8_t* buffer, int32_t count);
void ResetData();