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
uos
module. 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
AbstractBlockDev
protocol 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
len
operator.
Constructor args - mandatory, positional, integer
nbits
Block size reported to the filesystem expressed as a number of bits: the block size is2^nbits
. The usual value is 9 (512 bit block).nchips
Number of chips in the array.chip_size
Size of each chip in bytes.
The subclass must provide a method readwrite
taking the following args:
addr
Address relative to the start of the array.buf
A buffer holding data to write or to contain data to be read.read
Boolean:True
to read,False
to 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 theBlockDevice
class this does nothing. It is defined in theFlashDevice
class 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=0
Works as above. -
ioctl
This supports the following operands: -
sync
Calls the.sync()
method. -
sector count
Returnschip_size
*nchips
//block_size
-
block size
Returns block size calculated as in section 2.1. -
erase
Necessary 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 bytes
The 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
nbits
Block size reported to the filesystem expressed as a number of bits: the block size is2^nbits
. The usual value is 9 (512 bit block).nchips
Number of chips in the array.chip_size
Size of each chip in bytes.sec_size
Physical 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.read
or.write
methods ofFlashDevice
as required.rdchip(addr, mvb)
Argsaddr
: address into the array,mvb
amemoryview
into a buffer for read data. This reads from the chip into thememoryview
.flush(cache, addr)
Argscache
a buffer holding one sector of data,addr
address 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
) Argsaddr
address into array,mvb
amemoryview
into a buffer. Fills thememoryview
with data read. If some or all of the data is cached, the cached data is provided.write(addr, mvb
) Argsaddr
address into array,mvb
amemoryview
into 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._dirty
flag. 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:addr
start address of a sector. Reads the sector returningTrue
if 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.