FlushExtendedWeakReferences is missing in SDK 4.3

Board: FezHydra
GHI SDK Ver: NetMF 4.3 , NETMF and Gadgeteer Package 2014 R5

I am trying to save a variable in flash memory of the device.

Here is the code that I have written for this purpose:


private static string strSn = "12345";
private static uint SnId = 1;
private static void SaveSn()
{
      flashReference = ExtendedWeakReference.RecoverOrCreate(
                typeof(Program), // marker class
                SnId, // id number in the marker class
                ExtendedWeakReference.c_SurvivePowerdown); // flags    
      flashReference.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;
      flashReference.Target = strSn; // saving data
}

Code runs without any problem, but apparently does not save the data object, as every time I re-power the board, load method returns null.

I searched the forum and found a thread discussing similar issue:
https://www.ghielectronics.com/community/forum/topic?id=3833

It suggested using “[em]FlushExtendedWeakReferences[/em]” method to ensure that data is actually saved.

However, I can’t find this method in the mentioned namespaces. In “NETMF and Gadgeteer Troubleshooting” document (https://www.ghielectronics.com/docs/165/netmf-and-gadgeteer-troubleshooting#3020)

[quote]in 4.1 SDK: GHIElectronics.NETMF.System.Util.FlushExtendedWeakReferences
in 4.2 SDK: GHI.Premium.System.Util.FlushExtendedWeakReferences[/quote]
are mentioned. But, nothing is mentioned for 4.3 SDK.

Is the same approach is applicable for 4.3 SDK? Or, are the required libraries in this version of SDK? if yes, what assembly should I add to my project, and in what namespace I should look for it?

@ Afshin - Try calling PushBackIntoRecoverList method.

@ Architect - Thanks for the hint and sorry for the delay in responding.

I am actually facing another problem now that stops me from trying what you recommend.
Every time I re-power the board, at first it comes up as a “GHI NetMF Inteface” in device manager. But when I start the application a message pops up saying “USB Device Not Recognized”. Then, further plugging in and out of the device doesn’t help, and the same scenario happens everytime, unless I reload the loader and firmware again. Then, the device is fine for one run, and everything repeats after the first board repowering.

I tried on three different boards, and got the same result. Not sure what causes that.

I attached a snapshot from the moment the message popped up .

If it is multiple boards I guess something is wrong with the driver. I would remove it completely and will reinstall it.

@ Architect - Thanks, I believe uninstalling and re-installing the driver solved the problem of unknown device.

Back to the original question. I tried [em]PushBackIntoRecoverList [/em]method.

Here is my code to test it:

        
        private static ExtendedWeakReference flashReference;
        private static string strItem;

        void ProgramStarted()
        {
            Debug.Print("Program Started");
            string strTemp = (string)Load();
            if (strTemp == null)
                Save();
            else
                Debug.Print("Restored sucessfully: " + strTemp);
            Thread.Sleep(5000);
            Debug.Print("DONE!");
        }

        private static void Save()
        {
            flashReference = ExtendedWeakReference.RecoverOrCreate(typeof(string), 0, ExtendedWeakReference.c_SurvivePowerdown);
            if (flashReference.Target == null)
                strItem = DateTime.Now.ToString();
            else
                strItem = (string)flashReference.Target;
            flashReference.Target = strItem;
            Debug.Print("Stored: " + strItem);
        }

        private static Object Load()
        {
            flashReference = ExtendedWeakReference.RecoverOrCreate(typeof(string), 0, ExtendedWeakReference.c_SurvivePowerdown);
            object obj = null;
            flashReference.PushBackIntoRecoverList();
            ExtendedWeakReference restoreReference = ExtendedWeakReference.Recover(typeof(string), 0);
            if (restoreReference != null && restoreReference.Target != null)
            {
                obj = restoreReference.Target;
                restoreReference.PushBackIntoRecoverList();
            }
            else
                Debug.Print("Could not restore the string.");
            return obj;
        }

The code still not work properly. Few things that I noticed:
(1) The code works fine in a boot case (when I restart the application, without powering down the board). However, when the item gets restored, it cannot be restored in the next run. It seems it can be restored once, and if it is required to be restored in every run, it must be stored again, somehow after each restoration.

(2) In the case a of power down after a save, a DateTime object is returned but with an incorrect value. The value shows the current DateTime, not the stored value. And still restoring once is valid here. After one restore, even with an incorrect value, saved DateTime cannot be restored again.

(3) If static variables and methods are not used, data is never get stored properly, or at least, Load() method always returns null, causing the code to go through Save() method in every run.

@ Afshin - To your original question, FlushExtendedWeakReferences was removed from our SDK because Microsoft added support for it. It can be found at Microsoft.SPOT.ExtendedWeakReference.FlushAll() in Microsoft.SPOT.Native.dll.

1 Like

@ John - Thanks John. You Answered my original question.

But, it seems FlushAll() method does not make any differences.

To ensure that I did not miss anything, I made a new Gadgeteer project and copied the exact code from Microsoft’s sample for ExtendedWeakRefrences:

The only difference was, I renamed Main() method to Run1(), and called it from ProgramStarted() method of my Gadgeteer project. The rest was exactly the same.

I ran it for the first time. Output was as expected: No saved data, and application saved 1 as the boot count into the flash memory.

However, the second run always led to driver failure, as mentioned before. The board came up as an “Unknown USB Device”, and the only fix for it was reloading the loader and firmware.

I tried it on four hydra boards, and got the same results for all.

As the last chance, I will make an NetMF Console application, and try the code again.

Something is wrong here, and I really have no clue why this is happening! ???

@ Afshin - I was able to reproduce the issue with the board failing to start. We’ll let you know once we have a fix.

1 Like

@ John - Thanks John. I’m looking forward to it.

@ Afshin -

We were able to reproduce your post #6 and fixed it.

But in your post #4, it looks your code does not describe what you wanted, or i am missing something.

@ Afshin - The issue with the board failing to start should be fixed in the next SDK.

@ Dat - Thanks Dat.

What I basically needed was being able to store data in flash memory, and restore them after a power down.

Post #4 was according to @ Architect’s suggestion for using PushBackIntoRecoverList method. I posted my code and thoughts, because I was not able to make it work.

@ John - Great news.

Do you have any idea roughly when the next version of SDK should be released? Thanks.

@ Afshin - We do not have any firm plans yet, but it likely won’t be until after new year’s.

1 Like

Any news about the fix?

Anyway, a short sample code would be great how exactly this API has to be used!

Lots of examples provided by MS on your computer. On my PC, the samples are located at:

C:\Users\mike\Documents\Microsoft .NET Micro Framework 4.3\Samples

@ Alex111 - The SDK I mentioned has not been released yet.

I could not find any samples related to EWR?

@ Alex111 - Do you see any samples?

I had the folder with its subfolders, but unfortunately it seems that the files are missing. Looks like they were left from an earlier version which already has been uninstalled…

Do you have any samples? Do you know in wich packet they are included?