• Writing and Reading 'persistent' data using FlashEEPROM (journaling) module - Part 1 - Basics

    Writing data to and reading data using FlashEEPROM module and Flash Library/Class for storing data and reading it back does already an excellent job to hide the hardware API... nevertheless, for the application space: mmmh - we will have to 'talk' about.

    Users would expect the following code just to work and show 513 in the console:

    // Store the (integer) number 513 at flash address 1 (2nd page of available pages and read it back).
    var flash = new (require("FlashEEPROM"))();
    flash.write(1, 513);
    var n = flash.read(1);
    console.log(n);
    

    The module after all 'implies' - kind of - this simplicity ...at least to users who usually do not (need to) look under the hood or are somewhat 'HW remote'. After all, for all our gadgets from tiny to jumbo we have 'mechanics' that take care of them, and if the sorrow gadget is so very highly integrated as electronics is or as fast assembled as snaps, we just get the next and also newer and more capable version... (do not want to touch the ecology subject here...)

    The simple, straight forward code executes without complaint and reads back the value 1 ...as first and only element of a Uint8Array:

    new Uint8Array([1])
    

    How come?

    Of course, there is absolutely nothing wrong with the module: it works as designed (AND implemented). To use it as simply as intended, some more abstraction is required in the direction of the intended usage.

    Looking at the code, the 'issue' is that 513 is an integer that requires two bytes to fit / to be represent by. .write() writes bytes - one for every given value - and thus from any integer number (or any other typed value) *.write() writes only the least significant byte... (number % 256 (modulo) or number & 255 and number & 0xFF (bit masking by bitwise AND), which for 513 (= 2 * 256 + 1) is 1, exactly what we got back so far).

    With 'a few small' changes, the user - me - get's it 'wright' (at least working):

    // Store the (integer) number 513 at flash address 1 (2nd page of available pages and read it back).
    var flash = new (require("FlashEEPROM"))();
    flash.write(1, [513 >> 8, 513 & 255]);
    var r = flash.read(1);
    var n = (r[0] << 8) + r[1];
    console.log(n);
    console.log(r);
    

    Line 7 has been added to show what we read back and from what we have to reconstruct the expected value 513, which is 2 * 256 + 1:

    513
    new Uint8Array([2, 1])
    

    Some how this 'business' of bit (byte) banging should be hidden. Could we not add it to the module/class, since it is already doing a great job in 'understanding' my intentions: For example I did not have to specify how much to read, the (journaling) module/class some how knew how much I wrote the last time and fills that in for me (lazy coder) for number of bytes to read - the second argument - in .read()... (nothing against efficient laziness, but to much of it in code become 'crypticness',... or ambiguities at least).

    The approach of adding a particular abstraction 'to fix an issue' into an existing module could limit the general (re-) use of the module and put it into a niche; or worse: it could lead over time to creation of lots of similar modules that - duplicate code for the core of write and read, which - at worst - result for a larger application using multiple abstractions in duplicate code and unnecessary foot print. The really worst is though yet to come: the implementation of the logical duplicate code pieces could be out of sync since coming from different modules - and lead to compromising results.

    Different applications have needs for different abstractions or sets of abstractions, and therefore, the onion principle of layering software can save you 'many tears', even though the real onions tend to do the opposite...

    Layering means to put a layer around the existing, re-usable code and put the additional, particular functionality into that new layer. Lets take a look what that new layer has to do:

    Flash library reference - espruino.com/Reference#Flash - through which the FlashEEPROM module's function is exposed to programming - says that the data argument - the second argument - of *.write() accepts some kind of string or array of 1 to n byte(s). In practice, this can be a String - which is a string of bytes and kind of an array of bytes - or an Uint8Array - which is also a 'string' of bytes, just a bit differently stored internally. It also accepts any single value or common array of any values. From experience we though know that any single value or any value in the array that 'is more that just one byte', .write() takes only the least significant byte and writes just that byte. For the number 513 - which is (hexadecimal) 0h0201, binary 0b0000001000000001 (6 leading 0 bits, followed by a 1 and then 0 bit for the first byte, and 7 leading 0 bits followed by a 1 bit for the second byte) - it means that just the value 1 is written. Therefore, we have to make two (2) bytes out of it, first the most significant one, and then the least significant one.

    To get thew most significant byte, we use the bit_shift_right operation (>>) by 8 bits 513 >> 8 - which pushes out the 8 bits of to 2nd byte and pulls in 0 bits from the left and gives a the - 1 byte fitting - integer number 2 - binary 0b00000010.

    To get the second byte, we could just use the 513 number as is, because .write() ignores anyway any more significant bytes than the least one. But the 'sake of clarity', we use the bit-wise AND operation 513 & 255 or 513 & 0hff or 513 & 0b11111111or (to make it really really obvious) 513 & 0b0000000011111111, which masks anything but (all) the (8) bits of the least significant byte.

    We stick both bytes - also called most and least significant bytes - into an array and pass that array to .write().

    After reading, we have - of course - to do the 'reverse': we shift the byte we read first (r[0]) 8 bits to the left (with shift-left operator (<<) to get the most significant byte back (as a number) and add just the second byte as is. Again, the nicety of JavaScript does its part and makes out of the the first read Uint8, which is just 1 byte or 8 bits AND unsigned, a signed number of (at least) two bytes, and adds the second Uint8 - unsigned integer of 8 bits - as number properly to it.

    *Note: arithmetic operators have precedence over shift-right (>>)) and shift-left (<<) and therefore the shift operations have to be given precedence by surrounding them with parenthesis (where needed). With 513 and no parenthesis, the typical binary-computing error of + or - 1 shows for read back: 512. ;-)

    What if we have integer that do not fit into to bytes anymore, such as values greater than 65535 and less than -65536? Espruino can store integer values from 2^31-1 down to -2^31 - 4 bytes, 32 bits.

    *Note: Watch out, that any intermediary result or term within an arithmetic expression or Math function that is or uses floating point will result in a floating point, even if mathematics 'suggests' an integer result and the result is within range of the max to min integer.

    An abstraction handling just one non-negative integer value up to 32767 - the most simple thing next to a single integer value up to 255 or character - in one write is not good enough. Multiple values of any mix and match types of string, int, float, boolean and even Date have to be handled with on write and read operation. And what about handling of null and undefined?...

    Two common abstraction options are:

    • CSV - write/read a CSV (comma separated values) string like:

          513,ABC,2,-513
    
    • JSON - write/read a JSON stringified object, a string like:

          {"pInt":513,"s":"ABC","i":2,"nInt":-513}­
    

    Immediately one notice the storage footprint difference 14 vs. 41 bytes (I love this number coincidence), but also the difference in freedom, constraints AND consequences that go with either option. For example, even with providing the column names for CSV for that single one record of data in a first line or row (in file or a string that contains multiple such 'records'), CSV outperforms footprint wise JSON by 13 bytes (28 vs 41 bytes), and with each additional record, it gets better for CSV and worse for JSON:

          pInt,s,i,nInt[lf]
          513,ABC,2,-513
    

    Note: mileage varies with length of column names and some other things... you may claim... rightly so for one line... ;-)

    Multiple records:

          pInt,s,i,nInt[lf]
          513,ABC,2,-513[lf]
          6,xyzAndMore,7023,-1023
    

    respective

          [{"pInt":513,"s":"ABC","i":2,"nInt":-513­}[lf]
          ,{"pInt":6,"s":"xyzAndMore","i":7023,"nI­nt":-1023}[lf]
          ]
    

    Parsing get also a bit different... but for handling anything beyond a simple list of values, for example nested lists of values - where 'one value' is actually a list of values - arrays or objects - JSON's might cannot be beaten.

    A third option is XML, which is even more verbose / has a tremendously larger foot print - but on the other hand enables a plethora of very good things for robust data exchange between disparate systems and applications, such as validation against a schema for (coditional and non-conditional) structures AND values, name spaces, character sets, etc.,... Since in recent times JSON became THE quasi de-facto standard pushing xml a bit a side - even in non-JavaScript environments - JSON is used all over the place. Even though JSON is not as robust and flexible in (global) interpretation of conveyed data as XML is, it is a good middle ground.

    CSV is about equally useful and used, especially when data has - for what ever reason - table-like format, like data that is exchanged with spread-sheets and relational (SQL) databases, and last but not least legibility for people with little exposure to - and need for - formal languages. With CSV data (as many lines in a file as rows in the spread sheet or data base table), one has to know what each columns means. Therefore, CSV writers and readers have the option to exchange the meaning in the first line (row before the first data row): instead of the values, the names of the values are passed. As noticed in comparison with JSON, CSV data elements have no explicit type notion what so ever: ...,123,... can be interpreted as number 123 OR as string "123". Since columns are usually of the same type, name/position/column number information is sufficient for proper interpretation, and the penalty for absent values is practically negligible: just a comma,... and strings save too (even if it is just 2 bytes). The only contention to look out for is when string values contain comma(s). Such commas have to be escaped in order to allow proper parsing on the receiving end, which is splitting by comma (,) to get the individual (column) values.

    For both CSV and JSON options, writing requires - first - a (payload) string to be composed, then - 2nd - prepended with the payload's length by two bytes (Uint8s) - similar to the processing of the number 513 - and then - 3rd and lastly - writing of the whole, new string is to be written.

    For reading back, first, two bytes are read of which - 2nd - the length of the payload is calculated - exactly as reconstructing the number 513 from Uint8Array - to - 3rd - read the whole string and - 4th and lastly - parsing is applied to reconstruct the values.

    Unique to the options is just the composing of the payload string for writing and the parsing after reading.

    CSV is RYO - may be long after Rio 2016, RYO may become an olympic discipline - though for now it just means: roll your own (module), where JSON is easy-peasy. JavaScript (language) has built JSON handling and it is a refreshing breeze. It is a breeze not just for the coding, but also for the execution: from a performance and storm' point of view, JavaScript makes it THE Perfect Storm: it is fast because it is part of the heart of the JavaScript source code compiler / parser / interpreter, and those are by definition performance tuned to the utmost detail.

    Code examples demonstrating both CSV and JSON options follow in separate posts.

  • Thanks! Yes, I think perhaps right at the top of the FlashEEPROM reference we should include information about reading an writing JSON (perhaps even adding utility functions for it?).

    IMO initially most people won't be worried about storage footprints... That should only really come later if they find there's a problem (which is probably unlikely with a 128kB flash page on the Pico!)...

  • @Gordon, working on the examples that provide some utilities for CSV and JSON storage. I'm currently exploring some variations...

  • Try this exercise:

    //Uint8Trick.js
    //30 Aug 2016
    var i;
    
    console.log("Create an object with an Uint8Array");
    var A={B:512,C:new Uint8Array(16)};
    for(i=0;i<16;i++)A.C[i]=i;
    console.log("A= ",A);
    
    console.log("Stringify the object");
    var D=JSON.stringify(A);
    console.log("D= ",D);
    
    console.log("Parse it back returns undefined");
    var F=JSON.parse(D);
    console.log("F= ",F); //F is undefined
    
    //notice I skipped E to avoid conflict with E.toUint8Array
    
    //the trick
    console.log("apply btoa trick");
    A.C=btoa(A.C);
    console.log("A= ",A);
    
    console.log("stringify");
    var D=JSON.stringify(A);
    console.log("D= ",D);
    
    console.log("parse");
    var F=JSON.parse(D);
    console.log("F= ",F);
    
    //the trick in reverse
    console.log("reverse the trick");
    F.C=E.toUint8Array(atob(F.C));
    console.log("Frev= ",F);
    

    and the output

    >echo(0);
    Create an object with an Uint8Array
    A=  { "B": 512,
      "C": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
     }
    Stringify the object
    D=  {"B":512,"C":new Uint8Array([0,1,2,3,4,5,6,7,8,9,10,11,12­,13,14,15])}
    Parse it back returns undefined
    F=  undefined
    apply btoa trick
    A=  { "B": 512,
      "C": "AAECAwQFBgcICQoLDA0ODw=="
     }
    stringify
    D=  {"B":512,"C":"AAECAwQFBgcICQoLDA0ODw=="}­
    parse
    F=  { "B": 512,
      "C": "AAECAwQFBgcICQoLDA0ODw=="
     }
    reverse the trick
    Frev=  { "B": 512,
      "C": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
     }
    =undefined
    

    1 Attachment

  • An object containing a Unit8Array is written to flash and read back again.
    I think I read somewhere thatthe atob and btoa functions operate on 4 byte increments and you have to pad the length to a integer multiple of 4.
    var A=new Uint8Array(8) will work. var A=new Uint8Array(7) won't.
    Give it a try anyway.

    console.log("Create an object with an Uint8Array");
    var A={B:10,C:new Uint8Array(16)};
    for(i=0;i<16;i++)A.C[i]=i;
    console.log("A= ",A);
    

    The output:

    >echo(0);
    Create an object with an Uint8Array
    A=  { "B": 10,
      "C": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
     }
    Stringify the object
    D=  {"B":10,"C":new Uint8Array([0,1,2,3,4,5,6,7,8,9,10,11,12­,13,14,15])}
    Parse it back returns undefined
    F=  undefined
    apply btoa trick
    A=  { "B": 10,
      "C": "AAECAwQFBgcICQoLDA0ODw=="
     }
    stringify
    D=  {"B":10,"C":"AAECAwQFBgcICQoLDA0ODw=="}
    Write D to flash
    Read G from flash
    G=  new Uint8Array([123, 34, 66, 34, 58, 49, 48, 44, 34, 67, 34, 58, 34, 65, 65, 69, 67, 65, 119, 81, 70, 66, 103, 99, 73, 67, 81, 111, 76, 68, 65, 48, 79, 68, 119, 61, 61, 34, 125])
    parse G into H
    H=  { "B": 10,
      "C": "AAECAwQFBgcICQoLDA0ODw=="
     }
    reverse the trick
    H=  { "B": 10,
      "C": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
     }
    

    1 Attachment

  • So I tried an array of 15 length and it works.
    This version adds another step to do Uint16Array.

    console.log("Add this step to convert Uint16Array to Uint8Array");
    A.C = new Uint8Array(A.C.buffer);
    //the trick
    console.log("apply btoa trick");
    A.C=btoa(A.C);
    console.log("A= ",A);
    

    and

    //the trick in reverse
    console.log("reverse the trick");
    H.C=E.toUint8Array(atob(H.C));
    console.log("H= ",H);
    
    console.log("Convert H.C back to Uint16Array");
    H.C= new Uint16Array(H.C.buffer);
    console.log("H16= ",H);
    

    The output:

    >echo(0);
    Create an object with an Uint16Array
    A=  { "B": 1240,
      "C": new Uint16Array([0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400])
     }
    Stringify the object
    D=  {"B":1240,"C":new Uint16Array([0,100,200,300,400,500,600,7­00,800,900,1000,1100,1200,1300,1400])}
    Parse it back returns undefined
    F=  undefined
    Add this step to convert Uint16Array to Uint8Array
    apply btoa trick
    A=  { "B": 1240,
      "C": "AABkAMgALAGQAfQBWAK8AiADhAPoA0wEsAQUBXg­F"
     }
    stringify
    D=  {"B":1240,"C":"AABkAMgALAGQAfQBWAK8AiADh­APoA0wEsAQUBXgF"}
    Write D to flash
    Read G from flash
    G=  new Uint8Array([123, 34, 66, 34, 58, 49, 50, 52, 48, 44, 34, 67, 34, 58, 34, 65, 65, 66, 107, 65, 77, 103, 65, 76, 65, 71, 81, 65, 102, 81, 66, 87, 65, 75, 56, 65, 105, 65, 68, 104, 65, 80, 111, 65, 48, 119, 69, 115, 65, 81, 85, 66, 88, 103, 70, 34, 125])
    parse G into H
    H=  { "B": 1240,
      "C": "AABkAMgALAGQAfQBWAK8AiADhAPoA0wEsAQUBXg­F"
     }
    reverse the trick
    H=  { "B": 1240,
      "C": new Uint8Array([0, 0, 100, 0, 200, 0, 44, 1, 144, 1, 244, 1, 88, 2, 188, 2, 32, 3, 132, 3, 232, 3, 76, 4, 176, 4, 20, 5, 120, 5])
     }
    Convert H.C back to Uint16Array
    H16=  { "B": 1240,
      "C": new Uint16Array([0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400])
     }
    

    Anyone want to try Uint32Array?


    1 Attachment

  • Unionized arrays anyone?

    //ArrayTrick1.js
    //30 Aug 2016
    //https://developer.mozilla.org/en-US/do­cs/Web/JavaScript/Reference/Global_Objec­ts/ArrayBuffer
    
    var f = new (require("FlashEEPROM"))();
    
    var buff = new ArrayBuffer(16);
    var view   = new Int32Array(buff);
    
    for(var i=0;i<4;i++)view[i]=i*256*256*256+i*100;­
    
    //buff and view are unionized
    console.log("buff= ",buff);
    console.log("view= ",view);
    
    buff[0]=5;
    console.log("buff= ",buff);
    console.log("view= ",view);
    
    console.log("write to ROM");
    f.write(21,JSON.stringify(btoa(buff)));
    
    console.log("read from ROM");
    var H=new Int32Array(E.toArrayBuffer(atob(JSON.par­se(E.toString(f.read(21))))));
    
    console.log("H= ",H);
    

    The output:

    ent>echo(0);
    buff=  new ArrayBuffer([0, 0, 0, 0, 100, 0, 0, 1, 200, 0, 0, 2, 44, 1, 0, 3])
    view=  new Int32Array([0, 16777316, 33554632, 50331948])
    buff=  new ArrayBuffer([5, 0, 0, 0, 100, 0, 0, 1, 200, 0, 0, 2, 44, 1, 0, 3])
    view=  new Int32Array([5, 16777316, 33554632, 50331948])
    write to ROM
    read from ROM
    H=  new Int32Array([5, 16777316, 33554632, 50331948])er code here
    

    1 Attachment

  • Hi @Gordon,

    How many read/write cycles PUCK can perform on each of 0-255 addresses using FlashEEPROM module ?

    I found some discussions here:
    https://devzone.nordicsemi.com/question/­88814/nrf52-writeerase-flash-cycles-conf­usion/
    https://devzone.nordicsemi.com/question/­91122/flash-endurance-on-nrf52/

    Can we say that 10000 is the absolute limit of writes on each of 0-255 addresses using FlashEEPROM for nrf52832 chip embedded on the Puck ?

    How many writes can we perform ?

    Thank you.

  • Hi,

    The FlashEEPROM module uses a kind of journalling approach, so when you write something, if it is different (it checks if it's the same) then the change gets written onto the end of the data. Everything is only rewritten when the 4kB page of data gets full and needs to be compacted.

    So... Lets assume you write 4 bytes of data:

    • It doesn't matter which addresses you use, the wear of one write will always be the same
    • A 4kB page can hold 512 4 byte data chunks, and will only be rewritten after that time. So if there are 10000 page writes available, that means you can write the data 512*10000 = 5,120,000 times before you have problems (so once a minute for 10 years).

    Hope that helps!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Writing and Reading 'persistent' data using FlashEEPROM (journaling) module

Posted by Avatar for allObjects @allObjects

Actions