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

📄 hwaccess.c

📁 经过测试好用的读卡器源程序
💻 C
字号:
#include <ansi_c.h>
#include <userint.h>
#include "SMCanalyzer.h"
#include <utility.h>
#include <cvirte.h>		/* Needed if linking in external compiler; harmless otherwise */
#include "hwcfg.h"
#include "HwAccess.h"
#include "common.h"
extern struct ADDRESS Media;

void HwInitial(void)
{
	outp (ECRPORT,0x00);        // Configure Parallel to SPP mode
	outp (CONTROLPORT,0x04);	// Initialize control line
	outp (ECRPORT,0x80);        // Configure Parallel to EPP mode
	Hw_VccOff();
	Hw_chip_disable();
	Hw_write_disable();
}

unsigned char CheckReader(void)
{
	int i;
	outp (CONTROLPORT,0x04);	// output High level
	for (i=500;i;i--);
	if (!(inp (STATUSPORT) & READER_STATUS)) return 0;
	outp (CONTROLPORT,0x00);	// output Low level
	for (i=500;i;i--);
	if (inp (STATUSPORT) & READER_STATUS) return 0;
    outp (CONTROLPORT,0x04);	// output High level
    return 1;
}

void SoftReset_Smc(void)                              //Reset Flash
{
	short int i;
	Hw_cmd_latch_enable();
	Hw_OutData(Reset_cmd );          /*Reset command (ffh)*/
	Hw_cmd_latch_disable();
	for(i=0; i<1000; i++);		     /* Waiting for tRST */
}
/* Physical Page Read
   PageRead()
   Read 528 bytes from SmartMedia
*/
void PageRead(unsigned char *page_data, unsigned int page_add)
{
    unsigned char Adr1, Adr2, Adr3, Adr4;    // Startaddress pieces
	short int i;
    
    Adr1 = 0x00;
    Adr2 = (unsigned char)((page_add >> 9) & 0xFF);  	// Adr2 = A9-A16
    Adr3 = (unsigned char)((page_add >> 17) & 0xFF);  	// Adr3 = A24-A17
    if (info_flash[1] == 16) Adr3 &= 0x7F;            // set A24 to 0 if 16MB
    Adr4 = (unsigned char)((page_add >> 25) & 0x01);  	// Adr3 = A25
    SoftReset_Smc();
    Hw_chip_enable();
	Hw_cmd_latch_enable();
	Hw_OutData(Read1_cmd);   				/* Read Mode1 command (00h) */
	Hw_cmd_latch_disable();
	Hw_add_latch_enable();
	Hw_OutData(Adr1);
	Hw_OutData(Adr2);
	Hw_OutData(Adr3);
	if(info_flash[1]>=64)   Hw_OutData(Adr4);
	Hw_add_latch_disable();
	for(i=0; i<2000; i++);
	for(i=0;i<info_flash[4];i++) page_data[i]= Hw_InData();    /*Page data Read*/
	Hw_chip_disable();
}

short int StatusCheck(void)                              //status check
{
	short int i;
	unsigned char status;

	for(i=0; i<30000; i++){
		Hw_cmd_latch_enable();
		Hw_OutData(Read_Status1_cmd);    // 0x70
		Hw_cmd_latch_disable();
		status= Hw_InData();
		if (status == 0xc0) break;
	}
	if (status == 0xc0)      return  PASS;
	else 			 return  FAIL;
}


short int BlockErase(unsigned int page_add) 		// Erasing single block
{
    unsigned char Adr2, Adr3, Adr4;    // Startaddress pieces
	short int i;

    Adr2 = (unsigned char)((page_add >> 9) & 0xFF);  	// Adr2 = A9-A16
    Adr3 = (unsigned char)((page_add >> 17) & 0xFF);  	// Adr3 = A24-A17
    if (info_flash[1] == 16) Adr3 &= 0x7F;            // set A24 to 0 if 16MB
    Adr4 = (unsigned char)((page_add >> 25) & 0x01);  	// Adr3 = A25

    Hw_chip_enable();
    Hw_write_enable();
	Hw_cmd_latch_enable();
	Hw_OutData(Block_Erase_cmd);  			//Block Erase command (60h)
	Hw_cmd_latch_disable();
	Hw_add_latch_enable();
	Hw_OutData(Adr2);
	Hw_OutData(Adr3);
	if(info_flash[1]>=64)   Hw_OutData(Adr4);
	Hw_add_latch_disable();
	Hw_cmd_latch_enable();
	Hw_OutData(Block_Erase_Confirm_cmd);	/* Block Erase Confirm command (D0h) */
	Hw_cmd_latch_disable();
	if(StatusCheck()!=FAIL)	return PASS;
	else	                return FAIL;
}

/* Physical Page Write
   PageWrite()
   Write 528 bytes to SmartMedia from DataBuffer
*/
short int PageWrite(unsigned char *write_data,unsigned int page_add)
{
    unsigned char Adr1, Adr2, Adr3, Adr4;    // Startaddress pieces
	short int i;

    Adr1 = 0x00;
    Adr2 = (unsigned char)((page_add >> 9) & 0xFF);  	// Adr2 = A9-A16
    Adr3 = (unsigned char)((page_add >> 17) & 0xFF);  	// Adr3 = A24-A17
    if (info_flash[1] == 16) Adr3 &= 0x7F;            // set A24 to 0 if 16MB
    Adr4 = (unsigned char)((page_add >> 25) & 0x01);  	// Adr3 = A25
    SoftReset_Smc();
    Hw_chip_enable();
    Hw_write_enable();
	Hw_cmd_latch_enable();
	Hw_OutData(Serial_Data_Input_cmd);  			//Sequential Data Input command (80h)
	Hw_cmd_latch_disable();
	Hw_add_latch_enable();
	Hw_OutData(Adr1);
	Hw_OutData(Adr2);
	Hw_OutData(Adr3);
	if(info_flash[1]>=64)   Hw_OutData(Adr4);
	Hw_add_latch_disable();
	for(i=0; i<2000; i++);
	for(i=0;i<info_flash[4];i++) Hw_OutData(write_data[i]);   //Page data write
	Hw_cmd_latch_enable();
	Hw_OutData(Page_Program_True_cmd);	    //Page Program command (10h)
	Hw_cmd_latch_disable();

	if (StatusCheck() !=PASS)
	  {
	  printf("Invalid Blocks are detected");
      Hw_chip_disable();
	  Hw_write_enable();
	  return FAIL;
	  }
	Hw_chip_disable();
	Hw_write_enable();
	return PASS;

} 

unsigned int ReadID(void)		// Read ID
{
	unsigned int i,dent_bit;
	unsigned char m_code,d_code;

	Hw_chip_enable();
	Hw_cmd_latch_enable();
	Hw_OutData(ReadID1_cmd);   	/*Read ID command*/
	Hw_cmd_latch_disable();
	Hw_add_latch_enable();
	Hw_OutData(0x00);	    	/*1st address cycle*/
	Hw_add_latch_disable();
	m_code = Hw_InData();    /* Reading 2 byte data */
	d_code = Hw_InData();

	if(d_code==0xa4)	 				 dent_bit=4;
	else if(d_code==0x6e||d_code==0xec)	 dent_bit=8;
	else if(d_code==0xea||d_code==0x64)	 dent_bit=16;
	else if(d_code==0xe3||d_code==0xe5||d_code==0x6b)	 dent_bit=32;
	else if(d_code==0xe6||d_code==0x6d||d_code==0x70)	 dent_bit=64;
	else if(d_code==0x73||d_code==0xf2)	 dent_bit=128;
	else if(d_code==0xf4||d_code==0x75)	 dent_bit=256;
	else if(d_code==0xf7||d_code==0x76)	 dent_bit=512;
	else if(d_code==0xf8||d_code==0x79)	 dent_bit=1024;

	info_flash[0]= m_code;               //0xec;samsung,0x98:toshiba
	switch (dent_bit){
        case 8:
			info_flash[1]=1;       // device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
			info_flash[2]=256;     // blocks_disk		value: blocks in a disk
			info_flash[3]=16;      // pages_block		value: pages in a block
			info_flash[4]=264;      // page_size		value: page_size in bytes
			info_flash[5]=256;      // data_size		value: data_size in bytes
			info_flash[6]=8;        // spare_size		value: spare_size in bytes
			info_flash[7]=8;        // sector count		value: sectors per block
			info_flash[8]=0x0f;     // Max Address          value: ADD3 value
			info_flash[9]=0x00;     // Max Address          value: ADD4 value
			break;
		case 16:
			info_flash[1]=2;       // device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
			info_flash[2]=512;     // blocks_disk		value: blocks in a disk
			info_flash[3]=16;      // pages_block		value: pages in a block
			info_flash[4]=264;      // page_size		value: page_size in bytes
			info_flash[5]=256;      // data_size		value: data_size in bytes
			info_flash[6]=8;        // spare_size		value: spare_size in bytes
			info_flash[7]=8;        // sector count		value: sectors per block
			info_flash[8]=0x1f;     // Max Address          value: ADD3 value
			info_flash[9]=0x00;     // Max Address          value: ADD4 value
			break;
		case 32:
			info_flash[1]=4;       // device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
			info_flash[2]=512;     // blocks_disk		value: blocks in a disk
			info_flash[3]=16;      // pages_block		value: pages in a block
			info_flash[4]=528;      // page_size		value: page_size in bytes
			info_flash[5]=512;      // data_size		value: data_size in bytes
			info_flash[6]=16;        // spare_size		value: spare_size in bytes
			info_flash[7]=16;        // sector count	value: sectors per block
			info_flash[8]=0x1f;     // Max Address          value: ADD3 value
			info_flash[9]=0x00;     // Max Address          value: ADD4 value
			break;
		case 64:
			info_flash[1]=8;       // device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
			info_flash[2]=1024;     // blocks_disk		value: blocks in a disk
			info_flash[3]=16;      // pages_block		value: pages in a block
			info_flash[4]=528;      // page_size		value: page_size in bytes
			info_flash[5]=512;      // data_size		value: data_size in bytes
			info_flash[6]=16;        // spare_size		value: spare_size in bytes
			info_flash[7]=16;        // sector count	value: sectors per block
			info_flash[8]=0x3f;
			info_flash[9]=0x00;     // Max Address          value: ADD4 value
			break;
		case 128:
			info_flash[1]=16;       // device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
			info_flash[2]=1024;     // blocks_disk		value: blocks in a disk
			info_flash[3]=32;      // pages_block		value: pages in a block
			info_flash[4]=528;      // page_size		value: page_size in bytes
			info_flash[5]=512;      // data_size		value: data_size in bytes
			info_flash[6]=16;        // spare_size		value: spare_size in bytes
			info_flash[7]=32;        // sector count	value: sectors per block
			info_flash[8]=0x7f;     // Max Address          value: ADD3 value
			info_flash[9]=0x00;     // Max Address          value: ADD4 value
			break;
		case 256:
			info_flash[1]=32;       // device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
			info_flash[2]=2048;     // blocks_disk		value: blocks in a disk
			info_flash[3]=32;      // pages_block		value: pages in a block
			info_flash[4]=528;      // page_size		value: page_size in bytes
			info_flash[5]=512;      // data_size		value: data_size in bytes
			info_flash[6]=16;        // spare_size		value: spare_size in bytes
			info_flash[7]=32;        // sector count	value: sectors per block
			info_flash[8]=0xff;     // Max Address          value: ADD3 value
			info_flash[9]=0x00;     // Max Address          value: ADD4 value
			break;
		case 512:
			info_flash[1]=64;       // device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
			info_flash[2]=4096;     // blocks_disk		value: blocks in a disk
			info_flash[3]=32;      // pages_block		value: pages in a block
			info_flash[4]=528;      // page_size		value: page_size in bytes
			info_flash[5]=512;      // data_size		value: data_size in bytes
			info_flash[6]=16;        // spare_size		value: spare_size in bytes
			info_flash[7]=32;        // sector count	value: sectors per block
			info_flash[8]=0xff;     // Max Address(UNKNOWN) value: ADD3 value
			info_flash[9]=0x01;     // Max Address          value: ADD4 value
			break;
		case 1024:
			info_flash[1]=128;       // device_type		1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
			info_flash[2]=8192;     // blocks_disk		value: blocks in a disk
			info_flash[3]=32;      // pages_block		value: pages in a block
			info_flash[4]=528;      // page_size		value: page_size in bytes
			info_flash[5]=512;      // data_size		value: data_size in bytes
			info_flash[6]=16;        // spare_size		value: spare_size in bytes
			info_flash[7]=32;        // sector count	value: sectors per block
			info_flash[8]=0xff;     // Max Address(UNKNOWN) value: ADD3 value
			info_flash[9]=0x03;     // Max Address          value: ADD4 value
			break;
		default:
			for(i=0;i<10;i++) 	info_flash[i]=0x00; //Unknown device type and density
			dent_bit=0xff;
	}
	return (dent_bit);               // Returning to device density
}

⌨️ 快捷键说明

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