Next Previous Contents

9. 程式码□例

这边是一段用来存取 I/O 埠的简单的程式码□例:


/*
 * example.c: 一个用来存取 I/O 埠的非常简单的□例
 *
 * 这个程式码并没有什麽用处, 他只是做了埠的写入, 暂停, 
 * 以及埠的读出几个动作. 编译时请使用 `gcc -O2 -o example example.c',
 * 并以 root 的身份执行 `./example'.
 */

#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>

#define BASEPORT 0x378 /* lp1 */

int main()
{
  /* 取得埠位址的存取权限 */
  if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); exit(1);}
  
  /* 设定埠的输出资料信号 (D0-7) 全为零 (0) */
  outb(0, BASEPORT);
  
  /* 休息一下 (100 ms) */
  usleep(100000);
  
  /* 从状态埠 (BASE+1) 读出资料并显示结果 */
  printf("status: %d\n", inb(BASEPORT + 1));

  /* 我们不再需要这些埠位址 */
  if (ioperm(BASEPORT, 3, 0)) {perror("ioperm"); exit(1);}

  exit(0);
}

/* 结束 example.c */


Next Previous Contents