Skip to main content
Graduate II
March 3, 2025
Solved

ST Link error when trying to add LED blink code

  • March 3, 2025
  • 2 replies
  • 638 views

STM32 NUCLEO 64 -F411RE

 

From SMT32Cube IDE I build & upload the following code that does nothing ,stripped everything away to show that the ST-Link is working,

#include "stm32f4xx.h"

int main()
{
 while(1)
 {
 /* Do nothing */
 }
}

 

This builds, uploads & does nothing,

 

File download complete

Time elapsed during download operation: 00:00:00.326

 

Verifying .

 

Download verified successfully

 

Shutting down...

Exit.

 

I then add some code to blink LED2 on & off

 

 

/* hardware_modules.c 
 light_init: set up GPIOA PA5 as output
 light_on : set PA5 high
 light_off : set PA5 low
 */

#include "hardware_modules.h"
#include "stm32f4xx.h"

#define GPIOAEN		(1U<<0)

void light_init(void)
{
	/* Enable clk access to GPIOA */
	RCC->AHB1ENR |= GPIOAEN;

	/* Set PA5 as an output pin */
	GPIOA->MODER = (1U<<10);
	GPIOA->MODER &= ~(1U<<11);
}

void light_on(void)
{
	/* Set PA5 output to high*/
	GPIOA->ODR |= (1U<<5);
}

void light_off(void)
{
	/* Set PA5 output to high*/
	GPIOA->ODR &=~(1U<<5);
}

 

 

The associated header,

 

/* hardware_modules.h */

#ifndef __HARDWARE_MODULES_H__
#define __HARDWARE_MODULES_H__

#include <stdint.h>
#include <stdbool.h>

void light_init(void);
void light_on(void);
void light_off(void);

#endif

 

 

I now put the init into main()

#include "stm32f4xx.h"
#include "hardware_modules.h"

int main()
{
 light_init();
 while(1)
 {
 /* Do nothing */
 }
}

 

 

 

Builds fine but when I try to upload I get,

 

Download verified successfully

 

Error in executing 'cont' command ...

Failed to read all registers from target

Target is not responding, retrying...

Target is not responding, retrying...

Target is not responding, retrying...

Target is not responding, retrying...

Shutting down...

Target is not responding, retrying...

Target is not responding, retrying...

Target is not responding, retrying...

Target is not responding, retrying...

Target is not responding, retrying...

Target is not responding, retrying...

Target is not responding, retrying...

Target is not responding, retrying...

 

If I remove light_init() it all works fine again.

Sanity checked using older projects & they all build & upload fine.

So what in my code is causing this upload error?

 

 

 

 

 

 

 

 

 

    This topic has been closed for replies.
    Best answer by TDK

    PA14/PA15 are used for SWD.

    > GPIOA->MODER = ...

    This reconfigures ALL GPIOA pins, which makes the SWD pins not work anymore.

    Only change the function of the pins you want, in this case pin PA5, so bits 10+11.

    2 replies

    TDKAnswer
    Super User
    March 3, 2025

    PA14/PA15 are used for SWD.

    > GPIOA->MODER = ...

    This reconfigures ALL GPIOA pins, which makes the SWD pins not work anymore.

    Only change the function of the pins you want, in this case pin PA5, so bits 10+11.

    Graduate II
    March 5, 2025

    Thanks for pointing that out I meant to OR the register. Have now changed it to...

    GPIOA->MODER |= (1U<<10);

     

     

    ..and its all working as expected now.

    Graduate II
    March 3, 2025

    Working with magic numbers is not the way, code is for humans to read aswel as machines.

    Im chatgpting your question, i suggest you do the same, because i dont remember what every register does when you put a random 3 in it.

     

     

    void light_init(void)
    {
     /* Enable clk access to GPIOA */
     RCC->AHB1ENR |= GPIOAEN;
    
     /* Ensure clock is enabled before modifying GPIOA */
     (void)RCC->AHB1ENR;
    
     /* Set PA5 as an output pin safely instead of carpet booming the whole MODER register*/
     GPIOA->MODER &= ~(3U << 10); // Clear bits 10 and 11
     GPIOA->MODER |= (1U << 10); // Set bit 10 (output mode)
    }