私は基本的なLinux GPIOユーザー空間アプリケーションを作成しようとしています。何らかの理由で、エクスポートファイルを開き、指定された番号のGPIOをエクスポートできます。ただし、エクスポート後には、/ sys/class/gpio/gpio < ###>/directionファイルが作成されていないため、入力か出力かを指定することはできません。その結果、私のCは間違っています。LinuxのGPIO値ファイルがエクスポート時に作成されない
ルートの@ plnx_arm::〜#は/ usr/binに/基本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があります:〜#のLS/SYS /クラス/ GPIO/gpio971/
active_lowデバイス方向エッジ電源サブシステム UEVENT値
ファイルは明らかに存在しています。 gpio番号を 'export'ファイルに書き込むことと、対応する' gpio971'ディレクトリを表示することとの間に遅延が生じる可能性があります。ピンをエクスポートした後に短い遅延を挿入するとどうなりますか? – larsks
また、 'perror'関数を使ってシステムコールが失敗した理由を表示することをお勧めします([here](https://stackoverflow.com/questions/12102332/when-should-i-use-perror参照) -and-fprintfstderr)を参照してください)。 – larsks