STM32 Win10下搭建嵌入式ARM開發環境
一個嵌入式應用的開發一般由如下幾個環節構成:
- 創建工程,將中間件、應用代碼、驅動代碼添加至工程中
- 配置工程的編譯選項,編譯並連接,生成二進制目標碼
- 將PC通過仿真器與開發板連接,將二進制目標碼通過仿真器傳輸至芯片flash
- 測試並修改bug,直到完成預期目標
這個過程中,工程文件一般為文本文件,與平台無關,編譯和連接過程與平台相關,燒寫調試過程與平台相關。因此一個便於移植的工程應盡可能將工程文件和工程配置文件獨立出來,在其他開發環境開發時只需要配置好編譯連接工具鏈和燒寫調試工具即可進行開發,而盡可能較少對IDE的依賴,同時要考慮到商業授權等因素。
因此這里搭建的STM32開發環境為:
- (1) 編寫工程文件和配置文件(.c,.s,.a,.lib,makefile,.ld,.scatter)
- (2) 准備文本編輯器的配置(VScode)
- (3) 安裝編譯連接工具鏈(arm-gcc)
- (4) 安裝下載調試工具(stlink+openocd)
其中(1)為工程文件,與路徑和平台無關,在配置好(2)(3)(4)后即可實現開發,實際開發時只需關注(1)即可。
編譯流程梳理
一個ARM嵌入式程序的編譯需要以下幾個環節:
- 由C語言源碼和匯編代碼經過編譯器生成obj文件(.o), 目標文件(.o)通常以ELF格式保存,里面包含了對各個函數的入口標記,描述等
- 由內存地址分配文件即Linker,Scatter(.ld,.scat)和.o文件經連接器進行連接,得到可執行鏡像文件(.elf,.out,.axf)以及二進制鏡像文件(.bin)和其他描述文件等
- elf文件包含了程序的相關信息,包括入口地址等等
- axf文件是ARM芯片使用的文件格式,它包含bin代碼外,還包括了調試信息。
考慮開發環境以及平台移植性等等因素,開發嵌入式應用應盡量使用文本編輯器+交叉編譯工具鏈+燒錄工具的方式,這樣可以盡可能避免由於操作系統或IDE更新等造成工程編譯失敗等問題。
交叉編譯工具鏈
交叉編譯工具鏈是一個由編譯器、連接器和解釋器組成的綜合開發環境,交叉編譯工具鏈可以代替IDE的編譯和連接等工作。
交叉編譯工具鏈的命名規則為:arch [-vendor] [-os] [-(gnu)eabi]
- arch - 體系架構,如ARM,MIPS
- vendor - 工具鏈提供商
- os - 目標操作系統
- eabi - 嵌入式應用二進制接口(Embedded Application Binary Interface)
以windows下開發為例,則該工具鏈應為:arm-none-eabi-gcc
,即ARM架構,無提供商,無操作系統(os未指定),eabi格式。
GNU提供了ARM的交叉編譯工具鏈的下載:
GNU Arm Embedded Toolchain
交叉編譯工具鏈安裝完成后,將安裝目錄下的.\bin
路徑添加至環境變量,重啟電腦后可以在win10的PowerShell或者CMD中使用arm-none-eabi-gcc -v
查看是否安裝成功。
我們還需要Make工具來實現我們對Makefile的操作,GNU中提供了windows下的make工具:
選擇其中的Complete package, except sources
進行下載,安裝后將安裝路徑下的.\bin
添加環境變量,使用make -v
查看安裝是否成功。
使用Makefile創建工程
通過編寫Makefile文件可以對項目工程的編譯鏈接過程進行控制,這里使用CubeMX來生成一個STM32的Makefile工程,簡化配置的過程,其他ARM項目可以自行編寫Makefile文件進行配置,關於CubeMX的使用這里不做介紹。
CubeMX中我們選擇STM32F407ZGTx芯片,配置RCC時鍾為HSE選擇Crystal,輸入時鍾為8M,主PLL為168M,開發板的LED對應GPIO為PG9,配置GPIO為輸出模式。修改main.c的代碼實現一個LED閃爍:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
static uint8_t fac_us=0;//us延時倍乘數
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void delay_us(uint32_t nus)
{
uint32_t temp;
SysTick->LOAD=nus*fac_us; //時間加載
SysTick->VAL=0x00; //清空計數器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //開始倒數
do
{
temp=SysTick->CTRL;
}
while((temp&0x01)&&!(temp&(1<<16)));//等待時間到達
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //關閉計數器
SysTick->VAL =0X00; //清空計數器
}
void delay_ms(uint16_t nms)
{
delay_us((uint32_t)(nms*1000)); //普通方式延時
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
delay_ms(500);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_RESET);
delay_ms(500);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_SET);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_RESET);
/*Configure GPIO pin : PG9 */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
使用make指令編譯后的輸出:
arm-none-eabi-size build/demo_project.elf
text data bss dec hex filename
4376 20 1572 5968 1750 build/demo_project.elf
arm-none-eabi-objcopy -O ihex build/demo_project.elf build/demo_project.hex
arm-none-eabi-objcopy -O binary -S build/demo_project.elf build/demo_project.bin
Shell及下載調試配置
shell
由於Win10的PowerShell的操作指令並不等同於Linux,因此make clean
中的rm無法被正確執行,未避免這個麻煩,安裝Msys2:
根據計算機平台選擇下載,這里選擇x86_64
,在msys2文件夾下有msys2_shell.cmd
,用記事本打開,把第16行rem set MSYS2_PATH_TYPE=inherit
的rem去掉,改成set MSYS2_PATH_TYPE=inherit
,這樣可以使用windows路徑,雙擊msys2_shell.cmd
即可使用shell
下載
將bin文件下載可以使用OpenOCD,由於我們使用的是STM32,也可以使用官方工具STM32 ST-LINK Utility.exe
工具對flash進行下載,ST公司同樣提供了官方下載器的命令行版本ST-LINK_CLI.exe
,使用方法在STM32 ST-LINK Utility.exe
的幫助文檔中有介紹,將下面的語句
"D:\Embedded\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe" -c SWD UR -ME -P "./OBJ/cristal_stm32.hex" -V -Rst
pause
添加至一個新建的批處理文件download.bat
中,在shell中執行./download.bat
即可下載:
$ ./download.bat
d:\Embedded\STM32CubeIDE\workspace_1.0.0\demo_project>"D:\Embedded\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe" -c SWD UR -ME -P "./build/demo_project.hex" -V -Rst
STM32 ST-LINK CLI v1.5.1
STM32 ST-LINK Command Line InterfaceConnected via SWD.
Connexion mode : Connect Under Reset.
ST-LINK Firmware version : V2J27S6
Device ID:0x413
Device flash Size : 1024 Kbytes
Device family :STM32F40x/STM32F41xFull chip erase...
Flash memory erased.Flash Programming:
File : ./build/demo_project.hex
Address : 0x08000000
Flash Programming...
壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙北 100%
Verification...
北北北北北北北北北北北北北北北北北北北北北北北北北?100%
Flash memory programmed in 0s and 547ms.
Verification...OK
Programming Complete.MCU Reset.
d:\Embedded\STM32CubeIDE\workspace_1.0.0\demo_project>pause
請按任意鍵繼續. . .
調試
調試工具使用OpenOCD,OpenOCD是開放式片上調試器,旨在為嵌入式目標器件提供調試,系統內編程和邊界掃描測試,支持多種JTAG,SWD接口的仿真調試器,根據官網可知OpenOCD是支持STlink的,這里下載OpenOCD是源碼:
OpenOCD - Open On-Chip Debugger
這里可以得到預編譯后的工具:
http://gnutoolchains.com/arm-eabi/openocd/
解壓縮后將.\bin
添加至環境變量即可使用,在cmd使用openocd -v
檢查是否安裝成功。
在工程路徑下創建OcdStm32.cfg文件:
interface stlink-v2 #調試器的名字,在openocd-0.10.0\scripts\interface里找
transport select swd #接口名,swd或jtag
source [find target/stm32f4x.cfg] #芯片類型,在openocd-0.10.0\scripts\target里找
創建一個ocd_init.bat文件:
openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg
控制台中執行.\ocd_init.bat
可以啟動仿真器:
D:\Embedded\STM32CubeIDE\workspace_1.0.0\demo_project>openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg
Open On-Chip Debugger 0.10.0 (2019-10-29) [https://github.com/sysprogs/openocd]
Licensed under GNU GPL v2
libusb1 09e75e98b4d9ea7909e8837b7a3f00dda4589dc3
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
WARNING: interface/stlink-v2.cfg is deprecated, please switch to interface/stlink.cfg
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 2000 kHz
Info : STLINK V2J27S6 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.209980
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
出現hardware has 6 breakpoints, 4 watchpoints
即啟動成功
VScode配置
在工程路徑的.vscode文件夾下打開c_cpp_properties.json文件,沒有自己新建一個,內容配置如下:
{
"configurations": [
{
"name": "STM32",
"includePath": [
"C:/Program Files (x86)/keil/ARM/ARMCC/**",
"${workspaceFolder}/**",
""
],
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "${workspaceRoot}/.vscode/.browse.c_cpp.db",
"path": [
"C:/Program Files (x86)/keil/ARM/ARMCC/**",
"${workspaceFolder}/**",
""
]
},
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"__CC_ARM"
],
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
其中,需要在includePath和path中添加頭文件路徑,${workspaceFolder}/**是工程路徑,不用改動,額外需要添加的是keil的頭文件路徑
然后在defines中添加宏,也就是在keil的Options for Target的C++選項卡中配置的宏
然后就可以體驗VS Code強大的代碼提示,函數跳轉等功能了(甩keil的編輯器一整個時代)
在VsCode中,使用快捷鍵ctrl+shift+p搜索setting,找到“首選項:打 開設置(json)”。點擊會進入一個文件,把這兩行添加進去,注意msys2_shell.cmd的地址。
{
"terminal.integrated.shell.windows":"D:/Embedded/GNU Tools ARM Embedded/msys64/msys2_shell.cmd",
"terminal.integrated.shellArgs.windows": ["-defterm", "-mingw32", "-no-start", "-here"],
}
即可在VScode中使用msys2終端。
配置調試工具:
在launch.json配置中添加下面的代碼:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "ARM Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/${workspaceRootFolderName}.elf",
"cwd": "${workspaceFolder}",
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/Embedded/GNU Tools ARM Embedded/5.4 2016q3/bin/arm-none-eabi-gdb.exe", //交叉編譯工具鏈地址
"setupCommands": [
{
"text": "file D:/Embedded/STM32CubeIDE/workspace_1.0.0/demo_project/build/demo_project.elf", //工程使用絕對地址
},
{
"text": "target remote localhost:3333", //調試器端口號
},
//以下命令根據不同的單片機可能有變化,例如STM32F1就不行,需要更換命令,STM32F4可以。gdb是可以手動用命令行執行的,這部分就是需執行的命令。百度,谷歌可以找到的。
{
"text": "monitor reset",
},
{
"text": "monitor halt",
},
{
"text": "load",
}
],
"preLaunchTask": "build"
}
]
}
在CTRL+SHIFT+P中搜索task,配置任務,進入后選擇others,則在工程路徑下的.vscode路徑下新建一個task.json,內容如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "echo compile exeover&make -j4"
}
]
}
全流程演示
打開終端(ctrl + ~
),執行make clean
:
$ make clean
rm -fR build
執行make
:
$ make
mkdir build
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/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 -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_it.d" -Wa,-a,-ad,-alms=build/stm32f4xx_it.lst Src/stm32f4xx_it.c -o build/stm32f4xx_it.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_msp.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_msp.lst Src/stm32f4xx_hal_msp.c -o build/stm32f4xx_hal_msp.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_tim.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_tim.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c -o build/stm32f4xx_hal_tim.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_tim_ex.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_tim_ex.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c -o build/stm32f4xx_hal_tim_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_rcc.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_rcc.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c -o build/stm32f4xx_hal_rcc.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_rcc_ex.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_rcc_ex.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c -o build/stm32f4xx_hal_rcc_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_flash.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_flash.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c -o build/stm32f4xx_hal_flash.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_flash_ex.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_flash_ex.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c -o build/stm32f4xx_hal_flash_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_flash_ramfunc.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_flash_ramfunc.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c -o build/stm32f4xx_hal_flash_ramfunc.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_gpio.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_gpio.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c -o build/stm32f4xx_hal_gpio.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_dma_ex.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_dma_ex.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c -o build/stm32f4xx_hal_dma_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_dma.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_dma.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c -o build/stm32f4xx_hal_dma.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_pwr.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_pwr.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c -o build/stm32f4xx_hal_pwr.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_pwr_ex.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_pwr_ex.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c -o build/stm32f4xx_hal_pwr_ex.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_cortex.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_cortex.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c -o build/stm32f4xx_hal_cortex.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c -o build/stm32f4xx_hal.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_exti.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_exti.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c -o build/stm32f4xx_hal_exti.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall
-fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/system_stm32f4xx.d" -Wa,-a,-ad,-alms=build/system_stm32f4xx.lst Src/system_stm32f4xx.c -o build/system_stm32f4xx.o
arm-none-eabi-gcc -x assembler-with-cpp -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F407xx -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/startup_stm32f407xx.d" startup_stm32f407xx.s -o build/startup_stm32f407xx.o
arm-none-eabi-gcc build/main.o build/stm32f4xx_it.o build/stm32f4xx_hal_msp.o build/stm32f4xx_hal_tim.o build/stm32f4xx_hal_tim_ex.o build/stm32f4xx_hal_rcc.o build/stm32f4xx_hal_rcc_ex.o build/stm32f4xx_hal_flash.o build/stm32f4xx_hal_flash_ex.o build/stm32f4xx_hal_flash_ramfunc.o build/stm32f4xx_hal_gpio.o build/stm32f4xx_hal_dma_ex.o build/stm32f4xx_hal_dma.o build/stm32f4xx_hal_pwr.o build/stm32f4xx_hal_pwr_ex.o build/stm32f4xx_hal_cortex.o build/stm32f4xx_hal.o build/stm32f4xx_hal_exti.o build/system_stm32f4xx.o build/startup_stm32f407xx.o -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -specs=nano.specs -TSTM32F407ZGTx_FLASH.ld -lc -lm -lnosys -Wl,-Map=build/demo_project.map,--cref -Wl,--gc-sections -o build/demo_project.elf
arm-none-eabi-size build/demo_project.elf
text data bss dec hex filename
4376 20 1572 5968 1750 build/demo_project.elf
arm-none-eabi-objcopy -O ihex build/demo_project.elf build/demo_project.hex
arm-none-eabi-objcopy -O binary -S build/demo_project.elf build/demo_project.bin
此時已經生成目標文件(demo_project.hex),直接下載./download.bat
:
$ ./download.bat
D:\Embedded\STM32CubeIDE\workspace_1.0.0\demo_project>"D:\Embedded\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe" -c SWD UR -ME -P "./build/demo_project.hex" -V -Rst
STM32 ST-LINK CLI v1.5.1
STM32 ST-LINK Command Line InterfaceConnected via SWD.
Connexion mode : Connect Under Reset.
ST-LINK Firmware version : V2J27S6
Device ID:0x413
Device flash Size : 1024 Kbytes
Device family :STM32F40x/STM32F41xFull chip erase...
Flash memory erased.Flash Programming:
File : ./build/demo_project.hex
Address : 0x08000000
Flash Programming...
壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙壙北 100%
Verification...
北北北北北北北北北北北北北北北北北北北北北北北北北?100%
Flash memory programmed in 0s and 563ms.
Verification...OK
Programming Complete.MCU Reset.
D:\Embedded\STM32CubeIDE\workspace_1.0.0\demo_project>pause
請按任意鍵繼續. . .
需要調試時,啟動調試器./ocd_init.bat
$ ./ocd_init.bat
D:\Embedded\STM32CubeIDE\workspace_1.0.0\demo_project>openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg
Open On-Chip Debugger 0.10.0 (2019-10-29) [https://github.com/sysprogs/openocd]
Licensed under GNU GPL v2
libusb1 09e75e98b4d9ea7909e8837b7a3f00dda4589dc3
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
WARNING: interface/stlink-v2.cfg is deprecated, please switch to interface/stlink.cfg
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 2000 kHz
Info : STLINK V2J27S6 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.209980
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
按下F5執行GDB進行調試:
Executing task: echo compile exeover&make -j4 <
/d: /d: Is a directory
make: Nothing to be done for `all'.終端將被任務重用,按任意鍵關閉。
至此開發環境搭建完畢,由於CubeMX生成的Makefile文件以及工程組織性並不理想,同時VScode的插件配置尚未完善,后續優化插件和Makefile結構