Dr. Abhishek Dutta

isa.c

/* 
 *	isa.c : Code to illustrate the access the Four 8-Bit I/O Lines 
 *		obtained by interfacing the ISA Bus
 *	
 *	Authors: Abhishek Dutta		<thelinuxmaniac@gmail.com>
 *		 Badri Adhikari		<badri.adhikari@gmail.com>
 *		 Pushpeshwor Pokharel	<pushpeshwor@gmail.com>
 *
 *
 *	Coded by: Abhishek Dutta <thelinuxmaniac@gmail.com>
 *		  http://www.mycgiserver.com/~thelinuxmaniac
 *		  17 Jan 2006
 */

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

/* Change these constants and addresses according to your device settings */
const int OUTPUT_PORT = 0x338;	/* Starting address of output port */
const int INPUT_PORT = 0x238;	/* Starting address of input port */
const int LENGTH = 3;		/* No of continuous port addresses */
const int SLEEP_TIME = 1;	/* Delay for DANCING LIGHT TEST */

void clear_screen(void);
void write_port(void);
void read_port(void);

/* DANCING LIGHT TEST
 * Method: Connect eight LED (Light Emitting Diode) to any of the 8 output lines
 *	   and run this test.It will turn the LED ON one by one with some delay.
 *
 * Integer constants for running the dancing light test
 */ 
int a[9] = {1,2,4,8,16,32,64,128,256};
int b[9] = {256,128,64,32,16,8,4,2,1};

void test_output(short count)
{
	short i,j;
	clear_screen();
	/* Run the dancing lights 'count' no of times */
	printf("No of tests\t= %d",count);
	printf("\nDELAY\t\t= %d",SLEEP_TIME);
	printf("\n\n\t\tCan you see the lights dancing ;)\n");
	for(i=0;i<count;i++)
	{
		printf("\nCOUNT %d",i+1);
		for(j=0;j<9;j++)
		{
			outb(a[j],OUTPUT_PORT);
			outb(a[j],OUTPUT_PORT+1);
			outb(a[j],OUTPUT_PORT+2);
			outb(a[j],OUTPUT_PORT+3);
			j==8?printf("\t#"):printf("\t*");
			fflush(stdout);
			sleep(SLEEP_TIME);
		}
		/* Reset all the ports */
		outb(0x00,OUTPUT_PORT);
		outb(0x00,OUTPUT_PORT+1);
		outb(0x00,OUTPUT_PORT+2);
		outb(0x00,OUTPUT_PORT+3);
		sleep(SLEEP_TIME);;
	}
}
/* Just to make things look better
   I did not use 'ncurses' as it might scare some guys out there:)
   */
void clear_screen(void)
{
	int i;
	for(i=0;i<24;i++)
		printf("\n");
}
/* Writes a BYTE to the specified port */
void write_port(void)
{
	clear_screen();
	int port,data;
	printf("\n\t\tWrite to ports 0x%x - 0x%x",OUTPUT_PORT,OUTPUT_PORT+LENGTH);
	printf("\n\t\tEnter negative address to return to MAIN MENU\n");
	printf("\t\tEnter hexadecimal values for address and data.\n");
	while(1)
	{
		printf("\nEnter port address to WRITE TO: ");
		scanf("%x",&port);
		if(port <0)
			return;

		if(port<OUTPUT_PORT || port > OUTPUT_PORT+LENGTH)
			printf("\tERR: Port address out of range 0x%x - 0x%x",OUTPUT_PORT,OUTPUT_PORT+LENGTH);
		else
		{
			printf("Enter data: ");
			scanf("%x",&data);
			outb(data,port);
			printf("\tWritten 0x%x to address 0x%x",data,port);
		}
	}
}
/* Reads a BYTE from the specified port */
void read_port(void)
{
	int port,data;
	clear_screen();
	printf("\n\t\tRead from ports 0x%x - 0x%x",INPUT_PORT,INPUT_PORT+LENGTH);
	printf("\n\t\tEnter negative address to return to MAIN MENU\n");
	printf("\t\tEnter hexadecimal values for address and data.\n");
	while(1)
	{
		printf("\nEnter port address to READ FROM: ");
		scanf("%x",&port);
		if(port <0)
			return;
		if(port<INPUT_PORT || port > INPUT_PORT+LENGTH)
			printf("\tERR: Port address out of range 0x%x - 0x%x",INPUT_PORT,INPUT_PORT+LENGTH);
		else
		{
			data = inb(port);
			printf("\tByte read from address 0x%x = 0x%x",port,data);
		}
	}	
}
int main()
{
	char choice;
	int count;
	count = 1;	/* Default value for no of times to run the DANCING LIGHT test */

	/*
	 * Get access to the I/O ports from KERNEL
	 * 
	 * NOTE: 'ioperm' only allows access to limited range of I/O 
	 * 	 address 0 - 0x3FF
	 */
	if(ioperm(OUTPUT_PORT,LENGTH+1,1))
	{
		perror("ERR");
		printf("Unable to get permission to access OUTPUT ADDRESS 0x%x - 0x%x",OUTPUT_PORT,OUTPUT_PORT+LENGTH);
		printf("\nYou must be 'root' to be able to run this program.\n\n");
		exit(1);
	}
	if(ioperm(INPUT_PORT,LENGTH+1,1))
	{
		perror("ERR");
		printf("Unable to get permission to access INPUT ADDRESS 0x%x - 0x%x",INPUT_PORT,INPUT_PORT+LENGTH);
		printf("\nYou must be 'root' to be able to run this program.\n\n");
		exit(1);
	}
	do
	{
		clear_screen();
		printf("\nProgram for Input/Output operations using ISA Interface");
		printf("\n-------------------------------------------------------");
		printf("\nMain Menu");
		printf("\n\n\t1. Write to a port");
		printf("\n\t2. Read from a port");
		printf("\n\t3. Run the output test");
		printf("\n\t9. Exit");
		printf("\n\nEnter Choice (0 - 9): ");
		fflush(stdin);
		choice = getchar();
		switch(choice)
		{
			case '1':
				write_port();
				break;
			case '2':
				read_port();
				break;
			case '3':
				printf("\nNo of times to run the test ? ");
				scanf("%d",&count);
				test_output(count);
				break;
			case '9':
				printf("\nBye!\n");
				break;
			default:
				break;
		}
	}
	while(choice != '9');
	/* Release the permissions for the ports */
	ioperm(OUTPUT_PORT,LENGTH,0);
	ioperm(INPUT_PORT,LENGTH,0);
	return 0;
}
Advertisements

Written by abhishekdutta

December 13, 2010 at 5:58 am