I followed tutorial to store data in flash memory of stm32. I have stm32f407 discovery board, I first flashed code to store a string in sector11 of the flash memory (0x080E0000). Then I checked in stlink utility and found the data was written. Then I tried to flash code to read data from that address and blink led if it matches, but I am not able to flash this program on the board, it gives error that debugger could not halt the device. Any suggestion if I am missing on something.
Code to write data to memory:
uint32_t Flash_Write_Data (uint32_t StartSectorAddress, uint32_t *Data, uint16_t numberofwords){
static FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SECTORError;
int sofar=0;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Erase the user Flash area */
/* Get the number of sector to erase from 1st sector */
uint32_t StartSector = GetSector(StartSectorAddress);
uint32_t EndSectorAddress = StartSectorAddress + numberofwords*4;
uint32_t EndSector = GetSector(EndSectorAddress);
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector = StartSector;
EraseInitStruct.NbSectors = (EndSector - StartSector) + 1;
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
you have to make sure that these data are rewritten before they are accessed during code
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
DCRST and ICRST bits in the FLASH_CR register. */
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
{
return HAL_FLASH_GetError ();
}
/* Program the user Flash area word by word
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
while (sofar<numberofwords)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartSectorAddress, Data[sofar]) == HAL_OK)
{
StartSectorAddress += 4; // use StartPageAddress += 2 for half word and 8 for double word
sofar++;
}
else
{
/* Error occurred while writing data in Flash memory*/
return HAL_FLASH_GetError ();
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
return 0;}
1 Answer 1
If you are using STLINK : You need to ensure that the SWD pins are set as SWD in application mode - its an option in the STM32Cube tool. In other environments you may need to write code or call a fucntion . Otherwise depending on other defaults they may become outputs . If you are using JTAG : you need to ensure all the JTAG pins are set as JTAG mode..
A trick to recover from this is to release the reset on the MCU while attempting to connect with STLINK. It may take a few attempts. As the code boots up it sets the pins from reset state to the application mode, and if the signals on the STLINK are doing the right thing at that time, the MCU goes into SWD mode instead.
Sometimes using the STM32Cube Programmer tool with the STLINK can have a higher chance of managing to grab hold of your MCU as it comes out of reset and get it into SWD mode and allow you to reset the program memory.
Once you have done this a few times it becomes second nature to check that the SWD pins are reserved in application mode. (and have external pullups to prevent accidentally entering SWD mode with noise on the pins)