This doc is primarily to aid those wishing to use these base classes to write
drivers for additional memory devices. It describes the two classes in
bdevice.py namely BlockDevice and the subclass FlashDevice. Both provide
hardware-independent abstractions of memory devices. The base class provides
the API. This has the following characteristics:
- Support for single or multiple chips on the same bus. Multiple chips are automatically configured as a single byte array.
- The byte array can be accessed using Python slice syntax.
- Alternatively the array can be formatted and mounted as a filesystem using
methods in the
uosmodule. Any filesystem supported by the MicroPython build may be employed: FAT and littlefs have been tested. The latter is recommended.
The BlockDevice class supports byte-addressable technologies such as EEPROM
and FRAM. Such devices can be written on a single byte basis. Where a chip also
offers multi-byte writes this optimisation can be handled in the user driver:
see the EEPROM drivers for examples of this.
FlashDevice subclasses BlockDevice to support devices which must buffer a
sector of data for writing. The API continues to support byte addressing: this
is achieved by modifying the buffer contents and writing it out when necessary.
The class provides these characteristics:
- An API which represents multiple physical devices as a single byte array. The physical means of achieving this is provided in the hardware subclass.
- An implementation of the
AbstractBlockDevprotocol with extended interface as required by littlefs as documented here. - An API based on Python slice notation for byte level access to the array.
- Support for the
lenoperator.
Constructor args - mandatory, positional, integer
nbitsBlock size reported to the filesystem expressed as a number of bits: the block size is2^nbits. The usual value is 9 (512 bit block).nchipsNumber of chips in the array.chip_sizeSize of each chip in bytes.
The subclass must provide a method readwrite taking the following args:
addrAddress relative to the start of the array.bufA buffer holding data to write or to contain data to be read.readBoolean:Trueto read,Falseto write.
The amount of data read or written is defined by the length of the buffer.
Return value: the buffer.
The method must handle the case where a buffer crosses chip boundaries. This involves physical accesses to each chip and reading or writing partial buffer contents. Addresses are converted by the method to chip-relative addresses.
This is provided by the following methods:
-
sync()In theBlockDeviceclass this does nothing. It is defined in theFlashDeviceclass section 3.3. -
readblocks(blocknum, buf, offset=0)Converts the block address and offset to an absolute address into the array and callsreadwrite. -
writeblocks(blocknum, buf, offset=0Works as above. -
ioctlThis supports the following operands: -
syncCalls the.sync()method. -
sector countReturnschip_size*nchips//block_size -
block sizeReturns block size calculated as in section 2.1. -
eraseNecessary for correct filesystem operation: returns 0.
The drivers make no use of the block size: it exists only for filesystems. The
readwrite method hides any physical device structure presenting an array of
bytes. The specified block size must match the intended filesystem. Littlefs
requires >=128 bytes, FATFS requires >=512 bytes. All testing was done with 512
byte blocks.
This is provided by __getitem__ and __setitem__. The addr arg can be an
integer or a slice, enabling the following syntax examples:
a = eep[1000] # Read a single byte
eep[1000] = 42 # write a byte
eep[1000:1004] = b'\x11\x22\x33\x44' # Write 4 consecutive bytes
b = eep[1000:1004] # Read 4 consecutive bytesThe last example necessarily performs allocation in the form of a buffer for
the resultant data. Applications can perform allocation-free reading by calling
the readwrite method directly.
This returns the array size in bytes.
By subclassing BlockDevice, FlashDevice provides the same API for flash
devices. At a hardware level reading is byte addressable in a similar way to
EEPROM and FRAM devices. These chips do not support writing arbitrary data to
individual byte addresses. Writing is done by erasing a block, then rewriting
it with new contents. To provide logical byte level writing it is necessary to
read and buffer the block containing the byte, update the byte, erase the block
and write out the buffer.
In practice this would be slow and inefficient - erasure is a slow process and
results in wear. The FlashDevice class defers writing the buffer until it is
necessary to buffer a different block.
The class caches a single sector. In currently supported devices this is 4KiB of RAM. This is adequate for littlefs, however under FATFS wear can be reduced by cacheing more than one sector. These drivers are primarily intended for littlefs with its wear levelling design.
Constructor args - mandatory, positional, integer
nbitsBlock size reported to the filesystem expressed as a number of bits: the block size is2^nbits. The usual value is 9 (512 bit block).nchipsNumber of chips in the array.chip_sizeSize of each chip in bytes.sec_sizePhysical sector size of the device in bytes.
A subclass supporting a flash device must provide the following methods:
readwrite(addr, buf, read)Args as defined in section 2.2. This calls the.reador.writemethods ofFlashDeviceas required.rdchip(addr, mvb)Argsaddr: address into the array,mvbamemoryviewinto a buffer for read data. This reads from the chip into thememoryview.flush(cache, addr)Argscachea buffer holding one sector of data,addraddress into the array of the start of a physical sector. Erase the sector and write out the data incache.
The constructor must call initialise() after the hardware has been
initialised to ensure valid cache contents.
read(addr, mvb) Argsaddraddress into array,mvbamemoryviewinto a buffer. Fills thememoryviewwith data read. If some or all of the data is cached, the cached data is provided.write(addr, mvb) Argsaddraddress into array,mvbamemoryviewinto a buffer. If the address range is cached, the cache contents are updated. More generally the currently cached data is written out usingflush, a new sector is cached, and the contents updated. Depending on the size of the data buffer this may occur multiple times.sync()This flushes the current cache. An optimisation is provided by the._dirtyflag. This ensures that the cache is only flushed if its contents have been modified since it was last written out.is_empty(addr, ev=0xff)Arg:addrstart address of a sector. Reads the sector returningTrueif all bytes matchev. Enables a subclass to avoid erasing a sector which is already empty.initialise()Called by the subclass constructor to populate the cache with the contents of sector 0.