A note previously written, occasionally flipped out, put here to make a souvenir
LinuxunderIOPort Programmatic Access
The method recorded here is to access the IO port in the user state and does not involve the driver programming.
First to include the header file/usr/include/asm/io.h
Ioperm ()
Declared in the unistd.h . Used to open access to the IO Port, requiring the program to execute with root privileges. Only the IO Port of the address segment of 0x00 to 0x3ff can be opened .
Ioperm (from, NUM, turn_on)
Like what:
#include <asm/io.h> #include <unistd.h>ioperm (0x300, 5, 1);
represents the0x300Continuous with Address5aIOthe access permission for the address is opened. IOPL ()
If you need to access a higher address segment than 0x3ff , you need to use the iopl () function.
IOPL (3) indicates that access to all IO ports is turned on.
IoRead and write operations
INB (port); inw (port); Outb (value, port); Outw (value, port);
The IO Port reads and writes approximately 1us. It is important to note that the order of the parameters of the OUTB function and the OUTPORTB function in Turbo C is reversed and not mistaken.
Inb_p (port); inw_p (port); outb_p (value, port); outw_p (value, port);
The functions of these four functions are basically similar to the above four functions, but after accessing the IO Port, a delay of about 1us is added. If the REALLY_SLOW_IO macro is defined , the delay is about 4us.
#define REALLY_SLOW_IO 1#include<asm/io.h>inb_p (port);
/dev/port
Another way to access the IO port is by accessing the /dev/port device file.
This method does not need to have root permission, only has the access to the /dev/port to be OK.
Access is like normal file access, first Lseek (), and then the read (), write () function.
However, you cannot use the Select () function and the poll () function because the IO Port does not support these features.
Switch interrupt
IOPL (3) after opening access, you can switch interrupts by inserting assembly statements.
ASM ("CLI"); Interrupt asm ("STI"); Open Interrupt