About two months ago, Atmel announced a smart new set of AVR development boards, the XPlained series. One of these boards (which I’ve just recently purchased for $30) boasts a shiny new AVR XMega microcontroller. What? An XMega you say? Why yes, haven’t you heard? Come now, they’ve been around for fully three years at this point. Well, don’t worry if this is fresh news, you’re not alone. For some reason, adoption of the powerful new XMega MCU has been slow amongst hobbyists.
When Atmel introduced their new XMega series of AVR microcontrollers back in 2008, I was pretty sure they’d be a quick sell. Even in spite of their unavailability in DIP packaging, and this cheesy marketing video. But sadly, a quick search of Hack a Day currently yields only four articles containing the term “XMega” (versus more than three hundred articles for “ATMega”). I guess change is hard. And yet, it’s really not.
The XMegas utilize the same AVR core as the ATMegas, and are fully supported by the free AVR-GCC compiler and AVR Studio (obviously, I guess). Plus, you can still use your trusty AVRISP mkII programmer to load code onto an XMega. In other words, the tool-chain is unchanged. So, with a few register name changes, code from an ATMega will run perfectly well on an XMega. Sadly though, the XMegas and ATMegas are not pin compatible, so you won’t be able to solder a new XMega onto your Arduino.
Alright, so for those of you keeping track, I guess I’ve now listed three strikes against the XMega which may help to explain its present unpopularity: no DIP packaging, no pin compatibility with the ATMegas, and changes to register names (plus the addition of new registers for new features). Oh, and odd marketing tactics. By the way, as a side note, it could be argued that the XMega register scheme is better than that of the old ATMegas. They’re making good use of structs, so that you now address a port’s direction using PORTX.DIR, rather than DDRX. And you output to that port using PORTX.OUT.
But let’s now take a look at the data in favor of the XMegas, yes? To do that, we’ll compare the ATMega1280 (utilized on the Arduino MEGA) against the ATXMega128A1 (utilized on Atmel’s new XPlained development board):
|Max CLK Speed
||1.6 – 3.6V
||2.7V – 5.5V
||16, 12-bit, 2Msps
||16, 10-bit, 76.9Ksps
||4, 12-bit, 1Msps
||8 (one supports IrDA)
||128-bit AES, DES
||2, 8-bit and 4, 16-bit
|Current Draw, 1Mhz, 1.8V
Not bad, right? I’d say the XMega wins this round. It’s faster, provides substantially better analog to digital conversion, offers digital to analog conversion (in other words, analog outputs – a feature not available on any ATMega), hardware-based encryption (again, not found on any ATMega), lower power consumption, and, wonder of wonders, a lower price. I’m quite impressed (for what that’s worth) and am particularly excited about putting these new analog features to the test. In fact, as I mentioned, I’ve just received my XPlained development board and have already written a quick test program to do ADC → DAC pass-through. But I’ll describe my experiences with the XPlained board a bit later (spoiler alert: they weren’t all pleasant).
So what precisely does the new XMega series offer that makes it, in my opinion, such a substantial improvement over the ATMega series? Let’s talk speed for a moment. The old ATMega topped out at 20Mhz, at least officially (though overclocking is possible, as seen in the Uzebox gaming system). But furthermore, there is no way to adjust the system clock on the fly (although you can, of course, adjust peripheral clocks). You’d have to make fuse adjustments with an external programmer. With the new XMega series, you can adjust the system’s clock frequency at run-time. Both a 2Mhz and a 32Mhz internal RC oscillator are provided, plus a PLL which allows for clock multiplication (1x, 2x, 3x, …, 31x). According to application note AVR1005, you can even use the PLL to increase the clock speed of your peripherals to a maximum of 128Mhz. This might be useful for generating high-resolution PWM signals, for computing precise time intervals (think range-finders), or for just blinking an LED really really fast (although perhaps not this fast). Man, I could’ve used this on my MS thesis…
Another neat feature of the XMega series is the brand new event system which allows for high-speed signaling between peripherals. This is not a communications bus in the traditional sense. It’s actually more like a set of, shall we say, “personalized” interrupts sent between features. Event signals can be sent quite rapidly – in no more than two clock cycles – and don’t require the CPU to be active.
The XMega’s event system opens up a whole new world of possibilities. With it, you could tie a set of 16-bit counters together to form one highly-accurate 32-bit counter. Or, how about this application note example:
You could use one event to synchronize two modules. For instance, you could use a pin change event to do an ADC conversion and an input capture on the Timer/Counter to get exact time-stamps for each conversion.
For more details on the event system, see the “Getting Started with XMega” application note AVR1005 or the “Getting Started with the XMega Event System” note AVR1001.
First Impressions of the XMega XPlained Dev Board
Alright, well let’s get down to business here. As I mentioned earlier, I’m now the proud owner of a brand new Atmel XMega XPlained development board. I was putting in an order with Digi-Key the other night when I thought to search for XMega products (yes, shame on me, I haven’t tried them out until now). I found just one, but it was precisely what I was looking for: low cost ($30.16), USB-powered, and covered in blinking lights (well, nine of them anyways). So of course I bought it. I mean, it’s not quite as much of a steal as the $4.30 TI Launchpad, but even at $30, I didn’t even bother to do research before adding it to my cart. I just figured it would work. 🙂 Bad idea, I know…
So what did I get for my money? A pretty padded box and the board itself. Nothing more. No documentation whatsoever, only a printed messages on the outside of the box requesting I go online for any required drivers and data.
Now the lack of paper is fine with me; if Atmel wants to save some trees, good for them. I’d have gone online for datasheets and schematics anyway. My only gripe here is that Atmel’s site isn’t all that easy to navigate. In fact I don’t think I ever located a link to the ZIP file associated with AVR1927 (instead I just crossed my fingers, manually typed in the assumed link, and bingo). But maybe I’m just bad at the internet. Well for the sake of centralization, here are a few URLs I found helpful when getting started:
The XMega XPlained comes pre-programmed with a cute little application that blinks its nine LEDs and plays different sounds (drums, trumpets, etc. – just one- or two-second clips) when each of the eight different buttons are pressed. And I’ve got to say, the little speaker actually impressed me with its sound quality. It’s not exactly a M-Audio studio monitor, but it’ll probably hold its own against a speakerphone. And it’s certainly not being driven by square waves; they’re making good use of the XMega’s analog outputs (DACs). So what else does the XPlained offer? Well, here’s the official list:
- External memory (8MB SDRAM, MT48LC16M4A2TG)
- Atmel AT32UC3B1256
- Communication gateway
- Programmer for Atmel AVR XMEGA
- Analog input (to ADC)
- Analog output (from DAC)
- Mono speaker via audio amplifier
- Digital I/O
- UART communication through USB gateway
- 8 mechanical button switches
- 8 LEDs (plus one bi-color LED)
- 8 spare analog pins
- 24 spare digital pins
Programming the XPlained using FLIP
So the pre-loaded software entertained me for about sixty seconds, after which my desire to reprogram the board overcame my fascination with lights and sound effects. I didn’t have my AVRISP mkII handy (left it at work again), so I started by looking into reprogramming via the board’s USB connection. The first thing I needed was a driver for the virtual COM port (Windows 7 did not recognize the XPlained), a single INF file:
USB CDC Driver (Virtual COM Port) – required for USART communications.
I was pleased to find that the instructions provided in the “Getting Started” guide (AVR1927) for using the Flexible In-System Programmer (FLIP) for RS232 programming were quite simple. I downloaded and installed FLIP via this link. I also had to import an XML configuration file (provided here), although it sounded like this file should have been included in the latest FLIP installer. But before adding this file to <Install Directory>Flip 3.4.xbinPartDescriptionFiles, I received an error stating that “the device does not exist” when using BatchISP (the FLIP command line utility). I also attempted to use the FLIP GUI directly, but for some reason the RS232 communication option was greyed out. No problem though, I simply threw the programming commands given in the instructions into a simple batch file:
batchisp -device ATXMEGA128A1 -hardware RS232 -port COM25 -baudrate 115200 -operation onfail abort memory flash erase f blankcheck loadbuffer c:xmegatestdefaultxmegatest.hex program verify start reset 0
I’ve highlighted the elements you’ll want to change when using this file. Including that pause command causes the command window to wait for you to press a key before closing, that way you can take a look at the results of your programming attempt:
So using BatchISP (FLIP) worked just fine for me. The whole programming process took a bit longer than I would have expected (maybe 20 seconds), but it’s not terrible. The one catch is that you have to unplug the XPlained board, and then plug it back in while holding down switch SW0, every time you want to reprogram. This is required in order to activate the bootloader. Doing this gets old fast, and it didn’t seem to please my computer (it would occasionally freeze for a few seconds when the device was quickly plugged back in). But there is an easier way; keep reading…
Programming the XPlained using the AVRISP mkII
So based on the literature I’ve run across, it seems the preferred means of reprogramming (and debugging) an XMega is via JTAG using either the AVR Dragon ($50), the AVR JTAGICE mkII ($300), or the Cadillac of debugger/programmers, the AVR ONE! ($600 – perhaps this is the reason for the exclamation point). The new XMega series uses the PDI (Program and Debug Interface) programming interface (as opposed to ISP). However, it is possible to use the AVRISP mkII programmer (though you cannot use it for debugging), which costs just $35. And if you’ve done anything with AVRs in the past, you’ve probably got one of these hanging around (I should note that the original AVRISP, the one with the DB9 port, will not work with the XMega series).
Now, to get your AVRISP working with the XMega Xplained, you’ll need to create a simple pin adapter. You cannot connect directly to the board as the pins are arranged for a 10-pin JTAG connection (I guess Atmel really wants you to use JTAG). However, you’ll only need to connect four of the six pins present on your AVRISP, as shown in this diagram (found on page 9 of the XMega “Getting Started” guide, AVR1005):
These pins can be found on the XPlained’s JTAG connector, as shown in the schematics:
Once you’ve made these connections, you can use AVR Studio to reprogram your device as usual. Well, almost. First, you need to make sure it’s fairly up-to-date (I used version 4.18, build 700 with success, but you might go straight to AVR Studio 5, which I’ve also tried with success). You’ll then need to manually specify the ATXMega128A1 before programming or adjusting fuses. Plus, and this is key, you’ll want to disable the JTAG interface by using AVR Studio to clear the JTAGEN bit on the fuses tab. If you don’t, your programming may or may not be successful. I actually got into an interesting cycle where alternate programming/read events would fail. I’d perform one operation successfully, but on the next I’d see “Entering programming mode…FAILED.” But disabling the JTAG interface took care of this issue. And doing so does not prevent you from re-enabling JTAG later, or from using BatchISP (though you may need to reload the bootloader if you’ve erased it, which may be found in this ZIP).
For more details on programming and debugging an XMega, see this article (and actually, check out all of these “Getting Started with XMega” articles, they’re quite good).
A First Test of the Analog I/O
Now I think I’ve stated this already, but again, I’m pretty excited about the new analog I/O offered on the XMega series. In particular, the 12-bit digital to analog converters (DACs) open up a whole new world of options. I mean, there are all sorts of applications out there that might benefit from an on-chip DAC: function generators, analog power supplies, audio processors, lighting controls, you name it!
So the first bit of code I wrote for my XMega is a simple ADC → DAC pass-through. It’s a touch long to include in this post (because of all the comments), but please feel free to download it here. The code takes an analog input on ADCA1 (PORTA1, pin 96) in the 0-2.1V range and outputs a proportional analog signal on DAC0 (PORTA2, pin 97) in the 0-3.0V range. Here’s a screenshot of the results taken using my RPI IOBoard and LabVIEW interfaces. The bottom graph (red line) shows the sine wave signal being generated by the IOBoard and connected to the XMega’s ADC input. The top graph (white line) shows the scaled sine wave being measured at the XMega’s DAC output:
You may be wondering: why the difference in input and output voltage ranges? Well, here’s one additional problem I see with the XMega: Vcc (max 3.6V) is not directly available for use as a reference for the A to D conversion, only Vcc/1.6V (which is 3.3/1.6 = 2.0625V, in this case). Now you can select AVcc (typically tied together with Vcc, perhaps via a filter) as a reference for the DACs, although according to the hardware datasheet, both ADC and DAC reference sources are limited to AVcc-0.6V, or 2.7V on a 3.3V source (which is what you get on the XPlained development board).
Now in testing these specifications, I have found the ADC limit to be fixed as stated, although when I’ve selected AVcc as the reference for the DACs I’ve seen max outputs reach just over 3V. Honestly, I don’t know what prevents the XMegas from using Vcc as a reference, as this is commonly done with ATMegas. Oh well! You just may need to throw in a voltage divider and/or op-amp to compensate.
One other issue I noticed was noise in the ADC signal when measuring values near 0V. This could be an issue with how I’ve setup my code, or with some other aspect of my hardware. But you can see this effect in the slightly garbled low points of the sine wave shown in white in the above screenshot. I guess the bottom line is that I’ll need to play around with this a bit more. (NOTE: I believe we have solved this issue; it is a problem with the XMega chip itself. The solution is to use a lower (e.g. 1V) reference for the DAC. See comments section below.) Apparently there are also calibration registers for the ADC, and some pretty advanced tweaks you can make. Take a look at this page on configuring and tuning the XMega ADCs – it’s been a great help to me already.
Overall, I’m cautiously optimistic about the XMega and the XPlained development board. I’ve encountered a couple of minor issues, and the list of problems in the errata section of the datasheet is frighteningly long. I should also point out that a previous version of the XPlained, the XPlain, apparently had quite a few more serious issues. You’ll find references and pictures of the XPlain if you do a bit of Googling. I’m not sure who’s still selling it, but I can tell you that despite the picture and the name, Digi-Key is shipping the newer XPlained, not the old XPlain (this is where I got mine).
So I still say that the XMega a great leap forward by comparison with the ATMega series. The only question left is what to do next?
I’ve been thinking about going further with this ADC-DAC application and creating an audio compressor and volume control. You see, I’ve got this cheap portable speaker that I use with my Blackberry for listening to MP3s. The trouble is, there’s no remote, and each track seems to play at a slightly different volume. So I’m thinking of using the XMega to receive IR signals from a remote and then adjust the volume accordingly (by scaling the ADC result before sending it to the DAC). And at the same time, it could automatically adjust volume, based on the incoming audio signal, within a certain range. This is called compression. My setup would require a bit of analog work to get the signals into the correct voltage ranges before and after processing, but a couple of op-amps would likely do the trick without much work.
That said, I’m open to other ideas. Has anybody out there got suggestions for projects?