openwch / arduino_core_ch32

Core library for CH32duino
243 stars 42 forks source link

Add support for chprog flash tool #109

Open KenjutsuGH opened 2 months ago

KenjutsuGH commented 2 months ago

I want to add support for the chprog flash tool: wagiminator / MCU-Flash-Tools

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:
    ## 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:
    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: 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:

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 I know my environment is set up correctly.

Running which python3 gives: /Library/Frameworks/Python.framework/Versions/3.12/bin/python3

The problem is that from Arduino, the following version of python3 is executed: /Applications/Xcode.app/Contents/Developer/usr/bin/python3

My question is, how do I get Arduino to invoke the version given by which python3, or how do I set up the Xcode version with the required Python libraries?

My setup:

Update: 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.

DeqingSun commented 2 months ago

For Arduino dependencies, even dynamic linked c libraries would create trouble for some users. Using Python is a bad idea.