How do you save a variable into NVRAM so that it is available when the device powers up the next time?
This is useful for saving settings for an embedded application.
Thank you
-Peter

  | ||||||||||||||
Non-Voliatile Storage - NVRAM
Started by EagleSparrow, Dec 04 2014 02:51 PM
16 replies to this topic
#1Posted 04 December 2014 - 02:51 PM How do you save a variable into NVRAM so that it is available when the device powers up the next time?
This is useful for saving settings for an embedded application.
Thank you
-Peter #2Posted 04 December 2014 - 06:42 PM
I've been struggling with that too. I found mutterings about a new storage mechanism in .net 4.2, but I never found any concrete information or evidence of it, so I gave up and just used an SD card. You can use stream I/O and just open a file on the card.
Here is a bit of code I started working on: // This file is part of the TA.MonktonFocuser project // // Copyright © 2014 TiGra Networks, all rights reserved. // // File: NonVolatileSettings.cs Created: 2014-10-19@08:08 // Last modified: 2014-10-20@19:05 by Tim using System.Collections; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace TA.MonktonFocuser.Firmware { /// <summary> /// Class NonVolatileSettings. Provides persistent storage for a collection of key/value pairs. /// </summary> internal class NonVolatileSettings : IPersistentSettingsStore { const string SettingsFile = "\\SD\\MonktonFocuser.config"; const string settingParserRegEx = @"^(?<Key>\w+)\s*=\s*(?<Value>.*)$"; static readonly Hashtable settingsCollection = new Hashtable(); readonly string filename; readonly Regex parser = new Regex(settingParserRegEx); public NonVolatileSettings(string filename) { this.filename = filename; } public void Load() { var stream = new StreamReader(filename); while (!stream.EndOfStream) { var line = stream.ReadLine(); if (line.Length >= 3) ParseSettingAndAddToCollection(line); } } void ParseSettingAndAddToCollection(string text) { var matches = parser.Match(text); var key = matches.Groups["Key"].Value; var value = matches.Groups["Value"].Value; settingsCollection[key] = value; } public void Save() { var stream = new FileStream(filename, FileMode.Truncate, FileAccess.Write, FileShare.None); using (var writer = new StreamWriter(filename)) { var builder = new StringBuilder(); foreach (var setting in settingsCollection.Keys) { builder.Length = 0; builder.Append(setting); builder.Append(" = "); builder.Append(settingsCollection[setting]); writer.WriteLine(builder.ToString()); } writer.Close(); } } public IDictionary Settings { get { return settingsCollection; } } } internal interface IPersistentSettingsStore { void Load(); void Save(); } } #3Posted 04 December 2014 - 07:57 PM Well. It should not be too hard then.
#4Posted 09 December 2014 - 01:51 PM
I don't know if you have time to research it. but what you do is that there is an area that you can MEMORY Map with the processor space. I have the netduino plus 2. so I have the M4 Core. you map a pointer to the area after Framework stuff where you have free room and you map a structure to that area. Done. you write and read there. easily.
#5Posted 09 December 2014 - 01:52 PM Does anyone know what the framework uses in terms of NVStorage, if it does at all or the bootstrap and how much of the NVRam area that the ARM M4 has?Its a simple peek and poke to a memory location. you do it in assembly if you have to push the call stack for the calling function. I have not had to do that in some time but it's not too difficult. Old School. LOL
Peter
#6Posted 10 December 2014 - 01:32 AM You sure have my curiosity peaked. I want see easy peeking and poking of memory in a managed application and/or I want to see anyone build the firmware without a $6000 tool chain. Be sure to take notes and share. #7Posted 10 December 2014 - 01:37 AM Sure. If you have to you write a driver to access the memory. Its a framework. You can get to registers. so if you can do that then you can do this. I have done it w/o this embedded microsoft stuff.
I am betting you need something like the DDK for doing this or hack this. I find it hard to believe that it does not exist already as an assembly or namespace. -pete #8Posted 05 January 2015 - 03:36 PM
Well, it is possible to build the firmware with GCC, I have been doing that for years, and so were others. The only challenge is Netduino Plus firmware, due to the size of default C runtime libraries (newlib) - it now can be solved with newlib-nano from GCC Tools For ARM Embedded Processors - yes, it requires rebuilding the toolchain, to enable support for 64-bit integers, but it is trivial change in the configuration script and only a few hours wait... #9Posted 05 January 2015 - 03:43 PM
You just need to call one function to update the flash memory contents (it requires a few steps to initialize certain registers and properly aligned and timed access, a little bit different than direct peek/poke). The NETMF itself uses that, e.g. during deployment, no need to implement anything but a simple wrapper to expose it. There is already such functionality implemented in Valkyrie-MT's settings feature, see his repo. #10Posted 05 January 2015 - 04:54 PM
Yeah, that's the thing. I did it once for a quad flight controller. Step1 reformat your computer and install old Windows XP stuff. Step 2 track down old out of date products, if you can find them.
The fact that you can do it, is not the same as saying it is easy. In fact it is somewhat intentionally misleading.
In your case you are saying something new allows it happen. That is far more preferable. Can I assume it runs with the current version of the OS that has nearly 90% of the market?
Still waiting on detailed notes .......
The spirit of open source is to allow people to make changes and re-share them. Many are capitalizing on the open source momentum by claiming open source, when using in this way is impractical. There is a Visual Studio plugin for the Arduino which talks about how great it is to be open source. What they don't tell you is the useful part of the project is not open (Visual Micro). My other favourites are the ones that sell current versions, only the older buggy broken versions are free (XCeed). I know, its easy to make money on gullible people, but I am going to call them out when I see it. Open source has a place. It is helpful to many people. It is in spirit a good idea. It is the marketing vultures that are ruining it.
And now that I am soapbox mode: What the heck do you think will happen when a company who only sells software starts giving it away? How serious do you think they can be? Indeed Open Source MF COULD drive OEMs, and commercial licenses would be the business model. But for consumers - no way. #11Posted 05 January 2015 - 06:54 PM Well, building NETMF firmware requires some effort - it is certainly not on the same comfort level as F7/Ctrl+B in Visual Studio, but I can ensure you it is still a piece of cake in comparison to other [embedded] projects I've had a chance to work with. And the real fun comes much later, when you need to F10 in native code
Regarding Netduino Plus, technically it was possible to build the firmware with GCC, but due to the size of its C Runtime libraries and flash memory consumed by networking code, either some of the features had to be removed or the flash region dedicated to user application had to be significantly reduced. After release of newlib-nano - an optimized C Runtime, a part of GNU Tools for ARM Embedded Processors - in December 2012, the code produced by GCC is on par with Keil MDK (formerly ARM RVDS) output, in fact I was able to get even smaller output in certain cases (optimized compiler and linker switches for Cortex-M Thumb code generation).
The other issue with GCC was that after introduction of Cortex-M port, there were wrong build settings and interrupt-related code that did not work correctly for Thumb instruction set - this was fixed and published in 2012 (unfortunately, the repository has been deleted, but the code is included in newer releases).
Hope this helps. #12Posted 05 January 2015 - 08:22 PM CW2, thanks for that. I believe you are in the true spirit of open source.
I had looked at, ran into the 'it wont work because of ...' problems, asked on this forum, and received no replies a while back (maybe before March because I do remember searching the wiki). What is published there is certainly enough for a someone brave to attempt - although I'd bet it is more like a weeks effort, than a few hours, unless you do it every other day. Just tracking down all the 'updates' is more than several hours. How much reading must someone do to understand the Cortex-M Thumb code linker switches?
My point remains about the process being more sophisticated than the average consumer could probably accomplish. But, I do recognize 99.999% of open source users could not modify any code, but are all hyped up about Open Source anyhow, that's what marketing departments are depending on.
edit: I just went and looked at the wiki again, and I still can not find anything other than the 4.2.2 yargo stuff, with a bunch of "it wont work because of" comments, and nothing updated since 20113. Perhaps it is displaying incorrectly in my browser. #13Posted 28 January 2015 - 06:43 PM Thanks Guys.
Is the Micro-framework free for product distribution?
#14Posted 02 February 2015 - 05:18 PM
Hello I would like to be able to do this with the .Net 4.3 framework. I don't want t port everything. there us a lot done here with the micro-framework in terms of all the Com Stacks and stuff like that. I find it hard to believe it is not included in the framework as a namespace to save say and IP address and port or other info to NVRAM, also version numbers. Got any Ideas #15Posted 02 February 2015 - 05:52 PM What would be great is a declaration or decoration that makes an object reside in the NVRAM area.
like static or global or something like that. or possibly a decoration such as [DataContract]
[Storage(type=storageType.NVRAM)] public object MyData;
[Storage(type=storageType.NVRAM)] public static ComSettings MyData = new ComSettings () { baudRate = 9600, Port=9340, IP="10.0.0.198" }
Does anyone know how to do this. Maybe Chris Walker can comment on this. #16Posted 03 February 2015 - 11:09 PM
Hi EagleSparrow,
First of all, yes, the Micro-framework is open source and royalty free for distribution with your product Netduino is also open source (software and hardware). Enjoy the freedom of open source. On storage the best option today is MicroSD. We're knee-deep in IP stack code at the moment, but we could look at a potential "storage" block snuck into Netduino's STM32's flash in the future. [It would probably have a class to manage it, rather than metadata.] There used to be a feature that would sort of do this called ExtendedWeakStorage--but it was incompatible in some ways with the garbage collector. So we decided _not_ to implement it. Better to add "create some on-chip storage for the future" to our feature wish list. Chris #17Posted 04 February 2015 - 07:36 PM
All that needs to be taken into consideration is the modes for the processor and the processor model. depending on the memory model and which features are enabled in the processor affecting the pins I think. If not it's all the memory space and where and how much is needed.
I think this is a must have feature because not everyone wants to implement an SD card interface. It is really good that there is one. but going to a production product, it's too much hardware cost. SD can be very expensive in terms of Real estate on a board and components if not needed. So if you need to save data that is not changing all the time, the feature is needed.
Even 2k of NV mapped to a struct would be amazing. Also being able to check sum a region in that space to have two sets of data for fall back values. this may be an application implementation but this kind of stuff is needed. I have had to use that for a Boot-loader for a digital modem product many years ago to save data. 0 user(s) are reading this topic0 members, 0 guests, 0 anonymous users | ||||||||||||||
|
||||||||||||||
This webpage is licensed under a Creative Commons Attribution-ShareAlike License. | ||||||||||||||