WPILibC++ 2023.4.3-108-ge5452e3
I2C Functions

Namespaces

namespace  hal
 WPILib Hardware Abstraction Layer (HAL) namespace.
 

Enumerations

enum  HAL_I2CPort : int32_t { HAL_I2C_kInvalid = -1 , HAL_I2C_kOnboard , HAL_I2C_kMXP }
 

Functions

void HAL_InitializeI2C (HAL_I2CPort port, int32_t *status)
 Initializes the I2C port. More...
 
int32_t HAL_TransactionI2C (HAL_I2CPort port, int32_t deviceAddress, const uint8_t *dataToSend, int32_t sendSize, uint8_t *dataReceived, int32_t receiveSize)
 Generic I2C read/write transaction. More...
 
int32_t HAL_WriteI2C (HAL_I2CPort port, int32_t deviceAddress, const uint8_t *dataToSend, int32_t sendSize)
 Executes a write transaction with the device. More...
 
int32_t HAL_ReadI2C (HAL_I2CPort port, int32_t deviceAddress, uint8_t *buffer, int32_t count)
 Executes a read transaction with the device. More...
 
void HAL_CloseI2C (HAL_I2CPort port)
 Closes an I2C port. More...
 

Detailed Description

Enumeration Type Documentation

◆ HAL_I2CPort

enum HAL_I2CPort : int32_t
Enumerator
HAL_I2C_kInvalid 
HAL_I2C_kOnboard 
HAL_I2C_kMXP 

Function Documentation

◆ HAL_CloseI2C()

void HAL_CloseI2C ( HAL_I2CPort  port)

Closes an I2C port.

Parameters
portThe I2C port, 0 for the on-board, 1 for the MXP.

◆ HAL_InitializeI2C()

void HAL_InitializeI2C ( HAL_I2CPort  port,
int32_t *  status 
)

Initializes the I2C port.

Opens the port if necessary and saves the handle. If opening the MXP port, also sets up the channel functions appropriately.

Parameters
[in]portThe port to open, 0 for the on-board, 1 for the MXP.
[out]statusError status variable. 0 on success.

◆ HAL_ReadI2C()

int32_t HAL_ReadI2C ( HAL_I2CPort  port,
int32_t  deviceAddress,
uint8_t *  buffer,
int32_t  count 
)

Executes a read transaction with the device.

Reads 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.

Parameters
portThe I2C port, 0 for the on-board, 1 for the MXP.
deviceAddressThe register to read first in the transaction.
countThe number of bytes to read in the transaction.
bufferA pointer to the array of bytes to store the data read from the device.
Returns
>= 0 on success or -1 on transfer abort.

◆ HAL_TransactionI2C()

int32_t HAL_TransactionI2C ( HAL_I2CPort  port,
int32_t  deviceAddress,
const uint8_t *  dataToSend,
int32_t  sendSize,
uint8_t *  dataReceived,
int32_t  receiveSize 
)

Generic I2C read/write transaction.

This is a lower-level interface to the I2C hardware giving you more control over each transaction.

Parameters
portThe I2C port, 0 for the on-board, 1 for the MXP.
deviceAddressThe address of the register on the device to be read/written.
dataToSendBuffer of data to send as part of the transaction.
sendSizeNumber of bytes to send as part of the transaction.
dataReceivedBuffer to read data into.
receiveSizeNumber of bytes to read from the device.
Returns
>= 0 on success or -1 on transfer abort.

◆ HAL_WriteI2C()

int32_t HAL_WriteI2C ( HAL_I2CPort  port,
int32_t  deviceAddress,
const uint8_t *  dataToSend,
int32_t  sendSize 
)

Executes a write transaction with the device.

Writes a single byte to a register on a device and wait until the transaction is complete.

Parameters
portThe I2C port, 0 for the on-board, 1 for the MXP.
deviceAddressThe address of the register on the device to be written.
dataToSendThe byte to write to the register on the device.
sendSizeNumber of bytes to send.
Returns
>= 0 on success or -1 on transfer abort.