System clock stays at 24MHz??

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey guys,

I'm currently working on a AtXmega 256a3bu xplained board.
In my code i'm setting the system clock at 32MHz, but when i get the value F_CPU it says it 24MHz and also after i init my counter the CC value is also caluclated at 24MHz, because i use the F_CPU value there also.

But i know almost for sure that i set the system clock at 32MHz.

void Clock_init(void){
 /* De clock van de microcontroller wordt ingesteld op 32MHz */
	OSC.CTRL |= OSC_RC32MEN_bm;
 /* Wachten totdat 32MHz klok enabled is */
	while(!(OSC.STATUS & OSC_RC32MEN_bm));
	CCP = CCP_IOREG_gc;
	CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
 /* Enables the clock on TC1, SPI and TWI in the power reduction register */
	PR.PRPC = (TC1_clock_enable|SPI_clock_enable|TWI_clock_enable);
}

Am i forgetting something or is something wrong.
Thanks for the help anyway. :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

but when i get the value F_CPU it says it 24MHz

You don't mean the preprocessor macro called "F_CPU" do you? But that's just a compile time define. If it's defined as 1234567UL it will always read 1234567UL when you examine it. It has no relation to the speed that the CPU is actually running at (though the hope is that the programmer will always try to make this so).

If you want to know what speed an Xmega is actually going at then have the CPU output it on a pin and measure with a scope/LA/freq meter.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey clawson,

I'm using the ASF from atmel to get the F_CPU value.
What i have seen in the functie the ASF file is using is that it is looking at the bits that are 1 or 0 and determined the F_CPU value. But i cannot really see how i get 24MHz. i have also looked at the bits that are 1 or 0, but i can't see a error.

It cannot be that the Chip has a big tolerance that i get 24MHz. Which cannot be because the program looks at the bits and doesn't measure the clock.

So there has to be a bit that has to be set or something that i'm forgetting, but i cannot find which one it should be.

EDIT:
The only thing i can come up with is that the system clock is set to 48MHz from the USB and divided by 2.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do Atmel use the term in F_CPU for something else in ASF then? How very confusing. Obviously there's no way a pre-processor defined macro called F_CPU could be changed at run time. In fact even the C compiler itself is never aware of the existence of something called "F_CPU". Which ASF API are you talking about?

EDIT: Oh I see what they did:

http://asf.atmel.com/docs/3.0.1/...

so

#define F_CPU   sysclk_get_cpu_hz()

What an extraordinarily stupid thing for them to have done!

So what you are really saying is that the return from sysclk_get_cpu_hz() is not what you expect.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
EDIT: Oh I see what they did:

http://asf.atmel.com/docs/3.0.1/...

so

#define F_CPU   sysclk_get_cpu_hz()

What an extraordinarily stupid thing for them to have done!

Read it again. It clearly says "deprecated". So #define F_CPU sysclk_get_cpu_hz(), and I assume F_CPU itself, should not even be used at all.

EDIT: You still need to use F_CPU. See my post below.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Thu. Jan 30, 2014 - 06:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So they mark that as deprecated but continue to use it in most of the routines in Busy-Wait Delay Routines. Only delay_init() is marked deprecate there.

clawson wrote:
What an extraordinarily stupid thing for them to have done.
This lack of consistency in ASF is causing me to rethink if I will use it or not.

I am going to revise my previous post.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey guys,

Quote:
So what you are really saying is that the return from sysclk_get_cpu_hz() is not what you expect.

Yea i'm not getting the value that i expect from the MCU. Also if i change the F_CPU to 32MHz value the timing of the program isn't right so the MCU is definitely not 32MHz but 24MHz i assume. Because on 24MHz the timong on my MCU is right.

So you guys are advising not to use the F_CPU value?
But even if i'm not using the F_CPU value. I'm still stuck at the point that the MCU is not running on the 32MHz but a lower freq. (What i think is 24MHz)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rgamer145 wrote:
So you guys are advising not to use the F_CPU value?

No, You still have to use their version of F_CPU for the Busy-Wait Delay Routines to work properly.

Changing the value of F_CPU manually will not change the system clock frequency though. There should be a method to do that within the ASF. It is very late here, so I will let someone else try to help you find where and what that routine is.

This page might be of some help:

http://asf.atmel.com/docs/3.0.1/...

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks Lerry,

gonna look in to it. And i know the MCU freq won't change if i change the F_CPU value.

I wanne step off the ASF file. So i wanne try to set the bits myself, instead of using a ASF function.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

No, You still have to use their version of F_CPU for the Busy-Wait Delay Routines to work properly.

But this is the thing I don't get. If you use they rely on all the numbers being presented being known at compile time. That works when F_CPU is defined as 123456UL. it doesn't work if F_CPU is defined as a run-time call to sysclk_get_cpu_hz().

Presumably the use of is "verbooten" when you use ASF?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey clawson,

In the ASF file there is a file named delay.h
So i think the ASF uses a different file to use the delay functions. And the thinks there are calculated for the functions that the ASF file uses.

But it is still weird that i won't get the right clock speed when i set the bits for the clock. Without using an ASF file.
But still,
If i get the clock speed right i could dump the F_CPU variable and use a preset value to calculated the right setting for my program.
So I think there needs to be a bit changed that will result to the right clock speed, but i cannot figure out what bit it should be.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey guys,

It seems that the functie sysclk_init from the ASF file changes the setting from the 32MHz to a 24MHz value. (Half of the USB clock 48MHz) and if i init the 32MHz clock the settings are 24MHz and not 32MHz.

If i remove the sysclk_init function the system clock is 32MHz. I haven't tested i further, but at first sight it is working. But i'm gonna look further if nothing is going wrong, now that i removed the sysclk_init function.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That would make sense if your ASF program uses USB. Full speed USB requires a 48 MHz clock.

On the other hand if it uses USB then I don't know if it makes sense to use the _delay stuff unless it is just used at initialization.

Last Edited: Fri. Jan 31, 2014 - 11:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The ASF program doesn't use USB. It only uses TWI and TCC0 nothing else.
So it is weird that it is initialising USB while it is not used.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

It seems that the functie sysclk_init from the ASF file changes the setting from the 32MHz to a 24MHz value. (Half of the USB clock 48MHz) and if i init the 32MHz clock the settings are 24MHz and not 32MHz.

I try to avoid ASf as much as possible but isn't it the case that there's some kind of configuration .h file where you edit things like your chosen clock option then when you call sysclk_init() it applies your choice?

(I may be dreaming about this but I *think* I've read something about that here!).

EDIT: yup maybe I wasn't dreaming. See:

http://asf.atmel.com/docs/latest...

it wrote:
Set up the system clock prescalers as specified by the application's configuration file.

Also towards the top of the page:

Quote:
If any of the symbols are not set, reasonable defaults will be provided.

CONFIG_SYSCLK_SOURCE: The initial system clock source.
CONFIG_SYSCLK_PSADIV: The initial Prescaler A setting.
CONFIG_SYSCLK_PSBCDIV: The initial Prescaler B setting.
CONFIG_USBCLK_SOURCE: The initial USB clock source.

EDIT2: a bit more ferreting leads to this quick start for the Xmega A3:

http://asf.atmel.com/docs/latest...

So the file you need to edit is apparently called conf_clock.h

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks for the help clawson,

I'm gonna look for it in my project.

it isn't necessary to use the ASF file anymore, but i made my project out of a example code. So in the beginning i was using the ASF file much, but later on i was doing thing in my own code.
At the end forgot that the ASF file also initialised somethings. So that made some problems with the code i was writing. Then i removed some of the ASF file and the program worked again at that point. And so on till i hardly use the ASF file.
Maybe it isn't the best way to program a MCU, but it worked at the end. :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think it is a good way when you start. Find something that works and then modify it and hope it still works.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yea it sure was handy and also hard to get it working when i modified something.
I didn't have any experience with AtXmega's, but i have some experience with a Atmega32 so it was somehow familiar with the MCU family. But still had to find the ways to set the registers on the MCU and other things.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your conf_clock.h contains this:

#define CONFIG_USBCLK_SOURCE	USBCLK_SRC_RCOSC
#define CONFIG_OSC_RC32_CAL		48000000UL

the call to sysclk_init() is going to set the RC32M oscillator to run at 48MHz and set one of the prescalers (I forget which) to divide by 2. If you are not using USB, remove the USB clock stuff from conf_clock.h.

Gamu The Killer Narwhal
Portland, OR, US
_________________
Atmel Studio 6.2
Windows 8.1 Pro
Xplained boards mostly