diff --git a/bdevice.py b/bdevice.py index 5ea4bf0..748eb3d 100644 --- a/bdevice.py +++ b/bdevice.py @@ -63,7 +63,10 @@ class BlockDevice: return def readblocks(self, blocknum, buf, offset=0): - self.readwrite(offset + (blocknum << self._nbits), buf, True) + if blocknum < 0 or (offset + (blocknum << self._nbits)) > self._a_bytes: + print('rb', hex(blocknum), offset, hex(offset + (blocknum << self._nbits))) + else: + self.readwrite(offset + (blocknum << self._nbits), buf, True) def writeblocks(self, blocknum, buf, offset=0): self.readwrite(offset + (blocknum << self._nbits), buf, False) diff --git a/flash/FLASH.md b/flash/FLASH.md index 021d72b..04ec201 100644 --- a/flash/FLASH.md +++ b/flash/FLASH.md @@ -133,20 +133,26 @@ each chip select line a flash array is instantiated. A `RuntimeError` will be raised if a device is not detected on a CS line. The test has no effect on the array contents. -Arguments: - 1. `spi` Mandatory. An initialised SPI bus created by `machine`. +Arguments. In most cases only the first two mandatory args are required: + 1. `spi` An initialised SPI bus created by `machine`. 2. `cspins` A list or tuple of `Pin` instances. Each `Pin` must be initialised as an output (`Pin.OUT`) and with `value=1` and be created by `machine`. 3. `size=None` Chip size in KiB. The size is read from the chip. If a value is passed, the actual size is compared with the passed value: a mismatch will - raise a `ValueError`. Optionally set to 32768 for the S25FL256L chip or 16384 - for the S25FL128L. + raise a `ValueError`. A `ValueError` will also occur if chips in the array + have differing sizes. See table below for values. 4. `verbose=True` If `True`, the constructor issues information on the flash devices it has detected. 5. `sec_size=4096` Chip sector size. 6. `block_size=9` The block size reported to the filesystem. The size in bytes is `2**block_size` so is 512 bytes by default. +Size values: +| Chip | Size | +|:---------:|:-----:| +| S25FL256L | 32768 | +| S25FL128L | 16384 | + ### 4.1.2 Methods providing byte level access It is possible to read and write individual bytes or arrays of arbitrary size. diff --git a/flash/flash_spi.py b/flash/flash_spi.py index 1f8f3d0..599bc94 100644 --- a/flash/flash_spi.py +++ b/flash/flash_spi.py @@ -63,8 +63,8 @@ class FLASH(FlashDevice): cs(1) scansize = 1 << (mvp[3] - 10) if size is None: - size = scansize - if size != scansize: + size = scansize # Save size of 1st chip + if size != scansize: # Mismatch passed size or 1st chip. raise ValueError('Flash size mismatch: expected {}KiB, found {}KiB'.format(size, scansize)) if verbose: s = '{} chips detected. Total flash size {}MiB.' @@ -152,10 +152,16 @@ class FLASH(FlashDevice): # Given an address, set current chip select and address buffer. # Return the number of bytes that can be processed in the current chip. def _getaddr(self, addr, nbytes): +# print(hex(addr), hex(self._a_bytes), hex(self._c_bytes), self._nbits, self._block_size, divmod(addr, self._c_bytes)) if addr >= self._a_bytes: +# print(hex(addr), hex(self._a_bytes), hex(self._c_bytes), self._nbits, self._block_size, divmod(addr, self._c_bytes)) raise RuntimeError("Flash Address is out of range") ca, la = divmod(addr, self._c_bytes) # ca == chip no, la == offset into chip - self._ccs = self._cspins[ca] # Current chip select + try: + self._ccs = self._cspins[ca] # Current chip select + except: + print(ca, la) + raise cmdlen = self._cmdlen mvp = self._mvp[:cmdlen] if cmdlen > 3: