Programming access to IO ports under Linux

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.