I2C
The I2C protocol also known as the two wire interface is a simple serial communication protocol that uses just two pins of a microcontroller namely SCL (serial clock) and SDA (serial data). This is a very popular protocol that can be used to address a large number of slave devices that are connected to the same bus. This protocol comes in handy when there is scarcity of available pins in the microcontroller. Each slave device has a slave address or a name for which they respond. This is usually a 7-bit binary number. Once a master sends a valid slave address, that slave alone will respond to the master’s queries and all other slaves will ignore any conversation between the master and that particular slave.
There are a number of conditions that can be made over the I2C bus such as start and stop sequence. The data line does not change when the clock line is HIGH. If the data line changes when the clock line is High, the slave device interprets it as a command and not as data. This is the only feature in the interface that puts a distinct line between the command and data.
#
Struct definitionTwi represents a struct containing registers for TWI namely:
TWBR
: TWI Bit Rate Register.TWSR
: TWI Status Register.TWAR
: TWI (Slave) Address Register.TWDR
: TWI Data Register.TWSR
: TWI Status Register.TWAMR
: TWI Address Mask.
More about there registers and their functions can be found out at Section 21.9 of ATmega328P datasheet.
#
Usage:#
Function DefinitionThese functions set the data direction of port C to either read or write.
Sets DDRC to write direction.
Sets DDRC to write direction.
#
Usage:#
Trait implementationnew
for Twi
#
Impl #
Usage:Returns a pointer to the struct Twi.
wait_to_complete
for Twi
#
Impl Waits for the ongoing process to be complete. List of processes include:
Start
Repeat Start
Master Trasmitter Slave Acknoledgement
Master Trasmitter Data Acknoledgement
Master Receiver Slave Acknoledgement
Master Receiver Data Acknoledgement
Master Receiver Slave No-Acknoledgement
Returns True
if process was successful.
init
for Twi
#
Impl #
Usage:Initiates the Twi bus.
start
for Twi
#
Impl #
Usage:Sends a start signal to the slave and returns true if operation is successful.
rep_start
for Twi
#
Impl #
Usage:Sends a Repeat Start Signal to the Slave and returns True
is process is successful.
Repeated Start condition is one which allows a master to continue the current transaction without losing atomicity. This is achieved by NOT sending a stop after the transaction but sending a Start in its place.
stop
for Twi
#
Impl #
Usage:Stops the data transfer between master and slave and sets slave as free. Marks the end of transaction with the slave device.
set_address
for Twi
#
Impl #
Usage:Sets address of Slave and returns True
if operation is successful.
Here, the slave address with the write specifier is sent after the Start sequence. The slave sends an Acknowledge to the master (MCU). Then the next byte of data written to the slave device is the address of the register to write to.
address_read
for Twi
#
Impl #
Usage:Checks if slave is acknowledged and returns True
if operation is successful.
write
for Twi
#
Impl #
Usage:write_burst
for Twi
#
Impl #
Usage:Writes consecutive bytes of data to the Slave and returns number of bytes written.
write_to_slave
for Twi
#
Impl #
Usage:This function:
- Starts the Twi Bus : Returns false if start fails.
- Sets slave address : Returns false if slave does not acknowledge on address being set.
- Writes all data bytes to the salve : Aborts and returns false if all data bytes were not written.
- Returns true if all the above three steps are successful and also sends a stop singal to the slave.
read_from_slave
for Twi
#
Impl #
Usage:This function:
- Starts the Twi Bus : Returns false if start fails.
- Reads slave address : Returns false if slave does not acknowledge.
- Reads data bytes from the salve : Aborts and returns false if the length of data specified is not read.
- Returns true if all the above three steps are successful and also sends a stop singal to the slave.