Problem to add board STM32F334K6

Hello,

I’m trying to add a custom board on PlatformIO (a STM32F334K6)

I followed this doc, add a JSON file:

{
  "build": {
    "core": "stm32",
    "cpu": "cortex-m4",
    "extra_flags": "-DSTM32F3 -DSTM32F334x6",
    "f_cpu": "72000000L",
    "mcu": "stm32f334k6t7",
    "product_line": "stm32f334k6" 
  },
  "connectivity": [
    "can"
  ],
  "debug": {
    "jlink_device": "STM32F334K6",
    "openocd_target": "stm32f3x"
  },
  "frameworks": [
    "cmsis",
    "stm32cube",
    "libopencm3"
  ],
  "name": "STM32F334K6 (16k RAM. 32k Flash)",
  "upload": {
    "maximum_ram_size": 16384,
    "maximum_size": 32768,
    "protocol": "stlink",
    "protocols": [
      "jlink",
      "cmsis-dap",
      "stlink",
      "blackmagic"
    ]
  },
  "url": "https://www.st.com/en/microcontrollers/stm32f334K6.html",
  "vendor": "Generic"
}

Then I tried to create a new project based on this board, with the framework stm32cube but when I include the file stm32f3xx_hal.h which contains all definitions of stm32cube I have thousand of errors like unknown type name uint16_t, uint32_t, etc.

I tested the use of framework stm32cube with a Nucleo board based on a stm32f4 mcu (pre-built settings) and I had no problem (with the stm32f4xx_hal.h file). So I don’t know if the problem comes from my custom JSON file, or from the stm32f3 framework…

Thank you in advance for your help

You can make some progress if you change the extra_flags to:

    "extra_flags": "-DSTM32F3 -DSTM32F334x8",

See ~/.platformio/packages/framework-stm32cubef3/Drivers/CMSIS/Device/ST/STM32F3xx/Include/stm32f3xx.h, line 56ff.

But most likely, this is just a partial solution. As far as I can tell, a few thing related to ld script and/or startup code are still missing.

/Users/me/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld: warning: cannot find entry symbol Reset_Handler; defaulting to 0000000008000000
Warning! Cannot find a linker script for the required board! An auto-generated script will be used to link firmware!
Warning! Cannot find the default startup file for `stm32f334k6t7 `. Ignore this warning if the startup code is part of your project.

Then the product_line value is wrong, because this is how the startup script is deduced. I’ll have a look.

Thanks for the extra_flags correction @manuelbl
But yes, now there is again a problem with the product_line value. How do you find it ?

Just like I did in Compilation failure using platform-ststm32 10.0.0 - #4 by maxgerhardt. Just wait a second.

As I’ve linked in the topic above, you can see the available startup scripts in the framework package (C:\Users\<user>\.platformio\packages\framework-stm32cubef3\Drivers\CMSIS\Device\ST\STM32F3xx\Source\Templates\gcc)

Thus for your STM32F334K6 the correct startup file is startup_stm32f334x8.s.

(Yes, a comment in the file states STM32F334x4/STM32F334x6/STM32F334x8 devices vector table for GCC toolchain, so the file is correct).

Thus you need

"product_line": "STM32F334X8"

as the correct value. (also see linked topic)

Thus when I use a platformio.ini of

[env:generic_stm32f334k6]
platform = ststm32
board = generic_stm32f334k6
framework = stm32cube

with a boards/generic_stm32f334k6.json file of

{
    "build": {
      "core": "stm32",
      "cpu": "cortex-m4",
      "extra_flags": "-DSTM32F3 -DSTM32F334x8",
      "f_cpu": "72000000L",
      "mcu": "stm32f334k6t7",
      "product_line": "STM32F334X8"
    },
    "connectivity": [
      "can"
    ],
    "debug": {
      "jlink_device": "STM32F334K6",
      "openocd_target": "stm32f3x"
    },
    "frameworks": [
      "cmsis",
      "stm32cube",
      "libopencm3"
    ],
    "name": "STM32F334K6 (16k RAM. 32k Flash)",
    "upload": {
      "maximum_ram_size": 16384,
      "maximum_size": 32768,
      "protocol": "stlink",
      "protocols": [
        "jlink",
        "cmsis-dap",
        "stlink",
        "blackmagic"
      ]
    },
    "url": "https://www.st.com/en/microcontrollers/stm32f334K6.html",
    "vendor": "Generic"
  }

and src/main.c of

#include "stm32f3xx_hal.h"
#define LED_PIN                                GPIO_PIN_5
#define LED_GPIO_PORT                          GPIOA
#define LED_GPIO_CLK_ENABLE()                  __HAL_RCC_GPIOA_CLK_ENABLE()

int main(void)
{
  HAL_Init();
  
  LED_GPIO_CLK_ENABLE();
  
  GPIO_InitTypeDef GPIO_InitStruct;
  
  GPIO_InitStruct.Pin = LED_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct); 

  while (1)
  {
    HAL_GPIO_TogglePin(LED_GPIO_PORT, LED_PIN);
    
    HAL_Delay(1000);
  }
}

void SysTick_Handler(void)
{
  HAL_IncTick();
}

void NMI_Handler(void)
{
}

void HardFault_Handler(void)
{
  while (1) {}
}


void MemManage_Handler(void)
{
  while (1) {}
}

void BusFault_Handler(void)
{
  while (1) {}
}

void UsageFault_Handler(void)
{
  while (1) {}
}

void SVC_Handler(void)
{
}


void DebugMon_Handler(void)
{
}

void PendSV_Handler(void)
{
}

the project compiles

Processing generic_stm32f334k6 (platform: ststm32; board: generic_stm32f334k6; framework: stm32cube)
--------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/generic_stm32f334k6.html
PLATFORM: ST STM32 (10.0.0) > STM32F334K6 (16k RAM. 32k Flash)
HARDWARE: STM32F334K6T7 72MHz, 16KB RAM, 32KB Flash
DEBUG: Current (blackmagic) External (blackmagic, cmsis-dap, jlink, stlink)
PACKAGES:
 - framework-stm32cubef3 1.11.1
 - tool-ldscripts-ststm32 0.1.0
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
Warning! Cannot find a linker script for the required board! An auto-generated script will be used to link firmware!
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 26 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\generic_stm32f334k6\src\main.o
..
Linking .pio\build\generic_stm32f334k6\firmware.elf
Checking size .pio\build\generic_stm32f334k6\firmware.elf
Building .pio\build\generic_stm32f334k6\firmware.bin
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   0.3% (used 44 bytes from 16384 bytes)
Flash: [          ]   3.5% (used 1144 bytes from 32768 bytes)

Thank you for your quick response :smiley:

But it still doesn’t work, the upload is successful but when I try to toggle a led it stays off (a code similar to yours but with registers updated to my board). I specify that exactly the same code work with STM32Cube IDE.

I noticed that yes it compiles but there’s still the warning

Warning! Cannot find a linker script for the required board! An auto-generated script will be used to link firmware!

So the problem with the linker script seems not solved.
I use link are debugger, so I don’t know if it’s linked to the problem… Below the output code of the upload :

Connecting to target via SWD
Found SW-DP with ID 0x2BA01477
Found SW-DP with ID 0x2BA01477
DPIDR: 0x2BA01477
Scanning AP map to find all available APs
AP[1]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x24770011)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0xE00FF000
CPUID register: 0x410FC241. Implementer code: 0x41 (ARM)
Found Cortex-M4 r0p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl[0] @ E00FF000
ROMTbl[0][0]: E000E000, CID: B105E00D, PID: 000BB00C SCS-M7
ROMTbl[0][1]: E0001000, CID: B105E00D, PID: 003BB002 DWT
ROMTbl[0][2]: E0002000, CID: B105E00D, PID: 002BB003 FPB
ROMTbl[0][3]: E0000000, CID: B105E00D, PID: 003BB001 ITM
ROMTbl[0][4]: E0040000, CID: B105900D, PID: 000BB9A1 TPIU
Cortex-M4 identified.
PC = 08000FA6, CycleCnt = 00A6B9B3
R0 = 00011850, R1 = FFFFFFF9, R2 = 000003E9, R3 = 2000052C
R4 = 20000530, R5 = 00000000, R6 = 00000000, R7 = 20002FDC
R8 = 00000000, R9 = 00000000, R10= 00000000, R11= 00000000
R12= 00000000
SP(R13)= 20002FDC, MSP= 20002FDC, PSP= 00000000, R14(LR) = 08000FE5
XPSR = 21000000: APSR = nzCvq, EPSR = 01000000, IPSR = 000 (NoException)
CFBP = 00000000, CONTROL = 00, FAULTMASK = 00, BASEPRI = 00, PRIMASK = 00

FPS0 = 00000000, FPS1 = 00000000, FPS2 = 00000000, FPS3 = 00000000
FPS4 = 00000000, FPS5 = 00000000, FPS6 = 00000000, FPS7 = 00000000
FPS8 = 00000000, FPS9 = 00000000, FPS10= 00000000, FPS11= 00000000
FPS12= 00000000, FPS13= 00000000, FPS14= 00000000, FPS15= 00000000
FPS16= 00000000, FPS17= 00000000, FPS18= 00000000, FPS19= 00000000
FPS20= 00000000, FPS21= 00000000, FPS22= 00000000, FPS23= 00000000
FPS24= 00000000, FPS25= 00000000, FPS26= 00000000, FPS27= 00000000
FPS28= 00000000, FPS29= 00000000, FPS30= 00000000, FPS31= 00000000
FPSCR= 00000000

Downloading file [.pio/build/genericSTM32F334K6/firmware.bin]...
Comparing flash   [100%] Done.
Erasing flash     [100%] Done.
Programming flash [100%] Done.
J-Link: Flash download: Bank 0 @ 0x08000000: 1 range affected (6144 bytes)
J-Link: Flash download: Total: 0.237s (Prepare: 0.026s, Compare: 0.002s, Erase: 0.067s, Program & Verify: 0.137s, Restore: 0.003s)
J-Link: Flash download: Program & Verify speed: 43 KB/s
O.K.

Reset delay: 0 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.


Script processing completed.

===================== [SUCCESS] Took 1.44 seconds =====================

I noticed another strange thing that probably has nothing to do with it. But when my microcontroller isn’t under power, terminal said that the connection to the target isn’t possible:

Target connection not established yet but required for command.
Device "STM32F334K6" selected.


Connecting to target via SWD
Cannot connect to target.


Script processing completed.

============================= [SUCCESS] Took 1.03 seconds =============================

Terminal will be reused by tasks, press any key to close it.

But he still return SUCCESS ?! Is it wanted ?

This should be still fine, it then generates a default script then with known RAM and FLASH sizes.

If you want to be sure to use the same LDScript as from STM32Cube, you can add in the platformio.ini

board_build.ldscript = STM32F334K6TX_FLASH.ld

and then copy the STM32F334K6TX_FLASH.ld from STM32F334K6TX_FLASH.ld · GitHub (raw) into the project folder. Then the error / warning will be gone.

Can you show the exact code please? There might be some things going on with optimization…

This is an issue of the JLink program, PlatformIO reads the return code of the JLink.exe client to see whether it was successful, and apparently it’s 0 / no error when it does “Script processing completed”… Not perfect or intended but yeah.

1 Like

Wonderful ! That’s where the problem came from. Now the upload works perfectly. So platformIO took probably a wrong linker script…

Okay I understand, interesting.

Thanks for the help and all explanations.

1 Like