flash型号:w25q32
/4K擦除/
/*****************************************************************/
void W25q16_4KErase(uint32_t addr)
{
Spi_Write_Enable();
CS_L;
Spi_WR_Byte(0x20);
Spi_WR_Byte((addr & 0xFF0000)>>16);
Spi_WR_Byte((addr & 0x00FF00)>>8);
Spi_WR_Byte(addr & 0xFF);
CS_H;
SPI_Wait_WriteEnd();
}
/页写/
/*****************************************************************/
void SPI_PageProgram_Write(uint8_t *rx,uint32_t len,uint32_t addr)
{
Spi_Write_Enable();
CS_L;
Spi_WR_Byte(0x02);
Spi_WR_Byte((addr & 0xFF00)>>16);
Spi_WR_Byte((addr & 0xFF00)>>8);
Spi_WR_Byte(addr& 0xFF);
for(int i=0;i<len;i++)
{
Spi_WR_Byte(*rx++);
}
CS_H;
SPI_Wait_WriteEnd();
//nrf_delay_ms(10);
}
/跨页写/
/*****************************************************************/
void SPI_Write_Data(uint8_t *buf,uint32_t len,uint32_t addr)
{
uint16_t pagenum;
uint16_t addrbyte;//最低八位地址
addrbyte = addr%256;
if (len > (256 - addrbyte))//跨页了
{
SPI_PageProgram_Write(buf, 256 - addrbyte, addr);//写满本页
addr += 256-addrbyte;
buf += 256-addrbyte;
len -= 256-addrbyte;
pagenum = len/256;
while (pagenum--)
{
SPI_PageProgram_Write(buf, 256, addr);
addr += 256;
buf += 256;
len -= 256;
}
SPI_PageProgram_Write(buf, len, addr);
}
else
{
SPI_PageProgram_Write(buf, len, addr);
}
//nrf_delay_ms(10);
}
/读flash/
/*****************************************************************/
void SPI_Read_Data(uint8_t *rx,uint32_t len,uint32_t addr)
{
CS_L;
Spi_WR_Byte(0x03);
Spi_WR_Byte((addr & 0xFF0000)>>16);
Spi_WR_Byte((addr & 0x00FF00)>>8);
Spi_WR_Byte(addr & 0xFF);
while(len--)
{
*rx=Spi_WR_Byte(0xff);
rx++;
}
CS_H;
//nrf_delay_ms(10);
}
/-----------------------------------------------------------------------/
/* fatfs读接口 /
/-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE buff, / Data buffer to store read data /
DWORD sector, / Start sector in LBA /
UINT count / Number of sectors to read */
)
{
printf("disk_read---sector:%d,count:%d\r\n",sector,count);
if(pdrv == SPI_FLASH)
{
SPI_Read_Data(buff,count<<12,sector<<12);//spi flash的读接口,注意函数参数类型一致性
return RES_OK;
}
else
{
printf("!!!disk_read ERR\r\n");
return RES_PARERR;
}
}
/-----------------------------------------------------------------------/
/* fatfs写接口 /
/-----------------------------------------------------------------------/
DRESULT disk_write (
BYTE pdrv, / Physical drive nmuber to identify the drive */
const BYTE buff, / Data to be written /
DWORD sector, / Start sector in LBA /
UINT count / Number of sectors to write */
)
{
printf("disk_write---sector:%d,count:%d\r\n",sector,count);
if(pdrv == SPI_FLASH)
{
W25q16_4KErase(sector<<12);
nrf_delay_ms(10);
SPI_Write_Data((uint8_t *)buff,count<<12,sector<<12);//spi flash的写接口,注意函数参数类型一致性
return RES_OK;
}
else
{
printf("!!!disk_write ERR\r\n");
return RES_PARERR;
}
}
/-----------------------------------------------------------------------/
/* Miscellaneous Functions /
/-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) /
BYTE cmd, / Control code */
void buff / Buffer to send/receive control data */
)
{
if (pdrv == SPI_FLASH)
{
switch (cmd)
{
case CTRL_SYNC:
return RES_OK;
case GET_SECTOR_COUNT:
*(DWORD * )buff = 1024;//W25Q32 有1024个大小为4k bytes 的扇区
return RES_OK;
/* 扇区大小 */
case GET_SECTOR_SIZE :
*(WORD * )buff = 4096;//spi flash的扇区大小是 4K Bytes
return RES_OK;
/*块大小 */
case GET_BLOCK_SIZE :
*(DWORD * )buff = 1;
return RES_OK;
default:
return RES_PARERR;
}
}
else
{
printf("!!!disk_ioctl ERR\r\n");
return RES_PARERR;
}
}
/-----------------------------------------------------------------------/
/* ff conf.h /
/-----------------------------------------------------------------------/
#define FF_MIN_SS 512
#define FF_MAX_SS 4096
#define FF_USE_MKFS 1
/-----------------------------------------------------------------------/
/ 主函数 /
/-----------------------------------------------------------------------*/
static int fileSystemInit()
{
res = f_mount (&sysFs,"0:",1); //挂载
printf("f_mount函数的返回值是%d\r\n",res);
if(res != 0)
{
res = f_mkfs("0:",FM_FAT,4096,gFsWork,sizeof(gFsWork)); //格式化
printf("f_mkfs函数的返回值是%d\r\n",res);
if(res == 0)
{
res = f_mount (&sysFs,"0:",1); //在挂载
printf("f_mount2函数的返回值是%d\r\n",res);
if(res == 0)
{
gFsInited = 1;
return 0;
}
else
return -1;
}
else
return -1;
}
else
gFsInited = 1;
return 0;
}
/-----------------------------------------------------------------------/
/* 串口打印结果 /
/-----------------------------------------------------------------------*/
DeviceID=ef15 //设备id可以读出来
disk_read---sector:0,count:1
disk_read---sector:63,count:1
f_mount函数的返回值是13
disk_write---sector:63,count:1
disk_write---sector:64,count:1
disk_write---sector:65,count:1
disk_write---sector:66,count:1
disk_write---sector:67,count:1
disk_write---sector:68,count:1
disk_write---sector:0,count:1
f_mkfs函数的返回值是0
disk_read---sector:0,count:1
disk_read---sector:63,count:1
f_mount2函数的返回值是13
gFsInited=0