mirror of
https://gitee.com/Armink/EasyFlash.git
synced 2024-12-04 05:09:41 +08:00
Merge pull request #37 from redocCheng/master
修复日志扇区初始化和针对stm32的LOGIC更改
This commit is contained in:
commit
6cfb878b9f
@ -30,30 +30,30 @@
|
|||||||
|
|
||||||
#ifdef EF_USING_LOG
|
#ifdef EF_USING_LOG
|
||||||
|
|
||||||
/* magic code on every sector header. 'EF' is 0x4546 */
|
/* magic code on every sector header. 'EF' is 0xEF30EF30 */
|
||||||
#define LOG_SECTOR_MAGIC 0x4546
|
#define LOG_SECTOR_MAGIC 0xEF30EF30
|
||||||
/* sector header size, includes the sector magic code and status magic code */
|
/* sector header size, includes the sector magic code and status magic code */
|
||||||
#define LOG_SECTOR_HEADER_SIZE 4
|
#define LOG_SECTOR_HEADER_SIZE 12
|
||||||
|
/* sector header word size,what is equivalent to the total number of sectors header index */
|
||||||
|
#define LOG_SECTOR_HEADER_WORD_SIZE 3
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sector status magic code
|
* Sector status magic code
|
||||||
* The sector status is 16-Bits after LOG_SECTOR_MAGIC at every sector header.
|
* The sector status is 8B after LOG_SECTOR_MAGIC at every sector header.
|
||||||
* =======================
|
* ==============================================
|
||||||
* | header(4B) | status |
|
* | header(12B) | status |
|
||||||
* -----------------------
|
* ----------------------------------------------
|
||||||
* | 0x4546FFFF | empty |
|
* | 0xEF30EF30 0xFFFFFFFF 0xFFFFFFFF | empty |
|
||||||
* | 0x4546FFFE | using |
|
* | 0xEF30EF30 0xFEFEFEFE 0xFFFFFFFF | using |
|
||||||
* | 0x4546FFFC | full |
|
* | 0xEF30EF30 0xFEFEFEFE 0xFCFCFCFC | full |
|
||||||
* =======================
|
* ==============================================
|
||||||
*
|
*
|
||||||
* State transition relationship: empty->using->full
|
* State transition relationship: empty->using->full
|
||||||
* The FULL status will change to EMPTY after sector clean.
|
* The FULL status will change to EMPTY after sector clean.
|
||||||
*/
|
*/
|
||||||
enum {
|
#define SECTOR_STATUS_MAGIC_EMPUT 0xFFFFFFFF
|
||||||
SECTOR_STATUS_MAGIC_EMPUT = 0xFFFF,
|
#define SECTOR_STATUS_MAGIC_USING 0xFEFEFEFE
|
||||||
SECTOR_STATUS_MAGIC_USING = 0xFFFE,
|
#define SECTOR_STATUS_MAGIC_FULL 0xFCFCFCFC
|
||||||
SECTOR_STATUS_MAGIC_FULL = 0xFFFC,
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SECTOR_STATUS_EMPUT,
|
SECTOR_STATUS_EMPUT,
|
||||||
@ -62,6 +62,12 @@ typedef enum {
|
|||||||
SECTOR_STATUS_HEADER_ERROR,
|
SECTOR_STATUS_HEADER_ERROR,
|
||||||
} SectorStatus;
|
} SectorStatus;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
SECTOR_HEADER_MAGIC_INDEX,
|
||||||
|
SECTOR_HEADER_USING_INDEX,
|
||||||
|
SECTOR_HEADER_FULL_INDEX,
|
||||||
|
} SectorHeaderIndex;
|
||||||
|
|
||||||
/* the stored logs start address and end address. It's like a ring buffer implemented on flash. */
|
/* the stored logs start address and end address. It's like a ring buffer implemented on flash. */
|
||||||
static uint32_t log_start_addr = 0, log_end_addr = 0;
|
static uint32_t log_start_addr = 0, log_end_addr = 0;
|
||||||
/* saved log area address for flash */
|
/* saved log area address for flash */
|
||||||
@ -109,34 +115,37 @@ EfErrCode ef_log_init(void) {
|
|||||||
* @return the flash sector current status
|
* @return the flash sector current status
|
||||||
*/
|
*/
|
||||||
static SectorStatus get_sector_status(uint32_t addr) {
|
static SectorStatus get_sector_status(uint32_t addr) {
|
||||||
uint32_t header = 0, header_addr = 0;
|
uint32_t header_buf[LOG_SECTOR_HEADER_WORD_SIZE] = {0}, header_addr = 0;
|
||||||
uint16_t sector_magic = 0, status_magic = 0;
|
uint32_t sector_header_magic = 0;
|
||||||
|
uint32_t status_full_magic = 0, status_use_magic = 0;
|
||||||
|
|
||||||
/* calculate the sector header address */
|
/* calculate the sector header address */
|
||||||
header_addr = addr / EF_ERASE_MIN_SIZE * EF_ERASE_MIN_SIZE;
|
header_addr = addr / EF_ERASE_MIN_SIZE * EF_ERASE_MIN_SIZE;
|
||||||
|
|
||||||
if (ef_port_read(header_addr, &header, sizeof(header)) == EF_NO_ERR) {
|
if (ef_port_read(header_addr, header_buf, sizeof(header_buf)) == EF_NO_ERR) {
|
||||||
sector_magic = header >> 16;
|
sector_header_magic = header_buf[SECTOR_HEADER_MAGIC_INDEX];
|
||||||
status_magic = header;
|
status_use_magic = header_buf[SECTOR_HEADER_USING_INDEX];
|
||||||
|
status_full_magic = header_buf[SECTOR_HEADER_FULL_INDEX];
|
||||||
} else {
|
} else {
|
||||||
EF_DEBUG("Error: Read sector header data error.\n");
|
EF_DEBUG("Error: Read sector header data error.\n");
|
||||||
return SECTOR_STATUS_HEADER_ERROR;
|
return SECTOR_STATUS_HEADER_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compare header magic code */
|
/* compare header magic code */
|
||||||
if (sector_magic == LOG_SECTOR_MAGIC) {
|
if(sector_magic == LOG_SECTOR_MAGIC){
|
||||||
switch (status_magic) {
|
if((status_use_magic == SECTOR_STATUS_MAGIC_EMPUT) && (status_full_magic == SECTOR_STATUS_MAGIC_EMPUT)) {
|
||||||
case SECTOR_STATUS_MAGIC_EMPUT:
|
|
||||||
return SECTOR_STATUS_EMPUT;
|
return SECTOR_STATUS_EMPUT;
|
||||||
case SECTOR_STATUS_MAGIC_USING:
|
} else if((status_use_magic == SECTOR_STATUS_MAGIC_USING) && (status_full_magic == SECTOR_STATUS_MAGIC_EMPUT)) {
|
||||||
return SECTOR_STATUS_USING;
|
return SECTOR_STATUS_USING;
|
||||||
case SECTOR_STATUS_MAGIC_FULL:
|
} else if((status_use_magic == SECTOR_STATUS_MAGIC_USING) && (status_full_magic == SECTOR_STATUS_MAGIC_FULL)) {
|
||||||
return SECTOR_STATUS_FULL;
|
return SECTOR_STATUS_FULL;
|
||||||
default:
|
} else {
|
||||||
return SECTOR_STATUS_HEADER_ERROR;
|
return SECTOR_STATUS_HEADER_ERROR;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return SECTOR_STATUS_HEADER_ERROR;
|
return SECTOR_STATUS_HEADER_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -148,29 +157,33 @@ static SectorStatus get_sector_status(uint32_t addr) {
|
|||||||
* @return result
|
* @return result
|
||||||
*/
|
*/
|
||||||
static EfErrCode write_sector_status(uint32_t addr, SectorStatus status) {
|
static EfErrCode write_sector_status(uint32_t addr, SectorStatus status) {
|
||||||
uint32_t header = 0, header_addr = 0;
|
uint32_t header_buf[LOG_SECTOR_HEADER_WORD_SIZE] = {0}, header_addr = 0;
|
||||||
uint16_t status_magic;
|
|
||||||
|
|
||||||
/* calculate the sector header address */
|
/* calculate the sector header address */
|
||||||
header_addr = addr / EF_ERASE_MIN_SIZE * EF_ERASE_MIN_SIZE;
|
header_addr = addr / EF_ERASE_MIN_SIZE * EF_ERASE_MIN_SIZE;
|
||||||
|
|
||||||
|
/* calculate the sector staus magic */
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case SECTOR_STATUS_EMPUT: {
|
case SECTOR_STATUS_EMPUT: {
|
||||||
status_magic = SECTOR_STATUS_MAGIC_EMPUT;
|
header_buf[SECTOR_HEADER_USING_INDEX] = SECTOR_STATUS_MAGIC_EMPUT;
|
||||||
|
header_buf[SECTOR_HEADER_FULL_INDEX] = SECTOR_STATUS_MAGIC_EMPUT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SECTOR_STATUS_USING: {
|
case SECTOR_STATUS_USING: {
|
||||||
status_magic = SECTOR_STATUS_MAGIC_USING;
|
header_buf[SECTOR_HEADER_USING_INDEX] = SECTOR_STATUS_MAGIC_USING;
|
||||||
|
header_buf[SECTOR_HEADER_FULL_INDEX] = SECTOR_STATUS_MAGIC_EMPUT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SECTOR_STATUS_FULL: {
|
case SECTOR_STATUS_FULL: {
|
||||||
status_magic = SECTOR_STATUS_MAGIC_FULL;
|
header_buf[SECTOR_HEADER_USING_INDEX] = SECTOR_STATUS_MAGIC_USING;
|
||||||
|
header_buf[SECTOR_HEADER_FULL_INDEX] = SECTOR_STATUS_MAGIC_FULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
header = (LOG_SECTOR_MAGIC << 16) | status_magic;
|
|
||||||
|
header_buf[SECTOR_HEADER_MAGIC_INDEX] = LOG_SECTOR_MAGIC;
|
||||||
return ef_port_write(header_addr, &header, sizeof(header));
|
|
||||||
|
return ef_port_write(header_addr, header_buf, sizeof(header_buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -263,6 +276,7 @@ static void find_start_and_end_addr(void) {
|
|||||||
/* get the first sector status */
|
/* get the first sector status */
|
||||||
cur_sec_status = get_sector_status(log_area_start_addr);
|
cur_sec_status = get_sector_status(log_area_start_addr);
|
||||||
last_sec_status = cur_sec_status;
|
last_sec_status = cur_sec_status;
|
||||||
|
|
||||||
|
|
||||||
for (cur_size = EF_ERASE_MIN_SIZE; cur_size < LOG_AREA_SIZE; cur_size += EF_ERASE_MIN_SIZE) {
|
for (cur_size = EF_ERASE_MIN_SIZE; cur_size < LOG_AREA_SIZE; cur_size += EF_ERASE_MIN_SIZE) {
|
||||||
/* get current sector status */
|
/* get current sector status */
|
||||||
@ -332,8 +346,8 @@ static void find_start_and_end_addr(void) {
|
|||||||
/* like state 2 when the sector is the last one */
|
/* like state 2 when the sector is the last one */
|
||||||
if (cur_size + EF_ERASE_MIN_SIZE >= LOG_AREA_SIZE) {
|
if (cur_size + EF_ERASE_MIN_SIZE >= LOG_AREA_SIZE) {
|
||||||
cur_log_sec_state = 2;
|
cur_log_sec_state = 2;
|
||||||
log_start_addr = log_area_start_addr + cur_size;
|
log_start_addr = get_next_flash_sec_addr(log_area_start_addr + cur_size);
|
||||||
cur_using_sec_addr = log_area_start_addr + cur_size - EF_ERASE_MIN_SIZE;
|
cur_using_sec_addr = log_area_start_addr + cur_size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -372,6 +386,7 @@ static void find_start_and_end_addr(void) {
|
|||||||
/* find the end address */
|
/* find the end address */
|
||||||
log_end_addr = find_sec_using_end_addr(cur_using_sec_addr);
|
log_end_addr = find_sec_using_end_addr(cur_using_sec_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user