Browse Source

fix:lf 擦除范围不全

Dozingfiretruck 5 months ago
parent
commit
77bd1552f8

+ 7 - 7
components/little_flash/luat_lib_little_flash.c

@@ -218,13 +218,13 @@ static int luat_little_flash_mount(lua_State *L) {
     size_t maxsize = luaL_optinteger(L, 4, 0);
     lfs_t* lfs = flash_lfs_lf(flash, offset, maxsize);
     if (lfs) {
-	    luat_fs_conf_t conf = {
-		    .busname = (char*)lfs,
-		    .type = "lfs2",
-		    .filesystem = "lfs2",
-		    .mount_point = mount_point,
-	    };
-	    int ret = luat_fs_mount(&conf);
+        luat_fs_conf_t conf = {
+            .busname = (char*)lfs,
+            .type = "lfs2",
+            .filesystem = "lfs2",
+            .mount_point = mount_point,
+        };
+        int ret = luat_fs_mount(&conf);
         LLOGD("vfs mount %s ret %d", mount_point, ret);
         lua_pushboolean(L, 1);
     }

+ 19 - 6
components/little_flash/luat_little_flash_lfs2.c

@@ -17,22 +17,24 @@ static size_t lf_offset = 0;
 // Read a block
 static int lf_block_device_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
     little_flash_t* flash = (little_flash_t*)cfg->context;
+    // LLOGD("lf_block_device_read block:%d off:%d size:%d", block, off, size);
     return little_flash_read(flash, lf_offset + block * flash->chip_info.erase_size + off, buffer, size);
-    // LLOGD("lf_block_device_read ret %d", ret);
+    // LLOGD("lf_block_device_read ret:%d", ret);
     // return LFS_ERR_OK;
 }
 
 static int lf_block_device_prog(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
     little_flash_t* flash = (little_flash_t*)cfg->context;
+    // LLOGD("lf_block_device_prog block:%d off:%d size:%d", block, off, size);
     return little_flash_write(flash, lf_offset + block * flash->chip_info.erase_size + off, buffer, size);
-    // LLOGD("lf_block_device_prog ret %d", ret);
+    // LLOGD("lf_block_device_prog ret:%d", ret);
     // return LFS_ERR_OK;
 }
 
 static int lf_block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
     little_flash_t* flash = (little_flash_t*)cfg->context;
     return little_flash_erase(flash, lf_offset + block * flash->chip_info.erase_size, flash->chip_info.erase_size);
-    // LLOGD("lf_block_device_erase ret %d", ret);
+    // LLOGD("lf_block_device_erase ret:%d block:%d", ret, block);
     // return LFS_ERR_OK;
 }
 
@@ -40,9 +42,13 @@ static int lf_block_device_sync(const struct lfs_config *cfg) {
     return LFS_ERR_OK;
 }
 
+#define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
 typedef struct LFS2 {
     lfs_t lfs;
     struct lfs_config cfg;
+    uint8_t* lookahead_buffer[LFS_BLOCK_DEVICE_LOOK_AHEAD];
+    uint8_t* read_buffer;
+    uint8_t* prog_buffer;
 }LFS2_t;
 
 lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize) {
@@ -54,6 +60,7 @@ lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize) {
     if (_lfs == NULL)
         return NULL;
     memset(_lfs, 0, sizeof(LFS2_t));
+
     lf_offset = offset;
     lfs_t *lfs = &_lfs->lfs;
     struct lfs_config *lfs_cfg = &_lfs->cfg;
@@ -70,10 +77,16 @@ lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize) {
     lfs_cfg->prog_size = flash->chip_info.prog_size;
     lfs_cfg->block_size = flash->chip_info.erase_size;
     lfs_cfg->block_count = (maxsize > 0 ? maxsize : (flash->chip_info.capacity - offset)) / flash->chip_info.erase_size;
-    lfs_cfg->block_cycles = 400;
+    lfs_cfg->block_cycles = 200;
     lfs_cfg->cache_size = flash->chip_info.prog_size;
-    lfs_cfg->lookahead_size = flash->chip_info.prog_size;
+    lfs_cfg->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
+
+    _lfs->read_buffer = luat_heap_malloc(flash->chip_info.prog_size);
+    _lfs->prog_buffer = luat_heap_malloc(flash->chip_info.prog_size);
 
+    lfs_cfg->read_buffer = _lfs->read_buffer;
+    lfs_cfg->prog_buffer = _lfs->prog_buffer;
+    lfs_cfg->lookahead_buffer = _lfs->lookahead_buffer;
     lfs_cfg->name_max = 63;
     lfs_cfg->file_max = 0;
     lfs_cfg->attr_max = 0;
@@ -85,7 +98,7 @@ lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize) {
 
     // ------
     int err = lfs_mount(lfs, lfs_cfg);
-    // LLOGD("lfs_mount %d",err);
+    LLOGD("lfs_mount %d",err);
     if (err)
     {
         err = lfs_format(lfs, lfs_cfg);

+ 26 - 19
components/little_flash/src/little_flash.c

@@ -279,6 +279,7 @@ lf_err_t little_flash_device_init(little_flash_t *lf){
     uint8_t recv_data[4]={0};
     result = lf->spi.transfer(lf,(uint8_t[]){LF_CMD_JEDEC_ID}, 1, recv_data, sizeof(recv_data));
     if(result) return result;
+    // LF_DEBUG("recv_data [0]:0x%02X [1]:0x%02X [2]:0x%02X [3]:0x%02X",recv_data[0],recv_data[1],recv_data[2],recv_data[3]);
 
     // nor flash?
     manufacturer_id = recv_data[0];
@@ -439,22 +440,28 @@ lf_err_t little_flash_erase(const little_flash_t *lf, uint32_t addr, uint32_t le
 
     if(little_flash_write_enabled(lf, LF_ENABLE)) goto error;
     cmd_data[0] = lf->chip_info.erase_cmd;
-    while (len){
-        uint32_t page_addr = addr/lf->chip_info.prog_size;
-        cmd_data[1] = page_addr >> 16;
-        cmd_data[2] = page_addr >> 8;
-        cmd_data[3] = page_addr;
+    uint32_t erase_off = addr % lf->chip_info.erase_size;
+    uint32_t erase_addr = addr / lf->chip_info.erase_size * lf->chip_info.erase_size;
+    uint32_t erase_len = len - erase_off;
+    while (erase_off || erase_len){
+        cmd_data[1] = erase_addr >> 16;
+        cmd_data[2] = erase_addr >> 8;
+        cmd_data[3] = erase_addr;
         lf->spi.transfer(lf,cmd_data, 4,LF_NULL,0);
 
         lf->wait_ms(lf->chip_info.erase_times);
 
         if(little_flash_cheak_erase(lf)) goto error;
 
-        addr += lf->chip_info.erase_size;
-        if (len<=lf->chip_info.erase_size){
+        if (erase_len == 0){
             break;
+        }
+        
+        erase_addr += lf->chip_info.erase_size;
+        if (erase_len<=lf->chip_info.erase_size){
+            erase_len = 0;
         }else{
-            len -= lf->chip_info.erase_size;
+            erase_len -= lf->chip_info.erase_size;
         }
     }
 
@@ -470,7 +477,7 @@ error:
     if (lf->unlock) {
         lf->unlock(lf);
     }
-    return LF_ERR_READ;
+    return LF_ERR_ERASE;
 }
 
 lf_err_t little_flash_write(const little_flash_t *lf, uint32_t addr, const uint8_t *data, uint32_t len){
@@ -623,8 +630,8 @@ lf_err_t little_flash_read(const little_flash_t *lf, uint32_t addr, uint8_t *dat
         }
     }else{
         while (len){
-            uint32_t page_addr = addr/lf->chip_info.prog_size;
-            uint16_t column_addr = addr%lf->chip_info.prog_size;
+            uint32_t page_addr = addr/lf->chip_info.read_size;
+            uint16_t column_addr = addr%lf->chip_info.read_size;
 
             cmd_data[0] = LF_NANDFLASH_PAGE_DATA_READ;
             cmd_data[1] = page_addr >> 16;
@@ -639,22 +646,22 @@ lf_err_t little_flash_read(const little_flash_t *lf, uint32_t addr, uint8_t *dat
             cmd_data[2] = column_addr;
             cmd_data[3] = 0;
             if (column_addr){
-                if ((column_addr+len)<=lf->chip_info.prog_size){
+                if ((column_addr+len)<=lf->chip_info.read_size){
                     lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],len);
                     break;
                 }else{
-                    lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],lf->chip_info.prog_size-column_addr);
-                    len -= (lf->chip_info.prog_size-column_addr);
-                    addr += (lf->chip_info.prog_size-column_addr);
+                    lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],lf->chip_info.read_size-column_addr);
+                    len -= (lf->chip_info.read_size-column_addr);
+                    addr += (lf->chip_info.read_size-column_addr);
                 }
             }else{
-                if (len<=lf->chip_info.prog_size){
+                if (len<=lf->chip_info.read_size){
                     lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],len);
                     break;
                 }else{
-                    lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],lf->chip_info.prog_size);
-                    len -= lf->chip_info.prog_size;
-                    addr += lf->chip_info.prog_size;
+                    lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],lf->chip_info.read_size);
+                    len -= lf->chip_info.read_size;
+                    addr += lf->chip_info.read_size;
                 }
             }
         }