-3
#include <stm32f2xx_gpio.h>
#include <stm32f2xx_rcc.h>
#include <stm32f2xx_flash.h>
#include <stm32f2xx_exti.h> 
#include <stm32f2xx.h>
#include <stm32f2xx_syscfg.h>
#include <misc.h>

void SetupClock()
{
RCC->CR |= 1 << 16;

while ((RCC->CR & 1 << 17) == RESET) {}

//RCC->PLLCFGR |= 1 << 17;
RCC->PLLCFGR = 0x5401E06;

RCC->CFGR &= ~(1 << 4 | 1 << 5 | 1 << 6 | 1 << 7 | 1 << 11 | 1 << 14 | 1 << 13);
RCC->CFGR |= 1 << 10 | 1 << 12;
RCC->CFGR |= 1 << 15;

FLASH->ACR &= ~(1 << 2);
FLASH->ACR |= 1 << 0 | 1 << 1;

RCC->CR |= 1 << 24;

while ((RCC->CR & 1 << 25) == RESET) {}

RCC->CFGR &= ~(1 << 0);
RCC->CFGR |= 1 << 1;

while (((RCC->CFGR & 1 << 2) != RESET) && ((RCC->CFGR & 1 << 3) != SET)) { }
}

int main()
{
SetupClock();

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

for (;;)
{
  GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
  asm("nop");
  < ... >
  asm("nop");
  GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET);
  asm("nop");
  < ... >
  asm("nop");
}
}

The code is written according to manual but the frequency is off.

user61143
  • 128
  • 9
  • What doesn't work? How can you tell? What is happening? What should happen? – Eugene Sh. Mar 25 '15 at 17:30
  • Too many magic numbers. There should be `#define`s for all those Bits in a verdor supplied header file. – Turbo J Mar 25 '15 at 17:31
  • "the frequency is off" - what does that mean? Do you have your code running? Have you stepped it over with debugger? Does it jump to a Hard Failure ISR? – Eugene Sh. Mar 25 '15 at 17:34
  • I don't have a debugger. I changed the amount of NOPs between toggles and calculated the frequency from the data from a logical analyser. The frequency was about 32MHZ, just like without any clock code. – user61143 Mar 25 '15 at 17:37
  • Where are the code comments? How is this electronics design? Have you asked on the STM forums? – KyranF Mar 25 '15 at 17:48
  • Also, why mess with register-level programming when you have an extensive library to deal with it without any black magic? (`RCC_*` functions). – Eugene Sh. Mar 25 '15 at 17:52
  • 4
    I'm voting to close this question as off-topic because it's about using a device, and it's particular set up code, not electronics design. – KyranF Mar 25 '15 at 19:12
  • 2
    @KyranF This question is more *unclear* than *off-topic*. – Nick Alexeev Mar 25 '15 at 20:23

1 Answers1

0

This code works:

void SetupClock()
{
RCC->CR |= 1 << 16;

while ((RCC->CR & 1 << 17) == RESET) {}

//RCC->PLLCFGR |= 1 << 17;
RCC->PLLCFGR = 0x5401E06;

RCC->CFGR &= ~(1 << 4 | 1 << 5 | 1 << 6 | 1 << 7 | 1 << 11 | 1 << 14 | 1 << 13);
RCC->CFGR |= 1 << 10 | 1 << 12;
RCC->CFGR |= 1 << 15;

FLASH->ACR &= ~(1 << 2);
FLASH->ACR |= 1 << 0 | 1 << 1 | 1 << 8 | 1 << 9 | 1 << 10;

RCC->CR |= 1 << 24;

while ((RCC->CR & 1 << 25) == RESET) {}

RCC->CFGR &= ~(1 << 0);
RCC->CFGR |= 1 << 1;

while (((RCC->CFGR & 1 << 2) != RESET) && ((RCC->CFGR & 1 << 3) != SET)) { }
}

With

SetSysClock();

Commented out in init file.

user61143
  • 128
  • 9
  • 2
    So, you've found the answer? And it was just that? So it was a software/setup issue... not sure if this entire question is relevant enough to stay on this site.. – KyranF Mar 25 '15 at 19:11
  • If you ever intend to use the global variable SystemCoreClock such as in a call to set up the SysTick_Handler() using something like SysTick_Config(SystemCoreClock/1000); you will either need to set this variable yourself or call SystemCoreClockUpdate(); which is probably in your init file (system_stm32f2xx.c ?) since you are setting up the clock yourself. The comments for SystemCoreClockUpdate() read "Updates the variable SystemCoreClock and must be called whenever the core clock is changed during program execution.". – Tut Mar 25 '15 at 19:53
  • Why aren't you letting system_stm32f2xx.c do the clock setup via the function SetSysClock()? You can edit that if you need to. There should be no need for your SetupClock() function. – Tut Mar 25 '15 at 20:02