Skip to content

Commit 0b573f3

Browse files
committed
tinyprog: limit trailing padding to aligned writes
To avoid wrap around when writing beyond the 256 octet sector, if the write starts part way into the 256 octet sector, only do the padding to a full 256 octet sector if we are writing from the begining of the sector (and thus writing a full 256 octets will not wrap around). We also write 0xff for safety, as in most SPI flash that is the erased value, and effectively one can only write 0 bits, so writing twice to a cell (without erasing) writes (existing value AND new value), which for a new value of 0xff should be the existing value.
1 parent 959ab55 commit 0b573f3

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

programmer/tinyprog/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,20 @@ def program_sectors(self, addr, data):
512512
minor_offset:minor_offset + minor_sector_size]
513513

514514
# The TinyFPGA firmware and/or flash chip does not handle
515-
# partial minor sector writes properly, so pad out short
516-
# writes to a whole minor sector. (This is safe because
517-
# we explicitly erased the whole sector above, so the
518-
# following bytes must be freshly erased. Usually it will
519-
# only happen on the final minor sector to be written.)
515+
# partial minor sector writes properly, so pad out a final
516+
# write of a partial to a whole minor sector, if we are
517+
# writing aligned to the SPI flash internal minor sectors.
520518
#
521-
if (len(minor_write_data) < minor_sector_size):
519+
# Due to the way SPI flash works, writing 0xff *without
520+
# erasing* should be a no-opt, because 0xff is what you
521+
# get after erasing, and you can only write 0 bits.
522+
#
523+
current_minor_addr = current_addr + minor_offset
524+
525+
if (((current_minor_addr % minor_sector_size) == 0) and
526+
(len(minor_write_data) < minor_sector_size)):
527+
assert((current_minor_addr % minor_sector_size) == 0)
528+
522529
pad_len = minor_sector_size - len(minor_write_data)
523530
padding = b'\xff' * pad_len
524531

0 commit comments

Comments
 (0)