Note: The term SD card mentioned here generally covers/means the microSD card.
This is a guide to prepare such card from scratch. If you already have the one that comes along with the board (NOOBS card, is it?), this procedure will override and delete existing content. Backup or forever hold your peace! Well, if you cannot do that and you want to have NOOBS back, head on to this page and download the latest NOOBS image.
Note: Your instructor will show you how to partition/format the SD card.
To exclusively work on bare-metal programming (no OS):
A nice config for multiple kernels - this is what I use to have multiple kernel (my1barepi codes) images on my SD card
# kernel is the alternative kernel filename # - [Pi 1, Pi Zero, and Compute Module] kernel.img # - [Pi 2, Pi 3, and Compute Module 3] kernel7.img # - [Pi4] kernel7l.img. #kernel=kernel_video_temp.img #kernel=kernel_sdcard.img kernel=kernel_pick_one.img
Update20210907 The official OS is now known as Raspberry Pi OS (instead of Raspbian). Check out this official page. I will try to update this site A.S.A.P. - but, do not hold your breath
We will be using Raspbian (the official Linux distribution for Raspberry Pi). This enables us to run web servers and other network-related stuffs.
[201804011654] Note: I just noticed there is now an option to use Windows10 IOT Core (which is prepared by Microsoft as a third party option), but I will not be using that here. Checkout this page for other options
dd if=2018-03-13-raspbian-stretch.img of=/dev/sdb
and wait…To have Raspbian-ready card as well as working on bare-metal programming:
Note: Do the following procedure on your PC.
To use bare-metal code:
To get Raspbian back running:
Pi Zero has a USB On-the-Go (OTG) hub - which, basically means that it can be both host (like USB hub on a PC) AND client (like USB hub on Android or most gadgets these days). So, to setup Pi Zero as a client (this is done on a PC - while preparing the card),
config.txt
(on boot partition) and insert dtoverlay=dwc2
linecmdline.txt
and insert modules-load=dwc2,g_ether
kernel parameterg_ether.host_addr=<mac_addr>
to get a fixed MAC address (easier to manage!)ssh
(same location as config.txt
) - this will enable sshConnect Pi Zero to a PC (allow some time for it to finish booting) and it should appear as ethernet device. To connect to it,
To share the internet with the Pi
$ ifconfig usb0
echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
echo 0 > /proc/sys/net/ipv4/ip_forward iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
eth0
to wlan0
# route add default gw <ip-add-of-usb0>
Have fun!
Note:I have tested this using Raspbian Lite on Pi Zero and it worked! (Obviously)
Using the above setup should get simple projects going without any problems. However, some things on the BCM2835 require special configurations on the GPU side. This can be changed by having a config.txt
file in the same path as the files above.
Example of the configuration file:
# -------------- # MEMORY OPTIONS # -------------- # specify gpu memory allocation # - min 16, max 192 (256), 448(512), 944 (1024) # - default 64 #gpu_mem=64 # disables CPU access to GPU L2 cache # - default 0 (enabled) #disable_l2cache=1 # -------------- # KERNEL OPTIONS # -------------- # specify kernel name # - kernel7.img default for pi2/pi3 # - kernel8.img preferred on pi3 (for 64b mode) # - common default is kernel.img? #kernel=kernel.img # specify startup address for ARM kernel # - default 32b: 0x8000 # - default 64b: 0x80000 # - kernel_old=1 option overrides to 0? #kernel_address=0x8000 #kernel_old=1 # ---------------- # ADVANCED OPTIONS # ---------------- # camera needs start_x.elf firmware # - or, start_file=start_x.elf, fixup_file=fixup_x.dat #start_x=1 # prevent red camera led to turn on while camera is active # - default 0 (enabled) #disable_camera_led=1
To activate an option, simply remove the '#' character for the beginning of the option line (uncomment). More information on config.txt
can be found here. We can also have conditional filters.
Other custom configuration(s):
# for 5-inch lcd with touchscreen max_usb_current=1 hdmi_group=2 hdmi_mode=87 hdmi_cvt 800 480 60 6 0 0 0 dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900 #display_rotate=0
MicroSD Card Failed Boot
Sometimes, the card simply cannot boot. Use fdisk to check/create 255 heads, 63 sectors and calculate the required cylinders based on
disk_size = cylinders * head * sector * sector_size
Not sure why this happens… maybe BIOS issue when formatting on PC? Or, maybe my students' laptops are infected with virus?
Raspbian Update Error
fdisk /dev/sdb
e2fsck -f /dev/sdb2
resize2fs /dev/sdb2
Dumped…
- write image (bullseye) to sd card, boot - change password $ sudo passwd pi - change hostname $ sudo raspi-config nonint do_hostname <hostname> - change locale $ sudo raspi-config nonint do_change_locale en_US.UTF-8 - enable ssh $ sudo raspi-config nonint do_ssh 0 - change timezone $ sudo timedatectl set-timezone Asia/Kuala_Lumpur - change keyboard map $ sed -i /etc/default/keyboard -e "s/^XKBMODEL.*/XKBMODEL=\"pc105\"/" \ -e "s/^XKBLAYOUT.*/XKBLAYOUT=\"us\"/" \ -e "s/^XKBVARIANT.*/XKBVARIANT=\"\"/" \ -e "s/^XKBOPTIONS.*/XKBOPTIONS=\"\"/" - (optional) if not using wifi (e.g. on pi 1), disable wpa_supplicant $ sudo systemctl disable wpa_supplicant - (optional) change wifi country $ sudo raspi-config nonint do_wifi_country MY - (optional) disable camera $ sudo raspi-config nonint do_camera 1 - (optinal) running lite most probably do not need much graphics! $ sudo raspi-config nonint get_config_var gpu_mem /boot/config.txt $ sudo raspi-config nonint do_memory_split 32 - update pkg repo $ sudo apt update - install dev tools $ sudo apt install git raspberrypi-kernel-headers screen vim - work stuff $ mkdir $HOME/work $ cd $HOME/work $ git clone https://codeberg.org/azman/my1shell $ git clone https://codeberg.org/azman/my1linuxpi $ git clone https://codeberg.org/azman/my1codelib $ git clone https://codeberg.org/azman/my1apisrvc $ mkdir $HOME/.local $ cd $HOME/.local $ ln -sf $HOME/work/my1shell $ cd my1shell $ ./bash-setup --profile --write - install minimal gui $ sudo apt install --no-install-recommends xserver-xorg xinit - install pi desktop $ sudo apt install raspberrypi-ui-mods - customize ''/etc/xdg/user-dirs.defaults'' - reboot to desktop
Another try… incomplete
[] - write image (bullseye) to sd card, boot = auto-resize, generate ssh, reboot = change to english (us) keyboard = setup username/password - change hostname $ sudo raspi-config nonint do_hostname <hostname> = reboot, just in case - change locale $ sudo raspi-config nonint do_change_locale en_US.UTF-8 - change timezone $ sudo timedatectl set-timezone Asia/Kuala_Lumpur - enable ssh $ sudo raspi-config nonint do_ssh 0 > perl: warning: Setting locale failed.
Note: This assumes the previous how-to on preparing the SD card has been covered and Raspbian is already 'installed' on the SD card.
sudo apt update
and upgrade sudo apt upgrade
if necessarysudo apt install raspberrypi-kernel-headers
sudo apt install sqlite3 libsqlite3-dev
sudo apt install apache2 php php-cgi libapache2-mod-php php-sqlite3
php.ini
to enable pdo supportrewrite
/var/www/html
screen
and ntfs-3g
(ntfs with r/w)sudo apt install screen ntfs-3g
This is about doing GUI development while running Raspbian on Pi itself.
sudo apt install glade
sudo modprobe bcm2835-v4l2
sudo apt install libavcodec-dev libavdevice-dev
sudo apt install libgtk2.0-dev
sudo apt install libsdl1.2-dev
mkdir -pv /home/pi/Work && cd /home/pi/Work git clone git://github.com/azman/my1codelib.git git clone git://github.com/azman/my1webapp.git git clone git://github.com/azman/my1apisrv.git git clone git://github.com/azman/my1linuxpi.git git clone git://github.com/azman/my1termu.git git clone git://github.com/my1matrix/my1goose.git
git
for thissudo apt install git
To upgrade locally installed sofware:
$ sudo apt update
$ sudo apt upgrade
If packages got held/kept back, simply reinstall those packages:
$ sudo apt install --reinstall
To do an upgrade (e.g. I did this to upgrade stretch to buster):
/etc/apt/sources.list
and replace the release codenames (e.g. change stretch to buster)/etc/apt/sources.list.d/*.list
$ sudo apt update
$ sudo apt dist-upgrade
$ sudo apt autoremove $ sudo apt clean
That should do it!
Note: This is already in the default install.
To make Pi hostname visible on local network, get avahi-daemon (default install)
sudo apt install avahi-daemon
Note: This is an alternative to using Raspbian. Although not necessary, reading how-to on preparing the SD card is recommended.
work in progress…
- download image from https://distro.ibiblio.org/tinycorelinux/ports.html - select latest stable image (in zip format) - unzip and dd to sdcard - use fdisk to resize second partition (linux) = e.g. fdisk /dev/sde = watch the start block! - use resize2fs to resize to full capacity = e.g. resize2fs /dev/sde2 - reboot and do stuffs = [INIT] filetool.sh -b = [BASE] tce-load -wi TC = [MINE] tce-load -wi compiletc git geany fdisk -lu /path/disk.img -> in sector counts (usually 512-bytes sectors) mount -o loop,offset=xxxx /path/disk.img /mnt/disk.img.partition -> offset in bytes! picore requires: > tce-load -wi i2c-tools > sudo modprobe i2c-dev i2cdetect -y 1
Some notes on build/compiler stuffs.
The gcc compiler flags which will produce the most optimal code for the BCM2835 are:
-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s
Note that -Ofast
may produce compile errors. In this case, -O3
or -O2
should be used instead.
Note that -mcpu=arm1176jzf-s
can be used in place of -march=armv6zk -mtune=arm1176jzf-s
.
Some stuffs…
To check if we are on lite version, (well one way to do it…) is to check if raspberrypi-ui-mods package is installed. The following command will return nothing if it is not installed - so, we are probably on lite then.
$ sudo apt list raspberrypi-ui-mods --installed
I run desktop-less Raspbian (Buster) and somehow the timezone got reset. To set that,
# sudo timedatectl set-timezone Asia/Kuala_Lumpur
Edit /etc/hostname
and /etc/hosts
- modify accordingly, and reboot immediately.
$ name=my1pi $ prev=$(cat /etc/hostname) $ sudo sed -i "s/$prev/$name/" /etc/hostname $ sudo sed -i "s/$prev/$name/" /etc/hosts $ sudo reboot
The simplest way is… install xscreensaver and disable it!
# sudo apt install xscreensaver
Script to make Pi an e-Photo Frame.
#!/bin/bash SHOW_PATH="/home/pi/Pictures" [ -d "$1" ] && SHOW_PATH=`cd $1;pwd` SHOW_EXEC="feh" SHOW_FULL=`which $SHOW_EXEC 2>/dev/null` # args: delay 5s, fullscreen, recursive, randomize SHOW_ARGS="-D5 -F -r -z" echo "Accessing pictures in $SHOW_PATH..." # press esc to quit? ${SHOW_FULL} ${SHOW_ARGS} ${SHOW_PATH}
$ ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.170.7.195 netmask 255.255.255.0 broadcast 10.170.7.255
$ netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 10.170.7.1 0.0.0.0 UG 0 0 0 eth0 10.170.7.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
/etc/network/interfaces
and change iface eth0 inet dhcp
to
iface eth0 inet static address xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx network xxx.xxx.xxx.xxx broadcast xxx.xxx.xxx.xxx gateway xxx.xxx.xxx.xxx
note: fill in the info accordingly
Note Got these from R-Pi site.
The HDMI monitor (or HDMI adapter) may only support a limited set of formats for CEA (TV) or DMT (Monitor). To find out which formats are supported, use the following method:
Then edit config.txt to select:
Note: This assumes a Raspbian installation with desktop environment, and username is pi.
/home/pi/.config/autostart
[Desktop Entry] Type=Application Name=Monitor Exec=/path/to/exec
#!/bin/bash chromium-browser http://server.local:1337 --start-fullscreen
Update in progress…
More info here.
This is the code i use… I may put a more elaborate one some time in the future.
WARNING: Do NOT simply copy and run this…. make sure you understand what it does.
#!/bin/bash #sudo apt install libqmi-utils udhcpc #sudo qmicli -d /dev/cdc-wdm0 --dms-set-operating-mode='online' #sudo qmicli -d /dev/cdc-wdm0 --dms-get-operating-mode #sudo qmicli -d /dev/cdc-wdm0 --nas-get-signal-strength #sudo qmicli -d /dev/cdc-wdm0 --nas-get-home-network #sudo qmicli -d /dev/cdc-wdm0 -w sudo ip link set wwan0 down echo 'Y' | sudo tee /sys/class/net/wwan0/qmi/raw_ip sudo ip link set wwan0 up sudo qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network="apn=celcom3g,ip-type=4" --client-no-release-cid sudo qmi-network /dev/cdc-wdm0 start sudo udhcpc -i wwan0 #ip a s wwan0 #ip r s