diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_f1.c b/bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_f1.c index 8f7d45ade7..01f706cd8f 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_f1.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_f1.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2018-12-5 SummerGift first version + * 2020-03-05 redoc support stm32f103vg + * */ #include "board.h" @@ -122,16 +124,17 @@ int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size) } /** - * Erase data on flash. + * Erase data on flash with bank. * @note This operation is irreversible. * @note This operation's units is different which on many chips. * + * @param bank flash bank * @param addr flash address * @param size erase bytes size * * @return result */ -int stm32_flash_erase(rt_uint32_t addr, size_t size) +int stm32_flash_erase_bank(uint32_t bank, rt_uint32_t addr, size_t size) { rt_err_t result = RT_EOK; uint32_t PAGEError = 0; @@ -151,7 +154,8 @@ int stm32_flash_erase(rt_uint32_t addr, size_t size) EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; 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,6 +174,78 @@ __exit: return size; } +/** + * Erase data on flash . + * @note This operation is irreversible. + * @note This operation's units is different which on many chips. + * + * @param addr flash address + * @param size erase bytes size + * + * @return result + */ +int stm32_flash_erase(rt_uint32_t addr, size_t size) +{ +#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; + size_bank1 = size; + size_bank2 = 0; + } + else if(addr > FLASH_BANK1_END) + { + size_bank1 = 0; + addr_bank2 = addr; + size_bank2 = size; + } + else + { + 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(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; + goto __exit; + } + } + + 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)) + { + result = -RT_ERROR; + goto __exit; + } + } + +__exit: + if(result != RT_EOK) + { + 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);