How to manual read/write internal flash?
#1
Posted 09 May 2011 - 04:13 PM
#2
Posted 09 May 2011 - 05:54 PM
#3
Posted 09 May 2011 - 06:12 PM
#4
Posted 10 May 2011 - 10:47 AM
#5
Posted 11 May 2011 - 10:19 AM
#6
Posted 11 May 2011 - 11:02 AM
#7
Posted 12 May 2011 - 02:15 PM
You can read/write flash using the MFDeployEngine (which is used internally by MFDeploy).
When the v4.2 SDK is released, we'll include the ability to read/write a bit of flash data from your Netduino app.
Chris
Has anyone here used the MFDeployEngine.dll for detecting the Netduino USB, connect to it and read/write custom data to the flash/memory?
I've created a little test Windows form app which easily detects the Netduino, connects to it and pings it via the MFDeploy class and tbe MFDevice class from the MFDeployEngine.dll.
But when going a step deeper by using the _DBG.Engine and trying to connect to it I'm getting exceptions. As I can see it I need an instance of the Engine to be able to read/write directly to the flash/memory with the methods ReadMemory(...) and WriteMemory(...).
Does anyone know if I'm on the right track here? Hope you can help me out.
Thanks.
#8
Posted 12 May 2011 - 03:00 PM
I think so. You might want to check out MFDeploy source code, it is included in the Porting Kit or you can browse it online.Does anyone know if I'm on the right track here? Hope you can help me out.
#9
Posted 12 May 2011 - 03:20 PM
I think so. You might want to check out MFDeploy source code, it is included in the Porting Kit or you can browse it online.
I'm looking at the MFDeploy source code and have located a Deploy method in the MFDevice class in which a direct memory write is called on the _DBG.Engine: the WriteMemory(...) method.
I've succeded creating an instance of the _DBG.Engine class - but everytime I try to call a method on it I get the following Exception:
if(!m_state.IsRunning) throw new ArgumentException( "Controller not started, cannot create message" );
thrown in the debugger Controller class ln 503.
I'm having a hard time figuring out how to start the controller or if it's something else. Does anyone know what the problem is?
Thanks.
#10
Posted 12 May 2011 - 08:35 PM
Strange thing: It seems the data written to the storage block persists until the CLR engine is executed, then it is erased - I have not investigated it further, it may be caused by initialization of the block storage device driver. I did not really want to write into deployment or configuration area.
namespace MyMFDeploy { using System; using System.Linq; using System.Reflection; using Microsoft.NetMicroFramework.Tools.MFDeployTool.Engine; using Microsoft.SPOT.Debugger; static class Program { static void Main() { using(var deploy = new MFDeploy()) { // Connect to the first USB port var usbPort = deploy.EnumPorts(TransportType.USB).FirstOrDefault(); if(usbPort != null) { using(var device = deploy.Connect(usbPort)) { if(device.ConnectToTinyBooter()) { // Get the DbgEngine (via reflection, because the property is iternal) var prop = typeof(MFDevice).GetProperty("DbgEngine", BindingFlags.NonPublic | BindingFlags.Instance); System.Diagnostics.Debug.Assert(prop != null); var dbgEngine = (Engine)prop.GetValue(device, null); // Direct cast used for type validation System.Diagnostics.Debug.Assert(dbgEngine != null); System.Diagnostics.Debug.Assert(dbgEngine.IsConnected); const int StorageBlockA = 0x17A000; // Size 0x2000 //const int StorageBlockB = 0x17C000; // Size 0x2000 // Data to be written in the middle of storage block 'A' var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var dataAddress = (uint)(StorageBlockA + 0x1000); // Inspect the memory content byte[] buffer; if(dbgEngine.ReadMemory(dataAddress, (uint)data.Length, out buffer)) { System.Diagnostics.Debug.WriteLine(BitConverter.ToString(buffer)); } // Erase is required before Write (?) if(dbgEngine.EraseMemory(dataAddress, (uint)data.Length)) { if(dbgEngine.WriteMemory(dataAddress, data)) { // Check signature - commit (?) if(dbgEngine.CheckSignature(new byte[128], 0)) // Empty signature { // Write succeeded, verify if(dbgEngine.ReadMemory(dataAddress, (uint)data.Length, out buffer)) { System.Diagnostics.Debug.WriteLine(BitConverter.ToString(buffer)); } } } } } } } } } } }
#11
Posted 13 May 2011 - 06:49 AM
The following code demonstrates simple way to read and write flash memory via Debugger.Engine. Please note it is just for experimental purposes, error checking has been omitted for sake of clarity and the device should be probably rebooted at the end. Use at your own risk
Hi CW2, Thanks alot - I'll try out your code and see if I can get it to work the way I want. Simply put I just want to write some data (a struct) from an desktop app into the available user flash memory area which the netduino code then can access. That should be possible - it doesn't sound very complex at all.
Thanks.
#12
Posted 13 May 2011 - 10:16 AM
#13
Posted 13 May 2011 - 12:35 PM
You can embed data inside your app if you want as well. .NET MF supports resources.
That's also very nice but unfortunately I cannot use this feature. I'm going to have a running netduino device which needs to access data in the flash memory (always same start address). The data needs to be written/uploaded to the flash memory from en external windows app by connecting the netduino device via USB.
Do you know which address area that is not used by the MF and the netduino firmware? There must be some empty user flash area to use. Can you maybe help with clarifying this?
#14
Posted 13 May 2011 - 12:41 PM
Strange thing: It seems the data written to the storage block persists until the CLR engine is executed, then it is erased - I have not investigated it further, it may be caused by initialization of the block storage device driver. I did not really want to write into deployment or configuration area.
I have now played around with your code example and am able to persist the data written to the memory but the netduino firmware seems to get unloaded/erased! I can unplug and plug in the netduino again and again and the written data is still there. But when pinged the netduino only response with 'TinyBooter' and not which I had hoped 'TinyCLR'
Does anyone know why the netduino firmware gets removed simply just by writting some data to the "empty" flash memory?
#15
Posted 13 May 2011 - 01:35 PM
If I remember it correctly, there is a note in the Platform Kit documentation that says something about TinyBooter erasing blocks if the verification fails and that there are security measures to protect against overwriting. The code is obviously missing something important here, perhaps a valid signature or security key.Does anyone know why the netduino firmware gets removed simply just by writting some data to the "empty" flash memory?
#16
Posted 13 May 2011 - 02:42 PM
Just to see that I understand : lets say you have a normal netduino with XBee but no SD card, you could send info to the netduino that would persist even after power recycling (like encryption key or remote ip address of neighbor sensor or ...) ? That could be interesting.NET MF 4.2 (beta coming by summer) includes a new "simple storage" mechanism for storing data on-chip. We'll be embracing this in the Netduino 4.2 firmware to enable you to read/write data from your Netduino app as well.
#17
Posted 13 May 2011 - 05:48 PM
Yes, you'll have 4KB or more of storage space to store data. It only has 10,000 rewrite cycles for flash rewrites...so you won't want to rewrite it frequently...but it'll be awesome for configuration data.Just to see that I understand : lets say you have a normal netduino with XBee but no SD card, you could send info to the netduino that would persist even after power recycling (like encryption key or remote ip address of neighbor sensor or ...) ? That could be interesting
And of course, Netduino Plus has MicroSD support...so you can store data there easily too.
Chris
#18
Posted 13 May 2011 - 08:27 PM
Yes, you'll have 4KB or more of storage space to store data. It only has 10,000 rewrite cycles for flash rewrites...so you won't want to rewrite it frequently...but it'll be awesome for configuration data.
Quite ! specially for very small remote sensors using the mini ! Configuration yes, daily values ... not really since 10,000 cycles (about 27 years if one per day)
And of course, Netduino Plus has MicroSD support...so you can store data there easily too.
Was thinking of the mini or normal ones
#19
Posted 16 May 2011 - 11:41 AM
#20
Posted 16 May 2011 - 12:03 PM
And what the different segments (Code, Deployment and Configuration) are used for?
- Bootstrap - bootloader (TinyBooterDecompressor.bin),
- Code - CLR and native code (ER_FLASH),
- Deployment - application and managed dlls (via MFDeploy or Visual Studio),
- Storage A, B - for Extended Weak Reference,
- Config - configuration data (ER_CONFIG, e.g. network settings)
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users