void I2C_LeaderWrite(uint16_t followerAddress, , uint8_t targetAddress, uint8_t *txBuff,
uint8_t numBytes) {
/* wait until I2C bus is idle */
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY))
;
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
;
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, followerAddress, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
;
/* clear ADDSEND bit */
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE))
;
for (i = 0; i < numBytes; i++) {
/* data transmission */
i2c_data_transmit(I2C0, txBuff[i]);
/* wait until the TBE bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE))
;
}
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
/* wait until stop condition generate */
while (I2C_CTL0(I2C0) & 0x0200)
;
}
void I2C_LeaderRead(uint16_t followerAddress, uint8_t targetAddress, uint8_t *rxBuff,
uint8_t numBytes) {
/* wait until I2C bus is idle */
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY))
;
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
;
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, followerAddress, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
;
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while (SET != i2c_flag_get(I2C0, I2C_FLAG_TBE))
;
/* enable I2C0*/
i2c_enable(I2C0);
/* send the EEPROM's internal address to write to */
i2c_data_transmit(I2C0, targetAddress);
/* wait until BTC bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC))
;
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
;
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, followerAddress, I2C_RECEIVER);
/* wait until ADDSEND bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
;
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
/* while there is data to be read */
for (int i = 0; i < numBytes; i++) {
/* code */
/* read a data from I2C_DATA */
rxBuff[i++] = i2c_data_receive(I2C0);
/* send a stop condition */
i2c_stop_on_bus(I2C0);
}
/* wait until the stop condition is finished */
while (I2C_CTL0(I2C0) & 0x0200)
;
/* enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
}