Andy666
    • 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
    # 嵌入式 linux <!-- linux的重點不是安裝,而是troubleshooting --> 安裝套件 ```bash= sudo apt update sudo apt upgrade -y sudo apt install \ make \ bision flex \ ncurses-dev \ libssl-dev # sudo apt-get install python-dev # for python2.x installs # sudo apt-get install python3-dev # for python3.x installs ``` ## 編譯環境建置 ### ARM toolchain 在普通電腦中裝的編譯器,可以編譯出在自身平台執行的程式,而安裝 ARM toolchain 可以在自身電腦編譯出在arm linux平台中執行的程式 也就是所謂的交叉編譯器(Cross compiler) * armel - 沒有 FPU 的 ARM CPU * armhf - 有 FPU,可以硬體執行浮點運算的 ARM CPU * arm64 - 64 位元的 ARM CPU ```bash= #安裝 ARM tool chain sudo apt-get install # armhf toolchain crossbuild-essential-armhf gcc-arm-linux-gnueabihf cpp-arm-linux-gnueabihf g++-arm-linux-gnueabihf # armel toolchain crossbuild-essential-armel gcc-arm-linux-gnueabi cpp-arm-linux-gnueabi g++-arm-linux-gnueabi # arm64 toolchain ( arm64 aarch64 混用 ) crossbuild-essential-arm64 gcc-aarch64-linux-gnu cpp-aarch64-linux-gnu g++-aarch64-linux-gnu ``` ### 安裝 虛擬機器/硬體模擬器 新增 armel 架構使x86架構可以運行arm執行檔( Debain ) 方便驗證程式是否正確 ```bash= dpkg --print-foreign-architectures dpkg --add-architecture armel sudo apt-get update sudo apt-get install libc6:armel sudo apt-get install binfmt-support qemu qemu-user-static qemu-system-arm ``` ## Bootloader Bootloader有很多種,現今(2021)幾乎90%以上都是使用u-boot作為bootloader。 ### u-boot ( DAS u-boot ) #### 下載 u-boot 下載與解壓縮 u-boot ```bash # http://www.denx.de/wiki/U-Boot/SourceCode # https://ftp.denx.de/pub/u-boot/u-boot-2021.04.tar.bz2 wget ftp://ftp.denx.de/pub/u-boot/u-boot-lates tar xfva u-boot-lates.tar.bz2 -C bootloader ``` * rc1,rc1 -> 為推出之後選版本1,2,可以選用非rc的正式版使用 * 選用的版本可以依照硬體平台去選新舊 #### 設定 u-boot config 編譯前需要先針對硬體設定 config,但是如果有前人已經做完該板的config,可以直接使用 已有的 config 可以在資料夾 u-boot-<version>/configs/ 裡面查詢 ARCH = arm or arm64 ```bash= make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- "Your_board"_defconfig ``` 如果不幸的沒有人設定過該板,則需要自行設定 ```bash= make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig ``` #### 修改u-boot開機指令 修改u-boot開機addr ( 0x60000000 ) ```bash= nano bootloader/u-boot-<VERSION>/include/configs/vexpress_common.h ``` 有些板子可以透過修改uEnv.txt來加載開機指令,但是不是所有板子都支援,可以去搜尋u-boot是否有支援 #### 編譯 u-boot 使用8個cpu核心編譯 u-boot ```bash= make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j8 ``` 最後拿到 u-boot、u-boot.bin 就可以用來開機了~ 將 u-boot、u-boot.bin 複製進去sd卡的第一分區 ## Kernel ### 選擇 linux kernel 在嵌入式系統中的核心由自己挑選,可以從linux官方取得 https://www.kernel.org/ ```bash= # 下載 linux kernel wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-<version>.tar.xz # 解壓縮到 kernel 資料夾 tar xfva linux-<version>.tar.xz -C kernel ``` * mainline : 開發中 * stable : 穩定 * longterm : 長期維護 核心中的版本命名為: a.b.c ex: 4.14.241 a.b (4.14) -> 版本號 c (241) -> 修正次數 a 的不同在於新增支援的硬體 核心越後面越肥大,因此不是選擇越新越好,修正次數越多,也可以代表bug相對越少。 ### 編譯 linux kernel 編譯 linux kernel 需要先設定 config,但是如果有前人已經做完該板的config,可以直接用 如要查詢本機編譯的config,可以在資料夾 /boot 中查詢 ```bash= # 複製本機 config 到編譯環境 cp /boot/config-5.4.0-1041-raspi .config ``` 手動執行核心編譯參數設定: ARCH = arm or arm64 ```bash= make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig ``` ``` 請執行底下修改,否則之後無法開機: General setup ---> [*] open by fhandle syscalls System Type ---> [ ] Enable the L2x0 outer cache controller -*- Enable the block layer ---> [*] Support for large (2TB+) block devices and files 接著離開核心編譯環境,並儲存參數設定。 ``` --- 編譯核心 ( armhf ) LOADADDR=0x60000000 請指定為"當下開發版"的開機位置 -j 8 指定使用8個cpu核心去編譯 可以在前方加入 time 指令,取得編譯持間 ```bash= make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- uImage LOADADDR=0x60000000 -j 8 # or make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- uImage LOADADDR=0x60000000 -j 8 # make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- dtbs modules -j 8 ``` 編譯核心模組 ```bash= # Compile Kernel Modules time make -s ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules -j $CPU_CORE # Install Kernel Modules time make -s ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules_install INSTALL_MOD_PATH=../modules/ -j $CPU_CORE ``` 最後可以在 linux-<version>/arch/arm/boot 看到 uImage 的檔案 即是我們的Linux kernel image 再將 uImage 複製進去sd卡的第一分區 如果在 make 時發生 no rule 問題,可以透過以下指令查看內容是否有變化: ```bash= make help ``` ## rootfs 系統只有核心沒有檔案系統(根目錄)無法執行,因此要建立檔案系統給核心用。 ### Debian 安裝 debootstrap debootstrap為我們用來建立檔案系統的工具 ```bash= apt-get install debootstrap ``` #### 建立檔案系統( rootfs ) ```bash= debootstrap \ --arch=armhf \ # 指定cpu架構 --keyring=/usr/share/keyrings/debian-archive-keyring.gpg \ # 數位簽章位置 --verbose \ # 看到log --foreign \ # 非 386 amd64 架構 buster \ # 指定buster版本 ( Debian ) /mnt/sdcard2 \ # 安裝檔案系統的目錄( sd卡第二分區 ) # file:///home/ftp/debian # 抓取package的來源,可指定速度快的鏡像站 ``` #### 進行檔案系統的設定與安裝 ```bash= # 切換至安裝的檔案系統 chroot /mnt/sdcard2 /bin/bash # 安裝檔案系統 /debootstrap/debootstrap --second-stage # 編輯 host name cd /etc nano hostname # "YOUR_HOSTNAME" # 建立 root 密碼為 linux echo root:linux | chpasswd # 建立使用者 以及設定密碼 useradd "YOUR_NAME" echo "YOUR_NAME":"YOUR_PASSWORD" | chpasswd ``` 也可以將rootfs建立在本機,然後再將目錄rsync進去SD卡 ```bash= rsync -av rootfs/ /mnt/sdcard2 --delete ``` #### 修改rootfs (瘦身) ```bash= sudo apt-get clean # 危險 慎用!!! sudo rm -f /var/lib/apt/lists/* # apt install 的資料 sudo rm -rf /usr/share/doc/* # 說明文件 sudo rm -rf /usr/share/local/* # 語系文件 刪掉後只剩英文 sudo rm -rf /usr/share/man/* # 線上說明文件 sudo rm -rf /usr/share/zoneinfo/* # 時區文件 ``` --- ### Busybox http://fichugh.blogspot.com/2017/04/step-by-step.html --- ### Buildroot 從官往下載壓縮包:https://buildroot.org/download.html ```bash= wget https://buildroot.org/downloads/buildroot-2021.05.tar.gz # 解壓縮 tar xvf buildroot-2021.05.tar.gz cd buildroot-2021.05.tar.gz/ make menuconfig ``` 配置文件 ```bash= Target options ---> Target Architecture Variant (arm926t) ---> // arm926ejs架构 [ ] Enable VFP extension support // Nano 没有 VFP单元,勾选会导致某些应用无法运行 Target ABI (EABI) ---> Floating point strategy (Soft float) ---> // 软浮点 System configuration ---> (Lichee Pi) System hostname // hostname (licheepi) Root password // 默认账户为root 密码为licheepi [*] remount root filesystem read-write during boot // 启动时重新挂在文件系统使其可读写 ``` 編譯buildroot ```bash= # buildroot第一次編譯需要下載套件包會比較久 (buildroot不支援多線程編譯) make ``` 編譯完成的 image 文件會在 buildroot-2021.05/output/images/rootfs.tar 將 image 文件解壓到SD卡根目錄分區即可 檢查 /etc/inittab 有沒有啟動串口 ```bash= ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100 # GENERIC_SERIAL // 串口登录使能 ``` 如果掛載權限有問題 ```bash= cd rootfs/bin ll # 如果看到busybox權限有s : -rwsr-xr-x busybox chmod a-s busybox ``` http://fichugh.blogspot.com/2016/02/buildroot-study.html https://www.cnblogs.com/arnoldlu/p/9553995.html ## Device Tree (待續) [Device Tree中的.dts和dtsi文件介绍](https://blog.csdn.net/gxlzyt123456/article/details/55209829) [Day 8:Device Tree (Part 1)](https://ithelp.ithome.com.tw/articles/10242811) ## Final 此時在 SD 卡中有兩個分區: 1. uImage、boot.bin、boot : 作為嵌入式Linux的開機與系統磁區 2. rootfs : 作業系統使用的檔案系統 此時的SD卡就能讓我們的硬體開機與開發嵌入式系統的應用了。 ## Ref: https://www.youtube.com/playlist?list=PLdrYbn8q6soONlL-aEc9b8bKeAtgK0VmB http://pominglee.blogspot.com/p/linux.html http://pominglee.blogspot.com/p/linux_9.html https://www.itread01.com/content/1550311562.html https://blog.csdn.net/u011847345/article/details/110727472 https://harmonyos.51cto.com/posts/2907 https://www.gushiciku.cn/pl/gNHI/zh-tw https://forums.xilinx.com/t5/Embedded-Linux/Building-and-deploying-u-boot-on-the-ZCU102/td-p/802328 [Device Tree中的.dts和dtsi文件介绍](https://blog.csdn.net/gxlzyt123456/article/details/55209829) [Day 8:Device Tree (Part 1)](https://ithelp.ithome.com.tw/articles/10242811) [Device Tree 簡介](https://silverwind1982.pixnet.net/blog/post/360681821-device-tree-%E7%B0%A1%E4%BB%8B) https://bootlin.com/pub/conferences/2013/elce/petazzoni-device-tree-dummies/petazzoni-device-tree-dummies.pdf [LINUX系统移植(史上最全最细,强烈推荐)](https://blog.csdn.net/iteye_17686/article/details/82270803?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.searchformbaiduhighlight&spm=1001.2101.3001.4242) ## Question: 1. 即便是不同廠商出的cpu,只要是ARM架構,都能用相同架構的cross compiler編譯? :::success 能使用相同的cross compiler編譯 ::: 2. 在MCU中,除了ARM內核相同還有其他的peripheral需要撰寫config。在ARM cortex-A中要使用peripheral所撰寫的device tree source(dts file),例如CPU本身提供的SPI,是否要依照不同的CPU做處理? :::success 是,要依照不同CPU處理driver。 :::

    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