linux stm32 开发
最近换了双系统,发现还是linux下面写程序爽。windows还是比较适合打游戏233. 这篇文章记录一下linux下开发stm32的一些东西。
安装vscode以及交叉编译器
在官网下载即可
安装交叉编译链
下载地址 下载好后解压并且设置路径,可以看这篇文章中的安装交叉编译链部分。
安装stm32cubemx
下载地址
下载好后执行××.linux
即可
安装jlink驱动
下载地址
Ubuntu的话双击即可安装。
安装成功后运行如下出现信息说明成功,输入q
退出。
➜ gitio JLinkExe
SEGGER J-Link Commander V6.12j (Compiled Feb 15 2017 18:03:38)
DLL version V6.12j, compiled Feb 15 2017 18:03:30
Connecting to J-Link via USB...O.K.
Firmware: J-Link ARM V8 compiled Jan 31 2018 18:34:52
Hardware version: V8.00
S/N: 20080643
License(s): RDI,FlashDL,FlashBP,JFlash,GDBFull
VTref = 3.371V
Type "connect" to establish a target connection, '?' for help
J-Link>
使用cubemx建立一个makefile工程
建立工程我就不赘述了。这里放一下vscode的配置 {
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/Inc"
],
"defines": [
"USE_HAL_DRIVER",
"STM32L431xx"
],
"compilerPath": "/opt/gccStm32/bin/arm-none-eabi-gcc",
"cStandard": "c99",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
编译
编译只需要make
即可在build
文件下生成bin
和hex
文件
➜ gcclight make
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER-DSTM32L431xx -IInc -IHardware/BH1750 -IHardware/DHT11 -IHardware/GPS -IHardware/OLED -IDrivers/STM32L4xx_HAL_Driver/Inc -IDrivers/STM32L4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32L4xx/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/main.d" -Wa,-a,-ad,-alms=build/main.lst Src/main.c -o build/main.o
arm-none-eabi-gcc build/main.o build/gpio.o build/adc.o build/tim.o build/usart.o build/delay.o build/BH1750.o build/DHT11_BUS.o build/gps.o build/oled.o build/stm32l4xx_it.o build/stm32l4xx_hal_msp.o build/stm32l4xx_hal_adc.o build/stm32l4xx_hal_adc_ex.o build/stm32l4xx_hal_tim.o build/stm32l4xx_hal_tim_ex.o build/stm32l4xx_hal_uart.o build/stm32l4xx_hal_uart_ex.o build/stm32l4xx_hal.o build/stm32l4xx_hal_i2c.o build/stm32l4xx_hal_i2c_ex.o build/stm32l4xx_hal_rcc.o build/stm32l4xx_hal_rcc_ex.o build/stm32l4xx_hal_flash.o build/stm32l4xx_hal_flash_ex.o build/stm32l4xx_hal_flash_ramfunc.o build/stm32l4xx_hal_gpio.o build/stm32l4xx_hal_dma.o build/stm32l4xx_hal_dma_ex.o build/stm32l4xx_hal_pwr.o build/stm32l4xx_hal_pwr_ex.o build/stm32l4xx_hal_cortex.o build/system_stm32l4xx.o build/startup_stm32l431xx.o -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard-specs=nano.specs -TSTM32L431RBTx_FLASH.ld -lc -lm -lnosys -Wl,-Map=build/gcclight.map,--cref -Wl,--gc-sections -o build/gcclight.elf
arm-none-eabi-size build/gcclight.elf
text data bss dec hex filename
18160 120 2128 20408 4fb8 build/gcclight.elf
arm-none-eabi-objcopy -O ihex build/gcclight.elf build/gcclight.hex
arm-none-eabi-objcopy -O binary -S build/gcclight.elf build/gcclight.bin
烧录
连接到jlink 我使用如下脚本执行。
# 30s 超时
set timeout 30
# 执行
spawn JLinkExe
# 启动成功
expect "J-Link>"
# 连接
send "connect\r"
# 检测到默认设备
expect "<Default>:"
# 确认设备
send "\r"
# 选择连接方式
expect "TIF>"
# 确认连接方式
send "S\r"
# 选择速度
expect "Speed>"
# 确认速度
send "\r"
# 用于测试
# # 连接成功
# expect "J-Link>"
# # 重启
# send "rx 20\r"
# 烧录程序
# 如果连接成功
expect "identified."
# 烧录程序
send "loadbin build/gcclight.bin 0x8000000\r"
# 如果烧录成功
expect "O.K."
# 重启
send "rx 20\r"
# 如果重启成功
expect "J-Link>"
# 运行程序
send "g\r"
# 如果运行成功
expect "J-Link>"
# 退出jlink
send "qc\r"
# 将控制权交给用户
interact
终端输入如下
➜ gcclight ./install.sh |