A collection of notes, scripts, and projects for Raspberry Pi development — covering setup, kernel building, bare-metal programming, Python projects, and more.
| Directory | Description |
|---|---|
android-11-rpi4/ |
Android 11 build guide and scripts for Raspberry Pi 4 |
bare-bones/ |
Bare-metal C/Assembly examples (blinky LED, UART driver) |
python-projects/ |
Python GPIO projects (blinky LED, light sensor with IFTTT) |
documents/ |
Reference PDFs, images, and VNC service files |
- 1. Getting Started
- 2. How does Pi boot
- 3. Building a Custom Kernel
- 4. Making our own minimalistic kernel
- 5. Ubuntu Snappy Core on RPi 2
- 6. Wi-Fi and Remote Access
Get the ARM toolchain:
git clone https://github.com/raspberrypi/tools
Get the kernel sources:
git clone --depth=1 https://github.com/raspberrypi/linux
Get the bootloader and firmware configs:
git clone https://github.com/raspberrypi/firmware.git
The Raspberry Pi will not boot without a properly formatted SD card containing the bootloader and a suitable operating system.
Write the Raspbian image to your SD card:
sudo dd bs=1M if=2015-05-05-raspbian-wheezy.img of=/dev/sdb
sync
Cloning a 4 GB SD card to an 8 GB SD card:
time sudo dd bs=64K conv=sync,noerror if=/dev/sdc of=/dev/sdb
Cloning a 32 GB SD card to a 16 GB SD card:
- Partition the 16 GB card with two partitions: FAT (lba) and EXT4.
- Mount both cards and use rsync to copy data:
cd /media/b-ak
rsync -ah --info=progress2 --partial ./boot/ ./boot1/
sudo rsync -ah --info=progress2 --partial ./7f593562-9f68-4bb9-a7c9-2b70ad620873/ ./4d7d6d24-a608-46fb-9b39-a4f30ddb8902/
Alternative: shrink the 32 GiB card first
Total Size in MiB = 14.77 * 1024 + 4 + 63 = 15192
bs (in dd command) = 4M, so 15192/4 = 3798 blocks
- Create the image:
sudo dd bs=4M of=dapm-91.img if=/dev/mmcblk0 count=3798
- Copy the image to the smaller SD card.
Generate SSH keys — follow the GitHub SSH guide
Initialize a repository:
git init
git remote add origin git@github.com:b-ak89/RPi.git
Commit and push:
git add README
git commit -m "first commit"
git push -u origin master
- When the Raspberry Pi is first turned on, the ARM core is off, and the GPU core is on. At this point the SDRAM is disabled.
- The GPU starts executing the first stage bootloader (ROM code), which is stored in ROM on the SoC. The first stage bootloader reads the SD card, and loads the second stage bootloader (bootcode.bin) into the L2 cache, and runs it.
- bootcode.bin enables SDRAM, and reads the third stage bootloader (loader.bin) from the SD card into RAM, and runs it.
- loader.bin reads the GPU firmware (start.elf).
- start.elf reads config.txt, cmdline.txt and kernel.img, and at last the ARM cpu is started running that kernel image.
loader.bin doesn't do much. It can handle .elf files, and so is needed to load start.elf at the top of memory (ARM uses SDRAM from address zero). There is a plan to add elf loading support to bootcode.bin, which would make loader.bin unnecessary, but it's a low priority (I guess it might save you 100ms on boot).
List all references (branches, tags, etc.) in a local kernel repository:
git show-ref
...
b6cafbf16c095a671b1a5e90dd734390f53046b2 refs/remotes/origin/rpi-4.4.y_rebase
237402141fd74ca989bd86ebb76d834cb6fa5454 refs/remotes/origin/rpi-4.5.y
2b1791a6693b0e22568426a5c99efa92403f3ffe refs/remotes/origin/rpi-4.6.y
c2cbd9c611256e7b957f75c23d9f76d58a4893c1 refs/remotes/origin/rpi-4.7.y
061dccce6cf6705bbb5da29a643f4b0ad1d11630 refs/remotes/origin/rpi-4.8.y
3f53e7886737a975e3fe76bc8ae6cc78f33c8cf8 refs/remotes/origin/rpi-4.9.y
ad9d278a772d0f725126f1d947427e1947d7a39a refs/remotes/origin/rpi-4.9.y-rebase
8fa2e19c4fdbe17704947e444674e19ea47c8ddd refs/remotes/origin/rpi-patches
935c7ce84c982a26f567a03a58a1537424569938 refs/remotes/origin/rpi-r.9.y
...
Check out a specific commit:
git checkout 3f53e7886737a975e3fe76bc8ae6cc78f33c8cf8
Or switch to a named branch:
git checkout rpi-4.9.y
Export the toolchain path:
export PATH=$PATH:/data/raspberry-pi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
Raspberry Pi (original, BCM2835):
cd linux
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
mkdir ../modules
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=../modules/ modules
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=../modules/ modules_install
cd ../tools/mkimage/
./imagetool-uncompressed.py ../../linux/arch/arm/boot/zImage
mv kernel.img /media/b-ak/boot/
cd /data/raspberry-pi/modules/lib
tar -cvzf modules.tar.gz *
mv modules.tar.gz /media/b-ak/13d368bf-6dbf-4751-8ba1-88bed06bef77/tmp/
Raspberry Pi 2/3 (BCM2836/BCM2837):
cd linux
KERNEL=kernel7
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs -j4
sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=/media/b-ak/13d368bf-6dbf-4751-8ba1-88bed06bef77/ modules_install
sudo scripts/mkknlimg arch/arm/boot/zImage /media/b-ak/boot/$KERNEL.img
sudo cp arch/arm/boot/dts/*.dtb /media/b-ak/boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /media/b-ak/boot/
sudo umount /media/b-ak/*
Alternatively, use the RPi 3 kernel builder script.
See also:
bare-bones/for source files.
We need three input files:
boot.s- kernel entry point that sets up the processor environmentkernel.c- your actual kernel routineslinker.ld- for linking the above files- Booting the kernel
The section .text.boot will be used in the linker script to place the boot.S as
the very first thing in our kernel image. The code initializes a minimum C environment,
which means having a stack and zeroing the BSS segment, before calling the kernel_main function.
Note that the code avoids using r0-r2 so the remain valid for the kernel_main call.
Assemble boot.S:
arm-none-eabi-gcc -mcpu=arm1176jzf-s -fpic -ffreestanding -c boot.S -o boot.o
The GPU bootloader passes arguments to the kernel via registers r0–r2, preserved by boot.S:
| Register | Value |
|---|---|
r0 |
Device the RPi booted from (usually 0) |
r1 |
ARM Linux Machine Type — 3138 (0xc42) for BCM2708 (full list) |
r2 |
Address of the ATAGs |
Compile kernel.c:
arm-none-eabi-gcc -mcpu=arm1176jzf-s -fpic -ffreestanding -std=gnu99 -c kernel.c -o kernel.o -O2 -Wall -Wextra
Create a linker.ld with the following structure:
ENTRY(_start)— declares the entry point (defined inboot.S).SECTIONS— places code and data at the correct addresses and creates boundary symbols.- The kernel is linked to start at
0x8000.
Example text section:
__text_start = .;
.text : {
KEEP(*(.text.boot))
*(.text)
}
. = ALIGN(4096); /* align to page size */
__text_end = .;
.text.bootis placed first to ensureboot.Sis at the start of the kernel image.- Sections are page-aligned (4096 bytes) for the RPi memory model.
__bss_startand__bss_endsymbols are used inboot.Sto zero the BSS segment. They can also be referenced from C withexterndeclarations.__startand__endcan be used at runtime to determine kernel size.
Link and convert to a binary image:
arm-none-eabi-gcc -T linker.ld -o myos.elf -ffreestanding -O2 -nostdlib boot.o kernel.o
arm-none-eabi-objcopy myos.elf -O binary myos.bin
arm-none-eabi-objcopycopies and transforms binary files; here it strips the ELF container to produce a raw kernel image.
References:
cp myos.bin /media/b-ak/boot/kernel.img
sync
umount /media/b-ak/*
Put the SD card into the Pi — it should boot your kernel.
git clone git://git.denx.de/u-boot.git
make ARCH=arm CROSS_COMPILE=arm-bcm2708-linux-gnueabi- rpi_2_defconfig
make ARCH=arm CROSS_COMPILE=arm-bcm2708-linux-gnueabi-
echo 'kernel=u-boot.bin' > /media/b-ak/boot/config.txt
Reference: RPi U-Boot — elinux.org
- Format a micro-SD card with a legacy MBR partition table (GPT is not supported by the RPi 2 ROM).
- Create a FAT32 boot partition and copy the following files into it:
bcm2709-rpi-2-b.dtb,bootcode.bin,start.elf,fixup.dat,u-boot.bin,zImage - Boot the RPi 2 and interrupt at the U-Boot prompt (press Enter on the serial console).
Run these U-Boot commands:
set fdtfile bcm2709-rpi-2-b.dtb
setenv bootargs "earlyprintk console=tty0 console=ttyAMA0 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait noinitrd"
saveenv
mmc dev 0
setenv zImage "fatload mmc 0:1 ${kernel_addr_r} zImage"
setenv devtree "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}"
setenv up "bootz ${kernel_addr_r} - ${fdt_addr_r}"
saveenv
run zImage
run devtree
run up
This should boot to the Linux kernel.
wget http://people.canonical.com/~platform/snappy/raspberrypi2/ubuntu-15.04-snappy-armhf-rpi2.img.xz
xzcat ubuntu-15.04-snappy-armhf-rpi2.img.xz | sudo dd of=/dev/sdc bs=32M
sync
bootcode.bin and start.elf are SPL bits available under LICENCE.broadcom
The following files were extracted from the Snappy image:
zImage→boot/vmlinuz-3.19.1-2-generic-bcm2709bcm2836-rpi-2-b.dtb→lib/firmware/3.19.1-2-generic-bcm2709/device-tree/bcm2709-rpi-2-b.dtb
kernel7.img is u-boot.bin built from swarren/u-boot with Ubuntu's arm-linux-gnueabihf cross-compiler.
boot.env reference (U-Boot environment, binary format)
mmc_boot was changed from:
if mmc dev ${devnum}; then setenv devtype mmc; run scan_dev_for_boot_part; fi
to:
if mmc dev ${devnum}; then setenv devtype mmc;
if run loadbootenv; then
echo Loaded environment from ${bootenv};
run importbootenv;
fi;
if test -n "$uenvcmd"; then
echo Running uenvcmd ...;
run uenvcmd;
fi;
run scan_dev_for_boot_part;
fi
Additional variables defined:
bootenv=uEnv.txt
loadbootenv=load ${devtype} ${devnum} ${loadaddr} ${bootenv}
importbootenv=echo Importing environment from mmc ...; env import -t -r $loadaddr $filesize
- Set up a hotspot on your phone or connect to a local router.
- Edit the WPA supplicant config:
sudo vi /etc/wpa_supplicant/wpa_supplicant.confctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB network={ ssid="TP-LINK" psk="12345678" key_mgmt=WPA-PSK priority=1 id_str="homeFlash" } network={ ssid="slyfox" psk="grapesaresour" key_mgmt=WPA-PSK priority=2 id_str="Nexus" }
- Install TightVNC server:
sudo apt-get install tightvncserver - Run
tightvncserverand follow the prompts to set passwords (a view-only password is optional).
- Install RealVNC Viewer on your desktop/laptop.
- Find the RPi's IP address:
or
sudo arp-scan --localnetsudo nmap -sP --disable-arp-ping 192.168.43.0/24 - Connect to the RPi using RealVNC Viewer.
- Copy the systemd service file to
/etc/systemd/system:sudo cp documents/vnc/tightvncserver.service /etc/systemd/system/ - Set ownership and permissions:
sudo chown root:root /etc/systemd/system/tightvncserver.service sudo chmod 755 /etc/systemd/system/tightvncserver.service - Enable and verify the service:
sudo systemctl enable tightvncserver.service - Reboot, then check that the service is running:
systemctl list-units | grep vnc tightvncserver.service loaded active running TightVNC remote desktop server - If the service file is updated, reload and restart:
sudo systemctl daemon-reload sudo systemctl restart tightvncserver.service
