2020-04-02 07:57 AM
Hi All,
I am using STM32F407 development board and I am trying to do flash write operation but I am facing problem to erase a sector (I am using Sector no - 11 Block base addresses -0x080E 0000 - 0x080F FFFF size-128 Kbytes ) so that I can perform multiple write operations :
According to PM0059 Programming manual:
Sector Erase To erase a sector, follow the procedure below:
1. Check that no Flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register
2. Set the SER bit and select the sector (out of the 12 sectors in the main memory block) you wish to erase (SNB) in the FLASH_CR register
3. Set the STRT bit in the FLASH_CR register
4. Wait for the BSY bit to be cleared
But, while performing step no 3 from above mention steps it causing an error while debugging
"cannot access target shutting down debugging section".
and its come out of debug session
I am using code generator by STM32cubeMX
void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
{
uint32_t tmp_psize = 0U;
/* Check the parameters */
assert_param(IS_FLASH_SECTOR(Sector));
assert_param(IS_VOLTAGERANGE(VoltageRange));
if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
{
tmp_psize = FLASH_PSIZE_BYTE;
}
else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
{
tmp_psize = FLASH_PSIZE_HALF_WORD;
}
else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
{
tmp_psize = FLASH_PSIZE_WORD;
}
else
{
tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
}
/* If the previous operation is completed, proceed to erase the sector */
CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
FLASH->CR |= tmp_psize;
CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
FLASH->CR |= FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos);
FLASH->CR |= FLASH_CR_STRT; // this line causing above error
}
I am using user defined header file for flash read write operation
resource : https://github.com/MYaqoobEmbedded/STM32-Tutorials/tree/master/Tutorial%2030%20-%20FLASH%20Memory
With all due respect your help is all I need,
Thanking you.
2020-04-02 08:09 AM
OK but that's the problem of debugger not the program on STM32. So either set up your driver so that it won't talk to the target all the time (I don't know how, this is toolchain dependent so resort to manual of your toolchain) or simply run your program without the debugger.
JW
2020-04-02 08:13 AM
There is probably nothing wrong with your code, the issue is with the debugger. However, check that PSIZE is selected properly. Note that FLASH_PSIZE_DOUBLE_WORD needs 8V < Vpp < 9V applied.
Flash erase and write operations block read access to the whole flash memory in the same bank (STM32F407 has only one flash bank, so it is always so), blocking the MCU in the process. Erasing a full 128k sector can take 1 to 2 seconds, the debugger would just timeout. See the documentation of your debugger/IDE, it might offer a way to increase the timeout or reconnect. Setting a breakpoint after the offending instruction, and letting it run instead of single step might help too.
2020-04-02 08:45 AM
Ok, show how you're actually calling this function and the parameters. Super hard to debug through a key-hole. Linking to code that isn't yours, and doesn't show how you're using it.
It is probably disconnecting because you're erasing memory in the zero region and not the sector you want.
Use a serial port, output diagnostic/telemetry, use the SWV if you don't have a serial port.
The STM32F407G-DISCO board, or something else, be specific.