XMEGA128A1U clock setting for LUFA

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

Dear Experts,

Hi,

 

for a project based on XMEGA128A1U, I use the sample code of LUFA :

 

Clock startup system is like following:

 

	XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
	XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
	

	XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
	XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);

As it can be seen, Internal 2MHz clock is multiplied to 16 to produce F_CPU clock by using PLL and is set for CPU Clock.

 

And internal R32MHz clock is enabled and by using DFLL is set to be used for USB clock which is 48MHz.

 

 

Based on my observations, I find that CPU clock does not have enough accuracy and has near 2% error.

 

Thus I need to event precise it or use external clock source for CPU clock.

 

I use following code for internal clock source but it does not work properly.

 

	XMEGACLK_StartExternalOscillator(EXOSC_FREQ_9MHZ_MAX,EXOSC_START_6CLK);
	XMEGACLK_SetCPUClockSource(CLOCK_SRC_XOSC);

I find that I can not read any more data from USB device if I use above code.

 

Please help me.

 

1. How to set external clock (my external clock crystal is 8 MHz) in a way that it works.

2. Or how to make internal clock accurate (by using DFLL technique)

 

Please let me know how to use LUFA functions to reach this goal.

 

Best Wishes

Mehrdad,

 

 

 

 

 

 

This topic has a solution.
Last Edited: Mon. Nov 5, 2018 - 10:37 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again,

 

By checking crystal pins by using a oscilloscope, I find that it does not oscillate.

 

I do not know if I have to run some instruction in XMEGA device to activate it or it supposed to oscillate from beginning.

 

It is very bad to start with a board which has some malfunction.

 

As far as I saw, every board which has crystal oscillator oscillate from beginning.

 

 

 

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

Your last post asked about the 32.768kHz crystal oscillator - The first three lines in code below are needed to use that oscillator I believe. The amplitude of the oscillator signal is only about 250mvpp.

 

// ******* System Clock Initialization ***********************************************************
	VBAT.CTRL |= VBAT_ACCEN_bm;
	VBAT.CTRL |= VBAT_XOSCEN_bm;
		
	OSC.XOSCCTRL=0x02;							// Select TOSC 32.768kHz crystal oscillator as the external oscillator
		
	DFLLRC32M.CALA=0x3F;	// Fixed - middle of range
	DFLLRC32M.CALB=0x24;	// From table to select freq of '32MHz' oscillator
	DFLLRC32M.COMP1=0x1B;
	DFLLRC32M.COMP2=0xB7;						// 1.024kHz * 0xB71B = 48.0 MHz
		
	OSC.CTRL=OSC_RC32MEN_bm | OSC_XOSCEN_bm | OSC_RC2MEN_bm; // Enable 32MHz, 2MHz, and External Oscillators
		
	while (! (OSC.STATUS & OSC_XOSCRDY_bm));	// Wait for 32kHz crystal oscillator to be stable
	while(!(OSC.STATUS & OSC_RC32MRDY_bm)); 	// Wait for 32MHz RC oscillator to be stable

	OSC.DFLLCTRL = OSC_RC32MCREF_XOSC32K_gc;	// Select 32.768kHz xtal osc as reference for 32MHz DFLL
		
	DFLLRC32M.CTRL = DFLL_ENABLE_bm;			// Enable FDLL for 48MHz RC oscillator
	CCP=CCP_IOREG_gc;							// Unlock protected IO
	CLK.CTRL=CLK_SCLKSEL_RC32M_gc;				// Switch to the 48MHz clock

 

ArcherVic

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

Thanks alot Archer,

I do not develop battery powered device.

I use an atxmega128a1u device and try to develop on it by using atmel studio version 7.

 

But when I put following lines in my code I get compiler error:

VBAT.CTRL |= VBAT_ACCEN_bm;

VBAT.CTRL |= VBAT_XOSCEN_bm;

Please give me some hints about above code.

 

I get another board based on atxmega32a4u and find that the crystal osc of it does not oscillate like my one.

 

I think some code must be run to enable external clock oscillation.

 

Or some fuse setting must be set.

 

As I expect, these kind of problems must be very common and resolved soon. But it seems that most of xmega users prefer to use internal clock and have not faced this problem.

 

Any way If you know anything about this problem please let me know.

 

FYI, external crystal is connected to PR0 and PR1 pins of micro controller.

 

Internal clocks work but are not precise enough.

 

Project dead lines are near.

 

Thanks alot. 

 

 

 

 

 

 

 

 

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

Sorry, I thought you wanted to use the 32.78kHz crystal oscillator. The oscillator on PR1, PR0 is higher frequency, .4MHz to 16 MHz. What frequency crystal do you have? This oscillator has to be enabled using the XOSCEN bit in OSC.CTRL register, and also XOSCSEL[3:0] in OSC.XOSCCTRL register. See section 7.10 in the XMEGA AU manual PDF.

Is the code you posted from Atmel START or ASF? I don't know what that does, I just know how these registers have to be configured. Run the code in debug and see how those registers are configured. As far as the compile problem, have you included <avr/io.h>?

 

ArcherVic

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks alot @Archer-Vic

I use external 8 MHz crystal connected to Pr1 and Pr0 pins.

I use LUFA functions to setup clock frequency.

I use Xmega device.

I think something is wrong with settings.

Original code uses 2 clocks:

Internal 2MHz clock is multiplied by 16 to produce 32MHz clock which is feed to System Clock Source.

Internal 32MHz clock is DFLL ed and used to produce 48 MHz USB clock.

As internal 2 MHz clock is not accurate enough, I decided to use an external clock, But failed to get answer yet.

Main problem is that I do not see crystal oscillation by using an oscilloscope and I wonder If I have to activate it or it should be activated at first.

As this is my first project with Xmega, these kind of problems always happen, but I expect to get some basic answers because I think  it is a common problem. 

 

Anyway by surfing the web I find some sample codes especially this one:

 

https://www.edaboard.com/showthread.php?169385-Xmega-external-clock-selection

 

It advises to wait more for external clock stabilization, 

It also disables the 2MHz internal clock.

 

I hope to get answer from it because it has the same problem as mine.

 

God is big (The phrase which is used in my country in this situation which means God is enough to help us resolve the problems. )

 

Let me test ...

 

laugh

 

 

 

 

 

 

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

If the 8MHz oscillator is not on, then you need to run the code in debug and examine the OSC.CTRL and OSC.XOSCCTRL registers. Make sure the oscillator is enabled and selected. If it's not, then there is something wrong in your setup. Did you try a different crystal? maybe the one you have is bad.

Setting up the clocks yourself by writing directly to the registers is not that difficult, just study section 7 in XMEGA-AU_Manual.pdf - It will be worth your time and you will gain an understanding of how it all works.

ArcherVic

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

 

This sample code works for me:

I think I do not wait enough for clock stabilization and switch  CPU clock to it when it was not stabilized yet and face a hang problem. 

 

By waiting for 16K clock before using it, my code works. 

 

https://www.edaboard.com/showthread.php?169385-Xmega-external-clock-selection

 

Thanks for your support Archer-Vic,