FPGAs and Microcontrollers
FPGA is an initialism for Field Programmable Gate Array. They are digital logic chips that can be programmed with new logic designs in the same way that microcontrollers can be programmed with new firmware. On a microcontroller, a program is a series of instructions the CPU executes one at a time. Each instruction takes time to execute, program memory to store, and may access locations in RAM.
Designs for FPGAs are different from programs in microcontrollers. The design defines a series of digital logic circuits, the connections, RAM and ROMs they may use, multipliers, etc. Each element of the design consumes a physical location of the FPGA. In a microcontroller instructions are executed one at a time. In an FPGA all elements of the design can be active at once. This makes FPGAs very powerful. In fact, an FPGA can implement a microcontroller in addition to custom logic.
Inside many modern FPGAs there is an array of 4-input logic function generators, single bit storage elements, and other small bits of general purpose logic connected by a routing matrix. The image below is what a logic slice from the MachXO2 FPGA in the #TinyFPGA A-Series boards looks like.
MachXO2 FPGA Logic Slice. From the MachXO2 User Guide - http://www.latticesemi.com/view_document?document_id=38834
Rather than using individual 2-input logic gates, the LUT4 logic function generators or Look-Up Tables can generate any 4-bit logic function. The output of the logic generator can either be routed to another LUT4 elsewhere, or can be stored in the D Flip-Flop immediately connected to the output of the LUT4.
Many FPGA architectures group slices into larger elements. In the MachXO2 series from Lattice they are called PFUs, or Programmable Function Units. Since addition, subtraction, and comparison operators are so common, there is an optimized carry-chain between neighboring slices that allows for faster execution of such operations.
MachXO2 FPGA Programmable Function Unit. From the MachXO2 User Guide - http://www.latticesemi.com/view_document?document_id=38834
Between the PFUs is a routing matrix with many wires and switching matrices that allow for connections between PFUs and other components in the FPGA. In addition to the PFUs, FPGAs may contain other modules like RAMs, clocks, PLLs, non-volatile memory, and even hard-coded peripherals like SPI and i2c masters and slaves. The picture below is the basic layout of the MachXO2-1200 FPGA in the TinyFPGA A2 board.
MachXO2-1200 FPGA Top View. From the MachXO2 User Guide -http://www.latticesemi.com/view_document?document_id=38834
Every programmable element within an FPGA stores is programming locally. The LUT4 function generators for example contain 16-bits of information. The 4 bits of input can be interpreted as a numerical index into that information and the output is the value at that location. All of the routing programming contains storage bits that define how the design connects the various components. This configuration is stored in volatile storage elements. When the power goes off, they lose their state.
This means FPGAs need to be reprogrammed every time they power-on. Luckily there are automated mechanisms for FPGAs to load their configuration from an external flash memory, or even an internal flash memory on power-on.
In the #TinyFPGA A-Series this configuration flash is built into the FPGA itself. In the #TinyFPGA B-Series boards this configuration flash is on a separate SPI flash chip integrated on the board. Once a design is synthesized, it can be programmed onto the configuration flash on the board.
Developing for FPGAs
FPGAs come in chip packages that are difficult to integrate into hobbyist projects. Therefore there are development boards for FPGAs that contain the chip assembled on a PCB along with all the necessary support circuitry to operate the FPGA. The #TinyFPGA A-Series and #TinyFPGA B-Series boards are two such series of boards with differing capacities and capabilities. They are designed to be easy to integrate into existing hobbyist projects by plugging into breadboards. The low-cost of the boards allows them to be left in the project as a permanent component.
Designs for FPGAs are typically written using a hardware description language like Verilog or VHDL. However, there are also schematic entry tools that allow for graphical descriptions of digital logic circuits. The Lattice Diamond development environment for the MachXO2 FPGAs in the #TinyFPGA A-Series boards supports Verilog, VHDL, and schematic entry for developing designs. The iCECube2 development environment for the iCE40 FPGAs in the #TinyFPGA B-Series supports Verilog and VHDL, but it does not have a built-in schematic capture. However, there is an experimental graphical environment, icestudio, that may be used.
Uses for FPGAs
FPGAs can be used to solve a large variety of problems. They can act as a support or offload for a computer or microcontroller. Some examples:
- Input/Output Peripheral
- USB Device/Host
- SPI Slave/Master
- I2C Slave/Master
- Video Generator
- Keyboard Controller
- Physical Control and Interaction
- Precise Stepper Motor Control
- Servo Motor Control
- Sensor Reading
- Compute Acceleration
- High Speed Hash Functions
- Digital Signal Processing
- Neural Network Processing
- Graphics Acceleration
- Video Processing
- Audio Generation and Processing
These are just some examples of an FPGA being used to supplement a microcontroller. But an FPGA can be the central component of a project as well and utilize concepts from above:
- Homebrew Computer Designs
- Classic Computer Implementations
- Custom Microcontrollers and CPUs
- Esoteric CPUs
Another category small FPGA boards in particular can be used for is replacing components that are no longer available, hard to come by, or just not at your workbench at the moment:
- 74-series logic chips
- Classic computer peripheral chips
- Classic CPUs (6502, z80, 8080, etc.)
This list is just a tiny glimpse into the possibilities afforded by FPGAs. They are an indispensable component that every maker can benefit from.
Now we have a basic idea of what FPGAs are and what they can do. The next steps are learning how to develop for them.
If you are interested in getting started with FPGAs I encourage you to take a look at the TinyFPGA Tindie Store for some low-cost options. Tutorials and labs in this series will utilize those FPGA boards.