I set the SPI baud to 18MHz... does it really do it that fast? I could not find quickly what the limit is for the chip on the Espruino board running at 72Mhz.
Initially, I could not even reliably read the device id and the status register. The first break through came when putting command, command parameter(s), and necessary clocking into a single array and make it an spi.send(), even though one would expect that a spi.write() would be the thing for commands and command parameter(s), because nothing should be returned for those. With known number of 0xFFs to the response one can live. For the id and status read this was sending:
When using SPI.write() for the commands and parameters, the next SPI.send is returning first a 0xFF for each command and parameter byte, before the data, and leave stuff 'unread'... and subsequently 'read' by the next SPI.send()... just weird... there was no reasonable pattern to discover to discard reliably this unwanted, delayed read stuff... and adding more to the send to 'read' all made it worse: it prepended even more to the next 'read'. To not return unwanted information, I split sending of command and parameters from the clock ticking for receiving. The splitting requires the 'manual' handling of chip select outside of the SPI.send(), but can be kept inside latter for the last SPI.send().
I first did a manual un-set of the write protection, and then do the write, and that made the read work. This told me that I cannot chain commands, like un-set write protection, then write, and finally set write protection again.
, rd1: function(){
var d = spi.send(
[0x03
,0x00,0x00
,0x00,0x00,0x00,0x00
]
,cs);
return d;
}
, rd2: function(a){
var d = spi.send(
[0x03
,a>>8,a
,0x00,0x00,0x00,0x00
]
,cs);
return d;
}
From then on it was figuring out how to do it with the least statements, which unveiled another nifty corner: reset chip select and include the set of it in the send worked for all places except for the write. For the write I had to just do a simple send and then explicitly set chip select high.
Trying to convert a Uint8Array to a string became a bit a detour... Espruino JS does not look at Uint8Array as an array that can be used in a Function.call(null,args) as args parameter as regular JS does (see http://forum.espruino.com/conversations/258049). Resorting to beloved iteration and string concatenation in JS as Uint8Array asString() prototype extension gets it done.
There are a few things left to look into. I was not able to use A5..A7 with A2 as chip select for the above writing sequence. First writes made it, but after a while it garbled. Could it be that B13..B15 are configured differently for SPI? ...like pulling up/down things? Also, actual speed has to be found out. But more important for now: the protocol.
With 'hurray' going, it is now the time to think about a 'good' protocol for writing and reading of the FRAM in applications: a lean, concise protocol - not to fat and not to frugal . Questions for the protocol are: File System? ...may be not, since the amount of memory is small and filesystem overhead may be significant... read/write of data types? Strings, for example ( fram.substr(120,40 ) for 40 bytes starting with address 120,.... or 0 ending/delimited strings, or String objects with length at the beginning? ...reads and writes for every object type... or just limit to String with JSON for other things... Memory management with ribs... or even object-space w/ r/w of 'String objects' w/ 'simple' garbage collection.
Object store w/ simple garbage collection: no reference for mark, just check for active / inactive indicator in the object list. MSBit is not used for address and can therefore be used in an object list held in a heap. Heap residing in one side of the memory and data in the opposite one could be a quite luxury solution - function-wise - and not too fat for the implementation. The protocol would be a simple CRU(D), with create returning a handle (address into object list/heap) for future access, update with automatically managing updates with a different size, and delete with setting the deletede / inactive indicator.
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.
Hurray! ...got it working!
See updated version of module.
Key to the solution is: Commands CANNOT be chained..., they all have to be concluded / committed individually by the chip select to go high!
With that I just have increased my #memory (for very fast data) by 66%.
This is the output (see hurray() function):
I set the SPI baud to 18MHz... does it really do it that fast? I could not find quickly what the limit is for the chip on the Espruino board running at 72Mhz.
Initially, I could not even reliably read the device id and the status register. The first break through came when putting command, command parameter(s), and necessary clocking into a single array and make it an spi.send(), even though one would expect that a spi.write() would be the thing for commands and command parameter(s), because nothing should be returned for those. With known number of 0xFFs to the response one can live. For the id and status read this was sending:
When using SPI.write() for the commands and parameters, the next SPI.send is returning first a 0xFF for each command and parameter byte, before the data, and leave stuff 'unread'... and subsequently 'read' by the next SPI.send()... just weird... there was no reasonable pattern to discover to discard reliably this unwanted, delayed read stuff... and adding more to the send to 'read' all made it worse: it prepended even more to the next 'read'. To not return unwanted information, I split sending of command and parameters from the clock ticking for receiving. The splitting requires the 'manual' handling of chip select outside of the SPI.send(), but can be kept inside latter for the last SPI.send().
Next step was writing to the status register with the lesson learned.
The write and read was a bit more complicated... This was the first working write:
I first did a manual un-set of the write protection, and then do the write, and that made the read work. This told me that I cannot chain commands, like un-set write protection, then write, and finally set write protection again.
From then on it was figuring out how to do it with the least statements, which unveiled another nifty corner: reset chip select and include the set of it in the send worked for all places except for the write. For the write I had to just do a simple send and then explicitly set chip select high.
Trying to convert a Uint8Array to a string became a bit a detour... Espruino JS does not look at Uint8Array as an array that can be used in a Function.call(null,args) as args parameter as regular JS does (see http://forum.espruino.com/conversations/258049). Resorting to beloved iteration and string concatenation in JS as Uint8Array asString() prototype extension gets it done.
There are a few things left to look into. I was not able to use A5..A7 with A2 as chip select for the above writing sequence. First writes made it, but after a while it garbled. Could it be that B13..B15 are configured differently for SPI? ...like pulling up/down things? Also, actual speed has to be found out. But more important for now: the protocol.
With 'hurray' going, it is now the time to think about a 'good' protocol for writing and reading of the FRAM in applications: a lean, concise protocol - not to fat and not to frugal . Questions for the protocol are: File System? ...may be not, since the amount of memory is small and filesystem overhead may be significant... read/write of data types? Strings, for example ( fram.substr(120,40 ) for 40 bytes starting with address 120,.... or 0 ending/delimited strings, or String objects with length at the beginning? ...reads and writes for every object type... or just limit to String with JSON for other things... Memory management with ribs... or even object-space w/ r/w of 'String objects' w/ 'simple' garbage collection.
Object store w/ simple garbage collection: no reference for mark, just check for active / inactive indicator in the object list. MSBit is not used for address and can therefore be used in an object list held in a heap. Heap residing in one side of the memory and data in the opposite one could be a quite luxury solution - function-wise - and not too fat for the implementation. The protocol would be a simple CRU(D), with create returning a handle (address into object list/heap) for future access, update with automatically managing updates with a different size, and delete with setting the deletede / inactive indicator.
Any protocol suggestions are welcome!