Bolin Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 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)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully