Skip to content

Commit

Permalink
Remove floats from block calculations, fix regression in 0e67846 espr…
Browse files Browse the repository at this point in the history
…essif#53

Float argument strayed into flash_begin() size, caused erase sizes to be
calculated as too long. This triggered an error when writing near the end of
the flash, it would ask for erasing beyond the end of available space.

Closes espressif#60
  • Loading branch information
projectgus committed Sep 2, 2015
1 parent d0d6c5f commit 582a7ca
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions esptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ def load_section(self, section):
def arg_auto_int(x):
return int(x, 0)

def div_roundup(a, b):
""" Return a/b rounded up to nearest integer,
equivalent result to int(math.ceil(float(int(a)) / float(int(b))), only
without possible floating point accuracy errors.
"""
return (int(a) + int(b) - 1) / int(b)

if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'ESP8266 ROM Bootloader Utility', prog = 'esptool')

Expand Down Expand Up @@ -532,7 +539,7 @@ def arg_auto_int(x):
for (offset, size, data) in image.segments:
print 'Downloading %d bytes at %08x...' % (size, offset),
sys.stdout.flush()
esp.mem_begin(size, math.ceil(size / float(esp.ESP_RAM_BLOCK)), esp.ESP_RAM_BLOCK, offset)
esp.mem_begin(size, div_roundup(size, esp.ESP_RAM_BLOCK), esp.ESP_RAM_BLOCK, offset)

seq = 0
while len(data) > 0:
Expand Down Expand Up @@ -575,7 +582,7 @@ def arg_auto_int(x):
args.addr_filename = args.addr_filename[2:]
image = file(filename, 'rb').read()
print 'Erasing flash...'
blocks = math.ceil(len(image)/float(esp.ESP_FLASH_BLOCK))
blocks = div_roundup(len(image), esp.ESP_FLASH_BLOCK)
esp.flash_begin(blocks*esp.ESP_FLASH_BLOCK, address)
seq = 0
while len(image) > 0:
Expand Down Expand Up @@ -658,7 +665,7 @@ def arg_auto_int(x):

elif args.operation == 'read_flash':
print 'Please wait...'
file(args.filename, 'wb').write(esp.flash_read(args.address, 1024, int(math.ceil(args.size / 1024.)))[:args.size])
file(args.filename, 'wb').write(esp.flash_read(args.address, 1024, div_roundup(args.size, 1024))[:args.size])

elif args.operation == 'erase_flash':
esp.flash_erase()

0 comments on commit 582a7ca

Please sign in to comment.