Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

A DEVELOPER’S GUIDE

TO PROGRAMMABLE
HARDWARE

//
ro
bo
#i tM
n ai
#i clu n.
nc de cc
#i lu
nc de <s
lu
d <stdio
e
in
t m "r tdli .h>
ob b.
in ain ot h>
t .h
fo ste (vo "
r(
1= ps id)
0; = 0 {
1< ;
=1
0;
i+
+ ){

Programmable hardware opens up the exciting world of hardware projects


to anyone with some programming skills. Here, we explain what it is and
how you can try it out for yourselves.
A developer’s guide to programmable hardware

There is a well-known axiom that software is easy, but


hardware is hard. This reflects the fact that creating new
hardware requires multiple operations, all of which impact on
each other. However, programmable hardware helps to bridge
this gap and offers developers a chance to create their own
hardware projects.

In this eBook, we explore what makes hardware so hard. We


look at the different types of programmable hardware and the
different languages for programming these. We finish with
some practical advice and simple examples for you to try.

Why do we need programmable hardware?


Hardware design has always been challenging to get right. You might think it’s just a question of designing
a circuit, etching a board and adding some components. Designing a working circuit can be hard enough.
But in the real world, you have to consider all sorts of additional factors. These include the operating toler-
ances of components, crosstalk and electromagnetic effects, even the capacitance of circuit tracks. This is
where programmable hardware comes into its own.

But what is programmable hardware?


Programmable hardware is a catch-all term for any form of hardware that can be configured or controlled
by software. Typically, the aim is to control some other piece of hardware or to react to some form of
sensor. This is distinct from typical programs that run on a CPU, where usually the aim is to process some
data. Of course, often traditional software also interacts with hardware, such as the display or a printer. But
programmable hardware is used for things like robotics, Internet of Things (IoT) and smart home control.

As we will see, programmable hardware comes in many forms. But fundamentally, all these have one
simple aim: To open up hardware development to software engineers. In effect, this is bridging the gap
between electronic engineering and software engineering.

1
A developer’s guide to programmable hardware

The basics of programming hardware


Before we look in more detail at programming hardware, it is worth seeing a simple example. This is based
on the Arduino Uno board (see the next chapter). The project is a simple lighting controller built from easily
available components. The circuit and code are shown below.

const int switchPin = 3;


const int redPin = 12;
const int greenPin = 13;
int switchVal = 0;

void setup() {
pimMode(switchPin, INPUT);
pimMode(redPin, OUTPUT);
pimMode(greenPin, OUTPUT);
}

void loop() {
switchVal = digitalRead(switchPin);
if (switchVal == HIGH) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else {
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, LOW);
}
}

This simple program illustrates some of the key aspects of any hardware program. You start with some
definitions to assign useful names to pins. Next, the setup() function sets the pins to the correct state.
Finally, you have a loop() function. This is analogous to a main() function. However, unlike with a conven-
tional program, this function loops continuously. In this program the value of the switchPin is constantly
monitored. If it goes high (connected to the 5v rail), then the red LED is turned on, and the green LED is
turned off. When the switchPin goes low, the opposite happens. Ideally, this program should be modified
to add a “debounce” to the switch. See the info box for more details.

Debouncing
When you program hardware, you need to understand something of how the hardware works.
Programming is essentially digital—things are either on, or off, high or low. But many components are
analogue. Switches are particularly tricky. They are mechanical devices. As you push the switch, the
state doesn’t change instantaneously. Instead, it may bounce between on and off several times. If you
try to read an input from the switch during this state, you can end up getting an errroneous reading.
The way round this is to use a technique called debouncing. To learn more about this, take a look at
the following blog:
https://www.mouser.com/blog/battling-the-bouncing-button
https://www.mouser.com/blog/battling-the-bouncing-button
https://www.mouser.com/blog/battling-the-bouncing-button

2
A developer’s guide to programmable hardware

Types of programmable hardware


There are a vast number of programmable hardware platforms on the market. But in general, we
can split these into three families.

Microcontrollers
INPUT Microcontrollers, or MCUs, are one of the
most common forms of programmable
hardware devices. A microcontroller has three
main elements: a CPU (or sometimes several
ARRAYS cores), some memory, and a set of I/O

CLOCK CONTROL UNIT MEMORY interfaces. These are all contained in a single
integrated circuit, often with other elements,
such as power management or integrated
ALU
clock circuits.

Microcontrollers are used in control


automation and other edge applications. They
OUTPUT are found in engine management systems,
smart home devices, and even medical
Simple block diagram of a microprocessor implants.

Arduino
There are many manufacturers of microproces-
sors, such as Microchip Technology, Infineon
Technologies, and STMicroelectronics. But the
majority of these only target professional users.
However, Arduino has made a name for itself by
providing easy-to-use accessible microcontroller
development boards. These range from the classic
Uno and tiny Nano, to the high-end MKR Zero and
recent Portenta H7. All these are available from
Mouser.

One of the key benefits of Arduino boards is the


“shields”. These are simple add-on daughter
boards that sit on top of the controller board and
Arduino Due and Relay shield give easy access to I/O capabilities.

3
A developer’s guide to programmable hardware

System on a chip
Offering more advanced programmable hardware is the
System-on-a-Chip (SoC). These are essentially complete
computers in a single chip. They offer powerful processing,
embedded memory, secondary storage, and I/O drivers for
peripherals like displays and cameras. They are often paired
with co-processors that provide additional capabilities, such
as accelerometers or other sensors.

The archetypal SoC is the mobile phone processor. Most of


these are based on ARM architecture because it offers both
high performance and low power consumption. SoC chips
like these are typically provided in a dense surface-mount
package, which makes them complex to use in a design.
However, there is a wide range of accessible, easy to use
SoC boards, such as Raspberry Pi and the Beagleboard
Beaglebone Black and Raspberry Pi family. These make it extremely easy to learn how to program
Model 4B
SoCs and use them in projects.

Programmable logic
As you may know, most integrated circuits are assembled
from complex sets of logic circuits and transistors. For
instance, counters are one of the most basic components in
most processors. A counter is simply a set of flip-flops
joined together in series. In turn, flip-flops are formed from
logic gates.

Programmable logic ICs provide a dense matrix of logic


gates that can be connected together arbitrarily. This allows
you to assemble any functionality you want, from registers to
arithmetic logic units. Probably the best known is the
field-programmable gate array or FPGA. These devices are
used by chip manufacturers during prototyping to try out
new features. Their high performance means they are also
ideal for creating programmable network hardware. Many
manufacturers offer FPGA development kits, such as Intel, Logic diagram and symbol for a
Xilinx, Microchip, and Lattice. In effect, these let you D-type flip.flop with set-reset
program your own integrated circuits and processors.

4
A developer’s guide to programmable hardware

Types of HW programming language


Programming languages are a way to abstract the underlying processing hardware. In theory, you can
program devices using machine code—the underlying instruction set for the CPU. This is what the
compiler produces when you build your code for a particular chipset.

Low-level languages
Assembly language provides a minimal level of abstraction from the machine code. In
assembly, you use system calls to interact with the processor. A typical assembly program
loads a system call into memory, then loads any required arguments. Finally, it executes the
call. Historically, software engineers used assembly language when memory and CPU
resources were scarce, but modern compilers create far more efficient code.

Hardware description languages


Hardware description languages (HDLs) allow you to describe digital circuits in code.
Verilog allows you to program logic circuits directly into hardware. It is syntactically similar to C, but
with some key differences. This makes it easy to learn if you are used to C or C++.

VHDL was designed to simulate new ASICs (integrated circuits). It allows you to describe any logic
circuit in text form and then simulate its behaviour in a testbench program.

High-level languages
High-level languages provide significant abstraction from the underlying hardware.
C abstracts from the underlying machine code while stull allowing low-level operations. For
instance, it provides control loops (for, while) and conditional statements (if, else). But it still
allows bitwise manipulation of the memory. This combination of abstraction with low-level
memory access makes it particularly suitable for programming things like network stacks.
C++ was designed to make it easier to do system-level programming, with concepts like
templates and object-orientation. Since it builds on C, it is also ideal for programming hard-
ware or embedded devices.

Python has found favour because it is simple to learn, yet extremely extensible. Python is
great for programming SoC devices like Raspberry Pi. Indeed, the “pi” in the name is actually
short for Python. Python based programming tools are also available for MCU’s from compa-
nies such as Zerynth.

Java abstracts away the underlying hardware using a Java virtual machine or JVM. Java code
runs on any system with a JVM. So, it can be used on SoC devices like Beaglebone Black.
These devices run complete operating system stacks, and so they can run a JVM.

Specialised languages
There are also a number of specialised languages for programming hardware. For instance, there
are hardware description languages for describing analogue circuits. Or there are task-specific
languages,https://p4.org/
likehttps://
P4, which is optimised for programming packet-switched networks. There are even
options for people that don’t like imperative languages like C++. For instance, Bluespec is an HDL
based on Haskell and designed for programming RISC processors, such as ARM.

5
A developer’s guide to programmable hardware

Choosing the right platform


Choosing the right platform is essential in any programmable hardware project. The following is
designed to help you make an informed choice. These are split into platform considerations and appli-
cation considerations.

Platform considerations
Hardware platforms have very different characteristics in terms of processing power, speed, power
drawn, etc. The following table summaries the main considerations.
Table: Choosing the right programmable hardware

Feature Microcontroller SoC FPGA

How powerful is the processor? Low to mid Mid to high Variable

What range of I/O is available? Limited Large range Unlimited

How much power does it need? Low Low High

How expensive is the device? Low cost Low to mid cost Can be expensive

How difficult is it to program? Easy Medium Complex

Of course, within each type of hardware, there are multiple options available with differing capabilities.
This is especially true for I/O, with some systems offering options like built-in camera interfaces, Blue-
tooth and WiFi.

Application considerations
The application itself may impose restrictions on your choice. For instance, where will the hardware be
deployed? Will it need to be battery powered? What problem does it need to solve? How will it interface
with the outside world? Does it need network connectivity? Often, you will find there’s no perfect
device, and you will have to compromise some requirements.

Real-life applications
Hardware programming is not just an abstract concept. Nor is it simply the preserve of hobbyists. There
are many key real-life applications. Here are just three:

» Automotive: MCUs are used in all modern vehicles. This includes engine management systems,
ABS, cruise control, parking assistance, and lane departure warning systems. Often, optional extras are
actually enabled or disabled by updating the car’s firmware.

» Edge ML. Machine learning is increasingly run at the network edge. This allows it to work where
there is no network connectivity or when real-time detection is vital. Devices like smart headphones and
intelligent door entry systems often include embedded ML models

» Line-rate FPGAs. High-speed networks now routinely achieve line-rates of 40Gbps or more. This
means each packet must be processed in tens of nanoseconds. FPGAs are the only devices fast
enough for this job. This is why high-performance NICs always include FPGAs.

6
A developer’s guide to programmable hardware

Conclusions:
Hopefully, this eBook has sparked your interest in programmable hardware. One of the challenges
can be working out where to get started. This is where devices like Beaglebone, Raspberry Pi, and
Arduino really come into their own. Nowadays, there are a huge range of open source hardware
vendors offering all kinds of add-ons to allow you to tackle a wide range of projects. Here are just a
few ideas for you to try:

» Robotics. Arduino is perfect for creating simple robots. You’ll need an Arduino, a motor controller
shield, a couple of motors with wheels, a simple chassis, and some sensors. Or simply buy a kit like
the Seeed Studios AlphaBot.

» Smart lighting. The Beaglebone Black Wireless is a great board for creating a smart lighting
project. It has on-board WiFi, allowing it to connect to smart-lighting hubs like the Philips Hue. You
can then use the Hue API to program your lighting.

» Home CCTV. Couple the Raspberry Pi ZeroW with a camera and create a home CCTV network.
You will find many tutorials online to help you. Many of them are based on MotionEye. There are even
optimised Raspberry Pi disc images specifically for this.

About Codemotion About Mouser


Codemotion is the platform that improves the Mouser Electronics is the worldwide leading
developers’ professional growth by connecting authorised distributor of semiconductors and
IT professionals, tech communities and com- electronic components, specialising in the
panies. We bring together the latest tech rapid introduction of new products and tech-
trends and best practices among the tech nologies for design engineers and buyers.
community worldwide, help companies to find Mouser’s Technical Resource Centre offers
the perfect dev-match and enhance tech keyword search of thousands of articles,
communities’ visibility. ebooks, videos and blogs on the latest tech-
nologies.
In a nutshell, We code the future. Together.

You might also like