# Mini_OS on Raspberry Pi contributed by <`paul5566`> <`johnlin`> github Reference from: [tina0405/raspberry-pi3-mini-os ](https://github.com/tina0405/raspberry-pi3-mini-os) ###### tags: `rp3` `device driver` ## Enviroment Set Up ### 硬體選擇: [Raspberry Pi 3 model B](https://www.raspberrypi.com.tw/tag/bcm2837/) | item |specfication| | :--------: | :--------: | | SoC | Broadcom BCM2837 | | CPU | 1.2 GHz 64-bit quad-core ARM Cortex-A53 | | GPU | Dual Core VideoCore IV® Multimedia Co-Processor; Open GL ES 2.0; hardware-accelerated OpenVG; 1080p60 H.264 high-profie decode | |記憶體| 1GB LPDDR2(和 GPU 共享)| |視訊輸出| Composite RCA; HDMI| |音訊輸出| 3.5 mm jack; HDMI(1.3 & 1.4)| |儲存| microSD| |USB| USB 2.0 x 4| |Ethernet| 10/100 RJ45| |Wireless| 802.11n| |Bluetooth| Bluetooth 4.1; Bluetooth Low Energy(BLE)| |GPIO| 40-pin 2.54 mm (100 mil)| |expansion header| 2×20 strip| * 參考 [Ahmed El-Arabawy 的 Embedded Systems: Lecture 7 (page 46-47)](https://www.slideshare.net/a_elarabawy/cu-cmp445-lec07lab01preparingthepi-48396907) ![](https://i.imgur.com/zo803Hq.png) ### Raspberry Pi Boot Flow * 首先當我們將板子 power on 後, ARM 的處理器目前是 off 的狀態,而 SDRAM 是 disable 的狀態, 而是利用 GPU 來進行 start booting * GPU 是去 ROM 裡執行`第 1 階段 bootloader` * 接著 GPU 又會去 SD 卡中讀 FAT32 第 1 分區的 bootcode.bin * bootcode.bin 是`第 2 階段 bootloader` 他做兩件事 1. enable SDRAM 2. 載入且執行 start.elf * `第 3 階段 bootloader` 是 start.elf, 讀 kernel image(kernel.img), configuration 檔 (config.txt), 和 kernel command line 參數 (cmdline.txt), 把這些都載入記憶體然後再喚醒 ARM 核心 ##### [安裝 raspbian 步驟](https://www.raspberrypi.org/documentation/installation/installing-images/linux.md?fbclid=IwAR2bu_t8vauCXo1JtHy441EK14rgn6sYiVrnRL-_TvdSR02mlUt3ph8cCn4) * [下載官方網址](http://downloads.raspberrypi.org/raspbian/images/raspbian-2018-11-15/) * 我們下載的是 Raspbian Stretch Lite (Minimal image based on Debian Stretch) * Version: November 2018 * Release date: 2018-11-13 * Kernel version: 4.14 * 將 img 檔燒入 Raspberry Pi * 載完後先進行解壓縮,會得到`img` 檔 ~~~ unzip 2018-11-13-raspbian-stretch-lite.zip ~~~ * 可以用gprted 先清空檔案 * 由上面 boot 的資料,先格式化成 FAT32 ~~~ paul@paul-Precision-5520:sudo umount /dev/mmcblk paul@paul-Precision-5520:sudo mkdosfs -F 32 -v /dev/sdf1 ~~~ ~~~ mkfs.fat 3.0.28 (2015-05-16) /dev/sdf1 has 64 heads and 32 sectors per track, hidden sectors 0x2000; logical sector size is 512, using 0xf8 media descriptor, with 89854 sectors; drive number 0x80; filesystem has 2 32-bit FATs and 1 sector per cluster. FAT size is 691 sectors, and provides 88440 clusters. There are 32 reserved sectors. Volume ID is e6c75d1f, no volume label. ~~~ ![](https://i.imgur.com/zsrMuTn.png) * 先查看 SD 卡掛在 dev 下的名字 * `df` 顯示可使用之檔案儲存空間及檔案數目 * 發現插上 SD card 後,多了一個 `/dev/mmcblk` ~~~ tina@tina-X550VB:~/Hw$ df -h Filesystem Size Used Avail Use% Mounted on udev 3.9G 3.9G 0 100% /dev tmpfs 787M 26M 761M 4% /run /dev/sda5 16G 12G 3.5G 77% / tmpfs 3.9G 37M 3.9G 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda6 47G 41G 4.3G 91% /home none 3.9G 2.0M 3.9G 1% /tmp/guest-zvfiym tmpfs 787M 84K 787M 1% /run/user/991 tmpfs 787M 124K 787M 1% /run/user/1000 /dev/sdf1 44M 23M 22M 51% /media/tina/boot ~~~ * 反掛載(Unmount) `/dev/mmcblk`,避免再運行過程中有其他的寫入 ~~~ umount /dev/mmcblk ~~~ #### Review Flashing the firmware * 利用`dd`: 意為 data description, 能夠將輸入寫到標準輸出中 * `if`= input file ;`of`= output file * 所以`of` 應該要填入 SD 卡的位置 of=/dev/sdf ~~~ dd bs=4M if=2018-11-13-raspbian-stretch-lite.img of=/dev/sdx conv=fsync ~~~ ## LAB_0 [Test serial port](https://cdn-learn.adafruit.com/downloads/pdf/adafruits-raspberry-pi-lesson-5-using-a-console-cable.pdf) 在 SD 安裝完後 raspbian 後先別急著把其他除了 bootloader 的檔案刪掉,我們可以先用原本的 kernel 測試 serial port 是否正常能使用。 * (1) Enable the UART by editing the `config.txt` * (2) Connect to the right PIN #### (1) Enable the UART by editing the `config.txt` * 在 boot/[config.txt](https://my.oschina.net/funnky/blog/132885) 文件中打開 `enable_uart=1` (如下) ~~~ # Uncomment this to enable the lirc-rpi module #dtoverlay=lirc-rpi # Additional overlays and parameters are documented /boot/overlays/README # Enable audio (loads snd_bcm2835) dtparam=audio=on enable_uart=1 ~~~ #### (2) Connect to the right PIN ![](https://i.imgur.com/yVgzlqL.png) * 晶片上和板子上的 TX 接 RX, RX 接 TX ![](https://i.imgur.com/KecNtDf.png) * #### USB no need the 5V need GND RX TX * USB's TX to Pi's RX(pin8) * USB's RX to Pi's TX(pin10) ![](https://i.imgur.com/S134h25.png) ![](https://i.imgur.com/slYOToj.png) * 改好後,拿出 SD 卡插到板子上,接上 serial port, hdmi, 開啟電源 * 電腦端下指令和 ttyUSB0 做傳輸,就成功了! `sudo minicom -s`.......login ## LAB1 Hello_World UART #### 1. Prepare your SD card: The card should be formatted exactly in the same way as it is required to install Raspbian. Check HOW TO FORMAT AN SD CARD AS FAT section in the official documentation for more information. * Use an MBR partition table * Format the boot partition as FAT32 #### 2. Copy the following files to the card: * bootcode.bin This is the GPU bootloader, it contains the GPU code to start the GPU and load the GPU firmware. * start.elf This is the GPU firmware. It reads config.txt and enables the GPU to load and run ARM specific user code from kernel8.img #### 4. Copy kernel8.img and config.txt files. #### 5. Connect the USB-to-TTL serial cable. #### 6. Power on your Raspberry Pi. #### 7. Use your terminal emulator to connect to the RPi OS. ![](https://i.imgur.com/n2DWhAE.png) ### Boot Flow with LAB1 Hello_World ### The linker script ### Booting the kernel `Boot.s` 這個file包含了Kernel的start Up Code,可以從下列幾個地方來探討一下 ```clike= #include "mm.h" .section ".text.boot" .globl _start _start: mrs x0, mpidr_el1 and x0, x0,#0xFF // Check processor id cbz x0, master // Hang for all non-primary CPU b proc_hang proc_hang: b proc_hang master: adr x0, bss_begin adr x1, bss_end sub x1, x1, x0 bl memzero mov sp, #LOW_MEMORY bl kernel_main ``` ### Memory Mapping(記憶體映射) 對於 C 語言來說,採用《記憶體輸出入指令》進行輸出入是很方便的做法,因為我們可以利用《指標》直接定位《記憶體位址》,不須透過組合語言就可以進行輸出入。 #### `mm.h` ```clike= #ifndef _MM_H #define _MM_H #define PAGE_SHIFT 12 #define TABLE_SHIFT 9 #define SECTION_SHIFT (PAGE_SHIFT + TABLE_SHIFT) #define PAGE_SIZE (1 << PAGE_SHIFT) #define SECTION_SIZE (1 << SECTION_SHIFT) #define LOW_MEMORY (2 * SECTION_SIZE) #ifndef __ASSEMBLER__ void memzero(unsigned long src, unsigned long n); #endif #endif /*_MM_H */ ``` ### Mini Uart Intializtion ```clike= void uart_init ( void ) { unsigned int selector; selector = get32(GPFSEL1); selector &= ~(7<<12); // clean gpio14 selector |= 2<<12; // set alt5 for gpio14 selector &= ~(7<<15); // clean gpio15 selector |= 2<<15; // set alt5 for gpio 15 put32(GPFSEL1,selector); put32(GPPUD,0); delay(150); put32(GPPUDCLK0,(1<<14)|(1<<15)); delay(150); put32(GPPUDCLK0,0); put32(AUX_ENABLES,1); //Enable mini uart (this also enables access to its registers) put32(AUX_MU_CNTL_REG,0); //Disable auto flow control and disable receiver and transmitter (for now) put32(AUX_MU_IER_REG,0); //Disable receive and transmit interrupts put32(AUX_MU_LCR_REG,3); //Enable 8 bit mode put32(AUX_MU_MCR_REG,0); //Set RTS line to be always high put32(AUX_MU_BAUD_REG,270); //Set baud rate to 115200 put32(AUX_MU_CNTL_REG,3); //Finally, enable transmitter and receiver } ``` ### Hello World Result ## Reference [tina0405-學習實作小型作業系統](https://hackmd.io/@tina0405/S1Ab0A9pX?type=view#) [底層的C語言世界陳鍾誠](https://ccckmit.gitbooks.io/lowlevelc/content/header.html)