An Introduction to GPIO Programming
By Jeff Tranter Wednesday, June 12, 2019
編者按:本 2019 年博客系列是 ICS 最受歡迎的系列之一,現已更新(2022 年 12 月),以確保內容仍然準確、相關和有用。
本博客是 Integrated Computer Solutions (ICS) 關于 GPIO 編程的新系列博客的開篇。GPIO 即通用輸入/輸出,是大多數現代嵌入式計算機硬件的一項功能,也是許多嵌入式系統的關鍵組件。在本系列中,我將結合理論和實際編程示例,介紹嵌入式編程的這一重要方面。在第一篇文章中,我將概述 GPIO 編程、它的用途以及使用它所需了解的一些基本概念。
在接下來的文章中,我將介紹如何從文件系統或 sysfs 接口、Python(使用各種模塊)以及 C 或 C++ 對 GPIO 進行編程。我將介紹一些低成本商用嵌入式板卡的功能,包括 Raspberry Pi 系列和 Toradex 的板卡。除了基本的數字輸入和輸出功能外,我還將介紹其他協議,如 SPI、I2C、1-Wire 和串行接口。
此外,我還會將一些低級編程與 Qt 聯系起來,并展示一兩個利用 Qt 功能的示例。雖然具體的代碼示例是為 Raspberry Pi 或 Toradex 平臺編寫的,但這些概念也適用于其他硬件,只要這些硬件運行某種形式的 Linux,如 Raspbian、Yocto 或 boot2qt。
Editor's note: This 2019 blog series, among ICS' most popular,??has been updated (December 2022) to ensure the content is still accurate, relevant and useful. This blog launches Integrated Computer Solutions' (ICS) new series on GPIO programming. GPIO, or General-Purpose Input/Output, is a feature of most modern embedded computer hardware and a key component of many embedded systems. In this series, I'll cover this important aspect of embedded programming, with a mix of??theory and practical programming examples. In this first installment I'll present an overview of GPIO programming, what it can be used for and some basic concepts you need to know in order to use it. In upcoming posts I explain how to program GPIO, starting with the file system or sysfs interface, as well as from Python (using various modules) and from C or C++. I'll cover the capabilities of some low-cost commercial embedded boards, including the Raspberry Pi series and boards from Toradex. In addition to basic digital input and output functions, I'll address other protocols, such as SPI, I2C, 1-Wire and serial interfaces. As a bonus, I'll tie some of this low-level programming back to Qt and show an example or two that make use of Qt features. While the specific code examples will be written for the Raspberry Pi or Toradex platforms, the concepts will be applicable to other hardware as long as it is running some form of Linux like Raspbian, Yocto or boot2qt. |
What is GPIO?
GPIO 是集成電路或電路板上的一個信號引腳,可用于執行數字輸入或輸出功能。根據設計,它沒有預定義的用途,硬件或軟件開發人員可以使用它來執行自己選擇的功能。典型的應用包括控制 LED、讀取開關和控制各類傳感器。
例如,大多數型號的 Raspberry Pi 都有一個 40 針 GPIO 連接器,可連接約 25 條 GPIO 線路。
A GPIO is a signal pin on an integrated circuit or board that can be used to perform digital input or output functions. By design it has no predefined purpose and can be used by the hardware or software developer to perform the functions they choose. Typical applications include controlling LEDs, reading switches and controlling various types of sensors. Most models of Raspberry Pi, for example, have a 40-pin GPIO connector that provides access to about 25 GPIO lines. |
GPIO 可由專用集成電路實現,或通常由片上系統 (SoC) 或模塊上系統 (SoM) 設備直接支持。
GPIO 引腳最常見的功能包括:
-
可通過軟件配置為輸入或輸出
-
啟用或禁用
-
設置數字輸出的值
-
讀取數字輸出的值
-
當輸入值發生變化時產生中斷
GPIO 引腳是數字引腳,這意味著它們只支持高/低或開/關電平。它們通常不支持具有許多離散電壓電平的模擬輸入或輸出。某些 GPIO 引腳可直接支持標準化通信協議,如串行通信、SPI、I2C、PCM 和 PWM。(我將在今后的文章中介紹這些內容)。
GPIO may be implemented by dedicated integrated circuits, or more often are directly supported by system on a chip (SoC) or system on a module (SoM) devices. The most common functions of GPIO pins include: * Being configurable in software to be input or output * Being enabled or disabled * Setting the value of a digital output * Reading the value of a digital output * Generating an interrupt when the input changes value GPIO pins are digital, meaning they only support high/low or on/off levels. They generally don't support analog input or output with many discrete voltage levels. Some GPIO pins may directly support standardized communication protocols like serial communication, SPI, I2C, PCM, and PWM. (I'll cover these in future posts). |
Hardware Concepts
如果您想嘗試 GPIO 編程,您需要一些硬件和工具。除了 Raspberry Pi 這樣的電路板外,還需要一些有用的部件和工具:
-
GPIO 分線或原型板
-
無焊面包板和跳線
-
各種 LED、電阻器和開關
-
低成本數字萬用表
這些可從許多供應商(如 AdaFruit)處購買。如果不想組裝自己的硬件,也可以控制各種市售電路板。在 ICS,我們設計了一個小型 GPIO 學習板(如下圖所示),用于我們的嵌入式編程課程。我將在以后的文章中詳細介紹。
If you want to experiment with GPIO programming, you'll need some hardware and tools. In addition to a board like a Raspberry Pi, some useful parts and tools to have include:
These are available from many vendors such as AdaFruit. There are also various commercially available boards that you can control if you don't want to assemble your own hardware. At ICS, we designed a small GPIO Learning Board (shown below) that we use for our embedded programming classes. I'll describe that in more detail in later posts. |
A Word on Safety /?安全問題
如果你是硬件方面的新手,就必須了解一些基本的安全知識。由于 Raspberry Pi 和類似電路板上的電壓很低(5 伏或更低),因此只要避免使用任何電源線電壓的電路,觸電的風險就很小。
如果你要進行任何焊接或剪切導線引線,都應小心謹慎并戴上護目鏡。
最有可能發生的事故是不慎損壞 Raspberry Pi 或其他設備。將 GPIO 或其他引腳連接到錯誤的電壓上很容易損壞電路板,使其無法使用。靜電,尤其是在干燥的冬季,也會產生高電壓,如果你產生靜電并觸摸電路板,就會損壞硬件。建議在工作區使用接地的防靜電墊和/或腕帶。
此外,請注意包括 Raspberry Pi 在內的許多設備使用 3.3 伏邏輯電平,如果連接到 5 伏電壓(Arduino 等許多其他設備使用 5 伏電壓,GPIO 連接器上也有 5 伏電壓),可能會損壞設備。
If you are new to hardware, a few words on basic safety are in order. Since the voltages on a Raspberry Pi and similar boards are low (5 volts or less), the risk of electric shock is minimal as long as you avoid any circuity that operates on power line voltages. You should use caution and wear eye protection if you do any soldering or clip wire leads. The most likely accident is to inadvertently damage your Raspberry Pi or other device. Connecting a GPIO or other pin to the wrong voltage can easily damage the board and render it unusable. Static electricity, especially during the dry winter months, can also generate high voltages that can damage hardware if you develop a static charge and then touch the board. A grounded anti-static mat and/or wrist strap are recommended for your work area. Also be aware that many devices, including the Raspberry Pi, use 3.3 volt logic levels and can be damaged if connected to 5 volts, which is used by many other devices like Arduino and is present on the GPIO connector. |
One Caveat /?一個注意事項
作為一個經常使用 Qt 框架開發圖形用戶界面應用程序的人,我想提最后一點注意事項。一般來說,嵌入式應用程序不會在運行圖形用戶界面的 CPU 上進行大量 GPIO 編程。相反,您通常希望使用另一個處理器或微控制器,尤其是在控制硬件有任何實時要求的情況下。
以下是一些例外情況,您可能需要在與圖形用戶界面相同的 CPU 上進行 GPIO 編程:
1,低速或很少使用的功能,如電源開關感應。
2,使用實時操作系統 (RTOS),可以從進程或線程運行代碼,并保證響應時間和延遲時間的設計。
3,使用 Raspberry Pi 等現成的電路板開發初始原型或概念驗證,這種電路板并非用于生產。
4,對性能要求不高的教育或自學應用。
As someone who often uses the Qt framework for developing applications with graphical user interfaces, I'd like to mention one final caveat. Generally speaking, embedded applications will not do any significant amount of GPIO programming on the same CPU as the one that runs the GUI. Instead, you'll typically want to use another processor or microcontroller, particularly if there are any real-time requirements for controlling the hardware. Some exceptions to this rule where you might want to do GPIO programming on the same CPU as the GUI can include: 1. Low-speed or rarely used functions, such as power on/off switch sensing. 2. A design where you're using a real-time operating system (RTOS) and can run code from a process or thread with guaranteed response and latency times. 3. Developing an initial prototype or proof of concept with an off-the-shelf board like the Raspberry Pi that is not meant for production use. 4. Educational or self-learning applications where performance is not a factor. |
隨著本系列文章的展開,我們還將推出更多有關 GPIO 編程的內容。希望您能繼續關注,嘗試并修改其中的一些程序示例。
關于作者
杰夫-特蘭特
Jeff Tranter 是 ICS 的 Qt 咨詢經理。他負責監督客戶軟件系統的架構和高層設計。在杰夫的領導下,組織全球團隊開發桌面和嵌入式應用程序。他曾在《電子設計》雜志上發表過文章。 (Home | Electronic Design)
Look for more on GPIO programming as this series unfolds. I hope you follow along, try out and modify some of the program examples. About the author Jeff Tranter Jeff Tranter is a Qt Consulting Manager at ICS. He oversees the architectural and high-level design of software systems for clients. Jeff’s leadership organizes global teams of developers on desktop and embedded applications. He has been published in Electronic Design magazine. |
參考:
An Introduction to GPIO Programming | ICS