Jump to content
 
  • entries
    31
  • comments
    46
  • views
    21,592

Contributors to this blog

About this blog

Over the years programming microcontrollers we have noticed that some problems keep on popping up again and again.

In this blog we will try to address these problems like floating point numbers, concurrency, pointers, interrupts, CRC calculations, sign extension etc. over time. These blogs will not aim to be a full reference, instead they will introduce the problems, explore what can go wrong, show some ways to avoid these problems and provide a load of external links which the reader can go explore for a thorough education on the topic.Although these topics are explained in the context of embedded programming they actually carry over to any kind of programming, but somehow embedded programmers have fallen prey to these more often, perhaps because there is not a lot of material published on these in Embedded Programming communities, until now!

Entries in this blog

A question about structures, strings and pointers

In the comments of our blog on structures in c a member asked me a specific question about what they observed. As this code is a beautiful example of a number of problems we often see we thought it a good idea to make an entry just to discuss this as there really is a lot going on here. We will cover the following: What allocation and freeing of memory on the stack means and the lifetime of objects In which direction the stack usually grows (note - the C standard does not cont

Orunmila

Orunmila

Modular code and how to structure an embedded C project

All too often I see programmers stumped trying to lay out the folder structure for their embedded C project. My best advice is that folder structure is not the real problem. it is just one symptom of dependency problems. If we fix the underlying dependencies a pragmatic folder structure for your project will probably be obvious due to the design being sound. In this blog I am going to first look briefly at Modularity in general, and then explore some program folder structures I see oft

Orunmila

Orunmila

Comments are a code smell

Comments I was musing over a piece of code this week trying to figure out why it was doing something that seemed to not make any sense at first glance. The comments in this part of the code were of absolutely no help, they were simply describing what the code was doing. Something like this: // Add 5 to i i += 5; // Send the packet sendPacket(&packet); // Wait on the semaphore sem_wait(&sem); // Increment thread count Threadcount++; These comments just added to the noi

Orunmila

Orunmila

Hardware Abstraction

It has been said that software is the most complicated system humanity has created and like all complicated things we manage this complexity by breaking the problem into small problems.  Each small problem is then solved and each solution is assembled to create larger and larger solutions.  In other fields of work, humans created standardized solutions to common problems.  For example, nails and screws are common solutions to the problem of fastening wood together.  Very few carpenters worry abo

N9WXU

N9WXU

PWM Resolution

Something that comes up all the time, PWM resolution. Engineers are often disappointed when they find out that the achievable resolution of the PWM is not nearly what they expected from the headline claims made in the microcontroller datasheet. Here is just one example of a typically perplexed customer. Most of the time this is not due to dishonest advertising but rather a easily overlooked property of how PWM's work, so lets clear that up so that we can avoid the disappointment. A con

Orunmila

Orunmila

The Problems of I2C - common problems and errors with using I2C

I2C is such a widely used standard, yet it has caused me endless pain and suffering. In principle I2C is supposed to be simple and robust, a mechanism for "Inter-Integrated Circuit" communication.  I am hoping that this summary of the battle scars I have picked up from using I2C might just save you some time and suffering. I have found that despite a couple of typical initial hick-ups it is generally not that hard to get I2C communication going, but making it robust and reliable can prove t

Orunmila

Orunmila

Getting Started in Embedded Programming Pt 6

Here I am on a clear cool evening, by the fire outside with my laptop.  Tonight I will talk about a new peripheral, the timer/counter.  Periodically I will be interrupted to put another log on my fire, but it should not slow me down too much. Timers and counters are almost the same peripheral with only the difference of what is causing the counter to count.  If the counter is incrementing from a clock source, it becomes a timer because each count registers the passage of a precise unit of t

N9WXU

N9WXU

How to use PIC32 Linker Scripts with XC32 and MPLAB-X IDE

I decided to write this up as bootloaders have pretty much become ubiquitous for 32-bit projects, yet I was unable to find any good information on the web about how to use linker scripts with XC32 and MPLAB-X. When you need to control where the linker will place what part of your code, you need to create a linker script which will instruct the linker where to place each section of the program. Before we get started you should download the MPLAB XC32 C/C++ Linker and Utilities Users Gui

Orunmila

Orunmila

Concurrency and race conditions

There is one problem we keep seeing bending people's minds over and over again and that is race conditions under concurrency. Race conditions are hard to find because small changes in the code which subtly modifies the timing can make the bug disappear entirely. Adding any debugging code such as a printf can cause the bug to mysteriously disappear. We spoke about this kind of "Heisenbug" before in the lore blog. In order for there to be a concurrency bug you have to either have multipl

Orunmila

Orunmila

Getting Started in Embedded Programming Pt 5

It has been a busy few weeks but finally I can sit down and write another installment to this series on embedded programming. Today's project will be another peripheral and a visualization tool for our development machines.  We are going to learn about the UART.  Then we are going to write enough code to attach the UART to a basic C printing library (printf) and finally we are going to use a program called processing to visualize data sent over the UART.  This should be a fun adventure so l

N9WXU

N9WXU

About Translation Units and how C code is compiled

Beginners always have a hard time understanding when to use a header file and what goes in the .c and what goes in the .h.  I think the root cause of the confusion is really a lack of information about how the compilation process of a C program actually works. C education tends to focus on the semantics of loops and pointers way too quickly and completely skips over linkage and how C code is compiled, so today I want to shine a light on this a bit. C code is compiled as a number of "tr

Orunmila

Orunmila

Getting Started in Embedded Programming Pt 4

Today I am going to introduce a peripheral.  So far we have interacted with the I/O pins on the micro controller.  This can get you pretty far and if you can manipulate the pins quickly enough you can produce nearly any signal.  However, there are times when the signal must be generated continuously and your MCU must perform other tasks. Today we will be generating a PWM signal.  PWM stands for Pulse Width Modulation.  The purpose of PWM is to produce a fixed frequency square wave but the w

N9WXU

N9WXU

Getting started in embedded firmware pt 3

It is now time for my favorite introductory lab.  This lab is my favorite because it.... nope I will not spoil the surprise! Today we will do two labs and we will learn about digital inputs, and pushbuttons. First the digital input.  The PIC16F18446 has 20 pins spread across 3 ports.  PORTA, PORTB and PORTC.  Last time we did not learn about the digital I/O pins but we did learn how to drive a pin as an output.  It turns out that making a pin an output is pretty simple... Just clear th

N9WXU

N9WXU

Introduction to Assembly Language on a PIC16F18446

Assembly language may no longer be the mainstream way to write code for embedded systems, however it is the best way to learn how a specific CPU works without actually building one.    Assembly language is simply the raw instruction set of a specific CPU broken into easy to remember pneumonics with a very basic syntax.  This enables you full control of everything the CPU does without any translation provided by a compiler.  Sometimes this is the only reasonable way to do something that cannot be

N9WXU

N9WXU

Using : Extern "C"

I realize now that I have a new pet peeve. The widespread and blind inclusion, by Lemmings calling themselves embedded C programmers, of  extern "C" in C header files everywhere. To add insult to injury, programmers (if I can call them that) who commit this atrocity tend to gratuitously leave a comment in the code claiming that this somehow makes the code "compatible" with C++ (whatever that is supposed to mean - IT DOES NOT !!!). It usually looks something like this (taken from M

Orunmila

Orunmila

Getting Started with Embedded software Pt 2

Time for part 2!  Last time, I gave you the homework of downloading and installing MPLAB and finding a Curiosity Nano DM164144 .   Once you have done your homework, it is time for STEP 3, get that first project running. Normally my advice would be to breakout Mplab Code Configurator and get the initialization code up and running, but I did not assign that for homework!  So we will go old school and code straight to the metal.  Fortunately, our first task is to blink an LED. S

N9WXU

N9WXU

Getting started with Embedded software

I have often been asked "how do I get started" in embedded software.  As I think about this question, I realize that the basic steps to get started with embedded software are nearly identical to the basic steps required to bring up a new MCU or new hardware.  There is always a bootstrapping process and a logic progression of steps before you are "home-free" and building your product.  So here is my bootstrapping process, but broken down into each step so the process is clear for those just start

N9WXU

N9WXU

What every embedded programmer should know about ADC measurement

What every embedded programmer should know about ADC measurement, accuracy and sources of error ADC's you encounter will typically be specified as 8, 10 or 12-bit. This is however rarely the accuracy that you should expect from your ADC. It seems counter-intuitive at first, but once you understand what goes on under the hood this will be much clearer. What I am going to do today is take a simple evaluation board for the PIC18F47K40 (MPLAB® Xpress PIC18F47K40 Evaluation Board ) and de

Orunmila

Orunmila

Using the CDC Serial port on the PIC18F47K40 Xpress Evaluation Board

I recently got my hands on a brand new PIC18F47K40 Xpress board (I ordered one after we ran into the Errata bug here a couple of weeks ago). I wanted to start off with a simple "Hello World" app which would use the built-in CDC serial port, which is great for doing printf debugging with, and interacting with the board in general since it has no LED's and no display to let me know that anything is working, but immediately I got stuck. Combing the user's guide I could not find any mention of

Orunmila

Orunmila

Initializing Strings in C

Embedded applications are hard for a large number of reasons, but one of the main issues is memory.  Today I want to talk about how our C variables get initialized and a few assumptions we make as we use C to write embedded software. Let us take a few simple declarations such as we might make every day. char *string1 = "string1"; const char *string2 = "string2"; char const *string3 = "string3"; char * const string4 = "string4"; char const * const string5 = "string5";   In C99 th

N9WXU

N9WXU

How to struct - lessons on Structures in C

Structures in the C Programming Language Structures in C is one of the most misunderstood concepts. We see a lot of questions about the use of structs, often simply about the syntax and portability. I want to explore both of these and look at some best practice use of structures in this post as well as some lesser known facts. Covering it all will be pretty long so I will start off with the basics, the syntax and some examples, then I will move on to some more advanced stuff. If you are an

Orunmila

Orunmila

Tokenizing Keywords Pt 5

Today we are going to explore the weird and wonderful trie (try).  We are actually going to brute force a trie and we will write a basic code generator to synthesize a static trie so it our trusty NMEA token tester can stay in flash memory and fit in the PICmcu.  I promise this is going to be the LEAST memory efficient trie you ever will see, but it is quick and it is easy to understand so let us press on. First things first, what is a trie?  Simply, the trie takes the if-else construct we

N9WXU

N9WXU

Tokenizing Keywords Pt 4

This week we will discuss a different strategy for tokenizing our words.  This strategy will be to convert our word to a number in an unambiguous manner.  Then we can simply see if our number is one we recognize.  How hard can that be? The process of converting something into an unambiguous number is hashing.  Hash algorithms come in all shapes and sizes depending upon their application needs.  One nice hash is the Pearson hash which can make an 8-bit number of a string.  For a reasonably c

N9WXU

N9WXU

Tokenizing Keywords Pt 3

“Code generation, like drinking alcohol, is good in moderation.” — Alex Lowe This episode we are going to try something different.  The brute force approach had the advantage of being simple and easy to maintain.  The hand-crafted decision tree had the advantage of being fast.  This week we will look at an option that will hopefully combine the simplicity of the string list and the speed of the decision tree.  This week we will use a code generator to automatically create the tokenizi

N9WXU

N9WXU

The secret weapon of the 8-bit PICmcu

The PICmcu is not known for being fast or supporting large memories but it does have one feature that can significantly simplify developing your applications.  That feature is the lack of a hardware call stack. Sacrilege you say!  But wait... The lack of this important feature has caused the compiler team to develop an incredibly useful alternative that I would argue is better in nearly every way than an actual stack. For those of you who are now wondering what I am talking about,

N9WXU

N9WXU

×
×
  • Create New...