DSTATUS disk_initialize (
BYTE drv /* Physical drive nmuber (0..) */
){
switch (drv){
case 0 :
if(W25QXX_Init()) return STA_NOINIT;
return RES_OK;
case 1 :
return RES_OK;
case 2 :
return RES_OK;
case 3 :
return RES_OK;
default:
return STA_NOINIT;
}
}
DSTATUS disk_status (
BYTE drv /* Physical drive nmuber (0..) */
){
switch (drv){
case 0 :
return RES_OK;
case 1 :
return RES_OK;
case 2 :
return RES_OK;
default:
return STA_NOINIT;
}
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
DRESULT disk_read (
BYTE drv, /* Physical drive nmuber (0..) */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address (LBA) */
BYTE count /* Number of sectors to read (1..255) */
){
if( count==0 ) return RES_PARERR;
switch (drv){
case 0:
if(0==W25QXX_Read_Sector(sector,buff,count)) return RES_OK;
return RES_ERROR;
case 1:
return RES_OK;
default:
return RES_ERROR;
}
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
#if _READONLY == 0
DRESULT disk_write (
BYTE drv, /* Physical drive nmuber (0..) */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address (LBA) */
BYTE count /* Number of sectors to write (1..255) */
){
if( count==0 ) return RES_PARERR;
switch (drv){
case 0:
if(W25QXX_Erase_Sector(sector,count)) return RES_ERROR ;
if(W25QXX_Write_Sector(sector,(unsigned char *)buff,count)) return RES_ERROR ;
return RES_OK;
case 1:
return RES_OK;
default:return RES_ERROR;
}
}
#endif /* _READONLY */
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
DRESULT disk_ioctl (
BYTE drv, /* Physical drive nmuber (0..) */
BYTE ctrl, /* Control code */
void *buff /* Buffer to send/receive control data */
){
//DWORD nFrom,nTo;
//char *buf = buff;
if (drv==0){
//MSD0_GetCardInfo(&SD0_CardInfo);
switch(ctrl){
case CTRL_SYNC :
return RES_OK;
//扇区擦除
case CTRL_ERASE_SECTOR:
//nFrom = *((DWORD*)buff);
//nTo = *(((DWORD*)buff)+1);
//W25QXX_Erase_Sector(nFrom,nTo-nFrom+1);
return RES_OK;
case GET_BLOCK_SIZE: //要删除的扇区的块大小
*(DWORD*)buff =W25QXX_BLOCK_SIZE;
return RES_OK;
case GET_SECTOR_SIZE:
*(DWORD*)buff = W25QXX_SECTOR_SIZE;
return RES_OK;
case GET_SECTOR_COUNT:
*(DWORD*)buff = W25QXX_MAX_SECTORS;
return RES_OK;
}
}
return RES_PARERR;
}
#ifndef __W25QXX_H
#define __W25QXX_H
#include "W25QXX.h"
//**********************************************************************************************************************
#define SPI_CONFIG SPI1_Config();
#define W25QXX_ReadWrite_Byte(a) SPI1_ReadWriteByte(a)
//**********************************************************************************************************************
//修改状态寄存器,允许芯片存储器被写
unsigned char W25QXX_Init(void){
W25QXX_Read_ID();
//W25QXX_Write_StatusReg(0x02); //使能状态寄存器中的写存储器
return 0; //0k
}
//***************************************************************
//读取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 W25QXX_Read_StatusReg(void){
unsigned char reg;
W25QXX_CS(0); //使能器件
W25QXX_ReadWrite_Byte(W25QXX_ReadStatusReg); //发送读取状态寄存器命令
reg=W25QXX_ReadWrite_Byte(0Xff); //读取一个字节
W25QXX_CS(1); //取消片选
return reg;
}
//*************************************************************************
//写SPI_FLASH状态寄存器
//只有SPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)可以写!!!
void W25QXX_Write_StatusReg(unsigned char sr){
W25QXX_CS(0); //片选
W25QXX_ReadWrite_Byte(W25QXX_EnableWriteStatusReg); //使能写状态寄存器命令
W25QXX_CS(1); //取消片选
W25QXX_CS(0); //片选
W25QXX_ReadWrite_Byte(W25QXX_WriteStatusReg); //发送写取状态寄存器命令
W25QXX_ReadWrite_Byte(sr); //写入一个字节
W25QXX_CS(1); //取消片选
}
//****************************************************************************
//SPI_FLASH写使能
//将WEL置位
void W25QXX_Page_Program_Enable(void){
W25QXX_CS(0); //使能器件
W25QXX_ReadWrite_Byte(W25QXX_WriteEnable); //发送写使能
W25QXX_CS(1); //取消片选
}
//****************************************************************************
//SPI_FLASH写禁止
//将WEL清零
void W25QXX_Page_Program_Disable(void){
W25QXX_CS(0); //使能器件
W25QXX_ReadWrite_Byte(W25QXX_WriteDisable); //发送写禁止指令
W25QXX_CS(1); //取消片选
}
//*********************************************************************************
//发送程序 COM=命令;adr=地址
void W25QXX_SendAdrOut(unsigned char com,unsigned int adr){
W25QXX_ReadWrite_Byte (com); /* 发送字节数据烧写命令 */
W25QXX_ReadWrite_Byte ((adr & 0xFFFFFF) >> 16); /* 发送3个字节的地址信息 */
W25QXX_ReadWrite_Byte ((adr & 0xFFFF) >> 8);
W25QXX_ReadWrite_Byte ( adr & 0xFF);
}
//****************************************************************************
//读取芯片ID W25QXXVF016的是 0XBF41 sst25vf064_ID=bf4b
//文帮的是128=0xEF17 w25q64==ef16
unsigned int W25QXX_Read_ID(void){
unsigned int ID = 0;
W25QXX_CS(0);
W25QXX_SendAdrOut(W25QXX_ManufactDeviceID,0);//发送读取ID命令
//读取返回的16位值
ID =W25QXX_ReadWrite_Byte(0xFF)<<8; //高8位数据
ID |=W25QXX_ReadWrite_Byte(0xFF); //底八位数据
W25QXX_CS(1);
return ID;
}
//****************************************************************************
//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535即64k)
unsigned int W25QXX_Read_Sector(unsigned int Sector,unsigned char* pBuffer,unsigned int cnt){
unsigned int i;
W25QXX_CS(0); //使能器件
W25QXX_SendAdrOut(W25QXX_FastReadData,Sector*W25QXX_SECTOR_SIZE); //发送读取命令
W25QXX_ReadWrite_Byte(0XFF); //发送一个哑数据!
for(i=0;i< cnt*W25QXX_SECTOR_SIZE;i++) pBuffer[i]=W25QXX_ReadWrite_Byte(0XFF); //循环读数
W25QXX_CS(1); //取消片选
return 0;
}
//****************************************************************************
//写入扇区已经擦除干净, NumByteToWrite 最好是8,16,32,64,128,256
//pBuffer:为待写数据组
//WriteAddr:所写数据的起始地址
//NumByteToWrite:所要写的数据的长度
unsigned int W25QXX_Write_Sector(unsigned int Sector,unsigned char* pBuffer,unsigned int cnt){
unsigned int i,j,addr;
addr =Sector*W25QXX_SECTOR_SIZE;
while(cnt--){
j=W25QXX_SECTOR_SIZE / W25QXX_PAGE_SIZE;
while(j--){
W25QXX_Page_Program_Enable(); //SET WEL
W25QXX_CS(0);
W25QXX_SendAdrOut(W25QXX_ByteProgram,addr); //发送写页命令
//发送待写的数据
for(i=0;i < W25QXX_PAGE_SIZE;i++) W25QXX_ReadWrite_Byte(*pBuffer++);
addr +=W25QXX_PAGE_SIZE;
W25QXX_CS(1);
W25QXX_Wait_Busy();//等待写完成
}
}
W25QXX_Page_Program_Disable();
return 0;
}
//****************************************************************************
//擦除整个芯片
//整片擦除时间:
//W25X16:25s
//W25X32:40s
//W25X64:40s
//等待时间超长...
void W25QXX_Erase_Chip(void){
W25QXX_Page_Program_Enable(); //SET WEL
W25QXX_CS(0); //使能器件
W25QXX_ReadWrite_Byte(W25QXX_ChipErase); //发送片擦除命令
W25QXX_CS(1); //取消片选
W25QXX_Wait_Busy(); //等待芯片擦除结束
W25QXX_Page_Program_Disable();
}
//****************************************************************************
//擦除一个扇区 Sector_Dst_Addr 物理扇区地址号!!
//Dst_Addr:扇区地址 0~511 for w25x16
//擦除一个山区的最少时间:150ms
unsigned int W25QXX_Erase_Sector(unsigned int Sector_Dst_Addr,unsigned int cnt){
unsigned int i;
W25QXX_Page_Program_Enable(); //SET WEL
for(i=0;i<cnt;i++){
W25QXX_CS(0); //使能器件
W25QXX_SendAdrOut(W25QXX_4KByte_BlockERASE,(Sector_Dst_Addr++)*W25QXX_SECTOR_SIZE);//发送扇区擦除指令 必须输入绝对物理地址
W25QXX_CS(1); //取消片选
if(W25QXX_Wait_Busy()) return 1; //等待擦除完成
}
//W25QXX_Page_Program_Disable();
return 0;
}
//****************************************************************************
//等待空闲
unsigned int W25QXX_Wait_Busy(void){
unsigned int i=0;
while ((W25QXX_Read_StatusReg() & 0x01)==0x01){ // 等待BUSY位清空
if(i++>90000) return 1; //一直忙!
}
return 0;
}
#endif
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>