Uploading to STM32F407VG Discovery board fails

Hey, I’m trying to flash this code to the microcontroller:

#include "controllers/stm32f407vg/stm32f4xx.h"
#include <utils/BitMacros.hpp>

void InitGPIO()
{
    SystemInit();
    constexpr int PD12 = 12;
    ENABLE(GPIOD->MODER, 2 * PD12);       // 0b01 -> General Purpose ouput
    DISABLE(GPIOD->MODER, (2 * PD12) + 1);// set PD12 to Output

    // PA0 - External Input
    constexpr int PA0 = 0;
    DISABLE(GPIOA->MODER, 2 * PA0);      // set PA0 to be an input
    DISABLE(GPIOA->MODER, (2 * PA0) + 1);// PA0 input   

    // PB3 - Input
    constexpr int PB3 = 3;
    DISABLE(GPIOB->MODER, 2 * PB3);      // set PB3 to be an input
    DISABLE(GPIOB->MODER, (2 * PB3) + 1);// PB3 input  
    
}

void InitExtInt0()
{
    __disable_irq();             // cmsis_gcc.h - Disable interrupts globally

    // Trigger EXTI0 with PA0 (EXTI[X] -> P[Y][X])
    constexpr uint32_t Port = 0;        // PA
    constexpr uint32_t EXTIX = 0;       // EXTI0
    constexpr uint32_t Trigger = 0b01;  // Rising Edge
    if (EXTIX > 22) return;

    /* 
        P[X] will trigger the interrupt
        let Port be A and EXTIX be 1,
        this will lead to PA1 to trigger the interupt EXTI1
    */
    SYSCFG->EXTICR[EXTIX / 4] |= (Port << (EXTIX % 4));
    
    // Trigger event and interrupt
    ENABLE(EXTI->IMR, EXTIX);  // InterruptMaskRegister
    ENABLE(EXTI->EMR, EXTIX);  // EventMaskRegister

    if (IS_SET(Trigger, 0))
        ENABLE(EXTI->RTSR, EXTIX);
    else
        DISABLE(EXTI->RTSR, EXTIX);

    if (IS_SET(Trigger, 1))
        ENABLE(EXTI->FTSR, EXTIX);
    else
        DISABLE(EXTI->FTSR, EXTIX);
    
    NVIC_EnableIRQ(EXTI0_IRQn);
    __enable_irq();            
}

void delay_ms(unsigned int ms)
{
    int resetValue = 6 * SystemCoreClock / (SystemCoreClock / 1000);
    while (ms-- > 0)
    {
        int x = resetValue;
        while (x-- > 0)
            __asm("nop");
    }
}

void ToggleLED()
{
    constexpr int PD12 = 12;
    TOGGLE(GPIOD->ODR, PD12);
}

extern "C" void EXTI0_IRQHandler()
{
    ToggleLED();
    ENABLE(EXTI->PR, EXTI0_IRQn);
}

int main()
{
    InitGPIO();     // configure GPIO
    InitExtInt0();  // configure external interrupt
    while(true){};
}
[platformio]
default_envs = stm32f407vg

[common]
src_filter = 
  +<*> 
  -<.git/> 
  -<.svn/> 
  -<example/> 
  -<examples/> 
  -<test/> 
  -<tests/> 
  -<target/>
  -<controllers/>
  -<shared/>

# C++17 is needed for inline static definitions.
# if no inline static is used, these flags can be omitted
build_flags =
  -std=c++1z
  -std=gnu++1z

[env]
build_type = debug

[env:stm32f407vg]
platform = ststm32
board = disco_f407vg

src_filter = 
  ${common.src_filter} 

build_flags = 
  ${common.build_flags}
  -D STM32F407VG
  -D DEBUG

upload_protocol = stlink
upload_port = /dev/cu.usbmodem14103
debug_tool = stlink

I’m getting weird assertion errors from openocd:

> Executing task: platformio run --target upload --environment stm32f407vg <

Processing stm32f407vg (platform: ststm32; board: disco_f407vg)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/disco_f407vg.html
PLATFORM: ST STM32 6.0.0 > ST STM32F4DISCOVERY
HARDWARE: STM32F407VGT6 168MHz, 128KB RAM, 1MB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, jlink)
PACKAGES: 
 - tool-dfuutil 1.9.190708 
 - tool-openocd 2.1000.190707 (10.0) 
 - tool-stm32duino 1.0.1 
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in debug mode
arm-none-eabi-g++ -o .pio/build/stm32f407vg/src/main.o -c -fno-rtti -fno-exceptions -std=c++1z -std=gnu++1z -ffunction-sections -fdata-sections -Wall -mthumb -nostdlib -mcpu=cortex-m4 -Og -g2 -ggdb2 -DF_CPU=168000000L -DDISCO_F407VG -DPLATFORMIO=40200 -DSTM32F4 -DSTM32F407xx -DSTM32F40_41xxx -DARDUINO_STM32DiscoveryF407 -DBOARD_discovery_f4 -DSTM32F407VG -DDEBUG -D__PLATFORMIO_BUILD_DEBUG__ -Iinclude -Isrc src/main.cpp
arm-none-eabi-g++ -o .pio/build/stm32f407vg/firmware.elf -T stm32f405x6.ld -Wl,--gc-sections,--relax -mthumb -nostartfiles -nostdlib -mcpu=cortex-m4 -Og -g2 -ggdb2 .pio/build/stm32f407vg/src/main.o -L/Users/john/.platformio/platforms/ststm32/ldscripts -L.pio/build/stm32f407vg -Wl,--start-group -lc -lgcc -lm -lstdc++ -lnosys -Wl,--end-group
/Users/john/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld: warning: cannot find entry symbol Reset_Handler; defaulting to 0000000008000000
MethodWrapper(["checkprogsize"], [".pio/build/stm32f407vg/firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   0.0% (used 0 bytes from 131072 bytes)
Flash: [          ]   0.0% (used 0 bytes from 1048576 bytes)
.pio/build/stm32f407vg/firmware.elf  :
section             size        addr
.isr_vector            0   134217728
.text                  0   134217728
.data                  0   536870912
.bss                   0   536870912
._user_heap_stack   1024   536870912
Total               1024
<lambda>(["upload"], [".pio/build/stm32f407vg/firmware.elf"])
AVAILABLE: blackmagic, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
openocd -d2 -s /Users/john/.platformio/packages/tool-openocd/scripts -f board/stm32f4discovery.cfg -c "program {.pio/build/stm32f407vg/firmware.elf}  verify reset; shutdown;"
xPack OpenOCD, 64-bit Open On-Chip Debugger 0.10.0+dev (2019-07-17-15:21)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 2

Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : clock speed 2000 kHz
Info : STLINK V2J31M21 (API v2) VID:PID 0483:374B
Info : Target voltage: 2.887632
Error: jtag status contains invalid mode value - communication failure
Polling target stm32f4x.cpu failed, trying to reexamine
Examination failed, GDB will be halted. Polling again in 100ms
Info : Previous state query failed, trying to reconnect
Error: jtag status contains invalid mode value - communication failure
Polling target stm32f4x.cpu failed, trying to reexamine
Examination failed, GDB will be halted. Polling again in 300ms
Info : Listening on port 3333 for gdb connections
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Error: mem2array: Read @ 0xe0042004, w=4, cnt=1, failed
Error executing event examine-end on target stm32f4x.cpu:
/Users/john/.platformio/packages/tool-openocd/scripts/mem_helper.tcl:6: Error: 
in procedure 'program' 
in procedure 'ocd_process_reset' 
in procedure 'ocd_process_reset_inner' called at file "embedded:startup.tcl", line 214
in procedure 'mmw' called at file "/Users/john/.platformio/packages/tool-openocd/scripts/target/stm32f4x.cfg", line 79
in procedure 'mrw' called at file "/Users/john/.platformio/packages/tool-openocd/scripts/mem_helper.tcl", line 36
at file "/Users/john/.platformio/packages/tool-openocd/scripts/mem_helper.tcl", line 6
Info : Previous state query failed, trying to reconnect
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x080001c0 msp: 0x20020000
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Polling target stm32f4x.cpu failed, trying to reexamine
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
** Programming Started **
Assertion failed: (image->num_sections > 0), function image_elf_read_headers, file /Users/ilg/Work/openocd-0.10.0-13/openocd.git/src/target/image.c, line 432.
*** [upload] Error -6
=========================================================================================== [FAILED] Took 1.08 seconds ===========================================================================================

Environment    Status    Duration
-------------  --------  ------------
atmega328p     IGNORED
lpc1768        IGNORED
stm32f407vg    FAILED    00:00:01.083
===================================================================================== 1 failed, 0 succeeded in 00:00:01.083 =====================================================================================
The terminal process terminated with exit code: 1

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

General uploads worked fine a while ago.

When trying to debug, I get this:

Processing stm32f407vg (platform: ststm32; board: disco_f407vg)
--------------------------------------------------------------------------------
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/disco_f407vg.html
PLATFORM: ST STM32 6.0.0 > ST STM32F4DISCOVERY
HARDWARE: STM32F407VGT6 168MHz, 128KB RAM, 1MB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, jlink)
PACKAGES: 
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in debug mode
MethodWrapper(["checkprogsize"], [".pio/build/stm32f407vg/firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   0.0% (used 0 bytes from 131072 bytes)
Flash: [          ]   0.0% (used 0 bytes from 1048576 bytes)
.pio/build/stm32f407vg/firmware.elf  :
section             size        addr
.isr_vector            0   134217728
.text                  0   134217728
.data                  0   536870912
.bss                   0   536870912
._user_heap_stack   1024   536870912
Total               1024
========================= [SUCCESS] Took 0.55 seconds =========================

Environment    Status    Duration
-------------  --------  ------------
atmega328p     IGNORED
lpc1768        IGNORED
stm32f407vg    SUCCESS   00:00:00.549
========================= 1 succeeded in 00:00:00.549 =========================
Reading symbols from /Users/john/Documents/PlatformIO/Projects/Testing_MCB1768/.pio/build/stm32f407vg/firmware.elf...
(no debugging symbols found)...done.
PlatformIO Unified Debugger -> http://bit.ly/pio-debug
PlatformIO: debug_tool = stlink
PlatformIO: Initializing remote target...
xPack OpenOCD, 64-bit Open On-Chip Debugger 0.10.0+dev (2019-07-17-15:21)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : tcl server disabled
Info : telnet server disabled
Info : clock speed 2000 kHz
Info : STLINK V2J31M21 (API v2) VID:PID 0483:374B
Info : Target voltage: 2.886219
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : accepting 'gdb' connection from pipe
Info : device id = 0x10076413
Info : flash size = 1024 kbytes
Info : flash size = 512 bytes
0x080001c0 in ?? ()
Start address 0x8000000, load size 0
Transfer rate: 0 bits in <1 sec.
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Unable to match requested speed 2000 kHz, using 1800 kHz
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x080001c0 msp: 0x20020000
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x080001c0 msp: 0x20020000
Function "main" not defined.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]
PlatformIO: Initialization completed
PlatformIO: Resume the execution to `debug_init_break = tbreak main`
PlatformIO: More configuration options -> http://bit.ly/pio-debug
pio_reset_run_target
Cannot execute this command while the target is running.
Use the "interrupt" command to stop the target
and then try again.
Cannot execute this command while the target is running.
Use the "interrupt" command to stop the target
and then try again.
Cannot execute this command while the target is running.
Use the "interrupt" command to stop the target
and then try again.

found my error, ty for doing rubber duck debugging, forum.
apparently I was missing all the assembly files and other stuff that’s needed for the controller to start up properly, especially these lines here in my platform.ini:

src_filter = 
  ${common.src_filter} 
  +<controllers/stm32f407vg/>

And the individual enabling of the specific Ports A, B, D is also missing:

void InitGPIO()
{
    SystemInit();
    constexpr int PD12 = 12;
    ENABLE(RCC->AHB1ENR, 3);              // Enable clock for Port D = 3
    ENABLE(GPIOD->MODER, 2 * PD12);       // 0b01 -> General Purpose ouput
    DISABLE(GPIOD->MODER, (2 * PD12) + 1);// set PD12 to Output

    // PA0 - External Input
    constexpr int PA0 = 0;
    ENABLE(RCC->AHB1ENR, 0);           // Enable Clock for Port A = 0
    DISABLE(GPIOA->MODER, 2 * PA0);      // set PA0 to be an input
    DISABLE(GPIOA->MODER, (2 * PA0) + 1);// PA0 input   

    // PB3 - Input
    constexpr int PB3 = 3;
    ENABLE(RCC->AHB1ENR, 1);           // Enable Clock for Port B = 1
    DISABLE(GPIOB->MODER, 2 * PB3);      // set PB3 to be an input
    DISABLE(GPIOB->MODER, (2 * PB3) + 1);// PB3 input  
    
}
1 Like