Skip to content

Commit 0fdc5b0

Browse files
FRASTMmarwaiehm-st
authored andcommitted
drivers: flash: stm32wba flash erase with 2 banks
Add the control of the BKER bit of the FLASH_NSCR1 to select BANK1 or 2 of the internal flash depending on the page number >127 for BANK2 Signed-off-by: Francois Ramu <[email protected]>
1 parent 4f92dfb commit 0fdc5b0

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

drivers/flash/flash_stm32wbax.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,40 @@ static int erase_page(const struct device *dev, unsigned int offset)
214214
return rc;
215215
}
216216

217+
#if defined(FLASH_OPTR_DUAL_BANK)
218+
bool bank_swap;
219+
/* Check whether bank1/2 are swapped */
220+
bank_swap =
221+
((regs->OPTR & FLASH_OPTR_SWAP_BANK) == FLASH_OPTR_SWAP_BANK);
222+
223+
if ((offset < (FLASH_SIZE / 2)) && !bank_swap) {
224+
/* The pages to be erased is in bank 1 */
225+
regs->NSCR &= ~FLASH_STM32_NSBKER_MSK;
226+
page = offset / FLASH_PAGE_SIZE;
227+
LOG_DBG("Erase page %d on bank 1", page);
228+
} else if ((offset >= BANK2_OFFSET) && bank_swap) {
229+
/* The pages to be erased is in bank 1 */
230+
regs->NSCR &= ~FLASH_STM32_NSBKER_MSK;
231+
page = (offset - BANK2_OFFSET) / FLASH_PAGE_SIZE;
232+
LOG_DBG("Erase page %d on bank 1", page);
233+
} else if ((offset < (FLASH_SIZE / 2)) && bank_swap) {
234+
/* The pages to be erased is in bank 2 */
235+
regs->NSCR |= FLASH_STM32_NSBKER;
236+
page = offset / FLASH_PAGE_SIZE;
237+
LOG_DBG("Erase page %d on bank 2", page);
238+
} else if ((offset >= BANK2_OFFSET) && !bank_swap) {
239+
/* The pages to be erased is in bank 2 */
240+
regs->NSCR |= FLASH_STM32_NSBKER;
241+
page = (offset - BANK2_OFFSET) / FLASH_PAGE_SIZE;
242+
LOG_DBG("Erase page %d on bank 2", page);
243+
} else {
244+
LOG_ERR("Offset %d does not exist", offset);
245+
return -EINVAL;
246+
}
247+
#else
217248
page = offset / FLASH_PAGE_SIZE;
218249
LOG_DBG("Erase page %d\n", page);
219-
250+
#endif
220251
/* Set the NSPER bit and select the page you wish to erase */
221252
regs->NSCR |= FLASH_STM32_NSPER;
222253
regs->NSCR &= ~FLASH_STM32_NSPNB_MSK;

0 commit comments

Comments
 (0)