API

Winbond Flash

Implementation to interact with Winbond W25Q Flash with software reset.

Credits & kudos to crizeo Taken from https://forum.micropython.org/viewtopic.php?f=16&t=3899

class winbond.winbond.W25QFlash(spi: <Mock name='mock.SPI' id='140257411520160'>, cs: <Mock name='mock.Pin' id='140257411520208'>, baud: int = 40000000, software_reset: bool = True)[source]

Bases: object

W25QFlash implementation

_await() None[source]

Wait for device not to be busy

_read(buf: list, addr: int) None[source]

Read the length of the buffer bytes from the chip.

The buffer length has to be a multiple of self.SECTOR_SIZE (or less).

Parameters
  • buf (list) – The buffer

  • addr (int) – The start address

_read_status_reg(nr) int[source]

Read a status register.

Parameters

nr (int) – Register number to read

Returns

The value (0 or 1) in status register (S0, S1, S2, …)

Return type

int

_sector_erase(addr) None[source]

Resets all memory within the specified sector (4kB) to 0xFF

Parameters

addr (int) – The address

_wren() None[source]

Set the Write Enable Latch (WEL) bit in the status register

_write(buf: list, addr: int) None[source]

Write the data of the given buffer to the address location

Writes the data from <buf> to the device starting at <addr>, which has to be erased (0xFF) before. Last byte of <addr> has to be zero, which means <addr> has to be a multiple of self.PAGE_SIZE (= start of page), because wrapping to the next page (if page size exceeded) is implemented for full pages only. Length of <buf> has to be a multiple of self.PAGE_SIZE, because only full pages are supported at the moment (<addr> will be auto-incremented).

Parameters
  • buf (list) – The data buffer to write

  • addr (int) – The starting address

_writeblock(blocknum: int, buf: list) None[source]

Write a data block.

To write a block, the sector (e.g. 4kB = 8 blocks) has to be erased first. Therefore, a sector will be read and saved in cache first, then the given block will be replaced and the whole sector written back when

Parameters
  • blocknum (int) – The block number

  • buf (list) – The data buffer

property capacity: int

Get the storage capacity of the flash

Returns

Capacity of the flash in bytes

Return type

int

count() int[source]

Return the number of blocks available on the device

Returns

Number of blocks

Return type

int

property device: int

Get the flash device type

Returns

Flash device type

Return type

int

format() None[source]

Format the Winbond flash chip by resetting all memory to 0xFF.

Important: Run “os.VfsFat.mkfs(flash)” to make the flash an accessible file system. As always, you will then need to run “os.mount(flash, ‘/MyFlashDir’)” then to mount the flash

get_size() int[source]

Get the flash chip size.

Returns

The flash size in byte.

Return type

int

identify() None[source]

Identify the Winbond chip.

Determine the manufacturer and device ID and raises an error if the device is not detected or not supported. The capacity variable is set to the number of blocks (calculated based on the detected chip).

property manufacturer: int

Get the manufacturer ID of the flash

Returns

Manufacturer ID of the flash

Return type

int

property mem_type: int

Get the memory type of the flash

Returns

Memory type of the flash

Return type

int

readblocks(blocknum: int, buf: list) None[source]

Read a data block. The length has to be a multiple of self.BLOCK_SIZE

Parameters
  • blocknum (int) – The starting block number

  • buf (list) – The data buffer

reset() None[source]

Reset the Winbond flash if the device has no hardware reset pin.

See datasheet section 7.2.43 Enable Reset (66h) and Reset (99h) Because of the small package and the limitation on the number of pins, the W25Q64FV provide a software Reset instruction instead of a dedicated RESET pin. Once the Reset instruction is accepted, any on-going internal operations will be terminated and the device will return to its default power-on state and lose all the current volatile settings, such as Volatile Status Register bits, Write Enable Latch (WEL) status, Program/Erase Suspend status, Read parameter setting (P7-P0), Continuous Read Mode bit setting (M7-M0) and Wrap Bit setting (W6-W4). “Enable Reset (66h)” and “Reset (99h)” instructions can be issued in either SPI mode or QPI mode. To avoid accidental reset, both instructions must be issued in sequence. Any other commands other than “Reset (99h)” after the “Enable Reset (66h)” command will disable the “Reset Enable” state. A new sequence of “Enable Reset (66h)” and “Reset (99h)” is needed to reset the device. Once the Reset command is accepted by the device, the device will take approximately tRST=30us to reset. During this period, no command will be accepted. Data corruption may happen if there is an on-going or suspended internal Erase or Program operation when Reset command sequence is accepted by the device. It is recommended to check the BUSY bit and the SUS bit in Status Register before issuing the Reset command sequence.

writeblocks(blocknum: int, buf: list) None[source]

Write a data block.The length has to be a multiple of self.BLOCK_SIZE

Parameters
  • blocknum (int) – The block number

  • buf (list) – The data buffer