基于 Ubuntu 18.04 的 STM32F407 开发
一、编译一个最小工程
编译任何文件都需要带上如下参数。
参数 | 说明 |
---|---|
-mthumb | 表明使用的指令集(必需) |
-mcpu=cortex-m3 | 表明芯片内核(必需) |
-g | 产生调试信息 |
文件树如下。
xiechen@xiechen-Ubuntu:~/文档/5.stm32/1.stm32f407t8u6/demo$ tree
.
├── main.c
├── startup_stm32f40xx.s
├── stm32f4xx.h
├── stm32_flash.ld
└── system_stm32f4xx.h
0 directories, 5 files
1.1 编译启动文件
编译汇编文件前需要添加的参数如下。
参数 | 说明 |
---|---|
-x assembler-with-cpp | 先对文件进行预处理 |
-Wa,option | 向汇编器Assembler传递参数 |
注:可以向汇编器传递的参数如下。
参数 | 说明 |
---|---|
-W或–no-warn | 关闭所有告警 |
–fatal-warnings | 将所有的警告提示为错误 |
–warn | 正常提示告警信息 |
在固件库中拷贝启动文件。
STM32F4xx固件库\STM32F4xx_DSP_StdPeriph_Lib_V1.5.1\STM32F4xx_DSP_StdPeriph_Lib_V1.5.1\Libraries\CMSIS\Device\ST\STM32F4xx\Source\Templates\gcc_ride7
使用工具链对启动文件进行编译。
arm-none-eabi-gcc -c -mthumb -mcpu=cortex-m4 -g -Wa,--warn -o startup_stm32f40xx.o startup_stm32f40xx.s
1.2 编译 C 文件
编译 C 需要添加的参数如下。
参数 | 描述 |
---|---|
-Wall | 允许输出所有警告 |
使用工具链对启动文件进行编译。
arm-none-eabi-gcc -c -mthumb -mcpu=cortex-m4 -g -Wall -o main.o main.c
1.3 链接文件
在固件的案例中找到文件 stm32_flash.ld,拷贝到当前工程中。接着让链接器开始根据 stm32_flash.ld 这个文件对 startup_stm32f40xx.o 和 main.o 这两个文件开始链接,生成包含了调试信息的 elf 文件,同时,我们还需要给链接器传递一些参数:
参数 | 描述 |
---|---|
-T | 指定链接文件 |
arm-none-eabi-gcc -o test.elf main.o startup_stm32f40xx.o -mthumb -mcpu=cortex-m4 -T stm32_flash.ld -specs=nosys.specs -static -Wl,-cref,-u,Reset_Handler -Wl,-Map=test.map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 -Wl,--start-group -lc -lm -Wl,--end-group
1.4 生成 bin 和 hex 文件
arm-none-eabi-objcopy test.elf test.bin
arm-none-eabi-objcopy test.elf -Oihex test.hex
1.5 烧写程序
通过 openocd 服务操作 Jlink 烧写程序
【STM32开发环境】Linux下开发stm32(二) | 使用openocd下载程序
sudo apt install openocd
安装好 Jlink 之后,启动 Jlink。
JLinkExe
接着在终端进行连接和配置。
connect
通过 stm32flash 烧写程序
sudo apt install stm32flash
stm32flash /dev/ttyUSB0
stm32flash -w basic_lib_test.hex -v -g 0 /dev/ttyUSB0