Question
STM32F407-Baremetal clock not working
Hi all, I tried to configure the clock to run at 32MHz and I was using Systick to toggle the LED every 1 Sec by configuring the Systick reload value by 32Mhz -1. But, Somehow toggling is not happening for exactly 1 sec I also used a logic analyzer to check the period and it was not 1 sec. Somehow, I am not able to figure out the problem.
I selected HSE(8MHz) and PLL as a System clock. PLLM = 2,PLLN=16,PLLP=2. AHB prescaler as 1.
RCC.h
#include <stdint.h>
#ifndef RCC_H_
#define RCC_H_
#define RCC_BASE_ADDR 0x40023800 /* Base Addr of RCC */
#define FLASH_INTR 0x40023C00 /* Base of Flash Interface Register */
#define RCC_CR *(volatile uint32_t*)(RCC_BASE_ADDR + 0x00)
#define RCC_PLLCFGR *(volatile uint32_t*)(RCC_BASE_ADDR + 0x04)
#define RCC_CFGR *(volatile uint32_t*)(RCC_BASE_ADDR + 0x08)
#define RCC_APB1RSTR *(volatile uint32_t*)(RCC_BASE_ADDR + 0x20)
#define FLASH_ACR *(volatile uint32_t*)(FLASH_INTR + 0x00)
void clock_init();
void clock_init1();
#endif /* RCC_H_ */
RCC.c
#include "rcc.h"
void clock_init()
{
FLASH_ACR |=(1<<0); /* 1 wait states */
RCC_CR |= (1<<16); /* HSE oscillator is ON */
while(!(RCC_CR & (1<<17))); /* wait till HSE oscillator is ready */
RCC_PLLCFGR |= (2<<0); /* PPLM is selected */
RCC_PLLCFGR |= (16<<6); /* PPLN is selected */
RCC_PLLCFGR |= (0<<16); /* PPLP is selected */
RCC_PLLCFGR |= (2<<24); /* PPLQ is selected */
RCC_PLLCFGR |= (1<<22); /* HSE is selected as SRC for PLL */
RCC_CFGR |= (0<<4); /* AHB Pre-scaler */
RCC_CFGR |= (0<<10); /* APB1 Pre-sclaer */
RCC_CFGR |= (0<<13); /* APB2 Pre-sclaer */
RCC_CR |= (1<<24); /* PLL is enabled */
while(!(RCC_CR & (1<<25))); /* wait till PLL oscillator is ready */
RCC_CFGR |= (2<<0); /* PLL selected as system clock */
while(!(RCC_CFGR & (2<<2))); /* wait till PLL to be clock */
}
Main.c
#include <stdint.h>
#include "GPIO.h"
#include "rcc.h"
void delay1(void);
void Systick_Init(void);
#define SYST_CSR *(volatile uint32_t*)(0xE000E010) /* Systick controller register */
#define SYST_RVR *(volatile uint32_t*)(0xE000E014) /* Systick Reload Register */
int main(void)
{
clock_init();
GPIOA_Init();
Systick_Init();
}
void Systick_Init()
{
/* SysTick Reload value = System clock(Hz) x Delay Desired(s) */
/*
Reload value = ((Clock_Speed(Hz)/Desired_Tick(Hz))-1))
Here 1sec is required to generate systick interrupt, so the Desired Tick is 1Hz(1Sec)
and clock speed is 16MHz
*/
SYST_RVR = 32000000-1; /* Reload value */
SYST_CSR |= 0x07UL; /* Enable counter, systick interrupt, clk source as processor clk */
}
void SysTick_Handler()
{
GPIO_ODR ^= (1<<0);
}
