scientifichackers / ampy

MicroPython Tool - Utility to interact with a MicroPython board over a serial connection.
MIT License
718 stars 156 forks source link

can't establish a connection anylonger after upload of main.py #66

Open jdeut opened 5 years ago

jdeut commented 5 years ago

On my system runs: ampy --versionampy, version 1.0.7 Used board: wemos lolin d32

Steps to reproduce the problem:

  1. Erase the flash with esptool.py --chip esp32 erase_flash
  2. Flash Micropython to the board with esptool.py --baud 600000 --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 esp32-20181024-v1.9.4-674-g27ca9ab8b.bin
  3. Put main.py on the board with ampy -p /dev/ttyUSB0 put main.py
  4. Run main.py with ampy -p /dev/ttyUSB0 run main.py (Execution of main.py starts without any issues -> LEDs start blinking)
  5. Try to put main.py on the board again with ampy -p /dev/ttyUSB0 put main.py

After step 5 ampy gives the following error:

Traceback (most recent call last):
  File "/home/johannes/.local/bin/ampy", line 11, in <module>
    sys.exit(cli())
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/cli.py", line 263, in put
    board_files.put(remote, infile.read())
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/files.py", line 209, in put
    self._pyboard.exec_("f = open('{0}', 'wb')".format(filename))
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/pyboard.py", line 265, in exec_
    ret, ret_err = self.exec_raw(command)
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/pyboard.py", line 256, in exec_raw
    self.exec_raw_no_follow(command);
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/pyboard.py", line 253, in exec_raw_no_follow
    raise PyboardError('could not exec command')
ampy.pyboard.PyboardError: could not exec command

rshell exists with a similar error (see https://github.com/dhylands/rshell/issues/27#issuecomment-433260583).

After that if i erase the flash and flash the micropython firmware again everything works as expected until i uploaded main.py of my blinking example. Maybe it's also woth to mention that i can access the Micropython repl with picocom without any problems.

At the same time mpfshell works without complaining. Here's a quick sample session i started right after invoking ampy:

johannes:~/iot/esp/Micropython/MyExamples/TimerTest % mpfshell -c "open ttyUSB0"
Connected to esp32

** Micropython File Shell v0.8.1, sw@kaltpost.de **
-- Running on Python 2.7 using PySerial 3.4 --

mpfs [/]> cat boot.py
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
#import webrepl
#webrepl.start()

mpfs [/]> cat main.py
import machine
from machine import Pin
from machine import Timer
import micropython

micropython.alloc_emergency_exception_buf(256)

print("alloc_emergency_exception_buf done")

io23 = Pin(23, Pin.OUT)
io05 = Pin(5, Pin.OPEN_DRAIN)

io23.value(1)
io05.value(0)

timer1 = Timer(-1)
timer1.init(mode=Timer.PERIODIC, period=100, callback=lambda t:io23.value(not io23.value()))

timer2 = Timer(-2)
timer2.init(mode=Timer.PERIODIC, period=100, callback=lambda t:io05.value(not io05.value()))

print("Let's start blinking")

mpfs [/]>
dhalbert commented 5 years ago

That's an unusual main.py, because it exits instead of looping forever. Do you have the same problem with a while True: pass type of main.py?

Given that there are two shell programs that don't work, and one that does, it would be worth looking at what's actually being sent. Also, try a --delay option of a few seconds in ampy and see if that helps.

jdeut commented 5 years ago

Finally i found some time to test your case.

If i touch a new main.py with the content

while True: pass

everything works as expected.

However if i put

import machine
from machine import Pin
from machine import Timer
import micropython

micropython.alloc_emergency_exception_buf(256)

print("alloc_emergency_exception_buf done")

io23 = Pin(23, Pin.OUT)
io05 = Pin(5, Pin.OPEN_DRAIN)

io23.value(1)
io05.value(0)

timer1 = Timer(-1)
timer1.init(mode=Timer.PERIODIC, period=10, callback=lambda t:io23.value(not io23.value()))

timer2 = Timer(-2)
timer2.init(mode=Timer.PERIODIC, period=500, callback=lambda t:io05.value(not io05.value()))

print("Let's start blinking")

while True: pass

in my main.py (my original file), ampy works very unreliable. If i execute ampy -p /dev/ttyUSB0 put main.py successively, ampy fails 2 times out of 3 with the following error message

Traceback (most recent call last):
  File "/home/johannes/.local/bin/ampy", line 11, in <module>
    sys.exit(cli())
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/johannes/.local/lib/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/cli.py", line 263, in put
    board_files.put(remote, infile.read())
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/files.py", line 209, in put
    self._pyboard.exec_("f = open('{0}', 'wb')".format(filename))
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/pyboard.py", line 265, in exec_
    ret, ret_err = self.exec_raw(command)
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/pyboard.py", line 256, in exec_raw
    self.exec_raw_no_follow(command);
  File "/home/johannes/.local/lib/python3.6/site-packages/ampy/pyboard.py", line 253, in exec_raw_no_follow
    raise PyboardError('could not exec command')
ampy.pyboard.PyboardError: could not exec command

Again, if i put main.py with mpfshell -n -c "open ttyUSB1; put main.py" on the board, everything works.

As a next step i will try to compare the code of mpfshell with ampy. I hope i find some time for this soon.

rrottmann commented 5 years ago

@nichtleiter: Did you check the delay option as suggested? Got the same error message after adding wifi connection to the code. Setting the delay fixed ampy in my case.

-d, --delay DELAY Delay in seconds before entering RAW MODE (default 0). Can optionally specify with AMPY_DELAY environment variable.

ladyada commented 5 years ago

Hiya! We are discontinuing support for ampy, and will no longer be maintaining it. We are leaving this repository available for continued use. If you would like to take over supporting it, please contact us on the Adafruit Discord server and we can transfer the repository to you. If you wish to continue developing it on your own, please fork the repository.