2020-06-18 11:51 AM
Hi ,
i am using STM32H753 MCU, it has 2 banks of 8 sector each using HAL_FLASH_PROGRAM function , it is not working, can anyone help me with this issue.
int data;
StartSectorAddress = 0x080E0000
eg:(HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, StartSectorAddress, data) == HAL_OK)
Regards,
Manjunath
2020-06-18 01:03 PM
Don't forget to unlock (enable write). Look for examples.
-- pa
2020-06-18 09:57 PM
hi ,
this is the function i am using to write to flash which is not working .
any suggestions on this.
unlock means HAL_FLASH_Unlock(); function right
uint32_t Flash_Write_Data (uint32_t StartSectorAddress, uint32_t * DATA_32)
{
static FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SECTORError;
int sofar=0;
int numberofwords = (strlen(DATA_32)/4) + ((strlen(DATA_32) % 4) != 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_32[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;
}
Regards,
Manjunath
2020-06-19 01:11 PM
2020-06-19 07:39 PM
> unlock means HAL_FLASH_Unlock(); function right
Right. But...
> int numberofwords = (strlen(DATA_32)/4) + ((strlen(DATA_32) % 4) != 0);
Why strlen?? What if there's a zero byte in it?
3rd argument of HAL_FLASH_Program is address of 32-byte block, not a word.
Flash of H7 is different from other models.
-- pa
2020-06-21 12:27 AM
Hi @Pavel A.
Yes i tried that its working
but on the same note when i want to write lets say 1024 bytes, which i am not able to write , and there are no zero bytes in the data.
strlen is used for calculating number of words in that buffer, i am not able to write to any address except for the start , can u suggest what modifications in the function should i make to make it generic for any data of any size for writing in to flash.
Regards,
Manjunath
2020-06-21 02:34 PM
Try this function to write a block of 32 bytes.
How to write 1024 bytes using this, is your homework ;)
bool Flash_write32B(uint8_t const *src, uint8_t *dst)
{
uint32_t FlashAddress = (uint32_t)dst;
if (FlashAddress & (32-1)) {
return false; // bad alignment
}
HAL_StatusTypeDef st;
st = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, FlashAddress, (uint32_t)src);
if (st != HAL_OK) {
//printf("Flash write error to %8.8lX err=%#8.8X\n", FlashAddress, (unsigned)pFlash.ErrorCode);
}
return st == HAL_OK;
}
2020-06-27 02:35 AM
Hi @Pavel A.
Thanks for your reply,
i am not able to erase, also from your method i am not write even 32 byte.
we need to erase and start writing right.
Regards,
manjunath
2020-06-27 02:17 PM
If you are not able to erase then write won't work of course.
The sector can be write protected.
Also, try to disable *both* D and I caches to see if this is something cache-related.
-- pa