Stage 3: Write the Embedded Software

Our last posts about specifying the requirements for our design and designing the hardware to meet those requirements have laid the groundwork for everything we need to know and enables us to do the next stage in our design process: write some code for our microcontroller to program our FPGA.

A Haze of Code

The endless list of modern programming languages…

Writing code for computers of any size has evolved hugely over time. From the humble beginnings of punch cards to the modern high level languages like Java and C++, there has been some extraordinary progress in how we instruct computers to perform the tasks we want.

In the modern ecosystem of processing there are different languages for different levels of integration.

Assembler and C code are the usual choice for many embedded level programmers and C++, Java and Python are a significant few for application level programmers.

In this project we’re at the embedded level, so we get to choose between assembler and C. There are some libraries that exist for microcontrollers in C++ but they’re by no means ubiquitous in the industry as of yet.

Choosing between C and assembler is a choice made entirely upon two main aspects: the technical complexity of the task and the code latency/size requirements.

Assembler lends itself well to doing algorithmically simple tasks which are size and speed constrained. That’s because when you’re writing assembly, you’re manually telling the processor which specific opcode you’re using from the processors instruction set one instruction at a time.

C on the other hand lends itself well to the converse. C is much more adapted to doing complex algorithms in timing-relaxed memory-abundant environments.

When you write code in C, the instructions you specify can end up being one or more instructions of assembler in order to complete the task. C also had an added benefit in that it usually has supported libraries for the microcontroller you’re using making the implementation a lot easier for the most used functions. It’s for that reason that we’re going to write the program in C.

Setting up the toolchain

In this design we’re using a Microchip PIC32 microcontroller which has a tool-chain available for download called MPLAB-X.

This toolchain from microchip provides the ability to create projects and manage integration through their MPLAB-X integrated development environment. It does not however include the compiler libraries required in order to compile your source code into a programming file. In order to get your compiler, you have to download the XC compiler intended for your architecture. In this specific instance, we’re using a 32 bit processor, so we select the XC32 compiler.

This is what you need to get started with the PIC and program it “Bare Metal”. However, most manufacturers have preexisting code which can be used in order to make many jobs easier: Middleware.

Middleware is a fairly new phenomenon as microcontrollers become more powerful, complex and cheaper. It enables programmers to interact with an easier to interpret layer of code, removing the difficulty of driving complex peripherals and managing access to these from multiple different processes.

This is also a complexity/latency/size problem, similar to choosing the language it’s written in. For Microchip PIC products this middleware is called