Procházet zdrojové kódy

1.增加基于little fs 的驱动接口文件

libo před 2 roky
rodič
revize
63de18ddda

+ 211 - 0
mid_littlefs/mid_littlefs_flash.c

@@ -0,0 +1,211 @@
+
+#include "main.h"
+
+#if USE_LFS_RECORD==1
+#define USE_MID_LFS_FLASH
+#endif
+
+#ifdef USE_MID_LFS_FLASH
+
+#include <stdio.h>
+#include "obj_spi_w25qxx.h"
+#include "obj_hal_w25qxx.h"
+#include "func_spi_w25qxx.h"
+#include "mid_littlefs_flash.h"
+
+// lfs for W25Q128 *********************************************************/
+//The W25Q128BV array is organized into 65,536 programmable pages of 256-bytes each.
+//128Mbit ==> 16Mbyte ==> 16*1024*1024 ==> 4*1024*block==>
+//-------------------------------------------------------------------------//
+lfs_t lfs; 
+lfs_file_t lfs_file;
+
+// configuration of the filesystem is provided by this struct
+#pragma pack(4) /*4字节对齐*/
+typedef struct lfs_sys_bufffer
+{
+	uint32_t read[LFS_CACH_SIZE/4];
+	uint32_t prog[LFS_CACH_SIZE/4];
+	uint32_t lookahead[LFS_CACH_SIZE/4/8];
+}lfs_sys_buff;
+
+lfs_sys_buff lfs_sys_buf = {0};
+
+
+/* lfs for STM32Flash ********************************************************/
+lfs_t lfs_flash; 
+lfs_file_t file_flash;
+#pragma pack()/*还原默认对齐*/
+/* Private function prototypes -----------------------------------------------*/
+int flash_block_read(const struct lfs_config *c, lfs_block_t block,
+            lfs_off_t off, void *buffer, lfs_size_t size);
+int flash_block_prog(const struct lfs_config *c, lfs_block_t block,
+            lfs_off_t off, const void *buffer, lfs_size_t size);
+int flash_block_erase(const struct lfs_config *c, lfs_block_t block);
+int flash_block_sync(const struct lfs_config *c);
+
+int lfs_file_open_user(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) ;
+
+// configuration of the filesystem is provided by this struct
+const struct lfs_config cfg_flash = {
+    // block device operations
+    .read  = flash_block_read,
+    .prog  = flash_block_prog,
+    .erase = flash_block_erase,
+    .sync  = flash_block_sync,
+
+    // block device configuration
+    .read_size = LFS_SECT_SIZE,
+    .prog_size = LFS_SECT_SIZE,
+	
+    .block_size = LFS_BLOCK_SIZE,
+    .block_count = LFS_BLOCK_CNT,
+	
+    .cache_size = LFS_CACH_SIZE,
+	
+    .lookahead_size = LFS_CACH_SIZE/8,
+	
+    .block_cycles = 500,
+		
+		.read_buffer = lfs_sys_buf.read,	
+	  .prog_buffer = lfs_sys_buf.prog,	
+	
+		.lookahead_buffer = lfs_sys_buf.lookahead,	
+};
+
+void mid_lfs_flash_init(void)
+{
+	int err = -1;	
+	int erase_flag = 0;	
+	
+	err = lfs_mount(&lfs_flash, &cfg_flash);	
+	
+	while( err != LFS_ERR_OK )
+	{ 
+		
+		if(erase_flag)
+		{
+			W25QXX_Erase_Chip();
+			lfs_format(&lfs_flash, &cfg_flash);
+			err = lfs_mount(&lfs_flash, &cfg_flash);
+			erase_flag = 0;
+		}
+
+		osDelay(1000);
+	}		
+	return;
+}
+
+int mid_lfs_flash_get_free(void)
+{
+	return 0xffffff;
+}
+
+
+
+
+int mid_lfs_flash_bootcnt(void)
+{
+	
+	//while(1)
+	{		
+		uint32_t boot_count = 0;
+		lfs_file_open_static(&lfs_flash, &file_flash, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
+		lfs_file_read(&lfs_flash, &file_flash, &boot_count, sizeof(boot_count));
+
+		
+		// update boot count
+		boot_count += 1;
+		lfs_file_rewind(&lfs_flash, &file_flash);  // seek the file to begin
+		lfs_file_write(&lfs_flash, &file_flash, &boot_count, sizeof(boot_count));
+
+		// remember the storage is not updated until the file is closed successfully
+		lfs_file_close(&lfs_flash, &file_flash);
+
+//	// release any resources we were using
+
+
+		// print the boot count
+		printf("boot_count: %d\n", boot_count);
+	};
+	return 0;
+}
+
+int mid_lfs_flash_boot_step(void)
+{
+	
+	//while(1)
+	{		
+		uint32_t boot_count = 0;
+		lfs_file_open_static(&lfs_flash, &file_flash, "boot_step", LFS_O_RDWR | LFS_O_CREAT);
+		lfs_file_read(&lfs_flash, &file_flash, &boot_count, sizeof(boot_count));
+
+		
+		// update boot count
+		boot_count += 1;
+		lfs_file_rewind(&lfs_flash, &file_flash);  // seek the file to begin
+		lfs_file_write(&lfs_flash, &file_flash, &boot_count, sizeof(boot_count));
+
+		// remember the storage is not updated until the file is closed successfully
+		lfs_file_close(&lfs_flash, &file_flash);
+
+//	// release any resources we were using
+
+
+		// print the boot count
+		printf("boot_count: %d\n", boot_count);
+	};
+	return 0;
+}
+
+int flash_block_read(const struct lfs_config *c, lfs_block_t block,
+            lfs_off_t off, void *buffer, lfs_size_t size)
+{
+
+		W25QXX_Read((uint8_t*)buffer, (uint32_t)((block * LFS_BLOCK_SIZE) + off),  (uint16_t)size);
+    return 0;
+}            
+
+int flash_block_prog(const struct lfs_config *c, lfs_block_t block,
+            lfs_off_t off, const void *buffer, lfs_size_t size)
+{
+	
+    //W25QXX_Write((uint8_t*)buffer, block * LFS_BLOCK_SIZE + off,  size);
+		W25QXX_Write_NoCheck((uint8_t*)buffer, block * LFS_BLOCK_SIZE + off,  size);
+    return 0;
+}
+
+int flash_block_erase(const struct lfs_config *c, lfs_block_t block)
+{
+	  W25QXX_Erase_Sector(block);
+    return 0;
+}
+
+int flash_block_sync(const struct lfs_config *c)
+{
+    return 0;
+}
+#if 0
+#pragma pack(4) /*4字节对齐*/
+//uint32_t file_buffer[LFS_CACH_SIZE/4];
+#pragma pack()/*还原默认对齐*/
+#else
+static uint8_t file_buffer[LFS_CACH_SIZE] __attribute__((aligned(4)));
+#endif
+
+struct lfs_file_config file_cfg = {.attr_count = 0, .attrs = 0, .buffer = file_buffer};
+
+int lfs_file_open_static(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) 
+{
+	memset(&file_cfg,0x00,sizeof(struct lfs_file_config));
+	file_cfg.buffer = file_buffer;		
+	memset(file_buffer,0x00,sizeof(file_buffer));
+	memset(file,0x00,sizeof(lfs_file_t));
+	return lfs_file_opencfg(lfs, file, path, flags, &file_cfg);
+}
+
+
+
+#endif //---------------USE_MID_LFS_FLASH---------------------//
+
+//------------------------the end of file-------------------------//

+ 41 - 0
mid_littlefs/mid_littlefs_flash.h

@@ -0,0 +1,41 @@
+/*********************************************************
+//file		:hd_dev_gpio.h
+//author	:libo
+//date		:2020/05/10
+//version	:V1.0
+//brief		:GSP HAL²ãGPIO½Ó¿ÚHÎļþ
+*********************************************************/
+
+
+#ifndef MID_LITTLEFS_FLASH_H
+#define MID_LITTLEFS_FLASH_H
+
+/* Includes----------------------------------------------------------------------------------*/
+#include "lfs.h"
+
+/* Public macro------------------------------------------------------------------------------*/
+#define LFS_PAGE_SIZE   	256
+#define LFS_SECT_SIZE   	(LFS_PAGE_SIZE*2)
+#define LFS_BLOCK_SIZE   	(LFS_PAGE_SIZE*16)
+#define LFS_BLOCK_CNT   	(1024)
+
+#define LFS_CACH_SIZE   	(LFS_SECT_SIZE)
+
+ 
+/* Public define-----------------------------------------------------------------------------*/
+
+/* Public typedef----------------------------------------------------------------------------*/
+
+/* public function---------------------------------------------------------------------------*/
+extern lfs_t lfs_flash; ;
+
+void mid_lfs_flash_init(void);
+int  mid_lfs_flash_bootcnt(void);
+int  mid_lfs_flash_boot_step(void);
+int  mid_lfs_flash_get_free(void);
+
+int lfs_file_open_static(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) ;
+#endif    /*********MID_LITTLEFS_FLASH_H****************/
+
+/******************************the end of file************************************************/
+

+ 159 - 0
mid_littlefs/mid_littlefs_ram.c

@@ -0,0 +1,159 @@
+
+#ifdef  USE_MID_LFS_RAM
+#include <stdio.h>
+#include "lfs.h"
+#include "mid_littlefs_ram.h"
+#include "cmsis_os.h"
+ 
+#define LFS_SECT_SIZE   	64
+//#define LFS_BLOCK_SIZE   	(LFS_SECT_SIZE*4)
+#define LFS_BLOCK_SIZE		64
+#define LFS_BLOCK_CNT   	32
+ 
+/* lfs for W25Q128 *********************************************************/
+lfs_t lfs; 
+lfs_file_t file;
+
+// configuration of the filesystem is provided by this struct
+
+typedef struct lfs_sys_bufffer
+{
+	uint8_t read[LFS_BLOCK_SIZE];
+	uint8_t prog[LFS_BLOCK_SIZE];
+	
+	uint32_t lookahead[LFS_BLOCK_CNT/(4*8)+1];
+}lfs_sys_buff;
+
+lfs_sys_buff lfs_sys_buf = {0};
+
+uint8_t ram_block[LFS_BLOCK_CNT][LFS_SECT_SIZE] = {0};
+
+/* lfs for STM32Flash ********************************************************/
+lfs_t lfs_Stm32_ram; 
+lfs_file_t file_ram;
+
+
+
+// configuration of the filesystem is provided by this struct
+const struct lfs_config cfg_Stm32Flash = {
+    // block device operations
+    .read  = ram_block_read,
+    .prog  = ram_block_prog,
+    .erase = ram_block_erase,
+    .sync  = ram_block_sync,
+
+    // block device configuration
+    .read_size 		= LFS_SECT_SIZE,
+    .prog_size 		= LFS_SECT_SIZE,
+    .block_size 	= LFS_BLOCK_SIZE,
+    .block_count 	= LFS_BLOCK_CNT,
+	
+    .cache_size 	= LFS_BLOCK_SIZE,
+	
+    .lookahead_size = sizeof(lfs_sys_buf.lookahead),
+    .block_cycles = 500,
+		
+		//
+		.read_buffer = lfs_sys_buf.read,	
+	  .prog_buffer = lfs_sys_buf.prog,	
+	
+		.lookahead_buffer = lfs_sys_buf.lookahead,	
+};
+
+
+
+
+int mid_little_fs_ram_main(void)
+{
+	int err = -1;
+	
+	
+	err = lfs_mount(&lfs_Stm32_ram, &cfg_Stm32Flash);
+	
+	if( err )
+	{
+		lfs_format(&lfs_Stm32_ram, &cfg_Stm32Flash);
+		lfs_mount(&lfs_Stm32_ram, &cfg_Stm32Flash);
+	}
+	
+		
+	
+	while(1)
+	{
+
+		
+		uint32_t boot_count = 0;
+		lfs_file_open_user(&lfs_Stm32_ram, &file_ram, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
+		lfs_file_read(&lfs_Stm32_ram, &file_ram, &boot_count, sizeof(boot_count));
+
+		
+		// update boot count
+		boot_count += 1;
+		lfs_file_rewind(&lfs_Stm32_ram, &file_ram);  // seek the file to begin
+		lfs_file_write(&lfs_Stm32_ram, &file_ram, &boot_count, sizeof(boot_count));
+
+		// remember the storage is not updated until the file is closed successfully
+		lfs_file_close(&lfs_Stm32_ram, &file_ram);
+
+//		// release any resources we were using
+   
+
+		// print the boot count
+		printf("boot_count: %d\n", boot_count);
+		
+		osDelay(1000);
+
+	}
+	//lfs_unmount(&lfs);
+}
+
+
+
+int ram_block_read(const struct lfs_config *c, lfs_block_t block,
+            lfs_off_t off, void *buffer, lfs_size_t size)
+{
+    uint8_t* pSrc 	= (uint8_t*)&ram_block[block][0] + off;
+    uint8_t* pDest = (uint8_t*)buffer;
+	
+    memcpy(pDest,pSrc,size);
+
+    return 0;
+}            
+
+int ram_block_prog(const struct lfs_config *c, lfs_block_t block,
+            lfs_off_t off, const void *buffer, lfs_size_t size)
+{
+
+    uint8_t* pDest = (uint8_t*)&ram_block[block][0] + off;
+    uint8_t* pSrc = (uint8_t*)buffer;
+	
+    memcpy(pDest,pSrc,size);
+    return 0;
+}
+
+int ram_block_erase(const struct lfs_config *c, lfs_block_t block)
+{
+    uint8_t* pDest = (uint8_t*)&ram_block[block][0];
+	
+    memset(pDest,0xff,c->block_size);
+    return 0;
+}
+
+int ram_block_sync(const struct lfs_config *c)
+{
+    return 0;
+}
+
+// Statically allocated file buffer. Must be cache_size.
+static uint8_t file_buffer[LFS_BLOCK_SIZE];
+
+struct lfs_file_config file_cfg = {
+		.buffer = file_buffer
+};
+
+int lfs_file_open_user(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) 
+{
+	return lfs_file_opencfg(lfs, file, path, flags, &file_cfg);
+}
+
+#endif	//---------------------USE_MID_LFS_RAM-----------------------//

+ 34 - 0
mid_littlefs/mid_littlefs_ram.h

@@ -0,0 +1,34 @@
+/*********************************************************
+//file		:mid_littlefs_ram.h
+//author	:libo
+//date		:2022/05/10
+//version	:V1.0
+//brief		:GSP HAL²ãGPIO½Ó¿ÚHÎļþ
+*********************************************************/
+#ifdef USE_MID_LFS_RAM
+
+#ifndef MID_LITTLEFS_RAM_H
+#define MID_LITTLEFS_RAM_H
+
+/* Includes----------------------------------------------------------------------------------*/
+#include "lfs.h"
+/* Public macro------------------------------------------------------------------------------*/
+
+/* Public define-----------------------------------------------------------------------------*/
+
+/* Public typedef----------------------------------------------------------------------------*/
+
+/* public function---------------------------------------------------------------------------*/
+int ram_block_read(const struct lfs_config *c, lfs_block_t block,
+            lfs_off_t off, void *buffer, lfs_size_t size);
+int ram_block_prog(const struct lfs_config *c, lfs_block_t block,
+            lfs_off_t off, const void *buffer, lfs_size_t size);
+int ram_block_erase(const struct lfs_config *c, lfs_block_t block);
+int ram_block_sync(const struct lfs_config *c);
+
+int lfs_file_open_user(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) ;
+int mid_little_fs_ram_main(void);
+#endif    /*********MID_LITTLEFS_RAM_H****************/
+#endif    /*********USE_MID_LFS_RAM*******************/
+/******************************the end of file************************************************/
+