Skip to content

Commit 55ce302

Browse files
warasilapmcarlescufi
authored andcommitted
drivers: flash: stm32l5_u5: refactor flash_stm32_page_layout() for clarity
While diagnosing a problem on the STM32U585 in this function in flash_stm32l5_u5.c, I had difficulty sussing out the flow of the function and conditional logic. This refactor seeks to improve clarity through a simplified flow control using an early return to short circuit logic on subsequent calls to the function and slightly more thorough comments. Tested using tests/drivers/flash on the b_u585i_iot02a and in a proprietary application and board which uses the STM32U585 and littlefs. Signed-off-by: Peter Maxwell Warasila <madmaxwell@soundcomesout.com>
1 parent ed3ad75 commit 55ce302

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

drivers/flash/flash_stm32l5_u5.c

+36-32
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
2727
#define STM32_SERIES_MAX_FLASH 2048
2828
#endif
2929

30+
#define PAGES_PER_BANK ((FLASH_SIZE / FLASH_PAGE_SIZE) / 2)
31+
3032
#define BANK2_OFFSET (KB(STM32_SERIES_MAX_FLASH) / 2)
3133

3234
#define ICACHE_DISABLE_TIMEOUT_VALUE 1U /* 1ms */
@@ -356,50 +358,52 @@ void flash_stm32_page_layout(const struct device *dev,
356358
{
357359
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
358360
static struct flash_pages_layout stm32_flash_layout[3];
359-
#define PAGES_PER_BANK ((FLASH_SIZE / FLASH_PAGE_SIZE) / 2)
361+
362+
*layout = stm32_flash_layout;
363+
*layout_size = ARRAY_SIZE(stm32_flash_layout);
364+
365+
if (stm32_flash_layout[0].pages_count != 0) {
366+
/* Short circuit calculation logic if already performed */
367+
return;
368+
}
360369

361370
if (((regs->OPTR & FLASH_STM32_DBANK) == FLASH_STM32_DBANK) &&
362371
(CONFIG_FLASH_SIZE < STM32_SERIES_MAX_FLASH)) {
363372
/*
364-
* For stm32l552xx with 256 KB flash
365-
* or For stm32u57x with 1MB flash
373+
* For stm32l552xx with 256 kB flash or stm32u57x with 1MB flash
374+
* which have space between banks 1 and 2.
366375
*/
367-
if (stm32_flash_layout[0].pages_count == 0) {
368-
/* Bank1 */
369-
stm32_flash_layout[0].pages_count = PAGES_PER_BANK;
370-
stm32_flash_layout[0].pages_size = FLASH_PAGE_SIZE;
371-
/* Dummy page corresponding to discontinuity between
372-
* bank 1/2
373-
*/
374-
stm32_flash_layout[1].pages_count = 1;
375-
stm32_flash_layout[1].pages_size = BANK2_OFFSET
376-
- (PAGES_PER_BANK * FLASH_PAGE_SIZE);
377-
/* Bank2 */
378-
stm32_flash_layout[2].pages_count = PAGES_PER_BANK;
379-
stm32_flash_layout[2].pages_size = FLASH_PAGE_SIZE;
380-
}
376+
377+
/* Bank1 */
378+
stm32_flash_layout[0].pages_count = PAGES_PER_BANK;
379+
stm32_flash_layout[0].pages_size = FLASH_PAGE_SIZE;
380+
381+
/* Dummy page corresponding to space between banks 1 and 2 */
382+
stm32_flash_layout[1].pages_count = 1;
383+
stm32_flash_layout[1].pages_size = BANK2_OFFSET
384+
- (PAGES_PER_BANK * FLASH_PAGE_SIZE);
385+
386+
/* Bank2 */
387+
stm32_flash_layout[2].pages_count = PAGES_PER_BANK;
388+
stm32_flash_layout[2].pages_size = FLASH_PAGE_SIZE;
381389
} else {
382390
/*
383-
* For stm32l562xx & stm32l552xx with 512 KB flash
384-
* or For stm32u58x with 2MB flash
391+
* For stm32l562xx & stm32l552xx with 512 flash or stm32u58x
392+
* with 2MB flash which has no space between banks 1 and 2.
385393
*/
386394

387-
if (stm32_flash_layout[0].pages_count == 0) {
388-
if ((regs->OPTR & FLASH_STM32_DBANK) == FLASH_STM32_DBANK) {
389-
/* flash with dualbank has 2k pages */
390-
stm32_flash_layout[0].pages_count = FLASH_SIZE / FLASH_PAGE_SIZE;
391-
stm32_flash_layout[0].pages_size = FLASH_PAGE_SIZE;
395+
if ((regs->OPTR & FLASH_STM32_DBANK) == FLASH_STM32_DBANK) {
396+
/* L5 flash with dualbank has 2k pages */
397+
/* U5 flash pages are always 8 kB in size */
398+
stm32_flash_layout[0].pages_count = FLASH_SIZE / FLASH_PAGE_SIZE;
399+
stm32_flash_layout[0].pages_size = FLASH_PAGE_SIZE;
392400
#if defined(CONFIG_SOC_SERIES_STM32L5X)
393-
} else {
394-
/* flash without dualbank has 4k pages */
395-
stm32_flash_layout[0].pages_count = FLASH_PAGE_NB_128_BITS;
396-
stm32_flash_layout[0].pages_size = FLASH_PAGE_SIZE_128_BITS;
397-
401+
} else {
402+
/* L5 flash without dualbank has 4k pages */
403+
stm32_flash_layout[0].pages_count = FLASH_PAGE_NB_128_BITS;
404+
stm32_flash_layout[0].pages_size = FLASH_PAGE_SIZE_128_BITS;
398405
#endif /* CONFIG_SOC_SERIES_STM32L5X */
399-
}
400406
}
401407
}
402408

403-
*layout = stm32_flash_layout;
404-
*layout_size = ARRAY_SIZE(stm32_flash_layout);
405409
}

0 commit comments

Comments
 (0)