Hardware: A Developer'S Guide To Programmable
Hardware: A Developer'S Guide To Programmable
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+
+ ){
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
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
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.
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.
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.
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.
4
A developer’s guide to programmable hardware
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.
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
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
How expensive is the device? Low cost Low to mid cost Can be expensive
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.