Interrupt Safe Ring Buffer Library

Christoper looks through a circular tube

I created a ring buffer library for embedded systems in C some years ago and have been using it when I need to add a simple queue to my software.  I knew it worked correctly since I created unit tests to validate the code.  However, I noticed that it wasn’t interrupt safe since one of the consumer variables is shared by the producer (the count).  This library requires disabling interrupts when consuming.  Not what I wanted.

There are a number of difficulties when designing ring buffers, mostly dealing with efficiencies of code or data, or the struggle between consumers and producers.  My generic ringbuf library had used a single fill count, but the count had to be updated by the interrupt service routine (producer) and by the routine that retrieved the data (consumer).  I tried to use read and write offset indices, but that required that I always keep an element open since the you can’t tell if the ring is empty or full when the head and tail are the same.

Absolute indices appeared to be interrupt safe, although they require that the buffer length be a power of two.  That seemed to be flexible enough for most embedded work.  I modified and tested my ring buffer library using that method, but my ringbuf, which allowed for a fixed length element,  seemed to be overkill for what I was using it for – gathering bytes from a serial interrupt service routine.  Its API also had a slight flaw such that if the consumer retrieved the data (via Pop rather than just viewing the first element), the producer was free to put new data into the buffer, overwriting the data just retrieved.

I had started my ring buffer library as a simple character (one byte) FIFO – first in, first out.  So, I revisited that code, and modified it to use absolute indices.  A simple byte ring buffer, first in, first out.  Now I had interrupt safe code that would work in my serial interrupt service routine.

Why go to the trouble to make a library module to handle a ring buffer when slapping the following quick and dirty ring buffer code (from Stefan) into the serial module would have worked as well?

   #define N 128
   volatile unsigned int head, tail;
   volatile char buffer[N];
   unsigned int inuse() { return head - tail; }
   void put(char c) { if (inuse() != N) { buffer[head++%N] = c; } }
   void get(char* c) { if (inuse() != 0) { *c = buffer[tail++%N]; } }

Because I needed to know that it worked correctly, and I like reusable library modules.  Especially high quality reusable modules.  Especially high quality reusable modules that include unit tests to validate functionality.  Unit tests that also serve as guides for implementers.

The new module files are ringbuf.c and ringbuf.h for the ring buffer with fixed sized elements, and fifo.c and fifo.h for the ring buffer with byte sized elements. All my versions of the ring buffer and the unit testing framework can be found in the file.

One of my original non-interrupt safe ringbuffer libraries that uses a count can be found in the ringbuf.tgz file archive.  Another buffer library I wrote is a linked list that is sorted by key as elements are added, and the elements can be retrieved by key, index, or FIFO. This keylist library is useful for caching dynamic data by key.

Posted in software | 3 Comments

BACnet – First to the Moon, and Beyond!

I drove to the BACnet meetings in nearby Atlanta and Bill photographed my license plate.  We need lofty goals, don’t we?

Posted in BACnet | Leave a comment

Convert to MP3 Konqueror Service Menu

I wanted an easy way to convert music to MP3 from various formats under Konqueror in KDE on Kubuntu Linux.  I found an interesting post about creating a KDE Service Menu which got me started in the right direction.  However, I didn’t like the way ffmpeg converted to 64kbps MP3 quality and the bad sounding results regardless of the bitrate settings that I chose.  I tried lame, and was quite pleased with the results.  I also didn’t like the way the service menu used the entire filename without stripping the dotted extension, so I copied another service menu perl example.  My “Convert to MP3” service menu script goes into the following location:


File convertmp3.desktop:

[Desktop Entry]

[Desktop Action convertToMP3]
Name=Convert to MP3
Exec=lame -V2 %f "`echo %f | perl -pe 's/\.[^.]+$//'`.mp3"


Posted in Linux, Music | 1 Comment

Gmail IMAP Invalid Credentials in Thunderbird

I kept getting “Invalid Credentials” in Thunderbird when connected to my Gmail IMAP account.  I found the solution to be:

Posted in Uncategorized | Leave a comment

Birds Are Fast

Birds are fast.  I guess that is why they have been able to survive all these years.  But I want to take a photograph of them!  So, I have learned to be fast.  Not just fast in taking the shot, but also fast regarding the shutter speed.

I usually use a longer lense (300mm) to get in close, and didn’t realize how much shaking is going on when I am photographing something.  Since a lot of birds eat in the morning or evening, and the setting is my back yard which is wooded, the birds are not in full sun light.  The other challenge in a wooded environment is auto focus.  Too many things for the auto focus to focus on, and how does it know I am looking at a bird?

1) Focus Problem  2) Lens Shake  3) Missed the Bird

So here is what I do:

  1. Increase the ISO.  I set my Canon Digital Rebel XTi to ISO 1600.
  2. Use shutter priority – TV – mode.  Set the shutter speed to 1/125s or faster.
  3. Use the rapid fire – AI Servo – mode for taking lots of photos very fast.
  4. Use manual focus (sometimes).

Posted in photography | Leave a comment

BACnet Meets in Salt Lake City

Salt Lake City Mountains

The BACnet committee met in Salt Lake City at the end of June.  We had a large number of Addenda that had finished public review, and had lots of comments for which to respond.  Salt Lake City had some interesting sites and some street items that I had not seen before.  The “Look” on the pavement, as well as the flags used at some crosswalks.
Look Both Ways! Street Crossing Flags

I attended Mass at Cathedral of the Madeleine on Saturday night.  It was a beautiful church and a thought provoking sermon.

Altar at the Cathedral

I ate some wonderful food in Salt Lake City, including a fabulous carrot cake at Squatter’s Pub Brewery, and some delightful salmon at Cucina Toscana.

Posted in BACnet, travel | Leave a comment

BACnet Committee Meets in Germantown

Our fun BACnet committee and working groups met in Germantown at the end of April for a week. The weather was perfect, and the Montgomery College Germantown Campus was beautiful and lively.

Cherry Tree in Bloomsunlit_cherry_tree.jpgPink Dogwood in Bloom

We had our traditional Tuesday night BACnet dinner at Dogfish Head Brewery.

Don and Dave chat about Georgia TechHoward and ChristophMike, Steve, and David need reading glasses - Craig does not

Most of the working groups met.

SSPC 135 BACnet CommitteeWorking Group MeetingMike Newman

Posted in BACnet | Leave a comment

Drawing a Fairy with Wings – Tinkerbell

My daughter Anna got a book in December – How to Draw Fairies. I like to draw, so I studied it and began drawing a fairy using Inkscape. Drawing scalable vector art that can be filled is different than sketching. After I got the sketching finished, I had to redraw most of it to make it fillable so that I could add colors.



I presented my handiwork to Anna. She told me that Tinkerbell’s wings were more blue, she needed a ribbon in her hair, and she had tassles or balls on her shoes. I guess I missed a few critical details. So back to the drawing board I went.


I had hoped to add this little gem to Tux Paint, but I am not sure that I can since Tinkerbell is a Disney trademark image.

Posted in clipart | Leave a comment

Configuring Kubuntu 8.04 Linux for Dell Latitude D820 Notebook

I was pleasantly surprised to have all the hardware working and a smooth install of Kubuntu 8.04rc for a year old Dell Latitude D820 notebook PC. There was still a bit of customization to do to make it work like I wanted, but who can fault Canonical for not knowing what I like?

Add Codecs, Java, and other Customizations

The official way to do this is documented in the Ubuntu Wiki under CommonCustomizations. There is also a wiki page on EasyCodecInstallation. The official meta package is called “ubuntu-restricted-extras” and this installs:

  • MP3 playback and decoding
  • Sun Java runtime environment
  • Flash plugin
  • DVD playback
  • LAME (to create MP3 compressed audio files)
  • msttcorefonts (may be missing in kubuntu extras)
  • DeCSS script to download and install for DVD viewing

$ sudo apt-get install kubuntu-restricted-extras
$ sudo /usr/share/doc/libdvdread3/
$ sudo apt-get install msttcorefonts

Add the ability to compile, edit, and maintain code

Here are the compilers and their documents
$ sudo apt-get install build-essential subversion-tools gcc-4.2-doc glibc-doc manpages-dev
$ sudo apt-get install mingw32 mingw32-binutils mingw32-runtime
I need access to subversion and XSLT ChangeLog tools
$ sudo apt-get install subversion-tools
$ sudo apt-get install xsltproc
I install a couple of editors, useful for various things. Kate is already installed.
$ sudo apt-get install scite
$ sudo apt-get install vim-full
Useful tools for cleaning up code, converting comments and line endings, and code statistics:
$ sudo apt-get install splint
$ sudo apt-get install sloccount
$ sudo apt-get install indent
$ sudo apt-get install liwc
$ sudo apt-get install tofrodos

Add favorite online music source and music/video player VLC

$ sudo apt-get install streamtuner
$ sudo apt-get install vlc

Get rid of limited use Dolphin

This is one of the main reasons I use Kubuntu and KDE: Konqueror allows me to do many things that Nautilus and Dolphin prevent or are unable to customize
$ sudo apt-get remove dolphin

Get Rid of IcedTea – Install Sun Java JRE

I had several errors which prevented me from doing useful work on some websites that used Java.  Turns out that the default install of Kubuntu used IcedTea – the OpenJDK.  Well, apparently it doesn’t mimic Java the way the websites expected, and so, it didn’t work.  Into the hopper it goes, and Sun gets the install.
$ sudo apt-get remove openjdk-6-jre
$ sudo apt-get install sun-java6-jre

Get rid of annoying PC speaker beep

Using a console, remove the PC Speaker driver from the kernel:
$ sudo modprobe -r pcspkr
Edit /etc/modprobe.d/blacklist and add a new line to keep the PC Speaker driver from automatically loading:
# Keep the annoying beep from occurring - don't load the PC Speaker driver
blacklist pcspkr

Get rid of annoying Synaptic Tap Button

Edit the X11 configuration file /etc/X11/xorg.conf

Add the Tapbutton line and any other missing lines in the Synaptic Touchpad section:

Section "InputDevice"
Identifier  "Synaptics Touchpad"
Driver      "synaptics"
Option      "SendCoreEvents" "true"
Option      "Device" "/dev/psaux"
Option      "Protocol" "auto-dev"
Option      "HorizScrollDelta" "0"
Option      "SHMConfig" "On"
Option      "TapButton1" "0"

You can disable the Tap Button effect immediately using the synclient tool as long as SHMConfig is On:
$ synclient TapButton1=0

Fix a bug in Firefox 3 rendering Check boxes and Radio boxes

Check boxes and Radio boxes show as a blank box or circle until the focus is moved away.  This bug was reported, and the solution is to install a GTK2 engine:
$ sudo apt-get install gtk2-engines-qtcurve

Additionally,  go to System Settings->Appearance->GTK Styles and fonts, and Set: Use another style = QtCurve.

Updating a Toshiba A135 Notebook

I also updated our Toshiba A135 Notebook and the sound and sound controls now work!  I did have to fix an fglrx bug to keep from locking up on logout.

Posted in Linux | 1 Comment

Sharkfest ’08 Conference for Wireshark

sharkfest logoI was invited to attend Sharkfest ’08, a conference for Wireshark users and developers, held March 31-April 2 at Foothill College in Los Altos Hills, California. I had a great time, and am so grateful that Gerald invited me to attend. I attended as a Wireshark developer since I actively maintain the BACnet dissector. I went to the conference with several goals in mind:

  1. Figure out how to do BACnet segmentation
  2. Figure out how to get BACnet MS/TP into libpcap/winpcap.
  3. Add a couple of BACnet dissector enhancements to Wireshark.
  4. Learn about using Wireshark for problem network analysis.

I attended Laura Chappell’s training sessions, and learned a whole lot about Network analysis and the love-hate relationship between TCP/IP SYN, ACK, and Keepalives (#4 completed). I also wrote some Wireshark code during the classes which I promply submitted (#3 completed). Loris came into the activity center and sat down with my son Joshua and me, and so we discussed how to integrate BACnet MS/TP RS485 from serial port into libpcap/winpcap (#2 completed). On the last day and the last session of the conference, I attended a session by Richard Sharpe about file sharing protocols and learned about Wireshark conversations (#1 completed).

Vint Cerf and Gerald CombsWe were treated to an inspiration talk on Tuesday morning by Vint Cerf. After the talk, I went to Laura’s session, and Joshua talked to Vint. Vint signed Joshua’s Half-Life player guide, had photos taken with Vint and Gerald, and the Wireshark crew gave Joshua some swag.

Gerald treated the developers to dinner on Tuesday night at Trader Vic’s. I drove to dinner with Joshua and Jaap Keuter, and learned about PBX systems and Jaap’s passion for skydiving. At dinner I sat next to Ulf Lamping and learned about yet another division of Siemens and about Ulf’s love for motorcycle riding. Guy Harrris and Mike sat across from me.  Joshua ate with Gerald’s wife and daughter, and John Bruno’s wife.

The Foothill College campus was beautiful and on Spring Break. The food was awesome and plentiful.  The people were great!  The Wireshark sessions were helpful.  Maybe I will get to attend next year.

Posted in applications, BACnet, software, travel | Leave a comment