Try   HackMD

Embedded (Linux) System Design

tags: LINUX KERNEL rootfs busybox embedded Linux

Authors: WhoAmI, CrazyMonkey
email: kccddb@gmail.com
Date: 2023-10-11
Copyright: CC BY-NC-SA

千里之行始於足下 ~老子

故不積跬步,無以至千里;不積小流,無以成江海~荀子
學而不思則罔 思而不學則殆~論語

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

偉大的 GNU GPL 前輩 Richard Matthew Stallman (rms)


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

**BUG 除不盡 春風吹又生**

The course covers the basic and advanced issues of Embedded Linux System such as boot loader, toolchain, linux kernel source code, root file system, kernel module and device driver.

Stability is an important issue.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

站在巨人的肩膀上

先修科目 (Prerequisites) : C (or C++) Language, Linux, Network programming and OS

教室 資電 515 星期二 13:10-16:00

c Language:
ANSI C for Programmers on UNIX Systems, Tim Love
C 也可以寫的很OOP 簡單範例與簡易Makefile, 務必要懂

在 linux kernel 中的 OOP 設計思維

Win32 c 有 cygwin 可以練習 c 語言 (使用 gcc)

LINUX:
Rute User's Tutorial and Exposition
(Version 1.0.0), by Paul Sheer

Object-oriented design patterns in the kernel, part 1

Object-oriented design patterns in the kernel, part 2

shell (bash): OnlineGDB 可以練習, 還是用自己的 Linux 方便

Linux network commands: ip addr xxxxx, ip route xxxxx, ifconfig xxxxx,

embedded system 軟體設計中 有遞迴(Recursive) 設計時 要特別注意 stack overflow

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

善用 ramdisk, tmpfs 降低對 flash 的讀寫次數: How to Create a Ramdisk in Linux

Linux Tmpfs

如果記憶體 夠大~用 Ramdisk 放網頁等也很適合

VirtualBox: Create Ubuntu 16.04.7 (最好有40G, two NICs (NAT Mode and Bridged Mode))

MobaXterm or putty

Text editor: vim, nano, gedit(need X-window),

Makefile範例教學

練習 看 8051 datasheet,[ 8390D datasheet]
(https://pdf1.alldatasheet.com/datasheet-pdf/view/8365/NSC/DP8390D.html)

Linux Command Cheat Sheet

Futher Reading (宣老師 提供):
Linux Kernel Teaching

Linux Network Programming, by WhoAmI

e.g., BeagleBone Black

Linux 核心模組運作原理
這課程大都是 原理的理解與實作~相信是很花時間的課程

原理老師們會教也給實例, 但要學生自己練習做過, 就像開車也要自己練習一樣, 只是會背名詞考試沒有大用, 基礎要好~計概(含 c, 資料結構) , 微處理機, OS, 要紮實, 基礎要學好

早期 吳大猷院長 也說 "基礎" 很重要, 因為是日後自我學習的基礎

我看過一本書~有讀者問 blog 作者一個問題, 結果 作者 回的是 請 複習 成語辭典 "囫圇吞棗"

我看了都笑出來~

PC817 Photocoupler 可隔離電源
A3144 LINEAR HALL-EFFECT SENSORS, 可用磁鐵當開關
2SD313 NPN (3A) 可換電壓
請看 datasheet 複習 電子學 電路學


Container (abstract data type)

e.g., The

<div> HTML element is the generic container for flow content.

Ref. w3schools


另外 還有 Boot Loader 部分 例如 [U-Boot], 因需硬體設備例如 樹莓派(英語:Raspberry Pi)或 Banana Pi ,本課程不含 這部分

What is a bootloader and how does it work?


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

stackoverflow 可以查到 很多 好心軟體系統等專家幫你解答疑惑, 是您進步的明燈

Linux Command Cheat Sheet

fcu embedded system (大三)
Linux 的 ls 指令教學與常用範例整理


Course Outlines


基本觀念

1. Embedded Linux and how the system works:
Boot Loader, Linux OS, root file system (rootfs) and init

2. Linux System Calls, VFS, and How it works

四大步驟 運用QEMU (Quick EMUlator) 完成 Linux ARM-xxx 成功開機的方法實例 (這裡皆用 source code 重新編譯而成)

以下就是 實作的基礎 請讀者 務必實作一遍, 用 QEMU 代替 實際的 embedded system, 可以方便學習與測試

本課程 與 kernel 相關 都由 source code 做起

第一步驟 能夠用 toolchain 建造 linux kernel image 與 rootfs.

  1. Build QEMU ( https://www.qemu.org/), QEMU 5.0.0 and QEMU 2.x.x

4. Busybox, build x86 kernel (linux-[4.4.50]) and rootfs (busybox 1.22.0)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

PXE Linux

How to enable PXE boot with VirtualBox, by Jack Wallen

Setting up a ‘PXE Network Boot Server’ for Multiple Linux Distribution Installations in RHEL/CentOS 7
Build SYSLINUX 6.03
A bootloader for Linux using the PXE network booting protocol
master boot record (MBR)
UEFI (Unified Extensible Firmware Interface)
UEFI 是什麼以及如何在 Windows 中使用它?【完整教學】

DHCP server and TFTP server

讀者有空時 可用 真正的電腦測試, 這種方式 可以 避免 每個人都要 裝 toolchain等等 方便 版本維護


HW (期中考後一星期完成, 同一組只須交一份紙本, 期末口試):

  1. 運用 QEMU 與 linux kernel source 添加 一個 system call, 例如 sys_hello() ( ARM version, 練習 cross compiler, Makefile)
    Ref. Add a system call (in x86 QEMU Environment)
  2. Tech. Report: Build your linux kernel and rootfs (ARM version, 可以用 QEMU 啟動)

Linux Kernel: 簡介HZ, tick and jiffies

sysfs - a filesystem for exporting kernel objects

proc - process information, system information, and sysctl pseudo-filesystem

enum
{
HI_SOFTIRQ=0,
TIMER_SOFTIRQ,
NET_TX_SOFTIRQ,
NET_RX_SOFTIRQ,
BLOCK_SOFTIRQ,
IRQ_POLL_SOFTIRQ,
TASKLET_SOFTIRQ,
SCHED_SOFTIRQ,
HRTIMER_SOFTIRQ,
RCU_SOFTIRQ, /* Preferable RCU should always be the last softirq */

​NR_SOFTIRQS

};

softirqs : deferred work runs in interrupt context
tasklets : deferred work runs in interrupt context
work queues : deferred work runs in process context

softirqs : Softirqs of the same type can run concurrently on several CPUs.
tasklets : Tasklets of different types can run concurrently on several CPUs, but tasklets of the same type cannot.
work queues : can run simultaneously on different CPU's

softirqs : cannot go to sleep
tasklets : cannot go to sleep
work queues : can go to sleep

softirqs : cannot be preempted/schedule
tasklets : cannot be preempted/schedule
work queues : maybe be preempted/schedule

softirqs : not easy to use
tasklets : easy to use
work queues : easy to use

work queue, kthread和timer对比

Character device drivers

Linux Kernel Teaching

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
register a range of char device numbers

udev - Dynamic device management

busybox: mdev

Parameters

dev_t *dev
output parameter for first assigned number

unsigned baseminor
first of the requested range of minor numbers

unsigned count
the number of minor numbers required

const char *name
the name of the associated device or driver

Description

Allocates a range of char device numbers. The major number will be chosen dynamically, and returned (along with the first minor number) in dev. Returns zero or a negative error code.

How to initialize device's class when register device in linux-6.0
通过 Linux sysfs 玩转硬件
Linux下通过sysfs方式控制GPIO(/sys/class/gpio)

Linux Kernel Module Programming


  1. Linux Device Driver Tutorial Programming – Linux Device Driver Tutorial Part 7
  2. sysfs, procfs (Sysfs in Linux Kernel – Linux Device Driver Tutorial Part 11)
  3. Linux kernel module programming, by Dr. Derek Molloy, interrupt 需要硬體板子, 沒硬體的看 14
  4. Workqueue in Linux Kernel Part 2 – Linux Device Driver Tutorial Part 15
  5. Kernel Thread – Linux Device Driver Tutorial Part 19
  6. Tasklets in Linux kernel | Dynamic Method – Linux Device Driver Tutorial Part 21
  7. Using Kernel Timer In Linux Device Driver – Linux
  8. Poll Linux Example Device Driver – Linux Device Driver Tutorial Part 42
  9. Interrupts Example Program in Linux Kernel – Linux Device Driver Tutorial Part 13, by SLR 作者 巧妙用 x86-> asm("int $0x3B"); 產生 irq 11

The Linux Kernel Module Programming Guide, by Pe Jay Salzman, Michael Burian , Ori Pomerantz, Bob Mottram, Jim Huang

Cultivate teamwork to complete homework in groups of 2-3 people

Final oral test
No interim written test
Class situation


TEXTBOOKS AND ON-LINE EDUCATIONAL MATERIAL

Install:

Ubuntu-16.04.7 server version
install openssh-server
modify sshd_config

putty or MobaXterm

Learn iproute2
ip addr
ip route

Learn commands: (請看 man page)
vim, nano, wget, curl, sudo, cd, ls, cp, cat, more, find, ln, grep, ps, chmod, chown, tar, mkdir, rm, rmdir, df, du, kill, echo, which, man, top, ping, touch, pstree, strace, strip, file, tcpdump, mount, umount, dd, objdump, gzip, mke2fs, sync, cpio, losetup, strip -g (Remove debugging symbols only), rsync, gzip, gunzip, file, chroot, tee, tail, dd, uname, uname -r, mdev (busybox 代替 udev), udevadm - udev management tool, gawk, awk 這比較難且與 Regular Expressions 有關, 您有空與需要再學,

簡明 Linux Shell Script 入門教學

了解 linux 檔案架構與主要用途

Linux Command Cheat Sheet

30+ awk examples for beginners / awk command tutorial in Linux/Unix

ntpd - Network Time Protocol (NTP) daemon

Learn linux pipe

Linux 使用 wget 指令自動下載網頁檔案教學與範例, by G. T. Wang

Shell Scripting for Beginners – How to Write Bash Scripts in Linux

grep: Linux 匹配文字 grep 指令用法教學與範例, by G. T. Wang

Ubuntu-16.04搭建DHCP服務

Linux 2.6.33

Documentation arch block crypto drivers firmware fs include init ipc kernel lib mm net samples scripts security sound tools usr virt .gitignore 843 2010-02-24 18:52:17 .mailmap 4021 2010-02-24 18:52:17 COPYING 18693 2010-02-24 18:52:17 CREDITS 94031 2010-02-24 18:52:17 Kbuild 2440 2010-02-24 18:52:17 MAINTAINERS 164165 2010-02-24 18:52:17 Makefile 53199 2010-02-24 18:52:17 README 17459 2010-02-24 18:52:17 REPORTING-BUGS 3371 2010-02-24 18:52:17 Th
Embedded Linux System, Boot, Linux VFS, System Call, SPI, GPIO, … 課程基礎原理
​​​​                                                      |
C 也可以寫的很OOP 簡單範例與簡易Makefile c language: function pointer, callback
busybox-1.22.0 and linux-4.4.50 QEMU 5.0.0

System calls in the Linux kernel. Part 1.

使用 Sourcery G++ Lite 2011.03-42 編譯 Linux Kernel for QEMU ARM Version 編譯 ARM Busybox 1.22.0 當 Rootfs for QEMU Linux
Syslinux 6.03 Linux simple sysfs, procfs and character device driver (Linux kernel >3.2)

Introduction to deferred interrupts (Softirq, Tasklets and Workqueues)

软中断与软中断的排查


Appendix

練習 看 8051 datasheet 與
DP8390 Datasheet~NIC controller

Sleeping in the Kernel and Frame Reception/Transmission
linux-insides
Linux Network Programming, by WhoAmI

getloadavg - get system load averages

Interface statistics

https://www.gnu.org/software/libc/manual/html_node/Integers.html

山不在高 有仙則名


水不在深 有龍則靈

有內功(基礎理論知識, 演算法, OS, 網路知識, ) 沒 外功 (程式設計, 微處理機, 網路程式設計) 無法打架 例如 覺遠大師

有外功 沒內功 進步有限, 除非 您是 洪七公

如果 經濟上許可 (課程上 不要求有)

樹莓派(英語:Raspberry Pi)

Banana Pi

如果對 單晶片+Wifi 或 物聯網專題 有興趣可以參考 ESP8266 (可以用 Arduino IDE 開發軟體)

有 硬體 可以練習 實作經驗與技巧

千里之行 始於足下