The software collects data from the FPGA and transmits it via TCP.
Similarly, the software accepts remote control commands via TCP and executes these by accessing registers in the FPGA.
### Operating system
The embedded software runs within an embedded Linux system on the Red Pitaya.
The core operating system is based on [Buildroot](https://buildroot.org/), a framework that builds an embedded Linux system from scratch.
Buildroot automatically downloads and builds many additional open source packages that are needed to construct the embedded system.
Among these packages is a cross-compilation toolchain.
The kernel for the embedded system is built from the [Xilinx distribution of the Linux kernel](https://github.com/Xilinx/linux-xlnx).
It is necessary to use the Xilinx variant of Linux.
The vanilla Linux kernel can run on the Red Pitaya, but the Xilinx kernel provides important features that are missing from the vanilla kernel, such as programming the FPGA.
The shell and system utilities of the embedded system are based on Busybox.
Dropbear is installed as SSH server, but disabled by default.
The set of packages to be installed in the embedded system, along with their compile-time settings, are specified in the Buildroot configuration.
Further tweaking of the embedded system is done via a _filesystem overlay_: a set of files that are injected into the embedded root filesystem as a final step.
The overlay mechanism is used to install boot scripts and configuration files.
It is also used to install custom user space software.
### Boot flow
[U-Boot](https://source.denx.de/u-boot/u-boot) is used as boot loader and as _secondary program loader_ (SPL).
Building U-Boot requires a proper device tree for the board.
Building the U-Boot SPL image also requires the file `ps7_init_gpl.c`.
The contents of this file depend on the configuration of the Zynq programming system which is specified in the Vivado block design.
To generate this file, the FPGA design must be _exported_ from Vivado in the form of an XSA file.
The XSA file is actually a ZIP archive which contains `ps7_init_gpl.c` and the corresponding header file.
The U-Boot build script (`12_build_uboot.sh`) copies these files to the correct location in the U-Boot source tree.
The boot process of the Red Pitaya is as follows:
- The Zynq PS boots from ROM.
The ROM program reads `boot.bin` from the SD card and runs it.
-`boot.bin` is the U-Boot SPL image.
It initializes many subsystems in the Zynq PS, including the DDR memory controller.
It then reads `u-boot.img` from the SD card and runs it.
Note that we don't use a Xilinx FSBL at all.
Many Zynq tutorials explain how to build `boot.bin` using Xilinx tools.
That requires a non-free FSBL image which must be built in a Vitis project.
The PuzzleFW package avoids that whole mess by using U-Boot SPL instead.
-`u-boot.img` is the main U-Boot program image.
It reads and executes `boot.scr`, a script which describes the Linux boot process.
The script instructs U-Boot to read the Linux kernel `uImage`,
the device tree `devicetree.dtb` and the root filesystem image `rootfs.cpio.uboot`
from the SD card.
It then runs the Linux kernel.
-`uImage` is the Linux kernel image.
The kernel initializes drivers and subsystems.
The root filesystem is unpacked and mounted as a RAM filesystem.
The filesystem is writeable, but any changes remain in RAM and will be forgotten on the next reboot.
- A set of boot scripts are invoked to initialize the embedded system and start services.
Most of these scripts are prepared by Buildroot, but some scripts have been tweaked or specially created for PuzzleFW.
These scripts take care of the following:
- Read the MAC address from the EEPROM on the Red Pitaya board, and assign it to the Ethernet interface.
- Set a unique hostname `rp-XXXXXX` based on the MAC address.
- Obtain an IPv4 address via DHCP or a preconfigured static address.
This is controlled by a file in the configuration partition of the SD card.
- Start an SSH server, if enabled. By default, this is disabled.
- Read a BIT file from the SD card and program the FPGA.
Information in the EEPROM is used to determine which FPGA image must be used.
- Configure ADCs on the Red Pitaya board via SPI or GPIO.
- Load the Linux kernel driver to access the FPGA firmware.
- Run a user space program that accepts remote control commands via TCP.
### Linux kernel driver
A custom Linux kernel module is used to access the FPGA firmware.
This module is called `puzzlefw.ko`.
The driver is based on the Linux UIO framework.
The kernel module itself has minimal functionality.
Its only functions are:
- mapping the register address range of the FPGA to user space via mmap
A Linux PC (x86-64) is required to build the system.
Development was done with Debian 12 (bookworm).
Other Linux distributions will probably also work.
The build process consists of two main steps.
These steps must be done in the following order:
1. Build the FPGA firmware for the Zynq PL.
2. Build the embedded software for the Zynq PS.
## Building FPGA firmware
Building the FPGA firmware requires Vivado release 2020.2.
Later releases will probably work, but may require adjustments.
There are two ways to build the firmware: as a Vivado project, or in Vivado non-project mode.
In most cases, non-project mode will be easier.
### Preparing the build environment
**Install Vivado** <br>
Download and install Vivado 2020.2.
The build scripts assume that Vivado is installed in `/opt/Xilinx/Vivado/2020.2`.
If a different location is used, it must be configured in the script environment file.
**Configure paths** <br>
If necessary, edit the file `redpitaya-puzzlefw/fpga/script_env` and make sure that `VIVADO_DIR` points to the directory where Vivado is installed.
**Install board files** <br>
When running Vivado in project mode, it requires board definition files
for the Red Pitaya.
These files are available from the original Red Pitaya FPGA repository.
Use the following steps to download and install these files.
- Go to directory `redpitaya-puzzlefw/fpga`
- Run `./01_get_redpitaya.sh`
- Copy the complete folder `redpitaya-puzzlefw/fpga/RedPitaya-FPGA/brd/redpitaya`
to `/opt/Xilinx/Vivado/2020.2/data/boards/board_files`
### Building FPGA firmware in non-project mode
There are two top-level designs, each targeting a specific Red Pitaya board type.
These designs are built in separate design runs:
-`puzzlefw_top` for the normal (2-input) Red Pitaya
-`puzzlefw_top_4ch` for the 4-input Red Pitaya
The following steps invoke Vivado in non-project mode to handle synthesis, implementation and bitfile generation of each top-level design.
- Go to directory `redpitaya-puzzlefw/fpga`
- Run `./11_build_bitfile.sh`
- Run `./12_build_bitfile_4ch.sh`
It is normal for the build process to display a large number of messages and warnings.
The build process produces the following output files:
-`puzzlefw_top.bit.bin`<br>
This file is required to program the FPGA.
-`redpitaya_puzzlefw.xsa`<br>
This file contains a description of the system configuration.
It is necessary to build U-boot.
- Similar files for the 4-input design.
The build process also creates various logs and report files in directory `vivado/output` (or `output_4ch`).
It is recommended to review the contents of `vivado/output/post_route_timing.rpt` to make sure that it does not report any timing violations and contains the line `All user specified timing constraints are met`.
### Building FPGA firmware in project mode
The repository contains a Vivado project file to build the design for the normal (2-input) Red Pitaya.
There is currently no project file for the 4-input board.
The following steps may be used to build the design in Vivado in project mode:
- Start Vivado, typically by running something like <br>
- Open the project file `redpitaya-puzzlefw/fpga/vivado/redpitaya_puzzlefw.xpr`.
- In the *Flow Navigator* panel on the left, click on "Run Synthesis", then click "Ok".
It may appear that very little is happening, but synthesis should be running in the background.
The status is displayed in the *Design Runs* panel at the bottom of the screen.
- When synthesis completes successfully, choose "Run Implementation" and click "Ok".
- When implementation completes successfully, choose "Generate Bitstream" and click "Ok".
- When bitstream generation completes successfully, choose "View Reports".
- In the *Project Summary* panel, check that timing is ok (Number of Falling Endpoints should be 0).
- In the main menu bar, click *File*, *Export*, *Export Hardware*.
Select "Pre-synthesis".
Change XSA file name and export folder to write the XSA file to
`redpitaya-puzzlefw/fpga/redpitaya_puzzlefw.xsa`.
Then click "Finish".
The build process produces a BIT file `puzzlefw_top.bit` containing the FPGA design.
The Zynq software needs this file in a different format (`.bit.bin`) which can not be created by the Vivado IDE.
Use the following steps to convert the BIT file:
- Go to directory `redpitaya-puzzlefw/fpga`
- Run `./make_binfile.sh vivado/redpitaya_puzzlefw.runs/impl_1/puzzlefw_top.bit`
### Editing the block design
Most source files for the FPGA firmware can be edited with any text editor.
The VHDL source files are in directory `redpitaya-puzzlefw/fpga/rtl`.
Constraint files are in `redpitaya-puzzlefw/fpga/constraints`.
The only exception is the block design that links the Zynq processing system to the FPGA logic.
The block design is stored as `redpitaya-puzzlefw/fpga/vivado/redpitaya_puzzlefw.srcs/sources_1/bd/puzzlefw/puzzlefw.bd`.
This file can only be edited through Vivado, as follows:
- Start Vivado as described above.
- Open the project file `redpitaya-puzzlefw/fpga/vivado/redpitaya_puzzlefw.xpr`.
- In the *Flow Navigator* panel on the left, click on "Open Block Design".
- Make changes as needed.
- In the main menu bar, click *File*, *Save Block Design*.
Both top-level designs (2-input and 4-input boards) use the same block design file.
This is a tricky situation because the block design file targets a specific FPGA part (e.g. `xc7z010clg400-1`), but the two boards have different FPGA parts.
When using the non-project build flow, we find that this just seems to work without issues.
During the build flow, Vivado quietly modifies the block design file to match the target part of the design run.
### Adding VHDL files to the project
When VHDL files are added to the project, both the Vivado project and the non-project build script must be updated.
To update the Vivado project:
- Open the project file `redpitaya-puzzlefw/fpga/vivado/redpitaya_puzzlefw.xpr`.
- In the *Flow Navigator* panel on the left, click "Add Sources".
- Add VHDL files directly from the directory `redpitaya-puzzlefw/fpga/rtl`.
Do not select "Copy sources into project".
- Vivado may get a confused because, by default, it does not support VHDL-2008.
To fix this, in the *Sources* panel, click "Libraries" below the hierachy tree.
Right-click each new VHDL file, choose "Set File Type" and change the file type to "VHDL 2008".
Vivado stores all project settings in the XPR file.
Changes to that file must be tracked in the Git repository.
To update the non-project build script:
- Open `redpitaya-puzzlefw/fpga/vivado/nonproject.tcl` in a text editor.
- Add `read_vhdl` statements to load the new VHDL files.
## Building embedded software
### Preparing the build environment
Install the following Debian packages (or equivalent packages on other Linux distributions):
<br>
`build-essential`,
`bc`,
`bzip2`,
`cpio`,
`dosfstools`,
`file`,
`git`,
`libncurses-dev`,
`mtools`,
`patch`,
`perl`,
`rsync`,
`unzip`,
`wget`.
### Downloading software
Use the following steps to download required software packages:
- Go to directory `redpitaya-puzzlefw/sw`
- Run `./01_get_buildroot.sh` to download [Buildroot](https://buildroot.org).
- Run `./02_get_uboot.sh` to download [U-Boot](https://source.denx.de/u-boot/u-boot).
- Run `./03_get_kernel.sh` to download the [Xilinx distribution of the Linux kernel](https://github.com/Xilinx/linux-xlnx).
### Building embedded software
Use the following steps to build the embedded software:
- Go to directory `redpitaya-puzzlefw/sw`
- Run `./11_build_buildroot.sh` to configure and build Buildroot.
<br>
One of the outcomes of this step is a cross-compilation toolchain for the ARM architecture.
Other build steps need this toolchain, therefore this must be the first step of the build flow.
- Run `./12_build_uboot.sh` to configure and build U-Boot.
<br>
This step requires the file `redpitaya-puzzlefw/fpga/redpitaya_puzzlefw.xsa` from the FPGA build flow.
- Run `./13_build_kernel.sh` to configure and build the Linux kernel.
- Run `./14_build_devicetree.sh` to build the device tree.
- Run `./15_build_driver.sh` to build the Linux kernel module that provides access to the FPGA firmware.
- Run `./16_build_userspace.sh` to build user space applications that will run on the Red Pitaya.
- Run `./21_rebuild_rootfs.sh` to re-build the embedded root filesystem, including the kernel driver and user space tools that were built during previous steps.
- Run `./22_prepare_sdcard.sh` to collect the files that must be installed on the SD card for the Red Pitaya.
- Run `./23_sdcard_image.sh` to build an image file which can be written to the SD card.
### Changing the Buildroot configuration
The Buildroot configuration for this project is stored in the folder `config`.
To change the configuration, start by applying the existing settings, then run the Buildroot configuration tool, then copy the new settings to the `config` folder.
Use the following steps:
- Copy `sw/config/buildroot_puzzlefw_defconfig` to `sw/buildroot-2023.02.08/.config`
- Go to `sw/buildroot-2023.02.8`
- Run `make olddefconfig`
- Run `make nconfig`
- Use the menu to change the configuration as needed.
- Run `make savedefconfig`
- Copy `sw/buildroot-2023.02.08/defconfig` to `sw/config/buildroot_puzzlefw_defconfig`
After changing the Buildroot configuration, rerun step `11_build_buildroot.sh`.
### Changing the U-Boot configuration
The U-Boot configuration for this project is stored in the folder `config`.
To change the configuration, start by applying the existing settings, then run the U-boot configuration tool, then copy the new settings to the `config` folder.
Use the following steps:
- Copy `sw/config/uboot_redpitaya_puzzlefw_defconfig` to `sw/u-boot/configs/redpitaya_puzzlefw_defconfig`
- Go to `sw/u-boot`
- Run `make redpitaya_puzzlefw_defconfig`
- Run `make nconfig`
- Use the menu to change the configuration as needed.
- Run `make savedefconfig`
- Copy `sw/u-boot/defconfig` to `sw/config/uboot_redpitaya_puzzlefw_defconfig`
After changing the Buildroot configuration, rerun step `12_build_uboot.sh`.
### Changing the Linux kernel configuration
The Linux kernel configuration for this project is stored in the folder `config`.
To change the configuration, start by applying the existing settings, then run the kernel configuration tool, then copy the new settings to the `config` folder.
Use the following steps:
- Copy `sw/config/linux_redpitaya_puzzlefw_defconfig` to `sw/linux-xlnx/.config`
- Go to `sw/linux-xlnx`
- Run `make ARCH=arm olddefconfig`
- Run `make ARCH=arm nconfig`
- Use the menu to change the configuration as needed.
- Run `make ARCH=arm savedefconfig`
- Copy `sw/linux-xlnx/defconfig` to `sw/config/linux_redpitaya_puzzlefw_defconfig`
After changing the Buildroot configuration, rerun step `13_build_kernel.sh`.
### Preparing the SD card
This section explains how to set up the SD card manually.
It is not normally necessary to do this.
An easier way to set up the SD card is by building an image file and writing it to the card.
The SD card contains two partitions:
-`/dev/mmcblk0p1` is a 256 MB partition with FAT filesystem