From a6edd12e067975c9cd0dfe48756a88ecd3c11966 Mon Sep 17 00:00:00 2001 From: armink Date: Tue, 26 Mar 2019 20:14:47 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=8A=9F=E8=83=BD=E5=9C=A8=20STM32F10X=20=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E4=B8=8A=E6=97=A0=E6=B3=95=E4=BD=BF=E7=94=A8=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E6=89=87=E5=8C=BA=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= =?UTF-8?q?=E6=84=9F=E8=B0=A2=E7=BD=91=E5=8F=8B=20@Bamwell=20@redocCheng?= =?UTF-8?q?=20@hjgqx996=20=E7=9A=84=E5=8F=8D=E9=A6=88=E4=B8=8E=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- easyflash/src/ef_log.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/easyflash/src/ef_log.c b/easyflash/src/ef_log.c index e7747a3..d69affd 100644 --- a/easyflash/src/ef_log.c +++ b/easyflash/src/ef_log.c @@ -1,7 +1,7 @@ /* * This file is part of the EasyFlash Library. * - * Copyright (c) 2015-2017, Armink, + * Copyright (c) 2015-2019, Armink, * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -161,7 +161,7 @@ static SectorStatus get_sector_status(uint32_t addr) { * @return result */ static EfErrCode write_sector_status(uint32_t addr, SectorStatus status) { - uint32_t header_buf[LOG_SECTOR_HEADER_WORD_SIZE] = {0}, header_addr = 0; + uint32_t header, header_addr = 0; /* calculate the sector header address */ header_addr = addr / EF_ERASE_MIN_SIZE * EF_ERASE_MIN_SIZE; @@ -169,25 +169,20 @@ static EfErrCode write_sector_status(uint32_t addr, SectorStatus status) { /* calculate the sector staus magic */ switch (status) { case SECTOR_STATUS_EMPUT: { - header_buf[SECTOR_HEADER_USING_INDEX] = SECTOR_STATUS_MAGIC_EMPUT; - header_buf[SECTOR_HEADER_FULL_INDEX] = SECTOR_STATUS_MAGIC_EMPUT; - break; + header = LOG_SECTOR_MAGIC; + return ef_port_write(header_addr, &header, sizeof(header)); } case SECTOR_STATUS_USING: { - header_buf[SECTOR_HEADER_USING_INDEX] = SECTOR_STATUS_MAGIC_USING; - header_buf[SECTOR_HEADER_FULL_INDEX] = SECTOR_STATUS_MAGIC_EMPUT; - break; + header = SECTOR_STATUS_MAGIC_USING; + return ef_port_write(header_addr + sizeof(header), &header, sizeof(header)); } case SECTOR_STATUS_FULL: { - header_buf[SECTOR_HEADER_USING_INDEX] = SECTOR_STATUS_MAGIC_USING; - header_buf[SECTOR_HEADER_FULL_INDEX] = SECTOR_STATUS_MAGIC_FULL; - break; + header = SECTOR_STATUS_MAGIC_FULL; + return ef_port_write(header_addr + sizeof(header) * 2, &header, sizeof(header)); } + default: + return EF_WRITE_ERR; } - - header_buf[SECTOR_HEADER_MAGIC_INDEX] = LOG_SECTOR_MAGIC; - - return ef_port_write(header_addr, header_buf, sizeof(header_buf)); } /** @@ -280,7 +275,6 @@ static void find_start_and_end_addr(void) { /* get the first sector status */ cur_sec_status = get_sector_status(log_area_start_addr); last_sec_status = cur_sec_status; - for (cur_size = EF_ERASE_MIN_SIZE; cur_size < LOG_AREA_SIZE; cur_size += EF_ERASE_MIN_SIZE) { /* get current sector status */ @@ -638,7 +632,8 @@ EfErrCode ef_log_write(const uint32_t *log, size_t size) { if (result != EF_NO_ERR) { goto exit; } - /* change the sector status to USING when write begin sector start address */ + /* change the sector status to EMPTY and USING when write begin sector start address */ + result = write_sector_status(write_addr, SECTOR_STATUS_EMPUT); result = write_sector_status(write_addr, SECTOR_STATUS_USING); if (result == EF_NO_ERR) { write_addr += LOG_SECTOR_HEADER_SIZE; @@ -710,7 +705,8 @@ EfErrCode ef_log_clean(void) { if (result != EF_NO_ERR) { goto exit; } - /* setting first sector is USING */ + /* setting first sector is EMPTY to USING */ + write_sector_status(write_addr, SECTOR_STATUS_EMPUT); write_sector_status(write_addr, SECTOR_STATUS_USING); if (result != EF_NO_ERR) { goto exit;