/*                 GPIO_CON
  								  _ _ _ 
GPIO21 >>----------|1	  2|----------<<GPIO36
GPIO33 >>----------|3		4|----------<<GPIO37
GPIO34 >>----------|5   6|----------<<GPIO38
GPIO35 >>----------|7		8|----------<<GPIO39
							-----|9	 10|----------OVCC
							|		  - - -			|	
							
GPIO21 GPIO_Base + offset_2_bit_5 Set 1 For Gpio Funtion
       GPIO_Base + offset_6_bit_5 Set 0/1 For Output/input	
       GPIO_Base + offset_E_bit_5 Set 0/1 For Low/High Only in Output Mode
       
GPIO32+n GPIO_Base + offset_30_bit_(1+n) Set 1 For Gpio Function      
       	 GPIO_Base + offset_34_bit_(1+n) Set 0/1 For Output/input	
      	 GPIO_Base + offset_38_bit_(1+n) Set 0/1 For Low/High Only in Output Mode		
parameter first
1-8
parameter second
0 ->output
1 ->input
parameter third
0 ->low
1 ->high
for NF98 GPIO				 
*/
#include <conio.h>
#include <stdio.h>
#include <dos.h>
int offset=0x30,Val_offset=0x8;
int inout;//second
int highlow;//three
int GPIO_Base=0x500;
int bit=0;
int status;

int main(int argc,char* argv[]){
	printf("-------------------------This Program only for NF98 GPIO Function\n-------------------------Don't Run At Others MainBoard!!!!!\n");
  if(argc <3)
  	{
  		printf("Error!\nUsage:NF98GPIO Pin_num Out/in_put High/low(Only Output mode)");
  		return 0;
  		}
  sscanf(argv[1],"%d",&bit);	
  sscanf(argv[2],"%d",&inout);	
  inout=inout&1;
  if(argc>=4)
  sscanf(argv[3],"%d",&highlow);	
  highlow=highlow&1;

  switch(bit)
  {
	case 1:offset=0x2; bit=5;Val_offset=0x0C;break;
	case 2:bit=4;break;
	case 3:bit=1;break;
	case 4:bit=5;break;
	case 5:bit=2;break;
	case 6:bit=6;break;
	case 7:bit=3;break;
	case 8:bit=7;break;
	default:printf("You must input correct NUM (1-8)!\n");
	return 0;
  }

	//setp 1.set gpio Function
	status=(int)inp(GPIO_Base+offset);
	outp(GPIO_Base+offset, (1<<bit)|status);//set bit to 1

	//step 2.set gpio mode
	status=(int)inp(GPIO_Base+offset+4);
	outp(GPIO_Base+offset+4,status&(~(1<<bit))|(inout<<bit));
  
  //setp 3.set gpio High / Low only for Output mode
  if(argc >=4)
  {
	status=(int)inp(GPIO_Base+offset+Val_offset);
	outp(GPIO_Base+offset+Val_offset,status&(~(1<<bit))|(highlow<<bit));	
	}
	
	return 1;
}
