Raspberry Pi: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
No edit summary
 
(7 intermediate revisions by 5 users not shown)
Line 1:
{{In Progress}}
{{FirstPerson}}
{{You}}
{{Sole Editor}}
This is a tutorial on bare-metal [OS] development on the Raspberry Pi. This tutorial is written specifically for the Raspberry Pi Model B Rev 2 because the author has no other hardware to test on. But so far the models are basically identical for the purpose of this tutorial (Rev 1 has 256MB ram, Model A has no ethernet).
Line 12 ⟶ 13:
You will need a:
* '''Raspberry Pi''', RPi in short.
* [[SD Card]] to boot from.
* A SD Card reader so you can write to the SD Card from your developement system.
* A serial adaptor for the RPi.
Line 19 ⟶ 20:
=== Serial adaptor ===
 
The RPi has 2 serial ports ([[UART|UARTs]]). This tutorial only concerns itself with UART0, called simply UART or serial port. UART1 is ignored from now on. The basic UART onboard uses a 3.3V TTL and is connected to some of the GPIO pins labeled "P1" on the board. x86 PCs and MACs do use 5V TTL so you need some adaptor to convert the TTL. I recommend aA '''USB to TTL Serial Cable - Debug/Console Cable for Raspberry Pi''' with separate connectors per lead, like [http://www.adafruit.com/products/954 commercial RPi serial adaptor], is recommended.. Which is then connected to the RPi [[Media:ARM_RaspberryPi_serial.jpg|like this]]. SlightlyThe slightly cheaper [http://www.ebay.com/itm/USB-To-RS232-TTL-PL2303HX-Auto-Converter-Module-Converter-Adapter-5V-3-3V-Output-/350568364250 PL2303HX adapter] was found usable, but seems to be unreliable if connected to an USB port with an extension cable (a USB 2.0 hub might remedy this).
 
Note: The serial adaptor I use provides both a 0V and 5V lead (black and red) which provide power to the RPi. No extra power supply is needed besides this.
 
Alternatively, you can use the FTDI chip on an Arduino (or clone thereof). Connect RX on the Arduino to RX on the Pi, TX to TX and GND to GND, then connect the Arduino's reset pin to ground to prevent code in flash from interfering. Due to reset being held low, the Arduino itself is entirely bypassed. If you have a clone that supports 3.3V operation (such as a Seeeduino) then you can enable it to be safe, but this approach works fine with 5V.
 
=== Testing your hardware/serial port ===
Line 29 ⟶ 32:
 
If you get 'Permission Denied' '''do NOT become root!''' This is unnecessary. Instead do:
<sourcesyntaxhighlight lang="bash">
sudo adduser <user> dialout
</syntaxhighlight>
</source>
This will let your user use serial ports without needing root.
 
Line 42 ⟶ 45:
Like me you are probably using a x86 PC as main machine and want to edit and compile the source on that and the RPi is an ARM CPU so you absolutely need a cross compiler. But even if you are developing on an ARM system it is still a good idea to build a cross compiler to avoid accidentally mixing stuff from your developement system with your own kernel. Follow the steps from [[GCC Cross-Compiler]] to build your own cross compiler but use:
 
<sourcesyntaxhighlight lang="bash">
export TARGET=arm-none-eabi
</syntaxhighlight>
</source>
 
Now you are ready to start.
 
== Tutorials and examples ==
# [httphttps://www.cl.cam.ac.uk/freshersprojects/raspberrypi/tutorials/os/ Tutorial in assembler (University of Cambridge)]
# [[Raspberry Pi Bare Bones|Tutorial in C]]
# [[Raspberry Pi Bare Bones Rust|Tutorial in Rust]]
# [https://github.com/dwelch67/raspberrypi Collection of examples and bootloader by dwelch67]
# [https://github.com/bztsrc/raspi3-tutorial Tutorials for AArch64 in C by bzt]
Line 62 ⟶ 66:
Now mount the first partition from the SD card and look at it:
 
<sourcesyntaxhighlight lang="text">
bootcode.bin fixup.dat kernel.img start.elf
cmdline.txt fixup_cd.dat kernel_cutdown.img start_cd.elf
config.txt issue.txt kernel_emergency.img
</syntaxhighlight>
</source>
 
When the RPi powers up the ARM CPU is halted and the GPU runs. The GPU loads the bootloader from rom and executes it. That then finds the SD card and loads the bootcode.bin. The bootcode handles the config.txt and cmdline.txt (or does start.elf read that?) and then runs start.elf. start.elf loads the kernel.img at 0x00008000, puts a few opcodes at 0x00000000 and the ATAGS at 0x00000100 and at last the ARM CPU is started. The CPU starts executing at 0x00000000, where it will initialize r0, r1 and r2 and jump to 0x00008000 where the kernel image starts.
Line 122 ⟶ 126:
To be able to use any floating point operations, such as storing or loading floating point numbers, you need to enable the FPU before using it. To do this, you have to enable access to the coprocessor to whoever should be able to use it, and you have to enable the FPU itself.
 
<sourcesyntaxhighlight lang="asm">
# enable FPU in coprocessor enable register - this gives everybody access to both locations of coprocessor.
ldr r0, =(0xF << 20)
mcr p15, 0, r0, c1, c0, 2
</syntaxhighlight>
</source>
 
And then enable the FPU itself:
 
<sourcesyntaxhighlight lang="asm">
# enable FPU in FP exception register
MOV r3, #0x40000000
# VMSR FPEXC, r3 # assembler bug
.long 0xeee83a10
</syntaxhighlight>
</source>
 
The third line is the actual instruction that you'd want to use, but due to a bug in Binutils 2.23 it does not assemble. The line below it is what it should assemble to, and replaces the opcode. After doing these two, it's possible to use the FPU.
Line 154 ⟶ 158:
 
[[Category:ARM]]
[[Category:ARMRaspberry RaspberryPiPi]]