A Trace Tool for Embedded Systems

Tracing tools monitor what is going in a program’s execution by logging low-level and frequent events. Thus tracing can detect and help debug performance issues in embedded system applications.

In Circuit Cellar’s April issue, Thiadmer Riemersma describes his DIY tracing setup for small embedded systems. His system comprises three parts: a set of macros to include in the source files of a device under test (DUT), a PC workstation viewer that displays retrieved trace data, and a USB dongle that interfaces the DUT with the workstation.

Small embedded devices typically have limited-performance microcontrollers and scarce interfaces, so Riemersma’s tracing system uses only a single I/O pin on the microcontroller.

Designing a serial protocol that keeps data compact is also important. Riemersma, who develops embedded software for the products of his Netherlands-based company, CompuPhase, explains why:

Compactness of the information transferred from the embedded system to the workstation [which decodes and formats the trace information] is important because the I/O interface that is used for the transfer will probably be the bottleneck. Assuming you are transmitting trace messages bit by bit over a single pin using standard wire and 5- or 3.3-V logic levels, the transfer rate may be limited to roughly 100 Kbps.

My proposed trace protocol achieves compactness by sending numbers in binary, rather than as human-readable text. Common phrases can be sent as numeric message IDs. The trace viewer (or trace ‘listener’) can translate these message IDs back to the human-readable strings.

One important part of the system is the hardware interface—the trace dongle. Since many microcontrollers are designed with only those interfaces used for specific application needs, Riemersma says, typically the first step is finding a spare I/0 pin that can be used to implement the trace protocol.

In the following article excerpt, Riemersma describes his trace dongle and implementation requiring a single I/O pin on the microcontroller:

This is the trace dongle.

This is the trace dongle.

Photo 1 shows the trace dongle. To transmit serial data over a single pin, you need to use an asynchronous protocol. Instead of using a kind of (bit-banged) RS-232, I chose biphase encoding. Biphase encoding has the advantage of being a self-clocking and self-synchronizing protocol. This means that biphase encoding is simple to implement because timing is not critical. The RS-232 protocol requires timing to stay within a 3% error margin; however, biphase encoding is tolerant to timing errors of up to 20% per bit. And, since the protocol resynchronizes on each bit, error accumulation is not an issue.

Figure 1 shows the transitions to transmit an arbitrary binary value in biphase encoding—to be more specific, this variant is biphase mark coding. In the biphase encoding scheme, there is a transition at the start of each bit.

Figure 1: This is an example of a binary value transferred in biphase mark coding.

Figure 1: This is an example of a binary value transferred in biphase mark coding.

For a 1 bit there is also a transition halfway through the clock period. With a 0 bit, there is no extra transition. The absolute levels in biphase encoding are irrelevant, only the changes in the output line are important. In the previous example, the transmission starts with the idle state at a high logic level but ends in an idle state at a low logic level.

Listing 1 shows an example implementation to transmit a byte in biphase encoding over a single I/O pin. The listing refers to the trace_delay() and toggle_pin() functions (or macros). These system-dependent functions must be implemented on the DUT. The trace_delay() function should create a short delay, but not shorter than 5 µs (and not longer than 50 ms). The toggle_pin() function must toggle the output pin from low to high or from high to low.

For each bit, the function in Listing 1 inverts the pin and invokes trace_delay() twice. However, if the bit is a 1, it inverts the pin again between the two delay periods. Therefore, a bit’s clock cycle takes two basic “delay periods.”

Listing 1: Transmitting a byte in biphase encoding, based on a function to toggle an I/O pin, is shown.

Listing 1: Transmitting a byte in biphase encoding, based on a function to toggle an I/O pin, is shown.

The biphase encoding signal goes from the DUT to a trace dongle. The dongle decodes the signal and forwards it as serial data from a virtual RS-232 port to the workstation (see Photo 2 and the circuit in Figure 2).

Photo 2: The trace dongle is inserted into a laptop and connected to the DUT.

Photo 2: The trace dongle is inserted into a laptop and connected to the DUT.

This trace dongle interprets biphase encoding.

Figure 2: This trace dongle interprets biphase encoding.

The buffer is there to protect the microcontroller’s input pin from spikes and to translate the DUT’s logic levels to 5-V TTL levels. I wanted the trace dongle to work whether the DUT used 3-, 3.3-, or 5-V logic. I used a buffer with a Schmitt trigger to avoid the “output high” level of the DUT at 3-V logic, plus noise picked up by the test cable would fall in the undefined area of 5-V TTL input.

Regarding the inductor, the USB interface provides 5 V and the electronics run at 5 V. There isn’t room for a voltage regulator. Since the USB power comes from a PC, I assumed it might not be a “clean” voltage. I used the LC filter to reduce noise on the power line.

The trace dongle uses a Future Technology Devices International (FTDI) FT232RL USB-to-RS-232 converter and a Microchip Technology PIC16F1824 microcontroller. The main reason I chose the FT232RL converter is FTDI’s excellent drivers for multiple OSes. True, your favorite OS already comes with a driver for virtual serial ports, but it is adequate at best. The FTDI drivers offer lower latency and a flexible API. With these drivers, the timestamps displayed in the trace viewers are as accurate as what can be achieved with the USB protocol, typically within 2 ms.

I chose the PIC microcontroller for its low cost and low pin count. I selected the PIC16F1824 because I had used it in an earlier project and I had several on hand. The microcontroller runs on a 12-MHz clock that is provided by the FTDI chip.

The pins to connect to the DUT are a ground and a data line. The data line is terminated at 120 Ω to match the impedance of the wire between the dongle and the DUT.

The cable between the DUT and the trace dongle may be fairly long; therefore signal reflections in the cable should be considered even for relatively low transmission speeds of roughly 250 kHz. That cable is typically just loose wire. The impedance of loose wire varies, but 120 Ω is a good approximate value.

The data line can handle 3-to-5-V logic voltages. Voltages up to 9 V do not harm the dongle because it is protected with a Zener diode (the 9-V limit is due to the selected Zener diode’s maximum power dissipation). The data line has a 10-kΩ to 5-V pull-up, so you can use it on an open-collector output.

The last item of interest in the circuit is a bicolor LED that is simply an indicator for the trace dongle’s status and activity. The LED illuminates red when the dongle is “idle” (i.e., it has been enumerated by the OS). It illuminates green when biphase encoded data is being received.

After the dongle is built, it must be programmed. First, the FT232RL must be programmed (with FTDI’s “FT Prog” program) to provide a 12-MHz clock on Pin C0. The “Product Description” in the USB string descriptors should be set to “tracedongle” so the trace viewers can find the dongle among other FTDI devices (if any). To avoid the dongle being listed as a serial port, I also set the option to only load the FTDI D2XX drivers.

To upload the firmware in the PIC microcontroller, you need a programmer (e.g., Microchip Technology’s PICkit) and a Tag-Connect cable, which eliminates the need for a six-pin header on the PCB, so it saves board space and cost.

The rest of the article provides details of how to create the dongle firmware, how to add trace statements to the DUT software being monitored, and how to use the GUI version of the trace viewer.

The tracing system is complete, but it can be enhanced, Riemersma says. “Future improvements to the tracing system would include the ability to draw graphs (e.g., for task switches or queue capacity) or a way to get higher precision timestamps of received trace packets,” he says.

For Riemersma’s full article, refer to our April issue now available for membership download or single-issue purchase.

Serial Carrier Card with Flexible I/O and Serial Technology

New 3U CompactPCI Serial Carrier Card from MEN Micro IntegratesThe G204 is a 3U CompactPCI Serial carrier card with an M-Module slot that combines fast CompactPCI Serial technology with flexible I/O options. The card serves as the basis for powerful 19″-based system solutions for transportation and industrial applications (e.g., data acquisition, process control, automation and vehicle control, robotics or instrumentation).

M-Modules are modular I/O extensions for industrial computers (e.g., embedded systems and high-end workstations). The M-Module slot enables users to interchange more than 30 I/O functions within a system. The M-Module, which needs only one CompactPCI Serial slot, is screwed tightly onto the G204 and does not require a separately mounted transition panel.

The G204 modular mezzanine card operates in a –40°C to 85°C extended temperature range for harsh environments and costs $483.

MEN Micro Inc.
www.menmicro.com

Open-Source Guide for Embedded Systems Developers (EE Tip #114)

What comes to mind when you hear the term “open source”? Hopefully, it means more to you than just a software application running on a PC.

As an embedded systems developer, you should familiarize yourself with the wide range of open-source programs, programming tools, and hardware platforms currently available. In addition to saving yourself the costs of pricey user licenses, you’ll find that open-source community forums helpful, informative, and engaging.

Open-source software offers a number of advantages. The product is independent of a particular manufacturer and there aren’t license costs. Plus, the product is usually high quality because it is often supported by a large active community of users. When a program’s source code is available, you have the chance to fix errors, change its behavior, and even add new features.

The aforementioned advantages should be good enough reasons for any designer of microcontroller applications to work with open-source software. PC tools such as editors, documentation programs, toolchains (for the vast majority of microcontrollers), operating systems, and libraries are widely available with open-source code.

On the hardware side, open-source microcontroller boards are gaining popularity among serious engineers. The circuits, PCBs, and CAD files are available so you can modify them, improve them, and add more features to meet the demands of your applications. It’s an added benefit that open-source hardware is always supported by software code and libraries that enable you to get up and running fairly quickly.

Since we couldn’t include in the space provided all the open-source resources currently available, we simply list several open-source projects that Elektor and Circuit Cellar engineers and editors recommend.

Below we provide the following lists: hardware; libraries and run-time tools; PC tools, and GNU toolchains. By no means are the lists complete. Still, they’re helpful starting points.

Download your Arduino Uno poster

Click image to download a free Arduino Uno poster

Arduino—This popular platform offers a range of simple microcontroller and development boards that you can purchase from several suppliers. The Arduino website has an active forum and the wide range of software examples will ensure that you are up and running in minimum time.

Openmoko—It’s a complete software stack for a smart. The Neo FreeRunner mobile phone is the target hardware platform. Development and debug boards are also available.

GNU Radio & Universal Software Radio Peripheral—The GNU Radio project is a software toolkit to produce a software-defined radio. The open-source hardware for this project is the Universal Software Radio Peripheral (USRPBoard), which is based on an FPGA.

KiCAD—One of the best-known suites of CAD programs for hardware production, KiCAD includes tools for generating circuit diagrams and PCBs. You can view 3-D representations of the finished board.

Fab Lab—This interesting project offers 3-D laser cutters, 3-D printers, and other machines for use by the general public. It’s a handy resource for making robot parts and art objects.

uIP/lwIP—Two outstanding network stacks, the first is for 8-bit microcontrollers. lwIP is a development of the first and more suited to medium sized controllers. The uIP licence is not so strict allowing the stack to be used in commercial products.

LUFA (formally MyUSB)—A large library of applications for interfacing (both Host and Device) USB enabled AVR controllers. The demonstration applications allow an AVR controller for example to emulate a keyboard and many other devices (mass storage device, audio I/O etc.)OpenSource2

Crypto-avr-lib—It’s a library of optimized cryptographic routines for the Atmel ATmega controller. Issued under the GPL Version 3 licence. Contact the author for other types of licence.

FreeRTOS—FreeRTOS is a lightweight Real Time kernel which can run on many controller families. It can be used in commercial applications and allows the use of closed-source software.

U-Boot—Universal bootloader with a large range of routines for memory, UART interface, SD card, network and USB etc. Conceived originally as a bootloader but now through comprehensive hardware support can be used as the basis of a C code module.

Embedded Filesystems Library—A useful (FAT) file format, when you are short of memory. The GPL licence includes a clause allowing static linking to the library without public disclosure of your code.

.NET Micro Framework—Now open source this very compact, trimmed down .NET Framework running on diverse ARM platforms. Programmable using the object orientated C variant C#; lots of resources including support for I2C, Ethernet and many more. Helps reduce development time.

Eclipse—This is a good development environment. It has a modular structure which makes it very easy to configure. There are around 1,000 plug-in modules (both open source and commercial) for a range of program languages and target systems.

Kdevelop—Kdevelop is an integrated development environment which should satisfy most power-user needs. Runs in MS Windows, Mac OsX, Linux, Solaris and FreeBSD. Plug-in expandable.

Programmer’s Notepad—A lightweight but efficient editor for writing source code. Allows fast, simple and comfortable program production. Can be expanded with plug-ins.

Doxygen—An intelligent tool which can automatically generate code documentation (C, C++, Java etc.). The programmer provides tags in the source file; Doxygen generates the comprehensive documentation in PDF or HTML format. It can also extract the code structure from undocumented source files.

WinMerge—A good tool for code comparison and code synchronization. The program can also compare the contents of folders/files and display the results in a visual text format that makes it easy to understand.

Tera Term—A terminal program to access COM ports, supports Telnet communication Protocol. A debugging tool to eavesdrop on serial communications.

Note: Toolchains for GNU projects are available most processor architectures AVR, Coldfire, ARM, MIPS, PowerPC and Intel x86. The GNU-toolchain includes not only compilers for C, C++ and in most cases also Java (GCC = GNU Compiler Collection), but also Linkers, Assemblers and Debuggers together with C libraries (libc = C library). The tools are used from within other-open source projects, like WinAVR, which provides a familiar user interface to speed up program development.

One Desk Serves Two Roles for Professor and Designer

Chris Coulston, head of the Computer Science and Software Engineering department at Penn State Erie, The Behrend College, has a broad range of technical interests, including embedded systems, computer graphics algorithms, and sensor design.

Since 2005, he has submitted five articles for publication in Circuit Cellar, on projects and topics ranging from DIY motion-controlled gaming to a design for a “smart” jewelry pendant utilizing RGB LEDs.

We asked him to share photos and a description of the workspace in his Erie, PA, home. His office desk (see Photo 1) has something of an alter ego. When need and invention arise, he reconfigures it into an “embedded workstation.”

Coulston's workspace configured as an office desk

Photo 1: Coulston’s workspace configured as an office desk

When working on my projects, my embedded workstation contains only the essential equipment that I need to complete my project (see Photo 2).  What it lacks in quantity I’ve tried to make up for in quality instrumentation; a Tektronix TDS 3012B oscilloscope, a Fluke 87-V digital multimeter, and a Weller WS40 soldering iron.  While my workstation lacks a function generator and power supply, most of my projects are digital and have modest power requirements.

Coulston can reconfigure his desk into the embedded workstation pictured here.

Photo 2: Coulston can reconfigure his desk into the embedded workstation pictured here.

Coulston says his workspace must function as a “typical office desk” 80 percent of the time and electronics station 20 percent of the time.

It must do this while maintaining some semblance of being presentable—my wife shares a desk in the same space. The foundation of my workstation is a recycled desk with a heavy plywood backing on which I attached shelving. Being a bit clumsy, I’ve tried to screw down anything that could be knocked over—speakers, lights, bulletin board, power strip, cable modem, and routers.

The head of a university department has different needs in a workspace than does an electronics designer. So how does Coulston make his single office desk suffice for both his professional and personal interests? It’s definitely not a messy solution.

My role as department chair and professor means that I spend a lot time grading, writing, and planning. For this work, there is no substitute for uncluttered square footage—getting all the equipment off the working surface. However, when it’s time to play with the circuits, I need to easily reconfigure this space.

I have found organization to be key to successfully realize this goal. Common parts are organized in a parts case, parts for each project are put in their own bag, the active project is stored in the top draw, frequently used tools, jumper wires, and DMM are stored in the next draw. All other equipment is stored in a nearby closet.

I’ve looked at some of the professional-looking workspaces in Circuit Cellar and must admit that I am a bit jealous. However, when it comes to operating under the constraints of a busy professional life, I have found that my reconfigurable space is a practical compromise.

To learn more about Coulston and his technical interests, check out our Member Profile posted earlier this year.

 

Chris Coulston

Chris Coulston

CC 277: Using Files in Concurrent Linux Designs

In the August issue of Circuit Cellar, columnist Robert Japenga, who has been designing embedded systems since 1973, wraps up his eight-part series on the benefits and challenges of designing concurrency into your systems and some of the specific tools Linux provides for IPC.

His final installment discusses file usage. It also recounts how the development of read/write nonvolatile memory (i.e., flash technology) enabled embedded systems to contain cost-competitive file systems.

“Disk drives in the early days were too big and weren’t reliable enough for embedded systems. The first real disk drive I used in 1975 was a Digital Equipment RK-05 for a PDP-11 that held an amazing 2.5 MB of data,” Japenga says in his column. “The RK-05 was released in 1972. It initially weighed 100 lbs. The $74 monthly maintenance cost would buy a 1-TB drive today or 12 per year.

In 1972, a Digital Equipment RK-05 disk drive held only 2.5 MB of data. (Photo courtesy of Mark Csele)

“In 1977, a friend from Bell Labs carried an RK-05 with a copy of Unix onto a plane. At the gate, the inspector opened the lid and put his finger on the magnetic platter. Whoops. The disk gloriously crashed when inserted into my disk drive. It seemed I would have to wait for my first copy of Unix.

“”For a time, companies produced hardened disk drives. The cost was very prohibitive and the reliability was questionable. Then in 2001, the iPod changed all that when Apple used Toshiba’s 1.8” hard drive, which is only 0.2” thick. As a consumer product, it had to be extremely rugged. Very small embedded systems now had hard drives.

“But not all of us built millions of systems, nor could we afford to put a hard disk in our temperature controllers, motion-control devices, or avionics boxes. However, with the advent of read/write nonvolatile memory (i.e., flash technology), embedded systems now had a way to contain cost-competitive file systems. This paved the way for putting real OSes into embedded systems. In the late 1990s and later, we were putting DOS on a flash card. Well, not everything was a real OS! And that is where Linux comes into the picture.”

Japenga’s column goes on to discuss file systems and the mechanisms to create concurrent systems, including nonvolatile flag files, volatile flag files, data sharing, and event triggering. It concludes with a thorough discussion of some of the risks of using a file system in a concurrent system.

“Modern embedded systems are doing more than I ever imagined when I started out,” Japenga says. “Adding a file system to your design can provide significant advantages to improve your product. As with all OS functions, we need to understand how our file system works if we are going to use it properly—especially in systems with concurrency.”

For more, check out  Japenga’s column, Embedded in Thin Slices, in Circuit Cellar‘s August issue.