🛠 GPIO Control and Simple Interfaces

HPMicro HPM5100: 400 MHz RISC-V MCU for Motor Control and Analog Sensing · debutant · Apprenez à configurer les broches du HPM5100 pour piloter une LED et lire un bouton via le SDK HPM.

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

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 ``

  1. 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.

  1. 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"

  1. 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); } ``

  1. 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 ``

  1. 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.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials