Function Name | Purpose | When to use |
---|---|---|
GPIO_DeInit(GPIO_TypeDef \*GPIOx) | Resets the configuration of the specified GPIO port (A–E) to its default, power-on state by toggling the peripheral’s reset line. | At the start of a program to ensure the port has no prior configuration OR when you want to completely reconfigure a port from scratch. |
GPIO_AFIODeInit(void) | Resets the Alternate Function registers (used for pin remapping, event output, EXTI config, etc.) for the entire chip. | To discard all pin remapping and alternate function configurations, e.g. before applying new ones. |
GPIO_Init(GPIO_TypeDef \*GPIOx, GPIO_InitTypeDef \*GPIO_InitStruct) | Configures one or more pins on a GPIO port for input, output, alternate function, pull-up, pull-down, speed, etc., according to the passed GPIO_InitTypeDef. | At the start of your program to configure pins for your application (e.g. LEDs, buttons, peripherals) OR any time you need to change a pin’s mode. |
GPIO_StructInit(GPIO_InitTypeDef \*GPIO_InitStruct) | Fills GIO_InitTypeDef struct with default settings (all pins as floating 2MHz input). | Before manual configuration, to ensure all structure fields are known states. Avoids issues from uninitialised structure fields. |
GPIO_ReadInputDataBit(GPIO_TypeDef \*GPIOx, uint16_t GPIO_Pin) | Reads the current logic level (HIGH/LOW ) of a single input pin on the port. | To read the state of a button or digital sensor attached to the MCU. |
GPIO_ReadInputData(GPIO_TypeDef \*GPIOx) | Reads the input values of all 16 pins on a GPIO port at once (returns as a 16-bit value). | To capture the entire state of a port, e.g. for a parallel input device or key matrix. |
GPIO_ReadOutputDataBit(GPIO_TypeDef \*GPIOx, uint16_t GPIO_Pin) | Reads the currently programmed output state (HIGH/LOW ) of a specific pin, regardless of external hardware. | For “read-back” of your own outputs, e.g. to confirm a pin is programmed HIGH or LOW . |
GPIO_ReadOutputData(GPIO_TypeDef \*GPIOx) | Reads the output register value (16 bits) for all port pins, i.e., bit pattern you last wrote. | To debug or keep track of programmed output states. |
GPIO_SetBits(GPIO_TypeDef \*GPIOx, uint16_t GPIO_Pin) | Sets one or more specific output pins HIGH (writes 1 to them). | To turn on an LED, drive a line HIGH , or activate an external circuit. Use bitwise OR to set multiple pins at once. |
GPIO_ResetBits(GPIO_TypeDef \*GPIOx, uint16_t GPIO_Pin) | Drives one or more output pins LOW (writes 0 to them). | To turn off an LED, deactivate a device, or bring a line LOW . |
GPIO_WriteBit(GPIO_TypeDef \*GPIOx, uint16_t GPIO_Pin, BitAction BitVal) | Sets or resets a single output pin according to the value (Bit_SET or Bit_RESET ). | When you want to control a pin based on application logic. |
GPIO_Write(GPIO_TypeDef \*GPIOx, uint16_t PortVal) | Directly sets the output value of the entire port (all 16 pins) using the bit pattern in PortVal. | Rapidly update all port outputs, such as driving a parallel bus or LEDs, in one instruction. |
GPIO_PinLockConfig(GPIO_TypeDef \*GPIOx, uint16_t GPIO_Pin) | Locks the configuration of one or more pins—prevents accidental or intentional reconfiguration until the next reset. | After a pin is safely configured (especially for critical or multiplexed pins like JTAG/SWD or power controls). Helps comply with safety/security requirements. |
GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource) | Selects which pin on which port to use for the chip’s “Event Output” function (used for sampling/triggering by other modules or debugging). | Rarely used in basic projects, but used when you need to signal events to other parts of the system or another microcontroller. |
GPIO_EventOutputCmd(FunctionalState NewState) | Enables or disables the Event Output function after configuration. | Enable after calling GPIO_EventOutputConfig , or disable it when not needed. |
GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState) | Remaps the function of selected pins—assigns alternate functions (like SPI, UART, TIM, CAN, etc.) to different pins per the MCU’s remap capabilities. | To move peripherals away from default pin assignments—needed for peripherals that conflict, add more devices, or customise board layout. |
GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource) | Maps a specific GPIO pin to an external interrupt (EXTI ) line, so it can trigger interrupts. | Configuring external interrupts, e.g. when you want an interrupt on a button press or sensor signal. |
GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface) | Selects the Physical Layer interface for the Ethernet module: either MII (Media Independent Interface) or RMII (Reduced MII). | If using Ethernet, set this depending on your PHY/hardware design. |
GPIO_IPD_Unused(void) | Automatically configures all unused GPIOs on the chip to be safe unused state (mainly input pull-up or pull-down), based on the detected chip variant. | At the very start of main() , it’s good practice to call this so all unused pins aren’t left floating (which saves power and avoids EMI/noise). Most users only need this once in system init. |
CH32Vxxx GPIO overview
·720 words·4 mins·