Thursday, September 3, 2015

Use an Arduino with Atmel Studio

The Arduino line of embedded platforms have become pretty popular among hobbyists and for prototyping or quick proof of concept.  Depending on the type of news you follow on the internet, you've probably heard about all sorts of projects people have done using them.  I've even seen them for sale in electronics stores.  And for good reason, Arduinos have a lot to offer.
The Setup:
All said and done, Arduino really comes down to being a combination of a custom compiler, a bootloader, and a minimum set of hardware so as to require nothing more than a USB cable.  The compiler reminds me of the pairing of C# and .NET: the language syntax is clean and easy to use, and the API abstracts you from all the granular detail.  Everything is handled thru wrapper functions that makes the hardware nearly invisible.  No digging around in datasheets for esoteric bit settings and flags to get peripherals to work, they take care of it for you.  The bootloader removes the need for a programming tool, and thus external hardware.  And finally, the hardware consists of an Atmel Atmega328 with enough components to power, communicate serially over USB, reset, and access the I/O of the microcontroller.  But what's really nice is that the initial investment is only the cost of the Arduino board itself.  The IDE is provided free of charge and loaded with example programs to get you started.

So I purchased a Nano (one of the several flavors offered), plugged it in, and got started.  The first thing I noticed is that it just shows up as a generic COM port in Windows.  This will be helpful later.  But I did what most people would do, the proverbial "Hello, World!" of embedded systems: blinking an LED.  And behold!  My will was manifest upon the physical world.... in the form of a small little light... turning on and off at one second intervals....  Well hey, it only took 2 minutes, and that's the power of the Arduino.  (and the IDE actually has code for a Blink program included which kinda takes the fun out of it, but it can be modified as I did to do things like blink at different rates based on input)

However, I found myself feeling rather constricted by the platform.  I wanted to have full control over that Atmega chip!  I guess the Arduino isn't meant for me....

The Solution:
Atmel provides their own IDE, Atmel Studio, intended for C/C++ development on their microcontrollers.  It's actually built in top of Microsoft Visual Studio and inherits many if the features.  The problem is that it requires specific programming tools.  So even if I write a program, I can't get it into the chip.  This is where understanding what the Arduino is came in helpful.

At the end of the day the Arduino isn't dumping Arduino code to the microcontroller.  It's sending a compiled binary the same as Atmel Studio would.  But how?  Turns out the Arduino IDE has an option for verbose output when compiling and loading a program.  The IDE uses a command line tool called AVRDUDE which supports programming Atmel chips using a myriad of tools (including the Arduino bootloader).  I can just copy the command line arguments to write any .hex file I want.  AVRDUDE can be downloaded or comes with the Arduino IDE.

So problem solved.  Build the program in Atmel Studio, copy the binary into the AVRDUDE directotry (or type the whole build path in the command line), open a console window and away we go.

Well actually, this kinda sucks.  It's a bit of a hassle.  You gotta move around between a few windows and manually do a bunch of stuff.  It gets old.  Fast.  But there is a better way.  Visual Studio (again, the underlying software for Atmel Studio) allows for custom commands.  In the image below you can see that I've created a tool using AVRDUDE and just copied all the command line arguments that I used above.  The important pieces to note are that the initial directory is set to the build directory of whatever project is open so that the path to the .hex file is unnecessary, and the same goes for the name of the .hex file.  I've also checked the box for "Use Output window" as it's nice to see any messages from AVRDUDE.  The only caveat is that you need to manually change the COM port argument if you use a different USB port.  Otherwise, assign the tool to the button and the process becomes quick and easy.
But wait!  There's more!  We have ourselves a programmer, but not a debugger.  Truthfully, that's a bigger hurdle, but we can at least come up with a way to easily get feedback from the Atmega328.  Since we're using an Arduino, we've already got ourselves a convenient COM port by default.  We can just have our code spit out info over the UART to allow for visibility into its operation.  I went ahead and wrote myself a simple console application in C# that takes a COM port and baud rate as arguments and will run in Atmel Studio the same as AVRDUDE.  Not nearly as powerful as a debugger, but still useful enough.
The Finish:
Well there we have it.  With only an Arduino board (and not just the Nano I'm using) and a USB cable we have a simple Atmega development platform.  We can use the Atmel software with a somewhat seamless workflow by only adding some command tools.  This may not be for everyone, as the Arduino platform in and of itslef provides a lot of functionality, but for some, this may give you extra flexibility with the Atmel chip to do what you want.  Alright, I've typed Arduino way too many times....