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

📄 compactflash.c

📁 ARM S3C2410处理器 CF卡 驱动程序 开发环境:ads1.2
💻 C
字号:
/*
*********************************************************
* Copyright (c)
* All rights reserved.				            
*
* 文件名称:compactflash.c
* 文件标识:
* 摘    要:S3C2410 cf card Test Program
* 当前版本:1.0
* 作    者:刘征
* 完成日期:2005.4.3
*
* 取代版本:
* 作    者:
* 完成日期:
*********************************************************
*/

/*
*********************************************************
*   					 头文件
*********************************************************
*/
#include "2410addr.h"
#include "2410lib.h"
#include "compactflash.h"

/*
*********************************************************
*  					  宏定义
*********************************************************
*/
#define DEBUG

/*
*********************************************************
*   					变量
*********************************************************
*/
unsigned long globe_statusflag=0;
unsigned char compactFlashBuffer[512];  
unsigned short IdentifyBuffer[256];

/*
*********************************************************
* 函数介绍:本函数是CF CARD中断处理函数		
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
static void __irq compactFlashInt_Net(void)
{
	if(rEINTPEND == (1<<5))
	{
	    globe_statusflag = STATUS_REG;
		rEINTPEND = (1<<5);
	}
    ClearPending(BIT_EINT4_7);
}

/*
*********************************************************
* 函数介绍:本函数CF CARD初始化程序(上电即复位)。			
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void compactFlashInit(void)
{
	rGPFCON = (rGPFCON & 0x0000)|(1<<11);//PF5=EINT5
	
	rEXTINT0 = (rEXTINT0 & ~(7<<20)) | 0x1<<20; //EINT5=high level triggered
	
	//设置中断入口函数
    pISR_EINT4_7 = (U32)compactFlashInt_Net;
    
    rEINTPEND = 0xffffff;
    rEINTMASK=~(1<<5);
    ClearPending(BIT_EINT4_7);
    rINTMSK=~(BIT_EINT4_7);//开中断
}

/*
*********************************************************
* 函数介绍:本函数CF CARD写缓存程序(Writes the local to the buffer of the card)。			
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void compactFlashWriteBuffer(void)
{
	int i;
	unsigned short temp;//数据寄存器是16位读取的
	
	for(i=0; i<512; i+=2)
	{
		temp = compactFlashBuffer[i+1];
		temp <<= 8;
		temp += compactFlashBuffer[i];
		DATA_REG = temp;
	}
}

/*
*********************************************************
* 函数介绍:本函数CF CARD读缓存程序(Reads the buffer of the card and stores it to the local buffer)。			
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void compactFlashReadBuffer(void)
{
	int i;
	unsigned short temp;//数据寄存器是16位读取的

	for(i=0; i<512; i+=2)
	{
		temp = DATA_REG;
		compactFlashBuffer[i] = (unsigned char)temp;
		compactFlashBuffer[i+1] = (unsigned char)(temp>>8);
	}
}

/*
*********************************************************
* 函数介绍:本函数清临时缓存程序(Clears the local buffer)。			
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void compactFlashClearBuffer(void)
{
	int i;
	
	for(i=0; i<512; i++)
	{
		compactFlashBuffer[i] = 0;
	}
}

/*
*********************************************************
* 函数介绍:本函数CF CARD 鉴别程序(Sends the Indentify Drive command to the card)。			
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void compactFlashIdentify(void)
{
	int i;
	
	COMMAND_REG = IDENTIFY;//发识别命令
	do
	{
		globe_statusflag = STATUS_REG;//读状态寄存器,判断前一操作是否有错;不使用中断处理时,使用本语句。
		if((globe_statusflag & 0x01) == 0x01)
		{
			#ifdef DEBUG
				Uart_Printf("Before act is error!\n");
			#endif
			return;
		}
	}while(globe_statusflag != 0x58);
	for(i=0; i<256; i++)
	{
		IdentifyBuffer[i] = DATA_REG;
	}
	#ifdef DEBUG
		Uart_Printf("\nIdentifing Drive...\n");
		Uart_Printf("General Configuration: \n");
		Uart_Printf("0x%x, \n",IdentifyBuffer[0]);
		Uart_Printf("Buffertype: \n");
	    Uart_Printf("0x%x, \n",IdentifyBuffer[20]);
		Uart_Printf("Serial number: \n");
		for(i=10; i<20; i++)
		{
			Uart_Printf("%c,",(unsigned char)(IdentifyBuffer[i]>>8));
			Uart_Printf("%c,",(unsigned char)IdentifyBuffer[i]);
		}
		Uart_Printf("\nFirmware revision:\n");
		for(i=23; i<27; i++)
		{
			Uart_Printf("%c,",(unsigned char)(IdentifyBuffer[i]>>8));
			Uart_Printf("%c,",(unsigned char)IdentifyBuffer[i]);
		}
	#endif
}

/*
*********************************************************
* 函数介绍:本函数CF CARD 诊断程序(Sends the Drive Diagnostic command to the card)。			
* 输入参数:无
* 输出参数:无
* 返回值  :returns the error number
*********************************************************
*/
char compactFlashDiagnostic(void)
{
	COMMAND_REG = DIAGNOSTIC;//发诊断命令
    return ERROR_REG;
}

/*
*********************************************************
* 函数介绍:本函数CF CARD 块读程序。(Sends the Read Sector command to the card, operates in logical block mode)
* 输入参数:blockHigh:  first byte of the 3 block address bytes
* 			blockMiddle:   middle byte...
* 			blockLow:   last byte...
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void compactFlashReadBlock(char blockHigh, char blockMiddle, char blockLow)
{	
    int i;
    
	HEAD_REG = 0xE0;//使用逻辑地址
    BLOCKHIGH_REG = blockHigh;
    BLOCKMIDDLE_REG = blockMiddle;
    BLOCKLOW_REG = blockLow;
    BLOCKCOUNT_REG = 1;// TODO: block count is always 1 because it makes no sense to read multible blocks, now!
    COMMAND_REG = READ_BLOCK;//发读命令
    
    do
	{
		globe_statusflag = STATUS_REG;//读状态寄存器,判断前一操作是否有错;不使用中断处理时,使用本语句。
		if((globe_statusflag & 0x01) == 0x01)
		{
			#ifdef DEBUG
				Uart_Printf("Before act is error!\n");
			#endif
			return;
		}
	}while(globe_statusflag != 0x58);	
	compactFlashReadBuffer();//读一扇区数据
	
	#ifdef DEBUG
		for(i = 0; i < 512; i++)
		{
			Uart_Printf("%c",compactFlashBuffer[i]);
		}
	#endif
}

/*
*********************************************************
* 函数介绍:本函数CF CARD 块写程序。(Sends the Write Sector command to the card, operates in logical block mode)
* 输入参数:blockHigh:  first byte of the 3 block address bytes
* 			blockMiddle:   middle byte...
* 			blockLow:   last byte...
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void compactFlashWriteBlock(char blockHigh, char blockMiddle, char blockLow)
{
	compactFlashBuffer[0] = 'H';
	compactFlashBuffer[1] = 'e';
	compactFlashBuffer[2] = 'l';
	compactFlashBuffer[3] = 'l';
	compactFlashBuffer[4] = 'o';
	compactFlashBuffer[5] = ' ';
	compactFlashBuffer[6] = 'W';
	compactFlashBuffer[7] = 'o';
	compactFlashBuffer[8] = 'r';
	compactFlashBuffer[9] = 'l';
	compactFlashBuffer[10] = 'd';
	compactFlashBuffer[11] = '!';
	#ifdef DEBUG
		Uart_Printf("write 'HelloWorld!'\n");
	#endif
	HEAD_REG = 0xE0;//使用逻辑地址
    BLOCKHIGH_REG = blockHigh;
    BLOCKMIDDLE_REG = blockMiddle;
    BLOCKLOW_REG = blockLow;
    BLOCKCOUNT_REG = 1;// TODO: block count is always 1 because it makes no sense to read multible blocks, now!
    COMMAND_REG = WRITE_BLOCK;//发写命令
    do
	{
		globe_statusflag = STATUS_REG;//读状态寄存器,判断前一操作是否有错
		if((globe_statusflag &0x01) == 0x01)
		{
			#ifdef DEBUG
				Uart_Printf("Before act is error!\n");
			#endif
			return;
		}
	}while(globe_statusflag != 0x58);
	compactFlashWriteBuffer();//写一扇区数据
	do
	{
	    globe_statusflag = STATUS_REG;//读状态寄存器,判断前一操作是否有错;不使用中断处理时,使用本语句。
	    //读状态寄存器,判断前一操作是否有错
		if((globe_statusflag &0x20) == 0x20)
		{
			#ifdef DEBUG
				Uart_Printf("Write fault!\n");
			#endif
			return;
		}
	}while(globe_statusflag != 0x50);
}


⌨️ 快捷键说明

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