pub struct Flash<'a, A: FlashAccess> { /* private fields */ }
Expand description

SPI Flash.

This struct provides methods for interacting with common SPI flashes.

Implementations

Create a new Flash instance using the given FlashAccess provider.

Get the number of address bytes which will be used in read and write commands.

Set the number of address bytes to use with read and write commands. By default this is set to 3, and can also be autodiscovered using SFDP.

Panics if n is less than 1 or greater than 4.

Get the total memory capacity in bytes, if known.

Set the total memory capacity in bytes.

If set, or discovered using SFDP, reads and writes are prevented from going beyond the memory capacity.

Get the page program size in bytes.

Set the page program size in bytes.

This must be known before page program operations can be performed.

Get the sector erase size in bytes, if known.

Set the sector erase size in bytes.

This must be known before sector erase operations can be performed.

Get the opcode used for the Erase Sector instruction.

Set the opcode used for the Erase Sector instruction.

This is 0x20 by default.

Get the flash ID, if it has already been read.

Call read_id() to read the ID from the flash.

Get the flash parameters, if they have already been read.

Call read_params() to read the params from the flash.

Read the device’s manufacturer ID and device IDs.

This method additionally brings the flash out of powerdown and resets it.

self.id is updated with the new ID; use get_id() to retrieve it without re-reading the ID from the flash.

Read SFDP JEDEC Basic Flash Parameter table from flash.

Access errors are returned as usual, but if SFDP is not supported (no SFDP signature is detected in the first SFDP DWORD) then Ok(None) is returned instead.

Depending on the version of SFDP supported, some fields may not be available.

Once read, the parameters are available using get_params(), and the parameter values are automatically used for the configuration of address bytes, capacity, page size, sector size, and erase sector opcode. Additionally, larger erase commands described by the SFDP parameters will be used when appropriate.

Read length bytes of data from the attached flash, starting at address.

This method uses the FastRead instruction; if it is not supported try using legacy_read() instead.

Read length bytes of data from the attached flash, starting at address.

This method uses the legacy ReadData instruction, which often has a low maximum clock speed compared to other operations, but is more widely supported and may be faster for very short reads as it does not require a dummy byte.

Read length bytes of data from the attached flash, starting at address.

This method is similar to the read() method, except it calls the provided callback function at regular intervals with the number of bytes read so far.

While read() performs a single long SPI exchange, this method performs up to 128 separate SPI exchanges to allow progress to be reported.

Read length bytes of data from the attached flash, starting at address.

This method is similar to the read() method, except it renders a progress bar to the terminal during the read.

While read() performs a single long SPI exchange, this method performs up to 128 separate SPI exchanges to allow progress to be reported.

Erase entire flash chip.

This method uses the ChipErase instruction, so no progress information is available, but the typical and maximum times taken may be available from the SFDP parameters.

Returns only after erase operation is complete.

Erase entire flash chip.

This method is identical to erase(), except it draws a progress bar to the terminal during the erase operation.

If SFDP data indicates a typical chip erase time, that value is used, otherwise the progress bar is drawn as a spinner.

Program the attached flash with data starting at address.

Sectors and blocks are erased as required for the new data, and existing data outside the new data to write is written back if it has to be erased.

If verify is true, the programmed data is read back, and a ReadbackError will be returned if it did not match what was written.

When available, SFDP parameters are used to generate an efficient sequence of erase instructions. If unavailable, the single erase instruction in erase_opcode is used, and its size of effect must be given in erase_size. If these are not set, a NoEraseInstruction is returned.

The programming page size is set by page_size, which is automatically set when SFDP parameters are read.

Program the attached flash with data starting at address.

This is identical to program(), except it also draws progress bars to the terminal.

Reset the attached flash.

The instruction sequence EnableReset 0x66 followed by Reset 0x99 is sent by default, but if the SFDP parameters indicate that only the 0xF0 instruction is supported for reset, that is sent instead.

Check if any block protect bits are set in status register 1.

Set block protection bits.

This sets the block protect bits in status register 1, using the non-volatile commands if supported. If available, the SFDP parameters are used to determine the correct non-volatile instruction.

Clear any protection bits that are set.

This checks and clears the block protect bits in status register 1, using the non-volatile commands if supported. If available, the SFDP parameters are used to determine the correct non-volatile instruction.

Clear the write-protect-selection bit in status register 3, if set.

This status bit configures the fine-granularity write protection which is a vendor-specific extension that is disabled by default.

Unfortunately it is not possible to determine automatically if a flash chip supports the WPS feature or even has a status register 3, so this command is not called automatically.

Power down the flash.

Power up the flash.

Returns the legacy device ID.

Program data to address, automatically split into multiple page program operations.

Note that this does not erase the flash beforehand; use program() for a higher-level erase-program-verify interface.

Program data to address, automatically split into multiple page program operations, and draws a progress bar to the terminal.

Note that this does not erase the flash beforehand; use program() for a higher-level erase-program-verify interface.

Program data to address, automatically split into multiple page program operations.

Note that this does not erase the flash beforehand; use program() for a higher-level erase-program-verify interface.

Calls cb with the number of bytes programmed so far after each page programming operation.

Send the WriteEnable command, setting the WEL in the status register.

Program up to one page of data.

This method sets the write-enable latch and then waits for programming to complete, including sleeping half the typical page program time if known before polling.

Note that this does not erase the flash beforehand; use program() for a higher-level erase-program-verify interface.

Reads the JEDEC manufacturer and long (16-bit) device IDs.

The manufacturer ID may be prefixed with up to 13 of the continuation code 0x7F; the number of continuation codes is returned as the bank number.

Returns (bank, manufacturer ID, device ID).

Reads the JEDEC manufacturer and short (8-bit) device IDs.

The manufacturer ID may be prefixed with up to 13 of the continuation code 0x7F; the number of continuation codes is returned as the bank number.

Returns (bank, manufacturer ID, device ID).

Read the device’s 64-bit unique ID, if present.

Read status register 1.

Read status register 2.

This status register is less widely supported and SFDP does not indicate whether or not it is present.

Read status register 3.

This status register is less widely supported and SFDP does not indicate whether or not it is present.

Write status register 2.

Write status register 3.

Check if the device is currently busy performing an operation.

If the flash parameters indicate support for the Flag Status Register instruction (0x70), it is used, otherwise legacy polling of status register 1 is used.

Wait until the device stops being busy.

This polls using is_busy(), which uses the flag status register if available or otherwise uses status register 1.

Read SFDP register data.

addr is always sent as a 24-bit address, regardless of the address_bytes setting.

Writes command and data to the flash memory, then returns nbytes of response.

Writes command and data to the flash memory, without reading the response.

Convenience method for issuing a single command and not caring about the returned data

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.