DEV Community

Cover image for GPIO- General-Purpose Input/Output
QuecPython
QuecPython

Posted on

GPIO- General-Purpose Input/Output

Basic Knowledge

It will benefit us a lot by learning about basic knowledge on GPIO.

Framework

The GPIO (Abbreviation of General-purpose input/output), a kind of input/output interface for general data, communicates with external electrical components or devices, which can be deployed in micro-processor, micro-controller, and other embedded systems.

See basic diagram as below:

I/O pin: The actual HW interface in a microprocessor or microcontroller that connects to an external device. Each GPIO pin can be programmed in input or output mode.

Protection diode: One special diode that protects the I/O pin in a microcontroller or microprocessor from influence caused by voltage kick-out or ESD.Pull-up/down resistor: It sets the status of the GPIO pin in IDLE or in a condition of an invisible input signal.

The Pull-up resistor will connect the GPIO pin to the positive to set it as the default high-level status. While the Pull-down resistor will connect the GPIO pin to the GND to set it as the default low-level status. Both of them will be controlled by a register configured by SW.

P-MOS and N-MOS: The circuit with P-MOS and N-MOS will facilitate GPIO with "Push-pull" and"Open-drain" modes.

TTL Schmitt trigger: Process noise or unstable input data. It will eliminate the noise of the data signal based on the highest threshold and the lowest threshold to provide a more accurate digital signal.

Output driver: Control the voltage and current of the GPIO pin in output mode.

Input receiver: Process input signal from the GPIO pin. In most cases, one TTL Schmitt trigger will be embedded to eliminate the noise of the digital signal.

Working mode

Floating input

Called as Hi-Z, it means the GPIO pin will be floating without being pulled to any logic level. In this status, the GPIO pin can pick up voltage noise easily and read random value. Therefore, please avoid it unless given reason.

The contents highlighted by a yellow background illustrate the data transmission channel. The external level signal will enter the internal chip via the I/O with number ①; After that, it will be reformed bySchmitt trigger with number ②, the signal will arrive at the "Input data register" with number ③, and ends at the other terminal (Number ④) of the "Input data register". Thus, it is available to read the level change of I/O via the internal data bus as you wish.

Pull-up input

The pull-up resistor is linked between GPIO pin and VDD. Therefore, under the circumstance that the GPIO pin is not driven by the device actively, it will be pulled up to logic high level. In the condition that the default status of the GPIO pin is high level, please configure it as pull-up mode.

Compared with the aforementioned floating input, one pull-up resistor ranged 30KΩ~50KΩ isconnected in data channel. In the condition that there is no signal input in I/O, the level in the input terminal can be maintained high (The VDD goes through a pull-up resistor). In addition, when the level input in I/O is low, the level in input terminal will be low as well.

Pull-down input

The pull-down resistor is linked between GPIO pin and VSS. Therefore, under the circumstances that the GPIO pin is not driven by device actively, it will be pulled down to logic low level. In the condition that the default status of GPIO pin is low level, please configure it as pull-down mode.

In terms of input pull-down mode, one pull-down resistor ranged 30KΩ~50KΩ is linked in the data channel.

Analog input

There is neither pull-up resistor nor pull-down resistor and the signal will enter analog input channel without going through Schmitt trigger. Normally, it will be used in ADC data sampling.

Open-Drain output

In Open-Drain output mode, the GPIO pin will pull down voltage actively via connecting to GND instead of pulling up. If it is needed to pull up voltage, one extra pull-up resistor shall be connected to the power supply. It will be applied in I2C communication normally, among which, multiple devices can share one data cable synchronously.

In condition that the CPU writes into data via "Bit set/reset registers" or "Output data register" with the levelnumber ①, this data bit will be transferred to I/O with number ④ via the "output control circuit“with number ②. If the logic "1" is written by CPU, the N-MOS with number ③ will be off and thelevel in I/O is determined by external pull-up resistor. While the logic "0" is written, the N-MOS withnumber ③ will be on and the level in I/O will be pulled down to zero correspondingly.

As above figure shows, the Schmitt trigger is switched on, which means the CPU can monitor theI/O on other side of "Input data register". Based on this characteristic, it is also available toimplement the virtual bilateral communication in I/O. Assuming that the CPU outputs logic "1", thelevel in I/O will be determined by external circuit completely since the N-MOS with Number ③ is off. Therefore, the CPU is capable to read the external circuit signal in "Input data register" instead of the logic "1" outputted by itself.

Push-pull output

In Push-Pull output mode, the GPIO pin will drive high level or low level actively. That means the GPIO can not only push the voltage to high level but also pull voltage to low level. This mode can provide a powerful driver, which is suitable for load such as LED and motor.

The most prominent characteristic of push-pull output lies in that it can output high leveland low level truly. Moreover, it is equipped with driver in both levels.

Interrupt Framework

Interrupt Mask Register: This register will mask or disable dedicated interrupt for sake of preventing it from triggering interrupt service routine (Abbreviated as ISR hereinafter). When the certain bit is set as 1, the corresponding interrupt will be masked.

Pending Request Register: This register will store the pending interrupt request. Once the valuein corresponding bit is 1, which means the relevant interrupt is pending.

Software Interrupt Event Register: This register allows software to generate interrupt. The interrupt service routine (ISR) related to dedicated interrupt will be triggered by writing into this register.

Rising Trigger Selection Register: This register will trigger interrupt in rising edge of signal.

Falling Trigger Selection Register: This register will trigger interrupt in falling edge of signal.

Edge Detector Circuit: This is HW circuit, which will trigger interrupt by detecting the change inrising edge or falling edge.

Timing Mask Register: This register will mask the interrupt generated in specific time. Normally, itwill eliminate the interrupt triggered in a error way.

Pulse Generator: It will generate a series of pulse in HW, which will trigger timing or synchronized events, including interrupt.

NVIC (Nested Vectored Interrupt Controller): As a part of ARM Cortex-M, it will manage interrupts, especially the nested and interrupt vector, that is the pointer points at interrupt service routine.

Function Multiplexing

In embedded systems, including plenty of micro-controllers and micro-processors, some dedicated physical pins can be configured as different functions, that is called Pin Multiplexing.

Then, how to understand it? The key point is to learn that some pins can be converted among different HW components, resulting in huger flexibility in stage of HW design. The purpose of pin multiplexing is to reduce space and cost since the quantity of physical pins will be decreased.

The physical pins of an embedded chip may be designed to switch among several functions:

  • General data Input/output as GPIO
  • As a part of SPI interface, it will communicate with other SPI devices
  • As a part of UART interface, it will used in serial communication
  • Or it is endowed with other specialized functions such as ADC input or PWM output.

In stage of embedded system design, it is possible to configure these pins according to HW and SW demands. In addition, it is also allowed by some embedded platforms to convert these pins dynamically when operating, providing much flexibility.

Absolutely, each coin has two sides, the pin multiplexing also owns limitation. I.e. one pin is not allowed to execute two or dmore functions at the same time. Therefore, please take it into consideration during the period of system design. Moreover, the pin multiplexing management will induce the system complexity and more SWs are demanded to configure and use these pins properly.

Debounce

Button debounce is common issue when processing mechanical button input in the corresponding system. Due to its characteristic, the touch point will be opened or closed for several times in a rapid way when pressing or releasing button, resulting in multiple level change in ultra-short time,that is called "bounce". The system may mistake the button for being pressed or released more than once if the bounce is not eliminated. Therefore, please take measures to eliminate or reduce it at least.

Normally, it is divided into two classes- HW debounce and SW debounce.

HW debounce

The purpose of HW debounce is to process the bounce caused by mechanical button and acquire one stable and undisturbed level signal. See following common methods.

RC filter:

Component:

  • Resistor(R): Connect to one end of button
  • Capacitor(C): Connect with the other end of button to GND

Characteristic:

- LPF (low-pass filter): Due to its structure, the RC filter will allow the low frequency signal topass and filter high frequency signal

Operation:

  • When the button is pressed or released, the capacitor will start charging or discharging. Therefore, for rapid voltage changes (such as those due to contact bounce), the RC filter smooths these changes, thereby preventing misjudgments.
  • The strength of this smoothing effect is determined by the RC time constant (τ = R×C). This time constant represents the time it takes the capacitor to charge to 63.2% of its initial voltage.

Common parameters:

  • Resistor(R): 1kΩ ~10kΩ
  • Capacitor(C): 0.1µF~10µF

Advantage:

  1. Simplicity: Only two basic components are needed
  2. Economy: Both resistor and capacitor are low-cost
  3. Effectiveness: In terms of majority of common applications, the debounce capability is sufficient.

Disadvantage:

  • Delay: The ultra huge RC time constant will cause unnecessary delay in button response.
  • Environment sensitivity: The variation in temperature and humidity may influence the capacitor value and debounce effect further.
  • Not all applications can be applied: For quick-response application, the RC filter will not be the optimum selection.

If the RC filter is taken in HW debounce solution, the suitable resistor and capacitor values are important since it will filter bounce effectively without inducing excessive response delay at the same time. Therefore, in design stage, it is necessary to test different combinations of R and C to achieve the optimum debounce effect.

Schmitt Trigger:

Component:

  • Schmitt trigger: In essential, it is a power comparator with dual thresholds. Due to its characteristic, it can not only be one independent component but also a part of a microcontroller.

Characteristic:

  • Dual threshold: The main characteristic of Schmitt trigger lies in that it owns two thresholds, Vt+ & Vt-.

Operation:

  • Once the input voltage rises and surpasses the Vt+, the output will switch to high status.
  • Once the input falls and is lower than Vt-, the output will switch to low status.
  • This characteristic can guarantee that the output status will not change if any minor change in input voltage so as to filter bounce effectively.Common parameter Vt+ & Vt-: The gap between two thresholds is called as hysteresis or dead zone.

Common parameter:

  • Vt+ & Vt-: The gap between two thresholds is called as hysteresis or dead zone.

Advantage:

  1. Perfect debounce: Owing to dual threshold characteristic. the Schmitt trigger is endowedwith powerful tolerance in input noise and bounce.
  2. Explicit output: The output will be either low or high level, which will not fluctuate betweenthem.
  3. Widespread application: The input of GPIO in multiple micro-controllers are embedded withoptional Schmitt trigger.

Disadvantage:

  1. Possible delay: In some cases, if the change in input signal is slow, the trigger will cause a minor delay.
  2. Inadequate fast signal: In terms of signal with high frequency, the Schmitt trigger will not be the optimum since it will lead to signal distortion
  3. Cost: Although many micro-controllers are embedded with Schmitt trigger. If the external trigger is needed, the cost will increase as well.

_As for selected debounce method and component value, please consider the bounce frequency and duration.

If RC filter is deployed, please confirm the RC time constant can eliminate bounce without unduly delaying the button response.

If it works in environment with noise, please consider other elements such as external EMI (Electromagnetic Interference)

The test plays a vital role. After the design is complete, the system should be tested enough to ensure that the debounce circuit will work properly under all expected conditions._

Shunt capacitor

Component:

  • Capacitor (C): Connect with key directly. Normally, it will connect with one terminal of button and the GND.

Characteristic:

  • Delayed charging/discharging: Owing to its characteristic, the capacitor should take time in charging and discharging

Operation:

  • Under the circumstance that the button is not pressed yet, the capacitor will in discharging status.
  • Once the button is pressed, it will spend certain time in charging till the capacitor is full, which will do a favor in filtering the short bounce that may occur.
  • Similarly, when the button is released, the capacitor will discharge in due time so as to eliminate possible bounce.

Common parameters:

  • Capacitor (C): 1nF ~10µF. The specific value is determined by dedicated demand and the frequency of key bounce.

Advantage:

  1. - Simplicity: Only one capacitor is needed and the connection is simple as well
  2. - Low cost: The capacitor is cheap and easy to acquire.
  3. - Apparent effect: It will debounce heavily in terms of most of common button bounces.

Disadvantage:

  1. Possible delay: Ultra-huge capacitor will lead to obvious delay in key response.
  2. Environment sensitivity: The variation in temperature and humidity may influence the capacitor value and debounce effect further.
  3. Power noise: If the power noise is generated by other components in system, this shunt capacitor may magnify the noise.
  4. Not all applications can be applied: For quick-response application, this method will not be the optimum.

RS trigger:

Component:

  • RS trigger: Normally, the basic bistable circuit is made by two NAND gates or NOR gates.
  • Two Inputs: R (Reset) & S (Set)。
  • Two outputs: Q & ~Q (Inversion of Q)。

Characteristic:

  • Bistable: The RS trigger always owns two stable output statuses

Operation:

  • In condition that the input in S is 1 and the input of R is 0. the output of Q will be 1 while the Q will be 0.
  • In condition that the input in R is 1 and the input of S is 0. the output of Q will be 0 while the Q will be 1.
  • Under the circumstance that both inputs are 0, the RS trigger will maintain current status
  • Under the circumstance that both inputs are 1, this status is not allowed and uncertain output will be triggered probably.

In order to eliminate button bounce, the RS trigger can be configured as the situation that only when the button is truly pressed or released can the status be changed. Any momentary bounce related to button will not change the output of RS trigger, leading to effective debounce.

Common parameter:

  • Input logic level
  • For RS trigger of NAND, the common logic level is 1
  • For RS trigger of NOR, the common logic level is 0

Advantage:

  1. High effective debounce: The RS trigger provides stable output for the button no matter how noisy the input.
  2. Explicit output status: There will be high or low output in any given time.
  3. Low cost: The RS trigger can be built by basic logic gate.

Disadvantage:

  1. Complexity: Compared with simple shunt capacitor or Schmitt trigger, more components and more complex traces are needed.
  2. Impermissible status: There exists one impermissible status in RS trigger, please avoid in stage of design.
  3. Power supply demand: Appropriate power shall be provided for RS trigger

Proprietary debouncer

_There are some proprietary debouncers such as MAX6816, MAX6817 and MAX6818 in market. The filter and Schmitt trigger involved in these ICs can be valid to connect to button and provide stable data output.

As for selected debounce method and component value, please consider the bounce frequency and duration.

If RC filter is deployed, please confirm the RC time constant can eliminate bounce without unduly delaying the key response.

If it works in environment with noise, please consider other elements such as external EMI (Electromagnetic Interference)

The test plays a vital role. After the design is complete, the system should be tested enough to ensure that the debounce circuit will work properly under all expected conditions._

SW debounce

This method will eliminate button bounce via algorithm and logic in SW. Compared with former method, it provides more flexibility in terms of different applications and conditions as well as burden increased.

See common embedded SW debounce methods:

### Delay
Operation:

Once the button status changes (i.e., changes from never-pressed status to pressed status),the program will wait one predetermined delay time instead of responding this change immediately. After this delay is over, the program will detect button status again. If the button status maintains, this operation will be considered as valid and the program will respond it. Otherwise, it will be taken as error or bounce, resulting in no response

Advantage:

1.Simple implementation: The delay will be simplest and targeted SW debounce method by adding one delay function after the button status is changed.
2.No extra resource is needed: Compared with other methods such as counter or FSM, neither extra storage nor counting resource are needed in this method.

Disadvantage:

  1. Response delay: Since it is needed to wait predetermined delay time to confirm the pressed action, the response delay will be caused.
  2. CPU time consumption: If the busy-waiting is used to implement delay, it will be a failure for CPU to execute any task in this period, which is unacceptable in embedded system with restricted resources.
  3. Unreliability: If the bounce lasts longer than the predetermined delay time, this method may still produce an error. In addition, if the delay time is set too long, you may miss the rapid continuous button action

Counter

Operation:

When it detects the button status change, the system will not confirm immediately. Instead, one counter will be initiated. In following consecutive tests, if the button status keeps changing, the value in counter will increase corresponding. While the button returns to original status, the counter will be reset instead. Only under the circumstance that the value in counter reaches the pre-determined threshold can the button status change be considered as the true press and responded by system. Thus, the momentary bounce will not reach the threshold of counter and it can't be mistaken for a valid button action.

Advantage:

  1. Perfect reliability: Compared with simple delay debounce, it will filter the momentary bounce more reliably.
  2. Perfect response: If the button is pressed consecutively, the system will confirm this action in a relatively short time to provide faster response.
  3. Flexibility: It is available to adjust counter threshold via actual need to adapt to different button and bounce characteristic.

Disadvantage:

  1. Resource consumption: It is needed to arrange one counter for each button or one global counter for system, which may consumer extra storage and counting resource.
  2. Complexity: Compared with simple delay debounce, the debounce by counter will be more complex actually, especially in scenario that needs multiple keys.
  3. Possible delay: If the predetermined threshold in counter is unduly high, it will cause delayed response in system and bad user feeling.

Top comments (0)