How to invoke a python upload script correctly?

Anything not related to STM32
Post Reply
Kenjutsu
Posts: 31
Joined: Tue Apr 07, 2020 10:58 am
Answers: 2

How to invoke a python upload script correctly?

Post by Kenjutsu »

I started playing with some WeAct CH32X035 boards and came across wagiminator / MCU-Flash-Tools which contains Python flash scripts for various MCUs including stm32isp

I want to add support for the chprog flash tool to arduino_core_ch32. For testing purposes, I have done the following:

1. Download and copy chprog.py to: `/Users/ZZZ/Library/Arduino15/packages/WCH/tools/openocd/1.0.0/bin`
2. Add the following to platform.txt:

Code: Select all

    ## CHPROG
    tools.CHPROG.path={runtime.tools.openocd-1.0.0.path}/bin/
    tools.CHPROG.cmd=chprog.py
    tools.CHPROG.upload.params.verbose=
    tools.CHPROG.upload.params.quiet=
    tools.CHPROG.upload.pattern=python3 "{path}{cmd}" "{build.path}/{build.project_name}.bin"
    
3. Add the following to boards.txt:

Code: Select all

    CH32X035_EVT.menu.upload_method.chprogMethod=Wagiminator chprog
    CH32X035_EVT.menu.upload_method.chprogMethod.upload.protocol=chprog
    CH32X035_EVT.menu.upload_method.chprogMethod.upload.options=
    CH32X035_EVT.menu.upload_method.chprogMethod.upload.tool=CHPROG
    
I then select Wagiminator chprog as the upload method and hit upload. The correct upload command is generated:

Code: Select all

python3 /Users/ZZZ/Library/Arduino15/packages/WCH/tools/openocd/1.0.0/bin/chprog.py /var/folders/tq/dqrghm993td49tz1pvg8psxm0000gn/T/arduino_build_807950/Blink.ino.bin
But the command fails with:

Code: Select all

Traceback (most recent call last):
  File "/Users/ZZZ/Library/Arduino15/packages/WCH/tools/openocd/1.0.0/bin/chprog.py", line 45, in <module>
    import usb.core
ModuleNotFoundError: No module named 'usb'
When I run the generated upload command from the command line, it works as expected, thus it seems as if my environment is set up correctly.

Running which python3 gives:

Code: Select all

/Library/Frameworks/Python.framework/Versions/3.12/bin/python3
From Arduino, the following version of python3 is executed:

Code: Select all

/Applications/Xcode.app/Contents/Developer/usr/bin/python3
I hardcoded the path to the non-xcode python3 executable in platform.txt with the same error described above. I do not know enough about the python environment to fix this.

I downloaded pyinstaller and bundle chprog.py to a "One File" executable using:

Code: Select all

pyinstaller --onefile chprog.py
I then modified platform.txt to reference this executable, and it is working as expected.

I was hoping someone can help me understand what I need to do to get the original chprog.py python script working

My setup:
  • macOS Sonoma 14.5 M1
    arduino_core_ch32 1.0.4
    Arduino 1.8.19
Pieter

macOS: 14.5 M1
Arduino: 1.8.19
Arduino IDE: 2.3.2
STM32 Core: 2.8.0
Post Reply

Return to “Off topic”