2018-05-07 10:55 AM
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 128KBuint8_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
2018-05-07 03:47 PM
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
2018-05-08 12:12 PM
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 //mentionedfor(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));}2018-05-08 12:37 PM
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 ROMThe 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
2018-05-10 11:22 AM
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 //mentionedfor(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); }}