Nucleo L432KC board json

Hi,
im trying to get arduino working on a STM32L431KCU6 custom board. After some fiddling around i got the servo sweep example working through the arduino ide with STM32duino.
Choosing “Generic L432KCUx” as board lets me upload and run this via my ST-Link V2.

The problems start with platformio. If i create a project selecting the ST Nucleo L432KC and upload the same sweep example, the board wont do anything.

.ini looks like this:

; 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_l432kc]
platform = ststm32
board = nucleo_l432kc
framework = arduino
; debug_build_flags = -Os -ggdb2 -g2


Debugging does not work correctly, pausing reveals that the program ist stuck in the error handler:

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
WEAK void _Error_Handler(const char *msg, int val)
{
  /* User can add his own implementation to report the HAL error return state */
  core_debug("Error: %s (%i)\n", msg, val);
  while (1) {
  }  // always holds here
}

After trying some different things i looked into the json file and compared it to similar ones.
The nucleo_l432kc.json was the only one that had this line missing:

    "core": "stm32",

I created a new json (genericSTM32L431KC.json) for the L431KC with the only difference being this line:

{
  "build": {
    "core": "stm32",  // added this line
    "cpu": "cortex-m4",
    "extra_flags": "-DSTM32L432xx",
    "f_cpu": "80000000L",
    "mcu": "stm32l432kcu6",
    "product_line": "STM32L432xx",
    "variant": "STM32L4xx/L432K(B-C)U_L442KCU"
  },
  "debug": {
    "default_tools": [
      "stlink"
    ],
    "jlink_device": "STM32L432KC",
    "onboard_tools": [
      "stlink"
    ],
    "openocd_target": "stm32l4x",
    "svd_path": "STM32L4x2.svd"
  },
  "frameworks": [
    "arduino",
    "cmsis",
    "mbed",
    "stm32cube",
    "libopencm3",
    "zephyr"
  ],
  "name": "ST Nucleo L432KC",
  "upload": {
    "maximum_ram_size": 65536,
    "maximum_size": 262144,
    "protocol": "stlink",
    "protocols": [
      "jlink",
      "cmsis-dap",
      "stlink",
      "blackmagic",
      "mbed"
    ]
  },
  "url": "https://developer.mbed.org/platforms/ST-Nucleo-L432KC/",
  "vendor": "ST"
}

Now with the .ini looking like this:

; 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_l432kc]
platform = ststm32
board = genericSTM32L431KC
framework = arduino
; debug_build_flags = -Os -ggdb2 -g2


The code runs just fine.

My question is: Why is the core for this particular board not specified?
To me it doesnt look like this has anything to with me using a slightly different MCU but i could be wrong.

PS: I also looked at this post and saw some similarities: Nucleo L432KC can't upload nor debug

What is the backtrace at that point shown in the let side of the debugger window?

Do you mean this:
image

or the terminal mybe?:
(removed username)

undefinedC:\Users\\.platformio\packages\toolchain-gccarmnoneeabi@1.90201.191206\bin\arm-none-eabi-gdb.exe: warning: Couldn't determine a path for the index cache directory.
Reading symbols from d:\Software\PF IO\STM32 L431\test_motor\.pio\build\nucleo_l432kc\firmware.elf...
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = stlink
PlatformIO: Initializing remote target...
xPack OpenOCD x86_64 Open On-Chip Debugger 0.11.0+dev (2021-10-16-21:19)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
hla_swd
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : DEPRECATED target event trace-config; use TPIU events {pre,post}-{enable,disable}
Info : tcl server disabled
Info : telnet server disabled
Info : clock speed 500 kHz
Info : STLINK V2J34S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.208900
Info : stm32l4x.cpu: Cortex-M4 r0p1 processor detected
Info : stm32l4x.cpu: target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32l4x.cpu on pipe
Info : accepting 'gdb' connection from pipe
target halted due to debug-request, current mode: Thread
xPSR: 0x81000000 pc: 0x0800030c msp: 0x2000ffd8
Info : device idcode = 0x10016435 (STM32L43/L44xx - Rev Z : 0x1001)
Info : RDP level 0 (0xAA)
Info : flash size = 256kbytes
Info : flash mode : single-bank
Info : device idcode = 0x10016435 (STM32L43/L44xx - Rev Z : 0x1001)
Info : RDP level 0 (0xAA)
Info : OTP size is 1024 bytes, base address is 0x1fff7000
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (1202 ms). Workaround: increase "set remotetimeout" in GDB
HAL_Init () at C:\Users\\.platformio\packages\framework-arduinoststm32\system\Drivers\STM32L4xx_HAL_Driver\Src/stm32l4xx_hal.c:184
184	    HAL_MspInit();
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08002fc4 msp: 0x20010000
Loading section .isr_vector, size 0x18c lma 0x8000000
Loading section .text, size 0x4288 lma 0x800018c
Loading section .rodata, size 0x51c lma 0x8004414
Loading section .init_array, size 0x18 lma 0x8004930
Loading section .fini_array, size 0xc lma 0x8004948
Loading section .data, size 0x88 lma 0x8004954
Info : Padding image section 0 at 0x080049dc with 4 bytes (bank write end alignment)
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08003aa4 msp: 0x20010000
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (1020 ms). Workaround: increase "set remotetimeout" in GDB
Start address 0x8003aa4, load size 18908
Transfer rate: 12 KB/sec, 2701 bytes/write.
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
Unable to match requested speed 500 kHz, using 480 kHz
Unable to match requested speed 500 kHz, using 480 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08003aa4 msp: 0x20010000
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08003aa4 msp: 0x20010000
Temporary breakpoint 1 at 0x8003a82: file C:\Users\\.platformio\packages\framework-arduinoststm32\cores\arduino\main.cpp, line 50.
PlatformIO: Initialization completed
PlatformIO: Resume the execution to `debug_init_break = tbreak main`
PlatformIO: More configuration options -> https://bit.ly/pio-debug
Note: automatically using hardware breakpoints for read-only addresses.

Program
 received signal SIGINT, Interrupt.
_Error_Handler (msg=msg@entry=0x80045ac "C:\\Users\\\\.platformio\\packages\\framework-arduinoststm32\\variants\\STM32L4xx\\L432K(B-C)U_L442KCU\\variant_NUCLEO_L432KC.cpp", val=val@entry=89) at C:\Users\\.platformio\packages\framework-arduinoststm32\libraries\SrcWrapper\src\stm32\stm32_def.c:18
18	  }

Choosing board = nucleo_l432kc causes the clock init code from Arduino_Core_STM32/variants/STM32L4xx/L432K(B-C)U_L442KCU/variant_NUCLEO_L432KC.cpp at main · stm32duino/Arduino_Core_STM32 · GitHub to be used. It wants to start the low speed external oscillator (32.768 kHz quartz). Do you have such a crystal on your board connected to your chip on OSC32K etc.? If not, this clock init will fail for you. With board = generic... you will get the Arduino_Core_STM32/variants/STM32L4xx/L432K(B-C)U_L442KCU/generic_clock.c at main · stm32duino/Arduino_Core_STM32 · GitHub clock code, which doesn’t try to activate LSE (which you say is working for you). So, seems all logical to me.

The developers haven’t yet added all boards as they are in the STM32Duino core. This is already tracked in Support all boards that the STM32 Arduino core supports · Issue #559 · platformio/platform-ststm32 · GitHub. It’s the dev’s responsibility to add full support now.

I deleted the extra line i added and it still works. Thanks for the explanation, i never would have guessed that its the name itself making the difference.
As you assumed i dont have anything on LSE, lucky for me that the name change actually made it drop the nucleo settings.

Thx also for this, with the explanation provided here i should get the rest of this working.

Exactly – for completeness, the code that controls this is here. (board_id is the board filename without .json.)

That’s why ARDUINO_NUCLEO_L432KC is defined when board = nucleo_l432kc but ARDUINO_GENERIC_L432KCUX is set when you start the board name with generic and have the mcu subfield set to stm32l432kcu6.