0
点赞
收藏
分享

微信扫一扫

【RK3399Pro学习笔记】十八、使用python、C语言点亮LED灯

洛茄 2022-03-12 阅读 26

目录

平台:华硕 Thinker Edge R 瑞芯微 RK3399Pro
固件版本:Tinker_Edge_R-Debian-Stretch-V1.0.4-20200615


GPIO对应关系

在这里插入图片描述
在这里插入图片描述

python

参考资料:《Tinker_Edge_R_Getting_Started》

python-periphery API手册

sudo apt-get update
sudo apt-get install python3-pip
sudo pip3 install python-periphery

(官方例程)在合适的地方编写代码

from periphery import GPIO
import time

LED_Pin = 73 #Physical Pin-3 is GPIO 73
# Open GPIO /sys/class/gpio/gpio73 with output direction
LED_GPIO = GPIO(LED_Pin, "out")

while True:
    try: #Blink the LED
        LED_GPIO.write(True)
        # Send HIGH to switch on LED
        print("LED ON!")
        time.sleep(0.5)
        LED_GPIO.write(False)
        # Send LOW to switch off LED
        print("LED OFF!")
        time.sleep(0.5)
    except KeyboardInterrupt:
        # Turn LED off before stopping
        LED_GPIO.write(False)
        break
    except IOError:
        print ("Error")

LED_GPIO.close()

运行

sudo python3 blink.py

可见LED灯闪烁
在这里插入图片描述

C语言(SysFs方式)

本代码来自SysFs方式下C语言控制GPIO(RK3399)—— 姚家湾

编写

在合适的地方编写代码:

gpiolib.c

nano gpiolib.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/stat.h>
#include "gpiolib.h"

int gpio_direction(int gpio, int dir)
{
    int ret = 0;
    char buf[50];
    sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);
    int gpiofd = open(buf, O_WRONLY);
    if (gpiofd < 0)
    {
        perror("Couldn't open IRQ file");
        ret = -1;
    }

    if (dir == 2 && gpiofd)
    {
        if (3 != write(gpiofd, "high", 3))
        {
            perror("Couldn't set GPIO direction to out");
            ret = -2;
        }
    }

    if (dir == 1 && gpiofd)
    {
        if (3 != write(gpiofd, "out", 3))
        {
            perror("Couldn't set GPIO direction to out");
            ret = -3;
        }
    }
    else if (gpiofd)
    {
        if (2 != write(gpiofd, "in", 2))
        {
            perror("Couldn't set GPIO directio to in");
            ret = -4;
        }
    }

    close(gpiofd);
    return ret;
}

int gpio_setedge(int gpio, int rising, int falling)
{
    int ret = 0;
    char buf[50];
    sprintf(buf, "/sys/class/gpio/gpio%d/edge", gpio);
    int gpiofd = open(buf, O_WRONLY);
    if (gpiofd < 0)
    {
        perror("Couldn't open IRQ file");
        ret = -1;
    }

    if (gpiofd && rising && falling)
    {
        if (4 != write(gpiofd, "both", 4))
        {
            perror("Failed to set IRQ to both falling & rising");
            ret = -2;
        }
    }
    else
    {
        if (rising && gpiofd)
        {
            if (6 != write(gpiofd, "rising", 6))
            {
                perror("Failed to set IRQ to rising");
                ret = -2;
            }
        }
        else if (falling && gpiofd)
        {
            if (7 != write(gpiofd, "falling", 7))
            {
                perror("Failed to set IRQ to falling");
                ret = -3;
            }
        }
    }

    close(gpiofd);

    return ret;
}

int gpio_export(int gpio)
{
    int efd;
    char buf[50];
    int gpiofd, ret;

    /* Quick test if it has already been exported */
    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
    efd = open(buf, O_WRONLY);
    if (efd != -1)
    {
        close(efd);
        return 0;
    }

    efd = open("/sys/class/gpio/export", O_WRONLY);

    if (efd != -1)
    {
        sprintf(buf, "%d", gpio);
        ret = write(efd, buf, strlen(buf));
        if (ret < 0)
        {
            perror("Export failed");
            return -2;
        }
        close(efd);
    }
    else
    {
        // If we can't open the export file, we probably
        // dont have any gpio permissions
        return -1;
    }
    return 0;
}

void gpio_unexport(int gpio)
{
    int gpiofd, ret;
    char buf[50];
    gpiofd = open("/sys/class/gpio/unexport", O_WRONLY);
    sprintf(buf, "%d", gpio);
    ret = write(gpiofd, buf, strlen(buf));
    close(gpiofd);
}

int gpio_getfd(int gpio)
{
    char in[3] = {0, 0, 0};
    char buf[50];
    int gpiofd;
    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
    gpiofd = open(buf, O_RDWR);
    if (gpiofd < 0)
    {
        fprintf(stderr, "Failed to open gpio %d value\n", gpio);
        perror("gpio failed");
    }

    return gpiofd;
}

int gpio_read(int gpio)
{
    char in[3] = {0, 0, 0};
    char buf[50];
    int nread, gpiofd;
    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
    gpiofd = open(buf, O_RDWR);
    if (gpiofd < 0)
    {
        fprintf(stderr, "Failed to open gpio %d value\n", gpio);
        perror("gpio failed");
    }

    do
    {
        nread = read(gpiofd, in, 1);
    } while (nread == 0);
    if (nread == -1)
    {
        perror("GPIO Read failed");
        return -1;
    }

    close(gpiofd);
    return atoi(in);
}

int gpio_write(int gpio, int val)
{
    char buf[50];
    int nread, ret, gpiofd;
    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
    gpiofd = open(buf, O_RDWR);
    if (gpiofd > 0)
    {
        snprintf(buf, 2, "%d", val);
        ret = write(gpiofd, buf, 2);
        if (ret < 0)
        {
            perror("failed to set gpio");
            return 1;
        }

        close(gpiofd);
        if (ret == 2)
            return 0;
    }
    return 1;
}

int gpio_select(int gpio)
{
    char gpio_irq[64];
    int ret = 0, buf, irqfd;
    fd_set fds;
    FD_ZERO(&fds);

    snprintf(gpio_irq, sizeof(gpio_irq), "/sys/class/gpio/gpio%d/value", gpio);
    irqfd = open(gpio_irq, O_RDONLY, S_IREAD);
    if (irqfd < 1)
    {
        perror("Couldn't open the value file");
        return -13;
    }

    // Read first since there is always an initial status
    ret = read(irqfd, &buf, sizeof(buf));

    while (1)
    {
        FD_SET(irqfd, &fds);
        ret = select(irqfd + 1, NULL, NULL, &fds, NULL);
        if (FD_ISSET(irqfd, &fds))
        {
            FD_CLR(irqfd, &fds); // Remove the filedes from set
            // Clear the junk data in the IRQ file
            ret = read(irqfd, &buf, sizeof(buf));
            return 1;
        }
    }
}

gpiolib.h

nano gpiolib.h
#ifndef _GPIOLIB_H_

/* returns -1 or the file descriptor of the gpio value file */
int gpio_export(int gpio);
/* Set direction to 2 = high output, 1 low output, 0 input */
int gpio_direction(int gpio, int dir);
/* Release the GPIO to be claimed by other processes or a kernel driver */
void gpio_unexport(int gpio);
/* Single GPIO read */
int gpio_read(int gpio);
/* Set GPIO to val (1 = high) */
int gpio_write(int gpio, int val);
/* Set which edge(s) causes the value select to return */
int gpio_setedge(int gpio, int rising, int falling);
/* Blocks on select until GPIO toggles on edge */
int gpio_select(int gpio);

/* Return the GPIO file descriptor */
int gpio_getfd(int gpio);

#endif //_GPIOLIB_H_

main.c

nano main.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "gpiolib.h"

int main(int argc, char **argv)
{
    int gpio_pin;

    if (argc != 2)
    {
        printf("Too few parameters in call!\r\n");
        return -1;
    }
    gpio_pin = atoi(argv[1]);

    gpio_export(gpio_pin);
    gpio_direction(gpio_pin, 1);

    for (int i = 0; i < 5; i++)
    {
        printf(">> GPIO %d Low\n", gpio_pin);
        gpio_write(gpio_pin, 0);

        sleep(1);

        printf(">> GPIO %d High\n", gpio_pin);
        gpio_write(gpio_pin, 1);

        sleep(1);
    }

    return 0;
}

编译

编写Makefile文件

nano Makefile
main: main.o gpiolib.o
        cc -o main main.o gpiolib.o
main.o: main.c gpiolib.h
        cc -c main.c gpiolib.h
gpiolib.o: gpiolib.c gpiolib.h
        cc -c gpiolib.c
.PHONY:clear
clear:
        rm *.o
        rm main

编译

make

测试

可见LED灯闪烁

chmod +x ./main
sudo ./main 73
举报

相关推荐

0 条评论