소스 검색

1.增加基于SPI SDH卡的fatfs基本功能代码

libo 2 년 전
부모
커밋
1cc3aff2ca
15개의 변경된 파일2281개의 추가작업 그리고 0개의 파일을 삭제
  1. 115 0
      dev/dev_spi_sdcard.c
  2. 73 0
      dev/dev_spi_sdcard.h
  3. 361 0
      func/func_fatfs.c
  4. 78 0
      func/func_fatfs.h
  5. 160 0
      func/func_ram_record.c
  6. 74 0
      func/func_ram_record.h
  7. 556 0
      func/func_record.c
  8. 75 0
      func/func_record.h
  9. 143 0
      func/func_sdmmc_sdcard.c
  10. 52 0
      func/func_sdmmc_sdcard.h
  11. 56 0
      mid_fatfs/fatfs.c
  12. 49 0
      mid_fatfs/fatfs.h
  13. 273 0
      mid_fatfs/ffconf.h
  14. 170 0
      mid_fatfs/user_diskio.c
  15. 46 0
      mid_fatfs/user_diskio.h

+ 115 - 0
dev/dev_spi_sdcard.c

@@ -0,0 +1,115 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.c
+  * @brief          : Main program body
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under BSD 3-Clause license,
+  * the "License"; You may not use this file except in compliance with the
+  * License. You may obtain a copy of the License at:
+  *                        opensource.org/licenses/BSD-3-Clause
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+#include "dev_spi_sdcard.h"
+
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* Private typedef -----------------------------------------------------------*/
+/* USER CODE BEGIN PTD */
+
+/* USER CODE END PTD */
+
+/* Private define ------------------------------------------------------------*/
+/* USER CODE BEGIN PD */
+
+/* USER CODE END PD */
+
+/* Private macro -------------------------------------------------------------*/
+/* USER CODE BEGIN PM */
+
+/* USER CODE END PM */
+
+/* Private variables ---------------------------------------------------------*/
+SPI_HandleTypeDef hspi2;
+
+/* USER CODE BEGIN PV */
+
+/* USER CODE END PV */
+
+/* Private function prototypes -----------------------------------------------*/
+
+/* Private user code ---------------------------------------------------------*/
+/* USER CODE BEGIN 0 */
+
+/* USER CODE END 0 */
+
+
+void SDCARD_SPI_Init(void)
+{
+
+  /* USER CODE BEGIN SPI1_Init 0 */
+
+  /* USER CODE END SPI1_Init 0 */
+
+  /* USER CODE BEGIN SPI1_Init 1 */
+
+  /* USER CODE END SPI1_Init 1 */
+  /* SPI1 parameter configuration*/
+  hspi2.Instance = SPI2;
+  hspi2.Init.Mode = SPI_MODE_MASTER;
+  hspi2.Init.Direction = SPI_DIRECTION_2LINES;
+  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
+  hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
+  hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
+  hspi2.Init.NSS = SPI_NSS_HARD_OUTPUT;
+  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
+  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
+  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
+  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
+  hspi2.Init.CRCPolynomial = 10;
+  if (HAL_SPI_Init(&hspi2) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  /* USER CODE BEGIN SPI1_Init 2 */
+
+  /* USER CODE END SPI1_Init 2 */
+
+}
+
+
+/**
+  * @brief GPIO Initialization Function
+  * @param None
+  * @retval None
+  */
+void SDCARD_GPIO_Init(void)
+{
+
+  /* GPIO Ports Clock Enable */
+  __HAL_RCC_GPIOD_CLK_ENABLE();
+  __HAL_RCC_GPIOB_CLK_ENABLE();
+
+}
+
+/* USER CODE BEGIN 4 */
+
+/* USER CODE END 4 */
+
+
+
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 73 - 0
dev/dev_spi_sdcard.h

@@ -0,0 +1,73 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.h
+  * @brief          : Header for main.c file.
+  *                   This file contains the common defines of the application.
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under BSD 3-Clause license,
+  * the "License"; You may not use this file except in compliance with the
+  * License. You may obtain a copy of the License at:
+  *                        opensource.org/licenses/BSD-3-Clause
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef DEV_SPI_SDCARD_H
+#define DEV_SPI_SDCARD_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f7xx_hal.h"
+
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* Exported types ------------------------------------------------------------*/
+/* USER CODE BEGIN ET */
+
+/* USER CODE END ET */
+
+/* Exported constants --------------------------------------------------------*/
+/* USER CODE BEGIN EC */
+
+/* USER CODE END EC */
+
+/* Exported macro ------------------------------------------------------------*/
+/* USER CODE BEGIN EM */
+
+/* USER CODE END EM */
+
+
+
+/* Exported functions prototypes ---------------------------------------------*/
+
+/* USER CODE BEGIN EFP */
+void SDCARD_GPIO_Init(void);
+void SDCARD_SPI_Init(void);
+/* USER CODE END EFP */
+
+/* Private defines -----------------------------------------------------------*/
+/* USER CODE BEGIN Private defines */
+
+/* USER CODE END Private defines */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* DEV_SPI_SDCARD_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 361 - 0
func/func_fatfs.c

@@ -0,0 +1,361 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.c
+  * @brief          : Main program body
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under BSD 3-Clause license,
+  * the "License"; You may not use this file except in compliance with the
+  * License. You may obtain a copy of the License at:
+  *                        opensource.org/licenses/BSD-3-Clause
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+
+#if USE_FATFS_RECORD
+
+#include "dev_spi_sdcard.h"
+#include "func_fatfs.h"
+
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* Private typedef -----------------------------------------------------------*/
+#define MAX_FILE_COUNT  20
+
+/* Private define ------------------------------------------------------------*/
+/* USER CODE BEGIN PD */
+
+/* USER CODE END PD */
+
+/* Private macro -------------------------------------------------------------*/
+/* USER CODE BEGIN PM */
+
+/* USER CODE END PM */
+
+/* Private variables ---------------------------------------------------------*/
+
+/* USER CODE BEGIN PV */
+
+/* USER CODE END PV */
+
+/* Private function prototypes -----------------------------------------------*/
+
+/* USER CODE BEGIN PFP */
+
+FATFS fileSystem;
+FIL 	testFile;
+uint8_t testBuffer[16] = "SD write success";
+
+
+char 		readbuffer[512];
+
+
+char SDPath[4];
+char readnrf,reafsd;
+
+/* USER CODE END PFP */
+
+
+void 	func_fatfs_Get_Capacity(void);
+/* Private user code ---------------------------------------------------------*/
+/* USER CODE BEGIN 0 */
+
+/* USER CODE END 0 */
+/**
+  * @brief  The application entry point.
+  * @retval int
+  */
+int func_fatfs_init(void)
+{
+	FRESULT res;
+
+  /* Initialize all configured peripherals */
+  SDCARD_GPIO_Init();
+  SDCARD_SPI_Init();
+  SDCARD_FATFS_Init();
+  /* USER CODE BEGIN 2 */
+	res = f_mount(&fileSystem, (const TCHAR*)&SDPath, 1);
+	
+  if(res != FR_OK)
+  {
+			func_record_fatfs_set_OK(false);
+	}
+  /* USER CODE END 2 */
+
+  /* Infinite loop */
+  /* USER CODE BEGIN WHILE */
+ 
+  /* USER CODE END 3 */
+	return res;
+}
+
+/**
+  * @brief  The application entry point.
+  * @retval int
+  */
+int func_fatfs_main(void)
+{
+  /* USER CODE BEGIN 1 */
+	
+	//func_fatfs_Get_Capacity();
+
+  /* USER CODE END 1 */
+	
+	return 0;
+ 
+}
+
+
+typedef struct fatfs_st
+{
+	uint16_t 	TotalSpace;	
+	DWORD 		AvailableSize;
+	DWORD 		UsedSize;
+}fatfs_st;
+
+fatfs_st sd_fatfs_st;
+
+void func_fatfs_Get_Capacity(void)
+{
+//	FRESULT result;
+//	FATFS FS;
+	FATFS *fs;
+	DWORD fre_clust;	
+
+	FRESULT res;
+	
+	res = f_getfree(SDPath, &fre_clust, &fs);  /* 根目录 */
+	if ( res == FR_OK ) 
+	{
+		sd_fatfs_st.TotalSpace=(uint16_t)(((fs->n_fatent - 2) * fs->csize ) / 2 /1024);
+		sd_fatfs_st.AvailableSize=(uint16_t)((fre_clust * fs->csize) / 2 /1024);
+		sd_fatfs_st.UsedSize=sd_fatfs_st.TotalSpace-sd_fatfs_st.AvailableSize;              
+		/* Print free space in unit of MB (assuming 512 bytes/sector) */
+//		printf("\r\n%d MB total drive space.\r\n""%d MB available.\r\n""%d MB  used.\r\n",TotalSpace, AvailableSize,UsedSize);
+	}
+	else 
+	{
+//		printf("Get SDCard Capacity Failed (%d)\r\n", result);
+	}	
+	return;
+} 
+
+FIL file;													/* 文件对象 */
+/* FatFs多项功能测试 */
+FRESULT func_fatfs_miscellaneous(void)
+{
+	FRESULT f_res;                    /* 文件操作结果 */
+	UINT fnum;            					  /* 文件成功读写数量 */
+
+	DIR dir;
+  FATFS *pfs;
+  DWORD fre_clust, fre_sect, tot_sect;
+  
+  printf("\n*************** 设备信息获取 ***************\r\n");
+  /* 获取设备信息和空簇大小 */
+  f_res = f_getfree((TCHAR const*)SDPath, &fre_clust, &pfs);
+
+  /* 计算得到总的扇区个数和空扇区个数 */
+  tot_sect = (pfs->n_fatent - 2) * pfs->csize;
+  fre_sect = fre_clust * pfs->csize;
+
+  /* 打印信息(4096 字节/扇区) */
+  printf("》设备总空间:%10lu KB。\n》可用空间:  %10lu KB。\n", tot_sect *4, fre_sect *4);
+  
+  printf("\n******** 文件定位和格式化写入功能测试 ********\r\n");
+  f_res = f_open(&file, "FatFs读写测试文件.txt",
+                            FA_OPEN_EXISTING|FA_WRITE|FA_READ );
+	if ( f_res == FR_OK )
+	{
+    /*  文件定位 */
+    f_res = f_lseek(&file,f_size(&file)-1);
+    if (f_res == FR_OK)
+    {
+      /* 格式化写入,参数格式类似printf函数 */
+      f_printf(&file,"\n在原来文件新添加一行内容\n");
+      f_printf(&file,"》设备总空间:%10lu KB。\n》可用空间:  %10lu KB。\n", tot_sect *4, fre_sect *4);
+      /*  文件定位到文件起始位置 */
+      f_res = f_lseek(&file,0);
+      /* 读取文件所有内容到缓存区 */
+      f_res = f_read(&file,readbuffer,f_size(&file),&fnum);
+      if(f_res == FR_OK)
+      {
+        printf("》文件内容:\n%s\n",readbuffer);
+      }
+    }
+    f_close(&file);    
+    
+    printf("\n********** 目录创建和重命名功能测试 **********\r\n");
+    /* 尝试打开目录 */
+    f_res=f_opendir(&dir,"TestDir");
+    if(f_res!=FR_OK)
+    {
+      /* 打开目录失败,就创建目录 */
+      f_res=f_mkdir("TestDir");
+    }
+    else
+    {
+      /* 如果目录已经存在,关闭它 */
+      f_res=f_closedir(&dir);
+      /* 删除文件 */
+      f_unlink("TestDir/testdir.txt");
+    }
+    if(f_res==FR_OK)
+    {
+      /* 重命名并移动文件 */
+      f_res=f_rename("FatFs读写测试文件.txt","TestDir/testdir.txt");      
+    } 
+	}
+  else
+  {
+    printf("!! 打开文件失败:%d\n",f_res);
+    printf("!! 或许需要再次运行“FatFs移植与读写测试”工程\n");
+  }
+  return f_res;
+}
+
+
+/**
+  * 文件信息获取
+  */
+FRESULT func_fatfs_file_check(void)
+{ 
+	FRESULT f_res;                    /* 文件操作结果 */	
+  static FILINFO finfo;
+  /* 获取文件信息 */
+  f_res=f_stat("TestDir/testdir.txt",&finfo);
+  if(f_res==FR_OK)
+  {
+    printf("“testdir.txt”文件信息:\n");
+    printf("》文件大小: %ld(字节)\n", finfo.fsize);
+    printf("》时间戳: %u/%02u/%02u, %02u:%02u\n",
+           (finfo.fdate >> 9) + 1980, finfo.fdate >> 5 & 15, finfo.fdate & 31,finfo.ftime >> 11, finfo.ftime >> 5 & 63);
+    printf("》属性: %c%c%c%c%c\n\n",
+           (finfo.fattrib & AM_DIR) ? 'D' : '-',      // 是一个目录
+           (finfo.fattrib & AM_RDO) ? 'R' : '-',      // 只读文件
+           (finfo.fattrib & AM_HID) ? 'H' : '-',      // 隐藏文件
+           (finfo.fattrib & AM_SYS) ? 'S' : '-',      // 系统文件
+           (finfo.fattrib & AM_ARC) ? 'A' : '-');     // 档案文件
+  }
+  return f_res;
+}
+
+/**
+  * @brief  scan_files 递归扫描FatFs内的文件
+  * @param  path:初始扫描路径
+  * @retval result:文件系统的返回值
+  */
+FRESULT func_fatfs_scan_files (char* path) 
+{ 
+  FRESULT res; 		//部分在递归过程被修改的变量,不用全局变量	
+  FILINFO fno; 
+	DIR 		dir;	
+  int i;            
+  char *fn;        // 文件名	
+	
+#if _USE_LFN 
+  /* 长文件名支持 */
+  /* 简体中文需要2个字节保存一个“字”*/
+  static char lfn[_MAX_LFN*2 + 1]; 	
+  fno.fname = lfn; 
+  fno.fsize = sizeof(lfn); 
+#endif 
+  //打开目录
+  res = f_opendir(&dir, path); 
+  if (res == FR_OK) 
+	{ 
+    i = strlen(path); 
+    for (;;) 
+		{ 
+      //读取目录下的内容,再读会自动读下一个文件
+      res = f_readdir(&dir, &fno); 								
+      //为空时表示所有项目读取完毕,跳出
+      if (res != FR_OK || fno.fname[0] == 0) break; 	
+#if _USE_LFN 
+      fn = *fno.lfname ? fno.lfname : fno.fname; 
+#else 
+      fn = fno.fname; 
+#endif 
+      //点表示当前目录,跳过			
+      if (*fn == '.') continue; 	
+      //目录,递归读取      
+      if (fno.fattrib & AM_DIR)         
+			{ 			
+        //合成完整目录名        
+        sprintf(&path[i], "/%s", fn); 		
+        //递归遍历         
+        res = func_fatfs_scan_files(path);	
+        path[i] = 0;         
+        //打开失败,跳出循环        
+        if (res != FR_OK) 
+					break; 
+      } 
+			else 
+			{ 
+				printf("%s/%s\n", path, fn);								//输出文件名	
+        /* 可以在这里提取特定格式的文件路径 */        
+      }//else
+    } //for
+  } 
+  return res; 
+}
+
+
+/*------------------------------------------------------
+ 读取指定目录下所有的文件
+ 说明:
+  f_readdir    按顺序读取目录内文件,
+ 重复调用此函数可读取目录内所有文件;
+---------------------------------------------------------*/
+void func_fatfs_ReadDirTest(char*path)
+{
+// FRESULT res; /* FatFs 函数通用结果代码 */
+		DIR 		dir;	
+	  FILINFO fno; 
+#if _USE_LFN//如果使能支持长文件名 先对下面两项初始化
+    fileinfo.lfsize=_MAX_LFN * 2 + 1;//
+    fileinfo.lfname=(TCHAR*)FileName;//
+#endif
+    if(f_opendir(&dir,(const char*)path) == FR_OK)/* 打开文件夹目录成功,目录信息已经在dir结构体中保存 */
+    {
+        while(f_readdir(&dir, &fno) == FR_OK)  /* 读文件信息到文件状态结构体中 */
+        {
+            if(!fno.fname[0]) break; /* 如果文件名为‘\0',说明读取完成结束 */
+            printf("%s/",path);//打印路径
+#if _USE_LFN
+            printf("文件名:%s\r\n",fileinfo.lfname );//打印信息到串口
+#else
+            printf("文件名:%s\r\n", fno.fname);//
+#endif
+        }
+    }
+}
+
+
+
+static int qsort_cmp(const void *a,const void *b)
+{
+    return strcmp(( char * ) a, (const char* )b);
+}
+
+char fileList[MAX_FILE_COUNT][30];//保存文件名
+
+void func_fatfs_dir_qsort(uint32_t fcount)
+{
+	qsort(fileList, fcount, sizeof(fileList[0]), qsort_cmp);
+}
+
+#endif //--------------------------USE_FATFS_RECORD------------------------------
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 78 - 0
func/func_fatfs.h

@@ -0,0 +1,78 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.h
+  * @brief          : Header for main.c file.
+  *                   This file contains the common defines of the application.
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under BSD 3-Clause license,
+  * the "License"; You may not use this file except in compliance with the
+  * License. You may obtain a copy of the License at:
+  *                        opensource.org/licenses/BSD-3-Clause
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef FUNC_FATFS_H
+#define FUNC_FATFS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f7xx_hal.h"
+#include "fatfs.h"
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* Exported types ------------------------------------------------------------*/
+/* USER CODE BEGIN ET */
+
+/* USER CODE END ET */
+
+/* Exported constants --------------------------------------------------------*/
+/* USER CODE BEGIN EC */
+
+/* USER CODE END EC */
+
+/* Exported macro ------------------------------------------------------------*/
+/* USER CODE BEGIN EM */
+
+/* USER CODE END EM */
+
+
+
+/* Exported functions prototypes ---------------------------------------------*/
+/* USER CODE BEGIN EFP */
+int func_fatfs_init(void);
+int func_fatfs_main(void);
+
+FRESULT func_fatfs_miscellaneous(void);			//FatFs多项功能测试
+FRESULT func_fatfs_file_check(void);				//文件信息获取
+FRESULT func_fatfs_scan_files (char* path); //递归扫描FatFs内的文件
+/* USER CODE END EFP */
+
+/* Private defines -----------------------------------------------------------*/
+/* USER CODE BEGIN Private defines */
+
+/* USER CODE END Private defines */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* FUNC_FATFS_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 160 - 0
func/func_ram_record.c

@@ -0,0 +1,160 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.c
+  * @brief          : Main program body
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under BSD 3-Clause license,
+  * the "License"; You may not use this file except in compliance with the
+  * License. You may obtain a copy of the License at:
+  *                        opensource.org/licenses/BSD-3-Clause
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+/* Includes ------------------------------------------------------------------*/
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include "cmsis_os.h"
+#include "main.h"
+
+#define  USE_RAM_RECORD    0
+
+#if USE_RAM_RECORD==0
+void 		 func_ram_record_init(void)											//记录文件初始化
+{
+	return;
+}
+uint32_t func_ram_record_write(uint8_t *buf ,uint32_t len)
+{
+	return 0;
+}
+uint32_t func_ram_record_read(uint8_t *buf ,uint32_t len)
+{
+	return 0;
+}
+uint32_t func_ram_record_delete(uint8_t *buf,uint32_t len)
+{
+	return 0;
+}
+
+#else
+
+#include "Data_deal.h"
+/* Private includes ----------------------------------------------------------*/
+#include "lib_buffer.h"
+
+
+#include "func_ram_record.h"
+/* Private typedef -----------------------------------------------------------*/
+
+
+/* Private define ------------------------------------------------------------*/
+
+
+/* Private macro -------------------------------------------------------------*/
+
+/* Private variables ---------------------------------------------------------*/
+uint8_t  			ram_record_buf[DATA_UNIT_NUM][DATA_UNIT_SIZE] = {0};
+TBuffferOBJ 	ram_record_obj 					= {0};
+TBufffer 			ram_record_handle 			= {0}; 
+/* Private function prototypes -----------------------------------------------*/
+
+
+/* Private user code ---------------------------------------------------------*/
+
+
+
+/**
+  * @brief  指定长度len,buf内容写入文件
+  * @retval int
+  */
+uint32_t func_ram_record_write(uint8_t *buf ,uint32_t len)
+{
+	uint32_t 		data_len = TBuffer_Push(ram_record_handle, (void *)buf, (int) len);
+	return data_len;
+
+}
+
+
+/**
+  * @brief  The application entry point.
+  * @retval int
+  */
+uint32_t func_ram_record_read(uint8_t *buf, uint32_t len)
+{
+	uint32_t 		data_len = TBuffer_Peek(ram_record_handle, (void *)buf, (int) len);
+	
+	return data_len;
+}
+
+
+uint32_t func_ram_record_delete(uint8_t *buf,uint32_t len)
+{
+	uint32_t 		read_len = TBuffer_Pop(ram_record_handle, (void *)buf, (int) len);
+	return read_len;
+}
+
+
+void func_ram_record_init(void)
+{	
+	ram_record_handle = TBuffer_Init(&ram_record_obj, ram_record_buf, sizeof(ram_record_buf));
+}
+
+
+typedef struct record_test_object
+{
+	uint32_t 	cnts;
+	uint8_t 	test_flag;
+	char 			buf[32];
+}record_test_OBJ;
+	
+record_test_OBJ record_test_obj;
+
+void func_ram_record_test(void)
+{	
+	
+	record_test_obj.cnts++;
+	sprintf(record_test_obj.buf,"XX%08xXX",record_test_obj.cnts);
+	switch(record_test_obj.test_flag)
+	{
+		case 0:
+		{
+			func_ram_record_write((uint8_t *)record_test_obj.buf ,strlen(record_test_obj.buf));
+		}break;
+		case 1:
+		{
+			func_ram_record_read((uint8_t *)record_test_obj.buf ,sizeof(record_test_obj.buf));
+			func_ram_record_delete((uint8_t *)record_test_obj.buf ,sizeof(record_test_obj.buf));
+			func_ram_record_read((uint8_t *)record_test_obj.buf ,sizeof(record_test_obj.buf));
+		}break;
+		default:
+		{
+			func_ram_record_read((uint8_t *)record_test_obj.buf ,sizeof(record_test_obj.buf));
+		}break;
+		
+	}
+	
+}
+
+void func_ram_record_main(void const *argument)
+{
+	osDelay(1000);//
+	func_ram_record_init();
+	while(1)
+	{
+		func_ram_record_test();
+		osDelay(1000);
+	}
+}
+
+#endif //------------------------USE_RAM_RECORD----------------------------
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 74 - 0
func/func_ram_record.h

@@ -0,0 +1,74 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.h
+  * @brief          : Header for main.c file.
+  *                   This file contains the common defines of the application.
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under BSD 3-Clause license,
+  * the "License"; You may not use this file except in compliance with the
+  * License. You may obtain a copy of the License at:
+  *                        opensource.org/licenses/BSD-3-Clause
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef FUNC_RECORD_H
+#define FUNC_RECORD_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f7xx_hal.h"
+#include "fatfs.h"
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* Exported types ------------------------------------------------------------*/
+/* USER CODE BEGIN ET */
+
+/* USER CODE END ET */
+
+/* Exported constants --------------------------------------------------------*/
+/* USER CODE BEGIN EC */
+
+/* USER CODE END EC */
+
+/* Exported macro ------------------------------------------------------------*/
+#define DATA_UNIT_NUM 			150
+#define DATA_UNIT_SIZE 			256
+
+
+
+/* Exported functions prototypes ---------------------------------------------*/
+void 		 func_ram_record_init(void);											//¼Ç¼Îļþ³õʼ»¯
+uint32_t func_ram_record_write(uint8_t *buf ,uint32_t len);
+uint32_t func_ram_record_read(uint8_t *buf ,uint32_t len);
+uint32_t func_ram_record_delete(uint8_t *buf,uint32_t len);
+
+void func_ram_record_test(void);	
+
+
+uint8_t func_ram_record_fatfs_set_OK(uint8_t stat);
+uint8_t func_ram_record_fatfs_isOK(void);
+/* Private defines -----------------------------------------------------------*/
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* FUNC_RECORD_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 556 - 0
func/func_record.c

@@ -0,0 +1,556 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.c
+  * @brief          : Main program body
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under BSD 3-Clause license,
+  * the "License"; You may not use this file except in compliance with the
+  * License. You may obtain a copy of the License at:
+  *                        opensource.org/licenses/BSD-3-Clause
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+#include "main.h"
+
+#if USE_FATFS_RECORD
+/* Includes ------------------------------------------------------------------*/
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include "dev_spi_sdcard.h"
+#include "func_record.h"
+#include "Data_deal.h"
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* Private typedef -----------------------------------------------------------*/
+#define MAX_FILE_COUNT  20
+#define NUM_FILE_SET	 	16
+/* Private define ------------------------------------------------------------*/
+/* USER CODE BEGIN PD */
+
+/* USER CODE END PD */
+
+/* Private macro -------------------------------------------------------------*/
+/* USER CODE BEGIN PM */
+
+/* USER CODE END PM */
+
+/* Private variables ---------------------------------------------------------*/
+
+/* USER CODE BEGIN PV */
+
+/* USER CODE END PV */
+
+/* Private function prototypes -----------------------------------------------*/
+
+
+
+typedef struct fs_record_struct
+{
+	int8_t    fatfs_ok;
+	int8_t 		path[32];   			//操作路径
+	
+	int8_t 		name_new[32];			//最新的文件名
+	int8_t 		name_send[32];			//最老的文件名
+	
+	FIL 			opt_file;					//当前操作的文件
+	FATFS 		fs;	
+	DIR 			dir;							//操作的文件名
+	uint32_t  opt_len;
+	
+	int8_t 		read_buf[512];	
+	uint32_t 	read_len;
+	
+	int8_t 		write_buf[512];		
+	uint32_t  write_len;
+	
+	uint32_t 	 file_num;
+	int8_t 		 file_list[MAX_FILE_COUNT][30];//保存文件名
+}fs_record_OBJ;
+
+
+fs_record_OBJ fs_record_obj = {
+	.fatfs_ok = true,
+	.path 		= "JLRecord",
+	.file_num = 0,
+};
+
+//add boly 20221020
+//uint8_t  data_buf[16*1024] = {0};
+//end boly
+void 	func_record_Get_Capacity(void);
+void func_record_files_update(char * path);
+void func_record_dir_qsort(size_t fcount);
+
+extern SDateTime m_datetime;
+/* Private user code ---------------------------------------------------------*/
+
+uint8_t func_record_fatfs_isOK(void)
+{
+		
+		return fs_record_obj.fatfs_ok;
+}
+
+uint8_t func_record_fatfs_set_OK(uint8_t stat)
+{
+		fs_record_obj.fatfs_ok = stat;
+		return fs_record_obj.fatfs_ok;
+}
+
+void func_record_mk_fileName(char *buf)
+{
+		fs_record_obj.path[12] = 0;
+		sprintf(buf,"%s/%04d%02d%02d.txt",fs_record_obj.path,
+																			m_datetime.year%100 + 2000,
+																			m_datetime.month%100,
+																			m_datetime.day%100
+																			);
+		return;
+}
+/**
+  * @brief  指定长度len,buf内容写入文件
+  * @retval int
+  */
+uint32_t func_record_write(uint8_t *buf ,uint32_t len)
+{
+		
+	FRESULT res;
+	UINT 		write_len = 0;
+
+	//根据日期生成文件名,用于文件写入
+	func_record_mk_fileName((char *)fs_record_obj.name_new);
+	
+	//打开当前存在的文件;
+	res = f_open(&fs_record_obj.opt_file,(char *)fs_record_obj.name_new , FA_WRITE| FA_OPEN_EXISTING);
+	if(FR_OK != res)
+	{
+		if(FR_NO_FILE == res)
+		{
+			//检查文件个数是否超限
+			func_record_files_update((char *)fs_record_obj.path);
+			
+			//创建文件并写入
+			res = f_open(&fs_record_obj.opt_file, (char*)fs_record_obj.name_new, FA_WRITE| FA_CREATE_NEW);
+			if(FR_OK != res)
+			{
+				fs_record_obj.fatfs_ok = false;
+				return 0;
+			}
+			
+		}		
+		else
+		{
+			return 0;
+		}
+	}
+	
+	fs_record_obj.opt_len = f_size(&fs_record_obj.opt_file);
+	
+	//操作指针移动到文件末尾
+	res = f_lseek(&fs_record_obj.opt_file, fs_record_obj.opt_len);
+
+	res = f_write(&fs_record_obj.opt_file, buf, len, &write_len);
+	
+	res = f_sync (&fs_record_obj.opt_file ); 	//刷新文件
+	
+	res = f_close(&fs_record_obj.opt_file);		//关闭文件
+	
+ 	return write_len;
+}
+
+void func_record_get_NameSend(void)
+{
+	//发送文件是否存在
+	if(strlen((char*)fs_record_obj.name_send) > 0)
+	{
+		if(strlen((char*)fs_record_obj.name_new) > 0)
+		{
+			strcpy((char*)fs_record_obj.name_send, (char*)fs_record_obj.name_new);
+		}
+	}
+	else
+	{
+		//检查文件个数是否超限
+		func_record_files_update((char *)fs_record_obj.path);
+	}	
+	
+	return;
+}
+
+/**
+  * @brief  The application entry point.
+  * @retval int
+  */
+uint32_t func_record_read(uint8_t *buf, uint32_t len)
+{
+	FRESULT res;
+	
+	//uint32_t 		file_len = 0;
+	uint32_t 		read_len = 0;
+	
+	func_record_get_NameSend();
+	
+	if(strlen((char *)fs_record_obj.name_send) > 0)
+	{
+	}
+	else
+	{
+		return 0;
+	}
+	
+	res = f_open(&fs_record_obj.opt_file, (char*)fs_record_obj.name_send, FA_READ | FA_OPEN_EXISTING);
+	if(FR_OK != res)
+	{	
+		return 0;
+	}		
+	
+	fs_record_obj.opt_len = f_size(&fs_record_obj.opt_file);
+	
+	//如果文件小于数据长度,则删除文件
+	if(fs_record_obj.opt_len < len)
+	{
+			res = f_close(&fs_record_obj.opt_file);
+		
+			res = f_unlink((char*)fs_record_obj.name_send);
+			if(res == FR_OK)
+			{
+				memset((char*)fs_record_obj.name_send, 0x00,sizeof(fs_record_obj.name_send));
+			}
+			return 0;
+	};
+	res = f_lseek(&fs_record_obj.opt_file, fs_record_obj.opt_len - len);
+	
+	res = f_read(&fs_record_obj.opt_file, buf, len, &read_len);
+
+	res = f_close(&fs_record_obj.opt_file);
+		
+	return read_len;
+}
+
+
+uint32_t func_record_delete(uint8_t *buf,uint32_t len)
+{
+	FRESULT res;
+	//uint32_t file_len = 0;
+	
+	if(strlen((char *)fs_record_obj.name_send) > 0)
+	{
+	}
+	else
+	{
+		return 0;
+	}
+	//打开文件
+	res = f_open(&fs_record_obj.opt_file, (char*)fs_record_obj.name_send, FA_WRITE | FA_OPEN_EXISTING );
+	
+	if ( res != FR_OK ) 
+	{
+		if(res == FR_NO_FILE )
+		{
+			memset((char*)fs_record_obj.name_send, 0x00,sizeof(fs_record_obj.name_send));
+			memset((char*)fs_record_obj.name_new,  0x00,sizeof(fs_record_obj.name_new));
+		}
+		return res;
+	}
+	
+	fs_record_obj.opt_len = f_size(&fs_record_obj.opt_file);
+	//如果文件小于数据长度,则删除文件
+	if(fs_record_obj.opt_len <= len)
+	{
+		res = f_close(&fs_record_obj.opt_file);
+		
+		res = f_unlink((char*)fs_record_obj.name_send);
+		if(res == FR_OK)
+		{
+			memset((char*)fs_record_obj.name_send, 0x00,sizeof(fs_record_obj.name_send));
+		}
+		return 0;			
+	};
+	
+	res = f_lseek(&fs_record_obj.opt_file, fs_record_obj.opt_len - len);
+	
+	res = f_truncate(&fs_record_obj.opt_file ); 		//截断后面的文件
+	
+	res = f_sync(&fs_record_obj.opt_file); 					//刷新关闭文件
+	
+	res = f_close(&fs_record_obj.opt_file);	
+	
+	return res;
+}
+
+
+
+/**
+  * 文件信息获取
+  */
+FRESULT func_record_file_check(void)
+{ 
+	FRESULT f_res;                    /* 文件操作结果 */	
+  static FILINFO finfo;
+  /* 获取文件信息 */
+  f_res=f_stat("TestDir/testdir.txt",&finfo);
+  if(f_res==FR_OK)
+  {
+    printf("“testdir.txt”文件信息:\n");
+    printf("》文件大小: %ld(字节)\n", finfo.fsize);
+    printf("》时间戳: %u/%02u/%02u, %02u:%02u\n",
+           (finfo.fdate >> 9) + 1980, finfo.fdate >> 5 & 15, finfo.fdate & 31,finfo.ftime >> 11, finfo.ftime >> 5 & 63);
+    printf("》属性: %c%c%c%c%c\n\n",
+           (finfo.fattrib & AM_DIR) ? 'D' : '-',      // 是一个目录
+           (finfo.fattrib & AM_RDO) ? 'R' : '-',      // 只读文件
+           (finfo.fattrib & AM_HID) ? 'H' : '-',      // 隐藏文件
+           (finfo.fattrib & AM_SYS) ? 'S' : '-',      // 系统文件
+           (finfo.fattrib & AM_ARC) ? 'A' : '-');     // 档案文件
+  }
+  return f_res;
+}
+
+/**
+  * @brief  scan_files 递归扫描FatFs内的文件
+  * @param  path:初始扫描路径
+  * @retval result:文件系统的返回值
+  */
+FRESULT func_record_scan_files (char* path) 
+{ 
+  FRESULT res; 		//部分在递归过程被修改的变量,不用全局变量	
+  FILINFO fno; 
+	DIR 		dir;	
+  int i;            
+  char *fn;        // 文件名	
+	
+#if _USE_LFN 
+  /* 长文件名支持 */
+  /* 简体中文需要2个字节保存一个“字”*/
+  static char lfn[_MAX_LFN*2 + 1]; 	
+  fno.fname = lfn; 
+  fno.fsize = sizeof(lfn); 
+#endif 
+  //打开目录
+  res = f_opendir(&dir, path); 
+  if (res == FR_OK) 
+	{ 
+    i = strlen(path); 
+    for (;;) 
+		{ 
+      //读取目录下的内容,再读会自动读下一个文件
+      res = f_readdir(&dir, &fno); 								
+      //为空时表示所有项目读取完毕,跳出
+      if (res != FR_OK || fno.fname[0] == 0) break; 	
+#if _USE_LFN 
+      fn = *fno.lfname ? fno.lfname : fno.fname; 
+#else 
+      fn = fno.fname; 
+#endif 
+      //点表示当前目录,跳过			
+      if (*fn == '.') continue; 	
+      //目录,递归读取      
+      if (fno.fattrib & AM_DIR)         
+			{ 			
+        //合成完整目录名        
+        sprintf(&path[i], "/%s", fn); 		
+        //递归遍历         
+        res = func_record_scan_files(path);	
+        path[i] = 0;         
+        //打开失败,跳出循环        
+        if (res != FR_OK) 
+					break; 
+      } 
+			else 
+			{ 
+				printf("%s/%s\n", path, fn);								//输出文件名	
+        /* 可以在这里提取特定格式的文件路径 */        
+      }//else
+    } //for
+  } 
+	
+	
+  return res; 
+}
+
+
+/*------------------------------------------------------
+ 读取指定目录下所有的文件
+ 说明:
+  f_readdir    按顺序读取目录内文件,
+ 重复调用此函数可读取目录内所有文件;
+---------------------------------------------------------*/
+void func_record_files_update(char * path)
+{		
+		char file_name[32] = {0};
+	  FRESULT res; 		//部分在递归过程被修改的变量,不用全局变量		
+	  FILINFO fno = {0}; 
+		
+		fs_record_obj.file_num = 0;				
+		memset((void *)fs_record_obj.file_list, 0x00,sizeof(fs_record_obj.file_list));		
+		memset((void *)fs_record_obj.name_send, 0x00,sizeof(fs_record_obj.name_send));
+		
+#if _USE_LFN//如果使能支持长文件名 先对下面两项初始化
+    fileinfo.lfsize=_MAX_LFN * 2 + 1;//
+    fileinfo.lfname=(TCHAR*)FileName;//
+#endif
+    if(f_opendir(&fs_record_obj.dir,(const char*)path) != FR_OK)/* 打开文件夹目录成功,目录信息已经在dir结构体中保存 */
+    {
+			res = f_mkdir((const char*)path);
+			if(res == FR_OK)
+			{
+				fs_record_obj.fatfs_ok = true;
+				return;
+			}
+			else
+			{				
+				return;
+			}
+		}		
+		
+		fs_record_obj.fatfs_ok = true;
+		
+		//打开日志路径正常,获取路文件夹内的文件列表
+		while(f_readdir(&fs_record_obj.dir, &fno) == FR_OK)  	//读文件信息到文件状态结构体中
+		{			
+				if(!fno.fname[0]) break; 													//如果文件名为‘\0',说明读取完成结束		
+			
+				if (fno.fattrib & AM_DIR)	continue; 						  //表示目录,跳过	
+			
+				if (fno.fname[0] == '.') continue; 								//点表示当前目录,跳过				
+
+						
+				if(fs_record_obj.file_num < MAX_FILE_COUNT)
+				{
+						sprintf((char *)fs_record_obj.file_list[fs_record_obj.file_num], "%s/%s", fs_record_obj.path,fno.fname);//将文件名存入列表中
+						fs_record_obj.file_num ++;	
+				}
+				else
+				{
+					/* 如果目录已经存在,关闭它 */
+					res = f_closedir(&fs_record_obj.dir);
+					
+					sprintf((char *)file_name,"%s/%s",fs_record_obj.path, fno.fname);								//输出文件名	
+					res = f_unlink(file_name);
+					if(FR_OK != res)
+					{
+						fs_record_obj.fatfs_ok = false;
+						return;
+					}
+				}
+    }
+		
+		/* 如果目录已经存在,关闭它 */
+		res = f_closedir(&fs_record_obj.dir);
+		
+		if(fs_record_obj.file_num > 0)
+		{			
+		}
+		else
+		{
+			memset((void *)fs_record_obj.name_send, 0x00,sizeof(fs_record_obj.name_send));
+			return;
+		}
+   
+		//按文件名最小到大排序;
+		if(fs_record_obj.file_num > 1)
+		{
+			func_record_dir_qsort(fs_record_obj.file_num);
+		}
+				
+		//获取最新的文件;
+		uint8_t max_idx = fs_record_obj.file_num - 1;
+		sprintf((char *)fs_record_obj.name_send,"%s", fs_record_obj.file_list[max_idx]);			
+		
+		//如果文件个数超限,
+		if(fs_record_obj.file_num > NUM_FILE_SET)
+		{			
+			//删除日期最早的文件
+			sprintf((char *)file_name,"%s",  fs_record_obj.file_list[0]);		
+			res = f_unlink(file_name);
+			if(FR_OK != res)
+			{
+				fs_record_obj.fatfs_ok = false;
+				return;
+			}									
+		}		
+			
+		UNUSED(res);		
+		return;
+}
+
+
+
+int qsort_cmp(const void *a,const void *b)
+{
+    return strcmp(( char * ) a, (const char* )b);
+}
+//(void * /*base*/, size_t /*nmemb*/, size_t /*size*/, int (* /*compar*/)(const void *, const void *)) __attribute__((__nonnull__(1,4)));
+void func_record_dir_qsort(size_t fcount)
+{
+	qsort((void *)fs_record_obj.file_list, fcount, sizeof(fs_record_obj.file_list[0]), qsort_cmp);
+}
+
+
+void func_record_init(void)
+{
+	
+	memset(fs_record_obj.file_list, 0x00,sizeof(fs_record_obj.file_list));		//清空文件列表
+	fs_record_obj.file_num = 0;
+	fs_record_obj.fatfs_ok = true;
+	func_record_files_update((char *)fs_record_obj.path);
+}
+typedef struct record_test_object
+{
+	uint32_t 	cnts;
+	uint8_t 	test_flag;
+	char 			buf[32];
+}record_test_OBJ;
+	
+record_test_OBJ record_test_obj;
+
+void func_record_test(void)
+{	
+	
+	record_test_obj.cnts++;
+	sprintf(record_test_obj.buf,"XX%08xXX",record_test_obj.cnts);
+	switch(record_test_obj.test_flag)
+	{
+		case 0:
+		{
+			func_record_write((uint8_t *)record_test_obj.buf ,strlen(record_test_obj.buf));
+		}break;
+		case 1:
+		{
+			func_record_read((uint8_t *)record_test_obj.buf ,sizeof(record_test_obj.buf));
+			func_record_delete((uint8_t *)record_test_obj.buf ,sizeof(record_test_obj.buf));
+			func_record_read((uint8_t *)record_test_obj.buf ,sizeof(record_test_obj.buf));
+		}break;
+		default:
+		{
+			func_record_read((uint8_t *)record_test_obj.buf ,sizeof(record_test_obj.buf));
+		}break;
+		
+	}
+	
+}
+
+void func_record_main(void const *argument)
+{
+	osDelay(1000);//
+	func_record_init();
+	while(1)
+	{
+		func_record_test();
+		osDelay(1000);
+	}
+}
+#endif //---------------------------USE_FATFS_RECORD----------------------------
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 75 - 0
func/func_record.h

@@ -0,0 +1,75 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.h
+  * @brief          : Header for main.c file.
+  *                   This file contains the common defines of the application.
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under BSD 3-Clause license,
+  * the "License"; You may not use this file except in compliance with the
+  * License. You may obtain a copy of the License at:
+  *                        opensource.org/licenses/BSD-3-Clause
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef FUNC_RECORD_H
+#define FUNC_RECORD_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f7xx_hal.h"
+#include "fatfs.h"
+/* Private includes ----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* Exported types ------------------------------------------------------------*/
+/* USER CODE BEGIN ET */
+
+/* USER CODE END ET */
+
+/* Exported constants --------------------------------------------------------*/
+/* USER CODE BEGIN EC */
+
+/* USER CODE END EC */
+
+/* Exported macro ------------------------------------------------------------*/
+/* USER CODE BEGIN EM */
+
+/* USER CODE END EM */
+
+
+
+/* Exported functions prototypes ---------------------------------------------*/
+void func_record_init(void);											//记录文件初始化
+void func_record_main(void const *argument);			//文件操作主函数
+void func_record_test(void);
+	
+
+uint32_t func_record_write(uint8_t *buf ,uint32_t len);
+uint32_t func_record_read(uint8_t *buf ,uint32_t len);
+uint32_t func_record_delete(uint8_t *buf,uint32_t len);
+uint8_t func_record_fatfs_set_OK(uint8_t stat);
+uint8_t func_record_fatfs_isOK(void);
+/* Private defines -----------------------------------------------------------*/
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* FUNC_RECORD_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 143 - 0
func/func_sdmmc_sdcard.c

@@ -0,0 +1,143 @@
+#ifdef USE_SDIO_SD
+/*
+#include "sys.h"
+
+#include "usart.h" 
+#include "led.h"
+#include "key.h"
+#include "ltdc.h"
+#include "lcd.h"
+#include "sdram.h"
+#include "usmart.h"
+#include "pcf8574.h"
+#include "mpu.h"
+
+*/
+#include "sys_delay.h"
+#include "sys_malloc.h"
+#include "obj_sdmmc_sdcard.h"
+/************************************************
+ ALIENTEK 阿波罗STM32H7开发板 实验40
+ SD卡实验-HAL库函数版
+ 技术支持:www.openedv.com
+ 淘宝店铺:http://eboard.taobao.com 
+ 关注微信公众平台微信号:"正点原子",免费获取STM32资料。
+ 广州市星翼电子科技有限公司  
+ 作者:正点原子 @ALIENTEK
+************************************************/
+
+//通过串口打印SD卡相关信息
+void show_sdcard_info(void)
+{
+	switch(SDCardInfo.CardType)
+	{
+		case STD_CAPACITY_SD_CARD_V1_1:printf("Card Type:SDSC V1.1\r\n");break;
+		case STD_CAPACITY_SD_CARD_V2_0:printf("Card Type:SDSC V2.0\r\n");break;
+		case HIGH_CAPACITY_SD_CARD:printf("Card Type:SDHC V2.0\r\n");break;
+		case MULTIMEDIA_CARD:printf("Card Type:MMC Card\r\n");break;
+	}	
+  	printf("Card ManufacturerID:%d\r\n",SDCardInfo.SD_cid.ManufacturerID);	//制造商ID
+ 	printf("Card RCA:%d\r\n",SDCardInfo.RCA);								//卡相对地址
+	printf("Card Capacity:%d MB\r\n",(u32)(SDCardInfo.CardCapacity>>20));	//显示容量
+ 	printf("Card BlockSize:%d\r\n\r\n",SDCardInfo.CardBlockSize);			//显示块大小
+}
+//测试SD卡的读取
+//从secaddr地址开始,读取seccnt个扇区的数据
+//secaddr:扇区地址
+//seccnt:扇区数
+void sd_test_read(u32 secaddr,u32 seccnt)
+{
+	u32 i;
+	u8 *buf;
+	u8 sta=0;
+	buf=mymalloc(SRAMIN,seccnt*512);	//申请内存,从SDRAM申请内存
+	sta=SD_ReadDisk(buf,secaddr,seccnt);//读取secaddr扇区开始的内容
+	if(sta==0)			
+	{	 
+		printf("SECTOR %d DATA:\r\n",secaddr);
+		for(i=0;i<seccnt*512;i++)printf("%x ",buf[i]);//打印secaddr开始的扇区数据    	   
+		printf("\r\nDATA ENDED\r\n"); 
+	}else printf("err:%d\r\n",sta);
+	myfree(SRAMIN,buf);	//释放内存	   
+}
+
+//测试SD卡的写入(慎用,最好写全是0XFF的扇区,否则可能损坏SD卡.)
+//从secaddr地址开始,写入seccnt个扇区的数据
+//secaddr:扇区地址
+//seccnt:扇区数
+void sd_test_write(u32 secaddr,u32 seccnt)
+{
+	u32 i;
+	u8 *buf;
+	u8 sta=0;
+	buf=mymalloc(SRAMIN,seccnt*512);	//从SDRAM申请内存
+	for(i=0;i<seccnt*512;i++) 			//初始化写入的数据,是3的倍数.
+	{
+		buf[i]=i*3;
+	}
+	sta=SD_WriteDisk(buf,secaddr,seccnt);//从secaddr扇区开始写入seccnt个扇区内容
+	if(sta==0)							
+	{	 
+		printf("Write over!\r\n"); 
+	}else printf("err:%d\r\n",sta);
+	myfree(SRAMIN,buf);					//释放内存	   
+}
+int func_sd_card_main(void)
+{
+//    u8 key;		 
+	u32 sd_size;
+	u8 t=0;	
+	u8 *buf;
+/*	
+	Cache_Enable();                			//打开L1-Cache
+	//HAL_Init();				        		//初始化HAL库
+	delay_init(400);						//延时初始化
+	uart_init(115200);						//串口初始化
+	usmart_dev.init(200); 		    		//初始化USMART	
+	LED_Init();								//初始化LED
+	KEY_Init();								//初始化按键
+	SDRAM_Init();                   		//初始化SDRAM
+	LCD_Init();								//初始化LCD
+	*/
+    my_mem_init(SRAMIN);            		//初始化内部内存池
+    //my_mem_init(SRAMEX);            		//初始化外部SDRAM内存池
+
+ 
+ 	while(SD_Init())//检测不到SD卡
+	{
+
+		sys_delay_ms(500);			
+
+		sys_delay_ms(500);
+
+	}
+	show_sdcard_info();	//打印SD卡相关信息
+									    
+	//检测SD卡成功 											    
+
+    while(1)
+    {
+
+
+			buf=mymalloc(0,512);		//申请内存
+			if(SD_ReadDisk(buf,0,1)==0)	//读取0扇区的内容
+			{	
+				printf("SECTOR 0 DATA:\r\n");
+				for(sd_size=0;sd_size<512;sd_size++)
+					printf("%x ",buf[sd_size]);//打印0扇区数据    	   
+				printf("\r\nDATA ENDED\r\n");
+
+			}
+			myfree(0,buf);//释放内存	   
+  
+		t++;
+		sys_delay_ms(10);
+		if(t==20)
+		{
+			t=0;
+		}
+	}    
+}
+
+#endif //--------------USE_SDIO_SD-------------------------
+

+ 52 - 0
func/func_sdmmc_sdcard.h

@@ -0,0 +1,52 @@
+/**
+  ******************************************************************************
+  * @file    Templates/Inc/main.h 
+  * @author  MCD Application Team
+  * @version V1.0.0
+  * @date    21-April-2017
+  * @brief   Header for main.c module
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
+  *
+  * Redistribution and use in source and binary forms, with or without modification,
+  * are permitted provided that the following conditions are met:
+  *   1. Redistributions of source code must retain the above copyright notice,
+  *      this list of conditions and the following disclaimer.
+  *   2. Redistributions in binary form must reproduce the above copyright notice,
+  *      this list of conditions and the following disclaimer in the documentation
+  *      and/or other materials provided with the distribution.
+  *   3. Neither the name of STMicroelectronics nor the names of its contributors
+  *      may be used to endorse or promote products derived from this software
+  *      without specific prior written permission.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+  *
+  ******************************************************************************
+  */
+  
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __MAIN_H
+#define __MAIN_H
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32h7xx_hal.h"
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+
+#endif /* __MAIN_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 56 - 0
mid_fatfs/fatfs.c

@@ -0,0 +1,56 @@
+/**
+  ******************************************************************************
+  * @file   fatfs.c
+  * @brief  Code for fatfs applications
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under Ultimate Liberty license
+  * SLA0044, the "License"; You may not use this file except in compliance with
+  * the License. You may obtain a copy of the License at:
+  *                             www.st.com/SLA0044
+  *
+  ******************************************************************************
+  */
+
+#include "fatfs.h"
+
+uint8_t retUSER;    /* Return value for USER */
+char USERPath[4];   /* USER logical drive path */
+FATFS USERFatFS;    /* File system object for USER logical drive */
+FIL USERFile;       /* File object for USER */
+
+/* USER CODE BEGIN Variables */
+
+/* USER CODE END Variables */
+
+void SDCARD_FATFS_Init(void)
+{
+  /*## FatFS: Link the USER driver ###########################*/
+  retUSER = FATFS_LinkDriver(&USER_Driver, USERPath);
+
+  /* USER CODE BEGIN Init */
+  /* additional user code for init */     
+  /* USER CODE END Init */
+}
+
+/**
+  * @brief  Gets Time from RTC
+  * @param  None
+  * @retval Time in DWORD
+  */
+DWORD get_fattime(void)
+{
+  /* USER CODE BEGIN get_fattime */
+  return 0;
+  /* USER CODE END get_fattime */
+}
+
+/* USER CODE BEGIN Application */
+     
+/* USER CODE END Application */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 49 - 0
mid_fatfs/fatfs.h

@@ -0,0 +1,49 @@
+/**
+  ******************************************************************************
+  * @file   fatfs.h
+  * @brief  Header for fatfs applications
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under Ultimate Liberty license
+  * SLA0044, the "License"; You may not use this file except in compliance with
+  * the License. You may obtain a copy of the License at:
+  *                             www.st.com/SLA0044
+  *
+  ******************************************************************************
+  */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __fatfs_H
+#define __fatfs_H
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+#include "ff.h"
+#include "ff_gen_drv.h"
+#include "user_diskio.h" /* defines USER_Driver as external */
+
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+extern uint8_t retUSER; /* Return value for USER */
+extern char USERPath[4]; /* USER logical drive path */
+extern FATFS USERFatFS; /* File system object for USER logical drive */
+extern FIL USERFile; /* File object for USER */
+
+void SDCARD_FATFS_Init(void);
+
+/* USER CODE BEGIN Prototypes */
+
+/* USER CODE END Prototypes */
+#ifdef __cplusplus
+}
+#endif
+#endif /*__fatfs_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 273 - 0
mid_fatfs/ffconf.h

@@ -0,0 +1,273 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  *  FatFs - Generic FAT file system module  R0.12c (C)ChaN, 2017
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under Ultimate Liberty license
+  * SLA0044, the "License"; You may not use this file except in compliance with
+  * the License. You may obtain a copy of the License at:
+  *                             www.st.com/SLA0044
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+#ifndef _FFCONF
+#define _FFCONF 68300	/* Revision ID */
+
+/*-----------------------------------------------------------------------------/
+/ Additional user header to be used
+/-----------------------------------------------------------------------------*/
+#include "main.h"
+#include "stm32f7xx_hal.h"
+
+#include "cmsis_os.h" /* _FS_REENTRANT set to 1 and CMSIS API chosen */
+/* Handle for USB Host */
+#define hUSB_Host hUsbHostFS
+
+/*-----------------------------------------------------------------------------/
+/ Function Configurations
+/-----------------------------------------------------------------------------*/
+
+#define _FS_READONLY         0      /* 0:Read/Write or 1:Read only */
+/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
+/  Read-only configuration removes writing API functions, f_write(), f_sync(),
+/  f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
+/  and optional writing functions as well. */
+
+#define _FS_MINIMIZE         0      /* 0 to 3 */
+/* This option defines minimization level to remove some basic API functions.
+/
+/   0: All basic functions are enabled.
+/   1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
+/      are removed.
+/   2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
+/   3: f_lseek() function is removed in addition to 2. */
+
+#define _USE_STRFUNC         2      /* 0:Disable or 1-2:Enable */
+/* This option switches string functions, f_gets(), f_putc(), f_puts() and
+/  f_printf().
+/
+/  0: Disable string functions.
+/  1: Enable without LF-CRLF conversion.
+/  2: Enable with LF-CRLF conversion. */
+
+#define _USE_FIND            0
+/* This option switches filtered directory read functions, f_findfirst() and
+/  f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
+
+#define _USE_MKFS            1
+/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
+
+#define _USE_FASTSEEK        1
+/* This option switches fast seek feature. (0:Disable or 1:Enable) */
+
+#define	_USE_EXPAND		0
+/* This option switches f_expand function. (0:Disable or 1:Enable) */
+
+#define _USE_CHMOD		0
+/* This option switches attribute manipulation functions, f_chmod() and f_utime().
+/  (0:Disable or 1:Enable) Also _FS_READONLY needs to be 0 to enable this option. */
+
+#define _USE_LABEL           0
+/* This option switches volume label functions, f_getlabel() and f_setlabel().
+/  (0:Disable or 1:Enable) */
+
+#define _USE_FORWARD         0
+/* This option switches f_forward() function. (0:Disable or 1:Enable) */
+
+/*-----------------------------------------------------------------------------/
+/ Locale and Namespace Configurations
+/-----------------------------------------------------------------------------*/
+
+#define _CODE_PAGE         936
+/* This option specifies the OEM code page to be used on the target system.
+/  Incorrect setting of the code page can cause a file open failure.
+/
+/   1   - ASCII (No extended character. Non-LFN cfg. only)
+/   437 - U.S.
+/   720 - Arabic
+/   737 - Greek
+/   771 - KBL
+/   775 - Baltic
+/   850 - Latin 1
+/   852 - Latin 2
+/   855 - Cyrillic
+/   857 - Turkish
+/   860 - Portuguese
+/   861 - Icelandic
+/   862 - Hebrew
+/   863 - Canadian French
+/   864 - Arabic
+/   865 - Nordic
+/   866 - Russian
+/   869 - Greek 2
+/   932 - Japanese (DBCS)
+/   936 - Simplified Chinese (DBCS)
+/   949 - Korean (DBCS)
+/   950 - Traditional Chinese (DBCS)
+*/
+
+#define _USE_LFN     0    /* 0 to 3 */
+#define _MAX_LFN     255  /* Maximum LFN length to handle (12 to 255) */
+/* The _USE_LFN switches the support of long file name (LFN).
+/
+/   0: Disable support of LFN. _MAX_LFN has no effect.
+/   1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
+/   2: Enable LFN with dynamic working buffer on the STACK.
+/   3: Enable LFN with dynamic working buffer on the HEAP.
+/
+/  To enable the LFN, Unicode handling functions (option/unicode.c) must be added
+/  to the project. The working buffer occupies (_MAX_LFN + 1) * 2 bytes and
+/  additional 608 bytes at exFAT enabled. _MAX_LFN can be in range from 12 to 255.
+/  It should be set 255 to support full featured LFN operations.
+/  When use stack for the working buffer, take care on stack overflow. When use heap
+/  memory for the working buffer, memory management functions, ff_memalloc() and
+/  ff_memfree(), must be added to the project. */
+
+#define _LFN_UNICODE    0 /* 0:ANSI/OEM or 1:Unicode */
+/* This option switches character encoding on the API. (0:ANSI/OEM or 1:UTF-16)
+/  To use Unicode string for the path name, enable LFN and set _LFN_UNICODE = 1.
+/  This option also affects behavior of string I/O functions. */
+
+#define _STRF_ENCODE    3
+/* When _LFN_UNICODE == 1, this option selects the character encoding ON THE FILE to
+/  be read/written via string I/O functions, f_gets(), f_putc(), f_puts and f_printf().
+/
+/  0: ANSI/OEM
+/  1: UTF-16LE
+/  2: UTF-16BE
+/  3: UTF-8
+/
+/  This option has no effect when _LFN_UNICODE == 0. */
+
+#define _FS_RPATH       1 /* 0 to 2 */
+/* This option configures support of relative path.
+/
+/   0: Disable relative path and remove related functions.
+/   1: Enable relative path. f_chdir() and f_chdrive() are available.
+/   2: f_getcwd() function is available in addition to 1.
+*/
+
+/*---------------------------------------------------------------------------/
+/ Drive/Volume Configurations
+/----------------------------------------------------------------------------*/
+
+#define _VOLUMES    2
+/* Number of volumes (logical drives) to be used. */
+
+/* USER CODE BEGIN Volumes */
+#define _STR_VOLUME_ID          0	/* 0:Use only 0-9 for drive ID, 1:Use strings for drive ID */
+#define _VOLUME_STRS            "RAM","NAND","CF","SD1","SD2","USB1","USB2","USB3"
+/* _STR_VOLUME_ID switches string support of volume ID.
+/  When _STR_VOLUME_ID is set to 1, also pre-defined strings can be used as drive
+/  number in the path name. _VOLUME_STRS defines the drive ID strings for each
+/  logical drives. Number of items must be equal to _VOLUMES. Valid characters for
+/  the drive ID strings are: A-Z and 0-9. */
+/* USER CODE END Volumes */
+
+#define _MULTI_PARTITION     0 /* 0:Single partition, 1:Multiple partition */
+/* This option switches support of multi-partition on a physical drive.
+/  By default (0), each logical drive number is bound to the same physical drive
+/  number and only an FAT volume found on the physical drive will be mounted.
+/  When multi-partition is enabled (1), each logical drive number can be bound to
+/  arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
+/  function will be available. */
+#define _MIN_SS    512  /* 512, 1024, 2048 or 4096 */
+#define _MAX_SS    4096  /* 512, 1024, 2048 or 4096 */
+/* These options configure the range of sector size to be supported. (512, 1024,
+/  2048 or 4096) Always set both 512 for most systems, all type of memory cards and
+/  harddisk. But a larger value may be required for on-board flash memory and some
+/  type of optical media. When _MAX_SS is larger than _MIN_SS, FatFs is configured
+/  to variable sector size and GET_SECTOR_SIZE command must be implemented to the
+/  disk_ioctl() function. */
+
+#define	_USE_TRIM      0
+/* This option switches support of ATA-TRIM. (0:Disable or 1:Enable)
+/  To enable Trim function, also CTRL_TRIM command should be implemented to the
+/  disk_ioctl() function. */
+
+#define _FS_NOFSINFO    0 /* 0,1,2 or 3 */
+/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
+/  option, and f_getfree() function at first time after volume mount will force
+/  a full FAT scan. Bit 1 controls the use of last allocated cluster number.
+/
+/  bit0=0: Use free cluster count in the FSINFO if available.
+/  bit0=1: Do not trust free cluster count in the FSINFO.
+/  bit1=0: Use last allocated cluster number in the FSINFO if available.
+/  bit1=1: Do not trust last allocated cluster number in the FSINFO.
+*/
+
+/*---------------------------------------------------------------------------/
+/ System Configurations
+/----------------------------------------------------------------------------*/
+
+#define _FS_TINY    0      /* 0:Normal or 1:Tiny */
+/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
+/  At the tiny configuration, size of file object (FIL) is reduced _MAX_SS bytes.
+/  Instead of private sector buffer eliminated from the file object, common sector
+/  buffer in the file system object (FATFS) is used for the file data transfer. */
+
+#define _FS_EXFAT	0
+/* This option switches support of exFAT file system. (0:Disable or 1:Enable)
+/  When enable exFAT, also LFN needs to be enabled. (_USE_LFN >= 1)
+/  Note that enabling exFAT discards C89 compatibility. */
+
+#define _FS_NORTC	0
+#define _NORTC_MON	6
+#define _NORTC_MDAY	4
+#define _NORTC_YEAR	2015
+/* The option _FS_NORTC switches timestamp functiton. If the system does not have
+/  any RTC function or valid timestamp is not needed, set _FS_NORTC = 1 to disable
+/  the timestamp function. All objects modified by FatFs will have a fixed timestamp
+/  defined by _NORTC_MON, _NORTC_MDAY and _NORTC_YEAR in local time.
+/  To enable timestamp function (_FS_NORTC = 0), get_fattime() function need to be
+/  added to the project to get current time form real-time clock. _NORTC_MON,
+/  _NORTC_MDAY and _NORTC_YEAR have no effect.
+/  These options have no effect at read-only configuration (_FS_READONLY = 1). */
+
+#define _FS_LOCK    2     /* 0:Disable or >=1:Enable */
+/* The option _FS_LOCK switches file lock function to control duplicated file open
+/  and illegal operation to open objects. This option must be 0 when _FS_READONLY
+/  is 1.
+/
+/  0:  Disable file lock function. To avoid volume corruption, application program
+/      should avoid illegal open, remove and rename to the open objects.
+/  >0: Enable file lock function. The value defines how many files/sub-directories
+/      can be opened simultaneously under file lock control. Note that the file
+/      lock control is independent of re-entrancy. */
+
+#define _FS_REENTRANT    1  /* 0:Disable or 1:Enable */
+
+#define _USE_MUTEX       0 /* 0:Disable or 1:Enable */
+#define _FS_TIMEOUT      1000 /* Timeout period in unit of time ticks */
+#define _SYNC_t          osSemaphoreId
+/* The option _FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
+/  module itself. Note that regardless of this option, file access to different
+/  volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
+/  and f_fdisk() function, are always not re-entrant. Only file/directory access
+/  to the same volume is under control of this function.
+/
+/   0: Disable re-entrancy. _FS_TIMEOUT and _SYNC_t have no effect.
+/   1: Enable re-entrancy. Also user provided synchronization handlers,
+/      ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
+/      function, must be added to the project. Samples are available in
+/      option/syscall.c.
+/
+/  The _FS_TIMEOUT defines timeout period in unit of time tick.
+/  The _SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
+/  SemaphoreHandle_t and etc.. A header file for O/S definitions needs to be
+/  included somewhere in the scope of ff.h. */
+
+/* define the ff_malloc ff_free macros as FreeRTOS pvPortMalloc and vPortFree macros */
+#if !defined(ff_malloc) && !defined(ff_free)
+#define ff_malloc  pvPortMalloc
+#define ff_free  vPortFree
+#endif
+
+#endif /* _FFCONF */

+ 170 - 0
mid_fatfs/user_diskio.c

@@ -0,0 +1,170 @@
+/* USER CODE BEGIN Header */
+/**
+ ******************************************************************************
+  * @file    user_diskio.c
+  * @brief   This file includes a diskio driver skeleton to be completed by the user.
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under Ultimate Liberty license
+  * SLA0044, the "License"; You may not use this file except in compliance with
+  * the License. You may obtain a copy of the License at:
+  *                             www.st.com/SLA0044
+  *
+  ******************************************************************************
+  */
+ /* 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"
+#include "obj_spi_sd_stm32.h"
+
+/* Private typedef -----------------------------------------------------------*/
+/* 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 = SD_disk_initialize(pdrv);
+    return Stat;
+  /* 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 = SD_disk_status(pdrv);
+    return Stat;
+  /* 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 */
+    return SD_disk_read(pdrv, buff, sector, count);
+  /* 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 */
+    return SD_disk_write(pdrv, buff, sector, count);
+  /* 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 = SD_disk_ioctl(pdrv, cmd, buff);
+    return res;
+  /* USER CODE END IOCTL */
+}
+#endif /* _USE_IOCTL == 1 */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 46 - 0
mid_fatfs/user_diskio.h

@@ -0,0 +1,46 @@
+/* USER CODE BEGIN Header */
+/**
+ ******************************************************************************
+  * @file    user_diskio.h
+  * @brief   This file contains the common defines and functions prototypes for  
+  *          the user_diskio driver.
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
+  * All rights reserved.</center></h2>
+  *
+  * This software component is licensed by ST under Ultimate Liberty license
+  * SLA0044, the "License"; You may not use this file except in compliance with
+  * the License. You may obtain a copy of the License at:
+  *                             www.st.com/SLA0044
+  *
+  ******************************************************************************
+  */
+ /* USER CODE END Header */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USER_DISKIO_H
+#define __USER_DISKIO_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/* USER CODE BEGIN 0 */
+
+/* Includes ------------------------------------------------------------------*/
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+extern Diskio_drvTypeDef  USER_Driver;
+
+/* USER CODE END 0 */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USER_DISKIO_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/