This tutorial will introduce you to I2C and provide a framework that you can use to start communicating with various devices that use I2C. This tutorial is meant for the Kinetis K40 and will probably not work on any other Kinetis chip. DISCLAIMER: This has not been fully tested and may not work for you and for all devices. The header file provided does not handle errors and should not be used for critical projects.
I2C is a simple two wire communication system used to connect various devices together, such as Sensory devices and microprocessors using 8 bit packets. I2C requires two wires: the first is called SDA and is used for transferring data, the second is called SCL and it is the clock used to drive the data to and from devices. I2C uses an open drain design which requires a pull up resistor to logic voltage (1.8,3.3,5) on both SDA and SCL for proper operation. I2C is a master-slave system where the master drives the clock and initiates communication.
The I2C protocol has 5 parts.
The Start signal which is defined as pulling the SDA line low followed by pulling SCL low.
The slave device address including the Read/Write bit
The register address that you will be writing to/ reading from
The acknowledgement signal which is sent from the receiving device after 8 bits of data has been transferred successfully.
the Stop signal, which is defined by SDA going high before SCL goes high.
1. The start signal is sent from the Master to intiate communication on the bus. The start and stop signals are the only time that SDA can change out of sync with SCL. Once the start signal is sent no other device can talk on the bus until the stop signal is sent. If for whatever reason another device tries to talk on the bus then there will be an error and the K40 can detect this.
2. The slave device is a 7 bit (sometimes 10bit but this will not be covered in this tutorial) address provided by the device and is specific to the device. The type of data operation (read/write) is determined by the 8th bit. A 1 will represent a write and a 0 a read operation.
3. The register addresses are provided by the device's specifications.
4. The data you will send to a device if you are writing or the data that you receive from the device when reading. This will always be 8 bits.
5. After 8 bits of data has been transferred successfully the receiving device will pull the SDA line low to signify that it received the data. If the transmitting device does not detect an acknowledgement then there will be an error. The K40 will be able to detect this.
6. The stop signal is sent from the Master to terminate communication on the bus. Some devices require this signal to operate properly but it is required if there will be more than one master on the bus (which will not be covered in this tutorial)
I2C header file
This header file only has functions for reading and writing one byte at a time. Most devices support reading and writing more than one byte at a time without sending the Stop signal. Typically the device will keep incrementing the register's address to the next one during read/writes when there is no stop signal present. See your device's user manual for more information.