FATFS文件系统管理W25Q128

1首先用STM32CUBEMX配置

如下图:
在这里插入图片描述

2重点修改user_diskio.c

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
  * @file    user_diskio.c
  * @brief   This file includes a diskio driver skeleton to be completed by the user.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
 /* USER CODE END Header */

#ifdef USE_OBSOLETE_USER_CODE_SECTION_0
/*
 * Warning: the user section 0 is no more in use (starting from CubeMx version 4.16.0)
 * To be suppressed in the future.
 * Kept to ensure backward compatibility with previous CubeMx versions when
 * migrating projects.
 * User code previously added there should be copied in the new user sections before
 * the section contents can be deleted.
 */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
#endif

/* USER CODE BEGIN DECL */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "ff_gen_drv.h"

/* Private typedef -----------------------------------------------------------*/
#include "w25qxx.h"
#define FLASH_SECTOR_SIZE     4096   //每个扇区4K
#define FLASH_SECTOR_COUNT   256*16  //W25Q128有256block,每个block16扇区
#define FLASH_BLOCK_SIZE       65536    //16*4096    

/* Private define ------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
/* Disk status */
static volatile DSTATUS Stat = STA_NOINIT;

/* USER CODE END DECL */

/* Private function prototypes -----------------------------------------------*/
DSTATUS USER_initialize (BYTE pdrv);
DSTATUS USER_status (BYTE pdrv);
DRESULT USER_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count);
#if _USE_WRITE == 1
  DRESULT USER_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count);
#endif /* _USE_WRITE == 1 */
#if _USE_IOCTL == 1
  DRESULT USER_ioctl (BYTE pdrv, BYTE cmd, void *buff);
#endif /* _USE_IOCTL == 1 */

Diskio_drvTypeDef  USER_Driver =
{
  USER_initialize,
  USER_status,
  USER_read,
#if  _USE_WRITE
  USER_write,
#endif  /* _USE_WRITE == 1 */
#if  _USE_IOCTL == 1
  USER_ioctl,
#endif /* _USE_IOCTL == 1 */
};

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Initializes a Drive
  * @param  pdrv: Physical drive number (0..)
  * @retval DSTATUS: Operation status
  */
DSTATUS USER_initialize (
	BYTE pdrv           /* Physical drive nmuber to identify the drive */
)
{
  /* USER CODE BEGIN INIT */
    Stat = STA_NOINIT;
    return RES_OK;
  /* USER CODE END INIT */
}

/**
  * @brief  Gets Disk Status
  * @param  pdrv: Physical drive number (0..)
  * @retval DSTATUS: Operation status
  */
DSTATUS USER_status (
	BYTE pdrv       /* Physical drive number to identify the drive */
)
{
  /* USER CODE BEGIN STATUS */
    Stat = STA_NOINIT;
    return RES_OK;
  /* USER CODE END STATUS */
}

/**
  * @brief  Reads Sector(s)
  * @param  pdrv: Physical drive number (0..)
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT USER_read (
	BYTE pdrv,      /* Physical drive nmuber to identify the drive */
	BYTE *buff,     /* Data buffer to store read data */
	DWORD sector,   /* Sector address in LBA */
	UINT count      /* Number of sectors to read */
)
{
  /* USER CODE BEGIN READ */
		for(;count>0;count--){
        W25QXX_Read(buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
        sector++;
        buff+=FLASH_SECTOR_SIZE;
    }
    return RES_OK;
  /* USER CODE END READ */
}

/**
  * @brief  Writes Sector(s)
  * @param  pdrv: Physical drive number (0..)
  * @param  *buff: Data to be written
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to write (1..128)
  * @retval DRESULT: Operation result
  */
#if _USE_WRITE == 1
DRESULT USER_write (
	BYTE pdrv,          /* Physical drive nmuber to identify the drive */
	const BYTE *buff,   /* Data to be written */
	DWORD sector,       /* Sector address in LBA */
	UINT count          /* Number of sectors to write */
)
{
  /* USER CODE BEGIN WRITE */
  /* USER CODE HERE */
for(;count>0;count--){                                            
        W25QXX_Write((uint8_t *)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
        sector++;
        buff+=FLASH_SECTOR_SIZE;
    }
	
	
    return RES_OK;
  /* USER CODE END WRITE */
}
#endif /* _USE_WRITE == 1 */

/**
  * @brief  I/O control operation
  * @param  pdrv: Physical drive number (0..)
  * @param  cmd: Control code
  * @param  *buff: Buffer to send/receive control data
  * @retval DRESULT: Operation result
  */
#if _USE_IOCTL == 1
DRESULT USER_ioctl (
	BYTE pdrv,      /* Physical drive nmuber (0..) */
	BYTE cmd,       /* Control code */
	void *buff      /* Buffer to send/receive control data */
)
{
  /* USER CODE BEGIN IOCTL */
    DRESULT res = RES_ERROR;
	
	switch(cmd){
        case CTRL_SYNC:
        res = RES_OK; 
                break;     
        case GET_SECTOR_SIZE:
                *(WORD*)buff = FLASH_SECTOR_SIZE;
                res = RES_OK;
                break;     
        case GET_BLOCK_SIZE:
                *(WORD*)buff = (WORD)FLASH_BLOCK_SIZE;
                res = RES_OK;
                break;     
        case GET_SECTOR_COUNT:
                *(DWORD*)buff = FLASH_SECTOR_COUNT;
                res = RES_OK;
                break;
        default:
                res = RES_PARERR;
                break;
    }
    return res;
  /* USER CODE END IOCTL */
}
#endif /* _USE_IOCTL == 1 */

3在main函数添加代码并测试

BYTE work[4096];
BYTE WriteBuffer[]=  "hello world123";
BYTE ReadBuffer[1024] = {0}; 

void FATFS_MOUNT(void){
   printf("FATFS START");
	retUSER  = f_mount(&USERFatFS, USERPath, 1);

    if(retUSER ==FR_NO_FILESYSTEM)
	{
       printf("fmkfs");
        /* 格式化 */
		retUSER  = f_mkfs(USERPath, 1, 0, work, sizeof(work));
		if(retUSER  == FR_OK)
			{
					 printf("fmkfs success");
					/* 格式化后,先取消挂载 */
					retUSER  = f_mount(NULL, USERPath, 1);
					/* 重新挂载 */
					retUSER  = f_mount(&USERFatFS,USERPath, 1);
			}
		else
		{
				printf("fmkfs error");
					while(1);
		}       
    }
    else if(retUSER  != FR_OK)
    {
      printf("mount error");
        while(1);
    }
    else
    {
        printf("mount succes");
    }
}

void FATFS_WRITE_TEST(void){
   
	retUSER =  f_open(&USERFile, "1:/data.txt", FA_OPEN_ALWAYS | FA_WRITE);
    if(retUSER){
			printf("open error");
			 while(1);
    }
    else
			printf("open success");
 		
    retUSER = f_write(&USERFile, WriteBuffer, sizeof(WriteBuffer),  &fnum);
    if(retUSER){
			printf("write error");
			 while(1);
    }
    else
        printf("write success");
		
    retUSER = f_close(&USERFile);
    
//读
	void FAFTF_READ_FILE_TEST(void){
  
    retUSER = f_open(&USERFile, "1:/data.txt", FA_READ);
    if(retUSER){
       	printf("open error");
			 while(1);
    }
    else
       printf("open success");
    
    retUSER = f_read(&USERFile, ReadBuffer, 100, &fnum);
    if(retUSER){
        	printf("read error");
			 while(1);
    }
    else
       printf("read success");
    
    retUSER = f_close(&USERFile);
  		
}

int main(void)
{
  MX_SPI1_Init();
  W25QXX_Init();
  MX_FATFS_Init();

  FATFS_MOUNT();
  HAL_Delay(1000);
  FATFS_WRITE_TEST();
  HAL_Delay(1000);

FAFTF_READ_FILE_TEST();
f_mount(NULL, USERPath, 1);
while(1);
}

用LCD显示过程。

在这里插入图片描述
经过测试能用,但是不推荐用FATFS管理spi flash。
因为使用FATFS管理上没有擦写均衡功能。spi flash寿命有限。
推荐使用LittleFS文件管理系统。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要将w25q128移植到fatfs文件系统中,需要按照以下步骤进行操作: 1. 首先,需要在系统中添加w25q128驱动程序,以便能够与芯片进行通信。 2. 接下来,需要在fatfs文件系统中添加w25q128驱动程序的支持,以便能够在文件系统中读写数据。 3. 然后,需要在系统中初始化fatfs文件系统,并将其与w25q128驱动程序进行关联。 4. 最后,可以在应用程序中使用fatfs文件系统来读写w25q128芯片中的数据。 需要注意的是,在移植过程中,需要根据具体的系统和芯片进行相应的调整和修改,以确保系统能够正常运行。 ### 回答2: W25Q128是一款Flash存储器,可以在嵌入式系统中使用。而FATFS是一个在嵌入式系统中常用的文件系统,本文将介绍如何将FATFS文件系统移植到W25Q128中。 一般来说,将文件系统移植到Flash存储器需要考虑以下几个方面: 1. 块大小 Flash存储器有特定的块大小。如果文件系统使用的块大小与Flash存储器不匹配,可能会导致空间浪费或读写效率低下。因此,需要在移植时考虑Flash存储器的块大小,并根据实际情况对文件系统进行适配。 2. 页大小 与块大小类似,Flash存储器还有特定的页大小。如果文件系统使用的页大小与Flash存储器不匹配,则可能会导致写入数据时无法正确对齐,进而导致数据损坏。因此,需要在移植时考虑Flash存储器的页大小,与文件系统的页大小进行适配。 3. 坏块管理 Flash存储器有可能出现坏块,需对其进行管理。一般有2种方式:动态坏块管理和静态坏块管理。动态坏块管理是指在运行时对坏块进行检测和处理;静态坏块管理是指在生产时对坏块进行识别和标记。一般来说,静态坏块管理的效率更高,但在使用过程中可能会出现新的坏块,需要对其进行动态管理。 4. 数据完整性 Flash存储器是一种非易失性存储器,但它的写入过程不具有原子性。因此,在写入数据时需要考虑数据的完整性。一般有2种方式:冗余备份和写入前检验。冗余备份是指将同一数据存储在2个地方,如果读取时发现其中一个数据有损坏,可以使用另一个数据进行恢复;写入前检验则是在写入数据时先检验数据的完整性,如果检验失败则不写入,避免数据损坏。 综上所述,将FATFS文件系统移植到W25Q128中,需要考虑以上几个方面,并根据实际情况进行适配,以确保文件系统的稳定性和可靠性。 ### 回答3: W25Q128是一款SPI串行Flash存储器,可以用于嵌入式系统中存储数据。FATFS文件系统是一款非常流行的文件系统,被广泛应用于各种嵌入式系统中。本文将介绍如何将FATFS文件系统移植到W25Q128串行Flash存储器上。 1. 硬件准备 首先需要准备好一款支持SPI接口的MCU和一块W25Q128串行Flash存储器。常用的MCU有STM32、ATmega等,根据MCU不同,代码实现有所不同。本文以STM32为例。同时需要准备好SPI的时钟、引脚和片选等参数。还需要购买一张SD卡,用于模拟W25Q128串行Flash存储器。 2. 软件准备 在将FATFS文件系统移植到W25Q128上之前,需要先编写SPI驱动程序,确保MCU能够与W25Q128串行Flash存储器通信。可以选择使用标准库或HAL库来编写SPI驱动程序。接着,需要下载FatFs库,并将源代码拷贝到工程文件夹中。 3. 移植FATFS文件系统 移植FATFS文件系统需要完成以下几个步骤: 1)定义磁盘接口结构体,并实现初始化、读写扇区等自定义函数,用于与W25Q128串行Flash存储器交互。 2)调用f_mount函数,将磁盘接口与FATFS文件系统进行绑定,可以通过该接口读写文件及目录。 3)编写测试代码,调用FATFS的API函数,读写文件等。 4. 样例代码 以下是一份基于STM32的代码示例,用于演示如何将FATFS文件系统移植到W25Q128串行Flash存储器上。 ```c #include "stm32f10x.h" #include "diskio.h" #include "ff.h" FATFS fs; FIL fil; BYTE work[FF_MAX_SS]; void SysTick_Handler(void) { static WORD cnt; disk_timerproc(); if (cnt++ >= 10) { cnt = 0; GPIOA->ODR ^= (1<<8); } } DSTATUS disk_initialize(BYTE pdrv) { return 0; } DRESULT disk_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) { for (int i = 0; i < count; i++) { SPI_Read(buff+i*512, sector+i, 512); } return RES_OK; } DRESULT disk_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) { for (int i = 0; i < count; i++) { SPI_Write(buff+i*512, sector+i, 512); } return RES_OK; } DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void* buff) { return RES_OK; } int main(void) { SystemInit(); SysTick_Config(SystemCoreClock/100); RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; GPIOA->CRL &= ~GPIO_CRL_CNF8; GPIOA->CRL |= GPIO_CRL_MODE8_1; GPIOA->ODR |= (1<<8); //SPI初始化 SPI_InitTypeDef spi; spi.SPI_Direction = SPI_Direction_2Lines_FullDuplex; spi.SPI_Mode = SPI_Mode_Master; spi.SPI_DataSize = SPI_DataSize_8b; spi.SPI_CPOL = SPI_CPOL_Low; spi.SPI_CPHA = SPI_CPHA_1Edge; spi.SPI_NSS = SPI_NSS_Soft; spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; spi.SPI_FirstBit = SPI_FirstBit_MSB; spi.SPI_CRCPolynomial = 0; SPI_Init(SPI1, &spi); SPI_Cmd(SPI1, ENABLE); //SD卡初始化 SD_Init(); //磁盘接口结构体 static const Diskio_drvTypeDef pdrv = { disk_initialize, disk_read, disk_write, disk_ioctl, }; //挂载文件系统 f_mount(&fs, "", 0); //打开文件 f_open(&fil, "hello.txt", FA_WRITE|FA_CREATE_ALWAYS); //写入文件 char buf[] = "Hello FATFS!"; UINT bw; f_write(&fil, buf, strlen(buf), &bw); f_close(&fil); //读取文件 f_open(&fil, "hello.txt", FA_READ); char read_buf[100]; f_read(&fil, read_buf, fil.fsize, &bw); f_close(&fil); //复制文件 f_open(&fil, "hello2.txt", FA_WRITE|FA_CREATE_ALWAYS); f_lseek(&fil, 0); f_truncate(&fil); f_copy("hello.txt", "hello2.txt"); f_close(&fil); while(1); } ``` 5. 结论 本文介绍了如何将FATFS文件系统移植到W25Q128串行Flash存储器上,并提供了一份基于STM32的代码示例。移植过程需要完成SPI驱动程序的编写、磁盘接口结构体的定义和实现、以及文件系统的绑定和API调用等步骤。对于初学者而言,需要具备一定的MCU编程和SPI协议的基础知识。移植过程中需要注意时序和命令格式等细节问题,以保证与W25Q128串行Flash存储器的正常通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值