Linux 下IO端口编程访问
这里记录的方法是在用户态访问IO端口,不涉及驱动程序的编写。
首先要包含头文件 /usr/include/asm/io.h
ioperm()
在
比如:
- #include
- #include
- ioperm(
0x300, 5, 1);
iopl()
如果需要访问比0x3ff更高的地址段,需要用iopl()
iopl(3)
IO 读写操作
- inb(port);
- inw(port);
- outb(value,
port); - outw(value,
port);
对IO端口的读写大约用时1us。需要注意的是outb 函数与 turbo C 中的 outportb 函数的参数的顺序是反的,不要搞错。
- inb_p(port);
- inw_p(port);
- outb_p(value,
port); - outw_p(value,
port);
这四个函数和上面四个函数的功能基本类似,但是在访问完IO端口后会增加一个1us左右的延时。如果定义了REALLY_SLOW_IO这个宏,则延时约4us。
- #define
REALLY_SLOW_IO 1 - #include
- inb_p(port);
/dev/port
另外一种访问IO端口的方法是通过对/dev/port
这种方法不需要有root
访问起来就像普通的文件访问一样,
但是不能使用
开关 中断
iopl(3)
- asm(“cli”);
//关中断 - asm(“sti”);
//开中断