cancel
Showing results for 
Search instead for 
Did you mean: 

ST Link error when trying to add LED blink code

NicRoberts
Associate III

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?

 

 

 

 

 

 

 

 

 

2 REPLIES 2
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".
Javier1
Principal

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)
}

 

 

 

Available for consulting/freelancing , hit me up in https://github.com/javiBajoCero