⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_main.c

📁 It is driver source code for IDE harddisk and used to learn IDE interface and file system.
💻 C
字号:
#include "44b.h"
#include "def.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stdarg.h>

extern KeyboardMatch(void);
extern KeyboardInitial(void);
extern int GetParamCount(void);
extern char* GetParam(unsigned int index);

void Port_Init(void);

int IPADDRESS = 0xc0a80064;

static int delayLoopCount=400;

void Delay(int time)
{

    int i,adjust=0;
    if(time==0)
    {
	time=200;
	adjust=1;
	delayLoopCount=400;
	rWTCON=((MCLK/1000000-1)<<8)|(2<<3);//MCLK/1M,Watch-dog disable,1/64,interrupt disable,reset disable
	rWTDAT=0xffff;//for first update
	rWTCNT=0xffff;//resolution=64us	@any MCLK 
	rWTCON=((MCLK/1000000-1)<<8)|(2<<3)|(1<<5);//Watch-dog timer start
    }
    
    for(;time>0;time--)  for(i=0;i<delayLoopCount;i++);
    if(adjust==1)
    {
	rWTCON=((MCLK/1000000-1)<<8)|(2<<3);//Watch-dog timer stop        
	i=0xffff-rWTCNT;		//1count->64us, 200*400 cycle runtime = 64*i us	   
        delayLoopCount=581;
    }
}

void Uart_Init(int baud)
{
    rUFCON0=0x0;     //FIFO disable
    rUFCON1=0x0;
    rUMCON0=0x0;
    rUMCON1=0x0;
    rULCON0=0x3;     //Normal,No parity,1 stop,8 bit
    rUCON0=0x245;    //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
    rUBRDIV0=( (int)(MCLK/16./baud + 0.5) -1 );   
}

int Uart_Getch()
{
     while(!(rUTRSTAT0 & 0x1)); //Receive data read
	return RdURXH0();
}

void Uart_SendByte(int data)
{
	while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
	Delay(10);
	WrUTXH0(data);
}

void Uart_SendString(char *pt)
{
    while(*pt)
	Uart_SendByte(*pt++);
}

void Uart_Printf(char *fmt,...)
{
    va_list ap;
    char string[256];

    va_start(ap,fmt);
    vsprintf(string,fmt,ap);
    Uart_SendString(string);
    va_end(ap);
}


void WriteBYTE(unsigned char cs, unsigned char adr, unsigned char dat)
{
  int add = ((adr&0x01)<<4) | ((adr>>1&0x01)<<5) | ((adr>>2&0x01)<<3) | (cs << 1);
  (*(volatile U8 *)(0x4000000+add)) = dat;
}

unsigned char ReadBYTE(unsigned char cs, unsigned char adr) 
{ 
  int add = ((adr&0x01)<<4) | ((adr>>1&0x01)<<5) | ((adr>>2&0x01)<<3) | (cs << 1);
  return (*(volatile U8 *)(0x4000000+add));
}

unsigned char SetMode(unsigned char DriveNo, unsigned char Mode,  unsigned char PwrDown) 
{
  WriteBYTE(1, 6, 0xA0 + (DriveNo ? 0x10:0x00)); // Select drive
  WriteBYTE(1, 2, (PwrDown ? 0x01:0x00)); // Enable automatic power down
  switch (Mode) {
    case 0:    WriteBYTE(1,7, 0xE2); break;
    case 1:    WriteBYTE(1,7, 0xE3); break;
    // NOTE: To recover from sleep, either issue a soft or hardware reset !
    // (But not on all drives, f.ex seagate ST3655A it's not nessecary to reset
    // but only to go in Idle mode, But on a Conner CFA170A it's nessecary with
    // a reset)
    case 2:   WriteBYTE(1,7, 0xE6); break;
  }
  while ((ReadBYTE(1,7) & 0xC0)!=0x40); // Wait for DRDY & NOT BUSY 
 
  // Return the error register...
  return ReadBYTE(1, 1);
}

unsigned char ReadSector(unsigned char Drive, 
                unsigned char Head, unsigned int Track, unsigned char Sector) 
{
  unsigned int i;
  // Prepare parameters...
  WriteBYTE(1,6, 0xA0+(Drive ? 0x10:00)+Head); // CHS mode/Drive/Head
  WriteBYTE(1,5, Track>>8);  // MSB of track
  WriteBYTE(1,4, Track);     // LSB of track
  WriteBYTE(1,3, Sector);    // sector
  WriteBYTE(1,2, 0x01);      // 1 sector
  // Issue read sector command...
  WriteBYTE(1,7, 0x20);      // Read sector(s) command
  while ((ReadBYTE(1,7) & 0x08)!=0x08); // Wait for DRQ or timeout

   for (i=0; i<256;i++) Uart_Printf("%x ", (*(volatile U16 *)0x4000002));
   return ReadBYTE(1, 1);
}



unsigned char WriteSector(unsigned char Drive, unsigned char Head, unsigned int Track, unsigned char Sector) 
{
  unsigned int i;

  while ((ReadBYTE(1,7) & 0x80)==0x80);

  // Prepare parameters...
  WriteBYTE(1,6, 0xA0+(Drive ? 0x10:00)+Head); // CHS mode/Drive/Head
  WriteBYTE(1,5, Track>>8);  // MSB of track
  WriteBYTE(1,4, Track);     // LSB of track
  WriteBYTE(1,3, Sector);    // sector
  WriteBYTE(1,2, 0x01);      // 1 sector

  while ((ReadBYTE(1,7) & 0x10)!=0x10 );
  // Issue write sector command...
  WriteBYTE(1,7, 0x30);      // Write sector(s) command

  while ((ReadBYTE(1,7) & 0x08)!=0x08 ) ;
 
  // write sector data...
  for (i=0; i<256; i++)  (*(volatile U16 *)0x4000002) = 0x4444;
  while ((ReadBYTE(1,7) & 0xC0)!=0xC0 ); // Wait for DRDY and NOT BUSY
 
  // Return the error register...
  return ReadBYTE(1, 1);
}

typedef struct 
{
  U8 Heads; 
  unsigned int Tracks;
  unsigned int SectorsPerTrack;
  U8 Model[41];
} tdefDriveInfo;

U8 IdentifyDrive(unsigned char DriveNo, tdefDriveInfo *DriveInfo) 
{
  unsigned int i; 
  U16 tv;
  U16 Buffer[256];

  WriteBYTE(1, 6, 0xA0 + (DriveNo ? 0x10:0x00)); // Select drive
  WriteBYTE(1, 1, 0);  
  WriteBYTE(1, 2, 1);  
  WriteBYTE(1, 3, 1);  
  WriteBYTE(1, 4, 0);  
  WriteBYTE(1, 5, 0);  
  WriteBYTE(1, 7, 0xEC);  

  
  while ((ReadBYTE(1,7) & 0x08)!=0x08); 

  // Two bytes at a time
  for (i=0; i<512; i+=2)   Buffer[i] = (*(volatile U16 *)0x4000002);

  // Extract drive info
  DriveInfo->Heads           = Buffer[6];
  DriveInfo->Tracks          = Buffer[2];
  DriveInfo->SectorsPerTrack = Buffer[12];

  // Swap bytes, because of 16 bit transfer...
  for (i=0; i<40; i+=2) 
  {
    tv = Buffer[54+i];
    DriveInfo->Model[i] = tv / 256;
    DriveInfo->Model[i+1] = tv % 256;
  }

  // Terminate string...
  DriveInfo->Model[40]='\0';

  Uart_Printf("Model         = %s\n\r", DriveInfo->Model);
  Uart_Printf("Heads         = %u\n\r", DriveInfo->Heads);
  Uart_Printf("Tracks        = %u\n\r", DriveInfo->Tracks);
  Uart_Printf("Sectors/Track = %u\n\r", DriveInfo->SectorsPerTrack);
  Uart_Printf("Model Size    = %uMB\n\r", DriveInfo->Heads * DriveInfo->Tracks * DriveInfo->SectorsPerTrack * 512 / 1024 / 1024);

  // Return the error register...
  return ReadBYTE(1, 1);
}


void WakeIDE()
{
  WriteBYTE(1, 6, 0xA0);
  while ((ReadBYTE(1,7) & 0xC0)!=0x40) ;  
}

void IdeTest()
{
  int i; 
  int a,b,c;
  U32 size;
  tdefDriveInfo DriveInfo;
  Uart_Printf("\n\rIDE Test...\n\r");
  Uart_Printf("Waiting for Initial DRDY & NOT BUSY from drive\n\r"); 
  WakeIDE();
  Uart_Printf("Spinning drive up (Going to IDLE mode)\n\r");
  Uart_Printf("SetMode=0x%0.2x\n\r", SetMode(0, 1, 1));
  Uart_Printf("Drive is ready!\n\r");

  IdentifyDrive(0, &DriveInfo);
//  i = ReadSector(0, 0, 0, 1);




//************************************************************************
//  WARNING:  NEXT CODE CAN DELETE HARD ALL DATAS.
//************************************************************************



/*
   size = 16 * 1010 * 51;

    for (a=1;a<16;a++)
   {
     for (b=1;b<1010;b++)
     {
       for (c=1;c<52;c++) 
        {
          WriteSector(0,a-1,b-1,c); 
          ReadSector(0, a-1, b-1, c);

//          Delay(2000);
        }
         Uart_Printf("%x ", b);
     
      }
}
*/


//  Uart_Printf("ReadSector=0x%0.2x\n\r", i);
  


  Delay(10000);   
}

int Main()
{
	rSYSCFG=CACHECFG;   // Using 8KB Cache//
	rINTCON=0x5;    //Non-vectored,IRQ enable,FIQ disable 
	rINTMOD=0x0;    //All=IRQ mode
	Port_Init();
	Lcd_DispOFF();
	Lcd_PowerUp();
	Led_Display(0x00);
	Delay(10000);
	Led_Display(0x07);
	Delay(5000);
	Led_Display(0x0);
         //-------------------------------------------------------------------------------
 	// Init LCD Contorl
	ClearVideoBuffer();
	rLCDCON1=(0)|(1<<5)|(0x3<<8)|(0x3<<10)|(10<<12);
	rLCDCON2=(LINEVAL)|(HOZVAL<<10)|(10<<21);  
	rLCDSADDR1= (0x00<<27) | ((0x0c000000>>22)<<21 ) | M5D(0x0c000000>>1);
	rLCDSADDR2= M5D( (((U32)0x0c000000+(LCD_XSIZE*LCD_YSIZE/8))>>1) ) | (13<<21);
	rLCDSADDR3= (LCD_XSIZE/16);
	rLCDCON1=(1)|(1<<5)|(0x3<<8)|(0x3<<10)|(10<<12);
	Intro();
	Lcd_DispON();
	//-------------------------------------------------------------------------------
 	Delay(50000);
	ClearIntro();
	Led_Display(0x00);
	Delay(10000);
	Led_Display(0x07);
	Delay(5000);
	Led_Display(0x0);

	mPosX = 154;
 	mPosY = 110;
	DrawMousePointer(mPosX, mPosY);

	Uart_Init(115200); 
	Uart_Printf("\n\rMOA Starting....");		

	//	KeyboardInitial();

    IdeTest();
/*	
 	ToMouse(0xFF);
 	if (ReadMouse() == 0xAA) ToMouse(0xFF);
 	if (ReadMouse() == 0xAA) ToMouse(0xFF);
 	ToMouse(0xF4);	
	ToMouse(0xEE);
*/
	while (1)
	{

//            mPosX = ReadMouse();
//           if (mPosX != 0)
//                Uart_Printf("\n\r %x", mPosX);

//		ProcessMouse();  
		ProcessKeyboard();

	};
	return 0;
}


void Port_Init(void)
{
    rPCONA=0x1ff;	

//    rPDATB=0x3ff;
//    rPCONB=0x3ff;
	rPDATB = 0x04f;
	rPCONB = 0x7cf;    

    rPDATC=0xffff;	//All I/O Is High
    rPCONC=0x0f05f555;	
    rPUPC=0x30c0;	//PULL UP RESISTOR should be enabled to I/O

    rPDATD=0xff;
    rPCOND=0xaaaa;	
    rPUPD=0xff;

    rPDATE=0x1ff;  	//All I/O Is High
    rPCONE=0x20428;	//All NC is INPUT	
    rPUPE=0x06;	//PE8 do not have programmable pull-up resistor.  

     rPDATF=0x1ff;	//All I/O Is High
    rPCONF=0x20900a;//All NC is INPUT
    rPUPF=0x163;

    rPDATG=0xff;
    rPCONG=0xC3FF;		
    rPUPG=0x0;		//should be enabled  
    
    rSPUCR=0x7; 	//D15-D0 pull-up disable
    
    rEXTINT=0x0;	//All EXTINT0-7 Low level interrupt
    rNCACHBE0=(((Non_Cache_End)>>12)<<16)|((Non_Cache_Start)>>12); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -