cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F205 - Store byte array in FLASH

Marco Cresseri
Associate II
Posted on August 01, 2013 at 10:33

Hi,

I need to store a byte array in the STM32F205 flash memory. I've followed an approach that was working on a STM32F103 mcu, but now it doesn't work anymore.

The problem is that the first call to the FLASH_ProgramByte works correctly and returns FLASH_COMPLETE, but int the next iteration of the loop it returns FLASH_ERROR_PROGRAM.

My code is the following:

#define FLASH_SECTOR_BASE_ADDRESS 0x080c0000

int WriteArray(const uint8_t * dataBuffer, uint32_t dataSize, uint32_t dataAddressOffset)

{

uint32_t tempAddress;

FLASH_Status result;

/* Compute temporary address */

tempAddress = FLASH_SECTOR_BASE_ADDRESS + dataAddressOffset;

/* Unlock flash memory */

FLASH_Unlock();

/* Clear pending flags (if any) */  

FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR |

FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |

FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); 

/* Copy data into flash memory */

for (uint32_t i = 0; i < dataSize; i++)

{

/* Program byte */

result = FLASH_ProgramByte(tempAddress, dataBuffer[i]);

if (result != FLASH_COMPLETE)

{

/* Lock flash memory */

FLASH_Lock();

return FALSE;

}

tempAddress++;

}

/* Lock flash memory */

FLASH_Lock();

return TRUE;

}

I've also tried with FLASH_ProgramHalfWord() with an address increment of 2 and with FLASH_ProgramWord() with an address increment of 4, but the problem is the same.

Thank you for your help

Marco
12 REPLIES 12
juozas
Associate III
Posted on September 17, 2014 at 11:19

Hi,

My problem was exactly the same.

I have changed the part of program:

for (i = 0; i < intSizeToWrite; i++)

{

    FLASH_ProgramByte(uint32Address + i, uint8BackupBuffer[i]);

        if (uint8BackupBuffer[i] != *(__IO uint8_t *) (uint32Address + i))

            {

                FLASH_Lock();

                return WRITE_RESULT_WRITE_NOT_SUCCESS;

            }

   // if (My_FLASHStatus != FLASH_COMPLETE)

     //   {

       //     FLASH_Lock();

         //   return WRITE_RESULT_WRITE_NOT_SUCCESS;

       // }

}

And this work for me. 

I wrote this replay to this old post, because I don't find any other post that matches to my problem, that I have ''solved'', but do not understand why.

My environment: STM32F405VG, STLinkV2, periph. lib. is V1.3.0, compiler and debugger em::blocks.

Posted on September 17, 2014 at 13:54

I wrote this replay to this old post, because I don't find any other post that matches to my problem, that I have ''solved'', but do not understand why.

Well your code fragment never sets My_FLASHStatus, but one might presume that a write might fail if the flash array has not been erased before you attempt to write, or rewrite, data to it.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
juozas
Associate III
Posted on September 18, 2014 at 13:09

Thank you very much for pointing to my error. I have attached full source code of my subroutine - for those who will be interested to use my achievement.

Best regards.

________________

Attachments :

MyWriteByteBufferToFlashMemory.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0bZ&d=%2Fa%2F0X0000000beO%2FBZqrTgFBXqAdDNExNdr3OBh.3s8PsQVUAyqLzABptf0&asPdf=false