XMega64A4U system clock not correctly sets

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

Hi all,

 

Sorry this has been maybe already discuss around but I'm not able to found a clear answer to my issue.

I'm programming a XMega64A4U, everything went well up to setup the USB clock to 48MHz.

Here what I'm trying to do:

- use the 2MHz Int. Osc x 16 (PLL) for the System clock

- use the 32MHz Int. Osc calibrated to 48MHz for the USB clock

 

But when I'm doing the second step (USB) the system clock is updated to 48MHz ????

I can see it on the logic analyzer as I'm using the SPI interface with a prescaler of DIV4, the SPI clock is now at 12MHz instead of 8MHz.

 

If someone can help?

 

Thanks,

 

Here my code:

	/* The 2MHz RC is already enabled. */

	/* Set the System Clock to 32MHz from 2MHz RC + PLL. */
	OSC.PLLCTRL = OSC_PLLSRC_RC2M_gc | 16; /* 16*2MHz=32MHz. */
	OSC.CTRL |= OSC_PLLEN_bm;
	while (!(OSC.STATUS & OSC_PLLRDY_bm)) {}
	CCP = CCP_IOREG_gc;
	CLK.CTRL = CLK_SCLKSEL_PLL_gc;
	CCP = CCP_IOREG_gc;

	/* Set the USB Clock to 48MHz from the 32MHz RC. */
	CLK.USBCTRL = CLK_USBSRC_RC32M_gc;

	OSC.DFLLCTRL = OSC_RC32MCREF_USBSOF_gc;
	NVM.CMD  = NVM_CMD_READ_CALIB_ROW_gc;
	DFLLRC32M.CALB = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSC));
	DFLLRC32M.COMP1 = 0x1B;
	DFLLRC32M.COMP2 = 0xB7;
	DFLLRC32M.CTRL = DFLL_ENABLE_bm;

	CCP = CCP_IOREG_gc;
	OSC.CTRL = OSC_RC32MEN_bm | OSC_RC2MEN_bm;
	while (!(OSC.STATUS & OSC_RC32MRDY_bm)) {}
	CCP = CCP_IOREG_gc;
This topic has a solution.
Last Edited: Mon. Sep 25, 2017 - 10:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

	CCP = CCP_IOREG_gc;
	CLK.CTRL = CLK_SCLKSEL_PLL_gc;
	CCP = CCP_IOREG_gc; // <--- what is this for???

	/* Set the USB Clock to 48MHz from the 32MHz RC. */
	CLK.USBCTRL = CLK_USBSRC_RC32M_gc;

	OSC.DFLLCTRL = OSC_RC32MCREF_USBSOF_gc;
	NVM.CMD  = NVM_CMD_READ_CALIB_ROW_gc;
	DFLLRC32M.CALB = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSC));
	// what about DFLLRC32M.CALA ???

 

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

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

BTW, once you get the clock working correctly, what USB stack are you going to use?  ASF?  LUFA?

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

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

Hi,

 

No change even adding set of CALA register using the USBRCOSCA.

 

	/* The 2MHz RC is already enabled. */

	/* Set the System Clock to 32MHz from 2MHz RC + PLL. */
	OSC.PLLCTRL = OSC_PLLSRC_RC2M_gc | 16; /* 16*2MHz=32MHz. */
	OSC.CTRL |= OSC_PLLEN_bm;
	while (!(OSC.STATUS & OSC_PLLRDY_bm)) {}
	CCP = CCP_IOREG_gc;
	CLK.CTRL = CLK_SCLKSEL_PLL_gc;	
	
	/* Set the USB Clock to 48MHz from the 32MHz RC. */
	OSC.DFLLCTRL = OSC_RC32MCREF_USBSOF_gc;
	NVM.CMD  = NVM_CMD_READ_CALIB_ROW_gc;
	DFLLRC32M.CALA = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSCA));
	DFLLRC32M.CALB = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSC));
	DFLLRC32M.COMP1 = 0x1B;
	DFLLRC32M.COMP2 = 0xB7;
	DFLLRC32M.CTRL = DFLL_ENABLE_bm;

	CCP = CCP_IOREG_gc;
	OSC.CTRL = OSC_RC32MEN_bm | OSC_RC2MEN_bm;
	while (!(OSC.STATUS & OSC_RC32MRDY_bm)) {}

	CCP = CCP_IOREG_gc;
	CLK.USBCTRL = CLK_USBSRC_RC32M_gc | CLK_USBSEN_bm;

 

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

Hi,

 

I have removed the code related to the Calibration Row read, put in CALA and CALB the values I'm reading manually from the AVR ISP MKII.

And this is working fine, USB clock @48MHz and the system clock at 32MHz.

 

Now I tried to print out the values read, and I soon as I try to access the NVM I got the System clock change to 48MHz ???

 

Any idea why accessing the NVM is causing this issue?

 

Thanks,

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

Any idea why accessing the NVM is causing this issue?

 

No.

 

Have you altered the compiler optimization setting?   Are you using ASF modules?  How are you verifying the change in clock frequency?

 

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

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

I think having the system clock at 48MHz is not a problem.  You can divide that down to 24MHz or lower for the CPU/PER clock.

 

 

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

Hi,

 

Sorry for the late reply, so now I have (it seems) the clocks setup correctly, for this I hqve added the following code line:

NVM_CMD = NVM_CMD_NO_OPERATION_gc;

After the calibration read.

 

Full code:

void CLK_Init(void)
{
	/* Set the USB Clock to 48MHz from the 32MHz RC. */
	OSC.DFLLCTRL = OSC_RC32MCREF_USBSOF_gc;
	NVM.CMD  = NVM_CMD_READ_CALIB_ROW_gc;
	DFLLRC32M.CALA = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSCA));
	DFLLRC32M.CALB = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSC));
	NVM_CMD = NVM_CMD_NO_OPERATION_gc;
	DFLLRC32M.COMP1 = 0x1B;
	DFLLRC32M.COMP2 = 0xB7;
	DFLLRC32M.CTRL = DFLL_ENABLE_bm;

	CCP = CCP_IOREG_gc;
	OSC.CTRL = OSC_RC32MEN_bm | OSC_RC2MEN_bm;
	while (!(OSC.STATUS & OSC_RC32MRDY_bm)) {}

	OSC.PLLCTRL = OSC_PLLSRC_RC2M_gc | 16; /* 16*2MHz=32MHz. */

	CCP = CCP_IOREG_gc;
	OSC.CTRL = OSC_RC32MEN_bm | OSC_PLLEN_bm | OSC_RC2MEN_bm;
	while (!(OSC.STATUS & OSC_PLLRDY_bm)) {}

	DFLLRC2M.CTRL = DFLL_ENABLE_bm;

	CCP = CCP_IOREG_gc;
	CLK.CTRL = CLK_SCLKSEL_PLL_gc;
	CLK.PSCTRL = 0x00;

	return;
}

 

Now need to have USB working...

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

jhdecker said:

Now need to have USB working...

Greg_Muth said:

BTW, once you get the clock working correctly, what USB stack are you going to use?  ASF?  LUFA?

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

Last Edited: Mon. Sep 11, 2017 - 06:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The NVM command "no operation" is a misnomer.  It's the command needed to read flash.   I've had gcc put a jump table, or something like that in flash, unbeknownst to me.  When attempting to read flash the compiler assumes the command is "no operation".  If it isn't, strange things happen.  Sometimes I've used pgm_read_byte and I didn't realize I needed to check the NVM command.  So Atmel's advice is good.  Put the command back to no_operation when finished with the NVM controller. 

Last Edited: Mon. Sep 11, 2017 - 08:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sometimes you have to read the fine print.  smiley

 

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

Hi,

 

I'm writting my own stack, but I'm getting issues, so I will open another thread soon to request help from the community.