MicroPython on Nucleo-F401RE, 2. How to transfer .py file from PC to the board

前一篇貼文提到pyboard接到PC的USB後, 在電腦上會出現一個硬碟, 傳送.py檔案到pyboard就是很容易的事情, 用拖的或下指令的, 只要把檔案放到這佪硬碟就好了。但對Nucleo-F401RE開發板來說, 就不是這樣的!

HardyTek也有一篇貼文在討論這件事: http://hardytek.com/writing-files-to-nucleo-pyb-flash/, 他的建議是使用ampy這個由Adafruit提供的工具。我之前玩ESP8266 MicroPython時也是常用ampy的。這個工具簡單可用, 但有一些小問題, 在某些平台或USB-Serial轉卡晶片, 使用時會出現一些狀況, 例如一些時間差, 需要加入 -d 0.5的參數來解決。另外這個工具沒有shell或REPL的功能, 所以會是常常在兩個不同工具之間切換的工作情況, 又因為Serial port是獨佔的, 要使用ampy就必須先將REPL那邊的Serial port關掉! 也是很令人困擾!

這次我嘗試使用Windows平台上的ampy, 來傳送.py檔案給Nucleo-F401RE上的MicroPython:
ampy -p COM10 ls /flash
可以看到板子上的檔案有那些
ampy -p COM10 get xx.py
可以看到板子上某個檔案的內容
ampy -p COM10 put xx.py
用來將檔案傳送至板子上檔案系統, 結果出現:

Traceback (most recent call last):
  File "c:\python27\lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Scripts\ampy.exe\__main__.py", line 9, in
  File "c:\python27\lib\site-packages\click\core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "c:\python27\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "c:\python27\lib\site-packages\click\core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\python27\lib\site-packages\click\core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\python27\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:\python27\lib\site-packages\ampy\cli.py", line 263, in put
    board_files.put(remote, infile.read())
  File "c:\python27\lib\site-packages\ampy\files.py", line 218, in put
    self._pyboard.exec_("f.write({0})".format(chunk))
  File "c:\python27\lib\site-packages\ampy\pyboard.py", line 265, in exec_
    ret, ret_err = self.exec_raw(command)
  File "c:\python27\lib\site-packages\ampy\pyboard.py", line 256, in exec_raw
    self.exec_raw_no_follow(command);
  File "c:\python27\lib\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

試了幾次, 都是這樣, 之後我去看, 其實有傳送檔案上去, 但並不完整, 有時候傳了10行, 有時候傳了5行!

失望之餘, 就想到用其他工具試試! 首先想到之前用mpfshell的經驗還不錯, 就去看一下https://github.com/wendlers/mpfshell, 結果這個工具主要是針對ESP8266和WiPy的板子的。那就換rshell看看好了:

C:\> pip3 install rshell
這樣就安裝好rshell了

https://github.com/dhylands/rshell是rshell的GitHub repo, 有使用說明,
也可以下指令查使用說明, 如下:
C:\>rshell --help
usage: rshell [options] [command]

Remote Shell for a MicroPython board.

positional arguments:
  cmd                   Optional command to execute

optional arguments:
  -h, --help            show this help message and exit
  -b BAUD, --baud BAUD  Set the baudrate used (default = 115200)
  --buffer-size BUFFER_SIZE
                        Set the buffer size used for transfers (default = 512
                        for USB, 32 for UART)
  -p PORT, --port PORT  Set the serial port to use (default 'None')
  --rts RTS             Set the RTS state (default '')
  --dtr DTR             Set the DTR state (default '')
  -u USER, --user USER  Set username to use (default 'micro')
  -w PASSWORD, --password PASSWORD
                        Set password to use (default 'python')
  -e EDITOR, --editor EDITOR
                        Set the editor to use (default 'vi')
  -f FILENAME, --file FILENAME
                        Specifies a file of commands to process.
  -d, --debug           Enable debug features
  -n, --nocolor         Turn off colorized output
  -l, --list            Display serial ports
  -a, --ascii           ASCII encode binary files for transfer
  --wait WAIT           Seconds to wait for serial port
  --timing              Print timing information about each command
  -V, --version         Reports the version and exits.
  --quiet               Turns off some output (useful for testing)

You can specify the default serial port using the RSHELL_PORT environment
variable.

我的使用方式:
C:\> rshell
Welcome to rshell. Use the exit command to exit rshell.

No MicroPython boards connected - use the connect command to add one

C:\> connect serial COM10
Connecting to COM10 (buffer-size 512)...
Trying to connect to REPL  connected
Testing if sys.stdin.buffer exists ... Y
Retrieving root directories ... /flash/
Setting time ... Sep 05, 2019 17:28:07
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 2000
C:\> ls
ssd1306.py   my.py 
C:\> cp ssd1306.py /flash
C:\> ls -l /flash
   238 Jan  1 2015  boot.py
    34 Jan  1 2015  main.py
  1396 Jan  1 2015  my.py
  4681 Sep  5 17:29 ssd1306.py
C:\> cat /flash/ssd1306.py
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces

from micropython import const
import framebuf


# register definitions
SET_CONTRAST        = const(0x81)
SET_ENTIRE_ON       = const(0xa4)
SET_NORM_INV        = const(0xa6)
... (略)

C:\> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.11-274-g06661890d on 2019-09-05; NUCLEO-F401RE with STM32F401xE
Type "help()" for more information.
>>>
>>>import my
>>> my.ls()
PWD: /flash, available size: 37.5KB

 -      238      boot.py
 -       34      main.py
 -     1396      my.py
 -     4681      ssd1306.py

可以看到只要下rshell指令, 進入後下connect serial COMxx就可以連到開發板的檔案系統。然後ls指令是看到本地端電腦的目前目錄, 要看開發板端的則下ls /flash就可以了。複製檔案則是cp指令, 看檔案內容則是cat指令, 就和一般的linux檔案系統指令是一樣的。另外, 下repl指令就會進入MicroPython的REPL操作界面。當然在這裡就可以使用python語法和MicroPython交談了, 要離開按Ctrl-X。
不管用起來好不好用, 但至少傳送檔案是很順暢的。

********************************************
我在這裡有用到自己寫的一支小程式my.py, 可以在不離開REPL的情況下, 做些簡單的檔案操作, 這樣就不用按Ctrl-X離開, 然後再回來! 離開再回來應該之前下的一些指令又要重下吧! 下次來分享這支小程式給大家.
另外也要談談GPIO在這張板子上的對應方式, 目前看來和ESP8266是有些不同的!




留言

這個網誌中的熱門文章

D-BUS學習筆記

關於藍牙裝置找尋(inquiry, scan)兩三事

Cisco Switch學習筆記: EtherChannel