Xapp385, Coolrunner-Ii CPLD I2C Bus Controller Implementation
Xapp385, Coolrunner-Ii CPLD I2C Bus Controller Implementation
Xapp385, Coolrunner-Ii CPLD I2C Bus Controller Implementation
R
CoolRunner CPLD I2C Bus Controller
Implementation
XAPP333 (v1.7) December 24, 2002
Summary This document details the VHDL implementation of an I2C controller in a Xilinx CoolRunner™
256-macrocell CPLD. CoolRunner CPLDs are the lowest power CPLDs available, making this
the perfect target device for an I2C controller. To obtain the VHDL code described in this
document, go to section VHDL Code Download, page 19 for instructions. This design fits both
XPLA3 and CoolRunner-II CPLDs. For the CoolRunner-II CPLD version, please refer to
XAPP385, CoolRunner-II CPLD I2C Bus Controller Implementation.
Introduction The I2C bus is a popular serial, two-wire interface used in many systems because of its low
overhead. The two-wire interface minimizes interconnections so ICs have fewer pins, and the
number of traces required on printed circuit boards is reduced. Capable of 100 KHz operation,
each device connected to the bus is software addressable by a unique address with a simple
Master/Slave protocol.
The CoolRunner I2C Controller design contains an asynchronous microcontroller (µC) interface
and provides I2C Master/Slave capability. It is intended to be used with a microcontroller (µC)
or microprocessor (µP) as shown in Figure 1.
SDA
CoolRunner I2C Bus Controller SCL
Address
Data I2C Master/
Microcontroller Microcontroller
Slave
Control Interface Interface
X315_01_091999
I2C Background This section will describe the main protocol of the I2C bus. For more details and timing
diagrams, please refer to the I2C specification.
The I2C bus consists of two wires, serial data (SDA) and serial clock (SCL), which carry
information between the devices connected to the bus. The number of devices connected to
the same bus is limited only by a maximum bus capacitance of 400 pF. Both the SDA and SCL
lines are bidirectional lines, connected to a positive supply voltage via a pull-up resistor. When
© 2002 Xilinx, Inc. All rights reserved. All Xilinx trademarks, registered trademarks, patents, and further disclaimers are as listed at http://www.xilinx.com/legal.htm. All other
trademarks and registered trademarks are the property of their respective owners. All specifications are subject to change without notice.
NOTICE OF DISCLAIMER: Xilinx is providing this design, code, or information "as is." By providing the design, code, or information as one possible implementation of this fea-
ture, application, or standard, Xilinx makes no representation that this implementation is free from any claims of infringement. You are responsible for obtaining any rights you
may require for your implementation. Xilinx expressly disclaims any warranty whatsoever with respect to the adequacy of the implementation, including but not limited to any war-
ranties or representations that this implementation is free from claims of infringement and any implied warranties of merchantability or fitness for a particular purpose.
the bus is free, both lines are High. The output stages of devices connected to the bus must
have an open-drain or open-collector in order to perform the wired-AND function.
Each device on the bus has a unique address and can operate as either a transmitter or
receiver. In addition, devices can also be configured as Masters or Slaves. A Master is the
device which initiates a data transfer on the bus and generates the clock signals to permit that
transfer. Any other device that is being addressed is considered a Slave. The I2C protocol
defines an arbitration procedure that insures that if more than one Master simultaneously tries
to control the bus, only one is allowed to do so and the message is not corrupted. The
arbitration and clock synchronization procedures defined in the I2C specification are supported
by the CoolRunner I2C Controller.
Data transfers on the I2C bus are initiated with a START condition and are terminated with a
STOP condition. Normal data on the SDA line must be stable during the High period of the
clock. The High or Low state of the data line can only change when SCL is Low. The START
condition is a unique case and is defined by a High-to-Low transition on the SDA line while SCL
is High. Likewise, the STOP condition is a unique case and is defined by a Low-to-High
transition on the SDA line while SCL is High. The definitions of data, START, and STOP insure
that the START and STOP conditions will never be confused as data. This is shown in Figure 2.
SDA MSB
SCL 1 2 3 7 8 9
S P
Start
ACK Stop
Condition Condition
x315_10_101599
Each data packet on the I2C bus consists of eight bits of data followed by an acknowledge bit
so one complete data byte transfer requires nine clock pulses. Data is transferred with the most
significant bit first (MSB). The transmitter releases the SDA line during the acknowledge bit and
the receiver of the data transfer must drive the SDA line low during the acknowledge bit to
acknowledge receipt of the data. If a Slave-receiver does not drive the SDA line Low during the
acknowledge bit, this indicates that the Slave-receiver was unable to accept the data and the
Master can then generate a STOP condition to abort the transfer. If the Master-receiver does
not generate an acknowledge, this indicates to the Slave-transmitter that this byte was the last
byte of the transfer.
Standard communication on the bus between a Master and a Slave is composed of four parts:
START, Slave address, data transfer, and STOP. The I2C protocol defines a data transfer
format for both 7-bit and 10-bit addressing. The implementation of the I2C controller in the Xilinx
CoolRunner CPLD supports the seven-bit address format. After the START condition, a Slave
address is sent. This address is seven bits long followed by an eighth-bit which is the read/write
bit. A "1" indicates a request for data (read) and a "0" indicates a data transmission (write). Only
the Slave with the calling address that matches the address transmitted by the Master
responds by sending back an acknowledge bit by pulling the SDA line Low on the ninth clock.
Once successful Slave addressing is achieved, the data transfer can proceed byte-by-byte as
specified by the read/write bit. The Master can terminate the communication by generating a
STOP signal to free the bus. However, the Master may generate a START signal without
generating a STOP signal first. This is called a repeated START.
CoolRunner I2C The CoolRunner CPLD implementation of the I2C Controller supports the following features:
Controller • Microcontroller interface
Signal The I/O signals of the CoolRunner I2C controller are described in Table 1. Pin numbers have
Descriptions not been assigned to this design, this can be done to meet the system requirements of the
designer.
Block Diagram The block diagram of the CoolRunner I2C Controller, shown in Figure 3 was broken into two
major blocks, the µC interface and the I2C interface.
ADDR_BUS[23:0]
DATA_BUS[7:0]
DTACK
R_W
MCF
IRQ
DS
AS
µC Interface
ADDR_DECODE/Bus Interface
RESET
START/
STOP
Arbitration and
Main State Machine SCL
START/STOP
Generation
Detection
I2C Interface
SDA
SCL
X315_02_101599
Microcontroller The µC interface for the I2C controller design supports an asynchronous byte-wide bus
Logic protocol. This protocol is the method in which the µC reads and writes the registers in the
design and is shown in Figure 4.
X315_11_101799
RESET
Asserted
IDLE
AS Asserted
RESET Negated
ADDRESS_MATCH
DS
Negated Negated
ADDR
DS Asserted
ADDRESS_MATCH Asserted
AS Negated
DS Negated DATA_TRS
AS Asserted
DS Asserted
ASSERT_DTACK
X315_03_010500
In the first cycle, the µC places the address on the address bus, sets the read/write line to the
correct state, and asserts address strobe (AS) and data strobe (DS). Address strobe indicates
that the address present on the address bus is valid. If this is a write cycle, the µC also places
the data on the data bus and DS indicates that valid data is present on the data bus. If this is a
read cycle, the µC 3-states the data bus and DS indicates that the CoolRunner I2C Controller
can place data on the data bus.
Upon the assertion of AS, the CoolRunner I2C Controller transitions to the ADDR state to
decode the address and determine if it is the device being addressed. The enables for the
internal registers are set in this state. If the CoolRunner I2C Controller is being addressed and
DS is asserted, the CoolRunner I2C controller progresses to the DATA_TRS state. If this is a
read cycle, the requested data is placed on the bus and if this is a write cycle, the data from the
data bus is latched in the addressed register. The CoolRunner I2C Controller automatically
progresses to the ASSERT_DTACK state and asserts DTACK indicating that the data
requested is ready if a read cycle or that the data has been received if a write cycle.
Upon the assertion of DTACK, the µC either removes data from the bus if this is a write cycle,
or latches the data present on the bus if this is a read cycle. The read/write line is set to read
and AS and DS are negated to indicate that the data transfer is complete. The negation of AS
and DS causes the CoolRunner I2C Controller to negate DTACK and transition to the IDLE
state.
I2C Interface The I2C bus interface logic consists of several different processes as seen in Figure 3. Control
Logic bits from the µC interface registers determine the behavior of these processes.
Arbitration
Arbitration of the I2C bus is lost in the following circumstances:
• The SDA signal is sampled as a "0" when the Master outputs a "1" during an address or
data transmit cycle
• The SDA signal is sampled as a "0" when the Master outputs a "1" during the
acknowledge bit of a data receive cycle
• A start cycle is attempted when the bus is busy
START/STOP Detection
This process monitors the SDA and SCL signals on the I2C bus for START and STOP
conditions. When a START condition is detected, the Bus Busy bit is set. This bit stays set until
a STOP condition is detected. The signals, DETECT_START and DETECT_STOP are
generated by this process for use by other processes in the logic. Note that this logic detects
the START and STOP conditions even when the CoolRunner I2C Controller is the generator of
these conditions.
The state machine that generates SCL and SDA when in Master mode is shown in Figure 6.
Note that SCL and SDA are held at the default levels if the bus is busy. This state machine
generates the controls for the system clock counter.
RST
GEN_START = 0
IDLE
GEN_START = 1
REP_START = 1 START
CLK_CNT = HIGH_CNT/2
CLK_CNT = START_HOLD
ARB_LOST = 1
BIT_CNT > 7
CLK_CNT = TBUF
SCL_LOW_EDGE
SCL_LOW
CLK_CNT = LOW_CNT
CLK_CNT < TBUF
CLK_CNT = HIGH_CNT
STOP_WAIT
SCL_INT = 0
SCL_HI_EDGE
GEN_STOP = 1
CLK_CNT = HIGH_CNT/2 SCL_INT = 1
SCL_HI
X315_04_071000
The internal SDA signal output from this design is either the SDA signal generated by this state
machine for START and STOP conditions or the data from the MBDR register when the
CoolRunner I2C Controller is in transmit mode. Note that both SCL and SDA are open-collector
outputs, therefore, they are only driven to a "0". When a "1" is to be output on these signals, the
CoolRunner I2C Controller 3-states their output buffers. The logic in the design will set internal
SDA and SCL signals to "1" or "0". These internal signals actually control the output enable of
the 3-state buffer for these outputs.
In the IDLE state, SCL and SDA are 3-stated, allowing any Master to control the bus. Once a
request has entered to generate a start condition, the CoolRunner I2C Controller is in Master
mode, and the bus is not busy, the state machine transitions to the START state.
The START state holds SCL High, but drives SDA Low to generate a START condition. The
system clock counter is started and the state machine stays in this state until the required hold
time is met. At this point, the next state is SCL_LOW_EDGE.
The SCL_LOW_EDGE state simply creates a falling edge on SCL and resets the system clock
counter. On the next clock edge, the state machine moves to state SCL_LOW. In this state, the
SCL line is held Low and the system clock counter begins counting. If the REP_START signal
is asserted then the SDA signal will be set High, if the GEN_STOP signal is asserted, SDA will
be set Low.
When the SCL low time has been reached, the state machine will transition to the IDLE state if
arbitration has been lost and the byte transfer is complete to insure that SCL continues until the
end of the transfer. Otherwise the next state is the SCL_HI_EDGE state.
The SCL_HI_EDGE state generates a rising edge on SCL by setting SCL to "1". Note,
however, that the state machine will not transition to the SCL_HI state until the sampled SCL
signal is also High to obey the clock synchronization protocol of the I2C specification. Clock
synchronization is performed using the wired-AND connection of the SCL line. The SCL line will
be held Low by the device with the longest low period. Devices with shorter low periods enter
a high wait state until all devices have released the SCL line and it goes High. Therefore the
SCL_HI_EDGE state operates as the high wait state as the SCL clock is synchronized.
The SCL_HI state then starts the system clock counter to count the high time for the SCL
signal. If a repeated START or a STOP condition has been requested, the state machine will
transition to the appropriate state after half of the SCL high time so that the SDA line can
transition as required. If neither of these conditions has been requested, then the state machine
transitions to the SCL_LOW_EDGE state when the SCL high time has been achieved.
The STOP_WAIT state is used to insure that the hold time requirement after a STOP condition
is met.
Master to Slave mode if arbitration is lost or if the CoolRunner I2C Controller is addressed as a
Slave.
RESET
Asserted
IDLE
DETECT_START = 1
BIT_CNT < 8
HEADER
Master Rcv
SDA = 0 ACK_HEADER Master Xmit
SDA = 0
Slave Rcv
Slave Xmit
ADDR_MATCH = 1
ADDR_MATCH = 1
RCV_DATA XMIT_DATA
Master
DETECT_STOP = 1 SDA = 1 DETECT_STOP = 1 SDA = 0
BIT_CNT = 8 BIT_CNT = 8
ACK_DATA WAIT_ACK
SDA = 1
STOP
X315_05_071000
This state machine utilizes and controls a counter that counts the I2C bits that have been
received. This count is stored in the signal BIT_CNT. This state machine also controls two shift
registers, one that stores the I2C header that has been received and another that stores the I2C
data that has been received or is to be transmitted.
Note:
This state machine and the associated counters and shift registers are clocked on the falling edge of
the incoming SCL clock. If the load is heavy on the SCL line, the rise time of the SCL signal may be
very slow which can cause susceptibility to noise for some systems. This can be particularly
dangerous on a clock signal. The designer is strongly encouraged to investigate the signal integrity of
the SCL line and if necessary, use external buffers for the SCL signal.
When a START signal has been detected, the state machine transitions from the IDLE state to
the HEADER state. The START signal detection circuit monitors the incoming SDA and SCL
lines to detect the START condition. The START condition can be generated by the
CoolRunner I2C controller or another Master—either source will transition the state machine to
the HEADER state.
The HEADER state is the state where the I2C header is transmitted on the I2C bus from the
MBDR register if in Master mode. In this state, the incoming I2C data is captured in the I2C
Header shift register. In Master mode, the I2C Header shift register will contain the data that
was just transmitted by this design. When all eight bits of the I2C header have been shifted in,
the state machine transitions to the ACK_HEADER state.
In the ACK_HEADER state, the CoolRunner I2C design samples the SDA line if in Master
mode to determine whether the addressed I2C Slave acknowledged the header. If the
addressed Slave does not acknowledge the header, the state machine will transition to the
STOP state which signals the SCL/START/STOP generator to generate a STOP. If the
addressed Slave has acknowledged the address, then the LSB of the I2C header is used to
determine if this is a transmit or receive operation and the state machine transitions to the
appropriate state to either receive data, RCV_DATA, or to transmit data, XMIT_DATA.
The I2C Header shift register is constantly compared with the I2C address set in the MADR
register. If these values match in the ACK_HEADER state, the CoolRunner I2C Controller has
been addressed as a Slave and the mode immediately switches to Slave mode. The MAAS bit
is then set in the MBSR status register. The SDA line will be driven as set in the TXAK register
to acknowledge the header to the current I2C bus Master. Again, the LSB of the I2C header is
used to determine the direction of the data transfer and the appropriate state is chosen.
The RCV_DATA state shifts the incoming I2C data into the I2C shift register for transfer to the
µC. When the whole data byte has been received, the state machine transitions to the
ACK_DATA state and the value of the TXAK register is output on the SDA line to acknowledge
the data transfer. Note that in Master mode, the indication that the Slave has transmitted the
required number of data bytes is to not acknowledge the last byte of data. The µC must negate
the TXAK bit to prohibit the ACK of the last data byte. The state machine exits this pair of states
when a STOP condition has been detected, otherwise, the transition between these two states
continues. In Master mode, the µC requests a STOP condition by negating the MSTA bit.
The XMIT_DATA state shifts the data from the I2C data register to the SDA line. When the
entire byte has been output, the state machine transitions to the WAIT_ACK state. If an
acknowledge is received, the state machine goes back to the XMIT_DATA to transmit the next
byte of data. This pattern continues until either a STOP condition is detected, or an
acknowledge is not received for a data byte.
Note that the data transfer states of this state machine assume that the µC can keep up with
the rate at which data is received or transmitted. If interrupts are enabled, an interrupt is
generated at the completion of each byte transfer. The MCF bit is set as well providing the
same indication. Data is transferred to/from the I2C data register to/from the µC data register
during the acknowledge cycle of the data transfer. The state machine does not wait for an
indication that the µC has read the received data or that new data has been written for
transmission. The designer should be aware of the effective data rate of the µC to insure that
this is not an issue.
The STOP state signals the SCL/START/STOP generator to generate a STOP condition if the
CoolRunner I2C design is in Master mode. The next state is always the IDLE state and the I2C
activity is completed.
Operational The flow of the interface between the µC and the CoolRunner I2C Controller is detailed in the
Flow Diagrams following flow charts. These flow charts are meant to be a guide for utilizing the CoolRunner I2C
Controller in a µC system.
Initialization
Before the CoolRunner I2C Controller can be utilized, certain bits and registers must be
initialized as shown in Figure 8.
BEGIN
Modify MBCR to
Enable Interrupts
END
X315_06_101599
Master Transmit/Receive
The flow charts for transmitting data and receiving data while I2C bus Master are shown in
Figure 9 and Figure 10. The major difference between transmitting and receiving is the
additional step in the Master Receive flow chart of turning off the acknowledge bit on the
second to last data word.
BEGIN
No
Write I2C Header
in MBDR
Transfer
No
Complete?
(MCF = 1)
Yes
Write Data
to MBDR
Transfer
No
Complete?
(MCF = 1)
Yes
No
Last Word?
Yes
Negate MSTA in MBCR
to Generate STOP
END
X315_07_091999
BEGIN
No
Write I2C Header
in MBDR
Transfer
No
Complete?
(MCF = 1)
Yes
Read Data
from MBDR
Transfer
No
Complete?
(MCF = 1)
Yes
No
Last Word - 1?
Yes
Set TXAK in MBCR
to Turn Off ACK
Yes
Read Data from
MBDR
Transfer
No
Complete?
(MCF = 1)
Yes
Negate MSTA in MBCR
to Generate STOP
END
X315_08_071000
BEGIN
Addressed
No
as Slave?
Yes
Check SRW Bit in
MSBR for Xmit/Rcv
Read/Write
Data
in MBDR
Transfer
No
Complete?
(MCF = 1)
Yes
Yes
Bus Busy?
MAAS = 1?
No
END
X315_09_071000
CoolRunner The design of the CoolRunner I2C Controller was implemented in VHDL and targeted to a 256
XPLA3 CPLD macrocell CoolRunner CPLD in a 144-pin TQFP package (XCR3256XL-10TQ144C) using
Xilinx Project Navigator. (Xilinx Project Navigator software is available free-of-charge from the
Implementation Xilinx website: www.xilinx.com/products/software/webpowered.htm.).
Note:
Since the system clock frequency was 1.832 MHz, the speed of the design was not critical and any
speed grade part could have been used.
Note:
The I2C SCL line is used as a clock input into the CoolRunner I2C Controller. If there are many loads
on the I2C bus, the rise time of the SCL line can be quite slow. The CoolRunner CPLD for this design
requires a rise time no greater than 100 ns, therefore, the designer is strongly encouraged to examine
the characteristics of the SCL signal in the I2C system. If the rise time of the I2C signals are greater
than 100 ns, external buffers can be used between the actual I2C bus connections and the
CoolRunner CPLD.
The I2C design utilization in an XPLA3 256-macrocell device is shown in Table 7. This
utilization was achieved using certain fitter parameters, actual results may vary. As shown,
there is plenty of room remaining in the device for the implementation of other logic in the
system.
Design Verification
The Xilinx Project Navigator software package outputs a timing VHDL model of the fitted
design. This post-fit VHDL was simulated with the original VHDL test benches to insure design
functionality. Also, the CoolRunner I2C Controller design was simulated with an independently
generated VHDL model of an I2C Slave design to verify that the interface specifications were
implemented correctly. Please note that all verification of this design has been done through
simulations.
VHDL Code VHDL source code and test benches are available for this design. THE DESIGN IS PROVIDED
Download TO YOU "AS IS". XILINX MAKES AND YOU RECEIVE NO WARRANTIES OR CONDITIONS,
EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, AND XILINX SPECIFICALLY
DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT,
OR FITNESS FOR A PARTICULAR PURPOSE. This design has not been verified on
hardware (as opposed to simulations), and it should be used only as an example design, not as
a fully functional core. XILINX does not warrant the performance, functionality, or operation of
this Design will meet your requirements, or that the operation of the Design will be
uninterrupted or error free, or that defects in the Design will be corrected. Furthermore, XILINX
does not warrant or make any representations regarding use or the results of the use of the
Design in terms of correctness, accuracy, reliability or otherwise.
XAPP333 - http://www.xilinx.com/products/xaw/coolvhdlq.htm
Disclaimer I2C is a registered trademark of Philips Electronics N.V.. Xilinx provides this reference design
as one possible implementation of this standard and claims no rights to this interface. To use
this reference design, you must contact Philips to obtain any necessary rights associated with
this interface.
Conclusion This document has detailed the design of an I2C Controller design for a CoolRunner XPLA3
CPLD. Though the design has been extensively verified in simulations, Xilinx assumes no
responsibility for the accuracy or the functionality of this design.
Revision The following table shows the revision history for this document.
History
Date Version Revision
01/05/00 1.0 Initial Xilinx release.
03/24/00 1.2 Added VHDL disclaimer and code link.
05/02/00 1.3 Added I2C disclaimer.
07/21/00 1.4 Corrected document errors and repeated start condition
11/07/00 1.5 Updated disclaimer for HDL code.
10/01/02 1.6 Minor revisions
12/24/02 1.7 Updated resource usage.