Low Level C Programming with the Raspberry Pi 2 - Getting Started

Home / Low Level C Programming with the Raspberry Pi 2 - Getting Started

A lot of the work being done on the Raspberry Pi involves high-level programming using languages such as Python. However, the Raspberry Pi is also well-equipped for lower-level "embedded-style" programming (like programming a microcontroller). In this article I'll be going through how to create drivers that implement GPIO and I2C functionality on a Raspberry Pi. These drivers will be written in C, a lower-level programming language. The description and source code of these drivers are on their own pages. The links to these can be found at the bottom of this article.

Why would you want to program the Raspberry Pi like a microcontroller? To learn, mainly. After all, the Pi was originally developed for educational purposes. Another advantage is that programs written in C are capable of running much faster than programs written in high-level languages such as Python. There are many articles online which compare the speed of different languages on a Raspberry Pi. Here is one example that shows how much faster the C language can be.

To follow the code, you should be familiar with C, have a decent understanding of pointers/pointer arithmetic, and understand how bit manipulation works.

Introduction

Low-level programming of microcontrollers is different because it involves writing to registers. A register is a memory location that is frequently used by the CPU. The size of a register can vary and is usually measured in the number of bits (e.g 16-bit register). Writing certain values to these registers allows us to program the CPU.

But how do we know what we need to write to the registers? The datasheets will tell us. The Raspberry Pi Model A, B, B+, and Zero use the Broadcom BCM2835 chip. The Raspberry Pi 2 uses the Broadcom BCM2836 chip. At the time of writing this, the datasheet for the BCM2836 has not been released. However, the underlying architectures of the BCM2835 and BCM2836 are the same. For the scope of this article, the only difference between the two is that the addresses of the registers are different.

Implementing the Drivers!

The implementation and source code of the drivers are in separate articles. The links can be found below.

GPIO Driver
I2C Driver

Leave a Reply

Your email address will not be published. Required fields are marked *