Microcontroller Solutions TechZone Magazine, April 2011 - Digikey
Microcontroller Solutions TechZone Magazine, April 2011 - Digikey
Microcontroller Solutions TechZone Magazine, April 2011 - Digikey
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
e implemented in hardware because of the data rates involved. SPI<br />
also needs to be hardware-based. Multi-master systems will need to<br />
be hardware-based as well.<br />
Beyond data rate, each interface offers specifi c features that may<br />
make it more appropriate for a specifi c application. UART is extremely<br />
simple to implement and is full-duplex. I 2 C is popular because it<br />
requires only two wires, can have a simple master, and offers a<br />
variety of advanced options including support for addressing, multiple<br />
masters, broadcasting, and clock stretching. Clock stretching, for<br />
example, allows an I 2 C slave to hold the clock and halt the fl ow of<br />
data while it processes the data it has received, thus preventing<br />
potential overrun issues. In contrast, a SPI slave is at the mercy of<br />
the SPI master.<br />
With all of the various options available for I 2 C and SPI, many silicon<br />
vendors supply example code which you’ll need to use as a model<br />
for creating your own code. Fortunately drivers for these interfaces<br />
can be fairly simple – an I 2 C driver can be 15 lines of code, allowing<br />
you to implement an interface in a short period of time. However, it is<br />
important that you determine which features you need to support, that<br />
you’ve implemented them correctly, and that the master and slave<br />
implement the same features.<br />
For example, there are a number of parameters you’ll need to decide<br />
upon when creating an I 2 C driver, such as 7- or 11-bit addressing,<br />
single or multi-master (with collision detection and recovery), data<br />
rate, whether to use ACK/NAK protocols, support for address holding,<br />
and a slew of other options. As a result, any two particular I 2 C<br />
interfaces can be very different. For example, one common problem<br />
developers run into is mistaking the System Management Bus<br />
(SMBus) used in laptops as a simple I 2 C bus. Even though the SMBus<br />
runs over an I 2 C interface, it usually uses fi xed voltages for high and<br />
low values whereas a generic I 2 C interface uses threshold values<br />
which are typically based on a percentage of the MCU power supply,<br />
thus representing a potential discrepancy between the two which can<br />
create diffi cult-to-debug intermittent errors.<br />
Note too, that UART, I 2 C, and SPI are just physical interfaces. You’ll still<br />
need to supply a logical protocol that operates on top of the physical<br />
layer. If the interface is between two MCUs under your design, this<br />
protocol can be whatever you want it to be. If the MCU is talking to<br />
a device like a serial EEPROM, then the protocol is already defi ned<br />
for you. As a consequence, a driver for a master is likely to be more<br />
complex than a slave given the number of different devices and their<br />
protocols it may communicate with. In addition, if your master doesn’t<br />
support all of the protocols features a slave does (or, conversely, tries<br />
to use a feature a slave does not support), compatibility problems will<br />
eventually surface.<br />
The simplicity of each of these interfaces is both its advantage and<br />
curse. You can implement intricate protocols, which makes the<br />
interface more effi cient and powerful, but this can also make it more<br />
diffi cult to debug. The simplicity gives you all the rope you need to<br />
hang the system up if you aren’t careful.<br />
For example, consider an application where the interface is<br />
controlling an analog multiplexer which needs to smoothly transition<br />
between two audio streams. Depending upon how the transition is<br />
implemented, there can be undesirable – and audible – side effects;<br />
i.e., switching from a soft to loud stream can result in a loud “Pop!”<br />
Additionally, command latency may affect the transition. Ideally,<br />
you’ll want to run the audio down, switch streams, and then run<br />
the audio back up. However, this may not always be convenient for<br />
your application. The same diffi culties apply if you are working with<br />
multiple power supplies at the same time.<br />
To facilitate initial driver design and verifi cation, many vendors<br />
offer powerful development tools for implementing and verifying<br />
interface functionality. For example, Microchip offers the PICkit Serial<br />
Analyzer which connects to your system and to a PC which you use<br />
to confi gure the link as a master or slave with any of the standard<br />
interface options for UART, I 2 C, and SPI. A classic problem in interface<br />
design is the need for a pre-existing master to design a slave or<br />
a pre-existing slave to design a master. The PICkit eliminates this<br />
problem by providing a reliable other half of the link to design against<br />
with visibility into what’s being sent over both sides of the link. The<br />
PICkit also has the capable of emulating a variety of I 2 C peripherals<br />
like ADCs and fan controllers.<br />
Many MCU vendors also offer a variety of full-featured development<br />
kits and libraries to facilitate the introduction of subsystems such<br />
as an LCD display, touchscreen, or other peripheral to a system.<br />
For example, TI offers the RDKIDML-35 Intelligent Display module<br />
reference design kit which makes it relatively easy to add a 3.5”<br />
QVGA touchscreen to a system connected over either an I 2 C or SPI<br />
link. The kit also includes a software library with various GUI functions<br />
to speed user interface design.<br />
Figure 1: PICKit Serial Analyzer.<br />
Avoiding common pitfalls<br />
In the end, the best way to learn to implement a UART, I 2 C, or SPI<br />
link is to do it yourself. The following list of tips and tricks has been<br />
suggested by experts from a variety of MCU manufacturers to help<br />
you avoid some of the more common mistakes, misconceptions,<br />
and missteps developers often take when implementing their fi rst<br />
processor-to-processor interface.<br />
16