簡體   English   中英

導出時未創建Linux GPIO值文件

[英]Linux GPIO Value File Not Created on Export

我正在嘗試編寫一個基本的Linux GPIO用戶空間應用程序。 由於某種原因,我能夠打開導出文件並使用給定的編號導出GPIO。 但是,在導出后,由於未創建/ sys / class / gpio / gpio <###> / direction文件,因此無法指定是輸入還是輸出。 結果,我的心錯了。

這是代碼

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main()
{

    int valuefd, exportfd, directionfd;

    printf("GPIO test running...\n");

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

    if(exportfd < 0)
    {
        printf("Cannot open GPIO to export it\n");
        exit(1);
    }

    write(exportfd, "971", 4);
    close(exportfd);

    printf("GPIO exported successfully\n");

    directionfd = open("/sys/class/gpio971/direction", O_RDWR);

    if(directionfd < 0)
    {
        printf("Cannot open GPIO direction it\n");
        exit(1);
    }

    write(directionfd, "out", 4);
    close(directionfd);

    printf("GPIO direction set as output successfully\n");

    valuefd = open("/sys/class/gpio/gpio971/value", O_RDWR);

    if(valuefd < 0)
    {
        printf("Cannot open GPIO value\n");
        exit(1);
    }

    printf("GPIO value opened, now toggling...\n");

    while(1)
    {
        write(valuefd, "1", 2);
        write(valuefd, "0", 2);
    }


    return 0;
}

運行的輸出:

root @ plnx_arm:〜#/ usr / bin / basic-gpio

GPIO測試運行中...

GPIO成功導出

無法打開GPIO方向

文件在那里

root @ plnx_arm:〜#ls / sys / class / gpio / gpio971 /

active_low設備方向邊緣電源子系統事件值

您需要打開文件“ / sys / class / gpio / gpio971 / direction”,而不是“ / sys / class / gpio971 / direction”

   directionfd = open("/sys/class/gpio/gpio971/direction", O_RDWR);

    if(directionfd < 0)
    {
        printf("Cannot open GPIO direction it\n");
        exit(1);
    }

您可以參考[1],並獲得導出/取消導出/設置方向/讀取/寫入gpio引腳的代碼。

[1] https://elinux.org/RPi_GPIO_Code_Samples#sysfs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM