From 2883158fd08dc8173eee3d693ee81802eb723990 Mon Sep 17 00:00:00 2001 From: armink Date: Sun, 28 Aug 2016 22:56:03 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91F?= =?UTF-8?q?lash=20=E5=AD=98=E5=82=A8=E6=97=A5=E5=BF=97=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=96=E9=83=A8=20Flash=20=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82=E6=84=9F=E8=B0=A2=E7=BD=91=E5=8F=8B=20@Teddy?= =?UTF-8?q?=5FLe=20=E7=9A=84=E5=8F=8D=E9=A6=88=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- easyflash/inc/easyflash.h | 12 ++++---- easyflash/src/ef_utils.c | 65 ++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/easyflash/inc/easyflash.h b/easyflash/inc/easyflash.h index b1cafec..dceefe8 100644 --- a/easyflash/inc/easyflash.h +++ b/easyflash/inc/easyflash.h @@ -67,7 +67,7 @@ if (!(EXPR)) \ while (1); \ } /* EasyFlash software version number */ -#define EF_SW_VERSION "2.07.28" +#define EF_SW_VERSION "2.08.26" typedef struct _ef_env{ char *key; @@ -86,10 +86,10 @@ typedef enum { /* the flash sector current status */ typedef enum { - FLASH_SECTOR_EMPTY, - FLASH_SECTOR_USING, - FLASH_SECTOR_FULL, -}FlashSecrorStatus; + EF_SECTOR_EMPTY, + EF_SECTOR_USING, + EF_SECTOR_FULL, +}EfSecrorStatus; /* easyflash.c */ EfErrCode easyflash_init(void); @@ -127,7 +127,7 @@ size_t ef_log_get_used_size(void); /* ef_utils.c */ uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size); -FlashSecrorStatus ef_get_sector_status(uint32_t addr, size_t sec_size); +EfSecrorStatus ef_get_sector_status(uint32_t addr, size_t sec_size); uint32_t ef_find_sec_using_end_addr(uint32_t addr, size_t sec_size); /* ef_port.c */ diff --git a/easyflash/src/ef_utils.c b/easyflash/src/ef_utils.c index da26f63..4c5161f 100644 --- a/easyflash/src/ef_utils.c +++ b/easyflash/src/ef_utils.c @@ -106,15 +106,15 @@ uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size) * * @return the flash sector current status */ -FlashSecrorStatus ef_get_sector_status(uint32_t addr, size_t sec_size) { +EfSecrorStatus ef_get_sector_status(uint32_t addr, size_t sec_size) { uint32_t cur_using_addr = ef_find_sec_using_end_addr(addr, sec_size); /* get current status by current using address */ - if (cur_using_addr == addr) { - return FLASH_SECTOR_EMPTY; - } else if (cur_using_addr < addr + sec_size - 4) { - return FLASH_SECTOR_USING; + if (cur_using_addr == 0 || cur_using_addr == addr - 4) { + return EF_SECTOR_EMPTY; + } else if (cur_using_addr == addr + sec_size - 4) { + return EF_SECTOR_FULL; } else { - return FLASH_SECTOR_FULL; + return EF_SECTOR_USING; } } @@ -127,37 +127,38 @@ FlashSecrorStatus ef_get_sector_status(uint32_t addr, size_t sec_size) { * @return current flash sector using end address */ uint32_t ef_find_sec_using_end_addr(uint32_t addr, size_t sec_size) { - size_t start, continue_ff; - /* counts continuous 0xFF */ - for (start = 0; start < sec_size; start++) { - if (*(uint8_t *) (addr + start) == 0xFF) { - /* make sure it is not the last byte */ - if (start < sec_size - 1) { - /* start counts */ - for (continue_ff = 1; continue_ff < sec_size - start; continue_ff++) { - if (*(uint8_t *) (addr + start + continue_ff) != 0xFF) { - start += continue_ff; - break; - } - } - /* all sector counts finish */ - if (continue_ff == sec_size - start) { - /* must be word alignment */ - if (start % 4 != 0) { - start = (start / 4 + 1) * 4; - } - break; - } +/* read section data buffer size */ +#define READ_BUF_SIZE 32 + + uint32_t start = addr, continue_ff = 0, read_buf_size = 0, i; + uint8_t buf[READ_BUF_SIZE]; + + EF_ASSERT(READ_BUF_SIZE % 4 == 0); + /* counts continuous 0xFF which is end of sector */ + while (start < addr + sec_size) { + if (start + READ_BUF_SIZE < addr + sec_size) { + read_buf_size = READ_BUF_SIZE; + } else { + read_buf_size = addr + sec_size - start; + } + ef_port_read(start, (uint32_t *)buf, read_buf_size); + for (i = 0; i < read_buf_size; i++) { + if (buf[i] == 0xFF) { + continue_ff++; + } else { + continue_ff = 0; } } + start += read_buf_size; } /* calculate current flash sector using end address */ - if ((start == 0) && (continue_ff == sec_size)) { + if (continue_ff == sec_size) { /* from 0 to sec_size all sector is 0xFF, so the sector is empty */ - return addr; - } else if (start < sec_size) { - /* form start to sec_size all area is 0xFF, so it's used part of the sector */ - return addr + start; + return addr >= 4 ? addr - 4 : 0; + } else if (continue_ff >= 4) { + /* form end_addr - 4 to sec_size length all area is 0xFF, so it's used part of the sector. + * the address must be word alignment. */ + return (addr + sec_size - continue_ff) * 4 / 4 - 4; } else { /* all sector not has continuous 0xFF, alignment by word */ return addr + sec_size - 4;