contributed by < hugo0406
>
參考 STM32F429 的 Reference Manual
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
開始執行
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.
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
tar jxvf arm-2010q1-189-arm-uclinuxeabi-i686-pc-linux-gnu.tar.bz2
export PATH=`pwd`/arm-2010q1/bin:$PATH
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.
sudo apt-get install genromfs
stlink
is an open source toolset to program and debug STM32 devices and boards manufactured by STMicroelectronics. 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
$ 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
STM32F42I-DISCO 內建 ST-LINK/V2,已經包含 ST-Link 了,所以 USB-mini 插上去後就可以直接燒錄,不需要額外的燒錄器。
參考 嵌入式系統建構:開發運作於STM32的韌體程式 確保能成功將檔案燒錄到開發版上
blink.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
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
SECTIONS
{
. = 0x0;
.text :
{
*(.text)
}
}
執行 make
:
$ 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
$ 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(紅)
開始交替閃爍
// 版本太舊
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
詳細內容請參閱核心程式碼目錄下的文件 ./Documentation/kbuild/kconfig-language.rst
make tinyconfig
產生最小的核心,產生的 .config 非常小,不能展示訊息
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
#
不能使用 tinyconfig
make ARCH=arm stm32_defconfig
#
# configuration written to .config
#
Menu Config
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
sudo apt-get install libncurses5-dev
編譯核心:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4
編譯完後,可以在核心程式碼目錄下 ./arch/arm/boot/ 看到 xipImage
xip 代表 execute in place :
程式直接在 flash 上執行,而不必搬到 RAM 上,只有 data 搬到 RAM 。可以減少 memory 的使用,但執行速度較慢。
查看 xipImage 大小
$ls -lh xipImage
-rwxrwxr-x 1 cychen cychen 1.8M 五 30 15:11 xipImage
xipImage 竟然有 1.8MB,幾乎佔了 Flash 大部分的空間,沒有足夠的空間來存放檔案系統,只能保留最基本的功能,盡量減小核心大小
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 is capable of booting images in two formats:
FIT 是 flattened image tree 的縮寫,類似於 FDT (flattened device tree)。它利用了Device Tree Source files(DTS)的語法,生成的 image 文件也和 dtb 文件類似(稱作 itb)
With U-Boot, "normal" build targets like "zImage" or "bzImage" are not used.
U-Boot supports the following STMP32 MCU SoCs:
The boot chain without SPL(Secondary Program Loader)
defconfig_file:
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 原始碼:
git clone git://git.denx.de/u-boot.git
cd u-boot
編譯 U-boot:
make mrproper
make stm32f429-discovery_defconfig
# make menuconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4
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
大小
$ ls -lh u-boot.bin
-rw-rw-r-- 1 cychen cychen 165K 六 2 21:22 u-boot.bin
燒進 FLASH
$ 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!
STM32 PIN | VCP |
---|---|
PA9 | USART1_TX |
PA10 | USART1_RX |
USB to TTL 接線:
$ 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:
sudo add-apt-repository universe
sudo apt install putty
查看 PUTTY版本
$ 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
可以從 官方文件中 得知 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
按開發版上的 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)
bdinfo
-> 顯示目前板子資訊
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
製作 uImage
git@github.com:fdu/STM32F429I-disco_Buildroot.git