cancel
Showing results for 
Search instead for 
Did you mean: 

F103C8 Clock issue?

tescaflown
Associate II
Posted on March 20, 2015 at 19:41

Hello,

I'm looking for some help about the main frequency. Maybe I do not understand correctly the documentation.

My test program is just a blinking LED on PORTA PIN 0.

RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;

IO_ACCESS* IO_PORTA = NEW_IO_ACCESS(&GPIOA->CRL,&GPIOA->CRH,&GPIOA->IDR,&GPIOA->ODR);

IO_PORTA->PinAsOutput(0,0);

while

(1)

{

for

(unsigned

int

long

i = 0; i<8000000; i++);

IO_PORTA->PinToggle(0);

}

With defaut values clock registers, this code use the HSI and run at 8MHz according the doc.

Not I want want use HSE and PLL to run at 32MHz.

So here my code:

LASH->ACR |= FLASH_ACR_LATENCY_1;

RCC->CR |= RCC_CR_HSEON;

while

(!(RCC->CR & RCC_CR_HSERDY));

RCC->CFGR |= RCC_CFGR_PLLSRC_HSE;

RCC->CFGR |= RCC_CFGR_PLLMULL4;

RCC->CR |= RCC_CR_PLLON;

while

(!(RCC->CR & RCC_CR_PLLRDY));

RCC->CFGR |= RCC_CFGR_SW_PLL;

while

(!(RCC->CFGR & RCC_CFGR_SWS_PLL));

RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;

IO_ACCESS* IO_PORTA = NEW_IO_ACCESS(&GPIOA->CRL,&GPIOA->CRH,&GPIOA->IDR,&GPIOA->ODR);

IO_PORTA->PinAsOutput(0,0);

while

(1)

{

for

(unsigned

int

long

i = 0; i<8000000; i++);

IO_PORTA->PinToggle(0);

}

I'm expect that my LED blinks 4x faster. But not. I tried some other comfiguration with HSI also other PLL, etc...

But whatever I did, my LED always blink at the same speed.

Is someone able to say me where is my mistake?

Thanks,

6 REPLIES 6
Posted on March 20, 2015 at 21:10

Blindly ORing on to registers seems to be prone to issues, and you probably want volatile loop iterators to stop the compiler removing the code.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tescaflown
Associate II
Posted on March 20, 2015 at 21:34

Hello Clive,

can you say me more about what you think with blindy ORing?

I use the file stm32f10x.h. I check every #define number with the datasheet. And it's exactly what I should have.

Compiler doesn't remove the for loop, if I change my max, I can speed down and speed up. So my problem is elsewhere. I'm continuing to test

jpeacock
Associate III
Posted on March 20, 2015 at 21:36

Since you are changing to 32Mhz and are above 24MHz where do you set the flash wait state to 1?  Try 16MHz as a quick test to see if your LED speeds up.  Above 24MHz you need wait states.  Below 24MHz you can leave the prefetch buffer turned off.

How do you set up PCLK1 (APB) and PCLK2 (AHB)?  PCLK2 (usually HCLK/1) should be twice the frequency of PCLK1 (usually set to HCLK/2).
Posted on March 20, 2015 at 21:46

can you say me more about what you think with blindy ORing?

Your use of OR assumes things about the initial condition of the registers. If the accumulation of bits means something else to the chip it's going to do that, not what you expect. If the CMSIS code here has already called SystemInit() the registers may not be in reset condition. It's always safer not to assume, but rather mask the register bits you wish to keep vs the ones you don't.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tescaflown
Associate II
Posted on March 20, 2015 at 22:13 Hello Jack, it's my first line

FLASH->ACR |= FLASH_ACR_LATENCY_1;

I tired a lot of other frequency with HSI, HSE. nothing give me something right. About PCLK, I assume defaut value register of RCC and FLASH after POR. So the div is always 1. My crystal is 8MHz. So do not need to change them. I will check again. Maybe the default value are not what it is written in the datasheet. I will force them. Hello Clive, Ok I get it. You right. maybe the defaut value are not what I assume. I will force them.
tescaflown
Associate II
Posted on March 21, 2015 at 15:24

Hello Clive, Jack,

my problem is solved. thanks for your help.

That's was well the ''blindy'' ORing. I thought I worked with defaut value register after POR according to the documentation.

But I included the file stm32f10x.h thinking it's was just all the #define and structure userfriendly register access. But somewhere it call the function SystemInit(void) and mess with my register access. I just deleted all these part, and now it's ok.

Thanks again.