---
# System prepended metadata

title: STM32-10/14
tags: [電系組]

---

# STM32
author: 陳煥宇
date: 2025/10/14

### Reference
* [STM32基礎入門教學2021iThome (Day1~Day9)](https://ithelp.ithome.com.tw/users/20141525/ironman/4839)
* [UM2505 STM32G4 Nucleo-64 boards (MB1367)](https://www.st.com/resource/en/user_manual/um2505-stm32g4-nucleo64-boards-mb1367-stmicroelectronics.pdf)
* [Schematic pack of NUCLEO64 STM32G4](https://www.st.com/resource/en/schematic_pack/mb1367-g474re-c04_schematic.pdf)

### Setup
* IDE: STM32CubeIDE1.19.0
* Board: NUCLEO-G474RE

### About STM32
* 類似Arduino(程式大多已包裝好，能修改幅度少)，但更自由
* About Memory
    * 32-bit data bus (1 word)
    * 32-bit address bus ($2^{32}$=4GB)
    * Ox0000 0000 ~ 0xFFFF FFFF
        * Flash: 512KB
        * SRAM: 128kB

### About IDE
* Perspective
    * 指 layout
* XX.ioc
    * 圖形化介面，源自STM32Cube MX的功能。
    * 選擇 Board、PIN腳和對應操作，存檔之後自動生成初始化程式
* main.c
    * 程式寫在 `/* USER CODE BEGIN XXXX */` 跟 `/* USER CODE END XXXX */` 中間，否則會被自動生成的程式覆寫掉
    * 接線接板子，按 debug XX debug，開始Compile、Build、Program、Debug，進入Debug Perspective，按 Resume。
        * live expression: can monitor global variables/pointers
        * Mermory: can modify value of variables.

### GIPO OUTPUT
* Write: 
    * `HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_PIN, GPIO_PinState PinState);`
    * PA5 High `HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);`
    * PB0 LOW `HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);`
* Toggle:
    * `HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);`
    * PA5 toggle `HAL_GPIO_TogglePIN(GPIOA, GPIO_PIN_5);`
* Delay:
    * `HAL_Delay();`
    * Delay 1s `HAL_Delay(1000);`
* Why Choose PA5
    * You can easily test PA5 output by observing the USER LED, since they are directly connected.
    ![image](https://hackmd.io/_uploads/rygRULi6lx.png)
    ![image](https://hackmd.io/_uploads/rJlZuUspeg.png)

### OUTPUT mode
![image](https://hackmd.io/_uploads/Hkm998ipxg.png)
* Push-Pull output
    * High -> 3.3V
    * Low -> 0V
* Open-Drain output
    * High -> Depends on external voltage
    * Low -> 0V
* So if you want 5V, use Open-Drain output.

### Debug Log
* "Expression" is not "Live Expression"
* "Live Expression" can only found under Debug Perspective
* "Debug XX Debug" button runs only 1 line, click "Resume" to proceed.

### No AI here - It would make me sad if you thought so.
