🛠 GPIO Control and Simple Interfaces
Objectives
This tutorial aims to master the configuration of General Purpose Input/Output (GPIO) pins on the HPMicro HPM5100 microcontroller. You will learn to light an LED and detect the state of a push button, fundamental skills for any physical interaction with the system.
Required Equipment
- HPM5100 Development Board (e.g., HPM5151)
- USB-C cable for power and debugging
- One push button and a pull-up/down resistor if not integrated
- PC with HPM SDK v1.13.0 installed
Steps
Open a command prompt and navigate to the GPIO example folder in the SDK. Ensure environment variables are correctly set for the HPM5100. ``bash cd hpm_sdk/examples/drivers/gpio ``
- Environment Preparation
In the configuration file (often board.h or pin_name.h), identify physical pins. For the HPM5100, consult the datasheet to confirm available pin numbers on your board. Associate one GPIO pin with an LED and another with the button.
- Hardware Configuration
Open the main main.c file. Include necessary headers and initialize the clock system. The 400 MHz RISC-V core requires precise peripheral initialization. ```c #include "board.h" #include "hpm_gpio_drv.h"
- Software Initialization
void gpio_init(void) { clock_add_to_group(clock_gpio, 0); gpio_set_pin_output_with_initial_state(BOARD_APP_LED_GPIO_CTRL, BOARD_APP_LED_GPIO_INDEX, BOARD_APP_LED_GPIO_PIN, 0); } ```
Implement the toggle logic. Read the button state to change the LED state. Note that the HPM5100 supports GPIO interrupts, but for this first step, polling is sufficient. ``c while (1) { uint8_t button_state = gpio_read_pin(BOARD_APP_BUTTON_GPIO_CTRL, BOARD_APP_BUTTON_GPIO_INDEX, BOARD_APP_BUTTON_GPIO_PIN); if (button_state == 0) { gpio_write_pin(BOARD_APP_LED_GPIO_CTRL, BOARD_APP_LED_GPIO_INDEX, BOARD_APP_LED_GPIO_PIN, 1); } else { gpio_write_pin(BOARD_APP_LED_GPIO_CTRL, BOARD_APP_LED_GPIO_INDEX, BOARD_APP_LED_GPIO_PIN, 0); } delay_ms(50); } ``
- Main Loop
Compile the project specifying the HPM5100 target, then flash the binary to the board via USB or JTAG interface. ``bash cmake -B build -DBOARD=hpm5100evk cmake --build build ``
- Compile and Flash
Verification
Open the serial monitor at 115200 bauds to check startup messages. Press the physical button: the LED should turn on or off immediately. If nothing happens, check physical connections and pin numbers in the code.
Further Reading
Explore GPIO interrupts to avoid polling, or use the HPM5100's synchronized timers for precise blinking without blocking the processor.