Directly getting the next free id of the flash memory

hello everyone.

if i want to write something onto the flash memory via adl_flhWrite() i need the id where i want to save it.
Since the module goes out every know and then (which is intended) i need to know the next free id, where i can save some information, without overriding something.

since the size of the objects isn´t fix, i can´t just do int nextId = (sizeOfFlash - adl_flhGetFreeMem()) / sizeOf(someThing); to get it.

adl_flhGetIDCount returns the number of avaiable ids (which is 5120 in my case), but not the next free one.
with adl_flhExist() i can check if a specific id is used or free.

ofcause i could write into the very first field, what the next free id should be. But this doesn´t feel like a good solution, since flash has only a limited amount of write/erease calls.

so, is there another way then guessing the next free id with adl_flhExist() to get the next free id?

First decide which flash memory type suits your needs best; flh, AD or even filesystem.

In case of flh, if device goes out every now and then without power loss, take a look at ADL_MEM_UNINIT macro and store next available ID into uninitialized part of RAM.

You still need restart after power loss initialization. I would suggest the adl_flhGetUsedSize function together with bisection method. For 5120 objects you would need ~13 iterations to find first free ID. Or you can simply write next free ID into other flh object.

And about limited amount of write/erase calls, it does not matter if you always write into cell ID 0 (or whatever number) or increment cell ID on each write.