Merge pull request #523 from DiUS/dio-fix

Workaround write-protected flash when using esptool.py to flash in DIO mode
This commit is contained in:
Vowstar 2015-07-02 21:53:35 +08:00
commit cfe5b23b47
1 changed files with 38 additions and 32 deletions

View File

@ -128,38 +128,32 @@ class ESPROM:
def connect(self): def connect(self):
print 'Connecting...' print 'Connecting...'
# RTS = CH_PD (i.e reset) for _ in xrange(4):
# DTR = GPIO0 # issue reset-to-bootloader:
# self._port.setRTS(True) # RTS = either CH_PD or nRESET (both active low = chip in reset)
# self._port.setDTR(True) # DTR = GPIO0 (active low = boot to flasher)
# self._port.setRTS(False) self._port.setDTR(False)
# time.sleep(0.1) self._port.setRTS(True)
# self._port.setDTR(False) time.sleep(0.05)
self._port.setDTR(True)
self._port.setRTS(False)
time.sleep(0.05)
self._port.setDTR(False)
# NodeMCU devkit self._port.timeout = 0.3 # worst-case latency timer should be 255ms (probably <20ms)
self._port.setRTS(True) for _ in xrange(4):
self._port.setDTR(True) try:
time.sleep(0.1) self._port.flushInput()
self._port.setRTS(False) self._port.flushOutput()
self._port.setDTR(False) self.sync()
time.sleep(0.1) self._port.timeout = 5
self._port.setRTS(True) return
time.sleep(0.1) except:
self._port.setDTR(True) time.sleep(0.05)
self._port.setRTS(False) # this is a workaround for the CH340 serial driver on current versions of Linux,
time.sleep(0.3) # which seems to sometimes set the serial port up with wrong parameters
self._port.setDTR(True) self._port.close()
self._port.open()
self._port.timeout = 0.5
for i in xrange(10):
try:
self._port.flushInput()
self._port.flushOutput()
self.sync()
self._port.timeout = 5
return
except:
time.sleep(0.1)
raise Exception('Failed to connect') raise Exception('Failed to connect')
""" Read memory address in target """ """ Read memory address in target """
@ -268,6 +262,15 @@ class ESPROM:
return data return data
""" Abuse the loader protocol to force flash to be left in write mode """
def flash_unlock_dio(self):
# Enable flash write mode
self.flash_begin(0, 0)
# Reset the chip rather than call flash_finish(), which would have
# write protected the chip again (why oh why does it do that?!)
self.mem_begin(0,0,0,0x40100000)
self.mem_finish(0x40000080)
""" Perform a chip erase of SPI flash """ """ Perform a chip erase of SPI flash """
def flash_erase(self): def flash_erase(self):
# Trick ROM to initialize SFlash # Trick ROM to initialize SFlash
@ -566,7 +569,10 @@ if __name__ == '__main__':
seq += 1 seq += 1
print print
print '\nLeaving...' print '\nLeaving...'
esp.flash_finish(False) if args.flash_mode == 'dio':
esp.flash_unlock_dio()
else:
esp.flash_finish(False)
elif args.operation == 'run': elif args.operation == 'run':
esp.run() esp.run()