Skip to content

Instantly share code, notes, and snippets.

@cshegedus-dsp
Created February 21, 2024 11:07
Show Gist options
  • Save cshegedus-dsp/e6346426278f7e1aeba785cb6d337dbf to your computer and use it in GitHub Desktop.
Save cshegedus-dsp/e6346426278f7e1aeba785cb6d337dbf to your computer and use it in GitHub Desktop.

How to enable SPI on BeagleV®-Fire

If You want to use the SPI periphery of the beagleV Fire from a user-space application, just like on a microcontroller, You need to modify the device tree: You must add the spidev node to the spi periphery so linux adds the /dev/spidev character device driver on boot. The device tree is located in an image file in /boot/firmware/beaglev_fire.itb This file is a “Flattened Image Tree (FIT) Format”. While booting, U-Boot is loading this file. This image contains the kernel image and the device tree blob. To add spidev node You must:

  • Extract the kernel image from the FIT image
  • Extract the device tree blob from the FIT image
  • Decompile the device tree blob to get the device tree source
  • Add the spidev node to the device tree source
  • Compile the device tree source
  • Add the kernel image and device tree blob to a new FIT image
  • Replace the FIT image

All can be done on the beagleV Fire board using the UART terminal.

Make a backup of the original FIT image:

beagle@BeagleV:~$ cp /boot/firmware/beaglev_fire.itb /boot/firmware/beaglev_fire.itb_bkp
beagle@BeagleV:~$ cp /boot/firmware/beaglev_fire.itb ~/
beagle@BeagleV:~$ cd

Let’s see what's inside the FIT image:

beagle@BeagleV:~$ dumpimage -l beaglev_fire.itb

Extract the kernel image from the FIT image:

beagle@BeagleV:~$ dumpimage -T flat_dt -p 0 beaglev_fire.itb -o Image

Extract the device tree blob from the FIT image:

beagle@BeagleV:~$ dumpimage -T flat_dt -p 1 beaglev_fire.itb -o beaglev_fire.dtb

Decompile the device tree blob to get the device tree source:

beagle@BeagleV:~$ dtc -I dtb -O dts -o beaglev_fire.dts beaglev_fire.dtb

Add the spidev node to the device tree source:

beagle@BeagleV:~$ nano beaglev_fire.dts

Press Alt-C to enable line numbers add these line after line 389:

				spidev@0{
					compatible = "rohm,dh2228fv";
					reg = <0>;
					spi-max-frequency = <4000000>;
				};

Ctrl+O to save Ctrl+X to exit

Compile the device tree source:

beagle@BeagleV:~$ dtc -I dts -O dtb -o beaglev_fire.dtb beaglev_fire.dts

Add the kernel image and device tree blob to a new FIT image:

beagle@BeagleV:~$ nano beaglev_fire.its

Paste this:

/dts-v1/;

/ {
    description = "U-Boot fitImage for the BeagleV-Fire";
    #address-cells = <1>;

    images {
        kernel {
            description = "Linux kernel";
            data = /incbin/("./Image");
            type = "kernel";
            arch = "riscv";
            os = "linux";
            compression = "gzip";
            load = <0x80200000>;
            entry = <0x80200000>;
            hash {
                algo = "sha256";
            };
        };
        base_fdt {
            description = "Flattened Device Tree Blob";
            data = /incbin/("./beaglev_fire.dtb");
            type = "flat_dt";
            arch = "riscv";
            compression = "none";
			load = <0x8a000000>;
            hash {
                algo = "sha256";
            };
        };
    };
    configurations {
        default = "kernel_dtb";
        kernel_dtb {
            description = "1 Linux kernel, FDT blob";
            kernel = "kernel";
            fdt = "base_fdt";
        };
        base_dtb {
            description = "Base FDT blob for BeagleV-Fire board";
            kernel = "unavailable";
            fdt = "base_fdt";
        };
    };
};

Ctrl+O to save Ctrl+X to exit

beagle@BeagleV:~$ mkimage -f beaglev_fire.its beaglev_fire.itb

Replace the FIT image:

beagle@BeagleV:~$ sudo cp beaglev_fire.itb /boot/firmware/
beagle@BeagleV:~$ sudo reboot

After successful boot, you should see the spidev under /dev/spidev0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment