avrtoolbox Ring Buffer
by Joe Pardue
Follow along with this
series! Joe’s book & kits
are available at
So far, in our briefly interrupted series on avrtoolbox,
we’ve looked at some software engineering principles and
applied them to creating the avrtoolbox project on Google
Code [ http://code.google.com/p/avrtoolbox/]. We’ve
learned about creating an open source project using a
consistent C programming style, documenting it with
doxygen, putting our functions in libraries, and keeping
track of the whole thing with a software versioning system.
Last month, we learned a bit about two more software
engineering tools: the FRS (Functional Requirements
Specification) and the API (Applications Programmer
Interface), and applied all that to an elementary serial
communications library meant to mimic the kinds of
novice-friendly functions we’d find in the Arduino or PBASIC.
The novice-friendly serial library was built on top of
two other libraries: one for storing the data (ring buffer)
and another for sending and receiving the data (USART).
These libraries are not particularly novice-friendly and
more like what you’d see in a professional software
production environment. This month, we are going to look
at ring buffers and next month we will look at the AVR
USART. Also this month, we will look at another software
engineering tool — the AVRStudio Simulator — that we will
use to test the ring buffer functions.
What is a Ring Buffer?
58 July 2011
A ring buffer algorithm turns a linear array into a circular
array which as you will see in a moment, can be very useful
for rapidly storing and retrieving data. A linear array is just
a sequence of contiguous memory locations set aside to store
data in such a way that you can access the data by using
a number indicating its position in the array. For instance,
if you had a 64-byte linear array named linArray and wanted
to get the 32nd byte, you would use: myByte = linArray[ 31].
You use 31 instead of 32 because in arrays, we start numbering
at 0 rather than 1, meaning we have bytes number 0 to 63
stored for a total of 64 bytes. A ring buffer is a linear array,
but we use a special algorithm to make it behave as if it
was a ring having neither a beginning nor an end.