Welcome!

.NET Authors: Bruce Armstrong, Marek Miesiac, Jason Dolinger, Yeshim Deniz, Liz McMillan

Related Topics: .NET

.NET: Article

Introduction to Programming for the TinyCLR

Managed code on embedded devices

When most Windows programmers think of embedded development we typically think of the .NET Compact Framework running under Windows CE. While CE and Windows XP Embedded have been the staples of Microsoft's embedded business for years, there's a new entrant known as Smart Personal Object Technology or SPOT.

Most people, if they're aware of SPOT at all, think that it simply equates to the watches available from a handful of watchmakers, but it's actually much more. SPOT is, in a nutshell, any device based on "Ollie" - a 32-bit ARM7 processor core that runs a very, very stripped down Windows OS. What's exciting about it is that right in the processor is a very scaled-down Common Language Runtime (CLR) known as TinyCLR. This means that hobbyists and developers can write managed applications for what is essentially an embedded microcontroller.

SPOT was developed at Microsoft Research as a technology for task-specific devices. Today's Windows CE devices use expensive, high-power processors so they can do a wide range of things from taking notes to displaying maps or playing music and videos.

Below this level of device is a vast array of electronic devices that we use every day that typically do a single thing, don't need a super-fast processor, and need to be inexpensive. These devices need more logic than can be reasonably done with hardware alone, but not a lot more - things like programmable thermostats for your home furnace or an outdoor holiday light system. This is where SPOT is intended to play.

The TinyCLR: A Very Compact Framework

There's already a large array of 8-, 16- and 32-bit microcontrollers available from many manufacturers. What sets SPOT apart is that it leverages the Rapid Application Development (RAD) capabilities Microsoft provides through managed code and Visual Studio 2005. Instead of having to trudge through procedural BASIC, C, or assembly code, developers can quickly write event-driven, multithreaded applications that run in an environment that automatically takes care of memory management and garbage collection.

Folks familiar with managed code development under Windows CE know that they get access only to a subset of the classes and methods available in the Microsoft .NET Framework - what is known as the .NET Compact Framework. Likewise, since the TinyCLR has an even smaller footprint and is designed for devices with limited resources and functionality, SPOT developers have an even more compact framework to use.

While many things have been removed - TinyCLR has no support for WinForms, database access, files, or XML - it retains critical items not available for other microcontroller environments like threading, synchronization objects, RSA and TEA cryptography, and real-time debugging. It also has a whole new set of classes unique to the SPOT environment like the InputPort, OutputPort, and InterruptPort classes that we'll take a look at.

Currently the TinyCLR compiler only supports C# development (you can't even develop in unmanaged code), but as the platform expands and gains popularity, expect VB.NET and C++ to come along as well.

A HelloWorld SPOT Application

The simplest way to get a feel for the similarities of SPOT development and other managed development is to look at an introductory "Hello World" kind of application. Let's take a look at one that's similar to a desktop program, but shows some TinyCLR uniqueness.

Just like the Full and Compact Frameworks, assembly execution starts in a static method called Main. In the example in Listing 1, Main creates a class instance and then calls a method on that instance. The construct is no different than any other managed application.

The class method then outputs some text through the Debug class, which ends up back in Visual Studio's Output window. Just to show the Thread class, between Debug outputs the Thread class is used to stall execution. For Compact Framework developers this sample may be refreshing, as the Compact Framework 1.0 can't send data back to Studio. Otherwise this code is no different than what would be done in the other frameworks.

Finally the sample does something unique to TinyCLR that's easy for anyone who has done any development, managed or otherwise, to understand. It throws an Exception (which isn't the unique part) that's caught back in Main, and the Exception handler uses the Microsoft.SPOT.Debug class to dump both the current stack and the current heap - two extremely useful debugging tools - to the debug the window.

As the example in Listing 1 shows, if you're familiar with developing against either the Full Framework or the Compact Framework you'll be quite comfortable doing development against TinyCLR.

Using the SPOT Processor's Microcontroller Features

Since the SPOT processor is designed to go into applications that traditionally use other 8-, 16- and 32-bit microcontrollers, it only makes sense that most of the uniqueness that SPOT classes have are related to the workhorse functionality of microcontrollers: interrupt handling and digital I/O.

The TinyCLR has four classes that encapsulate these basic functionalities. I/O is handled with the InputPort, OutputPort, and TristatePort classes. Interrupts are handled using the InterruptPort class.

As the names suggest, the InputPort is used for digital input and the OutputPort is used for digital output. The TristatePort is used when a line must be used for input and output as well as needing a high impedance (high-Z) state. All three support turning on optional internal pull-up or pull-down resistors in the processor. Listings 2 and 3 are short code snippets demonstrating the use of InputPort and OutputPort respectively. Of interest is the CPU class that contains an enumeration for every pin on the processor.

Since a TristatePort is inherently more complex than simple input or output, the code to use it is also a bit more complex, though not overwhelming. The most important aspect to note is that setting the TristatePort.Active property to the value already set (setting it true when already true or false when already false) will throw an exception, so to be safe your code must always check the property before setting it. Listing 4 is a short snippet showing the use of the TristatePort.

The final area that we'll look at is interrupt handling, which is one of the most common features used in microcontroller and embedded applications. When your application needs to be "notified" when the state of a digital line changes without having to poll it constantly, you set up an interrupt. In the TinyCLR this is done with an InterruptPort. The SPOT processor supports both edge and level interrupts and can be used in conjunction with the processor's internal pull up/down resistors. Listing 5 offers an example of setting up an interrupt and EventHandler that will get called with every interrupt event.

Conclusion

The TinyCLR running on the SPOT processor offers OEMs and developers a highly advanced operating environment. Coupled with the industry-leading Rapid Application Development features of Microsoft Visual Studio 2005, it's possible to deliver stable and robust blue-collar, function-specific embedded solutions in a very short time. In a world where time-to-market and reliability are two major factors in product success, these technologies provide a window of opportunity for a whole new generation of developers to move into embedded computing.

About Chris Tacke

Chris Tacke is a partner with OnWard Technology, a company specializing in smart sensors and SPOT development, and cofounder of OpenNETCF.org, an open source initiative for the .NET Compact Framework. Chris currently lives in Maryland with his wife Erin and two children, Olivia and Garrett.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.