Skip to main content
user 143
Associate III
January 18, 2022
Solved

STM32L431 mass erase over SWD not working

  • January 18, 2022
  • 2 replies
  • 1469 views

Hello,

I've created a custom PCB with a STM32F207 controller on it. With this board I want to address different targets using SWD. My targets are a STM32F407 and a STM32L431 controller. I implemented a custom SWD interface with Timer and DMA on my F207 controller and now I'm able to talk with my targets over their built-in SWD interface. For example I can read the UniqueID of the targets or write into the flash of them.

But I've got a problem with the mass erase of the flash. With the F407 the flash mass erase work fine. It took about 8 sec and then the hole flash is empty. But on the L431 it wont work.

Writing into the empty flash of the L431 works fine.

I take the following steps to perform a mass erase of the flash:

  • Unlock the write access to the flash register (works, because the LOCK bit is not set anymore and I can write to the flash).
  • Then I check if the BSY bit in the flash status register is set. When it's set, I wait until it's cleared.
  • Then I'm clearing the ICEN and DCEN bits in the FLASH_ACR register. This is not mentioned in the reference manual, but ST is doing this in the HAL functions. But it makes no difference whether I delete the bits or not, deleting the flash does not work in either case.
  • Now im clearing every error bit in the flash status register
  • Setting the MER1 bit in the flash control register (and then read out the flash control register to check whether the bit has been set)
  • And finally set the STRT bit in the FLASH_CR register

On the F407 this routine works fine. After setting the STRT bit it took about 8 sec. until the BSY flag is cleared. With the L431 on the other hand, mass erase should take only 22 ms (according to a presentation by ST). But it doesn't work at all.

Also the PGSERR bit is set in the FLASH_SR register on the L431. I don't understand the bit in this context, because I don't want to write, I want to delete.

Any ideas?

Thank you

This topic has been closed for replies.
Best answer by user 143

Kind of, yes.

I've found my mistake. The PG bit in the FLASH_CR register was set. For the F407 this was not a problem, but the L431 had a problem with it. Wenn the PG bit is cleared, mass erase works as expected. Thank you!

2 replies

Uwe Bonnes
Chief
January 18, 2022

Does your code look similar to:

       stm32l4_flash_unlock(t);

       /* Erase time is 25 ms. No need for a spinner.*/

       /* Flash erase action start instruction */

       stm32l4_flash_write32(t, FLASH_CR, action);

       stm32l4_flash_write32(t, FLASH_CR, action | FLASH_CR_STRT);

       /* Read FLASH_SR to poll for BSY bit */

       while (stm32l4_flash_read32(t, FLASH_SR) & FLASH_SR_BSY) {

               if(target_check_error(t)) {

                       return false;

               }

       }

       /* Check for error */

       uint16_t sr = stm32l4_flash_read32(t, FLASH_SR);

       if (sr & FLASH_SR_ERROR_MASK)

               return false;

       return true;

I have not heard of problems with that code.

user 143
user 143AuthorBest answer
Associate III
January 18, 2022

Kind of, yes.

I've found my mistake. The PG bit in the FLASH_CR register was set. For the F407 this was not a problem, but the L431 had a problem with it. Wenn the PG bit is cleared, mass erase works as expected. Thank you!