--- 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. GPIO pins have no special purpose defined, and go unused by default. The idea is that sometimes the system integrator building a full system that uses the chip might find it useful to have a handful of additional digital control lines, and having these available from the chip can save the hassle of having to arrange additional circuitry to provide them. For example, the Realtek ALC260 chips (audio codec) have 8 GPIO pins, which go unused by default. Some system integrators (Acer Inc. laptops) employing the ALC260 use the first GPIO (GPIO0) to turn on the amplifier used for the laptop's internal speakers and external headphone jack. In STM32F4-Discovery, each general-purpose I/O port has four 32-bit configuration registers (GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR and GPIOx_PUPDR), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), a 32-bit set/reset register (GPIOx_BSRR), a 32-bit locking register (GPIOx_LCKR) and two 32-bit alternate function selection register (GPIOx_AFRH and GPIOx_AFRL). Main Feature ============ - Up to 16 I/Os under control - Output states: push-pull or open drain + pull-up/down - Output data from output data register (GPIOx_ODR) or peripheral (alternate function output) - Speed selection for each I/O - Input states: floating, pull-up/down, analog - Input data to input data register (GPIOx_IDR) or peripheral (alternate function input) - Bit set and reset register (GPIOx_BSRR) for bitwise write access to GPIOx_ODR - Locking mechanism (GPIOx_LCKR) provided to freeze the I/O configuration - Analog function - Alternate function input/output selection registers (at most 16 AFs per I/O) - Fast toggle capable of changing every two clock cycles - Highly flexible pin multiplexing allows the use of I/O pins as GPIOs or as one of several peripheral functions Functional Description ====================== Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in several modes: - Input floating - Input pull-up - Input-pull-down - Analog - Output open-drain with pull-up or pull-down capability - Output push-pull with pull-up or pull-down capability - Alternate function push-pull with pull-up or pull-down capability - Alternate function open-drain with pull-up or pull-down capability Each I/O port bit is freely programmable, however the I/O port registers have to be accessed as 32-bit words, half-words or bytes. The purpose of the GPIOx_BSRR register is to allow atomic read/modify accesses to any of the GPIO registers. In this way, there is no risk of an IRQ occurring between the read and the modify access. Figure below shows the basic structure of a 5 V tolerant I/O port bit: .. image:: /GPIO_basic_src.PNG