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

📄 ideio.c

📁 基于UCOS-II制作的MP3
💻 C
字号:
/*C**************************************************************************
* NAME:         ideio.c         
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE:      snd1c-refd-nf-4_0_3      
* REVISION:     1.21     
*----------------------------------------------------------------------------
* PURPOSE:
* 
*****************************************************************************/


/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h"                         /* system configuration */
#include "ideio.h"
#include "..\usb\usb_drv.h"                 /* usb driver definition */
#include "lib_mcu\lcd\lcd_drv.h"                /* c51 driver definition */
#include "lib_mcu\serial\serial.h"

#define Usb_clear_RXOUTB0()  {if(UEPSTAX & 0x42)  UEPSTAX &= 0xbd;}

extern  bdata bit     gl_hdd_buffer_flag;
extern  xdata Byte    gl_hdd_buffer[512];
extern  xdata uint    gl_hdd_buffer_ptr; //hdd_buffer[]的序列号
extern data    Uint32 dCBWDataTransferLength;
extern  data  Uint16  hdd_data_length;

data  Uint32          hdd_current_sector;

/*bit  ata_install(void);
bit  hdd_uninstall(void);
bit hdd_read_open(Uint32);
bit hdd_read_close(void);
Byte hdd_read_byte(void);
bit hdd_write_open(Uint32);
bit hdd_write_close(void);
bit hdd_write_byte(Byte);
bit hdd_read_long_big_endian(void);
bit hdd_read_one_sector(void);
bit hdd_write_one_sector(void);
s_format *hdd_format(void);
extern  Uint32 (hdd_mem_size);*/


/**********************************************************
*函数名称:void DriveID(void)                              *
*用途:读硬盘ID                                            *
***********************************************************/ 
/*void DriveID(void) 
{uint i=512;
 do{ACC=FILE7;}while(BSY);
    FILE7=0xec;
 do{ACC=FILE7;}while(BSY|!DRQ);
 while(i){ReadData();i-=2;}
}*/

extern  void usb_mass_storage_csw (void);


/*********************************************************
*函数名称:WriteCommand(command)                          *
*函数用途:写命令                                         *
**********************************************************/
WriteCommand(command)
{do{ACC = FILE7;}while(BSY);
 FILE7 = command;
}

/**********************************************************
*函数名称:bit  ata_install(void)                          *
*用途:初始化硬盘                                          *
***********************************************************/
bit  ata_install(void)
{
 ARST = 0;
 ARST = 1;

 AUXR = 0x5c;         //设置EXT16=1,使能16-BIT传送模式

 do{FILE6 = 0xa0; ACC = FILE7;} while(!DRDY | BSY);
    FILE6 = 0x20;
    FILE2 = 64;
    FILE7 = 0x91;
 do{ACC = FILE7;} while(BSY);
    FILE7 = 0x10;
 do{ACC = FILE7;} while(BSY); 

return OK;
}

/**********************************************************
*函数名称:HardDiskSleep(void)                             *
*用途:硬盘休眠                                            *
***********************************************************/
void HardDiskSleep(void)
{
 do{ACC = FILE7;} while(BSY); //wait for NO busy
 WriteCommand(0xe0);   //write command code
 do{ACC = FILE7;} while(BSY |! DRQ); //wait for noBUSY and data request
}
/**********************************************************
*函数名称:bit hdd_read_open(Uint32 hdd_sector)            *
*用途:读硬盘OPEN动作                                      *
***********************************************************/ 
bit hdd_read_open(Uint32 hdd_sector)
{   
   uint i = 0;
   uchar head,cylinderh,cylinderl,sector;
   hdd_current_sector = hdd_sector;
   head      = ((Byte*)&hdd_current_sector)[0];
   cylinderh = ((Byte*)&hdd_current_sector)[1];
   cylinderl = ((Byte*)&hdd_current_sector)[2];
   sector    = ((Byte*)&hdd_current_sector)[3];
   do{ACC = FILE7;} while(BSY); //wait for NO busy
     FILE6 = (0xe0 | (head & 0x0f)); 
     FILE5 = cylinderh;    //high 2-bit cylinder
     FILE4 = cylinderl;    //low 8-bit cylinder
     FILE3 = sector;       //start sector
     FILE2 = 0x01;         //sector counter
     WriteCommand(0x20);   //write command code
 
   do{ACC = FILE7;} while(BSY |! DRQ); //wait for noBUSY and data request

   ACC = FILE7;
   if (ERR)
     return KO;

   for (i = 0; i<512;)
      {
	   gl_hdd_buffer[i++]=FILE0;
       gl_hdd_buffer[i++]=DAT16H;
      }

      gl_hdd_buffer_ptr = 0;
	  gl_hdd_buffer_flag = 1;
	  return OK;

}


/**********************************************************
*函数名称:Byte hdd_read_byte(void)                        *
*用途:读硬盘缓存中的一个字节                              *
***********************************************************/ 
Byte hdd_read_byte(void)
{
 gl_hdd_buffer_ptr++;
 
 if (gl_hdd_buffer_ptr > 512)  
   {
    gl_hdd_buffer_ptr = 0;
    hdd_current_sector +=1;
    hdd_read_open(hdd_current_sector);
    gl_hdd_buffer_ptr++;
   }

 return (gl_hdd_buffer[gl_hdd_buffer_ptr - 1]);
}


/**********************************************************
*函数名称:bit hdd_read_close(void)                        *
*用途:读硬盘CLOSE动作                                     *
***********************************************************/ 
bit hdd_read_close(void)
{  
   gl_hdd_buffer_flag = 0;
   gl_hdd_buffer_ptr = 0;
   return OK;
}


/**********************************************************
*函数名称:bit hdd_read_one_sector(Uint32 hdd_sector)      *
*用途:将硬盘中一个扇区的数据送入USB接口                   *
***********************************************************/ 
bit hdd_read_one_sector(Uint32 hdd_sector)
{Byte i;

 if (hdd_read_open(hdd_sector) == OK)
 {
  for (i = 0; i < 8; i++)
     {
      Usb_select_ep(EP_IN);
                      /* read 64 bytes from card */
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
        Usb_write_byte(hdd_read_byte());        
        Usb_write_byte(hdd_read_byte());
     
      Usb_set_TXRDY();
      Usb_select_ep(EP_IN);
      while (!Usb_tx_complete());
      Usb_clear_TXCMPL();  
     }	 
     hdd_read_close();

     return OK;
   }
  return KO;
}


/**********************************************************
*函数名称:bit hdd_write_open(Uint32 hdd_sector)           *
*用途:写硬盘OPEN动作                                      *
***********************************************************/ 
bit hdd_write_open(Uint32 hdd_sector)
{
 uchar head,cylinderh,cylinderl,sector;
   head      = ((Byte*)&hdd_sector)[0];
   cylinderh = ((Byte*)&hdd_sector)[1];
   cylinderl = ((Byte*)&hdd_sector)[2];
   sector    = ((Byte*)&hdd_sector)[3];

   do{ACC = FILE7;} while(BSY);
    FILE6 = (0xe0 | (head & 0x0f));
    FILE5 = cylinderh;    //high 2-bit cylinder
    FILE4 = cylinderl;//low 8-bit cylinder
    FILE3 = sector; //start sector
    FILE2 = 1;   //sector counter
    
    WriteCommand(0x30); 

  do{ACC = FILE7;} while(BSY |! DRQ); //wait for noBUSY and data request
  
  ACC = FILE7;
   if (ERR)
     return KO;

     return OK;
}


/**********************************************************
*函数名称:bit hdd_write_byte(Byte hdd_data)                      *
*用途:写硬盘CLOSE动作                                     *
***********************************************************/ 
bit   hdd_write_byte(Byte hdd_data)
{
 gl_hdd_buffer[gl_hdd_buffer_ptr++] = hdd_data ;
 return OK;
}

/**********************************************************
*函数名称:bit hdd_write_close(void)                       *
*用途:写硬盘CLOSE动作                                     *
***********************************************************/ 
bit hdd_write_close(void)
{uint i;
  for(i = 0; i < 512; i+=2)
   {
    DAT16H = gl_hdd_buffer[i+1];
    FILE0  = gl_hdd_buffer[i];	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   
   }

  do{ACC = FILE7;} while(BSY); 

  return OK;
   
}



/**********************************************************
*函数名称:bit hdd_write_usb_sector(Uint32 hdd_sector)     *
*用途:将USB接收到的一个扇区的数据写入硬盘                 *
***********************************************************/ 
bit hdd_write_usb_sector(Uint32 hdd_sector)
{uchar j;

   Usb_select_ep(EP_OUT);   
   gl_hdd_buffer_ptr = 0;

   for (j = 0; j < 8; j++)
       {
	    while(!Usb_rx_complete());          /* wait end of reception */
		  hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
	      hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
	      hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());
          hdd_write_byte(Usb_read_byte());

	      Usb_clear_RXOUT_PP();	     
        }
  
    hdd_write_open(hdd_sector);
    hdd_write_close();
	return OK;
}


s_format *hdd_format()
{
 code  s_format  hdd_tab_format=
   /* nb_cylinder, nb_head,  nb_sector, nb_hidden, nb_sector_per_cluster */
    { 1000, (Byte)16, (Byte)32,  (Byte)63,  (Byte)32 };

 return &hdd_tab_format;
 }




     
    





⌨️ 快捷键说明

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