# GPIO(General Purpose Input/Output)
- a generic pin on an integrated circuit whose behavior, including whether it is an input or output pin, can be controlled by the user at run time.
- GPIO是具彈性且可以軟體控制的一種數位訊號。
- 常見於開發版邊緣,以pin的形式呈現
- Pin是board跟外界的溝通橋樑
- 每個GPIO可以被當成Input/Ouput/analog/alternate function
- alternate function是指其他的功能,如 I2C、SPI、USART、PWM、Clock等,如何控制取決外部設備(peripheral)
## Open-Drain & Push-Pull
- 
1. Open-Drain(漏極開路):Open-Drain的輸出為開路,使用時必須加入上拉電阻,輸出由外部電壓決定。
- 
2. Push-Pull OutPut(推挽輸出):可以推既可以拉,所以不需要外部電壓源,當輸出為高電位時,上方transitor導通而下方的關閉,所以會輸出$V_{ss}$,輸出低電位則兩者相反,故不需加上上拉電阻
| | push-pull | open-drain |
| ---------- | -------- | ---------- |
| high | PMOS on,NMOS off Output:$V_{ss}=3.3V$ | PMOS & NMOS off 外部電壓源決定 |
| low | PMOS off,NMOS on Output:$V_{DD}=0 V$ | PMOS off,NMOS on Output:$V_{DD}=0 V$ |
| Pros | 可以直接輸出 $V_{ss}=3.3V$ | 配合外部電路更加靈活 |
| Cons | 只能直接輸出 $V_{ss}=3.3V$ | 高電平是高阻抗,無法輸出電流 |
- MOSFET 不需要直流閘級來推動電流,大大簡化推拉電路上的設計。
- MOSFET 操作的推拉速度遠高於 BJT 的 B 類的推拉速度(這大概是最重要的一點)。
但這種 MOSFET 是無法進行高功率的應用,而 BJT 的 B 類適合較高輸出功率的應用,這樣的話我們一般 MCU 一般使用都不會直接用 GPIO 來推動高功率的應用,例如較高瓦數的喇叭是推不動的,通常都會再接個功率放大器。
## GPIO特性
- input/output方向 : input是指記憶體方接收來自設備的訊號源,output是指記憶體傳送訊號給設備。
- 一個pin通常可被設定成input、output、alternate function或analog, (alternate function和analog只有在特定腳位)
- input會有兩種狀態(floating, pull-up/down),output也有兩種狀態(push-pull, open drain with pull-up/down resistors)。
## Functional Description
### Input
- **Floating vs. pull-up/pull-down**
- flaotin的時候,$V_{SS}$及$V_{DD}$皆不會導通,當input pin被處在高阻抗模式下,若沒有外部訊號源進入,此時會無法確定pin的state(不確定是在高電平還是低電平)除非有外部訊號來driven circuit,input floating這個input電位狀態完成是由外部訊號來決定,沒有訊號驅動即為高阻抗狀態。
如果需要這個pin有一個明確的預設狀態時,必須使用pull-up(pull-down)resistor來做調整,在pull-up resistor(pull-up外接高電壓,pull-down通常會接地)加入之下,讓pin的維持在明確的高電壓狀態(Pull-down則是讓pin維持在低電壓狀態)。
- pull-up/pull-down電阻在大部分腳位上為40kΩ,在PA10/PB12上為10kΩ
- **施密特觸發器(Schmitt trigger)**
- 將類比訊號的波形整成數位電路所能處理的方波波形,以利被input register讀取(處理完只能分辨出高低電位的差別),具有整流效果。
- 
### Output
- push-pull vs open-drain mode
- output處在push-pull模式下時,當output registers為“0”時輸出低電位,“1”時輸出高電位。而在open-drain模式下,“0”輸出低電位,“1”時為高阻抗(Z)狀態,電位無法確定。此兩種模式下,pin皆可pull-up/pull-down。
- 當有多個輸出pin在互相連通時(例如一個bus),若有兩個輸出呈不同狀態,電路上的電位便無法確定。因此,除了正在輸出的pin以外,其他pin應在高阻抗態以免干擾。以bus的例子來說,若令所有pin皆在open-drain mode pull-up,並使非輸出端pin為“1”(Z),當輸出pin為“1”時,bus為高電位(因為pull-up);當輸出pin為“0”時,bus為低電位。
### Analog
- 前面所述的input/output跟現在要談的類比模式是不一樣的類型,前者的資料型態主要是高低電位的數位型態(0/1的分別),而類比訊號是普遍自然界的訊號型態,故當我們設定成類比輸入的模式時,進來GPIO pin的原始訊號源在還沒經過施密特觸發器(Schmitt trigger)會有另一個線路將訊號做導向(通常是要導到ADC去),另一方面,當我們用了類比輸出模式後,GPIO的內部將會有一條線路接收DAC處理完的類比訊號,在經過此pin傳遞到外部去。
### basic structure
- 
1. $V_{DD}$:晶片內部工作的電壓
2. $V_{DD_{FT}}$:I/O Port的專用電壓,與$V_{DD}$不同。
3. $V_{SS}$:接地點
4. Protection Diode:當有靜電(瞬間電壓波)進入I/O pin時,若其>$V_{DD}$則與上方diode導通由Power吸收,若其<$V_{SS}$則與下方diode導通由GND吸收
- TTL vs. CMOS
- TTL由電阻器及三極體組成,CMOS內部零件多以金屬氧化物半導體組成
- 早期TTL速度較CMOS快,但功耗較高,近年來CMOS在速度已經超越TTL。
- TTL懸空一般會呈現高電位,CMOS則為不定態。
## Configuration
### Input configuration

I/O port is programmed as Input:
- the output buffer is disable
- the schmitt trigger input is activated
- 這種模式處理的數位訊號只在意高低電位的差別(開關控制)。
- the pull-up and pull-down resistor are activated depending on the value in the GPIOx_PUPDR reg
- 想要讓pin的state變成一個確定的狀態,可以設定pull-up/pull-down的使用。
- The data present on the I/O pin are sampled into the input data reg every AHB1 clock cycle
- input data的更新主要就是以AHB1本身的更新週期做決定,每一個cycle抵達時,data register就會根據當時Schmitt trigger整流完的狀態做更新。
- A read access to input data reg provides the I/O state
- 對data register的理解,我覺得用’狀態’比’數值’的敘述來的更好(大部分都是開關,就是外部訊號源是否有狀態改變,如 : button的按下與放開),而此處寫I/O state的意思是,即使我們現在是設成output(如 : LED控制),但我們仍然可用input data register來檢查LED現在的狀態。
### Output configuration

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 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 AHB1 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
- 不一定與當下pin的狀態相同
### Alternate Function configuration
- 
as alternate function:
- The output buffer can be configured as open-drain or push-pull
- The output buffer is driven by the signal 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 AHB1 clock cycle
- A read access to the input data register gets the I/O state
### Analog configuration
- 
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
- Read access to the input data register gets the value “0”
- Note: In the analog configuration, the I/O pins cannot be 5 Volt tolerant. analog operating voltage (VDDA) max at 3.6V
- Note2: Sampling jitter:In analog to digital and digital to analog conversion of signals, the sampling is normally assumed to be periodic with a fixed period—the time between every two samples is the same. If there is jitter present on the clock signal to the analog-to-digital converter or a digital-to-analog converter, the time between samples varies and instantaneous signal error arises.
## Register 暫存器
### Overview
- Mode, Speed, Type, PuPd 對應表

### GPIO port mode register (GPIOx_MODER)

- MODERy[1:0]決定第y pin GPIO使用的configuration,兩個bit為一組
- 00:Input
- 01:Output Mode
- 10:Alternate function mode
- 11:Analog mode
- 當reset後,GPIOA_MODER=0xA800 0000、GPIOB_MODER =0x0000 0280,其餘為0
### GPIO port output type register(GPIOx_OTYPER)

- 決定在output時使用的模式
- 0:Output push-pull
- 1:Output open-drain
- reset 皆為 0x0000 0000
### GPIO port output speed register (GPIOx_OSPEEDR)

- OSPEEDRy[1:0]決定Output的速度。在不同的電容及電壓($V_{DD}$)下,輸出頻率可能不同,以下只列出代表值。
- 00:Low speed(2MHz)
- 01:Medium speed(10MHz)
- 10:Fast speed(50MHz)
- 11:high speed(100MHz)
- reset GPIOB_OSPEEDR=0x0000 00C0,其他皆為0x0000 0000
- 速度越高,雜訊與耗電量越高
- 雜訊來源
- Thermal noise:在電阻性的材料中,由於熱擾動的影響,造成材料內電子或電洞等電荷載體產生隨機的速度擾動。
- Shot noise:散粒雜訊通常存在於真空管或半導體內,這種電子由負極到正極經外部電路的隨機運動,當電壓超過一定的臨界值而電流流出呈現散亂的形狀。
### GPIO port pull-up/pull-down register (GPIOx_PUPDR)

- PUPDRy[1:0]決定pin y是否pull up/pull down
- 00:No pull up,pull down
- 01:Pull-up
- 10:Pull-down
- 11:Reserved
- reset GPIOA_PUPDR=0x6400 0000、GPIOB_PUPDR=0x0000 01000其他為0x0000 0000
### GPIO port input data register (GPIOx_IDR)

- 讀取IDRy為input y的值
- Bits 31:16 :Reserved,must be kept at reset value。
- Bits 15:0 IDRy:Port y input data
- register為read-only & word mode access only
- Reset:0X0000 XXXX(X=undefined)
### GPIO port output data register (GPIOx_ODR)

- 輸出pin output的值
- Bits 31:16 :Reserved,must be kept at reset value
- Bits 15:0 ODRy:Port output data
- Reset: 0x0000 0000
### GPIO port bit set/reset register (GPIOx_BSRR)

- set or reset對應pin output data reg的值
- Bits 31:16 BRy:reset bit y
- Bits 15:0 BSy:set bit y
- 0:no action,1:set/reset
- write-only,讀取只會看到0
- Reset:0x0000 0000
### GPIO port configuration lock register (GPIOx_LCKR)

- 若給bit 16(LCKK)一個正確的write sequence