Skip to content

Commit

Permalink
untested: NUC1XX writeBinToFlash with proper flash per-page erase
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Heistermann committed Jan 6, 2012
1 parent c58959c commit 51329af
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions NUC1XX.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class NUC1XX(object):
LDROM_SIZE = 0x1000

USERCONFIG_START = 0x00300000

# pg 482:
ISPCMD_PAGE_ERASE = 0x22
ISPCMD_PROGRAM = 0x21
Expand All @@ -60,6 +61,8 @@ class NUC1XX(object):
ISPCON_ISPFF = 0x40
ISPCON_BS = 0x02

FLASH_PAGESIZE = 0x200

def __init__(self, debugport):
self.ahb = SWDCommon.MEM_AP(debugport, 0)

Expand Down Expand Up @@ -137,7 +140,6 @@ def readFlash(self, start_addr, length):
print 'reading 0x%x: 0x%x' % (addr, data)

def eraseFlash(self, addr):
#assert (addr & 0x1ff) == 0, "not the beginning of a flash page"
self.issueISPCommand(addr, NUC1XX.ISPCMD_PAGE_ERASE, 0x00)

def readRegister(self, register):
Expand Down Expand Up @@ -166,15 +168,22 @@ def readConfig(self):
print 'readConfig: %s' % hex(
self.ahb.readWord(NUC1XX.CONFIG0_ADDR))

def writeBinToFlash(self, binstr):
start_addr = NUC1XX.LDROM_START_ADDR
def writeBinToFlash(self, binstr, start_addr = LDROM_START_ADDR):
assert start_addr % NUC1XX.FLASH_PAGESIZE == 0
# TODO: maybe we should only allow multiples of FLASH_PAGESIZE,
# so we won't erase data between the end of the data and
# the end of the flash page. Could possibly implement partial
# flash page updates somewhere else.

if len(binstr) % 4 != 0:
raise FlashDataInvalid('Flash is not valid / divisible by 4')

print 'length: %i' % len(binstr)
for counter in range(0, len(binstr), 4):
addr = start_addr + counter
packed_data = binstr[counter:counter + 4]
for offset in range(0, len(binstr), 4):
addr = start_addr + offset
if addr % NUC1XX.FLASH_PAGESIZE == 0: # reached new page
self.eraseFlash(addr)

packed_data = binstr[offset:offset + 4]
data = struct.unpack(">I", packed_data)[0] # TODO: maybe <I
self.eraseFlash(addr)

self.writeFlash(addr, data)

0 comments on commit 51329af

Please sign in to comment.