Main Site Documentation

Configuration.ReadEntry()


#1

When trying to store small amounts in the G30 internal flash, I can successfully write an entry to the configuration area and read the size back. But…when actually trying to read the entry values back, I receive an exception stating that the offset must be equal to the length returned by GetEntrySize(). Here is my code:

  int index2=0;
  Byte[] calRecall=new Byte[GHI.Processor.Configuration.GetEntrySize("Calibration")];
  GHI.Processor.Configuration.ReadEntry("Cablibration",calRecall);
  for (int y = 0; y <= 32; y++)
     {
         TabsFinal[y] = (Int32)GHI.Utilities.Arrays.ExtractInt32(calRecall, index2);
         index2 = index2 + 4;
      }

What am I doing wrong here?


G30 Configuration One Write per Entry Name?
#2

What is the stack trace of the exception?

Reflash the firmware, including the config, on the G30 and try again. Also post the code you use to write to the config region.


#3

How do I reflash the firmware? In FEZ Config it says hold down LDR0 and LDR1 then press RESET while they are held down, then release. It fails because it disconnects the G30 when pressing RESET (on the dev board).

        try
        {
            // Debug.Print(Resources.GetString(Resources.StringResources.String1));
            TabsFinal = new Int32[33];
            calBuffer=new Byte[132];
            int index = 0;
            int index2 = 0;
            for (int x = 0; x <= 32; x++)
            {
                GHI.Utilities.Arrays.InsertInt32(calBuffer, index, Tabs[x]);
                index=index + 4;
            }
            

            bool success = GHI.Processor.Configuration.WriteEntry("Calibration", calBuffer);

            if (success){
            
               Byte[] calRecall=new Byte[GHI.Processor.Configuration.GetEntrySize("Calibration")];
                 GHI.Processor.Configuration.ReadEntry("Cablibration",calRecall);
                for (int y = 0; y <= 32; y++)
                {
                    TabsFinal[y] = (Int32)GHI.Utilities.Arrays.ExtractInt32(calRecall, index2);
                    index2 = index2 + 4;
                }
            }

        }
        catch(Exception ex)
        {
            Debug.Print(ex.ToString());
        }

I can’t post what Tabs[] is as it is protected information, all that matters is it is an array of 33 Int32’s. I verfied that Insert32 and Extract32 converted the correct values to and from the buffer before attempting to write the buffer to the flash.


#4

Here is the stack trace. The last two values [15] and [16] are 0. This code skips over the bool success = WriteEntry since it has already been successfully written, it came back true and is 132 bytes like it should be.

haha I misspelled the project name of storage demo…


#5

You can use the Loader Update under the Advanced menu in FEZ Config. You can hold down LDR0 and LDR1, then reset the board while holding them. It should re-appear as a COM port. You can proceed with the update at that point.


#6

It does not reappear. In the bottom right corner where it says status: G30 Connnected, while holding down LDR0 and LDR1 then pressing reset, it changes to “No Device,” and does not reappear as COM port like you say…if I unplug it and plug it back in then it says G30 is Connected. Otherwise it just sits there saying “No Device,” after pressing reset and releasing all buttons


#7

Look in the Device Manager as well, not just FEZ Config.


#8

Ok I can see GHI Bootloader interface in the device manager. When I click next in FEZ Config it tells me that it failed and to make sure the bootloader interface is installed…


#9

Firmware successfully reinstalled.

…Nope I get the same exact error running the code I have posted…offset must match the length of GetEntrySize() even though I directly initiate the buffer size with the call to GetEntrySize();

@Gus_Issa Do you have any idea what could cause this? I am using the configuration sector exactly as instructed and directly following the documentation for ReadEntry() and WriteEntry().


#10

WriteEntry() comes back as true…GetEntrySize() comes back as byte[132] exactly as would be expected from 33 x 4 bytes per 32bit Int. InsertInt32 converts successfully with an offset of 4 and can be converted back fine.

There is no way to specify the offset when calling ReadEntry(“name”, tempbuffer)…using tempbuffer[GetEntrySize(“name”)] does not work.

If I were to call just ReadEntry(“name”) how can I view the object it creates so that I can double check the size/offset?


#11

I’d start by making sure your configuration read/write code works, then move on to parsing the data.

I tested the below code on a freshly flashed G30 running 4.3.8.1 and everything worked as expected. The data returned from the call to ReadEntry matched what was written in WriteEntry as expected.

using GHI.Processor;
using GHI.Utilities;

public class Program {
    public static void Main() {
        var writeBuffer = new byte[] { 1, 2, 3, 4, 5 };
        var readBuffer = new byte[writeBuffer.Length];
        var maxNameLength = Configuration.MaximumNameLength;
        var totalSize = Configuration.TotalSize;
        var entryName = "TestEntry";

        var writeSuccess = Configuration.WriteEntry(entryName, writeBuffer);
        var readSuccess = Configuration.ReadEntry(entryName, readBuffer);
        var size = Configuration.GetEntrySize(entryName);

        var success = writeSuccess && readSuccess && Arrays.Compare(writeBuffer, readBuffer) && size == writeBuffer.Length;
    }
}

#12

How are you dealing with the offset?

Or is there no offset in your case?


#13

There is no concept of offset in the configuration. All it is is a place to a store a byte array with a given name.


#14

Ok then how do I properly store an array of Int32’s into the buffer before writing?

If I use a Byte[] of the same type as yours it works for both reading and writing but I get issues with the offset created by Int32’s taking up 4 bytes per number.


#15

If I seperately store my array of Int32’s using the InsertInt32() method then extract it back from the buffer I just inserted to everything works fine and checks out.

However I cannot combine the two operations.


#16

The below is one way to convert an int array to a byte array then back again.

using System;

public class Program {
    public static void Main() {
        var ints = new int[] { 1, 2, 3, 4, 5 };
        var bytes = new byte[ints.Length * sizeof(int)];

        for (var i = 0; i < ints.Length; i++)
            Array.Copy(BitConverter.GetBytes(ints[i]), 0, bytes, i * sizeof(int), sizeof(int));

        var result = new int[bytes.Length / sizeof(int)];

        for (var i = 0; i < result.Length; i++)
            result[i] = BitConverter.ToInt32(bytes, i * sizeof(int));
    }
}

After the first for loop bytes has the properly converted data so you can store it using WriteEntry. You can use the second loop to convert the array you get from ReadEntry back to an int array.


#17

Thank you I got it working…no idea what I was doing wrong with InsertInt32()