Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ROMSLOT_STM32F4.cpp
00001 // ROMSLOT_STM32F4.cpp 2016/4/9 00002 #if defined(TARGET_STM32F4) 00003 #include "ROMSLOT.h" 00004 00005 static const uint32_t FLASH_SECTOR_SIZE = 0x20000; // 128 Kbytes 00006 00007 ROMSLOT::ROMSLOT() { 00008 base = 0x20000; 00009 } 00010 00011 uint32_t ROMSLOT::New(uint32_t size) { 00012 uint32_t addr = base; 00013 base += (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE * FLASH_SECTOR_SIZE; 00014 return addr; 00015 } 00016 00017 static bool is_base(uint32_t addr) { 00018 return addr % FLASH_SECTOR_SIZE == 0; 00019 } 00020 00021 static uint32_t sector(uint32_t addr) { 00022 MBED_ASSERT(addr >= 0x20000 && addr <= 0x60000); 00023 uint32_t s = (addr - 0x20000) / FLASH_SECTOR_SIZE + 5; // sector 5,6 und 7 00024 return s; 00025 } 00026 00027 bool ROMSLOT::Write(uint32_t addr, const uint8_t buf[], uint32_t size) { 00028 uint32_t data; 00029 MBED_ASSERT(addr % sizeof(data) == 0); 00030 MBED_ASSERT(size % sizeof(data) == 0); 00031 if (memcmp((uint8_t*)addr, buf, size) == 0) { // skip ? 00032 return true; 00033 } 00034 HAL_FLASH_Unlock(); 00035 bool result = true; 00036 for(uint32_t n = 0; n < size; n += sizeof(data)) { 00037 if (is_base(addr + n)) { 00038 FLASH_EraseInitTypeDef Erase; 00039 Erase.TypeErase = FLASH_TYPEERASE_SECTORS; 00040 Erase.VoltageRange = FLASH_VOLTAGE_RANGE_1; 00041 Erase.NbSectors = 1; 00042 Erase.Sector = sector(addr + n); 00043 uint32_t PageError = 0; 00044 HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&Erase, &PageError); 00045 MBED_ASSERT(status == HAL_OK); 00046 if (status != HAL_OK) { 00047 result = false; 00048 break; 00049 } 00050 } 00051 memcpy(&data, buf + n, sizeof(data)); 00052 HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr + n, data); 00053 MBED_ASSERT(status == HAL_OK); 00054 if (status != HAL_OK) { 00055 result = false; 00056 break; 00057 } 00058 } 00059 HAL_FLASH_Lock(); 00060 return result; 00061 } 00062 #endif // TARGET_STM32F4 00063
Generated on Fri Jul 15 2022 00:32:17 by
1.7.2