w806 w25q128 spi flash 移植fatfs

一:spi初始化

    hspi.Instance = SPI;
    hspi.Init.Mode = SPI_MODE_MASTER;
    hspi.Init.CLKPolarity = SPI_POLARITY_LOW; 
    hspi.Init.CLKPhase = SPI_PHASE_1EDGE; 
    hspi.Init.NSS = SPI_NSS_SOFT;
    hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
    hspi.Init.FirstByte = SPI_LITTLEENDIAN;

二:io初始化

    __HAL_RCC_SPI_CLK_ENABLE();
	__HAL_AFIO_REMAP_SPI_CS(GPIOB, GPIO_PIN_4);//22 or 18
	__HAL_AFIO_REMAP_SPI_CLK(GPIOB, GPIO_PIN_2);//24
	__HAL_AFIO_REMAP_SPI_MISO(GPIOB, GPIO_PIN_3);//25
	__HAL_AFIO_REMAP_SPI_MOSI(GPIOB, GPIO_PIN_5);//26

三:下载fatfs

 

//#define DEV_RAM		0	/* Example: Map Ramdisk to physical drive 0 */
//#define DEV_MMC		1	/* Example: Map MMC/SD card to physical drive 1 */
//#define DEV_USB		2	/* Example: Map USB MSD to physical drive 2 */

#define DEV_SPI		0	/* Example: Map USB MSD to physical drive 2 */

//修改添加 spi
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */

#include "../src/wq25128.h"

/* Definitions of physical drive number for each drive */

//#define DEV_RAM		0	/* Example: Map Ramdisk to physical drive 0 */
//#define DEV_MMC		1	/* Example: Map MMC/SD card to physical drive 1 */
//#define DEV_USB		2	/* Example: Map USB MSD to physical drive 2 */

#define DEV_SPI		0	/* Example: Map USB MSD to physical drive 2 */


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat;
//	int result;
	
	uint8_t id[2] = {0};
	
	switch (pdrv) {
	case DEV_SPI:
		
		//BSP_W25Q128_Init();
		BSP_W25Q128_Read_ID(id);
		//result = id[0] << 8 | id[1];
		if(id[0] == 0xEF && id[1] == 0x17)//0x18 is w25q256
			stat = !STA_NOINIT;
		else   
			stat = STA_NOINIT;
		return stat;	
	
	
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	//DSTATUS stat;
	//int result;
	
	

	switch (pdrv) {
	case DEV_SPI:
		//BSP_W25Q128_Init();
		 
		return !STA_NOINIT;//disk_status(DEV_SPI);
	
	
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
	//DRESULT res;
	//int result;

	switch (pdrv) {
	case DEV_SPI:
		if(pdrv != 0 || count == 0)   return RES_PARERR;

		if(BSP_W25Q128_Read(buff, sector * W25Q128FV_SECTOR_SIZE, count * W25Q128FV_SECTOR_SIZE) == W25Q128_OK)
			return RES_OK;
		else return RES_ERROR;
	
	
	}

	return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	//DRESULT res;
	//int result;
	uint8_t i = 0;
	switch (pdrv) {
	case DEV_SPI:
		

		if(pdrv != 0 || count == 0)   return RES_PARERR;

		for(i = 0; i < count; i++)
		{
			if(BSP_W25Q128_Erase_Sector((sector + i) * W25Q128FV_SECTOR_SIZE) != W25Q128_OK)
				return RES_ERROR;

			if(BSP_W25Q128_Write((uint8_t*)buff + (i * W25Q128FV_SECTOR_SIZE), (sector + i) * W25Q128FV_SECTOR_SIZE, W25Q128FV_SECTOR_SIZE) != W25Q128_OK)
				return RES_ERROR;
		}

		return RES_OK;
	
	
	}

	return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res;
	//int result;

	switch (pdrv) {
	case DEV_SPI:
		if(pdrv != 0)   return RES_PARERR;

		switch(cmd)
		{
			case CTRL_SYNC:
				res = RES_OK;
				break;

			case GET_SECTOR_COUNT:
				*(DWORD*)buff = W25Q128FV_FLASH_SIZE / W25Q128FV_SECTOR_SIZE;
				res = RES_OK;
				break;

			case GET_SECTOR_SIZE:
				*(WORD*)buff =  W25Q128FV_SECTOR_SIZE;
				res = RES_OK;
				break;

			case GET_BLOCK_SIZE:
				*(DWORD*)buff = 1;
				res = RES_OK;
				break;

			default:
				res = RES_PARERR;
				break;

		}
		return res;
	
	
	
	}

	return RES_PARERR;
}

根据需要 修改 配置 文件 

#define FF_USE_MKFS		1         //支持格式化函数
#define FF_CODE_PAGE	936       //中文支持
#define FF_USE_LFN		1         //长文件名支持
#define FF_MAX_SS		4096        //扇区块字节
#define FF_MAX_SS		4096        //文件日期

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值