The (commented) write code has the issue of early terminating the write cycle:
AT25.prototype.write= function(add,data,num) {
var l=data.length;
if(typeof data!="string"){data=E.toString(data);}
var t=new Uint8Array([0,1,2,3]);
var i=0;
t[i++]=2;
t[i++]=add>>16;
t[i++]=add>>8;
t[i++]=add;
print("\r\nin write cmd and address :"+ t+'\n');
this.spi.send(t,this.cspin);
i=0;
// while (i < l)
this.spi.send(data,this.cspin);
print("\r\n wrinting data:"+ data);
i++;
return l;
};
By using the cspin in the spi.send() of the write command and addresses, the write cycle is terminated... if you want to split the write into two *spi.send()*s, you do separately lower the cspin, send the command and address, send the data to be written, and the rise the cspin again separately... you can also rise the cspin a bit hacky by mentioning it in the spi.send() of the data. You then do not need to do it separately. As per data sheet, you have to send the command, address and at least one byte for a successful write.
Something like this should actually work (single, combined send for command, address and data, until all data is written - first to write to (and fill up) the page holding add, then next page/s (if there are), and then remainder of data for the last page (if there is):
AT25.prototype.write= function(add,data) {
if(typeof data!="string"){data=E.toString(data);}
var l=data.length, idx=0, i, t;
while (l > 0) {
i=((i = this.pgsz-(add%this.pgsz)) > l) ? l : i;
t=(this.cap>65536)?E.toString(2,add>>16,add>>8,add):E.toString(2,add>>8,add);
t=t+data.substr(idx,i);
while ((this.spi.send(5,this.cspin)[0] & 1) === 1) { ""; }
this.spi.send(6,this.cspin);
this.spi.send(t,this.cspin);
l-=i;idx+=i; add+=i;
}
return idx;
};
The above code does also a bit reorder the sequence to do everything before actually being ready to write in the time while a possible write from before still has to complete. Furthermore, the length is gathered after the possible conversion to string.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
The (commented) write code has the issue of early terminating the write cycle:
By using the cspin in the spi.send() of the write command and addresses, the write cycle is terminated... if you want to split the write into two *spi.send()*s, you do separately lower the cspin, send the command and address, send the data to be written, and the rise the cspin again separately... you can also rise the cspin a bit hacky by mentioning it in the spi.send() of the data. You then do not need to do it separately. As per data sheet, you have to send the command, address and at least one byte for a successful write.
Something like this should actually work (single, combined send for command, address and data, until all data is written - first to write to (and fill up) the page holding add, then next page/s (if there are), and then remainder of data for the last page (if there is):
The above code does also a bit reorder the sequence to do everything before actually being ready to write in the time while a possible write from before still has to complete. Furthermore, the length is gathered after the possible conversion to string.