这是俺写得AT45DB321C的程序, 正常工作中, 给你贴上, 看看对你有帮助没.
/************************************************************************************/ /*! Copyright (c) 2006, cabletech,All rights reserved. * * @brief Flash相关操作文件 * * 开发平台 C8051F023. * 开发工具 Keil C51. * * @file Flash.c * * @author XiQingPing * @date 10/06/2006 * @version 1.0 */ /************************************************************************************/ /************************************************************************************/ // 导入头文件. /************************************************************************************/ #include "Target.h" #include "flash.h"
/************************************************************************************/ // 函数定义. /************************************************************************************/
/****************************************************************************/ /*! * @fn void Flash_ReadSecurityString(unsigned char *p) * * @brief Flash读取加密字符串 * * @param unsigned char *p 字符串存放地址 * * @return 无 */ /****************************************************************************/ void Flash_ReadSecurityString(unsigned char *p) reentrant { unsigned char i; ENABLE_FLASH; Target_SPIByte(0x77); Target_SPIByte(0x00); Target_SPIByte(0x00); Target_SPIByte(0x00); Target_SPIByte(0xFF); Target_SPIByte(0xFF); Target_SPIByte(0xFF); Target_SPIByte(0xFF); for(i=0;i<128;i++) { *p++ = Target_SPIByte(0xFF); } DISABLE_FLASH; }
/****************************************************************************/ /*! * @fn void Flash_ReadString(unsigned int pageaddr, unsigned int byteaddr, unsigned char *strp,unsigned char len) * * @brief 从flash中读取长度为len的字符串 * * @param pageaddr 要读入flash的page号码 * @param byteaddr 要读入的flash的page内的地址 * @param strp 用于存放读出的字符串 * @param len 要读出的字符串长度参数 * * @return 无 */ /****************************************************************************/ void Flash_ReadString(unsigned int pageaddr, unsigned int byteaddr, unsigned char *strp,unsigned char len) reentrant { // unsigned char addr[3]; #if (BYTES_PER_PAGE == 512) pageaddr = pageaddr << 1; #else pageaddr = pageaddr << 2; #endif // addr[2] = HighByte(pageaddr); // addr[1] = LowByte(pageaddr)+HighByte(byteaddr); // addr[0] = (unsigned char)byteaddr; ENABLE_FLASH; Target_SPIByte(0xE8); ///Continuous Array Read // Target_SPIByte(addr[2]); // Target_SPIByte(addr[1]); // Target_SPIByte(addr[0]); Target_SPIByte(HighByte(pageaddr)); Target_SPIByte(LowByte(pageaddr)+HighByte(byteaddr)); Target_SPIByte((unsigned char)byteaddr); Target_SPIByte(0xFF); Target_SPIByte(0xFF); Target_SPIByte(0xFF); Target_SPIByte(0xFF); for(; len; len--) { *strp++ = Target_SPIByte(0xFF); } // *strp = 0; DISABLE_FLASH; return; } /****************************************************************************/ /*! * @fn unsigned char Flash_ReadByte(unsigned int pageaddr, unsigned int byteaddr) * * @brief Flash读取字节函数程序 * * @param pageaddr 要读入flash的page号码 * @param byteaddr 要读入的flash的page内的地址 * * @return 返回读出的字节数据 */ /****************************************************************************/ unsigned char Flash_ReadByte(unsigned int pageaddr, unsigned int byteaddr) reentrant { unsigned char rc; Flash_ReadString(pageaddr,byteaddr,&rc,1); return rc; }
/****************************************************************************/ /*! * @fn void Flash_WriteString(unsigned int pageaddr, unsigned int byteaddr, unsigned char *strp, unsigned int len) * * @brief Flash写入字符串函数程序 * * @param pageaddr 要写入flash的page号码 * @param byteaddr 要写入的flash的page内起始地址 * @param strp 要写入flash中的字符串的起始地址 * @param len 要写入flash中的字符串的长度 * * @return 无 */ /****************************************************************************/ void Flash_WriteString(unsigned int pageaddr, unsigned int byteaddr, unsigned char *strp, unsigned int len) reentrant { unsigned char addr[3]; #if (BYTES_PER_PAGE == 512) pageaddr = pageaddr << 1; #else pageaddr = pageaddr << 2; #endif addr[2] = HighByte(pageaddr); addr[1] = LowByte(pageaddr)+HighByte(byteaddr); addr[0] = (unsigned char)byteaddr; /**Program the first page, Maybe not whole page*/ ENABLE_FLASH; ///Main Memory to Buffer 1 transfer. Target_SPIByte(0x53); Target_SPIByte(addr[2]); Target_SPIByte(addr[1]); Target_SPIByte(addr[0]); DISABLE_FLASH; BusyWait(); ENABLE_FLASH; Target_SPIByte(0x82); Target_SPIByte(addr[2]); Target_SPIByte(addr[1]); Target_SPIByte(addr[0]); for(; len; len--) Target_SPIByte(*strp++); DISABLE_FLASH; BusyWait(); return; } /****************************************************************************/ /*! * @fn void Flash_WriteByte(unsigned int pageaddr, unsigned int byteaddr, unsigned char databyte) * * @brief 向指定位置的flash中写入一个字节数据 * * @param pageaddr 要读入flash的page号码 * @param byteaddr 要读入的flash的page内的地址 * @param databyte 要写入的字符数据 * * @return 无 */ /****************************************************************************/ void Flash_WriteByte(unsigned int pageaddr, unsigned int byteaddr, unsigned char databyte)reentrant { Flash_WriteString(pageaddr,byteaddr,&databyte,1); }
/****************************************************************************/ /*! * @fn void Flash_ErasePage(unsigned int pageaddr) * * @brief Flash按页擦除 * * @param pageaddr 要写入flash page的起始号码 * * @return 无 */ /****************************************************************************/ void Flash_ErasePage(unsigned int pageaddr) reentrant { unsigned char addr[3]; #if (BYTES_PER_PAGE == 512) pageaddr = pageaddr << 1; #else pageaddr = pageaddr << 2; #endif addr[2] = HighByte(pageaddr); addr[1] = LowByte(pageaddr); addr[0] = 0; ENABLE_FLASH; Target_SPIByte(0x81); Target_SPIByte(addr[2]); Target_SPIByte(addr[1]); Target_SPIByte(addr[0]); DISABLE_FLASH; BusyWait(); return; }
/****************************************************************************/ /*! * @fn void Flash_Memory2Buffer(unsigned int pageaddr) * * @brief 读Flash的数据到缓冲区 * * @param pageaddr 要写入flash page的起始号码 * * @return 无 */ /****************************************************************************/ void Flash_Memory2Buffer(unsigned int pageaddr) reentrant { unsigned char addr[3]; #if (BYTES_PER_PAGE == 512) pageaddr = pageaddr << 1; #else pageaddr = pageaddr << 2; #endif addr[2] = HighByte(pageaddr); addr[1] = LowByte(pageaddr); addr[0] = 0; ENABLE_FLASH; Target_SPIByte(0x53); Target_SPIByte(addr[2]); Target_SPIByte(addr[1]); Target_SPIByte(addr[0]); DISABLE_FLASH; BusyWait(); return; }
/****************************************************************************/ /*! * @fn void Flash_MemoryWriteString(unsigned int byteaddr,unsigned char *p,unsigned char len) * * @brief Flash中写入字符串 * * @param byteaddr 要写入flash 的起始地址 * @param p 要写入flash中的数据的起始地址 * @param len 要写入flash中的数据的长度 * * @return 无 */ /****************************************************************************/ void Flash_MemoryWriteString(unsigned int byteaddr,unsigned char *p,unsigned char len) reentrant { unsigned char addr[3]; addr[2] = 0; addr[1] = HighByte(byteaddr); addr[0] = (unsigned char)byteaddr; ENABLE_FLASH; Target_SPIByte(0x84); Target_SPIByte(addr[2]); Target_SPIByte(addr[1]); Target_SPIByte(addr[0]); for(;len>0;len--) { Target_SPIByte(*p++); } DISABLE_FLASH; return; }
/****************************************************************************/ /*! * @fn void Flash_Buffer2Memory(unsigned int pageaddr) * * @brief Flash从缓冲区写入 * * @param pageaddr 要写入flash page的起始号码 * * @return 无 */ /****************************************************************************/ void Flash_Buffer2Memory(unsigned int pageaddr) reentrant { unsigned char addr[3]; #if (BYTES_PER_PAGE == 512) pageaddr = pageaddr << 1; #else pageaddr = pageaddr << 2; #endif addr[2] = HighByte(pageaddr); addr[1] = LowByte(pageaddr); addr[0] = 0; ENABLE_FLASH; Target_SPIByte(0x83); Target_SPIByte(addr[2]); Target_SPIByte(addr[1]); Target_SPIByte(addr[0]); DISABLE_FLASH; BusyWait(); return; } /***********************************************************************************/ // 文件结束. /***********************************************************************************/ |