Create Firmware Setup authored by Cole Hunt's avatar Cole Hunt
# Description
The Firmware Setup for the Raspberry Pi Zero 2W is a dual operation system with a bare-metal program running alongside an Ubuntu OS. The intent of this is to use a Linux environment to handle network connectivity and packet sending while still allowing the crazyflie firmware to run as a FreeRTOS program (as it natively does on a crazyflie). The system architecture is designed to operate as follows
![firmware-architecture.drawio.svg](uploads/b8b05d4dfdf636af80a92e067b064490/firmware-architecture.drawio.svg)
The Ubuntu portion of the system runs on 3 cores from the Pi Zero and the bare-metal program runs on one. They share data through a shared memory space which allows information like setpoints, parameter updates, etc to be sent from the ground station to the drone firmware.
# Prerequisites
- The current system is set up for Ubuntu Server 22.10 (64-bit) on the Raspberry Pi Zero 2W[OS Install Info](https://git.ece.iastate.edu/danc/MicroCART/-/wikis/Pi-Zero-Setup-Guide#os-install)
- Network Connection and SSH ability to the Raspberry Pi through the IASTATE network [Network Connection Info](https://git.ece.iastate.edu/danc/MicroCART/-/wikis/Pi-Zero-Setup-Guide#register-on-iastate-network)
- Access to either a Ubuntu Machine or VM
# U-Boot Setup
The following instructions are to set up u-boot to work as a bootloader and package the boot instructions to info the device to boot in a specific configuration. These instructions are adapted from the two following repos
- https://github.com/TImada/raspi4_freertos
- https://github.com/eggman/FreeRTOS-raspi3
## Cross-compiler
The U-Boot program will need to be cross-compiled for the ARM arch from a Ubuntu machine. The following steps describe how to set up the needed cross-compiler.
1. Download the [ARM GUN Toolchain](https://developer.arm.com/-/media/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-aarch64-none-elf.tar.xz?revision=ea238776-c7c7-43be-ba0d-40d7f594af1f&rev=ea238776c7c743beba0d40d7f594af1f&hash=62661FC8024B11FD84FB4C10C4907E1F) onto the development system
2. Extract the compressed release
```
$ tar -xJf <toolchain binary> -C <destination directory>
```
- Linux(x86_64) Example: `tar -xJf gcc-arm-9.2-2019.12-x86_64-aarch64-linux-gnu.tar.xz -C /path/to/destination/directory`
3. Confirm the installation using checksum
```
$ md5sum --check gcc-arm-9.2-2019.12-x86_64-aarch64-linux-gnu.tar.xz.asc gcc-arm-9.2-2019.12-x86_64-aarch64-linux-gnu.tar.xz:
```
4. Unpack the Toolchain
```
$ mkdir install-lnx
$ tar x -C install-lnx -f <filename>.tar.xz
$ PATH=`pwd`/install-lnx/aarch64/bin:$PATH
```
- Unless a setup script is created or an edit in made in your `.bashrc` you will need to add the cross-compiler back to your `$PATH` on every OS boot
## U-Boot Compilation
The following instructions will describe how to use the new cross-compiler to compile u-boot for the Raspberry Pi Zero 2W.
1. Download the u-boot source
```
$ git clone https://github.com/u-boot/u-boot
```
2. Setup u-boot compiler flags and configs
```
$ cd u-boot
$ export CROSS_COMPILE=aarch64-none-elf-
$ echo 'CONFIG_CMD_CACHE=y' >> ./configs/rpi_arm64_defconfig
$ make rpi_4_defconfig
```
3. Compile the u-boot binary
```
$ make -j4 (if your PC has 4 processor cores)
```
## Using U-boot
The following instructions will describe how to setup u-boot on the Raspberry Pi Zero 2W and access the u-boot command line
1. Copy over the compiler binary to the Raspberry Pi Zero 2W OS sd-card
```
$ sudo cp ./u-boot.bin /path/to/sd_boot_partition/kernel8.img
```
Example: `$ sudo cp ./u-boot.bin /media/<user/system-boot/kernel8.img`
- The new file must be named `kernel8.img`
2. Now you can place the sd-card back in the Raspberry Pi and Attempt to Boot
3. If the u-boot install was successful you should see this image in the top right and be prompted to `Press any key to disable auto boot` ![image](uploads/7989dd3dda1b5e1ebcaceb2c11c4287f/image.png)
\ No newline at end of file