2017-11-01 16 views
0

기본 Linux GPIO 사용자 공간 응용 프로그램을 작성하려고합니다. 어떤 이유로, 내보내기 파일을 열고 지정된 번호로 GPIO를 내보낼 수 있습니다. 그러나 출력 후에는/sys/class/gpio/gpio < ###>/direction 파일이 생성되지 않으므로 입력 또는 출력 여부를 지정할 수 없습니다. 결과적으로 제 C가 잘못되었습니다.Linux GPIO 값 파일이 내보내기시 생성되지 않음

루트 @의 plnx_arm : ~ #는/usr/빈/기본 - GPIO

GPIO 테스트 실행 ...

여기

코드 실행에서

#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; 
} 

출력입니다

GPIO를 성공적으로 내 보냈습니다.

GPIO 방향을 열 수 없습니다 그것은

파일

루트 @의 plnx_arm이있다 : ~ # 1!/SYS/클래스/GPIO/gpio971/

active_low 장치 방향 에지 전력 서브 시스템 uevent 값

+0

파일은 분명히 존재한다. 'export' 파일에 gpio 번호를 쓰는 것과 그 위로 나타나는'gpio971' 디렉토리 사이에 지연이있을 수 있습니다. 핀을 내 보낸 후 짧은 지연을 삽입하면 어떻게됩니까? – larsks

+0

또한 perror 함수를 사용하여 시스템 호출이 실패한 이유를 표시하는 것이 좋습니다 ([here] (https://stackoverflow.com/questions/12102332/when-should-i-use-perror 참조) - 및 - fprintfstderr) 해당 주제에 대한 일부 토론). – larsks

답변

0

"/ sys/class/gpio971/direction"이 아닌 "/ sys/class/gpio/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 핀 코드를 가져올 수 있습니다. 당신이 LS/SYS/클래스/GPIO/gpio971 /``를 실행할 때

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

+0

그래, 내가 그걸 놓쳤을 거라 믿어. 감사 –