How to Build and Debug MicroPython Source Code with Eclipse

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
User avatar
ta1db
Posts: 52
Joined: Mon Sep 02, 2019 12:05 pm
Contact:

How to Build and Debug MicroPython Source Code with Eclipse

Post by ta1db » Tue May 19, 2020 8:39 am

!!! In Windows !!!
An Example for STM32F746-DISCO
Hi,

I am already building and debugging MP source code with traditional ways however the Eclipse IDE with broad plugin selections and capabilities is becoming a standard tool for embedded software development, I am doing all my C and embedded linux development works with Eclipse; it is very nice to see the project tree graphically and make drag and drop changes etc. good to work and debug in an integrated environment etc., it also provides an OS independent development environment (works in windows as well), so I prefer to make my micropython works in Eclipse if possible.

In this forum I found 4 previously started topics related to this subject:
- viewtopic.php?f=15&t=7280&p=41417&hilit ... 09d#p41417
- viewtopic.php?f=3&t=3487&p=20327&hilit= ... 09d#p20327
- viewtopic.php?f=3&t=3195&p=18795&hilit= ... 09d#p18795
- viewtopic.php?f=12&t=1584&p=9207&hilit= ... f09d#p9207

There are a few issues with questions asked on this subject in the Micropython Github repo as well.

However none of these discussions provide a complete procedure to successfully build and debug Micropython source code using Eclipse IDE; Some say "import", some say "create new", some say "show the makefile" but doesn't mention how.., lucky frineds say "it worked, ole" but not the how!

Could experienced members give us a hand please :roll:
Last edited by ta1db on Sun May 31, 2020 6:13 pm, edited 2 times in total.

stijn
Posts: 496
Joined: Thu Apr 24, 2014 9:13 am

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by stijn » Tue May 19, 2020 9:38 am

It's a bit hard to provide complete instructions since it's different per port. Which port do you want to build?

In general its as 'simple' as telling Eclipse to use an existing Makefile (according to internet, something like File->New project->C/C++->Makefile Project with Existing Code).

For the unix port that works out of the box, if I remember correctly (has been years since I did that), and if you tell Eclipse the output executable is ports/unix/micropython debugging with gdb also just works.

For microcontroller ports that's something else since you also need a step to deploy the firmware to the board, though the instructions in the 3rd link shown look pretty complete and correct, for STM32. Still I assume steps are similar for each port so you're right: would be good if someone could write that out and have it included in the documentation somewhere.

User avatar
ta1db
Posts: 52
Joined: Mon Sep 02, 2019 12:05 pm
Contact:

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by ta1db » Tue May 19, 2020 11:56 am

Hi @stijn,
Thanks for your kind response.
Which port do you want to build?
I work on stm32, esp8266 and esp32 ports however to start with stm32 is enough for the moment, then I can find my way for other ports.
In general its as 'simple' as telling Eclipse to use an existing Makefile (according to internet, something like File->New project->C/C++->Makefile Project with Existing Code).
Yes, it should be easy because all I have to do is to execute just a simple build command "make -C ports/stm32/ BOARD=STM32F7DISC" but I couldn't do it despite several trials. :oops:

Edit: Everybody agree on the project should be a Makefile Project.
[*]The first question is, do we create an Empty Makefile Project and import the micropython source code into it,
[*]or do we create directly a Makefile Project From Existing (micropython) Code. [*]Another option is to import directly the Existing (micropython) Code as Makefile Project but if I am not wrong the last two way is identical and produces the same project tree.
[*]Another option is to import the micropython source code direct from File System as it is.
As you see there is a 4 way separation just in the beginning even if we agree on the project will be a Makefile Project.

I choose File -> New -> Makefile Project with Existing Code. Now I have to point Existing Code Location and the 2nd question arises: should I chose microphython root folder where the README.md exist or ports/stm32 (my case) ? I guess I have to choose micropython root folder as Existing Code Location because otherwise many source code stay outside of the project. I choose ARM Cross GCC as the Toolchain for Indexer Settings as well. -> Finish, project tree appears in the Project Explorer window..

Now I create a build target with the name of my target as BOARD=STM32F7DISC and change Build command from default to "make -C ports/stm32 V=1" , I do right-click the new build target and choose Build, I get the following:

10:51:53 **** Build of configuration Default for project micropython ****
make -C ports/stm32 V=1 BOARD=STM32F7DISC
make: Entering directory 'G:/microPython/project/micropython/ports/stm32'
GEN build-STM32F7DISC/genhdr/pins.h
python3 boards/make-pins.py --board boards/STM32F7DISC/pins.csv --af boards/stm32f746_af.csv \
--prefix boards/stm32f4xx_prefix.c --hdr build-STM32F7DISC/genhdr/pins.h --hdr-obj-decls \
--qstr build-STM32F7DISC/pins_qstr.h --af-const build-STM32F7DISC/genhdr/pins_af_const.h \
--af-defs build-STM32F7DISC/genhdr/pins_af_defs.h --af-defs-cmp-strings \
--af-py build-STM32F7DISC/pins_af.py > build-STM32F7DISC/pins_STM32F7DISC.c
c:/users/murat/appdata/roaming/gnu mcu eclipse/build tools/2.12-20190422-1053/bin/sh: python3: not found
make: *** [Makefile:666: build-STM32F7DISC/genhdr/pins.h] Error 127
make: *** [build-STM32F7DISC/genhdr/pins.h] Deleting file 'build-STM32F7DISC/pins_STM32F7DISC.c'
make: Leaving directory 'G:/microPython/project/micropython/ports/stm32'
"make -C ports/stm32 V=1 BOARD=STM32F7DISC" terminated with exit code 2. Build might be incomplete.
10:52:05 Build Failed. 2 errors, 0 warnings. (took 12s.636ms)

I feel that I am on the right way but I am doing something wrong or incomplete.

Edit2: I am running Eclipse in windows on purpose to build micropython in windows, I think invoking python as python3 cause problem, I am working on it.
Last edited by ta1db on Thu May 21, 2020 8:23 am, edited 4 times in total.

User avatar
jimmo
Posts: 1911
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by jimmo » Wed May 20, 2020 6:41 am

ta1db wrote:
Tue May 19, 2020 11:56 am
Yes, it should be easy because all I have to do is to execute just a simple build command "make -C ports/stm32/ BOARD=STM32F7DISC" but I couldn't do it despite several trials.
Sorry I don't know much about Eclipse, but it would be helpful to know where this is going wrong? i.e. is it a problem with telling it to run this command, or the command doesn't run at all, or it runs with error?

User avatar
ta1db
Posts: 52
Joined: Mon Sep 02, 2019 12:05 pm
Contact:

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by ta1db » Wed May 20, 2020 8:02 am

jimmo wrote:
Wed May 20, 2020 6:41 am
it would be helpful to know where this is going wrong? i.e. is it a problem with telling it to run this command, or the command doesn't run at all, or it runs with error?
Hi @jimmo, thanks for your kind response, please have look at my edit above.

stijn
Posts: 496
Joined: Thu Apr 24, 2014 9:13 am

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by stijn » Wed May 20, 2020 1:07 pm

ta1db wrote:
Tue May 19, 2020 11:56 am
10:51:53 **** Build of configuration Default for project micropython ****
make -C ports/stm32 V=1 BOARD=STM32F7DISC
make: Entering directory 'G:/microPython/project/micropython/ports/stm32'
GEN build-STM32F7DISC/genhdr/pins.h
python3 boards/make-pins.py --board boards/STM32F7DISC/pins.csv --af boards/stm32f746_af.csv \
--prefix boards/stm32f4xx_prefix.c --hdr build-STM32F7DISC/genhdr/pins.h --hdr-obj-decls \
--qstr build-STM32F7DISC/pins_qstr.h --af-const build-STM32F7DISC/genhdr/pins_af_const.h \
--af-defs build-STM32F7DISC/genhdr/pins_af_defs.h --af-defs-cmp-strings \
--af-py build-STM32F7DISC/pins_af.py > build-STM32F7DISC/pins_STM32F7DISC.c
c:/users/murat/appdata/roaming/gnu mcu eclipse/build tools/2.12-20190422-1053/bin/sh: python3: not found

I feel that I am on the right way but I am doing something wrong or incomplete.
Indeed you're on the right track. All of this looks fine: the only problem (for now) is that python3 isn't found. Which you can resolve by adding PYTHON=/path/to/python3 to the make command line.

User avatar
ta1db
Posts: 52
Joined: Mon Sep 02, 2019 12:05 pm
Contact:

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by ta1db » Wed May 20, 2020 7:01 pm

Hi @stijn
Thanks for your support.
The problem was micropython build system is invoking python version 3 as "python3" however this was not supported on my windows installation, I didn't want to change micropython source code so I made a symbolic (soft) link with

Code: Select all

c:\windows\system32> mklink python3.exe C:\path to python 3 dir\python.exe
now the build works. debugging next :)

Edit
Build started working but not finished :) It could finish with these: viewtopic.php?f=15&t=8387&p=47990#p47990
Also @stijn is correct here viewtopic.php?f=15&t=8387#p47673 , making a symlink is just a temporary solution and @chrismas 's advise here viewtopic.php?f=15&t=8387#p47944 can be a good alternative
Last edited by ta1db on Sat May 30, 2020 7:45 am, edited 2 times in total.

stijn
Posts: 496
Joined: Thu Apr 24, 2014 9:13 am

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by stijn » Wed May 20, 2020 7:36 pm

You don't have to change any source code, as shown you can specify the path to the python executable on the make commandline. And I'd really advise to do that, or even any other solution (like adding Python directory to PATH), as long as it's not putting things in system32. Unless you are really lucky that's going to cause problems sooner or later.

chrismas9
Posts: 149
Joined: Wed Jun 25, 2014 10:07 am

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by chrismas9 » Thu May 28, 2020 12:02 pm

If you install Python from the Windows Store instead of from a download it installs correctly as Python3.

User avatar
ta1db
Posts: 52
Joined: Mon Sep 02, 2019 12:05 pm
Contact:

Re: How to Build and Debug MicroPython Source Code with Eclipse

Post by ta1db » Sat May 30, 2020 7:30 am

-- continue --
Following descriptions are all quoted from our private messaging with @stijn, without his help I wouldn't resolve the difficulties
Mpy-Cross
mpy-cross is the micropython cross compiler utility, used to pre-compile python files into bytecode suitable for running on your target. In order to build micropython source code for ports/ we have to have a mpy-cross executable, mpy-cross.exe for windows, in micropython/mpy-cross directory.

mpy-cross needs to be built using 'system' gcc, or msvc, or acquired by installing with pip from https://pypi.org/project/mpy-cross/ or downloaded (currently Andrew Leech maintains the PyPi package using his builds from https://gitlab.com/alelec/mpy_cross/pipelines so I assume mpy-cross can be downloadd there somewhere, at https://ci.appveyor.com/project/stinos/ ... /artifacts you can dwonload it for now).

If you try to build mpy-cross with Eclipse and get errors you may consider these suggestions as well:

Eclipse tries to build using 'system gcc' i.e. a gcc which runs on Windows and ( if ) doesn't find it. perhaps it does use msys2 under the hood? If so you change the make commandline like `make CC=/path/to/gcc'. If not you'd have to install msys2 (well, if Eclipse doesn't have it) and within msys2 install mingw-w64 and pkg-config. I have something like this in my configuration scripts: in msys2 shell run "pacman -S --noconfirm make mingw-w64-x86_64-gcc pkg-config"

I simply downloaded 64 bit version (you may need 32 bit version) of mpy-cross.exe for my system from here https://ci.appveyor.com/project/stinos/ ... /artifacts and put into the mpy-cross/ folder.

stm32 Makefile LIBS path space issue
Check if LIBS in line 127 of stm32 Makefile is quoted as below, quote it if not. This revision should be pulled into the micropython sourcefile here https://github.com/micropython/micropython/pull/6092

Code: Select all

LIBS = "$(shell $(CC) $(CFLAGS) -print-libgcc-file-name)"
Debugging
with the help of @flindt 's post here : viewtopic.php?f=3&t=3195&p=18795&hilit= ... 09d#p20328

Create an externel tool configuration
Run -> External Tools -> External Tools Configurations -> Program -> New Launch Configuration
Name: st-util
Main / Location: Path-to\ST-LINK_gdbserver.exe
Main / Working Directory: ${workspace_loc:/micropython/ports/stm32/build-STM32F7DISC}
Main / Arguments: -cp "C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin" -d
Common / Display in favorites menu: Check External Tools selection

NOTE: Leave all other selections in their default states
Ensure that Common / Launch in background is "check"

Run the created configuration
You have to see something like this on the Console:

Code: Select all

STMicroelectronics ST-LINK GDB server. Version 5.4.0
Copyright (c) 2019, STMicroelectronics. All rights reserved.

Starting server with the following options:
        Persistent Mode            : Disabled
        Logging Level              : 31
        Listen Port Number         : 61234
        Status Refresh Delay       : 15s
        Verbose Mode               : Disabled
        SWD Debug                  : Enabled

Target connection mode: Default
Target connection failed. Try connecting under reset
Hardware watchpoint supported by the target 
COM frequency = 4000 kHz
ST-LINK Firmware version : V2J35M26
Device ID: 0x449
PC: 0x804f4bc
ST-LINK device status: HALT_MODE
ST-LINK detects target voltage = 3.22 V
ST-LINK device status: HALT_MODE
ST-LINK device initialization OK
Waiting for debugger connection...
Waiting for connection on port 61234...

Create a Debug Configuration
Run -> Debug Configurations -> GDB Hardware Debugging -> New Launch Configuration
Name: st-debug
Main / Project: micropython
Main / C/C++ Application: ports/stm32/build-STM32F7DISC/firmware.elf
Debugger / Remote Target -> Ensure Use remote target check,
-> Select JTAG Device: ST-LINK GDB server
-> ensure IP address is "localhost" port number fits to the "Waiting for connection on port 61234..." above
Startup / Load Image and Symbols:
-> Load Image / Use file :${workspace_loc:\micropython\ports\stm32\build-STM32F7DISC\firmware.elf}
-> Load symbols / Use file: ${workspace_loc:\micropython\ports\stm32\build-STM32F7DISC\firmware.elf}
Run Commands / Set breakpoint at: enter "main" and check
Run Commands / Resume : Check

Debug
Debugging should continue with the following on the Console:

Code: Select all

Accepted connection on port 61234...
Debugger connected
 ------ Switching to STM32CubeProgrammer ----- 
      -------------------------------------------------------------------
                       STM32CubeProgrammer v2.2.0                  
      -------------------------------------------------------------------

ST-LINK SN  : 0670FF485057775187195323
ST-LINK FW  : V2J35M26
Voltage     : 3.23V
SWD freq    : 4000 KHz
Connect mode: Under Reset
Reset mode  : Hardware reset
Device ID   : 0x449
Device name : STM32F74x/STM32F75x
Flash size  : 1 MBytes
Device type : MCU
Device CPU  : Cortex-M7



Memory Programming ...
Opening and parsing file: ST-LINK_GDB_server_a07392.srec
  File          : ST-LINK_GDB_server_a07392.srec
  Size          : 457032 Bytes
  Address       : 0x08000000 


Erasing memory corresponding to segment 0:
Erasing internal memory sector 0
Erasing memory corresponding to segment 1:
Erasing internal memory sectors [4 6]
Download in Progress:


File download complete
Time elapsed during download operation: 00:00:08.766
 ------ Switching context ----- 
Target connection mode: Default
Hardware watchpoint supported by the target 
COM frequency = 4000 kHz
ST-LINK Firmware version : V2J35M26
Device ID: 0x449
PC: 0x804f4bc
ST-LINK detects target voltage = 3.22 V
ST-LINK device status: HALT_MODE
ST-LINK device initialization OK
TraceCaptureStart and SWV event set to APP_TRUE 
ST-LINK device status: RUN_MODE

Post Reply