Top Related Projects
Learning operating system development using Linux kernel and Raspberry Pi
Bare metal Raspberry Pi 3 tutorials
Quick Overview
The dwelch67/raspberrypi repository is a collection of bare metal programming examples and tutorials for various Raspberry Pi models. It provides low-level code samples and explanations for interfacing with Raspberry Pi hardware without an operating system, focusing on direct hardware manipulation and embedded systems programming.
Pros
- Offers hands-on experience with low-level hardware programming
- Covers multiple Raspberry Pi models, including older and newer versions
- Provides detailed explanations and comments in the code
- Includes examples for various peripherals and interfaces (GPIO, UART, etc.)
Cons
- May be challenging for beginners without prior embedded systems experience
- Some examples might be outdated for the latest Raspberry Pi models
- Limited documentation outside of code comments
- Requires additional hardware tools for debugging and flashing
Code Examples
- GPIO LED Blink (for Raspberry Pi 3):
#define GPFSEL1 0x3F200004
#define GPSET0 0x3F20001C
#define GPCLR0 0x3F200028
void gpio_init()
{
PUT32(GPFSEL1,GET32(GPFSEL1)&~(7<<18));
PUT32(GPFSEL1,GET32(GPFSEL1)|1<<18);
}
void led_on()
{
PUT32(GPSET0,1<<16);
}
void led_off()
{
PUT32(GPCLR0,1<<16);
}
- UART Initialization (for Raspberry Pi 2):
#define GPFSEL1 0x3F200004
#define GPPUD 0x3F200094
#define GPPUDCLK0 0x3F200098
#define AUX_ENABLES 0x3F215004
#define AUX_MU_IO_REG 0x3F215040
#define AUX_MU_IER_REG 0x3F215044
#define AUX_MU_IIR_REG 0x3F215048
#define AUX_MU_LCR_REG 0x3F21504C
#define AUX_MU_MCR_REG 0x3F215050
#define AUX_MU_LSR_REG 0x3F215054
#define AUX_MU_CNTL_REG 0x3F215060
#define AUX_MU_BAUD_REG 0x3F215068
void uart_init()
{
unsigned int ra;
PUT32(AUX_ENABLES,1);
PUT32(AUX_MU_IER_REG,0);
PUT32(AUX_MU_CNTL_REG,0);
PUT32(AUX_MU_LCR_REG,3);
PUT32(AUX_MU_MCR_REG,0);
PUT32(AUX_MU_IER_REG,0);
PUT32(AUX_MU_IIR_REG,0xC6);
PUT32(AUX_MU_BAUD_REG,270);
ra=GET32(GPFSEL1);
ra&=~(7<<12); //gpio14
ra|=2<<12; //alt5
ra&=~(7<<15); //gpio15
ra|=2<<15; //alt5
PUT32(GPFSEL1,ra);
PUT32(GPPUD,0);
for(ra=0;ra<150;ra++) dummy(ra);
PUT32(GPPUDCLK0,(1<<14)|(1<<15));
for(ra=0;ra<150;ra++) dummy(ra);
PUT32(GPPUDCLK0,0);
PUT32(AUX_MU_CNTL_REG,3);
}
Getting Started
To use these examples:
Competitor Comparisons
Pros of pico-sdk
- Official SDK for Raspberry Pi Pico, providing comprehensive support and documentation
- Regularly updated with new features and bug fixes
- Includes a rich set of examples and libraries for various peripherals
Cons of pico-sdk
- Focused solely on Raspberry Pi Pico, limiting its use for other Raspberry Pi models
- Requires more setup and configuration compared to the simpler approach in raspberrypi
Code Comparison
pico-sdk:
#include "pico/stdlib.h"
int main() {
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
while (true) {
gpio_put(PICO_DEFAULT_LED_PIN, 1);
sleep_ms(500);
gpio_put(PICO_DEFAULT_LED_PIN, 0);
sleep_ms(500);
}
}
raspberrypi:
void PUT32 ( unsigned int, unsigned int );
unsigned int GET32 ( unsigned int );
void dummy ( unsigned int );
int notmain ( void )
{
unsigned int ra;
while(1)
{
PUT32(GPSET0,1<<16);
for(ra=0;ra<0x100000;ra++) dummy(ra);
PUT32(GPCLR0,1<<16);
for(ra=0;ra<0x100000;ra++) dummy(ra);
}
return(0);
}
Pros of pico-examples
- Officially maintained by Raspberry Pi, ensuring up-to-date and reliable examples
- Comprehensive coverage of Raspberry Pi Pico features and capabilities
- Well-documented and structured for easy understanding and implementation
Cons of pico-examples
- Focused solely on Raspberry Pi Pico, limiting its applicability to other Raspberry Pi models
- May lack some advanced or specialized examples found in community-driven repositories
Code Comparison
pico-examples:
#include "pico/stdlib.h"
int main() {
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
}
raspberrypi:
#include "gpio.h"
void notmain ( void )
{
unsigned int ra;
for(ra=0;ra<3;ra++) uart_putc('.');
uart_putc(0x0D);
uart_putc(0x0A);
}
The pico-examples code demonstrates a more modern and specific approach to Raspberry Pi Pico programming, while the raspberrypi example shows a lower-level, generic implementation that could work across multiple Raspberry Pi models.
Learning operating system development using Linux kernel and Raspberry Pi
Pros of raspberry-pi-os
- More comprehensive tutorial-style approach with detailed explanations
- Focuses on building a complete OS from scratch
- Includes more advanced topics like virtual memory and user processes
Cons of raspberry-pi-os
- Less variety in examples and hardware support
- Slower update frequency compared to raspberrypi
Code Comparison
raspberrypi:
void PUT32 ( unsigned int, unsigned int );
unsigned int GET32 ( unsigned int );
void dummy ( unsigned int );
void enable_irq ( void );
void enable_fiq ( void );
raspberry-pi-os:
#define PERIPHERAL_BASE 0x3F000000
#define GPFSEL1 (PERIPHERAL_BASE + 0x00200004)
#define GPSET0 (PERIPHERAL_BASE + 0x0020001C)
#define GPCLR0 (PERIPHERAL_BASE + 0x00200028)
#define GPPUD (PERIPHERAL_BASE + 0x00200094)
The raspberrypi repository provides low-level hardware access functions, while raspberry-pi-os defines memory-mapped I/O addresses for GPIO manipulation.
Both repositories offer valuable resources for Raspberry Pi bare-metal programming, with raspberrypi providing a wider range of examples and hardware support, and raspberry-pi-os offering a more structured approach to building a complete operating system from the ground up.
Bare metal Raspberry Pi 3 tutorials
Pros of raspi3-tutorial
- More comprehensive and structured tutorial approach
- Covers a wider range of topics, including graphics and USB
- Better documentation and explanations for beginners
Cons of raspi3-tutorial
- Less frequently updated compared to raspberrypi
- Focuses primarily on Raspberry Pi 3, while raspberrypi covers multiple versions
Code Comparison
raspberrypi:
void PUT32 ( unsigned int addr, unsigned int data )
{
*((volatile unsigned int*)addr) = data;
}
raspi3-tutorial:
static inline void mmio_write(uint32_t reg, uint32_t data)
{
*(volatile uint32_t*)reg = data;
}
Both repositories provide low-level hardware access functions, but raspi3-tutorial uses more modern C syntax and naming conventions.
Summary
raspi3-tutorial offers a more structured learning experience with better documentation, making it ideal for beginners. However, raspberrypi covers a broader range of Raspberry Pi versions and is updated more frequently. The choice between the two depends on the user's specific needs and experience level.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
The main directory examples are for the older/est pi1 boards maybe some attempts at newer boards. These are retained for legacy reasons some folks have direct links. They are still valid for those boards. The boards directory has the first attempt at solving the too many boards issue and that is where I recommend you go. My third string attempt is I have a raspberrypi-zero and raspberrypi-three repos that are for those boards specifically. Little to no verbage though, the rambling text is mostly in this repo.
This repo is getting a bit messy starting with the first released to the public pi and then all that came after. I want to rework it but it is going slowly. Recently started creating directories pi1, piaplus and so on to target the examples specifically to the different cards. Again that is slow going. Likewise this README will be rewritten perhaps but for now...
This repo serves as a collection of low level examples. No operating system, embedded or low level embedded or deeply embedded or bare metal, whatever your term is for this.
I am in no way shape or form associated with the raspberry pi organization nor broadcom. I just happen to own one (some) and am sharing my experiences. The raspberry pi is about education, and I feel bare metal education is just as important as Python programming.
So I started this years ago when got my first ARM11 based raspberry pi maybe we call that a raspberry pi 1, I dont know a good term. But now there is a growing number of variations.
ARM11 based (BCM2835) Raspberry Pi B Raspberry Pi B2 Raspberry Pi A+ Raspberry Pi B+ Raspberry Pi Zero Cortex-A7 based (BCM2836) Raspberry Pi 2 B Cortex-A53 based (BCM2837) Raspberry Pi 3 B
There is also the compute module but I dont have one of those.
General differences that we care about for these examples. The amount of ram varies from board to board. The peripheral base address is different between the BCM2835 and BCM2836. The BCM2835 looks for the file kernel.img the BCM2836 looks for kernel7.img. The ARM11 based Zero is a B with stuff removed and a new layout, but they up/over clocked the processor from 750MHz to 1000MHz, one led on gpio 16. The A+ and B+ they moved the led (or put two) on gpio 35 and 47. The raspberry pi 2 is B+ like but with the different chip, supposedly the BCM2836 is BCM2835 with the ARM11 core removed and replaced with the Cortex A7 and for the most part it appears to be. The raspberry pi 3 is Cortex A8 based, 64 bit. And they moved the LED (the leds) to an i2c gpio expander.
As of this writing I am adding plus and pi2 versions of the examples as many of them are based on the early/original. No guarantees I will do that, just looking at the differences between the blinker01 examples should show you what to do to convert these yourself. In some cases I am intentionally not trying to have one code base build for all three with ifdefs and such, keep it simple stupid then complicate it as needed. The text may say kernel.img but substitute with kernel7.img as needed.
I still have a number of raspberry pi 2 examples to port, and now a bunch of examples to port to the raspberry pi 3. The raspberry pi 3 as of this writing, without a config.txt, is switched to 32 bit compatibility mode. See the aarch64 directory for 64 bit ARM examples.
From what we know so far there is a gpu on chip which:
- boots off of an on chip rom of some sort
- reads the sd card and looks for additional gpu specific boot files bootcode.bin and start.elf in the root dir of the first partition (fat32 formatted, loader.bin no longer used/required)
- in the same dir it looks for config.txt which you can do things like change the arm speed, or change the address where to load kernel.img, and many others
- it reads kernel.img the arm boot binary file and copies it to memory
- releases reset on the arm such that it runs from the address where the kernel.img data was written
The memory is split between the GPU and the ARM, I believe the default is to split the memory in half. And there are ways to change that split (to give the ARM more)(using config.txt). Not going to worry about that here.
From the ARMs perspective the kernel.img file is loaded, by default, to address 0x8000. (there are ways to change that, not going to worry about that right now).
Hardware and programming information:
You will want to go here http://elinux.org/RPi_Hardware And the datasheet and schematic. These are moving targets the above elinux link has the datasheet and errata which is important. They didnt give us a full datasheet for the BCM2836 have to go with the BCM2835. You will want to go to http://raspberrypi.org and then the forum tab then slide down to the Bare Metal forum, the first (only) Sticky topic is Bare Metal Resources. There are many more links there for good information. Also go to http://infocenter.arm.com and get the Architectural Reference Manual and the Techincal Reference Manual for the ARM1176JZF-S (BCM2835) and/or the Cortex-A7 (BCM2836).
Early in the BCM2835 document you see a memory map. I am going to operate based on the middle map, this is how the ARM comes up. The left side is the system which we dont have direct access to in that form, the gpu probably, not the ARM. The ARM comes up with a memory space that is basically 0x40000000 bytes in size as it mentions in the middle chart. The bottom of this picture shows total system sdram (memory) and somewhere between zero and the top of ram is a split between sdram for the ARM on the bottom and a chunk of that for the VC SDRAM, basically memory for the gpu and memory shared between the ARM and GPU to allow the ARM to ask the GPU to draw stuff on the video screen. 256MBytes is 0x10000000, and 512MBytes is 0x20000000. Some models of raspberry pi have 256MB, newer models have 512MB total ram which is split between the GPU and the ARM. Assume the ARM gets at least half of this. Peripherals (uart, gpio, etc) are mapped into arm address space at 0x20000000. When you see 0x7Exxxxxx in the manual replace that with 0x20xxxxxx as your ARM physical address. Experimentally I have seen the memory repeats every 0x40000000, read 0x40008000 and you see the data from 0x8000. From the Broadcom doc this looks to be giving us access to the memory with different caching schemes (cached, uncached, etc) depending on which upper address bits you use. Most likely to allow more room for RAM the Raspberry Pi 2 uses a peripheral base address of 0x3Fxxxxxx instead of the 0x20xxxxxx.
I do not normally zero out .bss or use .data so if you do this to my examples
int x; fun() { static int y; }
dont assume x and y are zero when your program starts. Nor if you do this
int x=5; fun() { static int y=7; }
will x=5 or y=7.
See the bssdata directory for more information, you can most likely use the linker script to solve the problem for you since .text, .data, .bss, (.rodata), everything lives in ram.
Nor do I use gcc libraries nor C libraries so you can build most if not all of my examples using a gcc cross compiler. Basically it doesnt matter if you use arm-none-linux-gnueabi or arm-none-eabi. I have not looked in a while but formerly codesourcery.com (now a part of Mentor Graphics) had a free LITE version of their toolchain which was pretty easy to come by. An even easier place is here https://launchpad.net/gcc-arm-embedded to get a cross compiler. Building your own toolchain from gnu sources (binutils and gcc) is fairly straight forward see my build_gcc repository for a build script (Linux only but from that you might get other platforms to build). And also remember that you can run linux on the pi and on that it has a native, not cross, gnu toolchain.
As far as we know so far the Raspberry Pi is not "brickable". Normally what brickable means is the processor relies on a boot flash and with that flash it is possible to change/erase it such that the processor will not boot normally. Brickable and bricked sometimes excludes things like jtag or special programming headers. From the customers perspective a bricked board is...bricked. But on return to the vendor they may have other equipment that is able to recover the board without doing any soldering, perhaps plugging in jtag or some other cable on pins/pads they have declared as reserved. Take apart a tv remote control or calculator, etc and you may see some holes or pads on the circuit board, for some of these devices just opening the battery case you have a view of some of the pcboard. This is no doubt a programming header. Long story short, so far as I know the Raspberry Pi is not brickable because it boots off of an sd card which we can easily remove and replace ourselves. I dont know for sure, a lot more info is out about the GPU since I started with this, but I assume that there is some GPU code that boots off of an internal rom, I doubt with two on chip processors they created pure logic to read the sd card, wade through the filesystem to find a specific bootcode.bin file, load that into ram and run it. If that assumption is true is that on chip rom one time programmable or can it be erased/reprogrammed, and if the latter how lucky do we have to be with a broken program to erase that? So I am not 100% sure but almost 100% sure the Raspberry Pi is not brickable. This is actually a big deal for bare metal programming, in particular if it is your first platform. With decades of experience I still make mistakes from time to time and brick a board, never to be recovered.
To use my samples you do not need a huge sd card. Nor do you need nor want to download one of the linux images, takes a while to download, takes a bigger sd card to use, and takes forever to write to the sd card. I use the firmware from http://github.com/raspberrypi. The minimum configuration you need to get started at this level is:
go to https://github.com/raspberrypi, you DO NOT need to download the repo, they have some large files in there you will not need (for my examples). go to the firmware directory and then the boot directory. For each of these files, bootcode.bin and start.elf (NOT kernel.img, dont need it, too big)(loader.bin is no longer used/required). Click on the file name, it will go to another page then click on View Raw and it will let you download the file. For reference, I do not use nor have a config.txt file on my sd card. I only have the minimum number of files on the sd card, bootcode.bin, start.elf and either kernel.img or kernel7.img (or sometimes both).
My examples are basically the kernel.img file. Not a linux kernel, just bare metal programs. Since the GPU bootloader is looking for that file name, you use that file name. The kernel.img file is just a blob that is copied to memory, dont worry about what they named it.
What I do is setup the sd card with a single partition, fat32. And copy the above files in the root directory. bootcode.bin and start.elf. From there you take .bin files from my examples and place them on the sd card with the name kernel.img. It wont take you very long to figure out this is going to get painful.
- power off raspi
- remove sd card
- insert sd card in reader
- plug reader into computer
- mount/wait
- copy binary file to kernel.img
- sync/wait
- unmount
- insert sd card in raspi
- power raspi
- repeat
There are ways to avoid this, one is jtag, which is not as expensive as it used to be. It used to be in the thousands of dollars, now it is under $50 and the software tools are free. Now the raspi does have jtag on the arm, getting the jtag connected requires soldering on older of the older models, but unless you were an early adopter, you dont need to worry about that all the signals are on the P1 connector. How to use the jtag and how to hook it up is described later and in the armjtag sample.
Another method is a bootloader, typically you use a serial port connected to the target processor. That processor boots a bootloader program that in some way, shape, or form allows you to interact with the bootloader and load programs into memory (that the bootloader is not using itself) and then the bootloader branches/jumps to your program. If your program is still being debugged and you want to try again, you reboot the processor the bootloader comes up and you can try again without having to move any sd cards, etc. The sd card dance above is now replaced with the bootloader dance:
- power off raspi
- power on raspi
- type command to load and start new program
Or if you solder on a reset button
- reset raspi
- type command to load and start new program
I have working bootloader examples. bootloader05 is currently the last of the xmodem based ones (that basically take a kernel.img file), personally I use bootloader07 which takes an intel hex formatted file which these examples also build. The .bin file would be used with bootloader05, the .hex with bootloader07. But you need more hardware (no soldering is required). For those old enough to know what a serial port is, you CANNOT connect your raspberry pi directly to this port, you will fry the raspberry pi. You need some sort of serial port at 3.3V either a level shifter of some sort (transceiver like a MAX232) or a usb serial port where the signals are 3.3V (dont need to use RS232 just stay at the logic level). The solution I recommend is a non-solder solution:
A recent purchase, experimentally white is RX and green is TX, black GND http://www.nexuscyber.com/usb-to-ttl-serial-debug-console-cable-for-raspberry-pi Sparkfun has one https://www.sparkfun.com/products/12977 As does Adafruit https://www.adafruit.com/products/954 The above, assuming you figure out rx from tx, are all you need. The ones below you may need to solder or may need some jumper wires.
http://www.sparkfun.com/products/9873 plus some male/female wire http://www.sparkfun.com/products/9140
Solutions that may involve soldering http://www.sparkfun.com/products/718 http://www.sparkfun.com/products/8430
Or this for level shifting to a real com port. http://www.sparkfun.com/products/449
Or see the pitopi (pi to pi) directory. This talks about how to take two raspberry pi's and connect them together. One being the host/development platform (a raspberry pi running linux is a native arm development platform, no need to find/get/build a cross compiler) the other being the target that runs your bare metal programs.
Lastly and perhaps the best solution IMO, is the FT4232H or FT2232H mini module from FTDI. It gives you UART and JTAG for under $30. See the armjtag directory README for more and you will want some female/female wire from sparkfun or adafruit or elsewhere https://www.sparkfun.com/products/8430 (I use these F/F wires for most projects, buy/bought the 100 pack)
---- connecting to the uart pins ----
On the raspberry pi, the connector with two rows of a bunch of pins is P1. Starting at that corner of the board, the outside corner pin is pin 2. From pin 2 heading toward the yellow rca connector the pins are 2, 4, 6, 8, 10. Pin 6 connect to gnd on the usb to serial board pin 8 is TX out of the raspi connect that to RX on the usb to serial board. Pin 10 is RX into the raspi, connect that to TX on the usb to serial board. Careful not to have metal items on the usb to serial board touch metal items on the raspberry pi (other than the three connections described). On your host computer you will want to use some sort of dumb terminal program, minicom, putty, etc. Set the serial port (usb to serial board) to 115200 baud, 8 data bits, no parity, 1 stop bit. NO flow control. With minicom to get no flow control you normally have to save the config, exit minicom, then restart minicom and load the config in order for flow control changes to take effect. Once you have a saved config you dont have to mess with it any more.
2 outer corner 4 6 ground 8 TX out 10 RX in
ground is not necessarily needed if both the raspberry pi and the usb to serial board are powered by the same computer (I recommend you do that) as they will ideally have the same ground.
Read more about the bootloaders in their local README files. Likewise if you interested in jtag see the armjatag README file. Other than chewing up a few more GPIO pins, and another thing you have to buy, the jtag solution is the most powerful and useful. My typical setup is the armjtag binary as kernel.img, a usb to jtag board like the amontec jtag-tiny and a usb to serial using minicom.
Update, amontec is history their website is gone. But you can get j-link (or clone) boxes from asia on ebay for around $11, so far I have tried them with ARM JTAG and ARM SWD. Very pleased so far.
If you can solder, the A+, B+, Zero and Pi 2 all have a pair of holes sometimes with the text RUN next to them. I use buttons like this https://www.sparkfun.com/products/97 with two of the legs broken off then the others twisted and adjusted to fit in the holes and soldered down.
As far as these samples go I recommend starting with blinker01 then follow the discovery of the chip into uart01, etc.
The bssdata and baremetal directories attempt to explain a little bit about taking control of the gnu toolchain to build bare metal programs like these examples. As with any bare metal programmer I have my ways of doing things and these two directories hopefully will show you some basics, get you thinking about how these tools really work, take the fear away from using them, as well as some comments on why I take the approach I take (not worrying about .bss nor .data). Since the raspberry pi is from our perspective RAM based (the GPU loads our whole binary into memory), we dont have to deal with the things we would deal with on a FLASH/PROM + RAM system. This RAM only approach makes life a lot easier, but leaves out some important bare metal experiences that you will have to find elsewhere.
Top Related Projects
Learning operating system development using Linux kernel and Raspberry Pi
Bare metal Raspberry Pi 3 tutorials
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot