• sales

    +86-0755-88291180

Milk-V Duo GPIO LED Tutorial

There is an onboard LED on the Milk-V duo, connected to XGPIOC24.

The default GPIO related modules have all been programmed into the kernel, and there is no need to execute the load command again.

Run GPIO read and write commands on the console or write GPIO read and write programs in kernel or user mode to perform input and output operations on GPIO.

The group number value of XGPIOC is 416, so the GPIO number is 416+24=440.


After starting Duo and entering the command line, enter the following command:

echo 440 > /sys/class/gpio/export  # At this time, generate the /sys/class/gpio/gpio440 directory.

echo out > /sys/class/gpio/gpio440/direction # Reconfigure IO direction as output:

echo 1 > /sys/class/gpio/gpio440/value # Light the LED

echo 0 > /sys/class/gpio/gpio440/value # Turn off LED

echo 440 > /sys/class/gpio/unexport # Release GPIO resources


Using shell script to realize LED flashing;

The above can be used to operate the LED's on/off state in bash. Now, use a shell script to implement the LED's timed flashing function based on the above command:

Create an. sh file  led_blink.sh

Use the built-in vi editor in the official provided image, as shown in the following code:

#!/bin/bash

LED=440

# Run GPIO-LED

echo $LED > /sys/class/gpio/export

# Setup GPIO-LED

echo out > /sys/class/gpio/gpio$LED/direction

# Cycle 10 times:Turn on the LED and delay for 1 second, then turn off the LED and delay for 1 second.

for i in $(seq 1 30)

do

  echo 1 > /sys/class/gpio/gpio$LED/value

  sleep 0.4

  echo 0 > /sys/class/gpio/gpio$LED/value

  sleep 0.4

done

# cleanup gpio

echo $LED > /sys/class/gpio/unexport


After writing, save and exit (:wq), you can run the script directly: /root/led_blink.sh


If you want the LED to flash automatically after startup (as the system's status light), you can use vi to add/root/led to the/mnt/system/auto.sh script file_ Blink.sh&; As follows:



Using c language cross-compilation to realize LED flashing;


Enter the ubuntu system of the computer and download the tool chain:

wget https://sophon-file.sophon.cn/sophon-prod-s3/drive/23/03/07/16/host-tools.tar.gz


Then decompress:

tar xvf host-tools.tar.gz


Compiler confirms:

Enter the MIlk V Duo system.,cat /proc/version


From this, it can be seen that the official Duo system image uses the riscv64-unknown-linux-musl-gcc compiler (this compiler must be specified during computer ubuntu compilation, otherwise the compiled code cannot be implemented on the Duo development board)


Writing c language code:


In the duo compilation environment of ubuntu, establish the corresponding directory and write the led_blink.c

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <fcntl.h> //define O_WRONLY and O_RDONLY

// LED Pin

#define SYSFS_GPIO_EXPORT "/sys/class/gpio/export"

#define SYSFS_GPIO_UNEXPORT "/sys/class/gpio/unexport"

#define SYSFS_GPIO_RST_PIN_VAL "440"

#define SYSFS_GPIO_RST_DIR "/sys/class/gpio/gpio440/direction"

#define SYSFS_GPIO_RST_DIR_VAL "OUT"

#define SYSFS_GPIO_RST_VAL "/sys/class/gpio/gpio440/value"

#define SYSFS_GPIO_RST_VAL_H "1"

#define SYSFS_GPIO_RST_VAL_L "0"

int main()

{

    int fd;

    int count = 30;

    // Open the port/sys/class/gpio# echo 440 > export

    fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);

    if (fd == -1)

    {

        printf("ERR: export open error.\n");

        return EXIT_FAILURE;

    }

    write(fd, SYSFS_GPIO_RST_PIN_VAL, sizeof(SYSFS_GPIO_RST_PIN_VAL));

    close(fd);

    // Set the port direction/sys/class/gpio/gpio440# echo out > direction

    fd = open(SYSFS_GPIO_RST_DIR, O_WRONLY);

    if (fd == -1)

    {

        printf("ERR: direction open error.\n");

        return EXIT_FAILURE;

    }

    write(fd, SYSFS_GPIO_RST_DIR_VAL, sizeof(SYSFS_GPIO_RST_DIR_VAL));

    close(fd);

    // Output reset signal: pulled high>100ns

    fd = open(SYSFS_GPIO_RST_VAL, O_RDWR);

    if (fd == -1)

    {

        printf("ERR: gpio open error.\n");

        return EXIT_FAILURE;

    }

    while (count)

    {

        count--;

        write(fd, SYSFS_GPIO_RST_VAL_H, sizeof(SYSFS_GPIO_RST_VAL_H));

        usleep(1000000);

        write(fd, SYSFS_GPIO_RST_VAL_L, sizeof(SYSFS_GPIO_RST_VAL_L));

        usleep(1000000);

    }

    close(fd);

    // Open the port/sys/class/gpio# echo 440 > unexport

    fd = open(SYSFS_GPIO_UNEXPORT, O_WRONLY);

    if (fd == -1)

    {

        printf("ERR: unexport open error.\n");

        return EXIT_FAILURE;

    }

    write(fd, SYSFS_GPIO_RST_PIN_VAL, sizeof(SYSFS_GPIO_RST_PIN_VAL));

    close(fd);

    return 0;

}


Find the corresponding compiler in ubuntu.:sudo find / | grep  riscv64-unknown-linux-musl-gcc


Compile and generate the executable file led_blink:sudo.

/home/jw/duo/cvi_mmf_sdk/host-tools/gcc/riscv64-linux-musl-x86_64/bin/riscv64-unknown-linux-musl-gcc -static -o led_blink led_blink.c


Using SSH to connect the LED_ Blink executable file transfer to Duo development board:

(The prerequisite is that the image you are using has the RNDIS function in order to transfer files to the Duo development board using this method; the official system image for versions above Milkv duo 20230519-1809.img comes with the RNDIS function)

sudo scp ./led_blinkv2  root@192.168.42.1:/root/


Provide executable file LED on the Duoi development board_ Bink gives sufficient permissions chmod u+x led_ Blink and execute the code


After the execution, the on-board LED light flashes.

TAG: Industrial USB TO RS232/485 Serial UART Converter FT232RNL Compact Size Raspberry Pi 5 Pure-Copper Cooler Argon ONE V3 Case with FAN IR For Raspberry Pi 5 DeepSeek AI Voice Chat ESP32 S3 Development Board 2.06-inch AMOLED Display 410&times;502 2.06inch TouchScreen Programmable Watch Raspberry Pi Pico Display Mobile Robot MPS2280 POE Raspberry Pi 5 PCIe to M.2 NVMe SSD Adapter Board HAT Pi5 2280/2242/2230 1.5inch LCD 1.69inch LCD TouchScreen Display 240&times;280 ST7789 Arduino Raspberry Pi ESP32S3 Pico STM32 Raspberry Pi MIPI RM520N GL 5G/4G/3G M.2 Moudle IoT EMBB For LTE-A/NSA/SA And GNSS For DFOTA /VoLTE For Quectel FLASH File System Operation Raspberry Pi 5 IMX219 FT4232HL USB TTL RS232 Industrial UART 4-Ch UART VGA to LVDS Milk-V Duo Ethernet lilygo ESP32-S3 DeepSeek AI Box Robot Development Board N16R8 Toy Doll Option 0.71 inch Electronic Eyes MPTPU Raspberry Pi 5 TPU AI Kit 2TOPS PCIe to TPU HAT Pi5 For Google Coral Edge TPU Industrial Isolated Converter