• Hi,

    I have a relatively big codebase right now and I'm not sure what causes this error:

    Unable to create string as not enough memory
    

    I guess I'm having trouble while reading from FlashEEPROM with the following class:

    export pack = (x) ->
        JSON.stringify x
    
    export unpack = (wire-data) ->
        try
            x = JSON.parse wire-data
            throw if x is void
            return x
        catch
            console.log "Error on unpacking: ", e
            console.log "wire data: ", wire-data
            throw "Error on unpacking"
    
    export !function Config file-no
        self = this
        @file-no = file-no
        @f = new (require "FlashEEPROM")(0x076000)
        @f.endAddr = @f.addr + 1024
        @write-count = 0
    
        @write = (data) !->
            if @write-count++ > 10
                @f.cleanup!
                @write-count = 0
            @f.write self.file-no, pack data
    
        @read = ->
            try
                data = E.to-string @f.read self.file-no
                return unpack data
            catch
                console.log "ERROR CONFIG READ: ", e
                console.log "raw data read: ", data
                console.log "dump: ", @file-no
    
    

    Here is the Javascript output of above code:

    var pack, unpack, out$ = typeof exports != 'undefined' && exports || this;
    out$.pack = pack = function(x){
      return JSON.stringify(x);
    };
    out$.unpack = unpack = function(wireData){
      var x, e;
      try {
        x = JSON.parse(wireData);
        if (x === void 8) {
          throw null;
        }
        return x;
      } catch (e$) {
        e = e$;
        console.log("Error on unpacking: ", e);
        console.log("wire data: ", wireData);
        throw "Error on unpacking";
      }
    };
    out$.Config = Config;
    function Config(fileNo){
      var self;
      self = this;
      this.fileNo = fileNo;
      this.f = new (require("FlashEEPROM"))(0x076000);
      this.f.endAddr = this.f.addr + 1024;
      this.writeCount = 0;
      this.write = function(data){
        if (this.writeCount++ > 10) {
          this.f.cleanup();
          this.writeCount = 0;
        }
        this.f.write(self.fileNo, pack(data));
      };
      this.read = function(){
        var data, e;
        try {
          data = E.toString(this.f.read(self.fileNo));
          return unpack(data);
        } catch (e$) {
          e = e$;
          console.log("ERROR CONFIG READ: ", e);
          console.log("raw data read: ", data);
          return console.log("dump: ", this.fileNo);
        }
      };
    }
    

    I'm trying to use this code as follows:

    a = new Config 1
    
    inc-input = ->
        try
            x = a.read!
            throw if isNaN x
        catch
            console.log "error reading input counter: ", e
            x = 0
        a.write ++x
        x
    
    sim2 = !->
        <- :lo(op) ->
            console.log inc-input!
            <- sleep 5ms
            lo(op)
    
    

    ...the Javascript:

    var a, incInput, sim2;
    a = new Config(1);
    incInput = function(){
      var x, e;
      try {
        x = a.read();
        if (isNaN(x)) {
          throw null;
        }
      } catch (e$) {
        e = e$;
        console.log("error reading input counter: ", e);
        x = 0;
      }
      a.write(++x);
      return x;
    };
    sim2 = function(){
      (function lo(op){
        console.log(incInput());
        return sleep(5, function(){
          return lo(op);
        });
      })(function(){});
    };
    

    The error is:

    Error on unpacking:  null
    wire data:  undefined
    ERROR CONFIG READ:  Error on unpacking
    raw data read:  undefined
    dump:  1
    144
    145
    146
    147
    ERROR: Out of Memory!
    WARNING: Unable to create string as not enough memory
    ERROR: Error processing Serial data handler - removing it.
    Execution Interrupted during event processing.
    at line 1 col 148
    ....flash.read(a,r.addr+4),h==e))){if(r.end+e.length+4>=this.en...
                                  ^
    in function "write" called from line 1 col 89
    ...is.f.write(n.fileNo,pack(e))
                       in function "write" called from line 1 col 81
    ...g.read(),t.set),cfg.write(o),inpCounter.write(o.coinInpRx),o...
                                  ^
    in function called from system
    148
    149
    150
    151
    152
    

    I expect the sim2() function to increment flash memory area by 1 in every 50 ms and this is what happens most of the time. But sometimes, (in a random order of power cycles) it Config.read() throws an error and memory can not be read.

    Is there anything I might misunderstand about usage of FlashEEPROM?

About

Avatar for ceremcem @ceremcem started