# STM32 Environment Note
這裡會存放怎麼在Windows不透過IDE開發STM32的環境筆記。 (For Makefile)
# 目錄
[TOC]
# Build Tools
- [STM32CubeMX](https://www.st.com/en/development-tools/stm32cubemx.html)
- [STM32CubeProgrammer](https://www.st.com/en/development-tools/stm32cubeprog.html)
- [Arm-GNU Compiler](https://developer.arm.com/downloads/-/gnu-rm)
- [windows-build-tools-xpack](https://github.com/xpack-dev-tools/windows-build-tools-xpack)
- [openocd-pack](https://github.com/xpack-dev-tools/openocd-xpack)
# STM32CubeMX
- 編譯時在`Project Manager`選擇如下:
1. 工具/IDE選擇Makefile。
2. 只複製會用到的Library (節省程式碼大小)。
3. 個別產生初始化硬體的.c/.h檔案 (不會整個都擠在`main.c`中)。
4. 其他保持預設


# STM32CubeProgrammer
- 燒入

- 參數調整

# VSCode
## Extensions
- **Cortex-Debug**:可以Debug Arm的CPU
- **C/C++**:讓VSC支援C/C++
- **C/C++ Snippets**:一鍵調整Code內的排版
- **stm32-for-vscode**:整合makefile與flash/debug指令
## c_cpp_properties.json
- defines
- 第一個參數:因為採用HAL函式庫,所以要加入`USE_HAL_DRIVER`
- 第二個參數:要根據MCU型號做出的更改,不然會有一堆惱人的紅線Error
- 相關型號會在專案內的`MakeFile`的`C_DEF`裡面找到
```json
{
"configurations": [
{
"name": "stm32",
"includePath": [
"C:/Compiler/gcc-arm-none-eabi-10.3-2021.10/lib/gcc/arm-none-eabi/10.3.1/include",
"${workspaceFolder}/Inc/",
"${workspaceFolder}/Drivers/STM32L1xx_HAL_Driver/Inc",
"${workspaceFolder}/Drivers/STM32L1xx_HAL_Driver/Inc/Legacy",
"${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32L1xx/Include",
"${workspaceFolder}/Drivers/CMSIS/Include"
],
"defines": [
"USE_HAL_DRIVER",
"STM32L152xE"
],
"compilerPath": "C:\\Compiler\\gcc-arm-none-eabi-10.3-2021.10\\bin\\arm-none-eabi-gcc.exe",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "gcc-arm"
}
],
"version": 4
}
```
# Makefile
## Command
- `make`
- 在專案檔中產生build資料夾,並且編譯程式碼與在build資料夾內產生out文件,其中包含燒錄檔。

- `make clean`
- 刪除build資料夾與裡面所有的資料。
## Add Custom Library
### .c / Source File
```
# C sources
C_SOURCES = \
Src/main.c \
Src/gpio.c \
Src/usart.c \
Src/stm32l1xx_it.c \
Src/stm32l1xx_hal_msp.c \
# 以下略...
```
### .h / Include File
```
# C includes
C_INCLUDES = \
-IInc \
-IDrivers/STM32L1xx_HAL_Driver/Inc \
-IDrivers/STM32L1xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32L1xx/Include \
-IDrivers/CMSIS/Include
```
### Compile Flag
- **ASM:**`ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections`
- **C:**`CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections`