Using the Raspberry Pi as a Simple Current and Power Meter

On a recent project I needed to fairly accurately measure current and power consumption of a handful of parts in real time. We needed to measure current in the range of micro amps, so this was actually somewhat tricky.

I could have just used a bunch of super expensive current meters, but I managed to find a much more convenient and cheap solution using a Raspberry Pi. We were already using a Pi to allow for remote debugging, so I went looking for something I could hook up to that.

I found these ADCs:

They plug right into the GPIO headers on the Pi and even come with some python libraries for reading them.

Read more on Using the Raspberry Pi as a Simple Current and Power Meter…

Undefined Behaviors in C that You’ve Probably Run into without Knowing It

In many languages, when you are unsure of a particular detail of the language, you can often “just run it” and see what happens. This might work in another language, but in C this will almost certainly bite you. It’s too easy to to accidentally invoke “undefined behavior”, where your code might do one thing in one case, but something totally different in another, without even getting a warning from the compiler.

Here are a few undefined behaviors you might not know about, along with the relevant section from the C99 spec. These aren’t just pedantic ramblings; they’re all cases that I’ve encountered on real projects out in the wild. Read more on Undefined Behaviors in C that You’ve Probably Run into without Knowing It…

Inexpensive Ethernet JTAG Adapter with Raspberry Pi and OpenOCD

I recently wanted an ethernet JTAG adapter for a project I was working on. Unfortunately ethernet JTAG adapters can cost upwards of $300, and even then they can be specific to particular chipset and toolchains.

However, were already using OpenOCD with ST-LINK/V2 programmers to communicate with out hardware, and it turns out that it’s very easy to set up OpenOCD on the Raspberry Pi. You can then plug the programmer into the Pi, connect a debugger (gdb in our case) to the OpenOCD instance, and debug your firmware remotely! Read more on Inexpensive Ethernet JTAG Adapter with Raspberry Pi and OpenOCD…

2 Ways to Use Single-Member structs in C

I love C’s structs. There’s a lot of weirdness in C but, for the most part, structs are predictable, useful, and easy to understand.

Structs, for those not familiar with C, are collections of data. An example of a struct is a point on a Cartesian plane:

1
2
3
4
struct point {
  int x;
  int y;
};

Normally, structs are used to associate two or more pieces of data. As the title suggests, I’m going to demonstrate why you might want to use a struct with a single element. Read more on 2 Ways to Use Single-Member structs in C…

Getting Started with MQTT

As more and more things around us become networked, the communication protocols tying them together need careful rethinking. This network of devices, sometimes called the “Internet of Things” or “Machine-to-Machine” network (though it could also just be called “the Internet”), includes many embedded devices with very limited resources.

Protocols designed for typical ethernet networks, such as HTTP, are based around assumptions that no longer fit: they expect more bandwidth, processing power, and network reliability than may be available, and that networked devices will be on most of the time.

There is a more appropriate alternative, however: MQTT. It’s much lower in overhead, with only a 2-byte header for many messages. Its design suits devices that are suspended most of the time, with only occasional network activity. It also has support for reliable delivery built into the protocol, so simple sensors can just flag an outgoing message as requiring confirmed delivery and let the message broker take care of delivery reattempts. Using a standard messaging protocol for all communication also greatly reduces the surface area for possible security vulnerabilities. Read more on Getting Started with MQTT…

High-Quality Font Rendering for Embedded Systems

Setting up high-quality font rendering in a memory-constrained embedded system sounds like it would be hard. However, like many other problems in embedded systems, this one has pretty much already been solved by game developers. It turns out that getting proper font rendering, with kerning and all, can easily be done in a day’s work.

The game development communities are actually a great place to look for a lot of algorithms. Anything with graphics (even simple things like fonts), geometry/spatial algorithms (collision detection/prediction), or physical simulations have all gotten a lot of attention from the game developers. And their solutions are often mindful of memory and realtime constrains, which are of particular interest in embedded systems.

My particular use case is rendering text to a tiny black and white display (smaller than 150×50) in a resource-constrained environment (32k of RAM total) written in C. Read more on High-Quality Font Rendering for Embedded Systems…

Cheap, Long-Lasting, Low-Power Device Connectivity with Bluetooth LE

Imagine having a lamp in your house that you can control from your phone. You don’t need to pass Gigabytes of data back and forth, you only need a hand full of bytes to tell the lamp to turn on, or off, or report its current status. You also shouldn’t need to be in constant, on-going communication with the lamp. You should be able to connect long enough to send a command and then stop communicating. Every bit of data that is transmitted or received uses more energy, so reducing the data throughput reduces the energy usage.

This is exactly the type of interface provided by Bluetooth LE (marketed as “Bluetooth Smart“). New devices are entering the market today that, because of Bluetooth LE, are capable of doing things that have never been possible before. Read more on Cheap, Long-Lasting, Low-Power Device Connectivity with Bluetooth LE…

Lightweight Indexing for Small Strings

Lately, I have been investigating performance improvements for heatshrink, my data compression library for embedded systems. Since it may be processing sensor data in real-time, or compressing data as it transfers over a network, compression can directly impact overall throughput. After experiments with string search and indexing algorithms, I’ve settled on a particularly effective method, which I’m calling a “flattened linked-list index”.

Where’s the Time Going?

Before trying to speed it up, I measured where it was spending most of its time. Profiling revealed two hotspots:

  1. Detecting repeated patterns in recent history.
  2. Managing bit-buffering for its I/O.

Read more on Lightweight Indexing for Small Strings…

Comparing the Cost of Different Multiple-return Techniques in C

C’s design limits functions to directly returning at most one value. Unfortunately, there are many cases where returning more than one makes sense — returning a data buffer and its size, returning either a success code and data requested or an error code, splitting a tree node into two, etc. Making returning multiple values awkward has likely led to many security problems over the years, when people forget to track (or check) sizes associated with buffers. There still isn’t an obviously correct way to return multiple values, just a couple of methods with different trade-offs.

Three Common Techniques

The most common method involves mutation: The function returns one value, and additional values are written into pointers that were passed in by the caller. (They may or may not be NULL-checked.) While there are conventions for this, such as returning a status code and writing the result(s) into parameters, the language standard has no opinion. Passing around pointers to return values through can be a source of subtle errors, and (along with pointer arithmetic) also complicates static analysis.

Read more on Comparing the Cost of Different Multiple-return Techniques in C…

Making Real-Time Real Tame

One of my current personal projects is a home automation system, based around really cheap radio hardware. Unlike devices that provide a full wireless TCP/IP stack, such as Zigbee radios, these transmitters and receivers just give me control over the raw radio signal. (I’m intentionally using such barebones devices to learn more about radio.)

There are just four pins: power, ground, “data”, and antenna. The “data” line is just a GPIO pin — I need to create the radio signal myself. (In this case, I’m using on-off keying and Manchester coding, but more on that in another post.)

Read more on Making Real-Time Real Tame…