Snippet - Cerberus InternalFlashStorage

Cerberus InternalFlashStorage

This class enables C# to write a byte array to the internal Cerberus Flash.
***** BE CAREFUL *****
Notice that there are no flash sectors available for this, so our current implementation uses the last 128K of the deployment area, thus reducing the deployments from 384k to 256k.

Another option would be to use sector 4, a 16Kb sector reserved for Configuration, but if we do that we should re-write whatever tinybooter has stored there. This is not currently implemented but would be easy.

Many thanks to Johny Seven for the initial code


Well done. It should work fine as long as your programs do not become very large and use the entire flash deployment region.

I forgot to mention that when you deploy your project, the contents of the “internalflashstorage” will probably be erased, but of course this should not be a problem in run time.

I can imagine configuration values that need to survive a software update, would there be any solution for that?

There is a dedicated config region

How does this dedicated config section work?

I need a little space for configuration items like MAC address and such.

would it be safe to use the last 1k 15-16k in sector 4?
would tinybooter be using that space or would it just be using the beginning?

@ MikeCormier: We are using the last sector since our project is small.

More questions:
1 - Is it safe to use the config region?
2 - does tinybooter or CLR use that?
3 - if they do, we can always find an empty area and re-write the used area along with the new content we need, right?

@ Gus or anybody else.
My question is it safe to use just the last 1k in sector 3 - the dedicated config region, for persistant storage or is there a possibility that tinybooter or CLR use that?
It would be nice to have a small amount of config data survive program updates.

It is safer to use the end of deploy region. I wouldn’t use config.

Hi vqp,
How you find all the Register Address (ex private const UInt32 Flash_CR = 0x40023C10;),
the Data sheet state that Address offset of the Flash control Register : 0xA0000000 + 0x40 + 0x20 * (x – 1), x = 2…4…?

Regards YuvaRaja…

andre.m my last post is modified can you tell me how to find the address…?

My Controller is STM32f407Zg
the Cerberus use STM32F40X both are same or different…?

ok…its fine
Then how the Flash_CR Register got this 0x40023C10 Address…?

Read the manual. It has register map information.

Thank you guys…

I got a Flash programming manual (PM0081) from ST it is too help full for Program the FLASH

Regards YuvaRaja…

Hi vqp, when i am execute the below code, it start writing the data from the address of 6000.
Why its happening like that?
I want to write in address of 3000,for that what we have to do?
Is it correct or not…?

 InternalStorage.Write(bytes, 3000, 3000, bytes.Length);

Hi the modified code is given below it over comes the previous post problems

        public static void Write(byte[] buffer, uint storageAddress, int bufferIndex, int lenght)
           [b] uint address = StorageAddressBase;[/b]
            if (address > StorageAddressMax)
                throw new IndexOutOfRangeException();

            if (bufferIndex + lenght > buffer.Length)
                throw new IndexOutOfRangeException();

        [b]   if (StorageAddressMax < bufferIndex + lenght + StorageAddressBase)
                throw new IndexOutOfRangeException();
                if ((CR.Read() & FLASH_CR_LOCK) != 0)
                    throw new NotSupportedException(); // problem unlocking 

                uint myCR = CR.Read();
                myCR &= CR_PSIZE_MASK;
                CR.Write(myCR | FLASH_PSIZE_BYTE | FLASH_CR_PG);
                int count = bufferIndex;
             [b]   uint AddressOffset = storageAddress;[/b]
                while (count < (bufferIndex + lenght))
                 [b]   AddressSpace.Write(address + AddressOffset, buffer, count, 1);[/b]
                CR.Write(CR.Read() & (~FLASH_CR_PG));

Yes the size does not exceed to “StorageAddressMax”