jbaumann / pio_and_stm32cubeide

Use PlatformIO with Projects generated by STM32CubeIDE
Apache License 2.0
21 stars 9 forks source link

Script fails with custom board #10

Closed sidprice closed 4 months ago

sidprice commented 7 months ago

My project uses a custom board:

{ "build": { "core": "stm32", "cpu": "cortex-m4", "extra_flags": "-DSTM32G4xx -DSTM32G431xx", "f_cpu": "170000000L", "mcu": "stm32g431cbt6", "product_line": "STM32G431xx", "variant": "STM32G4xx/G431R(6-8-B)(I-T)_G441RB(I-T)" }, "connectivity": [ "can" ], "debug": { "default_tools": [ "jlink" ], "jlink_device": "STM32G431CB", "openocd_target": "stm32g4x", "svd_path": "STM32G431xx.svd" }, "frameworks": [ "arduino", "cmsis", "libopencm3", "stm32cube", "zephyr" ], "name": "d_lev_io_g431cb", "upload": { "maximum_ram_size": 32768, "maximum_size": 131072, "protocol": "stlink", "protocols": [ "stlink", "jlink", "cmsis-dap", "blackmagic", "mbed" ] }, "url": "https://www.st.com/en/evaluation-tools/nucleo-g431rb.html", "vendor": "ST" }

This is the .cproject file:

`<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?fileVersion 4.0.0?>

<storageModule moduleId="org.eclipse.cdt.core.pathentry"/>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
    <project id="pio_import_test.null.401297888" name="pio_import_test"/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
<storageModule moduleId="scannerConfiguration">
    <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
    <scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.906956702;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.release.906956702.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.109176906;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.1588530580">
        <autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
    </scannerConfigBuildInfo>
    <scannerConfigBuildInfo instanceId="com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.1739418742;com.st.stm32cube.ide.mcu.gnu.managedbuild.config.exe.debug.1739418742.;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.1479917564;com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.395841839">
        <autodiscovery enabled="false" problemReportingEnabled="true" selectedProfileId=""/>
    </scannerConfigBuildInfo>
</storageModule>

`

To test this script I created a new STM32CubeIDE project for the STM32G431CBT6, set up the clock and one GPIO. The option to add references to the lib files was selected and the project generated. I built it with STM32CubeIDE to check the project was good.

I copied my custom board to "boards" folder which I created in the project root. And executed this command line:

platformio init --board d_lev_io_g431cb --project-option "framework=stm32cube"

This failed with the following output:

`AssertionError: Traceback (most recent call last): File "C:\Users\sidpr.platformio\penv\lib\site-packages\platformio__main.py", line 103, in main cli() # pylint: disable=no-value-for-parameter File "C:\Users\sidpr.platformio\penv\lib\site-packages\click\core.py", line 1130, in call__ return self.main(*args, *kwargs) File "C:\Users\sidpr.platformio\penv\lib\site-packages\click\core.py", line 1055, in main rv = self.invoke(ctx) File "C:\Users\sidpr.platformio\penv\lib\site-packages\platformio\cli.py", line 85, in invoke return super().invoke(ctx) File "C:\Users\sidpr.platformio\penv\lib\site-packages\click\core.py", line 1655, in invoke sub_ctx = cmd.make_context(cmd_name, args, parent=ctx) File "C:\Users\sidpr.platformio\penv\lib\site-packages\click\core.py", line 920, in make_context self.parse_args(ctx, args) File "C:\Users\sidpr.platformio\penv\lib\site-packages\click\core.py", line 1378, in parse_args value, args = param.handle_parse_result(ctx, opts, args) File "C:\Users\sidpr.platformio\penv\lib\site-packages\click\core.py", line 2360, in handle_parse_result value = self.process_value(ctx, value) File "C:\Users\sidpr.platformio\penv\lib\site-packages\click\core.py", line 2322, in process_value value = self.callback(ctx, self, value) File "C:\Users\sidpr.platformio\penv\lib\site-packages\platformio\project\commands\init.py", line 39, in validate_boards
pm.boardconfig(id) File "C:\Users\sidpr.platformio\penv\lib\site-packages\platformio\package\manager\platform.py", line 136, in board_config
for manifest in self.get_installed_boards(): File "C:\Users\sidpr.platformio\penv\lib\site-packages\platformio\util.py", line 56, in wrapper self.cache[key] = (time.time(), func(
args, **kwargs)) File "C:\Users\sidpr.platformio\penv\lib\site-packages\platformio\package\manager\platform.py", line 112, in get_installed_boards for config in p.get_boards().values(): File "C:\Users\sidpr.platformio\platforms\atmelsam\platform.py", line 105, in get_boards result[key] = self._add_default_debug_tools(result[key]) File "C:\Users\sidpr.platformio\platforms\atmelsam\platform.py", line 154, in _add_default_debug_tools assert openocd_chipname AssertionError

============================================================

An unexpected error occurred. Further steps:

============================================================ `

I would appreciate help resolving this. Thank you.

jbaumann commented 7 months ago

Hm, I don't think you should copy the board definition. Instead, try to use an existing STM32G431 board, or alternatively simply do the following (also in the readme): You actually do not have to call "platformio init". Instead you can simply copy the provided platformio.ini, correct the board definition and create the lib directory. Even the lib directory doesn't have to be created. If the lib directory is not found the script prints a warning and creates the directory anyway.

sidprice commented 7 months ago

Hello and thank you for helping.

I have some progress but PIO does not find any source files. I selected a nucleo431G and this is the generated PlatformIO.ini file:

`; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html

[env:nucleo_g431kb] platform = ststm32 board = nucleo_g431kb framework = stm32cube `

I copied the script to the project root and ran:

platformio init --board nucleo_g431kb --project-option "framework=stm32cube" Resolving nucleo_g431kb dependencies... Already up-to-date. Project has been successfully updated!

When I build I get:

`Executing task: C:\Users\sidpr.platformio\penv\Scripts\platformio.exe run --environment nucleo_g431kb

Processing nucleo_g431kb (platform: ststm32; board: nucleo_g431kb; framework: stm32cube)

Verbose mode can be enabled via -v, --verbose option CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/nucleo_g431kb.html PLATFORM: ST STM32 (16.1.0) > Nucleo G431KB HARDWARE: STM32G431KBT6 170MHz, 32KB RAM, 128KB Flash DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink) PACKAGES:

jbaumann commented 7 months ago

That is understandable, because now you do not use the script at all. Please have a look at the script example in the repository:

https://github.com/jbaumann/pio_and_stm32cubeide/blob/main/automatic_cubemx/platformio.ini

and add the needed entries.

sidprice commented 7 months ago

Sorry, I made that mistake. I copied and updated the example INI file but still I get:

`Resolving nucleo_g431kb dependencies... Already up-to-date. Updating metadata for the vscode IDE... UserSideException: Processing nucleo_g431kb (platform: ststm32; board: nucleo_g431kb; framework: stm32cube)

Verbose mode can be enabled via -v, --verbose option AttributeError: 'NoneType' object has no attribute 'text': File "C:\Users\sidpr.platformio\penv\lib\site-packages\platformio\builder\main.py", line 167: env.SConscript(env.GetExtraScripts("pre"), exports="env") File "C:\Users\sidpr.platformio\packages\tool-scons\scons-local-4.5.2\SCons\Script\SConscript.py", line 598: return _SConscript(self.fs, *files, **subst_kw) File "C:\Users\sidpr.platformio\packages\tool-scons\scons-local-4.5.2\SCons\Script\SConscript.py", line 285: exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals) File "D:\DataRoot\Projects\D-Lev\STM32Cube_Workspace\pio_import_test\setup_cubemx_env_auto.py", line 72: linkedURI = linked_resource.find(".//locationURI").text ========================== [FAILED] Took 0.56 seconds ==========================`

The INI file is:

`; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html

;[platformio] ; This is the root directory of the STM32CubeIDE project. Platformio can only ; take one source directory, thus we filter the sources using a ; source filter and additional build options to include the headers. ; This is set in the script and changing it here has no effect ;src_dir = ./ ; The project headers are defined here to be available for the libraries as well ; This is no longer needed. Do not set it. ;include_dir = Core/Inc

[env:nucleo_g431kb] ; The platform name is needed to identify the right build script. ; Do not change it. platform = ststm32 ; The board must fit the uC you selected in STM32CubeIDE. Otherwise there ; will be compile errors. board = nucleo_g431kb ; Setting the framework is not strictly necessary and might download additional ; resources that might not be needed. If you don‘t want that, change this setting ; to a comment. ; If you then have problems with compiling or linking, set it again to see whether ; it helps. framework = stm32cube ; This is the actual script that forces platformio into compiling the ; STM32CubeIDE project. extra_scripts = pre:setup_cubemx_env_auto.py ; The project option containing the directory in which CubeMX resides ;custom_repo_location = ~ ; Set to the same toolchain as STM32CubeIDE ; For the mapping see ; https://api.registry.platformio.org/v3/packages/platformio/tool/toolchain-gccarmnoneeabi platform_packages = toolchain-gccarmnoneeabi@1.90301.200702

; The name of the library directory in which the linked resources will ; be placed. lib_deps = STLinkedResources ; Additional Build Flags ; Example: build_flags = -D DEBUG -D FREERTOS_TOTAL_RUNTIME_TIMER build_flags = -D DEBUG `

jbaumann commented 7 months ago

Could you post your ".project" file? There is something strange going on here. And could you verify again, that you have set "Add necessary files as reference in the toolchain project configuration file"?

sidprice commented 7 months ago

Thank you, I can confirm the "Add necessary files as reference ..." option is selected.

.project file: `<?xml version="1.0" encoding="UTF-8"?>

pio_import_test org.eclipse.cdt.managedbuilder.core.genmakebuilder clean,full,incremental, org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder full,incremental, com.st.stm32cube.ide.mcu.MCUProjectNature com.st.stm32cube.ide.mcu.MCUCubeProjectNature org.eclipse.cdt.core.cnature com.st.stm32cube.ide.mcu.MCUCubeIdeServicesRevAev2ProjectNature com.st.stm32cube.ide.mcu.MCUAdvancedStructureProjectNature com.st.stm32cube.ide.mcu.MCUSingleCpuProjectNature com.st.stm32cube.ide.mcu.MCURootProjectNature org.eclipse.cdt.managedbuilder.core.managedBuildNature org.eclipse.cdt.managedbuilder.core.ScannerConfigNature Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_cortex.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_cortex.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_dma.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_dma.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_dma_ex.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_dma_ex.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_exti.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_exti.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_flash.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_flash.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_flash_ex.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_flash_ex.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_flash_ramfunc.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_flash_ramfunc.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_gpio.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_gpio.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_pwr.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_pwr.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_pwr_ex.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_pwr_ex.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_rcc.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_rcc.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_rcc_ex.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_rcc_ex.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_tim.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_tim.c Drivers/STM32G4xx_HAL_Driver/stm32g4xx_hal_tim_ex.c 1 C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_tim_ex.c

` Sid

jbaumann commented 7 months ago

Ok, could you please edit the script "setup_cubemx_env_auto.py" and change the line 72 from

linkedURI = linked_resource.find(".//locationURI").text

to

linkedURI = linked_resource.find(".//location").text

and report the results?

And which version of STM32CubeIDE are you using?

sidprice commented 7 months ago

I made your edit and built my project with the following error:

`Processing nucleo_g431kb (platform: ststm32; board: nucleo_g431kb; framework: stm32cube)

Verbose mode can be enabled via -v, --verbose option CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/nucleo_g431kb.html PLATFORM: ST STM32 (16.1.0) > Nucleo G431KB HARDWARE: STM32G431KBT6 170MHz, 32KB RAM, 128KB Flash DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink) PACKAGES:

I am using CubeIDE 1.13.2.

jbaumann commented 7 months ago

Ok, this look good already. The script runs successfully, and the compiler finds all normal files.

For the specific error see e.g., here:

https://stackoverflow.com/questions/18137814/loopfillzerobss-has-an-undefined-reference-to-main

sidprice commented 7 months ago

No it isn't good because the project builds just fine in CubeIDE, it appears the PIO project is not correctly configured and does not find the file 'main.c'. In fact, after a 'c;lean' of the project none of the project files are compiled, only the stm library files.

jbaumann commented 7 months ago

Just created a new project in STM32CubeIDE 1.13.2 with an STM32G431 and a custom configuration. Then I copied the python script and the ini file to that directory and changed the entries as described in the Readme.

It cleans, compiles and links without a problem: Linking .pio/build/nucleo_g431kb/firmware.elf Checking size .pio/build/nucleo_g431kb/firmware.elf Advanced Memory Usage is available via "PlatformIO Home > Project Inspect" RAM: [====== ] 62.7% (used 20544 bytes from 32768 bytes) Flash: [== ] 15.1% (used 19824 bytes from 131072 bytes) Building .pio/build/nucleo_g431kb/firmware.bin ================================== [SUCCESS] Took 2.79 seconds ==================================

Could you provide the exact signature of the main()-function?

And could you please try these steps with a new project?

sidprice commented 7 months ago

As I said, this very simple Blink project builds and runs in CubeIDE.

int main(void)

So, I will delete and recreate the project and try from a clean starting place. I will report back later. Thank you.

sidprice commented 7 months ago

This is getting no better for me.

I created a new CubeMX project with minimal defintions, clock setup, SWD enable, and a single GPIO as output. I generated the project with the suggested relative library path option selected. I tested it built without error.

Next: before opening VSCode I copied the script (modified as you suggested earlier) and the PlatformIO.INI file. The latter was edited to have my board name (nucleo_g431rb) in two locations and I added "-D DEBUG" to the build flags.

When I open the folder with the new project in VSCode it went into a continuous loop, I think running the script. I had to exit VSCode to stop it.

When I opened VSCode again I got the following output:

`Resolving nucleo_g431rb dependencies... Already up-to-date. Updating metadata for the vscode IDE... UserSideException: Processing nucleo_g431rb (platform: ststm32; board: nucleo_g431rb; framework: stm32cube)

Verbose mode can be enabled via -v, --verbose option SETUP_CUBEMX Error: Unexpected relative path type 'C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c' ========================== [FAILED] Took 0.56 seconds ==========================`

I have zipped up this new project, hopefully this will help identify this issue. I REALLY want to be able to use PIO in VSCode, STM32CubeIDE is just not as well integrated for development as VSCode.

jbaumann commented 7 months ago

Could it be that you are not using the STM32CubeIDE, but only CubeMX to create the project? Your project doesn't work here either, but as soon as I open it once with STM32CubeIDE and build it, everything works fine. It seems that STM32CubeIDE adds the needed header files as dependencies...

sidprice commented 7 months ago

No, I am creating/building in CubeIDE :(

jbaumann commented 7 months ago

Then I honestly have no clue, currently. I have installed a pristine version of STM32CubeIDE 1.13.2, have created a new project, added the script and the platformio.ini, and it worked. I used your project, and after opening it in my version of STM32CubeIDE, cleaning and building it, then copying the script and platformio.ini, it worked. And I assume the library files are all installed (Help->Manage Embedded Software Packages). The version I have installed is 1.5.1.

sidprice commented 7 months ago

I just updated CubeIDE to v1.14.0 and I will experiment further. Thank you for your efforts, maybe something will occur to you later and we can resolve this issue.

sidprice commented 7 months ago

Hello again.

I have uninstalled the PIO VSCode extension and reinstalled it. I created a new CubeIDE project and tried to use the script to import it into VSCode. Once again it fails. It appears that the script does most of the work but fails in setting up whatever is needed for VSCode. I get this output inside VSCode:

Resolving nucleo_g431rb dependencies... Already up-to-date. Updating metadata for the vscode IDE... UserSideException: Processing nucleo_g431rb (platform: ststm32; board: nucleo_g431rb; framework: stm32cube) Verbose mode can be enabled via -v, --verbose option SETUP_CUBEMX Error: Unexpected relative path type 'C:/Users/sidpr/STM32Cube/Repository/STM32Cube_FW_G4_V1.5.1/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c' ========================== [FAILED] Took 0.56 seconds ==========================

I have no idea how to debug what is going on, since you were able to use the project I sent I have to assume there is some error in my VSCode environment. Any suggestions as to how to debug it?

Where is the "verbose" mode set, perhaps it could reveal a useful data point.

wormyrocks commented 7 months ago

I took a look at this sample project and found the issue: Sid, "Linked Resources" in "STM32CubeIDE -> Properties for pio_import_test -> Resource -> Linked Resources" include absolute Windows paths to the HAL C files, which are stored externally to the project directory. It looks like the PIO importer does not know how to deal with absolute paths (and it certainly failed on my machine, because I have a different home directory.) It looks like jbaumann re-ran code generation, which in the latest version of CubeIDE apparently switches the resource links to relative paths. In order to resolve "undefined reference to main()" you mention here, it's necessary to uncomment src_dir = ./ and include_dir = Core/Inc in platformio.ini.

jbaumann commented 5 months ago

@wormyrocks thanks for debugging this (sorry for the delay, I got Covid). My problem is that I do not use Windows, but OSX, and the change to relative paths might also have to do with the underlying OS, not only with the version of STM32CubeIDE. Did the problem go away with the new version of STM32CubeIDE or does it still exist?