Skip to content

Instantly share code, notes, and snippets.

@Kaapiii
Last active November 10, 2023 00:38
Show Gist options
  • Save Kaapiii/243ece6f64384afdfb01e04b03966f26 to your computer and use it in GitHub Desktop.
Save Kaapiii/243ece6f64384afdfb01e04b03966f26 to your computer and use it in GitHub Desktop.
Example Raspberry Pi - Ubuntu 20.04 LTS - Fixed IP

Installation

Preparation

  • Ubuntu 20.04 LTS image for Rasperry Pi4(64bit/32bit), Pi3(64bit/32bit), Pi2(32bit)
    Recommendation: 32bit image (Why? There are way more resources (toutorials, documentation) on the internet for 32bit issues. )
  • BaleanaEtcher
  • SD-Card less than 32GB (-> FAT)

Flash SD-Card

  • Start Etcher
  • Choose image file
  • Choose SD card
  • Flash SD Card

After the SD-Card flashing, edith the networkconfiguration.

Note: This is only needed, if your router does not support static DHCP leases for specific MAC addresses.

Example: A device with the Mac address of "dc:a6:32:18:xx:xx" will allways be assigned to the IP address of "192.168.1.200"

Setup Tuorial

Tutorial from Ubuntu

Network configuration

  • Edith the network-config file in the system-boot section of the SD-Card.

Start Raspberry Pi

  • Connect the ethernet cable (if configured with ethernet); if WiFi was configured ignore this step
  • Connect the Power
  • Raspberry starts

Connect to rasperry with ssh

Access your Pi with ssh. The commands are the same on Windows 10 and Linux

Default user: ubuntu Default password: ubuntu

ssh ubuntu@<ip-of-raspberry-pi>

Example: ssh ubuntu@192.168.1.200

Install software on the Raspberry Pi

GCC

Install the GNU Compiler Collection && Debugger

sudo apt install gcc gdb


Check if it's installed.

which gcc

If it's installed something like /usr/bin/gcc is shown. \

GNU ARM embedded toolchain

Check if ARM toolchain is installed

arm-none-eabi-gcc

If something like the below message is shown the toolchain is installed:

** arm-none-eabi-gcc: fatal error: no input files compilation terminated. **

Install the ARM embedded toolchain

sudo apt install gcc-arm-none-eabi

Python3

Check if python3 is installed.

which python3

If no output is shown install it.

sudo apt install python3

\

Install Pip3 (package manager for python3)

\

Check if pip3 is installed.

which pip3

If no output is shown install it.

sudo apt install python3-pip

\

VIM (Command line editor)

Check if VIM is installed.

which vim


If no output is shown install it.

sudo apt install vim

\

Create Project

Create projects folder

Show current folder

pwd

Change to the home folder of the ubuntu user and create a projects directory

cd
mkdir projects

Show content of the current folder. Verify that the folder projects was created.

ls -la

Example: drwxrwxr-x 2 ubuntu ubuntu 4096 Aug 28 09:23 projects

Install python dependencies with pip3 for test file (gpiozero)

sudo apt install python3-gpiozero

Check if module was installed corretly

pip3 freeze | grep RPi

Create first python3 test application

# folder /home/ubuntu/projects
vim ledon.py
  • Type: "i" for entering the VIM insert mode
  • Copy/Pastefollowing code
#!/usr/bin/env python3

from gpiozero import LED
from time import sleep

# Number of the GPIO PIN not the physical pin
# GPIOPin 24 -> physical pin 18
# GPIOPin 21 -> physical pin 40
gpioPin = 40
led = LED(gpioPin)
print("Using GIPOPin %d" % gpioPin)
print("-----------------")

while True:

    print("LED On")
    led.on()
    sleep(1)
    print("LED Off")
    led.off()
    sleep(1)
  • hit ESC and type
  • :wq which means write and quit

Make file executable for current user

chmod u+x ledon.py

Execute python code

./ledon.py

Result: An output shold be generated an the LED should turn on and off.

Assembly Raspberry Pi 4+

Create first assembly test application

Create folder

# folder /home/ubuntu/projects/test_asm1
mkdir test_asm1 && cd test_asm1

Create assembly file by pasting the contents of the file asmtest2.s

vim asmtest2.s

# hit i  -> for insert mode
# copy paste content from file
# hit ESC
# hit :wq

Create "makefile" file by pasting the contents of the file makefile

vim makefile

# hit i  -> for insert mode
# copy paste content from file
# hit ESC
# hit :wq

Compile test application on raspberry pi (local tools)

Manually:

as -o asmtest2.o asmtest2.s
ld -o asmtest2 asmtest2.o

Automatically:

make

Run Application

# in the projecet folder /home/ubuntu/projects/test_asm1
./asmest2 ; echo $?

Output should be 65

Complie test application with gcc-arm-none-eabi

Components gcc-arm-none-eabi

The gcc-arm-none-eabi consists of the following min binaries:

  • C Compiler (gcc): arm-none-eabi-ld
  • C++ Compiler (gcc): arm-none-eabi-g++
  • Assembler: arm-none-eabi-as
  • Object copy: arm-none-eabi-objcopy
  • Object dump: arm-none-eabi-objdump

List all "arm-*" binaries.

ls /usr/bin/ | grep arm-none-*

Compilation steps

@Todo needs to be tested first Combile Manually for (cortex-a72 - Raspberry Pi 4)

arm-none-eabi-gcc -c -mcpu=cortex-a72  asmtest2.s -o asmtest2.o

# link the object file using our linking script
$ arm-none-eabi-ld asmtest2.o -o asmtest2 -T asmtest2.ld

# we need pure binary code, not an ELF file, so strip the ELF header
$ arm-none-eabi-objcopy asmtest2 -O binary kernel.img

Combile Manually for (cortex-a53 - Raspberry Pi 3B)

arm-none-eabi-gcc -c -mcpu=cortex-a72  asmtest2.s -o asmtest2.o

# link the object file using our linking script
$ arm-none-eabi-ld asmtest2.o -o asmtest2 -T asmtest2.ld

# we need pure binary code, not an ELF file, so strip the ELF header
$ arm-none-eabi-objcopy asmtest2 -O binary kernel.img

When use which tool of the GCC ARM toolchain

For when to use which tool, consult page 36 of this PDF from the university of Michigan.

Assembly files (.s) => (assembler Tool:*-as ) => Object files (.o) => (linker Tool: *-ld)

Instruction Set for Raspberry Pi 4+ (ARM Cortex-A72)

The ARM-Cortex CPU's can run in two different modes/states.

  • aarch32 mode
  • aarch64 mode

Instruction Sets for:

  • aarch32 mode it's called A32, and for
  • aarch64 mode it's called A64

ARM-V8 Documentation: Chapter C3 - Page 164

Important:

  • In *aarch64 mode the registers are 64bit long. Because of this, the addressing of the registers change from "r1-r12" to "x1-x30"
  • The aarch64 mode has ist own instruction sets.
  • The arrch64 bit mode has to be triggered while "booting" with the following line:
# config.txt

# Set the following for running in 64 bit mode:
arm_control=0x200

# For triggering 32 bit mode remove the line or just write:
arm_control=0x0

Instruction Sets 64bit (Aarch64 State)

Instruction Set Aarch64

Registers ARM Cortex-A72

Registers in 32Bit state (Aarch32 State)

Register names: r0-r12

Doku ARM Aarch32

Registers in 64bit state (Aarch64 State)

Register names: x0-x30

Doku ARM Aarch64

Values

Type Value
Dezimal #28
Hexadezimal 0x1C
Binär, Dual 0b11100
/*
* Assember test for Raspberry Pi4
*/
.global _start
// Some comment
_start:
mov x0,#65 // Save int 65 to register x0
mov x8,#93 // Value #93 is exit
svc #0 // Systemcall
asmtest2: asmtest2.o
ld -o asmtest2 asmtest2.o
asmtest2.o: asmtest2.s
as -o asmtest2.o asmtest2.s
clean:
rm *.o asmtest2
# This file contains a netplan-compatible configuration which cloud-init
# will apply on first-boot. Please refer to the cloud-init documentation and
# the netplan reference for full details:
#
# https://cloudinit.readthedocs.io/
# https://netplan.io/reference
#
# Some additional examples are commented out below
version: 2
ethernets:
eth0:
dhcp4: false
optional: false
address: [192.165.10.40/24]
gateway4: 192.168.10.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
#wifis:
# wlan0:
# dhcp4: true
# optional: true
# access-points:
# myhomewifi:
# password: "S3kr1t"
# myworkwifi:
# password: "correct battery horse staple"
# workssid:
# auth:
# key-management: eap
# method: peap
# identity: "me@example.com"
# password: "passw0rd"
# ca-certificate: /etc/my_ca.pem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment