1

For example, I have a counter value that needs to be saved to flash and after I reboot I want to read that counter again. I am new to the ARM processors. I am using the HAL Drivers.

Something similar to:

s8 adl_flhRead( ascii * Handle, u16 ID, u16 Len, u8 * ReadData );

s8 adl_flhWrite( ascii * Handle, u16 ID, u16 Len, u8 * WriteData );
1
  • Which STM32? The flash API differs between the F0, F4 etc. (thanks, ST) Commented Aug 3, 2015 at 8:05

1 Answer 1

2

You will need some basic understanding of how flash write works - it is NOT as straight forward as writing to RAM, or EEPROM where you can write and RE-write a byte easily. In a flash sector a given byte can only be writen ONCE per erase of the Sector. You wouldn't normally want to do a full sector erase just to rewrite 1 byte - so normally you would write an api over flash that would enable you to erase once the sector and then move the byte write through the sector on each write - tracking the current offset of the byte so you know where to currently read from. A rought sketch of a write/read might look something like:

Read:
1. Erased sector bytes will be at 0xFF. Scan through the sector looking for the first byte that is 0xFF. Read and return the byte BEFORE that.
2. If no byte is found (that is 0xFF) then return the last sector byte.
3. If the first sector byte (an hence all subsequent bytes) are 0xFF then return error - the sector is empty there is no data to read.

Write:
1. Scan through the sector looking for first byte that is 0xFF - the byte before this was the last byte written and this is the next byte to write.
2. If no byte 0xFF can be found then the sector is full - erase the sector (using some thing like stm32f?xx_flash.c FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)) and write the byte in the first byte of the newly erased sector.
3. Else write the data byte to be recorded at the position of the located 0xFF byte (using same FLASH_Status FLASH_ProgramByte).

Note: because you are using the erased flash value of 0xFF as a flag clean byte you cannot store this value i.e you can only record byte values from 0x00 to 0xFE inclusive.

If the counter you want to write is larger than a byte you can adjust the above to write fix sized blocks (look at other functions in stm32f?xx_flash.c)

Sign up to request clarification or add additional context in comments.

1 Comment

i use the stm32f103c8t6, how to get sector size or first sector byte address in my case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.