W25Qxx and W25Xxx Serial NOR Flash
Winbond W25Qxx and W25Xxx chips (eg. W25Q32, W25Q64, W25X05, etc) are SPI flash memory chips with capacities ranging from 512K-bit to 512M-bit.
This W25 (About Modules) module is based on the code in https://github.com/pastaclub/espruino-w25q by Dennis Bemmann and atmelino
Wiring
Find out which pins of your Espruino are SPI-capable. Connect the SPI pins to the corresponding pins on the W25Q Flash memory, add GND, and 3.3v, and a pin for Chip Select (CS) which can be connected to any IO.
Note: Software SPI may also be used but is just slightly slower.
Initialization
var W25 = require("W25");
var myflash = new W25(SPI1, B6 /*CS*/);
SPI1.setup({ mosi: B5, miso: B4, sck: B3});
The memory of the W25Q Flash is organized in pages of 256 bytes each, and sectors of 16 pages each.
Sector | Start Page | End Page |
---|---|---|
0 | 0 | 15 |
1 | 16 | 31 |
2 | 32 | 47 |
etc.
Preparation for Writing
Before you can write to Flash memory, you must first erase the flash memory at the address which you want to write to. In the case of Winbond WQ25, individual bytes or pages cannot be erased-the minimum quantity that can be erased is a sector(16 pages at once.)
myflash.erase16Pages(page);
Note: If you send a command to erase a section with a page number in between start and end, the sector in which the page is will be erased. For example: If you send a command to erase at page 5, then pages 0 to 15 will be erased.
Writing and Reading
var page = 345;
const uint8 = new Uint8Array(256);
uint8.fill(101, 0, 256); //(value, start position, end position);
myflash.erase16Pages(page);
myflash.writePage(page, uint8);
console.log(myflash.readPage(page));
Sector Modification
If you want to modify data in a sector without losing the rest of the data of the sector, you would have to first read the sector into an array, modify the array, and then write the (modified) array back to the flash memory.
var mysector = myflash.readSector(1);
mysector[123] = 72;
myflash.erase16Pages(16);
myflash.writeSector(16, mysector);
Reference
W25.prototype.seek = function (pageNumber, offset) { ... }
W25.prototype.read = function () { ... }
W25.prototype.waitReady = function () { ... }
W25.prototype.eraseChip = function () { ... }
W25.prototype.erase16Pages = function (pageNumber) { ... }
W25.prototype.writePage = function (pageNumber, arrayBuffer) { ... }
W25.prototype.writePageFillSpace = function (pageNumber, arrayBuffer) { ... }
W25.prototype.writeSector = function (pageNumber, arrayBuffer) { ... }
W25.prototype.startWrite = function (pageNumber, offset) { ... }
W25.prototype.send = function (data) { ... }
W25.prototype.write = function (data) { ... }
W25.prototype.finish = function () { ... }
W25.prototype.getJedec = function () { ... }
W25.prototype.getCapacity = function () { ... }
W25.prototype.command = function (cmd) { ... }
W25.prototype.setAddress = function (pageNumber, offset) { ... }
W25.prototype.readPage = function (pageNumber) { ... }
W25.prototype.readSector = function (sector) { ... }
W25.prototype.readPageString = function (page) { ... }
exports.connect = function (spi, csPin) { ... }
function (spi, csPin) { ... }
Ultra Lightweight version
For simple read/write tasks there is an ultra-lightweight implementation (W25_tiny (About Modules)) which is only 20 lines.
It provides erase16Pages
, readPage
and writePage
only.
var W25 = require("W25_tiny");
var myflash = new W25(SPI1, B6 /*CS*/);
SPI1.setup({ mosi: B5, miso: B4, sck: B3});
This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.