---
# System prepended metadata

title: GPIO

---

# GPIO
Reference:[成大Wiki](https://wiki.csie.ncku.edu.tw/embedded/GPIO), [STM32G0x0](https://www.st.com/en/microcontrollers-microprocessors/stm32g030c8.html), [open drain push pull 上拉电阻理解](https://blog.csdn.net/chenpuo/article/details/105876739), [Open Drain Output vs. Push-Pull Output](https://open4tech.com/open-drain-output-vs-push-pull-output/)
## Introduction
由於開發MCU時會大量接觸GPIO的設定，對於硬體電路不熟的我有時就像霧裡看花。於是透過本篇筆記紀錄GPIO硬體相關特性及學習過程，方便複習。
## GPIO functional description
下圖是一個 I/O port 的電路圖，可搭配了解電路特性
![image](https://hackmd.io/_uploads/r1m1xtlu1g.png)

首先，可以先了解控制 GPIO 的 register 
:::info
:book: **GPIO 各個 register 功能**
1. GPIO port mode register (GPIOx_MODER):控制每個 GPIO 引腳的工作模式。
2. GPIO port output type register (GPIOx_OTYPER):設置輸出引腳的類型。
3. GPIO port output speed register (GPIOx_OSPEEDR):控制輸出信號的切換速度（影響上升沿和下降沿的時間）。
4. GPIO port pull-up/pull-down register (GPIOx_PUPDR):控制引腳是否啟用內部的上拉電阻或下拉電阻。
5. GPIO port input data register (GPIOx_IDR):用於讀取 GPIO 引腳的當前狀態。
6. GPIO port output data register (GPIOx_ODR):用於設置輸出引腳的狀態。
7. GPIO port bit set/reset register (GPIOx_BSRR):用於設置或清除 GPIO 引腳的輸出狀態（位操作）。
8. GPIO port configuration lock register (GPIOx_LCKR):鎖定 GPIO 的配置，防止意外修改。 
:::
由`GPIOx_MODER`可知，一個pin通常可被設定成input、output、alternate function或analog，其中input及output又細分多種模式:
### Input
When the I/O port is programmed as input:
* The output buffer is disabled.
* The [<font color="#f00">^**^Schmitt trigger</font>](#note-1) input is activate.
* The pull-up and pull-down resistors are activated depending on the value in the GPIOx_PUPDR register.
* The data present on the I/O pin are sampled into the input data register every AHB clock cycle.
* A read access to the input data register provides the I/O state.


![image](https://hackmd.io/_uploads/HkXFdFg_1e.png)

#### Mode
1. floating
當input pin被處在高阻抗的模式下，若沒有外部訊號源進來的話，此時是無法確定pin的狀態(不能確定現在處在高電位或低電位)，除非有外部訊號來驅動電路。換句話說，input floating，這個input電位狀態完全是由外部訊號來決定，沒有訊號驅動的話，就會呈現高阻抗狀態。
2. pull-up/pull-down
如果我們需要這個pin有一個明確的預設狀態時，必須借助pull-up(pull-down)resistor來做調整，在pull-up resistor(pull-up外接高電壓，pull-down通常會接地)加入之下，讓pin的維持在明確的高電壓狀態(pull-down則是讓pin維持在低電壓狀態)。
3. <font color="#f00">repeater</font>
***這個模式較少使用***，重複器主要的作用是 「維持 GPIO 先前的狀態」，防止訊號在沒有驅動時變成不穩定的狀態。
    * 當 GPIO 為 High（1）時，Repeater 會保持 High
    * 當 GPIO 為 Low（0）時，Repeater 會保持 Low
    * 如果沒有輸入（High-Z 狀態），GPIO 會保持最後的狀態，而不是變成浮動（Floating）
    

### Output
When the I/O port is programmed as output:
* The output buffer is enabled:
    * Open drain mode: A “0” in the Output register activates the N-MOS whereas a “1” in the Output register leaves the port in Hi-Z (the P-MOS is never activated).
    * Push-pull mode: A “0” in the Output register activates the N-MOS whereas a “1” in the Output register activates the P-MOS.
* The Schmitt trigger input is activated.
* The pull-up and pull-down resistors are activated depending on the value in the GPIOx_PUPDR register.
* The data present on the I/O pin are sampled into the input data register every AHB clock cycle.
* A read access to the input data register gets the I/O state.
* A read access to the output data register gets the last written value.

![image](https://hackmd.io/_uploads/BJn_oix_yl.png)

#### Mode
1. open-drain (with pull-up/pull-down)
此電路看到名子就可以理解他是開路的形式，所以選擇此電路時，輸出部分必須加上上拉電阻，透過 N-MOS 當開關，來決定輸出為 On/off，而輸出電壓則為外部決定。
![image](https://hackmd.io/_uploads/SkOtkJ-O1l.png)

2. push-pull (with pull-up/pull-down)
此時的電路有 P-MOS 與 N-MOS。當輸出設定為低電位， P-MOS 導通， N-MOS 關閉，如下圖路徑一；反之，當輸出為高電位時， P-MOS 關閉，而 N-MOS 導通。因此這種方法不需要額外加上拉電阻。
![image](https://hackmd.io/_uploads/H16LJJb_Jl.png)

### Alternate function
When the I/O port is programmed as alternate function:
* The output buffer can be configured in open-drain or push-pull mode.
* The output buffer is driven by the signals coming from the peripheral (transmitter enable and data).
* The Schmitt trigger input is activated.
* The weak pull-up and pull-down resistors are activated or not depending on the value in the GPIOx_PUPDR register.
* The data present on the I/O pin are sampled into the input data register every AHB clock cycle.
* A read access to the input data register gets the I/O state.

![image](https://hackmd.io/_uploads/ByRCojeOyx.png)

### Analog
When the I/O port is programmed as analog configuration:
* The output buffer is disabled.
* The Schmitt trigger input is deactivated, providing zero consumption for every analog.
value of the I/O pin. The output of the Schmitt trigger is forced to a constant value (0).
* The weak pull-up and pull-down resistors are disabled by hardware.
* Read access to the input data register gets the value “0”.
* ![image](https://hackmd.io/_uploads/BkUG_Tld1e.png)

## 電路元件
- [ ]  <a id="note-1">Schmitt trigger</a>
包含正回饋的比較器電路，將類比訊號的波形整成數位電路所能處理的方波波形，以利被input register讀取(處理完只能分辨出高低電位的差別)。

![image](https://hackmd.io/_uploads/Bk83JAl_1l.png)

![image](https://hackmd.io/_uploads/ryndy0lO1x.png)
