I2C

Defines

HDC_DEVADDR

HDC2021 I2C bus device address.

NT3H_DEVADDR

NFC EEPROM I2C bus device address.

Communicates with devices on an I2C bus.

Configures the EUSCI peripheral as an I2C master. Up to 16 bytes can be written to or read from a memory address on the I2C slave.

Author

Malcolm Mackay

Some devices embed up to 16 registers within each memory address. There is a function for reading one register only.

Defines

EUSCI_BASE

Base address of the EUSCI peripheral.

Functions

void i2c_init()

Initialise the EUSCI peripheral and I/O pins for I2C.

Weak pull-up resistors must be fitted to the I/O pins.

void i2c_off()

Put the EUSCI module into reset. Enable pull-downs on the I/O pins.

Floating pins waste power.

uint8_t i2c_write8(uint8_t sa, uint8_t mema, uint8_t txbyte)

Write one byte to the I2C device.

Blocks until the write has completed.

Parameters
  • sa[in] slave address

  • mema[in] memory address

  • txbyte[in] byte to write

int i2c_readreg(uint8_t sa, uint8_t mema, uint8_t rega)

Read one register on an I2C device.

Parameters
  • sa[in] slave address

  • mema[in] memory address

  • rega[in] register address

Returns

one byte of register data.

int i2c_write_block(uint8_t sa, uint8_t mema, uint8_t nbytes, uint8_t *txdata)

Write N bytes to the I2C device.

When NBYTES == 0:

| START | WRITE SA | MEMA | STOP |

When NBYTES >= 1:

| START | WRITE SA | MEMA | TXDATA[0] | TXDATA[…] | TXDATA[NBYTES-1] | STOP |

Parameters
  • sa[in] slave address.

  • mema[in] memory address.

  • nbytes[in] the number of bytes to write.

  • txdata[in] a pointer to the array of bytes to write.

int i2c_read_block(uint8_t sa, uint8_t mema, uint8_t nbytes, uint8_t *rxdata, uint8_t rega)

Read N bytes from the I2C device.

When a register address is specified:

| START | WRITE SA | MEMA | REGA | STOP | START | READ SA | BYTE0 | BYTE … | BYTE n-1 | STOP |

When no register address is specified:

| START | WRITE SA | MEMA | STOP | START | READ SA | BYTE0 | BYTE … | BYTE n-1 | STOP |

Parameters
  • sa[in] slave address.

  • mema[in] memory address.

  • nbytes[in] the number of bytes to read.

  • rxdata[out] a pointer to an array used to store read data. Must be at least nbytes long.

  • rega[in] register address. Set to 0xFF when a register read is not required.

Returns

-1 when the slave fails to respond, otherwise zero.

uint8_t i2c_read8(uint8_t sa, uint8_t mema)

Read one byte from memory on the I2C slave.

START | WRITE SA | MEMA | STOP | START | READ SA | BYTE0 | STOP.

Parameters
  • sa[in] slave address.

  • mema[in] memory address.

Returns

one byte read from the I2C slave.

uint16_t i2c_read16(uint8_t sa, uint8_t mema)

Read two bytes from memory on the I2C slave.

START | WRITE SA | MEMA | STOP | START | READ SA | BYTE0 | BYTE1 | STOP.

Parameters
  • sa[in] slave address.

  • mema[in] memory address.

Returns

one little-endian 16-bit integer read from the I2C slave.

uint16_t i2c_read16x2(uint8_t sa, uint8_t mema, uint16_t *uint0, uint16_t *uint1)

Read two consecutive unsigned integers from the I2C slave.

START | WRITE SA | MEMA | STOP | START | READ SA | BYTE0 | BYTE1 | BYTE2 | BYTE3 | STOP.

Parameters
  • sa[in] slave address.

  • mema[in] memory address.

  • uint0[out] pointer to an address for storing the first little endian unsigned integer.

  • uint1[out] pointer to an address for strong the second little endian unsigned integer.

void USCIB0_ISR(void)

Variables

uint8_t buffer[16] = {0}

Read or write buffer. This is declared volatile because it is accessed from an ISR.

uint8_t bytesLength = 0

Transaction length. This is declared volatile because it is read from an ISR.

uint8_t gbl_regOffset = 0

Memory address. Volatile because it is read from an ISR.

bool restartTx = false
bool nackFlag = false
bool stopFlag = false
bool restartRx = false