Skip to main content

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

Writes a full or partial blob into the SPI flash

Availability

Device (from impOS™ 30)

Parameters

Name Type Description
address Integer Address to write the data to
dataSource Blob The source blob from which to copy the data
writeFlags Constant Optional flags to select from pre- and post-verification of the write
startIndex Integer Optional location within the source blob at which reading starts
endIndex Integer Optional location within the source blob at which reading stops

Returns

Integer — 0 if no write verification selected or verification was successful, otherwise a verification failure indicator

Description

This method will copy the contents of a blob bitwise into the SPI flash at the given address. The rules of NOR flash apply, namely that a write can only program bits (change them from 1 to 0) or leave them alone (see spiflash).

The write will succeed on any pre-erased sectors, but it is not guaranteed to succeed otherwise, since no NOR flash write operation can change a bit from 0 to 1. A write which does not attempt this will succeed. A write which does attempt this will result in the stored data not matching the source data.

To help you deal with this, the method can take optional flags which will pre- or post-verify the write operation, or do both:

Flag Constant Value Description
SPIFLASH_PREVERIFY
 
2
 
Before carrying out the operation, the imp checks whether the write should succeed. If it will not, then no write is attempted, and the function returns failure.
SPIFLASH_POSTVERIFY
 
1
 
After carrying out the write operation, the imp reads back the data from the SPI flash and checks it against the request.

The values assigned to the constants may change in a future impOS™ release.

These flags can be specified in any combination. The verification operations will slow down the write operation, but the entire operation will still be dominated by the actual write to SPI flash. If you are sure that the write operation will succeed, then you can omit them. This may be the case if you have pre-erased the sector that you are writing to (using spiflash.erasesector() or have done diligent book-keeping to record the addresses you have written to.

If no verification flags are specified, this method will return 0. It will also return 0 if the write and verification are successful. Otherwise it will return SPIFLASH_PREVERIFY or SPIFLASH_POSTVERIFY to indicate which verification step failed.

The blob will be copied in its entirety (SPI flash space permitting) unless the optional start and end index value parameters are provided. These parameters operate like the string.slice() start and end parameters: the start value is the index of the first byte to be copied; the end value is the index of the byte after the last byte to be copied. For example, if you specify 2 and 6 as the start and end parameters, four bytes will be copied from the blob: bytes 2, 3, 4 and 5.

If the end parameter is omitted, then everything from the start index to the end of the blob is copied.

Example Code

Generate a blob, and save the contents into SPI flash storage.

Writes a slice of a string into SPI flash storage.