Skip to main content

SPIFlash

Latest Version: 1.0.1

The SPIFlash class allows you to use a SPI Flash on the imp001 and imp002 with an interface similar to the imp003-centric hardware.spiflash().

You can view the library’s source code on GitHub. Click here to see information on other versions of this library.

To add this library to your project, add #require "SPIFlash.class.nut:1.0.1" to the top of your device code.

Class Usage

Constructor: SPIFlash(spi, cs[, blocks])

The class’ constructor takes two required parameters: a configured imp SPI bus object object, and a pin object) for the imp pin connected to the SPI Flash Chip Select (CS) pin. Optionally, you may also pass the number of 64KB blocks in the SPI Flash chip.

Parameter Type Default Description
spi hardware.spi No Default A pre-configured SPI bus
cs hardware.pin No Default The chip select pin
blocks number 64 The number of 64KB blocks on the SPIFlash
#require "SPIFlash.class.nut:1.0.1"

spi <- hardware.spi257;
spi.configure(CLOCK_IDLE_LOW | MSB_FIRST, 30000);

cs <- hardware.pin8;

spiFlash <- SPIFlash(spi, cs);

Class Methods

The SPIFlash class conforms to the hardware.spiflash() API, and all methods available to the hardware.spiflash() object are available to instantiated SPIFlash objects. For more in-depth usage and examples, see the hardware.spiflash() documentation.

configure([dataRate_kHz])

This method will autoconfigure the SPI bus passed into the constructor, and return the set data rate.

#require "SPIFlash.class.nut:1.0.1"

spiFlash <- SPIFlash(hardware.spi257, hardware.pin8);
spiFlash.configure(30000);

chipid()

Returns the identity code of the SPI flash chip.

See hardware.spiflash.chipid() for more information.

spiFlash.enable();
server.log(spiFlash.chipid());

disable()

Disables the SPI flash for reading and writing.

See hardware.spiflash.disable() for more information.

spiFlash.disable();

enable()

Enables the SPI flash for reading and writing.

See hardware.spiflash.enable() for more information.

spiFlash.enable();

erasesector(sectorAddress)

Erases a 4KB sector of the SPI flash.

See hardware.spiflash.erasesector() for more information.

// Erase the first three sectors
spiFlash.erasesector(0x0000);
spiFlash.erasesector(0x1000);
spiFlash.erasesector(0x2000);

read(address, numberOfBytes)

Copies data from the SPI flash and returns it as a series of bytes.

See hardware.spiflash.read() for more information.

spiFlash.enable();
// Read 36 bytes from the beginning of the third sector
local buffer = spiFlash.read(0x2000, 36);
spiFlash.disable();

readintoblob(address, targetBlob, numberOfBytes)

Copies data from the SPI flash storage into a pre-existing blob.

See hardware.spiflash.readintoblob() for more information.

buffer <- blob(1024);

spiFlash.enable();
spiFlash.readintoblob(0x1000, buffer, 128);
spiFlash.readintoblob(0x2000, buffer, 256);
spiFlash.disable();

size()

Returns the total number of bytes in the SPI flash that are available to Squirrel.

See hardware.spiflash.size() for more information.

spiFlash.enable();
server.log(spiFlash.size() + " Bytes");

write(address, dataSource, writeFlags, startIndex, endIndex)

Writes a full or partial blob into the SPI flash.

See hardware.spiflash.write() for more information.

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release
1.0.1 GitHub Bug fixes

License

The SPIFlash class is licensed under MIT License.