Running bare metal blinker code on Raspberry Pi

Instead of running Linux on RPi, it is better to write the smallest code to run on RPi, so that we can learn how to control the individual CPU feature directly, or learn about the internal clocking mechanism on RPi, or how to do direct I/O via DMA on RPi etc etc.

Steps are as follows (all steps described here are based on Ubuntu 12.04 environment):

1. Make a primary partition on an SD Card via “fdisk /dev/sdX” if the SD Card is mounted as /dev/sdX, for myself the partition is sized at 40MB, too small seemed to have problem being automatically mounted in Ubuntu environment.

2. “mkfs.vfat /dev/sdX1” to format the newly created partition (from step 1) as vfat.

*** BE CARE HERE, running “mkfs.vfat /dev/your_hard_disk” and you will destroy your harddisk.***

3. “fdisk /dev/sdX” and change the “partition system id” to “c”, which is ” W95 FAT32 (LBA)”. This part is crucial, without which the RPi never seemed to automatically recognize the SDcard?

fdisk /dev/sdb

Command (m for help): p

Disk /dev/sdb: 3959 MB, 3959422976 bytes
37 heads, 8 sectors/track, 26125 cylinders, total 7733248 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xdaed00d7

Device Boot Start End Blocks Id System
/dev/sdb1 2048 83967 40960 c W95 FAT32 (LBA)

Command (m for help):

4. Copy the following files into the new partition:

bootcode.bin
fixup_cd.dat
fixup_x.dat
kernel_emergency.img
kernel.img
start_cd.elf
start.elf
start_x.elf

these files can be downloaded from below:

https://github.com/Hexxeh/rpi-firmware

https://github.com/raspberrypi/firmware/tree/master/boot

And for the bare metal codes, it is derived from here below:

https://github.com/dwelch67/raspberrypi

These files come as binary + source. If recompilation is desired, then just make sure that the ARM toolchain (eg, Code Sourcery from Mentor Graphics) is in the PATH environment variable.

For example:

which arm-none-eabi-ld

and the output:

/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/arm-none-eabi-ld

For the first blinker01:

make clean

and output:

rm -f *.o
rm -f *.bin
rm -f *.hex
rm -f *.elf
rm -f *.list
rm -f *.img
rm -f *.bc
rm -f *.clang.opt.s

After downloading and unpacking, “cd” to one of the sample directory and attempt compilation:

cd raspberrypi-master/blinker01>make

and output:

arm-none-eabi-as vectors.s -o vectors.o
arm-none-eabi-gcc -Wall -O2 -nostdlib -nostartfiles -ffreestanding -c blinker01.c -o blinker01.o
arm-none-eabi-ld vectors.o blinker01.o -T memmap -o blinker01.elf
arm-none-eabi-objdump -D blinker01.elf > blinker01.list
arm-none-eabi-objcopy blinker01.elf -O ihex blinker01.hex
arm-none-eabi-objcopy blinker01.elf -O binary blinker01.bin

Just copying “blinker01.bin” over to the SD card’s partition as “kernel.img” as the new filename, and you will be able to see the green ACT light blinking non-stop on the RPi.

Next target is to flash the ChibiOS on the SD card over-riding kernel.img:

http://www.stevebate.net/chibios-rpi/GettingStarted.html

Downloading and compiling the ChibiOS-RPi/demos/ARM11-BCM2835-GCC/

make
Compiler Options
arm-none-eabi-gcc -c -mcpu=arm1176jz-s -O2 -ggdb -fomit-frame-pointer -ffunction-sections -fdata-sections -Wall -Wextra -Wstrict-prototypes -Wa,-alms=build/lst/ -mno-thumb-interwork -MD -MP -MF .dep/build.d -I. -I../../os/ports/GCC/ARM -I../../os/ports/GCC/ARM/BCM2835 -I../../os/kernel/include -I../../test -I../../os/hal/include -I../../os/hal/platforms/BCM2835 -I../../boards/RASPBERRYPI_MODB -I../../os/various main.c -o main.o

mkdir -p build/obj
mkdir -p build/lst
Compiling ../../os/ports/GCC/ARM/crt0.s
Compiling ../../os/ports/GCC/ARM/chcoreasm.s
Compiling ../../os/ports/GCC/ARM/BCM2835/vectors.s
Compiling ../../os/ports/GCC/ARM/chcore.c
Compiling ../../os/kernel/src/chsys.c
Compiling ../../os/kernel/src/chdebug.c
Compiling ../../os/kernel/src/chlists.c
Compiling ../../os/kernel/src/chvt.c
Compiling ../../os/kernel/src/chschd.c
Compiling ../../os/kernel/src/chthreads.c
Compiling ../../os/kernel/src/chdynamic.c
Compiling ../../os/kernel/src/chregistry.c
Compiling ../../os/kernel/src/chsem.c
Compiling ../../os/kernel/src/chmtx.c
Compiling ../../os/kernel/src/chcond.c
Compiling ../../os/kernel/src/chevents.c
Compiling ../../os/kernel/src/chmsg.c
Compiling ../../os/kernel/src/chmboxes.c
Compiling ../../os/kernel/src/chqueues.c
Compiling ../../os/kernel/src/chmemcore.c
Compiling ../../os/kernel/src/chheap.c
Compiling ../../os/kernel/src/chmempools.c
Compiling ../../test/test.c
Compiling ../../test/testthd.c
Compiling ../../test/testsem.c
Compiling ../../test/testmtx.c
Compiling ../../test/testmsg.c
Compiling ../../test/testmbox.c
Compiling ../../test/testevt.c
Compiling ../../test/testheap.c
Compiling ../../test/testpools.c
Compiling ../../test/testdyn.c
Compiling ../../test/testqueues.c
Compiling ../../test/testbmk.c
Compiling ../../os/hal/src/hal.c
Compiling ../../os/hal/src/adc.c
Compiling ../../os/hal/src/can.c
Compiling ../../os/hal/src/ext.c
Compiling ../../os/hal/src/gpt.c
Compiling ../../os/hal/src/i2c.c
Compiling ../../os/hal/src/icu.c
Compiling ../../os/hal/src/mac.c
Compiling ../../os/hal/src/mmc_spi.c
Compiling ../../os/hal/src/mmcsd.c
Compiling ../../os/hal/src/pal.c
Compiling ../../os/hal/src/pwm.c
Compiling ../../os/hal/src/rtc.c
Compiling ../../os/hal/src/sdc.c
Compiling ../../os/hal/src/serial.c
Compiling ../../os/hal/src/serial_usb.c
Compiling ../../os/hal/src/spi.c
Compiling ../../os/hal/src/tm.c
Compiling ../../os/hal/src/uart.c
Compiling ../../os/hal/src/usb.c
Compiling ../../os/hal/platforms/BCM2835/hal_lld.c
Compiling ../../os/hal/platforms/BCM2835/pal_lld.c
Compiling ../../os/hal/platforms/BCM2835/serial_lld.c
Compiling ../../os/hal/platforms/BCM2835/i2c_lld.c
Compiling ../../os/hal/platforms/BCM2835/spi_lld.c
Compiling ../../os/hal/platforms/BCM2835/gpt_lld.c
Compiling ../../os/hal/platforms/BCM2835/pwm_lld.c
Compiling ../../os/hal/platforms/BCM2835/bcm2835.c
Compiling ../../boards/RASPBERRYPI_MODB/board.c
Compiling ../../os/various/shell.c
Compiling ../../os/various/chprintf.c
Compiling main.c
Linking build/ch.elf
Creating build/ch.hex
Creating build/ch.bin
Creating build/ch.dmp
Done

Now go to the subdirectory “build” and “ls -l”

20 -rwxrwxr-x 1 tthtlc tthtlc 17204 Jul 7 09:14 ch.bin
1340 -rw-rw-r– 1 tthtlc tthtlc 1371159 Jul 7 09:14 ch.dmp
140 -rwxrwxr-x 1 tthtlc tthtlc 169545 Jul 7 09:14 ch.elf
48 -rw-rw-r– 1 tthtlc tthtlc 48430 Jul 7 09:14 ch.hex
160 -rw-rw-r– 1 tthtlc tthtlc 163487 Jul 7 09:14 ch.map
4 drwxrwxr-x 2 tthtlc tthtlc 4096 Jul 7 09:14 lst
4 drwxrwxr-x 2 tthtlc tthtlc 4096 Jul 7 09:14 obj

Copy the “ch.bin” into SDcard’s and override the “kernel.img” and you should see another infinitely blinking ACT green light (on the RPi Model B).

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: