Fixup EFL and F4's sector selection. (#19221)
This commit is contained in:
		
							parent
							
								
									f98a7e8ffe
								
							
						
					
					
						commit
						cf3c26533c
					
				
					 26 changed files with 281 additions and 76 deletions
				
			
		| 
						 | 
				
			
			@ -19,12 +19,22 @@
 | 
			
		|||
 | 
			
		||||
#ifndef STM32_LSECLK
 | 
			
		||||
#    define STM32_LSECLK 32768U
 | 
			
		||||
#endif  // STM32_LSECLK
 | 
			
		||||
#endif // STM32_LSECLK
 | 
			
		||||
 | 
			
		||||
#ifndef STM32_HSECLK
 | 
			
		||||
#    define STM32_HSECLK 25000000U
 | 
			
		||||
#endif  // STM32_HSECLK
 | 
			
		||||
#endif // STM32_HSECLK
 | 
			
		||||
 | 
			
		||||
#ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP
 | 
			
		||||
#    define EARLY_INIT_PERFORM_BOOTLOADER_JUMP TRUE
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef WEAR_LEVELING_EMBEDDED_FLASH
 | 
			
		||||
#    ifndef WEAR_LEVELING_EFL_FIRST_SECTOR
 | 
			
		||||
#        ifdef BOOTLOADER_TINYUF2
 | 
			
		||||
#            define WEAR_LEVELING_EFL_FIRST_SECTOR 3
 | 
			
		||||
#        else
 | 
			
		||||
#            define WEAR_LEVELING_EFL_FIRST_SECTOR 1
 | 
			
		||||
#        endif
 | 
			
		||||
#    endif
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,12 +19,22 @@
 | 
			
		|||
 | 
			
		||||
#ifndef STM32_LSECLK
 | 
			
		||||
#    define STM32_LSECLK 32768U
 | 
			
		||||
#endif  // STM32_LSECLK
 | 
			
		||||
#endif // STM32_LSECLK
 | 
			
		||||
 | 
			
		||||
#ifndef STM32_HSECLK
 | 
			
		||||
#    define STM32_HSECLK 25000000U
 | 
			
		||||
#endif  // STM32_HSECLK
 | 
			
		||||
#endif // STM32_HSECLK
 | 
			
		||||
 | 
			
		||||
#ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP
 | 
			
		||||
#    define EARLY_INIT_PERFORM_BOOTLOADER_JUMP TRUE
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef WEAR_LEVELING_EMBEDDED_FLASH
 | 
			
		||||
#    ifndef WEAR_LEVELING_EFL_FIRST_SECTOR
 | 
			
		||||
#        ifdef BOOTLOADER_TINYUF2
 | 
			
		||||
#            define WEAR_LEVELING_EFL_FIRST_SECTOR 3
 | 
			
		||||
#        else
 | 
			
		||||
#            define WEAR_LEVELING_EFL_FIRST_SECTOR 1
 | 
			
		||||
#        endif
 | 
			
		||||
#    endif
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,3 +20,13 @@
 | 
			
		|||
#ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP
 | 
			
		||||
#    define EARLY_INIT_PERFORM_BOOTLOADER_JUMP TRUE
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef WEAR_LEVELING_EMBEDDED_FLASH
 | 
			
		||||
#    ifndef WEAR_LEVELING_EFL_FIRST_SECTOR
 | 
			
		||||
#        ifdef BOOTLOADER_TINYUF2
 | 
			
		||||
#            define WEAR_LEVELING_EFL_FIRST_SECTOR 3
 | 
			
		||||
#        else
 | 
			
		||||
#            define WEAR_LEVELING_EFL_FIRST_SECTOR 1
 | 
			
		||||
#        endif
 | 
			
		||||
#    endif
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,3 +20,13 @@
 | 
			
		|||
#ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP
 | 
			
		||||
#    define EARLY_INIT_PERFORM_BOOTLOADER_JUMP TRUE
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef WEAR_LEVELING_EMBEDDED_FLASH
 | 
			
		||||
#    ifndef WEAR_LEVELING_EFL_FIRST_SECTOR
 | 
			
		||||
#        ifdef BOOTLOADER_TINYUF2
 | 
			
		||||
#            define WEAR_LEVELING_EFL_FIRST_SECTOR 3
 | 
			
		||||
#        else
 | 
			
		||||
#            define WEAR_LEVELING_EFL_FIRST_SECTOR 1
 | 
			
		||||
#        endif
 | 
			
		||||
#    endif
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,27 +17,25 @@ static flash_sector_t first_sector = UINT16_MAX;
 | 
			
		|||
static flash_sector_t sector_count = UINT16_MAX;
 | 
			
		||||
static BaseFlash *    flash;
 | 
			
		||||
 | 
			
		||||
#ifndef WEAR_LEVELING_EFL_FIRST_SECTOR
 | 
			
		||||
// "Automatic" detection of the flash size -- ideally ChibiOS would have this already, but alas, it doesn't.
 | 
			
		||||
static inline uint32_t detect_flash_size(void) {
 | 
			
		||||
#    if defined(WEAR_LEVELING_EFL_FLASH_SIZE)
 | 
			
		||||
#if defined(WEAR_LEVELING_EFL_FLASH_SIZE)
 | 
			
		||||
    return WEAR_LEVELING_EFL_FLASH_SIZE;
 | 
			
		||||
#    elif defined(FLASH_BANK_SIZE)
 | 
			
		||||
#elif defined(FLASH_BANK_SIZE)
 | 
			
		||||
    return FLASH_BANK_SIZE;
 | 
			
		||||
#    elif defined(FLASH_SIZE)
 | 
			
		||||
#elif defined(FLASH_SIZE)
 | 
			
		||||
    return FLASH_SIZE;
 | 
			
		||||
#    elif defined(FLASHSIZE_BASE)
 | 
			
		||||
#        if defined(QMK_MCU_SERIES_STM32F0XX) || defined(QMK_MCU_SERIES_STM32F1XX) || defined(QMK_MCU_SERIES_STM32F3XX) || defined(QMK_MCU_SERIES_STM32F4XX) || defined(QMK_MCU_SERIES_STM32G4XX) || defined(QMK_MCU_SERIES_STM32L0XX) || defined(QMK_MCU_SERIES_STM32L4XX) || defined(QMK_MCU_SERIES_GD32VF103)
 | 
			
		||||
#elif defined(FLASHSIZE_BASE)
 | 
			
		||||
#    if defined(QMK_MCU_SERIES_STM32F0XX) || defined(QMK_MCU_SERIES_STM32F1XX) || defined(QMK_MCU_SERIES_STM32F3XX) || defined(QMK_MCU_SERIES_STM32F4XX) || defined(QMK_MCU_SERIES_STM32G4XX) || defined(QMK_MCU_SERIES_STM32L0XX) || defined(QMK_MCU_SERIES_STM32L4XX) || defined(QMK_MCU_SERIES_GD32VF103)
 | 
			
		||||
    return ((*(uint32_t *)FLASHSIZE_BASE) & 0xFFFFU) << 10U; // this register has the flash size in kB, so we convert it to bytes
 | 
			
		||||
#        elif defined(QMK_MCU_SERIES_STM32L1XX)
 | 
			
		||||
#            error This MCU family has an uncommon flash size register definition and has not been implemented. Perhaps try using the true EEPROM on the MCU instead?
 | 
			
		||||
#        endif
 | 
			
		||||
#    else
 | 
			
		||||
#        error Unknown flash size definition.
 | 
			
		||||
    return 0;
 | 
			
		||||
#    elif defined(QMK_MCU_SERIES_STM32L1XX)
 | 
			
		||||
#        error This MCU family has an uncommon flash size register definition and has not been implemented. Perhaps try using the true EEPROM on the MCU instead?
 | 
			
		||||
#    endif
 | 
			
		||||
#else
 | 
			
		||||
#    error Unknown flash size definition.
 | 
			
		||||
    return 0;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
#endif // WEAR_LEVELING_EFL_FIRST_SECTOR
 | 
			
		||||
 | 
			
		||||
bool backing_store_init(void) {
 | 
			
		||||
    bs_dprintf("Init\n");
 | 
			
		||||
| 
						 | 
				
			
			@ -46,8 +44,9 @@ bool backing_store_init(void) {
 | 
			
		|||
    // Need to re-lock the EFL, as if we've just had the bootloader executing it'll already be unlocked.
 | 
			
		||||
    backing_store_lock();
 | 
			
		||||
 | 
			
		||||
    const flash_descriptor_t *desc    = flashGetDescriptor(flash);
 | 
			
		||||
    uint32_t                  counter = 0;
 | 
			
		||||
    const flash_descriptor_t *desc       = flashGetDescriptor(flash);
 | 
			
		||||
    uint32_t                  counter    = 0;
 | 
			
		||||
    uint32_t                  flash_size = detect_flash_size();
 | 
			
		||||
 | 
			
		||||
#if defined(WEAR_LEVELING_EFL_FIRST_SECTOR)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -68,7 +67,6 @@ bool backing_store_init(void) {
 | 
			
		|||
#else // defined(WEAR_LEVELING_EFL_FIRST_SECTOR)
 | 
			
		||||
 | 
			
		||||
    // Work out how many sectors we want to use, working backwards from the end of the flash
 | 
			
		||||
    uint32_t       flash_size  = detect_flash_size();
 | 
			
		||||
    flash_sector_t last_sector = desc->sectors_count;
 | 
			
		||||
    for (flash_sector_t i = 0; i < desc->sectors_count; ++i) {
 | 
			
		||||
        first_sector = desc->sectors_count - i - 1;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue