#include "use.h"
//********************************************************
extern void Set_FW25_CS( unsigned char en);
extern unsigned char SPI1_ReadWriteByte(unsigned char TxData);
FLASH_INFO FLASH_info;
//-------------------------------------------------------------------------------------------------------
/* 宏,用于定义SST25VF016B的片选引脚 */
#define SST25V16_CS1 (GPIO_Pin_4) //PA04
//********************************************************************************************************************
#define SPI_FLASH_CS(x) ((x>0)? (GPIO_SetBits(GPIOA, SST25V16_CS1)) : (GPIO_ResetBits(GPIOA, SST25V16_CS1) ))//设置1,0
//**********************************************************************************************
//修改状态寄存器,允许芯片存储器被写
unsigned char SST25_SPI_Flash_Init(void){
//unsigned int SST25_ID;
SPI1_Config(); //初始化SPI
SST25_ReadID();
SST25_Page_Program_SR(0x02); //使能状态寄存器中的写存储器
return 0; //0k
}
//***************************************************************
unsigned char Flash_ReadWriteByte(unsigned char data){
return SPI1_ReadWriteByte(data);
}
//***************************************************************
//读取SPI_FLASH的状态寄存器
//BIT7 6 5 4 3 2 1 0
//SPR RV TB BP2 BP1 BP0 WEL BUSY
//SPR:默认0,状态寄存器保护位,配合WP使用
//TB,BP2,BP1,BP0:FLASH区域写保护设置
//WEL:写使能锁定
//BUSY:忙标记位(1,忙;0,空闲)
//默认:0x00
unsigned char SST25_Page_ReadSR(void){
unsigned char byte=0;
SPI_FLASH_CS(0); //使能器件
Flash_ReadWriteByte(SST25_ReadStatusReg); //发送读取状态寄存器命令
byte=Flash_ReadWriteByte(0Xff); //读取一个字节
SPI_FLASH_CS(1); //取消片选
return byte;
}
//*************************************************************************
//写SPI_FLASH状态寄存器
//只有SPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)可以写!!!
void SST25_Page_Program_SR(unsigned char sr){
SPI_FLASH_CS(0); //片选
Flash_ReadWriteByte(SST25_EnableWriteStatusReg); //使能写状态寄存器命令
SPI_FLASH_CS(1); //取消片选
SPI_FLASH_CS(0); //片选
Flash_ReadWriteByte(SST25_WriteStatusReg); //发送写取状态寄存器命令
Flash_ReadWriteByte(sr); //写入一个字节
SPI_FLASH_CS(1); //取消片选
}
//****************************************************************************
//SPI_FLASH写使能
//将WEL置位
void SST25_Page_Program_Enable(void){
SPI_FLASH_CS(0); //使能器件
Flash_ReadWriteByte(SST25_WriteEnable); //发送写使能
SPI_FLASH_CS(1); //取消片选
}
//****************************************************************************
//SPI_FLASH写禁止
//将WEL清零
void SST25_Page_Program_Disable(void){
SPI_FLASH_CS(0); //使能器件
Flash_ReadWriteByte(SST25_WriteDisable); //发送写禁止指令
SPI_FLASH_CS(1); //取消片选
}
//*********************************************************************************
//发送程序 COM=命令;adr=地址
void SPI_SendAdrOut(unsigned char com,unsigned int adr){
Flash_ReadWriteByte(com); /* 发送字节数据烧写命令 */
Flash_ReadWriteByte((adr & 0xFFFFFF) >> 16); /* 发送3个字节的地址信息 */
Flash_ReadWriteByte((adr & 0xFFFF) >> 8);
Flash_ReadWriteByte(adr & 0xFF);
}
//****************************************************************************
//读取芯片ID SST25VF016的是 0XBF41 sst25vf064_ID=bf4b
//文帮的是0xEF17
unsigned int SST25_ReadID(void){
unsigned int Temp = 0;
SPI_FLASH_CS(0);
SPI_SendAdrOut(SST25_ManufactDeviceID,0); //发送读取ID命令
//读取返回的16位值
Temp =Flash_ReadWriteByte(0xFF)<<8; //高8位数据
Temp +=Flash_ReadWriteByte(0xFF); //底八位数据
SPI_FLASH_CS(1);
return Temp;
}
//****************************************************************************
//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535即64k)
unsigned int SST25_Page_Read(unsigned int ReadAddr,unsigned char* pBuffer,unsigned int NumByteToRead){
unsigned int i;
//计算物理地址?? 2014-1-20
//if ((ReadAddr+NumByteToRead) >(SST25_MAX_SECTORS*SST25_SECTOR_SIZE)) return 5; //地址错误!
SPI_FLASH_CS(0); //使能器件
SPI_SendAdrOut(SST25_FastReadData,ReadAddr); //发送读取命令
Flash_ReadWriteByte(0XFF); //发送一个哑数据!
for(i=0;i<NumByteToRead;i++) pBuffer[i]=Flash_ReadWriteByte(0XFF); //循环读数
SPI_FLASH_CS(1); //取消片选
return 0;
}
//****************************************************************************
//写入扇区已经擦除干净, NumByteToWrite 最好是8,16,32,64,128,256
//pBuffer:为待写数据组
//WriteAddr:所写数据的起始地址
//NumByteToWrite:所要写的数据的长度
unsigned int SST25_Page_Write(unsigned char* pBuffer,unsigned int WriteAddr,unsigned int NumByteToWrite){
unsigned int i;
if(NumByteToWrite==0) return 0;
SST25_Page_Program_Enable(); //SET WEL
SPI_FLASH_CS(0);
SPI_SendAdrOut(SST25_ByteProgram,WriteAddr); //发送写页命令
//发送待写的数据
for(i=0;i<NumByteToWrite;i++) Flash_ReadWriteByte(*pBuffer++);
SPI_FLASH_CS(1);
SST25_Wait_Busy();//等待写完成
return NumByteToWrite;
}
//****************************************************************************
//写入扇区已经擦除干净, NumByteToWrite 最好是8,16,32,64,128,256
//pBuffer:为待写数据组
//WriteAddr:所写数据的起始地址
//NumByteToWrite:所要写的数据的长度
unsigned int SST25_Page_Program(unsigned int WriteAddr,unsigned char* pBuffer,unsigned int NumByteToWrite){
unsigned int i,page,offset;
if(NumByteToWrite==0) return 3; //溢出
//if((WriteAddr+NumByteToWrite) >(SST25_MAX_SECTORS*SST25_SECTOR_SIZE)) return 5; //地址错误!
offset=WriteAddr % SST25_PAGE_SIZE; //一个页内(256)的偏移地址
//*********************************************************************
if(offset){//>0未对其地址!!!!!
offset =256-offset;
//*************************************************************************
//开始地址从WriteAddr,一次写入(256-WriteAddr%256)个字节,以便对齐边界!
if(NumByteToWrite > offset){
SST25_Page_Write(pBuffer,WriteAddr,offset);
//*****************************************************************************
//重新计算后面的写入字节数!
NumByteToWrite -=offset;
WriteAddr +=offset;
pBuffer +=offset;
}else{
SST25_Page_Write(pBuffer,WriteAddr,NumByteToWrite); //写入字节数小于一个页内剩余的字节数,直接写完返回。
return 0;
}
}
//---------------------------------------------------------------------------
page=NumByteToWrite/SST25_PAGE_SIZE; //要写几页
offset=NumByteToWrite%SST25_PAGE_SIZE; //最后一页的字节数
//*************************************************************************
//开始地址从0 ,便可以一次写入256个字节
for(i=0;i<page;i++){
SST25_Page_Write(pBuffer+i*256,WriteAddr+i*256,256);
}
//*************************************************************************
//继续写完不到一页的数据
if(offset>0){
SST25_Page_Write(pBuffer,WriteAddr,offset);
}
return 0;
}
//****************************************************************************
//擦除整个芯片
//整片擦除时间:
//W25X16:25s
//W25X32:40s
//W25X64:40s
//等待时间超长...
void SST25_Erase_Chip(void){
SST25_Page_Program_Enable(); //SET WEL
SST25_Wait_Busy();
SPI_FLASH_CS(0); //使能器件
Flash_ReadWriteByte(SST25_ChipErase); //发送片擦除命令
SPI_FLASH_CS(1); //取消片选
SST25_Wait_Busy(); //等待芯片擦除结束
}
//****************************************************************************
//擦除一个扇区 Sector_Dst_Addr 物理扇区地址号!!
//Dst_Addr:扇区地址 0~511 for w25x16
//擦除一个山区的最少时间:150ms
unsigned int SST25_Erase_Sector(unsigned int Sector_Dst_Addr){
SST25_Page_Program_Enable(); //SET WEL
SST25_Wait_Busy();
SPI_FLASH_CS(0); //使能器件
SPI_SendAdrOut(SST25_4KByte_BlockERASE,(Sector_Dst_Addr)*SST25_SECTOR_SIZE);//发送扇区擦除指令 必须输入绝对物理地址
SPI_FLASH_CS(1); //取消片选
return SST25_Wait_Busy(); //等待擦除完成
}
//****************************************************************************
//等待空闲
unsigned int SST25_Wait_Busy(void){
unsigned int i;
while ((SST25_Page_ReadSR() & 0x01)==0x01){ // 等待BUSY位清空
if(i++>90000) return 1; //一直忙!
}
return 0;
}
//****************************************************************************
//文件接口的读、写、删除
unsigned int FAT_Wr_Flash(unsigned int byteAddr,unsigned char *p,unsigned int byteCnt){
// byteAddr +=FLASH_info.Fat_start_adr;
SST25_Page_Program(byteAddr,p,byteCnt);
return 0;
}
//****************************************************************************
//文件接口的读、写、删除
unsigned int FAT_Rd_Flash(unsigned int byteAddr,unsigned char *p,unsigned int byteCnt){
//byteAddr +=FLASH_info.Fat_start_adr;
SST25_Page_Read(byteAddr,p,byteCnt);
return 0;
}
//****************************************************************************
//文件接口的读、写、删除
unsigned int FAT_Erase_Flash(unsigned int sectorAddr){
// sectorAddr +=(FLASH_info.Fat_start_adr/FLASH_info.oneSectorByte); //计算出物理地址所在的扇区
SST25_Erase_Sector(sectorAddr);
return 0;
}
//*************************************************************************************
//对外部的接口程序
//******************************************************************************
//初始化各存储位置的参数
//必须传入扇区个数,不是物理地址!!!范围:0~4095 个扇区
//*******************************************************************************
unsigned char FLASH_ROM_Init(unsigned int sys_sector_start,unsigned int sys_sector_size,
unsigned int fat_sector_start,unsigned int fat_sector_size){
/*
FLASH_info.onePageByte =SST25_PAGE_SIZE ;//256;
FLASH_info.oneSectorByte =SST25_SECTOR_SIZE;//4096;
FLASH_info.maxSector =SST25_MAX_SECTORS;
FLASH_info.maxByte =SST25_MAX_SECTORS * SST25_SECTOR_SIZE;
FLASH_info.Sys_start_adr =sys_sector_start*SST25_SECTOR_SIZE;
FLASH_info.Sys_size =sys_sector_size*SST25_SECTOR_SIZE;
FLASH_info.Fat_start_adr =fat_sector_start*SST25_SECTOR_SIZE;
FLASH_info.Fat_size =fat_sector_size*SST25_SECTOR_SIZE;
if(FLASH_info.Sys_start_adr >=FLASH_info.maxByte) return 1; //系统开始地址超范围!
if(FLASH_info.Fat_start_adr >=FLASH_info.maxByte) return 2; //文件系统开始地址超范围!
if((FLASH_info.Sys_start_adr+FLASH_info.Sys_size) > FLASH_info.maxByte) return 3; //系统空间超范围!
if((FLASH_info.Fat_start_adr+FLASH_info.Fat_size) > FLASH_info.maxByte) return 4; //文件系统空间超范围!
*/
SST25_SPI_Flash_Init();
return 0; //初始化成功!!!
}
|