Update drv_flash_f1.c

1、【修改】运算方式,更加简洁易懂;
2、【修正】代码格式 tab、 LOG_D;
3、【增加】返回值;
4、【去除】bank1_flag和bank2_flag,改用bank_size判定。
This commit is contained in:
redoc 2020-03-18 15:05:12 +08:00 committed by GitHub
parent 91a52192b8
commit bac9410d7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 52 deletions

View File

@ -155,7 +155,7 @@ int stm32_flash_erase_bank(uint32_t bank, rt_uint32_t addr, size_t size)
EraseInitStruct.PageAddress = GetPage(addr);
EraseInitStruct.NbPages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
EraseInitStruct.Banks = bank;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
{
result = -RT_ERROR;
@ -170,7 +170,7 @@ __exit:
return result;
}
log_d("erase done: addr (0x%p), size %d", (void *)addr, size);
LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
return size;
}
@ -186,75 +186,58 @@ __exit:
*/
int stm32_flash_erase(rt_uint32_t addr, size_t size)
{
rt_err_t result = RT_EOK;
#if defined(FLASH_BANK2_END)
uint32_t addr_bank1 = 0;
uint32_t size_bank1 = 0;
uint32_t addr_bank2 = 0;
uint32_t size_bank2 = 0;
uint8_t bank1_flag = 0;
uint8_t bank2_flag = 0;
#endif
#if defined(FLASH_BANK2_END)
if(addr <= FLASH_BANK1_END)
#if defined(FLASH_BANK2_END)
rt_err_t result = RT_EOK;
rt_uint32_t addr_bank1 = 0;
rt_uint32_t size_bank1 = 0;
rt_uint32_t addr_bank2 = 0;
rt_uint32_t size_bank2 = 0;
if((addr + size) <= FLASH_BANK1_END)
{
addr_bank1 = addr;
bank1_flag = 1;
if((addr + size) > FLASH_BANK1_END)
{
addr_bank2 = FLASH_BANK1_END + 1;
size_bank2 = addr + size - (FLASH_BANK1_END + 1);
if(size_bank2)
{
bank2_flag = 1;
}
size_bank1 = FLASH_BANK1_END + 1 - addr_bank1;
}
else
{
size_bank1 = size;
}
size_bank1 = size;
size_bank2 = 0;
}
else if(addr > FLASH_BANK1_END)
{
size_bank1 = 0;
addr_bank2 = addr;
size_bank2 = size;
}
else
{
addr_bank2 = addr;
size_bank2 = size;
bank1_flag = 0;
bank2_flag = 1;
addr_bank1 = addr;
size_bank1 = FLASH_BANK1_END + 1 - addr_bank1;
addr_bank2 = FLASH_BANK1_END + 1;
size_bank2 = addr + size - (FLASH_BANK1_END + 1);
}
if(bank1_flag)
if(size_bank1)
{
log_d("bank1: addr (0x%p), size %d", (void *)addr_bank1, size_bank1);
if(size_bank1 != stm32_flash_erase_bank(FLASH_BANK_1, addr_bank1, size_bank1))
{
LOG_D("bank1: addr (0x%p), size %d", (void *)addr_bank1, size_bank1);
if(size_bank1 != stm32_flash_erase_bank(FLASH_BANK_1, addr_bank1, size_bank1))
{
result = -RT_ERROR;
}
}
}
if(bank2_flag)
if(size_bank2)
{
log_d("bank2: addr (0x%p), size %d", (void *)addr_bank2, size_bank2);
if(size_bank2 != stm32_flash_erase_bank(FLASH_BANK_2, addr_bank2, size_bank2))
{
LOG_D("bank2: addr (0x%p), size %d", (void *)addr_bank2, size_bank2);
if(size_bank2 != stm32_flash_erase_bank(FLASH_BANK_2, addr_bank2, size_bank2))
{
result = -RT_ERROR;
}
}
}
return result;
return size_bank1 + size_bank2;
#else
return stm32_flash_erase_bank(FLASH_BANK_1, addr, size);
#endif
}
#if defined(PKG_USING_FAL)
static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);