Adding support of STM32F103RE into mbed

Hello Everyone!

Could someone please help me with adding of of support of STM32F103RE microcontroller to mbed framework?

It seems that there is support of STM32F103RB with a single difference in smaller flash size and with the same pins.

What I did:

  1. Created environment in platformio.ini
    [env:STM32F103RET6-board]
    platform = ststm32
    board = STM32F103RE-board
    framework = mbed
    build_flags = -D PIO_FRAMEWORK_MBED_RTOS_PRESENT
  2. Added custom_targets.json into root of my project:
    {
    “build”: {
    “core”: “maple”,
    “cpu”: “cortex-m3”,
    “extra_flags”: “-DSTM32F103xE -DSTM32F1”,
    “f_cpu”: “72000000L”,
    “hwids”: [
    [
    “0x1EAF”,
    “0x0003”
    ],
    [
    “0x1EAF”,
    “0x0004”
    ]
    ],
    “ldscript”: “stm32f103xe.ld”,
    “mcu”: “stm32f103ret6”,
    “variant”: “stm32f1”
    },
    “debug”: {
    “jlink_device”: “STM32F103RE”,
    “openocd_target”: “stm32f1x”,
    “svd_path”: “STM32F103xx.svd”
    },
    “frameworks”: [
    “arduino”,
    “mbed”,
    “cmsis”,
    “libopencm3”,
    “stm32cube”
    ],
    “name”: “STM32F103RET6-board”,
    “upload”: {
    “disable_flushing”: false,
    “maximum_ram_size”: 65536,
    “maximum_size”: 524288,
    “protocol”: “stlink”,
    “protocols”: [
    “jlink”,
    “stlink”,
    “blackmagic”,
    “serial”,
    “dfu”
    ],
    “require_upload_port”: true,
    “use_1200bps_touch”: false,
    “wait_for_upload_port”: false
    },
    “url”: “STM32F103RE - Mainstream Performance line, Arm Cortex-M3 MCU with 512 Kbytes of Flash memory, 72 MHz CPU, motor control, USB and CAN - STMicroelectronics”,
    “vendor”: “Generic”
    }
  3. Added STM32F103RET6-board.json into boards folder:
    {
    “build”: {
    “core”: “maple”,
    “cpu”: “cortex-m3”,
    “extra_flags”: “-DSTM32F103xE -DSTM32F1”,
    “f_cpu”: “72000000L”,
    “hwids”: [
    [
    “0x1EAF”,
    “0x0003”
    ],
    [
    “0x1EAF”,
    “0x0004”
    ]
    ],
    “ldscript”: “stm32f103xe.ld”,
    “mcu”: “stm32f103ret6”,
    “variant”: “stm32f1”
    },
    “debug”: {
    “jlink_device”: “STM32F103RE”,
    “openocd_target”: “stm32f1x”,
    “svd_path”: “STM32F103xx.svd”
    },
    “frameworks”: [
    “arduino”,
    “mbed”,
    “cmsis”,
    “libopencm3”,
    “stm32cube”
    ],
    “name”: “STM32F103RET6-board”,
    “upload”: {
    “disable_flushing”: false,
    “maximum_ram_size”: 65536,
    “maximum_size”: 524288,
    “protocol”: “stlink”,
    “protocols”: [
    “jlink”,
    “stlink”,
    “blackmagic”,
    “serial”,
    “dfu”
    ],
    “require_upload_port”: true,
    “use_1200bps_touch”: false,
    “wait_for_upload_port”: false
    },
    “url”: “STM32F103RE - Mainstream Performance line, Arm Cortex-M3 MCU with 512 Kbytes of Flash memory, 72 MHz CPU, motor control, USB and CAN - STMicroelectronics”,
    “vendor”: “Generic”
    }
  4. Do compile and get Error: Unknown board ID ‘STM32F103RE-board’

I think that something can be also wrong but I got stuck with this…

I’ve corrected custom_targets.json to the following file:
{
“STM32F103RET6-board”:{
“inherits”:[“NUCLEO_F103RB”],
“extra_labels_add”: [“STM32F1”, “STM32F103RE”],
“detect_code”: [“0700”],
“release_versions”: [“2”, “5”],
“device_name”:“STM32F103RET6-board”
}
}

That helped to get rid of “Error: Unknown board ID ‘STM32F103RE-board’” error. But I get the new one:

write() takes exactly 2 arguments (3 given)
mbed build API internal error

And it is not clear what is the detect_code for this MCU.

Caused by improper call of sys.stderr.write() in <home dir>\.platformio\packages\framework-mbed\platformio\platformio-build.py

Corrected function at line 79 must be

    def get_target_config(self):
        target_info = TARGET_MAP.get(self.target, "")
        if not target_info:
            sys.stderr.write(
                "Failed to extract info for %s target\n" % self.target)
            sys.exit(1)

        return target_info

Which will then give you the real error message Failed to extract info for STM32F103RET6-board. So what’s actually missing is an entry in <home dir>\.platformio\packages\framework-mbed\platformio\variants_remap.json, just add a new line which maps the board = .. name unto your chosen target name from the custom_targets.json.

"STM32F103RET6-board" :"STM32F103RET6-board"
1 Like

Yes, that helps to avoid of “write() takes exactly 2 arguments (3 given)” error. But it looks like a hardcoding. Is there a way how to avoid the modification of variants_remap?

After solving of the “2 arguments error” I got a great number of the following error:
C:\Users\VLADIS~1\PLATFO~1\packages\FRA7E3~1\targets\TAE5D3~1\TA6EE1~1\TARGET~1\device/stm32f1xx.h:151:12: fatal error: stm32f103xe.h: No such file or directory

stm32f103xe.h exists but located here C:\Users\Vladislav.platformio\packages\framework-stm32cube\f1\Drivers\CMSIS\Device\ST\STM32F1xx\Include\

I barely understand what is wrong…

Yeah but this is in an entirely different framework, not mbed-os.
Did you put your target device source files (pin mapping etc) in the src/ folder and added it to the include paths? (as it’s seen in https://github.com/platformio/platform-nxplpc/tree/develop/examples/mbed-custom-target)

Yes, but I’m not sure that everything was made in the right way.
PlatfromIO env is the following:
[env:STM32F103RET6-board]
platform = ststm32
board = STM32F103RET6-board
framework = mbed
build_flags =
-D PIO_FRAMEWORK_MBED_RTOS_PRESENT
-I$PROJECTSRC_DIR/STM32F103RET6-board
-I$PROJECTSRC_DIR/STM32F103RET6-board/device

And I took the src files from Nucleo_F103RB (from here C:\Users\Vladislav.platformio\packages\framework-mbed\targets\TARGET_STM\TARGET_STM32F1\TARGET_NUCLEO_F103RB). The pins should be the same for these two targets, the difference (I believe) is only in the amount of Flash and RAM.

but as you can see it really does have the stm32f103xb.h file and not the *xe.h file. What happens when you remove that file and give it the correct file you found in the stm32cube folder?

After copying of stm32f103xe.h to the src dir I get rid of the “No such file or directory” error. And the compilation started. But the things in general are nto fixed:
src\STM32F103RET6-board\device\TOOLCHAIN_ARM_MICRO\startup_stm32f103xb.S:100: Error: bad instruction dcd TIM1_TRG_COM_IRQHandler' src\STM32F103RET6-board\device\TOOLCHAIN_ARM_MICRO\startup_stm32f103xb.S:100: Error: bad instructiontim1 Trigger and Commutation’
src\STM32F103RET6-board\device\TOOLCHAIN_ARM_MICRO\startup_stm32f103xb.S:101: Error: bad instruction dcd TIM1_CC_IRQHandler' src\STM32F103RET6-board\device\TOOLCHAIN_ARM_MICRO\startup_stm32f103xb.S:101: Error: bad instructiontim1 Capture Compare’
src\STM32F103RET6-board\device\TOOLCHAIN_ARM_MICRO\startup_stm32f103xb.S:102: Error: bad instruction dcd TIM2_IRQHandler' src\STM32F103RET6-board\device\TOOLCHAIN_ARM_MICRO\startup_stm32f103xb.S:102: Error: bad instructiontim2’
src\STM32F103RET6-board\device\TOOLCHAIN_ARM_MICRO\startup_stm32f103xb.S:103: Error: bad instruction dcd TIM3_IRQHandler' src\STM32F103RET6-board\device\TOOLCHAIN_ARM_MICRO\startup_stm32f103xb.S:103: Error: bad instructiontim3’

I got several screens of such errors. Also as such:
C:\Users\Vladislav.platformio\packages\framework-mbed\targets\TARGET_STM\serial_api.c:91:24: error: ‘UART_4’ undeclared (first use in this function); did you mean ‘UART_3’?

And as a result the compilation is failed.

I think that there should be a more straight way. I think that I did something wrong in general…

Remove this folder, PIO is compiling everything from all folders and that one contains code for a different compiler.

Yeah because the header file does declare the existance of UART_4 by defining its base address, but your pin mapping files don’t declare it.

You see, the STM32F103RE is different to RB in not only flash but also peripherals. (Must click to expand cropped thumbnail)

STM32F103RE:

STM32F103RB:

You probably need to write the pin mapping files from scratch based on the work in this repository:

https://github.com/stm32duino/Arduino_Tools/tree/master/src/genpinmap/Arduino/STM32F103R(C-D-E)Tx

I successfully did this for a STM32F401CCU6.

1 Like

Oh, the things getting more difficult than it looks before. I decided just to jump to RB version of the chip and forget about RE.
Anyway thank you for the support and explanations.