cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the unused flash location to use write

SS.Sagar
Associate III
Posted on May 07, 2018 at 19:55

Hi ,

I was trying to write array of 10 element in to the flash. But it was writing to used code memory.

I am using STM32F103C8T6 and keil compiler and code generated by cubemx.

Here is my code.

uint32_t startAddress = 0x8000002;//starting from 256KB

//0x8020000 starting 128KB

uint8_t aa[12]={'Hello world\0'};

void writeFlash(void) { uint32_t j; HAL_FLASH_Unlock();//unlock flash writing __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP|FLASH_FLAG_PGERR); FLASH_PageErase(startAddress);//erase the entire page before you can write as I //mentioned // for(i=0; i<mSize; i++) for(j=0; j<12; j++) HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,(startAddress + j*2),aa[j]);

HAL_FLASH_Lock();//lock the flash for writing

}

void readFlash(void)

{ uint32_t j; // for(i=0; i<mSize; i++) for(j=0; j<10; j++) aa[j] = *(uint16_t *)(startAddress + (j*2)); }

When i am calling write flash function then after code doesn't work and on next power ON initial routines also not at all working.
How to find flash location which is not being used in the code memory?

#stm32f103-flash #flash-writing
4 REPLIES 4
Pavel A.
Evangelist III
Posted on May 08, 2018 at 00:47

1. Get the data sheet document for your MCU and read the flash memory section. Find start addresses and sizes of flash pages (a.k.a. 'erase blocks'). Some STM32 models have pages of different sizes.

2. Enable linker map output in the project settings. Build your program. Open the map file and find the last used address.

3. Pick any flash page not occupied by the program.

-- pa

Posted on May 08, 2018 at 19:12

Thanks.

Now code is working on Power ON and i think it is writing to correct memory location.

I could not read the value properly.  i want to write Char Array to ROM. 

Is this write and read functions are correct?

uint32_t startAddress = 0x08006000;

uint8_t aa[12]={'Hello world\0'};

void writeFlash(void)

{

uint32_t j;

HAL_FLASH_Unlock();//unlock flash writing

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPERR);

FLASH_PageErase(startAddress);//erase the entire page before you can write as I //mentioned

for(j=0; j<12; j++)

{

HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,(startAddress + j*2),aa[j]);

// FLASH_Program_HalfWord((startAddress + j*2),aa[j]);

}

HAL_FLASH_Lock();//lock the flash for writing

}

void readFlash(void)

{

uint32_t j;

for(j=0; j<12; j++)

aa[j] = *(uint8_t *)(startAddress + (j*2));

}
Posted on May 08, 2018 at 19:37

No, your write function is not correct. aa[] is array of bytes, so this: 

HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,(startAddress + j*2),aa[j]);

will write each byte expanded to two bytes.

i want to write Char Array to ROM

The HAL library for some MCUs does not provide writing by one byte. Only by 2, 4 or 8 bytes.

For example, HAL library for STMF4 allows writing by byte (FLASH_TYPEPROGRAM_BYTE  is defined).

-- pa

Posted on May 10, 2018 at 18:22

i tried following one but didn't worked.

uint32_t startAddress = 0x08006000;

uint8_t aa[12]={0};

uint16_t bb[12] = {0x3030,0x3131,0x3232,0x3333,0x3434,0x3535,0x3636,0x3737,0x3838,0x3939};

uint16_t cc[12];

void writeFlash(void)

{

uint32_t j;

HAL_FLASH_Unlock();//unlock flash writing

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPERR);

FLASH_PageErase(startAddress);//erase the entire page before you can write as I //mentioned

for(j=0; j<12; j++)

{

HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,(startAddress + j*2),bb[j]);

// FLASH_Program_HalfWord((startAddress + j*2),aa[j]);

}

HAL_FLASH_Lock();//lock the flash for writing

}

void readFlash(void)

{

uint32_t j;

for(j=0; j<12; j++)

cc[j] = *(uint16_t *)(startAddress + (j*2));

for(j=0;j<12;j++)

{

aa[j] = (uint8_t)(cc[j] & 0x00FF);

}

}