# Linux 核心專題: STM32F4 移植和系統初始化流程
> 執行人: hugo0406
> [專題解說影片](?)
## 任務簡介
理解 Linux 核心在 [STM32F429i-Discovery](https://www.st.com/en/evaluation-tools/32f429idiscovery.html) 系統晶片上運作的方式,探究相關的準備工作,特別是相關系統初始化的流程。
## TODO: STM32F429i-Discovery 硬體簡介
> 注意: 沒有 MMU
[STM32F429i-Discovery](https://www.st.com/en/evaluation-tools/32f429idiscovery.html) 是意法半導體提供的開發板。
主要特點:
- STM32F429 MCU (ARM Cortex-M4 up to 180MHz, 2MB internal flash, 256KB internal RAM)
- 8MB SDRAM
- USB OTG Full-Speed
- 2.4" LCD
- MEMS motion sensor
- STLink/V2 (debugging)
- Extension headers
- No MMU
![image](https://hackmd.io/_uploads/HyjgefKE0.png)
:::danger
閱讀 [No-MMU memory mapping support](https://www.kernel.org/doc/Documentation/nommu-mmap.txt),闡述 Linux 執行於 Arm Cortex-M4 這樣的硬體,會有什麼限制,特別是記憶映射和其管理。
:::
### STM32 Reset
參考 STM32F429 的 [Reference Manual](https://www.st.com/content/ccc/resource/technical/document/reference_manual/3d/6d/5a/66/b4/99/40/d4/DM00031020.pdf/files/DM00031020.pdf/jcr:content/translations/en.DM00031020.pdf)
Boot mode selection pins: BOOT0,BOOT1
CPU reset 後會從這兩根 pin 的狀態來決定哪一塊 memory 當 boot space,STM32F429 選用的 default boot space 是 Main Flash memory
| BOOT1 | BOOT0 | Boot Mode | Aliasing |
|:-----:|:-----:|:----------------- |:-------- |
| X | 0 | Main Flash Memory |Main Flash Memory is selected as the boot space |
| 0 | 1 | System Memory |System Memory is selected as the boot space |
| 1 | 1 | Embedded SRAM |Embedded SRAM is selected as the boot space |
Flash memory 的 start address 是 `0x08000000` 等開機時,會自動將 `0x08000000-0x080FFFFF` 映射到 `0x00000000-0x000FFFFF`,所以將 bin檔燒在 `0x08000000` ,STM32F429 就可以從 `0x00000000` 開始執行
![image](https://hackmd.io/_uploads/B1dnMN3NA.png)
## TODO: 開發工具介紹和測試
> 應提及 GNU Toolchain, OpenOCD, st-link 等工具的使用。
> STM32F42I-DISCO 內建 ST-LINK/V2,已經包含 ST-Link 了,所以 USB-mini 插上去後就可以直接燒錄,不需要額外的燒錄器。參考[嵌入式系統建構:開發運作於 STM32 的韌體程式](https://docs.google.com/document/d/1Ygl6cEGPXUffhTJE0K6B8zEtGmIuIdCjlZBkFlijUaE/edit),確保能成功將檔案燒錄到開發版上
### Verify if udev rules are set correctly
Start by plugging the STLINK device into the usb port, then run `lsusb`.
```
$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 06cb:00cb Synaptics, Inc.
Bus 001 Device 004: ID 05c8:03d3 Cheng Uei Precision Industry Co., Ltd (Foxlink) HP Wide Vision HD Camera
Bus 001 Device 003: ID 0483:3748 STMicroelectronics ST-LINK/V2
Bus 001 Device 006: ID 0bda:b00a Realtek Semiconductor Corp. Realtek Bluetooth 4.2 Adapter
Bus 001 Device 002: ID 2717:5023 Xiaomi Inc. MI Wireless Receiver
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ ls -l /dev/bus/usb/001/003
crw-rw-rw- 1 root root 189, 2 六 3 03:43 /dev/bus/usb/001/003
```
There have several files in `/lib/udev/rules.d` directory. The `49-stlinkv2.rules` file contains the following:
```
# STM32 discovery boards, with onboard st/linkv2
# ie, STM32L, STM32F4.
# STM32VL has st/linkv1, which is quite different
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", \
MODE:="0666", \
SYMLINK+="stlinkv2_%n"
# If you share your linux system with other users, or just don't like the
# idea of write permission for everybody, you can replace MODE:="0666" with
# OWNER:="yourusername" to create the device owned by you, or with
# GROUP:="somegroupname" and mange access using standard unix groups.
```
and the `idVendor` of `0483` and `idProduct` of `3748` matches the vendor id from the lsusb output.
### 開發工具介紹
- OpenOCD
```bash
git clone git://git.code.sf.net/p/openocd/code openocd
cd openocd
./bootstrap
./configure --prefix=/usr/local --enable-stlink
echo -e "all:\ninstall:" > doc/Makefile
make
sudo make install
```
- ARM Cross Compile Toolchain:
```bash
sudo apt-get install arm-linux-gnueabi-gcc
which arm-linux-gnueabi-gcc
export PATH=/usr/bin:$PATH
source ~/.bashrc
```
Verify it by checking the installed version :
```
$ arm-linux-gnueabi-gcc --version
arm-linux-gnueabi-gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```
- STLINK Tools: `stlink` is an open source toolset to program and debug STM32 devices and boards manufactured by STMicroelectronics.
```bash
cd ~
git clone https://github.com/texane/stlink.git
cd ./stlink
sudo apt-get install libusb-1.0-0-dev
sudo apt-get -y install cmake
sudo apt-get install libstlink1
make
cd build/Release
sudo make install
```
To verify the successful installation
```bash
$ st-flash
invalid command line
usage: st-flash [options] read [file] [addr] [size]
st-flash [options] write <file> [addr] [size]
st-flash [options] write <value>
st-flash [options] erase <addr> <size>
st-flash [options] reset
options:
--freq <kHz> Frequency of JTAG/SWD, default 1800kHz.
--serial <serial> STLink device to use.
--connect-under-reset Pull reset low while connecting.
--hot-plug Connect without reset.
--reset Reset after writing.
--format {binary|ihex} Format of file to read or write. When writing
with ihex specifying addr is not needed.
--flash <size> Specify size of flash, e.g. 128k, 1M.
--area <area> Area to access, one of: main(default), system,
otp, option, option_boot_add, optcr, optcr1.
--opt Skip writing empty bytes at the tail end.
--debug Output extra debug information.
--version Print version information.
--help Show this help.
examples:
st-flash --area=option read [file] [size]
st-flash --area=option write 0xXXXXXXXX
st-flash --area=option_boot_add read
st-flash --area=option_boot_add write 0xXXXXXXXX
st-flash --area=optcr read
st-flash --area=optcr write 0xXXXXXXXX
st-flash --area=optcr1 read
st-flash --area=optcr1 write 0xXXXXXXXX
st-flash --area=otp read <file>
st-flash --area=otp write <file> 0xXXXXXXXX
```
`st-info`: a programmer and chip information tool
```
$ st-info --probe
Found 1 stlink programmers
version: V2J17
serial: 303030303030303030303031
flash: 2097152 (pagesize: 16384)
sram: 262144
chipid: 0x419
dev-type: STM32F42x_F43x
```
### 燒錄測試
:::danger
注意書寫規範:
* 使用 lab0 規範的程式碼書寫風格,務必用 clang-format 確認一致
> [name= hugo0406]
> 已修正
:::
- [ ] `blink.c`
```c
#define RCC_AHB1_PERI_ENBLR_ADDR \
(*((volatile unsigned long *) (0x40023800 + 0x30)))
#define GPIO_G_PORTMODE (*((volatile unsigned long *) 0x40021800))
#define GPIO_G_OUTPUT_TYPE (*((volatile unsigned long *) (0x40021800 + 0x4)))
#define GPIO_G_PUPD (*((volatile unsigned long *) (0x40021800 + 0xC)))
#define GPIO_G_PORT_SETRESET (*((volatile unsigned long *) (0x40021800 + 0x18)))
asm(".word 0x20001000");
asm(".word main ");
int main()
{
int i;
RCC_AHB1_PERI_ENBLR_ADDR |= (0x00000001 << 6); /*Enalbe GPIO G clock*/
GPIO_G_PORTMODE |=
(0x00000001 << 26 | 0x00000001 << 28); /* Set PG13 PG14 mode to GPIO */
GPIO_G_PORTMODE &= ~(0x00000001 << 27 | 0x00000001 << 29);
GPIO_G_OUTPUT_TYPE &=
~(0x00000001 << 13 | 0x00000001 << 14); /*Set PG13 PG14 to push-pull*/
GPIO_G_PUPD |=
(0x00000001 << 27 | 0x00000001 << 29); /*Set PG13 PG14 to pull-down*/
while (1) {
GPIO_G_PORT_SETRESET = 0x20004000; /*PG13 off, PG14 on*/
for (i = 0; i < 100000; i++)
;
GPIO_G_PORT_SETRESET = 0x40002000; /*PG14 off, PG13 on*/
for (i = 0; i < 100000; i++)
;
}
return 0;
}
```
- [ ] `Makefile`
```shell
CROSS_COMPILE ?= arm-none-eabi-
.PHONY: all
all: blink.bin
blink.o: blink.c
$(CROSS_COMPILE)gcc -mcpu=cortex-m4 -mthumb -nostartfiles -c blink.c -o blink.o
blink.out: blink.o blink.ld
$(CROSS_COMPILE)ld -T blink.ld -o blink.out blink.o
blink.bin: blink.out
$(CROSS_COMPILE)objcopy -j .text -O binary blink.out blink.bin
clean:
rm -rf *.o *.out *.bin
```
- [ ] `blink.ld`
```shell
SECTIONS
{
. = 0x0;
.text :
{
*(.text)
}
}
```
執行 `make` :
```shell
$ make
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -nostartfiles -c blink.c -o blink.o
arm-none-eabi-ld -T blink.ld -o blink.out blink.o
arm-none-eabi-objcopy -j .text -O binary blink.out blink.bin
```
編譯完後會產生 `blink.out` 及 `blink.bin` ,將 `blink.bin` 燒進 flash
```shell
$ st-flash write blink.bin 0x08000000
st-flash 1.8.0-20-gdce9d6e
2024-06-05T14:51:48 INFO common.c: STM32F42x_F43x: 256 KiB SRAM, 2048 KiB flash in at least 16 KiB pages.
file blink.bin md5 checksum: b18937a2d44b06afca48131c6027f2, stlink checksum: 0x00002c76
2024-06-05T14:51:48 INFO common_flash.c: Attempting to write 160 (0xa0) bytes to stm32 address: 134217728 (0x8000000)
EraseFlash - Sector:0x0 Size:0x4000 -> Flash page at 0x8000000 erased (size: 0x4000)
2024-06-05T14:51:48 INFO flash_loader.c: Starting Flash write for F2/F4/F7/L4
2024-06-05T14:51:48 INFO flash_loader.c: Successfully loaded flash loader in sram
2024-06-05T14:51:48 INFO flash_loader.c: Clear DFSR
2024-06-05T14:51:48 INFO flash_loader.c: enabling 32-bit flash writes
2024-06-05T14:51:48 INFO common_flash.c: Starting verification of write complete
2024-06-05T14:51:48 INFO common_flash.c: Flash written and verified! jolly good!
```
燒錄後按 RESET 即可看到 `LD3(綠)`、`LD4(紅)` 開始交替閃爍
:::danger
說明程式原理,特別要搭配 GPIO 說明。
:::
## TODO: 整理系統啟動流程
> 介紹 u-boot 到 Linux 核心的初始化流程,要提及 device tree
### Boot loader
#### What is a boot loader?
:::danger
用你的理解書寫,避免複製來路不明的文字片段。
:::
When the processor is powered on, the memory doesn't hold an operating system, so special software is needed to bring the OS into memory from the media on which it resides. This software is normally a small piece of code called the boot loader.
On a desktop PC, the boot loader resides on the master boot record (MBR) of the hard drive and is executed after the PC's basic input output system (BIOS) performs system initialization tasks.
### U-boot
#### Image formats
U-Boot is capable of booting images in two formats:
- New uImage format (**FIT**)
- It allows the use of images with multiple components (several kernels, ramdisks, etc.)
:::info
FIT 是 flattened image tree 的縮寫,類似於 FDT (flattened device tree)。它利用了Device Tree Source files(DTS)的語法,生成的 image 文件也和 dtb 文件類似(稱作 itb)
:::
- Old uImage format
- Old image format is based on binary files which can be basically anything, preceded by a special header
- Basically, the header defines the following image properties:
- Target Operating System
- Target CPU Architecture
- Compression Type
- Load Address
- Entry Point
- Image Name
- Image Timestamp
#### U-Boot supports the following image types:
With U-Boot, "normal" build targets like "zImage" or "bzImage" are not used.
- Standalone Programs
- OS Kernel Images
- RAMDisk Images
- Multi-File Images
- Firmware Images
- Script files
#### Supported devices
U-Boot supports the following STMP32 MCU SoCs:
- STM32F429
- STM32F469
- STM32F746
- STM32F769
- STM32H743
- STM32H750
The boot chain without `SPL(Secondary Program Loader)`
defconfig_file:
- stm32f429-discovery_defconfig
- stm32f429-evaluation_defconfig
- stm32f469-discovery_defconfig
- stm32746g-eval_defconfig
- stm32f746-disco_defconfig
- stm32f769-disco_defconfig
- stm32h743-disco_defconfig
- stm32h743-eval_defconfig
- stm32h750-art-pi_defconfig
| **FSBL** | **OS** |
| -------- | -------- |
| U-boot | Linux |
(**Debian based**) Depending on the build targets further packages maybe needed :
```
sudo apt-get install bc bison build-essential coccinelle \
device-tree-compiler dfu-util efitools flex gdisk graphviz imagemagick \
liblz4-tool libgnutls28-dev libguestfs-tools libncurses-dev \
libpython3-dev libsdl2-dev libssl-dev lz4 lzma lzma-alone openssl \
pkg-config python3 python3-asteval python3-coverage python3-filelock \
python3-pkg-resources python3-pycryptodome python3-pyelftools \
python3-pytest python3-pytest-xdist python3-sphinxcontrib.apidoc \
python3-sphinx-rtd-theme python3-subunit python3-testtools \
python3-virtualenv swig uuid-dev
```
下載 U-boot 原始碼:
```shell
git clone git://git.denx.de/u-boot.git
cd u-boot
```
編譯 U-boot:
```shell=
make mrproper
make stm32f429-discovery_defconfig
# make menuconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4
```
:::danger
注意用語:
* file 是「檔案」,而非「文件」(document)
> [name= hugo0406]
> 已修正
:::
- 第一行是移除之前編譯所產生的檔案、配置、備份
- 第二行是生成一個 `.config` 預設配置
- 第三行是用於更改預設配置,因此它會修改產生的 `.config`
- 第四行直接開始編譯 U-Boot 原始碼,目標檔案是 u-boot.bin
- Table. Files generated during U-Boot’s compilation
| Filename | Description |
|:----------:|:-------------- |
| u-boot.map | The symbol map |
| u-boot | U-Boot executable in ELF binary format |
| u-boot.bin | U-Boot raw binary image, which can be written to the boot storage device |
查看 `u-boot.bin` 大小
```shell
$ ls -lh u-boot.bin
-rw-rw-r-- 1 cychen cychen 165K 六 2 21:22 u-boot.bin
```
燒進 FLASH
```shell
$ st-flash write u-boot.bin 0x08000000
st-flash 1.8.0-20-gdce9d6e
2024-06-06T17:01:14 INFO common.c: STM32F42x_F43x: 256 KiB SRAM, 2048 KiB flash in at least 16 KiB pages.
file u-boot.bin md5 checksum: 8ca192dd4784d2f0238c045a47fd0, stlink checksum: 0x00d064a8
2024-06-06T17:01:14 INFO common_flash.c: Attempting to write 168912 (0x293d0) bytes to stm32 address: 134217728 (0x8000000)
EraseFlash - Sector:0x0 Size:0x4000 -> Flash page at 0x8000000 erased (size: 0x4000)
EraseFlash - Sector:0x1 Size:0x4000 -> Flash page at 0x8004000 erased (size: 0x4000)
EraseFlash - Sector:0x2 Size:0x4000 -> Flash page at 0x8008000 erased (size: 0x4000)
EraseFlash - Sector:0x3 Size:0x4000 -> Flash page at 0x800c000 erased (size: 0x4000)
EraseFlash - Sector:0x4 Size:0x10000 -> Flash page at 0x8010000 erased (size: 0x10000)
EraseFlash - Sector:0x5 Size:0x20000 -> Flash page at 0x8020000 erased (size: 0x20000)
2024-06-06T17:01:19 INFO flash_loader.c: Starting Flash write for F2/F4/F7/L4
2024-06-06T17:01:19 INFO flash_loader.c: Successfully loaded flash loader in sram
2024-06-06T17:01:19 INFO flash_loader.c: Clear DFSR
2024-06-06T17:01:19 INFO flash_loader.c: enabling 32-bit flash writes
2024-06-06T17:01:23 INFO common_flash.c: Starting verification of write complete
2024-06-06T17:01:26 INFO common_flash.c: Flash written and verified! jolly good!
```
### Debug
- The debug features are used by the debugger host when connecting to and debugging the STM32F4xx MCUs.
- Two interfaces for debug are available:
• **Serial wire**
• **JTAG debug port**
#### USART Connection
- The STM32F429 Discovery is equipped with various USARTs. USART stands for Universal Synchronous Asynchronous Receiver Transmitter. The USARTs on the STM32F429 support a wide range of serial protocols, the usual asynchronous ones, plus things like IrDA, SPI etc. Since the STM32 works on 3.3V levels, a level shifting component is needed to connect the USART of the STM32F429 to a PC serial port.
| STM32 PIN | VCP |
| --------- | ---- |
| PA9 | USART1_TX |
| PA10 | USART1_RX |
USB to TTL 接線:
- PA9 --> RXD
- PA10 --> TXD
- GND --> GND
![image](https://hackmd.io/_uploads/SJkzQZmrA.png =375x225)
```shell
$ sudo dmesg | grep tty
[ 0.134512] printk: console [tty0] enabled
[ 5.749521] usb 1-1.4: cp210x converter now attached to ttyUSB0
$ sudo chmod 666 /dev/ttyUSB0
```
安裝 PUTTY:
```shell
sudo add-apt-repository universe
sudo apt install putty
```
查看 PUTTY版本
```shell
$ putty --version
PuTTY: Release 0.76
Build platform: 64-bit Unix (GTK + X11)
Compiler: gcc 11.2.0
Compiled against GTK version 3.24.30
Source commit: 1fd7baa7344bb38d62a024e5dba3a720c67d05cf
```
:::danger
指名穩定版本的 Linux 核心超連結,如 https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
:::
可以從 <s>[官方文件中](https://github.com/siemens/u-boot/blob/master/drivers/serial/Kconfig)</s> 得知 **Baud Rate** 預設為 `115200` ,**UART number** 預設為 `1`
```
config BAUDRATE
int "Default baudrate"
default 115200
help
Select a default baudrate, where "default" has a driver-specific
meaning of either setting the baudrate for the early debug UART
in the SPL stage (most drivers) or for choosing a default baudrate
in the absence of an environment setting (serial_mxc.c).
config CONS_INDEX
int "UART used for console"
depends on SPECIFY_CONSOLE_INDEX
range 0 6
default 1
help
Set this to match the UART number of the serial console.
```
開啟 `putty` , 選擇 serial , /dev/ttyUSB0 , Baud rate 選擇 115200
![image](https://hackmd.io/_uploads/Sk-CMz_SC.png )
按開發版上的 RESET, 可看見 U-Boot 命令提示如下:
```
U-Boot 2024.07-rc3-00020-gea722aa5eb (Jun 09 2024 - 22:25:14 +0800)
DRAM: 8 MiB
Core: 73 devices, 11 uclasses, devicetree: separate
Flash: 2 MiB
Loading Environment from Flash... *** Warning - bad CRC, using default environme nt
In: serial@40011000
Out: serial@40011000
Err: serial@40011000
U-Boot > help
? - alias for 'help'
base - print or set address offset
bdinfo - print Board Info structure
boot - boot default, i.e., run 'bootcmd'
bootd - boot default, i.e., run 'bootcmd'
bootelf - Boot from an ELF image in memory
bootm - boot application image from memory
bootvx - Boot vxWorks from an ELF image
cmp - memory compare
coninfo - print console devices and information
cp - memory copy
crc32 - checksum calculation
dm - Driver model low level access
echo - echo args to console
editenv - edit environment variable
env - environment handling commands
erase - erase FLASH memory
exit - exit script
false - do nothing, unsuccessfully
fdt - flattened device tree utility commands
flinfo - print FLASH memory information
go - start application at address 'addr'
help - print command description/usage
iminfo - print header information for application image
imls - list all images found in flash
imxtract - extract a part of a multi-image
itest - return true/false on integer compare
loadb - load binary file over serial line (kermit mode)
loads - load S-Record file over serial line
loadx - load binary file over serial line (xmodem mode)
loady - load binary file over serial line (ymodem mode)
loop - infinite loop on address range
md - memory display
mm - memory modify (auto-incrementing address)
mw - memory write (fill)
nm - memory modify (constant address)
panic - Panic with optional message
pinmux - show pin-controller muxing
printenv - print environment variables
protect - enable or disable FLASH write protection
reset - Perform RESET of the CPU
run - run commands in an environment variable
saveenv - save environment variables to persistent storage
setenv - set environment variables
showvar - print local hushshell variables
sleep - delay execution for some time
source - run script from memory
test - minimal test like /bin/sh
timer - access the system timer
true - do nothing, successfully
version - print monitor, compiler and linker version
U-Boot >
```
`version` -> 顯示目前 U-Boot 版本
```
U-Boot > version
U-Boot 2024.07-rc3-00020-gea722aa5eb (Jun 09 2024 - 22:25:14 +0800)
```
:::danger
正確標注硬體名稱。
> [name= hugo0406]
> 已修正
:::
`bdinfo` -> 顯示目前 STM32F429 資訊
```
U-Boot > bdinfo
boot_params = 0x00000000
DRAM bank = 0x00000000
-> start = 0x90000000
-> size = 0x00800000
flashstart = 0x08000000
flashsize = 0x00200000
flashoffset = 0x000241b8
baudrate = 115200 bps
relocaddr = 0x907ca000
reloc off = 0x887ca000
Build = 32-bit
fdt_blob = 0x905c2cb0
new_fdt = 0x905c2cb0
fdt_size = 0x00005220
lmb_dump_all:
memory.cnt = 0x1 / max = 0x10
memory[0] [0x90000000-0x907fffff], 0x00800000 bytes flags: 0
reserved.cnt = 0x1 / max = 0x10
reserved[0] [0x905be9a8-0x907fffff], 0x00241658 bytes flags: 0
devicetree = separate
arch_number = 0x00000000
TLB addr = 0x907f0000
irq_sp = 0x905c2ca0
sp start = 0x905c2c90
Early malloc usage: f40 / 2000
```
## TODO: 調整 Linux 核心組態
> 針對系統啟動的需求,調整 Linux 核心組態,使其得以在 STM32F429-Discovery 運作。
### Build
- kernel
```shell
// 版本太舊
git clone git://git.kernel.org/pub/scm/linux/kernel/git/atorgue/stm32.git
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
```
#### Menuconfig & Kconfig
詳細內容請參閱核心程式碼目錄下的文件 ./Documentation/kbuild/kconfig-language.rst
:::danger
不能選擇 `tinyconfig`,務必依循 [stm32_defconfig](https://github.com/torvalds/linux/blob/master/arch/arm/configs/stm32_defconfig) 並針對 STM32F429 進行調整。
對照 [移植 linux 5.5.3 到 stm32f429-discovery 板資料](https://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=624411) 的核心組態。
:::
`make tinyconfig` 產生最小的核心,產生的 .config 非常小,不能展示訊息
```shell
make ARCH=arm tinyconfig
```
```
HOSTCC scripts/kconfig/conf.o
HOSTCC scripts/kconfig/confdata.o
HOSTCC scripts/kconfig/expr.o
LEX scripts/kconfig/lexer.lex.c
YACC scripts/kconfig/parser.tab.[ch]
HOSTCC scripts/kconfig/lexer.lex.o
HOSTCC scripts/kconfig/menu.o
HOSTCC scripts/kconfig/parser.tab.o
HOSTCC scripts/kconfig/preprocess.o
HOSTCC scripts/kconfig/symbol.o
HOSTCC scripts/kconfig/util.o
HOSTLD scripts/kconfig/conf
#
# configuration written to .config
#
Using .config as base
Merging ./kernel/configs/tiny.config
Value of CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE is redefined by fragment ./kernel/configs/tiny.config:
Previous value: CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE=y
New value: # CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE is not set
Value of CONFIG_CC_OPTIMIZE_FOR_SIZE is redefined by fragment ./kernel/configs/tiny.config:
Previous value: # CONFIG_CC_OPTIMIZE_FOR_SIZE is not set
New value: CONFIG_CC_OPTIMIZE_FOR_SIZE=y
Value of CONFIG_KERNEL_GZIP is redefined by fragment ./kernel/configs/tiny.config:
Previous value: CONFIG_KERNEL_GZIP=y
New value: # CONFIG_KERNEL_GZIP is not set
Value of CONFIG_KERNEL_XZ is redefined by fragment ./kernel/configs/tiny.config:
Previous value: # CONFIG_KERNEL_XZ is not set
New value: CONFIG_KERNEL_XZ=y
Value of CONFIG_SLUB_TINY is redefined by fragment ./kernel/configs/tiny.config:
Previous value: # CONFIG_SLUB_TINY is not set
New value: CONFIG_SLUB_TINY=y
#
# merged configuration written to .config (needs make)
#
#
# configuration written to .config
#
```
Menu Config
```shell
make ARCH=arm menuconfig
```
When I try to menuconfig, but got some error message
```
make[1]: Entering directory '/home/cychen/stm32/stm32/build/stm32f429'
GEN ./Makefile
HOSTCC scripts/kconfig/mconf.o
<command-line>: fatal error: curses.h: No such file or directory
compilation terminated.
make[2]: *** [scripts/Makefile.host:108: scripts/kconfig/mconf.o] Error 1
make[1]: *** [/home/cychen/stm32/stm32/Makefile:544: menuconfig] Error 2
make[1]: Leaving directory '/home/cychen/stm32/stm32/build/stm32f429'
make: *** [Makefile:150: sub-make] Error 2
```
`libncurses5-dev need` to be installed before compiling the kernel
```shell
sudo apt-get install libncurses5-dev
```
![image](https://hackmd.io/_uploads/BkrWbsVN0.png)
![image](https://hackmd.io/_uploads/Hk5w2i4VC.png)
編譯核心:
```shell
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4
```
編譯完後,可以在核心程式碼目錄下 ./arch/arm/boot/ 看到 `xipImage`
:::info
xip 代表 execute in place :
程式直接在 flash 上執行,而不必搬到 RAM 上,只有 data 搬到 RAM 。可以減少 memory 的使用,但執行速度較慢。
:::
查看 xipImage 大小
```
$ls -lh xipImage
-rwxrwxr-x 1 cychen cychen 1.8M 五 30 15:11 xipImage
```
:::danger
xipImage 竟然有 1.8MB,幾乎佔了 Flash 大部分的空間,沒有足夠的空間來存放檔案系統,只能保留最基本的功能,盡量減小核心大小
:::