Secure storage

When writing to secure storage , I get this occasionally.

#### Exception System.ArgumentException - 0xfd000000 (1) ####
#### Message: 
#### GHIElectronics.TinyCLR.Devices.SecureStorage.SecureStorageController::NativeWrite [IP: 0000] ####
#### GHIElectronics.TinyCLR.Devices.SecureStorage.SecureStorageController::Write [IP: 004b] ####
#### VersaStat.Stat::FlushVFlash [IP: 0048] ####
#### VersaStat.Stat::SetFlashValue [IP: 004f] ####
#### TitanVersa.Monitor::Process [IP: 0237] ####
#### TitanVersa.Program::Main [IP: 02a1] ####

Exception thrown: ‘System.ArgumentException’ in GHIElectronics.TinyCLR.Devices.SecureStorage.dll

When I get this, When I get it, it continues over and over. if I set a breakpoint at FlushVFlash, it doesn’t hit it

Can you build a small demo showing this issue please?

There are a lot of reasons cause this issue, check address and size…

are there any timing issues that I need to consider ?

Are you writing on Configuration or OTP region?

configStorage = new SecureStorageController(SecureStorage.Configuration);

If OTP then you can’t erase.
If configuration make sure the block is empty by erase it first.

if address, size… everything is OK then that block is not empty, has to erase it first.

I keep a ‘mirror’ in ram. When I write to flash, I write it to ‘mirror’, erase (which erases the entire 128k),
Then write the entire mirror to flash in 32byte blocks.

Below is example if you want to use ‘mirror’ 128K. That will Erase/Write/Read and use MD5 to compare entire configuration.

That may help you to find somewhere not correct in your project!

            var config = new SecureStorageController(SecureStorage.Configuration);
            var data = new byte[config.TotalSize];

            var rd = new Random();

            // Fill random            
            rd.NextBytes(data);

            var md5 = MD5.Create();
            var hash = md5.ComputeHash(data);

            // Erase
            config.Erase();

            var block = 0U;
            var idx = 0;

            // Write
            while (block < config.TotalSize / config.BlockSize)
            {
                var dataBlock = new byte[config.BlockSize];

                Array.Copy(data, idx, dataBlock, 0, (int)config.BlockSize);
                config.Write(block, dataBlock);
                idx += (int)config.BlockSize;
                block++;
            }

            // Read to compare
            block = 0U;
            idx = 0;

            while (block < config.TotalSize / config.BlockSize)
            {
                var dataBlock = new byte[config.BlockSize];
                config.Read(block, dataBlock);

                Array.Copy(dataBlock, 0, data, idx, (int)config.BlockSize);

                idx += (int)config.BlockSize;
                block++;
            }

            md5.Clear();
            var newHash = md5.ComputeHash(data);

            for (var i = 0; i < newHash.Length; i++)
            {
                if (newHash[i] != hash[i])
                {
                    Debug.WriteLine("Failed");

                    break;
                }
            }

            Debug.WriteLine("Erase - Write - Read Secure Config OK");

I have the mirror running, working fine. However, when I update flash from mirror, the Erase() call blocks for about 1 second. Is there a non-blocking Erase(0 ?

No, it is still in our plan.