Updated AVR UART Library
Peter Fleury wrote a very nice library to use the onboard UART (hardware serial port(s) of several AVR microcontrollers. His library can be found at his AVR Web Site and is released under the GPL. The most recent version on Mr. Fleury's Web site seems to date back to 2006 (files inside his most recent .zip file date to 2005); therefore, it seems to be unmaintained.
Rationale for an updated library.
Therefore, I am providing an updated version of the UART library, mainly to assure that it wll be supported by future versions of AVR-Libc, but also to add new functionality.
- While working on a project using a microcontroller not supported by the library, I learned that AVR-Libc introduced a new interrupt vector name format with version 1.4.0 (the current version, as of the inital writing, is 1.6.6). Mr. Fleury's UART library uses the old format, which brings with it a few issues:
- Many new AVR models are defined in AVR-Libc using only the new format.
- The AVR-Libc Interrupt.h page makes noises to the effect that the old format may be deprecated in the future.
- I've been working with Andrew Rapp's Xbee-Arduino library, which permits using API-mode communications with XBee modules. As Mr. Rapp's library is built for Arduino and is written in C++, I needed a way to "bridge" the gap between Mr. Fleury's C UART library and Mr. Rapp's C++ Xbee-Arduino. After trying several approaches to creating a "wrapper" class for the unmodified UART library (all ending in scope errors), I came to the conclusion that I needed the Arduino HardwareSerial "available()" and "flush()" functions inside the UART library.
- I've exchanged e-mails with Mr. Fleury and he doesn't seem interested in adding new functionality to his library. It's also not clear whether or not he'll be updating it in the future.
Modifications made to the original UART library.
I have made the following changes to the UART library:
I tested the updated library using WinAVR-20090313, an Adafruit Boarduino with an ATmega328P, and an ISP programmer (I blew away the Arduino bootloader). I haven't tested the library on other AVR models, but I did check the macros for the supported devices against their respective AVR-Libc header files to verify they pass the "Ho! Ho! Ho!" test. I previously used the "available()" and "flush()" functions in another project on an ATmega162 using both UARTs, so I have previous experience that these functions work correctly.
- I updated the UART/USART interrupt vector names for all AVR devices contained in the original library from the old "SIG_" format to the new "_vect" format, using the AVR-Libc version 1.6.6 device headers as a guide. This required a few other changes as there are a few inconsistencies in the new names as of AVR-Libc version 1.6.6.
- I updated the Interrupt Service Routine labels from the old "SIGNAL" format to the new "ISR" format.
- I added support for the newer ATMEGA48P/88P/168P/328P models (not as great a task as it might sound, as their requirements are identical to the ATMEGA48/88/168, but their header files only use the new format).
- I added "available()" and "flush()" functions, modeled after the Arduino HardwareSerial library, with support for both one- and two-UART AVR models to match the rest of the library.
Using the updated UART library.
I suggest reading the documentation on Peter Fleury's AVR Web Site. His documentation comprehensively covers all aspects of library usage, except for the new Arduino-style "available()" and "flush()" functions. At such a time as he no longer maintains the documentation on his site, or if he complains about my directing traffic there, I'll make it available on my site.
For information on using the new Arduino-style "available()" and "flush()" functions, I suggest the Arduino Reference. The "Serial" section fully explains the use of these functions, with examples. I've tried to keep the usage of both functions identical to that of the Arduino.
License. Mr. Fleury's original library was licensed under the GPL, version 2 or later. The Arduino HardwareSerial Library from which I adapted the "available()" and "flush()" functions is licensed under the LGPL, version 2.1 or later. As the GPL is more restrictive than the LGPL, and given the fact that I'm not a lawyer and therefore don't claim to understand license esoterica, I'm calling it that the updated library is licensed under the GPL. The more restrictive GPL was selected, vice the less restrictive LGPL, so as to not violate any of Mr. Fleury's rights as the original licenser.
I don't have any particular interest in becoming the library's official maintainer, but I'm willing to add additional AVR devices to it, provided the requestor is willing to do his/her homework. If you wish for a device to be supported, use the library source code, the device manual and the device's AVR-Libc header file to either (1) create a macro for the device or (2) identify an existing macro that's compatible with the device (settings are often the same within AVR model "groups"). I don't intend to add support for more than 2 UARTs unless I need it myself or someone else skins that cat and provides code to add to the library.
Download and Supported AVR Models
Download the library here.
Supported AVR Models (in alphabetical order):
- ATmega48P New!
- ATmega88P New!
- ATmega168P New!
- ATmega328P New!
Copyright 2001-2009, Tim Sharpe. You are free to use this information for personal, non-commerical use without restriction. All rights reserved for commerical, organizational, or government use. Questions or comments to firstname.lastname@example.org. Flames to /dev/nul.