Controlling GPIOs on a PIC64GX Curiosity Board From Linux® Userspace

Last modified by Microchip on 2025/05/19 12:25

Introduction

General Purpose Input/Output (GPIO) pins are essential for interacting with hardware components like LEDs and Dual In-line Package (DIP) switches on embedded systems, In a Linux® environment, you can control these GPIOs to turn LEDs on and off or read the state of DIP switches. 

This guide will walk you through the basics of using GPIOs on a PIC64GX using the PIC64GX Curiosity Board as hardware. We will use libgpiod for standard GPIO operations to control the on-board LEDs and read DIP switch states. 

Prerequisites

This application is developed for the Curiosity PIC64GX1000 Kit ES development platform:

Yocto Project® Configurations

With the default configuration of the Yocto Project® image builder for the PIC64GX Curiosity Board Board Support Package (BSP), it will include the libgpiod library on the Linux system, which can be used from the userspace.

Information

This application article is verified on PIC64GX Yocto Project BSP release 2024.10.

We can check configurations in the Linux Kernel, build a bootable image and Flash it onto an SD card for the PIC64GX Curiosity Board.

Follow the steps in the GitHub PIC64GX Yocto Project BSP Repo README to setup the Bitbake environment and build a core-image-minimal-dev Linux image with the default configuration:

MACHINE=pic64gx-curiosity-kit bitbake core-image-minimal-dev

Once the initial Linux image has been built without error, we can start configuring the Linux kernel to include the libgpiod.

Set up the Bitbake environment:

. ./meta-pic64gx-yocto-bsp/pic64gx_yocto_setup.sh
Information

core-image-minimal-dev default configuration includes the libgpiod library, you can skip the below steps if you're following the the default configs.

Back to Top


View/edit the kernel menuconfig:

MACHINE=pic64gx-curiosity-kit bitbake pic64gx-linux -c menuconfig

Back to Top


After the menuconfig interactive configuration tool opens, enable the GPIO Support by navigating to:

Device Drivers > [*] GPIO Support

Back to Top


Check if the Software Packages libgpiod and libgpiod-tools are included in your build image by navigating to your image's core recipe:

yocto-dev/meta-pic64gx-yocto-bsp/meta-pic64gx-bsp/recipes-core/images/core-image-minimal-dev.bbappend

Back to Top

Deploy the Linux Image on PIC64GX Curiosity Board

Steps to deploy the Yocto Project release image:

Back to Top

Hardware

For this application, we will control the GPIOs connected to the on-board LEDs and DIP switches of the Curiosity Board.

LEDs and DIP switches of the Curiosity Board

We can check the pins that are connected to the LEDs and DIP switches from the PIC64GX1000 Curiosity kit schematics.

ComponentsConnections ComponentsConnections
LED1GPIO1-2 DIP1GPIO2-8
LED2GPIO1-3 DIP2GPIO2-9
LED3GPIO1-4 DIP3GPIO2-11
LED4GPIO1-5 DIP4GPIO2-24
LED5GPIO1-6 PUSH-BUTTON 1GPIO_2_28
LED6GPIO1-7 PUSH-BUTTON 2GPIO_2_6
LED7GPIO1-8   
LED8GPIO1-9   

Back to Top

Software Tools and Utilities

libgpiod – C Library and Tools

libgpiod (GPIO device library) is a C library and tools for interacting with the Linux GPIO character device. This library encapsulates the ioctl() calls and data structures using a straightforward API.

For more information see the C library and tools for interacting with the Linux GPIO character device.

Back to Top

APIs of libgpiod

libgpiod provides a simple API to access the GPIO driver. When Yocto Project completes its build, the C header file for these APIs are located at /build/tmp-glibc/work/riscv64-oe-linux/libgpiod/1.6.3-r0/libgpiod-1.6.3/include/gpiod.h.

The libgpiod tools are located in the following folder. These provide demonstration source code that shows you how to work with the libgpiod API.

From the Yocto Project directory:

​$ cd /build/tmp-glibc/work/riscv64-oe-linux/libgpiod/1.6.3-r0/libgpiod-1.6.3/tools/
$ ls *.c
gpiodetect.c gpiofind.c gpioget.c gpioinfo.c gpiomon.c gpioset.c tools-common.c

Back to Top

Tools of libgpiod

There are some simple tools provided by libgpiod for accessing the GPIO driver from the shell.

​In the past, GPIO was accessed by the shell from the sysfs interface. As of Linux version 4.8, this method has been deprecated. The libgpiod was developed as a more effective way to access the GPIO driver.

There are six commands in libgpiod tools:

  1. gpiodetect: list all gpiochips present on the system, their names, labels, and number of GPIO lines
  2. gpioinfo: list all lines of specified gpiochips, their names, consumers, direction, active state, and additional flags
  3. gpioget: read values of specified GPIO lines
  4. gpioset: set values of specified GPIO lines, potentially keep the lines exported and wait until timeout, user input or signal
  5. gpiofind: find the gpiochip name and line offset given the line name
  6. gpiomon: wait for events on GPIO lines, specify which events to watch, how many events to process before exiting or if the events should be reported to the console

Back to Top

Using libgpiod Tools on PIC64GX Curiosity Board

Detect GPIOs on the target processor:

# gpiodetect
gpiochip0 [20120000.gpio] (14 lines)
gpiochip1 [20121000.gpio] (24 lines)
gpiochip2 [20122000.gpio] (32 lines)

Print all lines of information:

# gpioinfo
[...]
gpiochip1 - 24 lines:
        line   0:      unnamed       unused  output  active-high
        line   1:      unnamed       unused  output  active-high
        line   2:       "LED1"       unused  output  active-high
        line   3:       "LED2"       unused  output  active-high
        line   4:       "LED3"       unused  output  active-high
        line   5:       "LED4"       unused  output  active-high
        line   6:       "LED5"       unused  output  active-high
        line   7:       "LED6"       unused  output  active-high
        line   8:       "LED7"       unused  output  active-high
        line   9:       "LED8"       unused  output  active-high

[...]

Find the GPIO chip name and offset of LED1:

# gpiofind LED1
gpiochip1 2

Set LED1 output high (turning on the LED1):

# gpioset gpiochip1 2=1

Set LED2 output low (turning off the LED1):

# gpioset gpiochip1 2=0

Monitor SW1 pin status:

# gpiomon gpiochip2 28
event: FALLING EDGE offset: 28 timestamp: [1325983345.255958082]
event: RISING EDGE offset: 28 timestamp: [1325983345.256686960]
event: FALLING EDGE offset: 28 timestamp: [1325983348.205010375]
event: RISING EDGE offset: 28 timestamp: [1325983348.577229302]

Reading DIP1 Input state:

# gpioget gpiochip2 8

Back to Top

Application Programming in C Language

We can write a C program that runs from Userspace and uses the libgpiod library. The following example is a C code that will flash the LED3 five times.

Boot Linux on your Curiosity board, and navigate to /media and create gpio.c using vim editor.

#cd /media && vim gpio_flash.c

Back to Top


Copy and paste the following C code to gpio.c:

#include <gpiod.h>
#include
<stdio.h>
#include
<unistd.h>

#define CONSUMER "LED_Flasher"
#define LED_PIN 4  
// Change this to the GPIO pin number connected to your LED

int main(void) {
   struct gpiod_chip *chip;
   struct gpiod_line *line;
   int ret;
chip = gpiod_chip_open_by_name("gpiochip1");  // Change "gpiochip1" if necessary
if (!chip) {
    perror("Open chip failed");
   return 1;
}

line = gpiod_chip_get_line(chip, LED_PIN);
if (!line) {
    perror("Get line failed");
    gpiod_chip_close(chip);
   return 1;
}

ret = gpiod_line_request_output(line, CONSUMER, 0);
if (ret != 0) {
    perror("Request line as output failed");
    gpiod_chip_close(chip);
   return 1;
}

for (int i = 0; i < 5; i++) {  // Flash 5 times
   gpiod_line_set_value(line, 1);  // LED on
   sleep(1);  // 1 second delay
   gpiod_line_set_value(line, 0);  // LED off
   sleep(1);  // 1 second delay
}

gpiod_line_release(line);
gpiod_chip_close(chip);

return 0;
}

Back to Top


After saving the modification, compile the C code:

gcc gpio_flash.c -o gpio_flash -L/usr/lib -lgpiod

Back to Top


Run the gpio executable:

./gpio_flash
Success

Observe the LED3 flashes five times and the application exits.

Back to Top

Summary

We used Yocto Project to build an image with the libgpiod library and tools. We also wrote an application in C and interacted via the shell to read and write to the GPIO pins of a PIC64GX MPU.

Back to Top