So I have STM32F4 board and long story short - I am stuck at putting a user defined number (0 - 60) in FLASH memory, so after restarting, I could use last-used-settings. How do I allocate correct space in flash memory, so it doesn't interfere with my code? How to I store it?
Write data to Flash memory via EEPROM Emulation, as described in Application Note AN3969: EEPROM emulation in STM32F40x/STM32F41x microcontrollers, here.
To save a value to restart after an unexpected shutdown, it would be much better to use a byte of battery backed RAM than try to write to Flash.
An update of one byte in flash has to erase then update the entire 'sector'. According to ST's datasheet, the typical time for a 16kB sector erase is 250ms for a STM32F405/7 (5.3.12 "Memory characteristics", Table 40 "Flash memory programming"). If power is lost while flash is being updated, the entire sector may be corrupted. Also, the whole sectr is erased, so it can't be used for code. It depends on the exact STM32 part, but the smallest sector might be 16kB, which might be quite a lot to lose for the sake of one byte.
A smaller further problem with Flash is that it is only guaranteed for 10k erase/write cycles. This might be plenty for your application, but a bug in the program could burn through those in an hour or two.
Other things to consider, while Flash is being written, the CPU will be stalled from accessing that sector, so it won't be able to update the magic value, so you might need to protect access to the value with some code to check Flash is not being changed. So it might be more complex than you expect to use.
The obvious alternative, without adding an external EEPROM, is use battery backed RAM. This is slightly more complex to program than using an in-memory variable because it is part of the RTC which needs to be initialised. However once it set up, it can be read and written with only a few extra instructions. So it's access time is sub-microsecond, fast enough to use in an interrupt service routine, and it won't wear out.
This does mean you need to be able to get at the VBat pin, and connect it to a small battery, e.g. a hearing aid battery.