組み込みLinuxファイルのIOモードでgpioポートを操作する例
組み込みLinuxのアプリケーションレイヤーでgpioポートを操作するために、ドライバーの作成やレジスターのマニュアルを読むのが面倒な場合は、簡単な操作方法があり、ターミナルにioを適用するだけで、方向を設定して直接操作できます。ここでは、Wildfireのimxボードを例に取ります。デモ、LED点滅ライトプログラムは後で例として使用されます
1.コマンドライン操作のデモ
1.ファイルシステムのルートディレクトリにsysディレクトリがあることを確認します。ない場合は、カーネルのコンパイル時にデバイスドライバーを開く必要があります—> GPIOサポート—> / sys / class / gpio /…(sysfsインターフェイス)
2.計算アプリケーションの数を確認します。ここでは、例としてIO1.1を取り上げます。
root @ npi:/ sys / class / gpio#cat gpiochip0 / label
209c000.gpio
ここで、ベースアドレスは209c000です。imx6ullrmマニュアルのP1141で、GPIOメモリマップ
はGPIO1 root @ npi:/ sys / class / gpio#catを検出します。 gpiochip0 / base
0
はIO1.1に適用したい、0 + 1 = 1
2.
/ sys / class / gpioディレクトリでioを申請するには、コマンドターミナルでecho 1> exportと入力します。gpio1ディレクトリ
root @ npi:/ sys / class / gpio#echo 1> export が現在のディレクトリに生成されます
3.出力を設定します
。gpio1ディレクトリに入り、
root @ npi:/ sys / class / gpio / gpio1#echo out>方向を変更します。
4.レベルを設定します
gpio1ディレクトリに入り、値を変更します
root @ npi:/ sys / class / gpio / gpio1#echo 0>値
5.入力
root @ npi:/ sys / class / gpio / gpio1#echo in>方向を構成します
6.読み取りレベル
root @ npi:/ sys / class / gpio / gpio1#cat値
0
接続:active_lowデバイス方向エッジパワーサブシステムuevent値
active_low:割り込み構成アクティブ低レベルアクティブ高レベル
エッジ:割り込み構成、立ち上がりエッジ立ち下がりエッジ
uevent:割り込みイベント
2、cプログラム操作
#include <stdio.h>
#include <fcntl.h>
int led_init()
{
#defien led "1"
#define led_path "/sys/class/gpio/gpio1"
#define led_dir "/sys/class/gpio/gpio1/direction"
//申请io
int fd = 0;
fd = open("/sys/class/gpio/export", O_WRONLY);
if(-1 == fd)
{
printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
exit(1);
}
if(-1 == write(fd, led, 1))
{
printf("write file operation error\r\n");
exit(1);
}
close(fd);
//配置
fd = open(led_dir, O_WRONLY);
if(-1 == fd)
{
printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
exit(1);
}
if(-1 == write(fd, "in", sizeof("in")))
{
printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
close(direction_fd);
exit(1);
}
close(fd);
return 0;
}
int led_ctrl(unsigned char x)
{
#define led_value "/sys/class/gpio/gpio1/value"
int fd = 0;
fd = open(led_value, O_WRONLY);
if(-1 == fd)
{
printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
exit(1);
}
if(x)
{
if(-1 == write(fd, "1", sizeof("in")))
{
printf("[%s]:[%d] write operation value error\r\n", __FUNCTION__, __LINE__);
close(direction_fd);
exit(1);
}
}
else
{
if(-1 == write(fd, "0", sizeof("in")))
{
printf("[%s]:[%d] write operation value error\r\n", __FUNCTION__, __LINE__);
close(direction_fd);
exit(1);
}
}
return 0;
}
int read_ledstate()
{
#define led_value "/sys/class/gpio/gpio1/value"
int fd = 0;
char value;
fd = open(led_value, O_WRONLY);
if(-1 == fd)
{
printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
exit(1);
}
if(-1 == read(gpiovalue_fd, &value, sizeof(value)))
{
printf("[%s]:[%d] read gpiovalue is fail\r\n", __FUNCTION__, __LINE__);
close(gpiovalue_fd);
exit(1);
}
return value;
}
int main()
{
int x = 0;
led_init();
led_ctrl(1);
x = read_ledstate();
printf("led state = %d \r\n", x);
return 0;
}