## Implementing State Machines

State machines are a great way to design software but they can be difficult to implement well.To illustrate this I’ll develop a simple state machine then increase the complexity to demonstrate some of the difficulties

We’ve all washed dishes before - it’s easy isn’t it? Scrub, rinse, dry, scrub, rinse dry. Scrub the dish until all of the gunk is off of it, rinse until the soap is off, put it in the drying rack. If you want to design software to implement this you have options. You...

## Understanding and Preventing Overflow (I Had Too Much to Add Last Night)

December 4, 2013

Happy Thanksgiving! Maybe the memory of eating too much turkey is fresh in your mind. If so, this would be a good time to talk about overflow.

In the world of floating-point arithmetic, overflow is possible but not particularly common. You can get it when numbers become too large; IEEE double-precision floating-point numbers support a range of just under 21024, and if you go beyond that you have problems:

for k in [10, 100, 1000, 1020, 1023, 1023.9, 1023.9999, 1024]: try: ...

## Delayed printf for real-time logging

You often debug by adding a few printfs and looking at the logs. In some real-time/low-level contexts though, you don't have time for text formatting.

You don't want prints to affect timing too much, because then timing-related bugs you're chasing might disappear. And you certainly don't want the system to stop functioning altogether because prints cause it to miss real-time deadlines.

A common alternative to prints is more "raw" logging - an event buffer, where event is a union keeping...

## Coroutines in one page of C

A coroutine is a function that you can jump back into after returning from it - and it remembers where it was in the code, and all the variables. This is very useful at times.

One use is generating a sequence of values. Here's how you can generate all the x,y pairs in a 2D range in Python:

def iterate(max_x, max_y): for x in range(max_x): for y in range(max_y): yield x,y for x,y in iterate(2,2): print x,y

This prints:

0 0 0 1 1 0 1 1

The yield keyword is like...

## Code Metrics - SLOC Count

August 19, 2013

Many programmers will start having flashbacks at the title of this article because it contains the words 'metrics' and 'SLOC'.  Newer programmers are probably wondering what all of the fuss is about - most probably have no negative connotations with the term 'code metrics' and some may not even know what SLOC is.  While there is much baggage associated with metrics and SLOC you shouldn't be afraid to gather fundamentally useful data such as SLOC count from your programming projects...

## [ C Programming Techniques: integer type optimization ]

May 22, 20131 comment

I am currently working on a voltage controller running on a ATMEGA328P, ATMEL AVR 8 bits microcontroller. The controller logic is implemented in the main() routine and relies on a periodical timer whose frequency is fixed at application setup. Among other things, the timer ISR handler increments some per tick counters which are then used by the main routine to implement the voltage controller timing logic.By looking at the code, one noticed that I use the uint8_t type for counters instead of...

## Interfacing LINUX with microcontrollers

Introduction

I am increasingly asked to work on small spare time projects where a user needs to control some device over the INTERNET. Recently, a friend needed to control heater relays and measure the temperature of its geographically distant secondary house. Another case relates to the control of a pan tilt home monitoring camera. A last one is the control of an old XY plotter DACs.

In both applications, the user wants to access the system over a web browser using HTTP. From the user...

## C Programming Techniques: Function Call Inlining

Introduction

Abstraction is a key to manage software systems as they increase in size and complexity. As shown in a previous post, abstraction requires a developper to clearly define a software interface for both data and functions, and eventually hide the underlying implementation.When using the C language, the interface is often exposed in a header '.h' file, while the implementation is put in one or more  corresponding '.c' files.

First, separating an interface from its...

## Data Hiding in C

Strictly speaking, C is not an object-oriented language. Although it provides some features that fit into the object-oriented paradigm it has never had the full object-oriented focus that its successor C++ offers. C++ introduced some very useful concepts and abilities that I miss when I’m developing in ANSI C. One such concept is protected member variables and functions.

When you declare a class in C++ you can also declare member variables and functions as part of that class. Often, these...

## Chebyshev Approximation and How It Can Help You Save Money, Win Friends, and Influence People

Well... maybe that's a stretch. I don't think I can recommend anything to help you win friends. Not my forte.

But I am going to try to convince you why you should know about Chebyshev approximation, which is a technique for figuring out how you can come as close as possible to computing the result of a mathematical function, with a minimal amount of design effort and CPU power. Let's explore two use cases:

• Amy has a low-power 8-bit microcontroller and needs to compute $\sqrt{x}$...

## C++ on microcontrollers 3 – a first shot at an hc595 class with 8 output pins

November 2, 2011

previous parts: 1, 2

This blog series is about the use of C++ for modern microcontrollers. My plan is to show the gradual development of a basic I/O library. I will introduce the object-oriented C++ features that are used step by step, to provide a gentle yet practical introduction into C++ for C programmers.  Reader input is very much appreciated, you might even steer me in the direction you find most interesting.

In the first part of...

## Introduction to Deep Insight Analysis for RTOS Based Applications

September 20, 20171 comment

Over the past several years, embedded systems have become extremely complex. As systems become more complex, they become harder and more time consuming to debug. It isn’t uncommon for development teams to spend more than 40% development cycle time just debugging their systems. This is where deep insight analysis has the potential to dramatically decrease costs and time to market.

Defining Deep Insight Analysis

Deep insight analysis is a set of tools and techniques that can be...

## Surprising Linux Real Time Scheduler Behavior

November 5, 2016

I have recently been helping with embedded software design and development for a data acquisition and visualization device. The software executes within an embedded Linux context and consists of various animated user interfaces rendering the acquired data.

The data is received via a UART and a SPI connection. During project development we noticed poor UART data latency issues during heavy user interface animations. For this product to properly meet its acquisition requirements, the UART...

## Vala applications on Embedded Linux: maybe a clever choice [part 1]

December 19, 2016

Vala is a sexy, open source, high level programming language that appeared in 2006, it counts with a modern typing system, is object oriented, compiled and statically typed, it has a almost identical syntax to C# and is maintained by GNOME. The language was created as a power abstraction of the GLib and GTK libraries, two considerably lightweight and powerful libraries written in C, and it is used in projects such as GNOME Clocks, Shotwell, GXml and Elementary OS.

namespace...

## Cutting Through the Confusion with ARM Cortex-M Interrupt Priorities

February 26, 2016

The insanely popular ARM Cortex-M processor offers very versatile interrupt priority management, but unfortunately, the multiple priority numbering conventions used in managing the interrupt priorities are often counter-intuitive, inconsistent, and confusing, which can lead to bugs. In this post I attempt to explain the subject and cut through the confusion.

The Inverse Relationship Between Priority Numbers and Urgency of the Interrupts

The most important fact to know is that ARM...

## The Hardest Bug I Never Solved

I agreed to four hours.

Four hours to help hunt down and kill a bug. A terrible malicious bug that was eating away at this project, wreaking havoc upon the foundations of a critical feature, and draining time randomly from every one of eight firmware engineers on this project. Quite honestly, I can’t remember the last time it took more than an hour or two for me to locate, isolate, and fix a firmware bug. Surely I could help find and solve this issue within four...

## Embedded Firmware Refactoring, Optimisation and Migration

March 29, 2016

Legacy products are often based on older hardware platforms which often become under-powered or run out of memory which constrains further product development. Customers are always looking for new features and improved performance but often either don’t want to invest in new hardware or need to retain the current field population of devices.

These are ongoing challenges for any product manufacturer, but are particularly highlighted in embedded systems where product...

## AI at the Edge - Can I run a neural network in a resource-constrained device?

Hello Related Communities,

This is my first time blogging since joining Stephane in November. He and I were at Embedded World together and he asked me to write about some of the important trends as they relate to all of you. I expect to post others in the near future, but the biggest trend in the embedded space was all of the activity around artificial intelligence (AI) at the edge.

This trend caught me a bit by surprise. I have been doing a lot of reading about AI over the last...

## Short Circuit Execution vs. Unit Testing

July 7, 20141 comment

The key to effective communication is to say what you mean and avoid ambiguity.  Words and phrases with multiple meanings can confuse your audience and hinder communication. That’s why so many programmers prefer writing code to writing specifications: written human language introduces ambiguity and subsequently, confusion. Code only has one interpretation, period. This doesn’t, however, ensure that the right message is getting through. Code can, indeed, only do one thing,...