diff --git a/docs/zh/api.md b/docs/zh/api.md index f8e0591..1fae360 100644 --- a/docs/zh/api.md +++ b/docs/zh/api.md @@ -222,7 +222,19 @@ FlashErrCode flash_write(uint32_t addr, const uint32_t *buf, size_t size) |buf |源数据的缓冲区| |size |写入数据的大小(字节)| -### 2.4 打印调试日志信息 +### 2.4 对环境变量缓冲区加锁 + +```C +void flash_env_lock(void) +``` + +### 2.5 对环境变量缓冲区解锁 + +```C +void flash_env_unlock(void) +``` + +### 2.6 打印调试日志信息 在定义 `FLASH_PRINT_DEBUG` 宏后,打印调试日志信息 @@ -237,7 +249,7 @@ void flash_log_debug(const char *file, const long line, const char *format, ...) |format |打印格式| |... |不定参| -### 2.5 打印普通日志信息 +### 2.7 打印普通日志信息 ```C void flash_log_info(const char *format, ...) @@ -248,7 +260,7 @@ void flash_log_info(const char *format, ...) |format |打印格式| |... |不定参| -### 2.6 无格式打印信息 +### 2.8 无格式打印信息 该方法输出无固定格式的打印信息,为 `flash_print_env` 方法所用。而 `flash_log_debug` 及 `flash_log_info` 可以输出带指定前缀及格式的打印日志信息。 diff --git a/easyflash/inc/flash.h b/easyflash/inc/flash.h index b24c858..85746fa 100644 --- a/easyflash/inc/flash.h +++ b/easyflash/inc/flash.h @@ -56,7 +56,7 @@ if (!(EXPR)) \ while (1); \ } /* EasyFlash software version number */ -#define FLASH_SW_VERSION "1.05.25" +#define FLASH_SW_VERSION "1.05.30" typedef struct _flash_env{ char *key; @@ -103,6 +103,8 @@ FlashErrCode flash_copy_bl_from_bak(uint32_t bl_addr, size_t bl_size); FlashErrCode flash_read(uint32_t addr, uint32_t *buf, size_t size); FlashErrCode flash_erase(uint32_t addr, size_t size); FlashErrCode flash_write(uint32_t addr, const uint32_t *buf, size_t size); +void flash_env_lock(void); +void flash_env_unlock(void); void flash_log_debug(const char *file, const long line, const char *format, ...); void flash_log_info(const char *format, ...); void flash_print(const char *format, ...); diff --git a/easyflash/port/flash_port.c b/easyflash/port/flash_port.c index b1fb3a9..3572b24 100644 --- a/easyflash/port/flash_port.c +++ b/easyflash/port/flash_port.c @@ -128,6 +128,25 @@ FlashErrCode flash_write(uint32_t addr, const uint32_t *buf, size_t size) { return result; } +/** + * lock the ENV ram cache + */ +void flash_env_lock(void) { + + /* You can add your code under here. */ + +} + +/** + * unlock the ENV ram cache + */ +void flash_env_unlock(void) { + + /* You can add your code under here. */ + +} + + /** * This function is print flash debug info. * diff --git a/easyflash/src/flash_env.c b/easyflash/src/flash_env.c index 9134645..dfbd2c3 100644 --- a/easyflash/src/flash_env.c +++ b/easyflash/src/flash_env.c @@ -70,6 +70,7 @@ static uint32_t get_env_end_addr(void); static void set_env_end_addr(uint32_t end_addr); static FlashErrCode write_env(const char *key, const char *value); static uint32_t *find_env(const char *key); +static FlashErrCode del_env(const char *key); static size_t get_env_data_size(void); static FlashErrCode create_env(const char *key, const char *value); @@ -127,6 +128,9 @@ FlashErrCode flash_env_set_default(void){ FLASH_ASSERT(default_env_set); FLASH_ASSERT(default_env_set_size); + /* lock the ENV cache */ + flash_env_lock(); + /* set environment end address is at data section start address */ set_env_end_addr(get_env_data_addr()); @@ -135,6 +139,9 @@ FlashErrCode flash_env_set_default(void){ create_env(default_env_set[i].key, default_env_set[i].value); } + /* unlock the ENV cache */ + flash_env_unlock(); + flash_save_env(); return result; @@ -196,8 +203,6 @@ static size_t get_env_data_size(void) { * @return size */ size_t flash_get_env_total_size(void) { - /* must be initialized */ - return FLASH_USER_SETTING_ENV_SIZE; } @@ -262,7 +267,8 @@ static FlashErrCode write_env(const char *key, const char *value) { */ static uint32_t *find_env(const char *key) { uint32_t *env_cache_addr = NULL; - char *env_start, *env_end, *env, *env_bak; + char *env_start, *env_end, *env; + size_t key_len = strlen(key), env_len; FLASH_ASSERT(env_start_addr); @@ -282,18 +288,18 @@ static uint32_t *find_env(const char *key) { env = env_start; while (env < env_end) { - /* storage model is key=value\0 */ - env_bak = strstr(env, key); - /* the key name length must be equal */ - if (env_bak && (env_bak[strlen(key)] == '=')) { - env_cache_addr = (uint32_t *) env_bak; + /* the key length must be equal */ + if (!strncmp(env, key, key_len) && (env[key_len] == '=')) { + env_cache_addr = (uint32_t *) env; break; } else { + /* calculate ENV length, contain '\0'. */ + env_len = strlen(env) + 1; /* next ENV and word alignment */ - if ((strlen(env) + 1) % 4 == 0) { - env += strlen(env) + 1; + if (env_len % 4 == 0) { + env += env_len; } else { - env += ((strlen(env) + 1) / 4 + 1) * 4; + env += (env_len / 4 + 1) * 4; } } } @@ -320,7 +326,7 @@ static FlashErrCode create_env(const char *key, const char *value) { return FLASH_ENV_NAME_ERR; } - if (strstr(key, "=")) { + if (strchr(key, '=')) { FLASH_INFO("Flash ENV name can't contain '='.\n"); return FLASH_ENV_NAME_ERR; } @@ -343,7 +349,7 @@ static FlashErrCode create_env(const char *key, const char *value) { * * @return result */ -FlashErrCode flash_del_env(const char *key){ +static FlashErrCode del_env(const char *key){ FlashErrCode result = FLASH_NO_ERR; char *del_env_str = NULL; size_t del_env_length, remain_env_length; @@ -355,13 +361,14 @@ FlashErrCode flash_del_env(const char *key){ return FLASH_ENV_NAME_ERR; } - if (strstr(key, "=")) { + if (strchr(key, '=')) { FLASH_INFO("Flash ENV name or value can't contain '='.\n"); return FLASH_ENV_NAME_ERR; } /* find ENV */ del_env_str = (char *) find_env(key); + if (!del_env_str) { FLASH_INFO("Not find \"%s\" in ENV.\n", key); return FLASH_ENV_NAME_ERR; @@ -396,18 +403,24 @@ FlashErrCode flash_del_env(const char *key){ FlashErrCode flash_set_env(const char *key, const char *value) { FlashErrCode result = FLASH_NO_ERR; + /* lock the ENV cache */ + flash_env_lock(); + /* if ENV value is empty, delete it */ if (*value == NULL) { - result = flash_del_env(key); + result = del_env(key); } else { /* if find this ENV, then delete it and recreate it */ if (find_env(key)) { - result = flash_del_env(key); + result = del_env(key); } if (result == FLASH_NO_ERR) { result = create_env(key, value); } } + /* unlock the ENV cache */ + flash_env_unlock(); + return result; } @@ -424,11 +437,12 @@ char *flash_get_env(const char *key) { /* find ENV */ env_cache_addr = find_env(key); + if (env_cache_addr == NULL) { return NULL; } /* get value address */ - value = strstr((char *) env_cache_addr, "="); + value = strchr((char *) env_cache_addr, '='); if (value != NULL) { /* the equal sign next character is value */ value++; @@ -483,7 +497,6 @@ void flash_load_env(void) { /* read ENV CRC code from flash */ flash_read(get_env_system_addr() + ENV_PARAM_INDEX_DATA_CRC * 4, &env_cache[ENV_PARAM_INDEX_DATA_CRC] , 4); - /* if ENV CRC32 check is fault, set default for it */ if (!env_crc_is_ok()) { FLASH_INFO("Warning: ENV CRC check failed. Set it to default.\n"); diff --git a/easyflash/src/flash_env_wl.c b/easyflash/src/flash_env_wl.c index 26f967f..53cb47e 100644 --- a/easyflash/src/flash_env_wl.c +++ b/easyflash/src/flash_env_wl.c @@ -88,6 +88,7 @@ static uint32_t *find_env(const char *key); static size_t get_env_detail_size(void); static size_t get_env_user_used_size(void); static FlashErrCode create_env(const char *key, const char *value); +static FlashErrCode del_env(const char *key); static FlashErrCode save_cur_using_data_addr(uint32_t cur_data_addr); #ifdef FLASH_ENV_USING_CRC_CHECK @@ -144,6 +145,9 @@ FlashErrCode flash_env_set_default(void){ FLASH_ASSERT(default_env_set); FLASH_ASSERT(default_env_set_size); + /* lock the ENV cache */ + flash_env_lock(); + /* set ENV detail part end address is at ENV detail part start address */ set_env_detail_end_addr(get_env_detail_addr()); @@ -152,6 +156,9 @@ FlashErrCode flash_env_set_default(void){ create_env(default_env_set[i].key, default_env_set[i].value); } + /* unlock the ENV cache */ + flash_env_unlock(); + flash_save_env(); return result; @@ -313,7 +320,8 @@ static FlashErrCode write_env(const char *key, const char *value) { */ static uint32_t *find_env(const char *key) { uint32_t *env_cache_addr = NULL; - char *env_start, *env_end, *env, *env_bak; + char *env_start, *env_end, *env; + size_t key_len = strlen(key), env_len; FLASH_ASSERT(cur_using_data_addr); @@ -333,21 +341,22 @@ static uint32_t *find_env(const char *key) { env = env_start; while (env < env_end) { - /* storage model is key=value\0 */ - env_bak = strstr(env, key); - /* the key name length must be equal */ - if (env_bak && (env_bak[strlen(key)] == '=')) { - env_cache_addr = (uint32_t *) env_bak; + /* the key length must be equal */ + if (!strncmp(env, key, key_len) && (env[key_len] == '=')) { + env_cache_addr = (uint32_t *) env; break; } else { + /* calculate ENV length, contain '\0'. */ + env_len = strlen(env) + 1; /* next ENV and word alignment */ - if ((strlen(env) + 1) % 4 == 0) { - env += strlen(env) + 1; + if (env_len % 4 == 0) { + env += env_len; } else { - env += ((strlen(env) + 1) / 4 + 1) * 4; + env += (env_len / 4 + 1) * 4; } } } + return env_cache_addr; } @@ -371,7 +380,7 @@ static FlashErrCode create_env(const char *key, const char *value) { return FLASH_ENV_NAME_ERR; } - if (strstr(key, "=")) { + if (strchr(key, '=')) { FLASH_INFO("Flash ENV name can't contain '='.\n"); return FLASH_ENV_NAME_ERR; } @@ -394,7 +403,7 @@ static FlashErrCode create_env(const char *key, const char *value) { * * @return result */ -FlashErrCode flash_del_env(const char *key){ +static FlashErrCode del_env(const char *key) { FlashErrCode result = FLASH_NO_ERR; char *del_env_str = NULL; size_t del_env_length, remain_env_length; @@ -406,13 +415,14 @@ FlashErrCode flash_del_env(const char *key){ return FLASH_ENV_NAME_ERR; } - if (strstr(key, "=")) { + if (strchr(key, '=')) { FLASH_INFO("Flash ENV name or value can't contain '='.\n"); return FLASH_ENV_NAME_ERR; } /* find ENV */ del_env_str = (char *) find_env(key); + if (!del_env_str) { FLASH_INFO("Not find \"%s\" in ENV.\n", key); return FLASH_ENV_NAME_ERR; @@ -447,18 +457,24 @@ FlashErrCode flash_del_env(const char *key){ FlashErrCode flash_set_env(const char *key, const char *value) { FlashErrCode result = FLASH_NO_ERR; + /* lock the ENV cache */ + flash_env_lock(); + /* if ENV value is empty, delete it */ if (*value == NULL) { - result = flash_del_env(key); + result = del_env(key); } else { /* if find this ENV, then delete it and recreate it */ if (find_env(key)) { - result = flash_del_env(key); + result = del_env(key); } if (result == FLASH_NO_ERR) { result = create_env(key, value); } } + /* unlock the ENV cache */ + flash_env_unlock(); + return result; } @@ -475,11 +491,12 @@ char *flash_get_env(const char *key) { /* find ENV */ env_cache_addr = find_env(key); + if (env_cache_addr == NULL) { return NULL; } /* get value address */ - value = strstr((char *) env_cache_addr, "="); + value = strchr((char *) env_cache_addr, '='); if (value != NULL) { /* the equal sign next character is value */ value++; @@ -549,15 +566,15 @@ void flash_load_env(void) { /* read ENV CRC code from flash */ flash_read(get_cur_using_data_addr() + ENV_PARAM_PART_INDEX_DATA_CRC * 4, &env_cache[ENV_PARAM_PART_INDEX_DATA_CRC], 4); - /* if ENV CRC32 check is fault, set default for it */ if (!env_crc_is_ok()) { FLASH_INFO("Warning: ENV CRC check failed. Set it to default.\n"); flash_env_set_default(); } - } #endif + } + } }