Telecoms, Datacoms, Wireless, IoT


Add USB to anything - Part I

13 July 2005 Telecoms, Datacoms, Wireless, IoT

Articles about the universal serial bus (USB) used to begin by justifying USB as the new connection standard for personal computers. Thankfully, this is no longer necessary. If you have an embedded system and want to connect to a PC, the mainstream conduit is USB.

After introducing a new chip that makes it easy to add USB to any system, this article focuses on the SPI interface, providing example C code for a generic SPI implementation. The article concludes with code for a simple USB HID (human interface device) - a 'Windows Panic Button.'

Adding USB to anything

Microprocessor choices are often made based on integrated peripherals. Some processors include USB functionality, but most, especially the really low cost ones, do not. Have you ever chosen a microprocessor with the perfect combination of I/O and peripherals, but found that it lacks USB? Would it not be nice to design a USB peripheral without having to buy new tools and learn another processor?

It is now possible to add USB to any microprocessor with a new Maxim Integrated Products chip, the MAX3420E. This chip provides a USB full speed transceiver, an intelligent USB serial interface engine (SIE), and an SPI slave interface that can run with an SCK clock signal up to 26 MHz. The MAX3420E operates as a full-speed USB peripheral with one control endpoint, two double-buffered 64-byte data endpoints, and one 64-byte interrupt endpoint.

Bus-powered widget

Figure 1 illustrates a common USB peripheral architecture. The USB Vbus wire provides 5 V power to a 3,3 V regulator, which powers the micro and the MAX3420E (no wall wart needed!). The SPI interface can comprise three, four or five wires. The basic SPI signals are SCK (serial clock), CS (chip select, which frames an SPI transfer), MOSI (master out, slave in data) and MISO (master in, slave out data). Some interfaces can combine MOSI and MISO as a bi-directional pin, giving a three pin interface.

Figure 1.  A USB widget, powered by the USB 5 V Vbus wire. The SPI interface can be three, four or five pins. The MAX3420E gives back more I/O pins than it uses for the SPI interface
Figure 1. A USB widget, powered by the USB 5 V Vbus wire. The SPI interface can be three, four or five pins. The MAX3420E gives back more I/O pins than it uses for the SPI interface

The MAX3420E also provides an interrupt pin to reduce traffic over the SPI interface. Instead of exercising the SPI port to poll bits for USB activity, a μP can simply poll the INT pin for activity, or for more complex systems, use it as an actual interrupt.

What if the μP does not have an SPI port? No problem - it is very easy to make a firmware-driven SPI master by directly toggling general-purpose I/O pins. A strong feature of USB is that it is self-throttling; it automatically accommodates any speed interface on the SPI side. (It does this by using a NAK handshake on the USB side to indicate 'busy now, try again'.) Many USB peripherals, especially those connected to humans, can operate very responsively with even the slowest SPI interface.

What if the μP in Figure 1 is really small, say, under 10 pins? Do you not use μP precious I/O pins just to talk to the USB chip? Yes, and this is why the MAX3420E provides four general purpose inputs and four general purpose outputs to replace the pins used to talk to it, and then some. So your system actually gains I/O pins after connecting a MAX3420E.

The MAX3420E is not restricted to small systems. Figure 2 illustrates how to add USB functionality to an ASIC, FPGA, DSP, or other large chip. An obvious reason to do this is that the big chip may not have USB built in, or the USB inside may not be exactly what you want. Another good reason for this architecture is that as large chips shrink in process geometries, they are less able to touch 'high' voltages like the 3,3 V required by USB. The external USB chip with a low voltage SPI interface is a good answer. To run the low voltage interface, the MAX3420E has internal level shifters and a VL pin to set the operating voltage of the interface pins to anything between 1,7 V and 3,6 V.

Figure 2. The MAX3420E is not confined to small systems. Internal level shifters take care of interface voltages lower than 3,3 V. The SPI port can run as fast as 26 MHz
Figure 2. The MAX3420E is not confined to small systems. Internal level shifters take care of interface voltages lower than 3,3 V. The SPI port can run as fast as 26 MHz

As shown in Figure 3, the SPI interface is also an easy place to put an optical isolator, since the signals are unidirectional, and they can be programmed to run relatively slowly to support low cost optos.

Figure 3. The SPI interface provides a convenient way to isolate the USB port from the controller. The SPI signals are unidirectional and can be relatively slow (eg 1 MHz SCK) which allows the use of low cost opto-isolators
Figure 3. The SPI interface provides a convenient way to isolate the USB port from the controller. The SPI signals are unidirectional and can be relatively slow (eg 1 MHz SCK) which allows the use of low cost opto-isolators

The SPI interface

SPI (serial peripheral interface) is a simple serial interface that uses two data lines, a serial clock, and a chip select signal. The SPI master drops CS# low to start a transfer, and then drives the serial clock SCK to simultaneously clock data in and out of a slave device. The SPI master terminates a transfer by returning CS# high.

The SPI interface has four clocking modes, reflecting two mode signals called CPOL (clock polarity) and CPHA (clock phase). These signals are represented in the form (CPOL, CPHA). It turns out that an interface that expects positive edge SCKS and which also expects the MOSI data to be available before the first positive clock edge can operate in modes (0,0) and (1,1) without alteration. This property allows the MAX3420E to operate in mode (0,0) or (1,1) without requiring a mode pin.

Figure 4 illustrates a data transfer between a microprocessor (the MAXQ2000, described later) and the MAX3420E using SPI modes (0,0) and (1,1). The difference is the inactive level of the SCK signal, low for mode (0,0) and high for mode (1,1).

Figure 4. The top four traces show an SPI interface operating in mode (0,0). The bottom four traces show the same interface, sending the same data, but operating in SPI mode (1,1). The difference is the quiescent SCK level and when the first positive SCK edge occurs
Figure 4. The top four traces show an SPI interface operating in mode (0,0). The bottom four traces show the same interface, sending the same data, but operating in SPI mode (1,1). The difference is the quiescent SCK level and when the first positive SCK edge occurs

The MAX3420E accepts a command byte as the first byte of every transfer. The command byte contains the register number and a direction bit. The second and subsequent bytes contain data. You may be wondering what is coming out of the MAX3420E (MISO) while the command byte is being clock in (MOSI) in Figure 4. These are eight USB status bits that are available every time a command byte is clocked in. This feature is active only for interfaces that use separate data pins MISO and MOSI.

SPI code

The trick to writing general C code for the MAX3420E is to isolate the bare minimum SPI operations in a separate module, and customise only this module from SPI interface to SPI interface. At a minimum, this module needs to do only three things: initialise the SPI port, read a byte and write a byte.

The example application in this article uses a hardware SPI unit. For SPI masters that do not have one, we will first look at some generic C code for a bit-banged SPI interface.

Bit-banged SPI: The function that initialises the SPI port will change the most from processor to processor. It is responsible for assigning the particular I/O pins used by the interface, setting their directions, and then setting the initial conditions of CS=1 and SCK=0 (we are assuming a mode (0,0) SPI master). Listing 1 shows generic routines to read and write MAX3420E registers. The macros at the beginning define the low-level pin operations to drive SCK high and low, drive CS high and low, and clock data in and out using the MOSI and MISO pins. The macros insulate the functions from various I/O schemes of various microprocessors. Use of macros makes the code easy to read and processor independent. The macros in <a href= http://www.dataweek.co.za/articles/Dataweek%20-%20Published%20by%20Technews/w9292.doc " target="_blank">Listing 1</a> are for a typical older microprocessor that does not contain a hardware SPI unit.

For a clearer version click on <a href= http://www.dataweek.co.za/articles/Dataweek%20-%20Published%20by%20Technews/w9292.doc " target="_blank">Listing 1</a>

Listing 1. Bit-banging routines to read and write MAX3420E registers. By customising the low level macros these routines can be used for any &mu;P. These SPI functions use only general-purpose I/O pins
Listing 1. Bit-banging routines to read and write MAX3420E registers. By customising the low level macros these routines can be used for any μP. These SPI functions use only general-purpose I/O pins

Hardware SPI: Tom Cantrell introduced the MAXQ2000 to Circuit Cellar readers in the September 2004 issue. In brief, the MAXQ2000 is the first of a family of low power, high performance RISC processors from Dallas/Maxim. The 'Q' stands for Quiet, indicating that the architecture is designed to coexist nicely with sensitive analog circuits. From a USB interface standpoint, the MAXQ2000 is especially friendly to the MAX3420E because it has a built-in SPI port (see Figure 5).

Figure 5. The application code runs on the MAXQ2000 Evaluation Kit with a small daughter-board containing the MAX3420E. The figure shows the port-to-SPI signal assignments to serve as a reference for reading the program listing
Figure 5. The application code runs on the MAXQ2000 Evaluation Kit with a small daughter-board containing the MAX3420E. The figure shows the port-to-SPI signal assignments to serve as a reference for reading the program listing

Part-II

In the second part of this article, we will have a look at an example that uses the MAXQ2000 development kit and the MAX3420E to build a simple but interesting Windows widget. Part II of 'Add USB to anything" will follow in a subsequent issue of Dataweek.



Credit(s)



Share this article:
Share via emailShare via LinkedInPrint this page

Further reading:

SMT-mountable card connectors
Telecoms, Datacoms, Wireless, IoT
Würth Elektronik introduces four new SMT-mountable Nano SIM and microSD card connectors and expands its range with solutions for the smallest packages.

Read more...
Module for smart city and smart utility devices
iCorp Technologies Telecoms, Datacoms, Wireless, IoT
Quectel Wireless Solutions has launched the Quectel KCM0A5S, a high-performance Wi-SUN module designed for smart applications such as street lighting, precision agriculture, industrial IoT, smart meters and smart cities.

Read more...
Ultra-low-power wireless module
Altron Arrow Telecoms, Datacoms, Wireless, IoT
The STM32WBA5MMG from STMicroelectronics is an ultra-low-power, small form factor, certified 2,4 GHz wireless module that supports Bluetooth LE, Zigbee 3.0, OpenThread, and IEEE 802.15.4 proprietary protocols.

Read more...
Quectel partners with GEODNET
Quectel Wireless Solutions Telecoms, Datacoms, Wireless, IoT
Quectel Wireless Solutions has partnered with GEODNET to deliver Quectel’s Real-Time Kinematic (RTK) correction services, enabling high-precision positioning for IoT applications.

Read more...
Bringing Bluetooth Channel Sounding to automotive and beyond with KW47
Altron Arrow Telecoms, Datacoms, Wireless, IoT
NXP’s new Channel Sounding-certified KW47 and MCX W72 wireless MCUs are set to help automakers with distance measurement, bringing an additional ranging solution for car access and autonomous systems, and will be utilised across a broader spectrum of applications.

Read more...
Dual-band GNSS antenna
RF Design Telecoms, Datacoms, Wireless, IoT
The Taoglas Accura GVLB258.A, is a passive, dual-band GNSS L1/L5, high-performance antenna for high precision GNSS accuracy and fast positioning.

Read more...
What is Wi-Fi HaLow and why choose it for IoT?
iCorp Technologies Editor's Choice Telecoms, Datacoms, Wireless, IoT
Wi-Fi HaLow introduces a low power connectivity option that, in contrast to other Wi-Fi options, offers greater range of approximately 1 km, which opens up a raft of IoT use cases.

Read more...
Wi-Fi 6 and Bluetooth LE coprocessor module
Altron Arrow Telecoms, Datacoms, Wireless, IoT
The ST67W611M1 from STMicroelectronics boasts an all-in-one design which, together with its capabilities, contribute to making it an attractive choice for IoT edge devices requiring a single-chip solution.

Read more...
Futureproofing IoT connectivity
SIMcontrol Telecoms, Datacoms, Wireless, IoT
A managed private APN assigns every device to an isolated carrier slice, producing a single ingress to the enterprise network, with traffic bypassing shared internet paths and reducing exposure.

Read more...
Extra slim 2,4 GHz radio module
Telecoms, Datacoms, Wireless, IoT
The Thyone I radio module from Würth Elektronik now has a little sibling: Thyone-e, which takes up 30% less space and represents a cost-effective alternative for applications in which the long-range mode is not required.

Read more...









While every effort has been made to ensure the accuracy of the information contained herein, the publisher and its agents cannot be held responsible for any errors contained, or any loss incurred as a result. Articles published do not necessarily reflect the views of the publishers. The editor reserves the right to alter or cut copy. Articles submitted are deemed to have been cleared for publication. Advertisements and company contact details are published as provided by the advertiser. Technews Publishing (Pty) Ltd cannot be held responsible for the accuracy or veracity of supplied material.




© Technews Publishing (Pty) Ltd | All Rights Reserved