分享到plurk 分享到twitter 分享到facebook

版本 cb5144cdc28d11fa942e18bcb681b6d4f113c5bb

embedded/GPIO

Changes from cb5144cdc28d11fa942e18bcb681b6d4f113c5bb to 87a904549d015c933c37d443b58f02dcc8045d6d

---
title: General-purpose Input/Output (GPIO)
categories: GPIO, STM32F4
...

Introduction
============
**General Purpose Input/Output (GPIO)** is a generic pin on a chip whose behavior (including whether it is an input or output pin) can be controlled (programmed) by the user at run time.

通常我們program使用的data都是放在memory比較多,而GPIO也提供類似的操作方法給programmer(讓我們去更改記憶體內容就可以去控制pin,並影響周遭設備的運行),但真正的設備的位置並非真正落在記憶體上(如:LED、Button、...等),故GPIO的核心是記憶體操作與設備之間的一些電路特性。

Main Feature
============
GPIO雖然建立起記憶體與設備之間的橋梁,但也並非我們就可以隨意使用,我們必須要經過設定之後才能讓我們想要的設備正常工作。

一個pin一次只能被設定成是input或output,而input會有三種狀態表現(floating, pull-up/down, analog),而output只有兩種狀態表現(push-pull or open drain + pull-up/down)。

- input/output方向解說 : input是指記憶體方接收來自設備的訊號源,output是指記憶體傳送訊號給設備。

當pin被設定成input時,非analog的設定下,我們可以利用GPIO的input data register(GPIOx_IDR) 或是memory中提供給目標設備的data register (當設成alternate function的時候)去接收data。

當pin被設定成output時,非analog的設定下,GPIO本身有提供output data register (GPIOx_ODR)來對目標設備做控制,但要是pin不是使用原本預先定義好的功能時(非預先定義的功能都算是alternate function的類別),此時要用memory中,另外規劃給目標設備用的register。

如果pin被設成analog的話,無論input or output都會由adc那邊做處理。

Functional Description
======================
input
-------------------

- floating vs. pull-up/pull-down

當input pin被處在高阻抗的模式下,若沒有外部訊號源進來的話,此時是無法確定pin的狀態(不能確定現在處在高電位或低電位),除非有外部訊號來驅動電路。換句話說,input floating,這個input電位狀態完全是由外部訊號來決定,沒有訊號驅動的話,就會呈現高阻抗狀態。

剛剛提到floating在沒有外部訊號驅動的情況下是呈現高阻抗狀態(無法確定電位狀態=>不能明確表示現在值是0或1),如果我們需要這個pin有一個明確的預設狀態時,必須借助pull-up(pull-down)resistor來做調整,在pull-up resistor(pull-up外接高電壓,pull-down通常會接地)加入之下,讓pin的維持在明確的高電壓狀態(pull-down則是讓pin維持在低電壓狀態)。舉例來說,如果我們定電壓在3-4 V之間是1的狀態,0-1之間是0的狀態,高阻抗的時候,電壓是不明確的,有可能電壓值會落在1-3之間的不明確地帶,甚至是沒有在任何一個狀態維持一段時間,此時的狀態是未定的,但如果我們加入pull-up resistor的話,這個pin接受來自pull-up另一端的電壓供應,讓pin至少維持在3v以上時,我們就可以確定在沒有外部訊號驅動時,pin是維持在高電位狀態。

output
-------------------
- push-pull vs. open-drain with pull-up/pull-down


analog
-------------------

前面所述的input/output跟現在要談的類比模式是跟是不一樣的類型,前者的資料型態主要是高低電位的數位型態(0/1的分別),而類比訊號是普遍自然界的訊號型態,故當我們設定成類比輸入的模式時,進來GPIO pin的原始訊號源在還沒經過施密特觸發器(Schmitt trigger)會有另一個線路將訊號做導向(通常是要導到ADC去),另一方面,當我們用了類比輸出模式後,GPIO的內部將會有一條線路接收DAC處理完的類比訊號,在經過此pin傳遞到外部去。

- 施密特觸發器(Schmitt trigger) : 將類比訊號的波形整成數位電路所能處理的方波波形(處理完只能分辨出高低電位的差別)。

.. image:: /embedded/Schmitt_trigger.jpg
(施密特觸發器具有整流效果)

basic of structure (P.186, **Figure 17. Basic structure of a five-volt tolerant I/O port bit** in Reference manual)
-------------------------------------------------------------------------------------------------------------------

.. image:: /embedded/GPIO_basic_src.PNG

Vdd : 一個供電來源,電壓介在1.7 V - 3.6V之間

Vdd_ft : 5-volt tolerant用的供電來源。

Configuration
=============
Input configuration (P.193, **7.3.9 Input configuration** in Reference manual)
------------------------------------------------------------------------------
When the I/O port is programmed as Input:

- the output buffer is disabled
- the Schmitt trigger input is activated
  - 這種模式處理的數位訊號只在意高低電位的差別(開關控制)。

- the pull-up and pull-down resistors are activated depending on the value in the GPIOx_PUPDR register 
  - 想要讓pin的state變成一個確定的狀態,可以設定pull-up/pull-down的使用。

- The data present on the I/O pin are sampled into the input data register every AHB1 clock cycle
  - input data的更新主要就是以AHB1本身的更新週期做決定,每一個cycle抵達時,data register就會根據當時Schmitt trigger整流完的狀態做更新。

- A read access to the input data register provides the I/O State
  - 對data register的理解,我覺得用'狀態'比'數值'的敘述來的更好(大部分都是開關,就是外部訊號源是否有狀態改變,如 : button的按下與放開),而此處寫I/O state的意思是,即使我們現在是設成output(如 : LED控制),但我們仍然可用input data register來檢查LED現在的狀態。

Output configuration (P.194, **7.3.10 Output configuration** in Reference manual)
---------------------------------------------------------------------------------
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 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

Alternate function configuration (P.195, **7.3.11 Alternate function configuration** in Reference manual)
---------------------------------------------------------------------------------------------------------
When the I/O port is programmed 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

Supplement:
Remark:

GPIOx_AFRL[31:0] and GPIOx_ARHL[31:0] provide ways to select alternation functions. However, different alternate functions maps to different bits of ports. Table below is part of alternate function mapping, which is mainly about USART2/3.

For more information, please refer to **Table 8. Alternate function mapping** from P.58-62 in STM32F407xx Datasheet.

.. image:: /embedded/af_mapping.png

Analog configuration (P.196, **7.3.12 Analog configuration** in Reference manual)
---------------------------------------------------------------------------------
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
- 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.

Demo
====
工具介紹
-------
.. image:: /embedded/BNC line.JPG

- BNC對鱷魚夾線

.. image:: /embedded/oscilloscope.JPG

- 示波器(oscilloscope) 畫面很差...

示波器驗證
---------
.. image:: /embedded/stable.JPG

- 示波器我們調整的scale是1 volt為單位。
- 我們接線是使用BNC對鱷魚夾線,黑接Groud,紅接要觀察的Pin。此圖為我們還沒做任何操作呈現的狀態。

.. image:: /embedded/button_pressed.JPG

- 以button做為觀察GPIO input的部份
- USER button對應的腳位為PA0
- 按下button馬上放開後會產生一個小方波,由此可知,按下button是產生高電位訊號。(看'按下'多久高電位就維持多久)

.. image:: /embedded/LEDOn_2.8v.JPG

- 用LED4來觀察GPIO output的部份
- LED4對應的腳位是PD12,當LED4亮的時候,我們可以看到波形產生變化(從0v左右的位置升到高電位2.8v左右)

.. image:: /embedded/PD12_PD14CNT.JPG

- 我們在接Pin的過程的時候,發現有些燈會亮起來,圖中紅色鱷魚夾線把PD12跟PD14同時夾起來,LED5在LED4亮起來的時候也會亮,對照PD14的位置是LED5。
- 我們觀察示波器用的Code只有對button跟LED4做設定,我們推測原因是LED4亮起來的時候,PD12處於高電位狀態,而鱷魚夾線連通PD14讓此腳位也處於高電位使LED5也亮起來了。

Supplement
==========
- 一些有預設控制元件的腳位(詳見- STM32F4-Discovery 中文使用手冊P.16)
  - LD3:橙色,連接到STM32F407VGT6的PD13
  - LD4:綠色,連接到STM32F407VGT6的PD12
  - LD5:红色,連接到STM32F407VGT6的PD14
  - LD6:藍色,連接到STM32F407VGT6的PD15
  - B1(USER):連接到STM32F407VGT6的PA0
  - B2(RESET):連接到NRST,用於重置STM32F407VGT6

Reference
=========
- `General Purpose Input/Output - Wikipedia, the free encyclopedia <http://en.wikipedia.org/wiki/General_Purpose_Input/Output>`_
- `STM32F407xx Datasheet<http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/DM00037051.pdf>`_
- `STM32F407xx Reference Manual<http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/REFERENCE_MANUAL/DM00031020.pdf>`_
- `STM32F4-Discovery 中文使用手冊</embedded/STM32-Discovery-Manual-Chinese.pdf>`_
- `稀里糊塗學 STM32 第二講:源源不絕</embedded/learn-stm32-part-2.pdf>`_
- `GPIO PPT</embedded/GPIO_v3.ppt>`_