# Stlink connection lost after programing

#### varden

Joined Jul 11, 2016
38
Hi. I was trying to blink led on stm32f401re nucleo board. I am able to program chip but just after I reset it, stlink lost connection. I tried this with keil and truestudio and every time I program and reset, I lost connection, I can no longer debug or program chip. To program chip, I need to connect stlink utility and erase chip memory from there. Then stlink connection comes back. Different codes from internet seems to work without problem. I think something is wrong with my code but I am not sure. Does anyone know anything about this problem?

The code:

Code:
#include "stm32f4xx.h"

int main(void)
{
RCC->AHB1ENR |= 0x00000001;

GPIOA->MODER |= 0x0C000400;

while (1)
{
GPIOA->ODR = 0x00000020;
}

}

#### varden

Joined Jul 11, 2016
38
Okay I found it. Apparently I wrote wrong value to MODER register. After changing it, problem solved.

#### mckenney

Joined Nov 10, 2018
86
I am able to program chip but just after I reset it, stlink lost connection.
While this wasn't your case, I'll throw this in for people searching the forum:
Some low-power modes can make it difficult to get the ST-Link to connect. I find myself including this line in my programs (guarded by _DEBUG or some such). It keeps the debug clock running even in sleep mode. It costs a little bit in power, but assures I can always get in:
Code:
    DBGMCU->CR |= DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP;
In cases where I forget this line, I can usually get in by pushing the Reset button on the board while the debugger is connecting (often takes a few tries).