AnsweredAssumed Answered

STM32F407 User Button not working after setting up Clock and Bus Frequency

Question asked by beginner.st on Jun 26, 2015
Latest reply on Jul 16, 2015 by Clive One
Good Evening all,
I am new to Embedded Programming. I am using STM32f407vgt6 discovery board for programming. I am using HAL functions for setting up the SYSCLK and BUS frequencies.
In this program , after one presses USER button , LED should blink.
LED is blinking normally in case I don't use HAL libraries.
But after I have set the clock speed using HAL libraries , the user button does not work.
Could someone please help me in finding why the user button PA0 does not function ?
Here is my code (I am using CooCox IDE) :
//#include "stm32f4xx.h"
//#include "stm32f407xx.h"
#include "stm32f4xx_hal.h"
#define GPIO_TogglePE8()      (GPIOE->ODR ^= (uint16_t)0x0100)  /* Pin 8 selected */
    uint32_t sysclockfreq1;
    uint32_t HCLK11;
    uint32_t PCLK11;
    uint32_t PCLK21;
    TIM_HandleTypeDef htim5;
    void SystemClock_Config(void);
int main(void)
{
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();
    /* Configure the system clock */
    SystemClock_Config();
    // Debug Purpose
    sysclockfreq1=HAL_RCC_GetSysClockFreq();
    HCLK11=HAL_RCC_GetHCLKFreq();
    PCLK11=HAL_RCC_GetPCLK1Freq();
    PCLK21=HAL_RCC_GetPCLK2Freq();

    RCC->AHB1ENR |= 0x0019;       // Enable clock for GPIOE & GPIOD & GPIOA
    RCC->APB1ENR |= 0x00000008;  // Enable Clock for Timer 5
    GPIOE->MODER |= 0x00010000;  // Output pin for time mark at port E bit 8
    GPIOD->MODER |= 0x04000000;  // Output pin for PD13 LD3 Orange
    GPIOA->MODER |= 0x00000000;  // Input pin for reading User Button PA0
    while(!(GPIOA->IDR & 0x01));  //wait when PA0=0
    GPIOD->ODR |= 0x2000;// The program control does not reach this line even after pressing the user button ( But during debugging the control is coming here)
    HAL_NVIC_EnableIRQ(TIM5_IRQn);  // Enable IRQ for TIM5 in NVIC. TIM5_IRQn present in stm32f407xx.h

    TIM5->ARR = 8400;           // Auto Reload Register value => 0.1 ms
    TIM5->PSC = 9;             // => 1 ms
    TIM5->DIER |= 0x0001;        // DMA/IRQ Enable Register - enable IRQ on update
    TIM5->CR1 |= 0x0001;         // Enable Counting
    HAL_TIM_IRQHandler(&htim5);
    while(1)
    {
         //GPIOD->ODR |= 0x2000;
    }
}
void SystemClock_Config(void)
{
  // Enabling the external crystal Clock
  // Enable HSE Oscillator and Activate PLL with HSE as source

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  //__PWR_CLK_ENABLE(); //This is to enable access to the RTC Domain and RTC registers
  //__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  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 = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct)== HAL_OK)
    {
        // do printf. Could also turn on LED to indicate that HSE isn't running well
        RCC->AHB1ENR = 0x0000008;    // Enable Clock for PortD
        GPIOD->MODER |= (1<<30);     // Enable PortD Pin 15 as a digital output
        GPIOD->ODR |= (1<<15);       // Turn GPIO Pin 15 ON or Assert High
    }

  // Select PLL as system clock source and configure the HCLK, PCLK1
  // 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);
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5)== HAL_OK)
  {
      // do printf.
      RCC->AHB1ENR = 0x0000008;    // Enable Clock for PortD
      GPIOD->MODER |= (1<<28);     // Enable PortD Pin 14 as a digital output
      GPIOD->ODR |= (1<<14);       // Turn GPIO Pin 14 ON or Assert High
  }

}

Outcomes