# issue with timer code

I wrote a simple code to blink an LED connected to PA3 on the STM32F4 Discovery board every 1sec using timer4. I haven't declared any of the swd pins of the board for gpio, yet I get the following error" Target not connected" Why is it so? Are there any lines in the code causing this issue?

#include "stm32f4xx_hal.h"
#include "stm32f4xx.h"
#include "stm32f407xx.h"
#include "stdint.h"
#include "string.h"
#include "stm32f4xx_hal_rcc.h"

//NVIC_EnableIRQ(UART4_IRQn); // Enable IRQ for UART4 in NVIC
void Timer_init()
{
RCC->APB1ENR|=(1UL<<2); //TIM4 connected to APB1, hence enable clk though this register
RCC->AHB1ENR |= 1;  //clock to GPIOA
TIM4->PSC=42000;// 42MHZ/42000=1KHZ, 1millisec between two ticks
TIM4->ARR=1000;//count will increment for every one sec
//TIM4->DIER|=0X0001;//update interupt enable;
TIM4->CR1=0X0000;//UPCOUNTER;
GPIOA->MODER=0X00000040;
GPIOA->OSPEEDR=0X00000040;
TIM4->CR1|=0x0001;// enable counter
}

//#########################Clock Configuration#################
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;

/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();

/* The voltage scaling allows optimizing the power consumption when the
device is clocked below the maximum system frequency (see datasheet). */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 |
RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}
//########################################################

int main (void)
{
SystemClock_Config();
Timer_init();
while(1)
{
if(TIM4->SR&0x0001)// CHECK UIF FLAG
{
TIM4->SR&=!(0X0001);//CLEAR UIF FLAG
GPIOA->ODR^=(1<<3); //TOGGLE PIN A3
/* while(!(TIM4->SR&0X0001));
if((GPIOA->ODR)&(1<<3))
GPIOA->BSRR=((1<<3)+16);
else
GPIOA->BSRR=(1<<3);*/

}
}

}

• Your program code likely has nothing to do with the SWD connection error. Which port do you have the USB cable plugged into on the Discovery board? Are you using the on-board ST-Link? Do you have the two required jumpers installed? What programming software are you using, and with what exact command? – Chris Stratton Apr 12 '19 at 5:29
• Have you ever been able to download your code to the target at all? If not, then your code cannot in any conceivable way be responsible for the problem that you can't connect to the target. – Arsenal Apr 12 '19 at 7:44
• I think the line GPIOA->MODER= 0x00000040 is corrupting the debug pins(PA13,PA14) – sai pratyusha Apr 12 '19 at 9:46

I haven't declared any of the swd pins of the board for gpio

Yes you did in this line:

GPIOA->MODER=0X00000040;


SWD uses pins PA13 and PA14. These pins are initialized in alternate function mode at device reset, in order to let the debugger using them. According to the Reference Manual, GPIOA->MODER is set to 0xA8000000 at reset, alternate functions for PA13, PA14 and PA15 (PA15 is not used by SWD, but with JTAG debugging)

The above line sets all GPIOA pins except PA3 as input. Do some masking to preserve the port mode bits:

GPIOA->MODER = (GPIOA->MODER & ~0xC0) | 0x40;

• okay I get it now, the SWD pins are configured to alternate function and the statement in my code: GPIOA->MODER=0X00000040 contradicts it. I'll make the changes and hopefully the LED must blink every sec as per the codes functionality. – sai pratyusha Apr 12 '19 at 9:42
• I have an new issue now, the LED doen't blink for every one second as it should. Timer 4 is connected to APB1 bus which runs at 42Mhz and prescalar = 42000 and ARR =1000 – sai pratyusha Apr 12 '19 at 10:45

Your calculation for Timer auto reload register for 1 sec is wrong. The right value is approx for prescalar is 641D and 0x281H and Arr value will be 0xFFFF. I did calculations for 16 bit timer.

Calculation method:

AS APB1 has 42Mhz clock. Timer count will increase in every 1/42Mhz sec approx 23.8 nS. For 1sec or 1000000000 nS Count Value must be 1000000000/23.8 approx 42016806 counts. As 16 bit timer has max value to 65535 then Prescalar value 42016806 / 65535 approx to 641 in decimal and Arr value will be 65535.

• The values you mentioned are right, but Presalerof 42000 and ARR of 1000 is also doing the same job. 42Mhz/42000=1Khz= the new counter frequency. So each count happens after 1ms and if ARR is 1000, then 1000 counts will happen in 1sec, after which LED must toggle. – sai pratyusha Apr 12 '19 at 14:43