Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions programmer/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ __pycache__/
*~
tinyprog/full_version.py

# due to pip install . (for development)
# https://github.com/pypa/pip/issues/6213
pip-wheel-metadata

# due to using tox and pytest
.tox
.coverage
Expand Down
29 changes: 28 additions & 1 deletion programmer/tinyprog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,20 @@ def __str__(self):
return self.port_name

def __enter__(self):
# Timeouts:
# - Read: 2.0 seconds (timeout)
# - Write: 5.0 seconds (writeTimeout)
#
# Rationale: hitting the writeTimeout is fatal, so it pays to be
# patient in case there is a brief delay; readTimeout is less
# fatal, but can result in short reads if it is hit, so we want
# a timeout high enough that is never hit normally. In practice
# 1.0 seconds is *usually* enough, so the chosen values are double
# and five times the "usually enough" values.
#
try:
self.ser = serial.Serial(
self.port_name, timeout=1.0, writeTimeout=1.0).__enter__()
self.port_name, timeout=2.0, writeTimeout=5.0).__enter__()
except serial.SerialException as e:
raise PortError("Failed to open serial port:\n%s" % str(e))

Expand Down Expand Up @@ -499,6 +510,22 @@ def program_sectors(self, addr, data):
for minor_offset in range(0, 4 * 1024, minor_sector_size):
minor_write_data = current_write_data[
minor_offset:minor_offset + minor_sector_size]

# The TinyFPGA firmware and/or flash chip does not handle
# partial minor sector writes properly, so pad out short
# writes to a whole minor sector. (This is safe because
# we explicitly erased the whole sector above, so the
# following bytes must be freshly erased. Usually it will
# only happen on the final minor sector to be written.)
#
if (len(minor_write_data) < minor_sector_size):
pad_len = minor_sector_size - len(minor_write_data)
padding = b'\xff' * pad_len

minor_write_data = bytearray(minor_write_data)
minor_write_data.extend(padding)
assert(len(minor_write_data) == minor_sector_size)

self.write(
current_addr + minor_offset,
minor_write_data,
Expand Down