SAM SERCOM I2C Common Driver.
Copyright (c) 2012-2020 Microchip Technology Inc. and its subsidiaries.
Macros | ||||||||||||||||||||
Driver Feature Definition | ||||||||||||||||||||
This driver for Atmel® | SMART ARM®-based microcontrollers provides an interface for the configuration and management of the device's SERCOM I2C module, for the transfer of data via an I2C bus. The following driver API modes are covered by this manual: The following peripheral is used by this module:
The following devices can use this module:
The outline of this documentation is as follows: PrerequisitesThere are no prerequisites. Module OverviewThe outline of this section is as follows:
Driver Feature Macro Definition
Functional DescriptionThe I2C provides a simple two-wire bidirectional bus consisting of a wired-AND type serial clock line (SCL) and a wired-AND type serial data line (SDA). The I2C bus provides a simple, but efficient method of interconnecting multiple master and slave devices. An arbitration mechanism is provided for resolving bus ownership between masters, as only one master device may own the bus at any given time. The arbitration mechanism relies on the wired-AND connections to avoid bus drivers short-circuiting. A unique address is assigned to all slave devices connected to the bus. A device can contain both master and slave logic, and can emulate multiple slave devices by responding to more than one address. Bus TopologyThe I2C bus topology is illustrated in the figure below. The pull-up resistors (Rs) will provide a high level on the bus lines when none of the I2C devices are driving the bus. These are optional, and can be replaced with a constant current source.
I2C Bus Topology TransactionsThe I2C standard defines three fundamental transaction formats:
A data transfer starts with the master issuing a Start condition on the bus, followed by the address of the slave together with a bit to indicate whether the master wants to read from or write to the slave. The addressed slave must respond to this by sending an ACK back to the master. After this, data packets are sent from the master or slave, according to the read/write bit. Each packet must be acknowledged (ACK) or not acknowledged (NACK) by the receiver. If a slave responds with a NACK, the master must assume that the slave cannot receive any more data and cancel the write operation. The master completes a transaction by issuing a Stop condition. A master can issue multiple Start conditions during a transaction; this is then called a Repeated Start condition. Address PacketsThe slave address consists of seven bits. The 8th bit in the transfer determines the data direction (read or write). An address packet always succeeds a Start or Repeated Start condition. The 8th bit is handled in the driver, and the user will only have to provide the 7-bit address. Data PacketsData packets are nine bits long, consisting of one 8-bit data byte, and an acknowledgement bit. Data packets follow either an address packet or another data packet on the bus. Transaction ExamplesThe gray bits in the following examples are sent from master to slave, and the white bits are sent from slave to master. Example of a read transaction is shown in the figure below. Here, the master first issues a Start condition and gets ownership of the bus. An address packet with the direction flag set to read is then sent and acknowledged by the slave. Then the slave sends one data packet which is acknowledged by the master. The slave sends another packet, which is not acknowledged by the master and indicates that the master will terminate the transaction. In the end, the transaction is terminated by the master issuing a Stop condition.
I2C Packet Read Example of a write transaction is shown in the figure below. Here, the master first issues a Start condition and gets ownership of the bus. An address packet with the dir flag set to write is then sent and acknowledged by the slave. Then the master sends two data packets, each acknowledged by the slave. In the end, the transaction is terminated by the master issuing a Stop condition.
I2C Packet Write Packet TimeoutWhen a master sends an I2C packet, there is no way of being sure that a slave will acknowledge the packet. To avoid stalling the device forever while waiting for an acknowledge, a user selectable timeout is provided in the i2c_master_config struct which lets the driver exit a read or write operation after the specified time. The function will then return the STATUS_ERR_TIMEOUT flag. This is also the case for the slave when using the functions postfixed The time before the timeout occurs, will be the same as for unknown bus state timeout. Repeated StartTo issue a Repeated Start, the functions postfixed Multi MasterIn a multi master environment, arbitration of the bus is important, as only one master can own the bus at any point. Arbitration
Clock SynchronizationIn situations where more than one master is trying to control the bus clock line at the same time, a clock synchronization algorithm based on the same principles used for clock stretching is necessary. Bus StatesAs the I2C bus is limited to one transaction at the time, a master that wants to perform a bus transaction must wait until the bus is free. Because of this, it is necessary for all masters in a multi-master system to know the current status of the bus to be able to avoid conflicts and to ensure data integrity.
The bus state diagram can be seen in the figure below. Bus TimingInactive bus timeout for the master and SDA hold time is configurable in the drivers. Unknown Bus State TimeoutWhen a master is enabled or connected to the bus, the bus state will be unknown until either a given timeout or a stop command has occurred. The timeout is configurable in the i2c_master_config struct. The timeout time will depend on toolchain and optimization level used, as the timeout is a loop incrementing a value until it reaches the specified timeout value. SDA Hold TimeoutWhen using the I2C in slave mode, it will be important to set a SDA hold time which assures that the master will be able to pick up the bit sent from the slave. The SDA hold time makes sure that this is the case by holding the data line low for a given period after the negative edge on the clock. The SDA hold time is also available for the master driver, but is not a necessity. Operation in Sleep ModesThe I2C module can operate in all sleep modes by setting the run_in_standby Boolean in the i2c_master_config or i2c_slave_config struct. The operation in slave and master mode is shown in the table below.
Special ConsiderationsThere are no special considerations for this driver for the APIs listed in this document. Extra InformationFor extra information, see Extra Information for SERCOM I2C Driver. This includes: ExamplesFor a list of examples related to this driver, see Examples for SERCOM I2C Driver. API OverviewDefine SERCOM I2C driver features set according to different device family. | ||||||||||||||||||||
#define | FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED | |||||||||||||||||||
Fast mode plus and high speed support. More... | ||||||||||||||||||||
#define | FEATURE_I2C_10_BIT_ADDRESS | |||||||||||||||||||
10-bit address support More... | ||||||||||||||||||||
#define | FEATURE_I2C_SCL_STRETCH_MODE | |||||||||||||||||||
SCL stretch mode support. More... | ||||||||||||||||||||
#define | FEATURE_I2C_SCL_EXTEND_TIMEOUT | |||||||||||||||||||
SCL extend timeout support. More... | ||||||||||||||||||||
#define | FEATURE_I2C_DMA_SUPPORT | |||||||||||||||||||
Enumerations | |
enum | i2c_transfer_direction { I2C_TRANSFER_WRITE = 0, I2C_TRANSFER_READ = 1 } |
Transfer direction. More... | |
#define FEATURE_I2C_10_BIT_ADDRESS |
10-bit address support
#define FEATURE_I2C_DMA_SUPPORT |
#define FEATURE_I2C_FAST_MODE_PLUS_AND_HIGH_SPEED |
Fast mode plus and high speed support.
#define FEATURE_I2C_SCL_EXTEND_TIMEOUT |
SCL extend timeout support.
#define FEATURE_I2C_SCL_STRETCH_MODE |
SCL stretch mode support.