What Is an Arduino Shield and Why Should My Netduino Care?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
C9 Netduino Shield Series - Using Arduino Shield with Netduino - Part I http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/clip_image001_thumb1%5B2%5D.jpg <img title="clip_image001_thumb1" border="0" alt="clip_image001_thumb1" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/clip_image001_thumb1_thumb.jpg" width="236" height="240
Photo courtesy of http://tronixstuff.wordpress.com/ John Boxall <h3>Introduction</h3> When the http://www.arduino.cc/en/Guide/Introduction Arduino Duemilanove microcontroller appeared in 2005, it featured a set of female pin headers exposing most of the pins of the ATmega168 for easy hacking and for connecting accessory boards known as Shields. The purpose of a shield is to provide new plug-and-play functionality to the host microcontroller, such as circuit prototyping, motion control, sensor integration, network and radio communication, or gaming interfaces, without worrying too much about the hardware implementation details. Seven years after the birth of the original Arduino, new shields keep coming out and are being cataloged on http://shieldlist.org/ http://shieldlist.org/ , a testament to the versatility of the design. It is also simple to build a DIY shield when nothing out there will meet your needs or when you want to understand how the shield concept works from the ground up. In their infinite wisdom, Secret Labs, the makers of the http://www.netduino.com/ Netduino , adopted the same Arduino form factor and header pin out to let .NET Micro Framework users tap into the vast array of Arduino shields on the market. Yet, theres always one hurdle that needs to be overcome: in order to give access to their hardware functions easily, many Arduino shields provide http://www.arduino.cc/playground/Main/GeneralCodeLibrary sample sketches (a.k.a. C/C++ http://wiring.org.co/ Wiring programs ) and http://arduino.cc/en/Reference/Libraries libraries also http://arduino.cc/en/Reference/HomePage written in C/C++ designed to be used within the http://arduino.cc/en/Guide/Windows#toc6 Arduino IDE . Unfortunately, Arduino sketches and libraries arent compatible with the Visual Studio environment and the .NET runtime: they need to be adapted in order to make use of the shields hardware. The goal of this series of articles is to attempt to demystify what an Arduino shield is. To do this, well start by building one from scratch. Then, well present a generic approach for selecting Netduino-compatible Arduino shields and adapting Arduino code to interface with the .NET Micro Framework. <h5>Selecting Arduino shields for use with Netduino / Plus</h5> Arduino shields using hardware components interfaced over protocols such as http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus SPI , http://www.i2c-bus.org/ I2C , http://www.maxim-ic.com/products/1-wire/flash/overview/index.cfm 1-Wire or serially over a http://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter UART are generally good candidates for a Netduino adaptation because the .NET Micro Framework supports these protocols natively and can do bulk I/O transfers, yielding good performance. However, if a shield requires http://en.wikipedia.org/wiki/Bit_banging bit-banging data on GPIOs (i.e. communicating data serially, such as SPI, I2C, RS-232, in software by toggling GPIO lines individually instead of using dedicated hardware interfaces) or has very low I/O latency requirements, it may not be practical or even possible to use the shield on a Netduino / Plus. The reason for this is simple: the Arduino runs native code and can access hardware registers directly with a latency orders of magnitude lower than what the .NET Micro Framework can achieve on a Netduino / Plus where toggling GPIOs means going through multiple framework layers while running interpreted code. The maximum raw I/O toggling frequency on a Netduino / Plus clocked at 48 MHz http://fabienroyer.wordpress.com/2011/01/22/netduino-gpio-speed-test/ was measured around ~8.4KHz which is slow compared to the few MHz achievable on an Arduino clocked at 16 MHz. Fortunately, there are possible workarounds and strategies Whenever an Arduino library bit-bangs SPI data on GPIOs, regardless of the pins used to do it, it can be replaced by using the standard SPI interface on the Netduino / Plus. The Netduino / Plus has significantly more RAM available than the Arduino and can transfer larger SPI data buffer in a single shot, dramatically reducing I/O latency. For instance, http://fabienroyer.wordpress.com/2011/09/14/driving-an-adafruit-st7565-negative-lcd-display-with-a-netduino/ driving a display shield would greatly benefit from this method where a frame is cached in RAM on the Netduino / Plus then sent in one shot over SPI to the hardware display driver. The bottom line is that it is always a good idea to review how the Arduino code interfaces with a given shield before proceeding with a purchase or starting a conversion project. This review process is also facilitated by http://shieldlist.org/ http://shieldlist.org which shows which pins are being used to drive a shield. For example, the http://shieldlist.org/adafruit/logger Adafruit Industries Logger Shield uses hardware SPI (pins D10-D13) for the SD card and I2C (pins A4-A5) for the DS1307 real time clock, making it a perfect adaptation candidate for the Netduino / Plus as well see in our next article. http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image%5B18%5D.png <img title="image" border="0" alt="image" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image_thumb%5B8%5D-6.png" width="250" height="196 http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image%5B19%5D-2.png <img title="image" border="0" alt="image" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image_thumb%5B9%5D-1.png" width="250" height="224 Its also very important to know what features are available on the .NET Micro Framework running on the Netduino / Plus: some things come standard with the .NET Micro Framework which normally require add-ons libraries on the Arduino such as file system and networking support. Finally, it is wise to search code repositories such as http://www.codeplex.com/ CodePlex , https://bitbucket.org/ BitBucket and https://github.com/ GitHub for hardware drivers and libraries before undertaking writing one in C# from scratch based on component datasheets. This extends to the http://forums.netduino.com/index.php?/forum/15-project-showcase/ Netduino and the http://www.tinyclr.com/forum TinyCLR community forums where many people have posted drivers in discussion threads. <h4>Building a shield from scratch to meet specific requirements</h4> Recently, we were asked if it was possible to connect our http://nwazet.com/touch-display-module Touch Display module designed for a http://www.netduino.com/netduinogo/specs.htm Netduino Go! to a http://www.netduino.com/netduino/specs.htm Netduino / http://www.netduino.com/netduinoplus/specs.htm Plus . By doing a bit of research and seeing how the driver for the Touch Module works, this can be done by building a simple custom shield composed of a few parts connecting the Netduino pin headers to a Netduino Go! socket breakout board like so: http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/FinishedCustomShield%5B3%5D.jpg <img title="FinishedCustomShield" border="0" alt="FinishedCustomShield" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/FinishedCustomShield_thumb%5B1%5D.jpg" width="250" height="166 http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/GoTouchDisplayOnNetduino%5B3%5D.jpg <img title="GoTouchDisplayOnNetduino" border="0" alt="GoTouchDisplayOnNetduino" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/GoTouchDisplayOnNetduino_thumb%5B1%5D.jpg" width="250" height="267 <h5>High-Level Software Interface</h5> To frame the rest of this article, the communication interface with the shield is done using SPI and a GPIO used for synchronization. The communication protocol details are wrapped in the <i>Nwazet.Go.Display.Imaging.VirtualCanvas</i> class which needs to be initialized with the SPI and GPIO parameters matching the pin out of the shield: <pre><pre class="brush: csharp
var canvas = new VirtualCanvas(TouchEventHandler, WidgetClickedHandler);

canvas.Initialize(
displaySpi: SPI.SPI_module.SPI1,
displayChipSelect: Pins.GPIO_PIN_D9,
displayGPIO: Pins.GPIO_PIN_D8,
speedKHz: 5000);
[/code]

[/code] From that point on, all interactions with the shield are done through <i>Draw*</i> methods followed by a call to the <i>Execute() </i>method which handles sending and receiving SPI data packets. The <i>VirtualCanvas</i> class also provides methods for creating widgets such as buttons, receiving touch screen events and setting the screen orientation. For example, the following code snippet draws and updates a progress bar and uses one of the proportional fonts built into the display: <pre><pre class="brush: csharp
public static void Main() {
var canvas = new VirtualCanvas();
canvas.Initialize(
displaySpi: SPI.SPI_module.SPI1,
displayChipSelect: Pins.GPIO_PIN_D9,
displayGPIO: Pins.GPIO_PIN_D8,
speedKHz: 5000);
var fontInfo = new DejaVuSans9().GetFontInfo();
canvas.DrawFill(0xFFFF);
canvas.DrawString(5, 144, (ushort)BasicColor.Black, fontInfo.ID, "Progress");
for (var progress = 1; progress <= 100; progress++) {
canvas.DrawProgressBar(
70, 140,
75, 12,
RoundedCornerStyle.All,
RoundedCornerStyle.All,
(ushort)BasicColor.Black,
(ushort)GrayScaleValues.Gray_128,
(ushort)GrayScaleValues.Gray_30,
(ushort)BasicColor.Green,
progress);
Thread.Sleep(100);
canvas.Execute();
}
}
[/code]

[/code]<h5>Netduino Go! Socket Pin Out</h5> In order to build the shield, we need to start with mapping out the connections between the Go! bus socket and the header pins of the shield. The Go! socket pin out was designed by Secret Labs to be compatible with http://gadgeteer.codeplex.com/releases/view/72208 Gadgeteer modules supporting the following socket types : S (SPI), U (UART), X (3 GPIO) as documented in the http://gadgeteer.codeplex.com/downloads/get/273357 .NET Gadgeteer Mainboard Builders Guide v 1.8, page 6 . This makes our little shield compatible with a Gadgeteer board as long as it is connected to an S socket. As far as other .NET Micro Framework boards are concerned, they should also work fine if they can drive an SPI slave device at 5 MHz, the minimum SPI clock frequency required by the Touch Display module. <b><i></i></b> <h6>Gadgeteer Socket Types</h6> http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image%5B2%5D-3.png <img title="image" border="0" alt="image" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image_thumb-3.png" width="500" height="433 <h6>Netduino Go! Socket</h6> http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image%5B5%5D-5.png <img title="image" border="0" alt="image" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image_thumb%5B1%5D-2.png" width="500" height="375 The http://www.netduino.com/netduino/specs.htm hardware specifications of the Netduino / Plus tells us that the Netduino GPIOs correspond exactly to the http://arduino.cc/en/Main/ArduinoBoardUno Arduino Uno GPIOs : digital pins 0-1: UART 1 RX, TX digital pins 2-3: UART 2 RX, TX digital pins 5-6: PWM, PWM digital pins 7-8: UART 2 RTS, CTS digital pins 9-10: PWM, PWM digital pins 11-13: SPI MOSI, MISO, SPCK analog pins 4-5: I2C SDA, SCL With this information, we can proceed with the creation of schematics to bridge the gap between the two worlds. <h5>Shield schematics</h5> http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/What%20is%20a%20shield%20schematics%5B2%5D-1.png <img title="What is a shield schematics" border="0" alt="What is a shield schematics" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/What%20is%20a%20shield%20schematics_thumb-1.png" width="500" height="350 <h5>How does this custom shield work?</h5> The Netduino Go! bus is designed around the http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus SPI protocol (see Go! socket pins 6-10) which the Netduino / Plus can speak easily, making a Netduino Go! module or a http://www.ghielectronics.com/catalog/category/275/ S-type Gadgeteer module appear like any other SPI device to a Netduino / Plus. You will notice that the following pins arent connected on the Go! socket for the sake of simplification: Pin 2 (5v power): the Touch Display module operates on 3.3v, therefore the 5v power supply is not needed but could be connected to the 5v header of the Netduino to be fully compliant. Pin 4 and 5 (UART): the Touch Display module does not make use of the serial interface during normal operation, so these lines can remain disconnected. However, to be fully compliant, Pin 4 (RX) and 5 (TX) on the Go! socket could be respectively connected to Netduino pin D1 (TX) and D0 (RX). The fact that these lines arent connected in this specific scenario would cause another module requiring a UART and/or 5v to not work obviously. While the SPI part is straight forward, theres a bit more to the Go! bus electrical specification that must be taken into account: the Netduino Go! bus also uses its GPIO pin on boot (see Go! socket pin 3) to control the behavior of modules based on ARM Cortex chips: when the GPIO pin is held HIGH on module power-up, the ARM Cortex chip enters its bootloader mode and waits to be flashed with new firmware over the Go! bus UART (see Go! socket pins 4-5). The ARM Cortex bootloader remains active until the ARM Cortex chip is reset or power-cycled. This is important to know for two reasons: <ol>The Touch Display module is powered by an ARM Cortex M3 chip. On boot, a Netduino / Plus sets all of its I/O pins HIGH, preventing the Touch Display from booting its firmware, which is undesirable. The reason for the I/O pins being set HIGH is documented on http://www.atmel.com/Images/doc6120.pdf page 5 of the ATMEL AT91SAM7X datasheet which states that the pins on Parallel IO Controller A and B (PA0 - PA30 and PB0 - PB30) are pulled-up input at reset. The pins stay in that HIGH state until theyre reconfigured by the Tiny CLR and the C# application itself. </ol> http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/clip_image002%5B4%5D.gif <img title="clip_image002" border="0" alt="clip_image002" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/clip_image002_thumb%5B4%5D.gif" width="500" height="273 For this reason, the custom shield is designed to control the power supply of the Touch Display module through a general-purpose http://www.onsemi.com/pub_link/Collateral/2N4403-D.PDF 2N4403 http://www.onsemi.com/pub_link/Collateral/2N4403-D.PDF PNP transistor (see T1_PNP on the shield schematics) connected to pin D7: it is only when the base of the transistor is asserted LOW that power flows to the Touch Display module, countering the behavior of the Netduino / Plus on boot. In addition, the Netduino / Plus controls the state of the Go! bus GPIO pin through D8, asserting it LOW before powering up the Touch Display module. About the 2N4403 transistor: any other PNP transistor, or a http://www.electronics-tutorials.ws/transistor/tran_6.html P-Channel MOSFET , capable of switching at least 150mA continuously, the maximum current consumption of the touch display with some headroom, would have worked equally as well. The 2N4403 used here is more than adequate: http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image%5B8%5D-1.png <img title="image" border="0" alt="image" src="http://files.channel9.msdn.com/wlwimages/1932b237046e4743a4e79e6800c0220f/image_thumb%5B2%5D-4.png" width="356" height="337 In the context of this shield, the PNP transistor is used as a switch, either fully ON or OFF, as opposed to being used as an amplifier. To this end, a 1K ohm resistor is used between the base of the transistor and Netduino / Plus pin D7 (POWER_CTRL) to ensure that the transistor is fully saturated but also limiting the maximum current sunken through pin D7 and potential damage being done to the ATMEL chip. The 10K ohm pull-up resistor placed between the base of the transistor and the 3.3v power rail ensures that the transistor will always be turned off by default, even when pin D7 is floating (i.e. not specifically declared as an Input or as an Output and driven HIGH or LOW by the C# application). The PowerUpDisplay function summarizes the interaction with the module through the shield on boot: <pre><pre class="brush: csharp
// Pin connected to the transistor controlling the displays power supply
public static OutputPort PowerTransistor = new OutputPort(Pins.GPIO_PIN_D7, true);

public static void PowerUpDisplay() {
// Ensure that the GPIO pin in low to prevent the display module to start in bootloader mode
var goBusGPIO = new OutputPort(Pins.GPIO_PIN_D8, false);
// Power up the display module
PowerTransistor.Write(false);
// Always wait 250ms after power-up to ensure that the display module is fully initialized before sending commands
Thread.Sleep(250);
goBusGPIO.Dispose();
}
[/code]

[/code] <b></b> <h5>Refactoring a Netduino Go! C# driver to work on a regular Netduino / Plus</h5> The final step to get the Touch Display module working outside of its natural environment involved refactoring its https://bitbucket.org/fabienroyer/nwazet.go/src/b24909aff18e/Library/Nwazet.Go.SDT028ATFTLCDTS/Nwazet.Go.SDT028ATFTLCDTS/Imaging/VirtualCanvas.cs C# driver (VirtualCanvas.cs) , eliminating the idiosyncrasies specific to Netduino Go! bus. http://www.netduino.com/downloads/ Go! modules are supposed to implement the <i>GoModule</i> interface as defined by the Netduino Go! SDK . Because the regular Netduino / Plus SDK has no knowledge of this interface, its necessary to strip out any references and API calls specific to the Go! framework, leaving only whats absolutely required to communicate with the Touch Display module over SPI and maintaining the disposable nature of the class. For more details on Go! SDK, please refer to http://forums.netduino.com/index.php?/forum/26-netduino-go/ the Netduino Go! community forums and http://wiki.netduino.com/List-of-all-GO-Modules.ashx the source code of the existing module drivers as it is currently the best source of documentation. This step is akin to the work required when adapting an Arduino library to run on the .Net Micro Framework, only in this case, the task is easier since were dealing with a C# driver to begin with instead of going from C/C++ to C#. Starting from the top of VirtualCanvas.cs, the following changes were made in the Netduino / Plus version of the driver: Removed <i>using GoBus;</i> Replaced <i>public class VirtualCanvas : GoModule</i> <i>with: public class VirtualCanvas : IDisposable</i> Replaced <i>public void Initialize(GoSocket socket, uint speedKHz = 25000)</i> <i>with: public void Initialize(SPI.SPI_module displaySpi, Cpu.Pin displayChipSelect, Cpu.Pin displayGPIO, uint speedKHz = 25000). The core of the function simply initializes the SPI interface.</i> Replaced <i>protected override void Dispose(bool disposing)</i> <i>with: public void Dispose() also removing the following calls:</i> <i>SetSocketPowerState(false); this call is used by the GoModule object model to control when a socket is powered ON / OFF and is no longer applicable when the VirtualCanvas class is not derived from GoModule.</i> <i>base.Dispose(disposing); by the same token, deriving from GoModule requires overriding the behavior of the Dispose function and calling the base class at the end, something that is no longer needed now that the class implements the regular IDisposable interface.</i> Moved <i>GoBusIrqEvent.Reset(); from WaitUntilGoBusIrqIsAsserted()</i> <i>to: Execute(Synchronicity sync = Synchronicity.Synchronous) taking into account the higher latency of the Netduino / Plus</i> The rest of the code remained unchanged. https://bitbucket.org/fabienroyer/nwazet.channel9.articles/src/d33fe2c170e9/WhatIsAShield/NetduinoPlusTouchDisplay The Netduino / Plus Touch Display driver and test application for this custom shield can be downloaded from the https://bitbucket.org/fabienroyer/nwazet.channel9.articles/src [nwazet BitBucket repository . In addition, the following video shows the [nwazet Touch Display module and the shield in action with a Netduino Plus <link to be provided> <h4>Conclusion</h4> With nearly 300 known Arduino shields on the market, theres a wide variety of plug-and-play functionality waiting to be leveraged by the Netduino community, sometime with little or no software work required. When all else fails, building a shield from scratch to get a desired feature onto a Netduino / Plus is a relatively simple process only requiring reading component datasheets and some patience with a soldering iron or a small breadboard. In our next article, well put these guidelines to the test with a simple and popular off-the-shelf data logger shield made by Adafruit Industries. <h4>Appendix</h4> To build the shield interfacing with the [nwazet Touch Display, you will need the following parts: <blockquote><h5>Custom shield Eagle schematics</h5> https://bitbucket.org/fabienroyer/nwazet.channel9.articles/src/d33fe2c170e9/WhatIsAShield Custom shield <h5>Bill of materials (~$15 for the shield w/o the [nwazet Touch Display module)</h5>1 http://nwazet.com/touch-display-module [nwazet Touch Display module 1 http://nwazet.com/gocable10 10cm FCI connection cable 1 https://www.adafruit.com/products/187 Adafruit DIY shield for Arduino 1 http://www.proto-advantage.com/store/p/Netduino_GO_IDC_Adapter.php ProtoAdvantage IDC Cable Breakout Board 1 http://components.arrow.com/part/detail/40492237S7076394N5236 PNP Transistor 2N4403 1 http://components.arrow.com/part/detail/39827066S7077451N5146 1K ohm resistor 1 http://www.digikey.com/product-detail/en/CF14JT10K0/CF14JT10K0TR-ND/1741265?cur=USD 10K ohm resistor 1 http://www.radioshack.com/product/index.jsp?productId=2049745 22 AWG solid core wire 1 http://www.radioshack.com/product/index.jsp?productId=2062725 0.32" diameter 60/40 rosin-core solder <h5>Eagle libraries</h5> http://www.ladyada.net/library/pcb/eaglelibrary.html Adafruit Eagle library (Netduino shield part) https://bitbucket.org/fabienroyer/nwazet.go/src [nwazet Eagle library (Go Socket part) <h5>Netduino Go! Wiki</h5> http://wiki.netduino.com/Netduino-GO-Module-Builders-Guide.ashx Netduino GO! Module Builders Guide (documents the Go! bus specifications) </blockquote><h4>Bio</h4> Fabien is the Chief Hacker and co-founder of Nwazet, a start-up company located in Redmond WA, specializing in Open Source software and embedded hardware design. Fabiens passion for technology started 30 years ago, creating video games for fun and for profit. He went on working on mainframes, industrial manufacturing systems, mobile and web applications. Before Nwazet, Fabien worked at MSFT for eight years in Windows Core Security, Windows Core Networking and Xbox. During downtime, Fabien enjoys shooting zombies and watching sci-fi. <img src="http://m.webtrends.com/dcs1wotjh10000w0irc493s0e_6x1g/njs.gif?dcssip=channel9.msdn.com&dcsuri=http://channel9.msdn.com/Feeds/RSS&WT.dl=0&WT.entryid=Entry:RSSView:7615d43a7c264770886ca093015f17a6

View the full article
 
Back
Top