嵌入式 - GPIO编程简介

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:

  • GPIO breakout cable or prototyping board

  • Solderless breadboard and jumper wires

  • Variety of LEDs, resistors, and switches

  • Low-cost digital multimeter

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

  • 10
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值