Welcome!

Microsoft Cloud Authors: Janakiram MSV, Yeshim Deniz, David H Deans, Andreas Grabner, Stackify Blog

Related Topics: Microsoft Cloud

Microsoft Cloud: 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.

More Stories By 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.


@ThingsExpo Stories
The 22nd International Cloud Expo | 1st DXWorld Expo has announced that its Call for Papers is open. Cloud Expo | DXWorld Expo, to be held June 5-7, 2018, at the Javits Center in New York, NY, brings together Cloud Computing, Digital Transformation, Big Data, Internet of Things, DevOps, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
No hype cycles or predictions of a gazillion things here. IoT is here. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, an Associate Partner of Analytics, IoT & Cybersecurity at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He also discussed the evaluation of communication standards and IoT messaging protocols, data...
Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, discussed some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he covered some of the best practices for structured team migration an...
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and B...
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, discussed how they built...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
DevOps at Cloud Expo – being held June 5-7, 2018, at the Javits Center in New York, NY – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real results. Among the proven benefits,...
@DevOpsSummit at Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, is co-located with 22nd Cloud Expo | 1st DXWorld Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
SYS-CON Events announced today that T-Mobile exhibited at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on qua...
SYS-CON Events announced today that Cedexis will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Cedexis is the leader in data-driven enterprise global traffic management. Whether optimizing traffic through datacenters, clouds, CDNs, or any combination, Cedexis solutions drive quality and cost-effectiveness. For more information, please visit https://www.cedexis.com.
SYS-CON Events announced today that Google Cloud has been named “Keynote Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Companies come to Google Cloud to transform their businesses. Google Cloud’s comprehensive portfolio – from infrastructure to apps to devices – helps enterprises innovate faster, scale smarter, stay secure, and do more with data than ever before.
SYS-CON Events announced today that Vivint to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. As a leading smart home technology provider, Vivint offers home security, energy management, home automation, local cloud storage, and high-speed Internet solutions to more than one million customers throughout the United States and Canada. The end result is a smart home solution that sav...
SYS-CON Events announced today that Opsani will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Opsani is the leading provider of deployment automation systems for running and scaling traditional enterprise applications on container infrastructure.
SYS-CON Events announced today that Nirmata will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nirmata provides a comprehensive platform, for deploying, operating, and optimizing containerized applications across clouds, powered by Kubernetes. Nirmata empowers enterprise DevOps teams by fully automating the complex operations and management of application containers and its underlying ...
SYS-CON Events announced today that Opsani to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. Opsani is creating the next generation of automated continuous deployment tools designed specifically for containers. How is continuous deployment different from continuous integration and continuous delivery? CI/CD tools provide build and test. Continuous Deployment is the means by which...