Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
bpaddock
PostPosted: Sep 05, 2009 - 03:11 PM
Hangaround


Joined: Sep 27, 2001
Posts: 207
Location: North of Pittsburgh

This brief tutorial shows several examples related to initializing a XMega:

* Shows how to use MPCMASK to setup up multiple I/O pins in a port simultaneously.

* Shows how to output the system clock on a pin so that it can be measured, to verify the code is producing the correct frequency. The XMega hardware limits this to bit-7 of a few specific ports.

* Shows how to configure the external 32 kHz oscillator, and run it while in Power Save sleep mode.

* Shows how to configure the 2 and 32 MHz internal oscillator, with the option of calibrating to the external 32 kHz oscillator.

* Shows how to configure the 16-bit Real Time Counter. Also shows the configuration to keep the RTC running while sleeping to save power.

* Shows how to enable the PLL at multiple frequencies via using the clock prescaller.

* Shows how to how to shutdown the JTAG while sleeping to save power.

* Shows how to use the CCPWrite function found in Application Note #1003.

The Configuration Change Protection requires a bit more explanation. In the XMega certain bits within a byte are considered "critical" to an applications function, and they can not be modified without running a timed code sequence to unlock them.

Naively written code will be written like this:
Code:

 CCP = CCP_IOREG_gc;
 IOreg = data;

That will most likely work most of the time. The problem is that the code sequence is context sensitive, and an optimizing compiler is free to reorder the code, which could then violate the critical timing, that allows the bits to be unlocked.

* Shows how to use the obscure C direction #error to flag incompatible options when they are selected.

* This code is also a good example of what to never do in production code. It has far to many conditional compiles.

* A few XMega items also worth mentioning is this previous XMega tutorial on how to read the XMega revision letter, serial number, and ADC/DAC calibration constants:

http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=82824&sid=3cd77b0090b4eb43cfbd180162d6a53a

Also that XMega Application Note avr1307 has a Excel file showing how to set up the XMega serial baud rates.

Finally note that AVRStudio versions prior to 4.17 have the XMega 128A1 BOD voltage selection off-by-one, so that it is possible to set a BOD voltage higher than Vcc. This has the potential to kill a board.

Code:

/*
 *
 * Atmel XMega 128A1 Clock/Oscillator, RTC setup examples.
 *
 * V1.1 Bob Paddock Sep/5/2009
 *
 * Compile with WinAVR-200901313 GCC 4.3.2 or newer.
 *
 *
 * This code assumes the XMega has a 32.768 kHz Watch Crystal
 * connected to PQ0 and PQ1.
 *
 * The set of defines below will allow running the XMega at
 * frequencies of 8.192 kHz, 32 kHz, 2 MHz, 4 MHz, 16 MHz and 32 MHz,
 * with options for auto calibration, and RTC to be enabled.  With no
 * options test runs at default 2 MHz.
 *
 */

#include <avr/io.h>        /* XMega 128A1 */
#include <compat/ina90.h>  /* _NOP() */

#include "ccpwrite.h"      /* From Application Note AVR1003, does IOREG only */
#include "clksys_driver.h" /* From Application Note AVR1003 */
#include "rtc_driver.h"    /* RTC_BUSY() from Application Note AVR1314 */

/* Settings as shown run at 2 MHz: */
#define USE_CLOCK_OUT_ON_D7  (0) /* Non-zero for clock out on PD7.  Do not set when measuring current */

#define USE_SLEEP            (0) /* Non-Zero to enter sleep, otherwise use empty NOP loop */
#define USE_RTC              (0) /* Non-zero to enable RTC at 1 kHz */

#define ENABLE_16MHZ_PLL     (0) /* Non-zero to run at 16 MHz using PLL */
#define ENABLE_32MHZ_PLL     (0) /* Non-zero to run at 32 MHz using PLL */
#define ENABLE_32MHZ_OSC     (0) /* Non-zero to run at 32 MHz using internal 32 MHz Osc. */
#define ENABLE_DIV_4         (0) /* Non-zero to divide Clk. by 4.  Will give 4 MHz with 16 MHz PLL, and 8 MHz with 32 MHz PLL/Osc., 8.912 kHz with 32 kHz Osc. */

#define USE_32kHz_CRYSTAL    (0) /* Non-zero to enable external 32.768 kHz Watch Crystal for auto calibration and running at 32 kHz */
#define USE_AUTO_CALIBRATION (0) /* Non-zero to use external crystal as reference for the internal 2 MHz Osc., not used at 32 kHz */
#define RUN_AT_32KHZ         (0) /* Non-zero to run from 32.768 kHz system clock */

/* Generate error message(s) if have incompatible options selected: */

#if( (ENABLE_16MHZ_PLL > 0) && (ENABLE_32MHZ_PLL > 0) )
#error Both 16 and 32 MHz settings may not be enabled simultaneously
#endif

#if( (ENABLE_32MHZ_OSC > 0) && ((ENABLE_16MHZ_PLL > 0) || (ENABLE_32MHZ_PLL > 0)) )
#error Program not set up to use PLL with 32 MHz osc.
#endif

#if( (USE_32kHz_CRYSTAL < 1) && (USE_AUTO_CALIBRATION > 0) )
#error USE_32kHz_CRYSTAL must be set to use Auto Calibration.
#endif

#if( RUN_AT_32KHZ > 0 )

#if( 0 == USE_32kHz_CRYSTAL )
#error USE_32kHz_CRYSTAL must be set to run at 32 kHz
#endif

#if( (ENABLE_16MHZ_PLL > 0) || (ENABLE_32MHZ_PLL > 0) || (ENABLE_32MHZ_OSC > 0) )
#error Other internal oscillators may not be enabled when running at 32 kHz
#endif

#if( USE_AUTO_CALIBRATION > 0 )
#error Can not use auto calibration when running at 32 khz.
#endif

#endif /* #if( RUN_AT_32KHZ > 0 ) */

int main( void )
{
 /*
  * Reset has all port pins as inputs.
  * Set all pins to have internal pull-ups:
  */
  PORTCFG.MPCMASK = (uint8_t) -1; /* MPCMASK register is cleared automatically after the write operation to the pin configuration registers is finished. */
  PORTA.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTB.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTC.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTD.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTE.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTF.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTH.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTJ.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTK.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTQ.PIN0CTRL = PORT_OPC_PULLUP_gc; /* PQ0 and PQ1 have 32.768 kHz Watch Crystal connected */

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTR.PIN0CTRL = PORT_OPC_PULLUP_gc;

#if( USE_CLOCK_OUT_ON_D7 > 0 )
  PORTD.DIRSET =  _BV(7);                   /* To output clock, port must be set as output */
  PORTCFG.CLKEVOUT = PORTCFG_CLKOUT_PD7_gc; /* Output the clock frequency on PD7 to measure on Counter/Scope */
#endif

#if( USE_32kHz_CRYSTAL > 0 )
  CLKSYS_XOSC_Config( 0, false, OSC_XOSCSEL_32KHz_gc );
  CLKSYS_Enable( OSC_XOSCEN_bm );        /* Does an OR with current Osc bits */
  do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 ); /* 32 kHz Does AND of the bits */
#endif


  /* At Reset, the main clock source will already be the 2 MHz Osc. */
#if( ENABLE_32MHZ_OSC > 0)
  CLKSYS_Enable( OSC_RC32MEN_bm );                        /* Enable 32 MHz Osc. */
  do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); /* Wait for 32 MHz Osc. to stabilize */
#else/* ENABLE_2MHZ_OSC: */
  CLKSYS_Enable( OSC_RC2MEN_bm );/* Does an OR with current Osc bits.  Enabled by XMega reset, not actually required here */
  do {} while ( CLKSYS_IsReady( OSC_RC2MRDY_bm ) == 0 ); /* Wait for 2 MHz Osc. to stabilize */
#endif


#if( (USE_32kHz_CRYSTAL > 0) && (USE_AUTO_CALIBRATION > 0) )

#if( ENABLE_32MHZ_OSC > 0 )
  CLKSYS_AutoCalibration_Enable( OSC_RC32MCREF_bm, true ); /* 32MHz Osc. True= external crystal used as reference */
#else
  CLKSYS_AutoCalibration_Enable( OSC_RC2MCREF_bm, true );  /* 2MHz Osc.  True= external crystal used as reference */
#endif

#endif


#if( USE_RTC > 0 )
  CLKSYS_RTC_ClockSource_Enable( CLK_RTCSRC_TOSC_gc ); /* RTC source is 1024 Hz from 32kHz crystal oscillator on TOSC */
  while( RTC_Busy() ) /* Wait for the RTC clock domain to synchronize with the CPU clock domain */
    ;
  RTC.PER  = 2000;
  RTC.COMP = 1000;
  RTC.CNT  = 0;
  //  RTC.CTRL = RTC_PRESCALER_DIV1024_gc; /* Rollover in 18 hours, 12 minutes, and 15 seconds */
  RTC.CTRL = RTC_PRESCALER_DIV1_gc; /* Rollover at ~64 seconds */
#endif


#if( ENABLE_16MHZ_PLL > 0 )
  CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 8 );             /* 2MHz * 8 = 16 MHz.  PLL Min is 10 MHz */
#endif

#if( ENABLE_32MHZ_PLL > 0 )
  CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 16 );            /* 2MHz * 16 = 32 MHz.  PLL Min is 10 MHz */
#endif

#if( (ENABLE_16MHZ_PLL > 0) || (ENABLE_32MHZ_PLL > 0) )
  CLKSYS_Enable( OSC_PLLEN_bm );
  do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 );   /* Wait for PLL to be ready */
  CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc );   /* Switch main clock PLL */
#endif

#if( ENABLE_32MHZ_OSC > 0 )
  CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); /* Switch main clock 32 MHz Osc. */
  (void) CLKSYS_Disable( OSC_RC2MEN_bm );                 /* Disable the 2 MHz osc now that we are not using it */
#endif

#if( RUN_AT_32KHZ > 0 )

#if( USE_AUTO_CALIBRATION > 0 )

#if( ENABLE_32MHZ_OSC > 0 )
  CLKSYS_AutoCalibration_Disable( DFLLRC32M ); /* Turn off the 32MHz Auto Calibration so we can switch Osc. to 32 kHz */
#else
  CLKSYS_AutoCalibration_Disable( DFLLRC2M );  /* Turn off the  2MHz Auto Calibration so we can switch Osc. to 32 kHz */
#endif

#endif/* #if( USE_AUTO_CALIBRATION > 0 ) */

  CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_XOSC_gc );  /* Switch external 32 kHz watch crystal */
  (void) CLKSYS_Disable( (OSC_RC32MEN_bm|OSC_RC2MEN_bm) );/* Disable the internal RC Oscillators now that we are not using them */

#endif/* #if( RUN_AT_32KHZ > 0 ) */

#if( ENABLE_DIV_4 > 0 )
  CLKSYS_Prescalers_Config( CLK_PSADIV_4_gc, CLK_PSBCDIV_1_1_gc ); /* CLKper = CLKcpu = Frq/4.  PrescalerC drives CLKcpu */
#endif

#if( USE_SLEEP > 0 )
  CCPWrite(&MCU.MCUCR, MCU_JTAGD_bm);    /* Disables JTAG during sleep */

  SLEEP.CTRL  = (SLEEP_SMODE_PSAVE_gc|SLEEP_SEN_bm);    /* Set sleep mode to Power Save, asynchronous I/O wake-up, RTC runs if enabled, enable Sleep instruction */
#endif

  for(;;)
    {
#if( USE_SLEEP > 0 )
      sleep_cpu();                           /* AVR Sleep instruction */
#else
      _NOP();
#endif
    }
}/* main() */
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
js
PostPosted: Nov 23, 2009 - 01:36 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 22224
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Bob
Code:
#include "ccpwrite.h"      /* From Application Note AVR1003, does IOREG only */
ain't there, where did you get it from? I'm down to 16 errors now mostly EEPROM related.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bpaddock
PostPosted: Nov 23, 2009 - 01:55 AM
Hangaround


Joined: Sep 27, 2001
Posts: 207
Location: North of Pittsburgh

js wrote:
Bob
Code:
#include "ccpwrite.h"      /* From Application Note AVR1003, does IOREG only */
ain't there, where did you get it from? I'm down to 16 errors now mostly EEPROM related.


Yes it is. It is the very first function in
clksys_driver.c in AVR1003.zip
http://www.atmel.com/dyn/general/tech_doc.asp?doc_id=11479

ccpwrite.h/.c is that function with all of the non-GCC
compiler stuff striped out. Make your own version
of the compiler of your choice.

I did not think this code used EEPROM functions?
I was using AVR-LibC from CVS to for the XMega EEPROM
stuff. Might be your EEPROM issue. The next release
of WinAVR will have those in them. If you don't
want to wait you can copy the EEPROM specific files
in to your project and build them that way.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
js
PostPosted: Nov 23, 2009 - 02:43 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 22224
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Sorry but I have downloaded that appnote a few times today and it's not there. See screen shot below. Also I don't see that file being included anywhere in clksys_driver.c
Code:
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
#include "clksys_driver.h"

/*! \brief CCP write helper function written in assembly.
 *
 *  This function is written in assembly because of the timecritial
 *  operation of writing to the registers.
 *
 *  \param address A pointer to the address to write to.
 *  \param value   The value to put in to the register.
 */
void CCPWrite( volatile uint8_t * address, uint8_t value )
{
#ifdef __ICCAVR__

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bpaddock
PostPosted: Nov 23, 2009 - 12:44 PM
Hangaround


Joined: Sep 27, 2001
Posts: 207
Location: North of Pittsburgh

js wrote:
Code:

/*! \brief CCP write helper function written in assembly.
 *
 *  This function is written in assembly because of the timecritial
 *  operation of writing to the registers.
 *
 *  \param address A pointer to the address to write to.
 *  \param value   The value to put in to the register.
 */
void CCPWrite( volatile uint8_t * address, uint8_t value )
{
#ifdef __ICCAVR__
You quoted the function that you say you can't find.

Take that function and put in ccpwrite.h/.c.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
js
PostPosted: Nov 23, 2009 - 07:53 PM
10k+ Postman


Joined: Mar 28, 2001
Posts: 22224
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Quote:
You quoted the function

Confused confused..so it is NOT a file that comes with AVR1003 but one you made up yourself from AVR1003.

I wonder if anyone else has tried this code and come up with the same problems? I will also try to find out what's pulling in the unused eeprom code.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
js
PostPosted: Nov 23, 2009 - 10:34 PM
10k+ Postman


Joined: Mar 28, 2001
Posts: 22224
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Ok got it running and tried a few combination. To get it to work:
Comment out these next 2 lines
Code:
//#include <compat/ina90.h>  /* _NOP() */

//#include "ccpwrite.h"      /* From Application Note AVR1003, does IOREG only */
change the following line at the end of the file
Code:
//      _NOP();
   asm ("nop");
add clksys_driver.c to the project as well as putting the next 3 files in the project folder
avr_compiler.h
clksys_driver.h
rtc_driver.h

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
kubark42
PostPosted: Dec 11, 2009 - 12:42 PM
Hangaround


Joined: Jun 04, 2008
Posts: 256


As js noticed, this tutorial is somewhat broken. Which is a shame, because it's otherwise well written. Here is a fixed version, with an upgrade to include the internal 32.768 calibrated RC oscillator.

To get this working out of the box on AVRStudio, make sure you:

Quote:
1) add clksys_driver.c to the project

2)put the next 3 files in the project folder:
avr_compiler.h
clksys_driver.h
rtc_driver.h


Code:
/*
 *
 * Atmel XMega 128A1 Clock/Oscillator, RTC setup examples.
 *
 * V1.1 Bob Paddock Sep/5/2009
 *
 * Compile with WinAVR-200901313 GCC 4.3.2 or newer.
 *
 *
 * This code assumes the XMega has a 32.768 kHz Watch Crystal
 * connected to PQ0 and PQ1.
 *
 * The set of defines below will allow running the XMega at
 * frequencies of 8.192 kHz, 32 kHz, 2 MHz, 4 MHz, 16 MHz and 32 MHz,
 * with options for auto calibration, and RTC to be enabled.  With no
 * options test runs at default 2 MHz.
 *
 */

#include <avr/io.h>        /* XMega 128A1 */

#include "clksys_driver.h" /* From Application Note AVR1003 */
#include "rtc_driver.h"    /* RTC_BUSY() from Application Note AVR1314 */

/* Settings as shown run at 2 MHz: */
#define USE_CLOCK_OUT_ON_D7  (1) /* Non-zero for clock out on PD7.  Do not set when measuring current */

#define USE_SLEEP            (0) /* Non-Zero to enter sleep, otherwise use empty NOP loop */
#define USE_RTC              (1) /* Non-zero to enable RTC at 1 kHz */

#define ENABLE_16MHZ_PLL     (0) /* Non-zero to run at 16 MHz using PLL */
#define ENABLE_32MHZ_PLL     (0) /* Non-zero to run at 32 MHz using PLL */
#define ENABLE_32MHZ_OSC     (1) /* Non-zero to run at 32 MHz using internal 32 MHz Osc. */
#define ENABLE_DIV_4         (0) /* Non-zero to divide Clk. by 4.  Will give 4 MHz with 16 MHz PLL, and 8 MHz with 32 MHz PLL/Osc., 8.912 kHz with 32 kHz Osc. */

#define USE_32kHz_RC         (1) /* Non-zero to enable internal 32.768 kHz RC for auto calibration and running at 32 kHz */
#define USE_32kHz_CRYSTAL    (0) /* Non-zero to enable external 32.768 kHz Watch Crystal for auto calibration and running at 32 kHz */
#define USE_AUTO_CALIBRATION (1) /* Non-zero to use external crystal as reference for the internal 2 MHz Osc., not used at 32 kHz */
#define RUN_AT_32KHZ         (0) /* Non-zero to run from 32.768 kHz system clock */

/* Generate error message(s) if have incompatible options selected: */

#if( (ENABLE_16MHZ_PLL > 0) && (ENABLE_32MHZ_PLL > 0) )
#error Both 16 and 32 MHz settings may not be enabled simultaneously
#endif

#if( (ENABLE_32MHZ_OSC > 0) && ((ENABLE_16MHZ_PLL > 0) || (ENABLE_32MHZ_PLL > 0)) )
#error Program not set up to use PLL with 32 MHz osc.
#endif

#if( (USE_32kHz_CRYSTAL < 1 && USE_32kHz_RC < 1) && (USE_AUTO_CALIBRATION > 0) )
#error USE_32kHz_CRYSTAL must be set to use Auto Calibration.
#endif

#if( RUN_AT_32KHZ > 0 )

#if( 0 == USE_32kHz_CRYSTAL && 0 == USE_32kHz_RC)
#error USE_32kHz_CRYSTAL must be set to run at 32 kHz
#endif

#if( (ENABLE_16MHZ_PLL > 0) || (ENABLE_32MHZ_PLL > 0) || (ENABLE_32MHZ_OSC > 0) )
#error Other internal oscillators may not be enabled when running at 32 kHz
#endif

#if( USE_AUTO_CALIBRATION > 0 )
#error Can not use auto calibration when running at 32 khz.
#endif

#endif /* #if( RUN_AT_32KHZ > 0 ) */

int main( void )
{
 /*
  * Reset has all port pins as inputs.
  * Set all pins to have internal pull-ups:
  */
  PORTCFG.MPCMASK = (uint8_t) -1; /* MPCMASK register is cleared automatically after the write operation to the pin configuration registers is finished. */
  PORTA.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTB.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTC.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTD.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTE.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTF.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTH.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTJ.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTK.PIN0CTRL = PORT_OPC_PULLUP_gc;

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTQ.PIN0CTRL = PORT_OPC_PULLUP_gc; /* PQ0 and PQ1 have 32.768 kHz Watch Crystal connected */

  PORTCFG.MPCMASK = (uint8_t) -1;
  PORTR.PIN0CTRL = PORT_OPC_PULLUP_gc;

#if( USE_CLOCK_OUT_ON_D7 > 0 )
  PORTD.DIRSET =  _BV(7);                   /* To output clock, port must be set as output */
  PORTCFG.CLKEVOUT = PORTCFG_CLKOUT_PD7_gc; /* Output the clock frequency on PD7 to measure on Counter/Scope */
#endif

#if( USE_32kHz_CRYSTAL > 0 )
  CLKSYS_XOSC_Config( 0, false, OSC_XOSCSEL_32KHz_gc );
  CLKSYS_Enable( OSC_XOSCEN_bm );        /* Does an OR with current Osc bits */
  do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 ); /* 32 kHz Does AND of the bits */
#endif

#if( USE_32kHz_RC > 0 )
   /* Turn on internal 32kHz. */
   OSC.CTRL |= OSC_RC32KEN_bm;
   /* Wait for the 32kHz oscillator to stabilize. */
   while ( ( OSC.STATUS & OSC_RC32KRDY_bm ) == 0);
#endif


  /* At Reset, the main clock source will already be the 2 MHz Osc. */
#if( ENABLE_32MHZ_OSC > 0)
  CLKSYS_Enable( OSC_RC32MEN_bm );                        /* Enable 32 MHz Osc. */
  do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); /* Wait for 32 MHz Osc. to stabilize */
#else/* ENABLE_2MHZ_OSC: */
  CLKSYS_Enable( OSC_RC2MEN_bm );/* Does an OR with current Osc bits.  Enabled by XMega reset, not actually required here */
  do {} while ( CLKSYS_IsReady( OSC_RC2MRDY_bm ) == 0 ); /* Wait for 2 MHz Osc. to stabilize */
#endif


#if( (USE_32kHz_CRYSTAL > 0 || USE_32kHz_RC > 0) && (USE_AUTO_CALIBRATION > 0) )

#if( ENABLE_32MHZ_OSC > 0 )
  CLKSYS_AutoCalibration_Enable( OSC_RC32MCREF_bm, true ); /* 32MHz Osc. True= external crystal used as reference */
#else
  CLKSYS_AutoCalibration_Enable( OSC_RC2MCREF_bm, true );  /* 2MHz Osc.  True= external crystal used as reference */
#endif

#endif


#if( USE_RTC > 0 )
  CLKSYS_RTC_ClockSource_Enable( CLK_RTCSRC_TOSC_gc ); /* RTC source is 1024 Hz from 32kHz crystal oscillator on TOSC */
  while( RTC_Busy() ) /* Wait for the RTC clock domain to synchronize with the CPU clock domain */
    ;
  RTC.PER  = 2000;
  RTC.COMP = 1000;
  RTC.CNT  = 0;
  //  RTC.CTRL = RTC_PRESCALER_DIV1024_gc; /* Rollover in 18 hours, 12 minutes, and 15 seconds */
  RTC.CTRL = RTC_PRESCALER_DIV1_gc; /* Rollover at ~64 seconds */
#endif


#if( ENABLE_16MHZ_PLL > 0 )
  CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 8 );             /* 2MHz * 8 = 16 MHz.  PLL Min is 10 MHz */
#endif

#if( ENABLE_32MHZ_PLL > 0 )
  CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 16 );            /* 2MHz * 16 = 32 MHz.  PLL Min is 10 MHz */
#endif

#if( (ENABLE_16MHZ_PLL > 0) || (ENABLE_32MHZ_PLL > 0) )
  CLKSYS_Enable( OSC_PLLEN_bm );
  do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 );   /* Wait for PLL to be ready */
  CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc );   /* Switch main clock PLL */
#endif

#if( ENABLE_32MHZ_OSC > 0 )
  CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); /* Switch main clock 32 MHz Osc. */
  (void) CLKSYS_Disable( OSC_RC2MEN_bm );                 /* Disable the 2 MHz osc now that we are not using it */
#endif

#if( RUN_AT_32KHZ > 0 )

   #if( USE_AUTO_CALIBRATION > 0 )

      #if( ENABLE_32MHZ_OSC > 0 )
  CLKSYS_AutoCalibration_Disable( DFLLRC32M ); /* Turn off the 32MHz Auto Calibration so we can switch Osc. to 32 kHz */
      #else
  CLKSYS_AutoCalibration_Disable( DFLLRC2M );  /* Turn off the  2MHz Auto Calibration so we can switch Osc. to 32 kHz */
      #endif
   #endif/* #if( USE_AUTO_CALIBRATION > 0 ) */

   #if( USE_32kHz_CRYSTAL > 0 )
  CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_XOSC_gc );  /* Switch external 32 kHz watch crystal */
  (void) CLKSYS_Disable( (OSC_RC32MEN_bm|OSC_RC2MEN_bm) );/* Disable the internal RC Oscillators now that we are not using them */
   #endif

   #if( USE_32kHz_RC > 0 )
   CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32K_gc );
   CLKSYS_Disable( OSC_XOSCEN_bm );
   #endif

#endif/* #if( RUN_AT_32KHZ > 0 ) */

#if( ENABLE_DIV_4 > 0 )
  CLKSYS_Prescalers_Config( CLK_PSADIV_4_gc, CLK_PSBCDIV_1_1_gc ); /* CLKper = CLKcpu = Frq/4.  PrescalerC drives CLKcpu */
#endif

#if( USE_SLEEP > 0 )
  CCPWrite(&MCU.MCUCR, MCU_JTAGD_bm);    /* Disables JTAG during sleep */

  SLEEP.CTRL  = (SLEEP_SMODE_PSAVE_gc|SLEEP_SEN_bm);    /* Set sleep mode to Power Save, asynchronous I/O wake-up, RTC runs if enabled, enable Sleep instruction */
#endif

  for(;;)
    {
#if( USE_SLEEP > 0 )
      sleep_cpu();                           /* AVR Sleep instruction */
#else
   asm ("nop");
#endif
    }
}/* main() */
 
 View user's profile Send private message  
Reply with quote Back to top
Eng.Ahmed
PostPosted: Feb 17, 2012 - 09:43 PM
Newbie


Joined: Nov 23, 2011
Posts: 1
Location: Cairo, Egypt

Can I just use:
Code:

void setClockTo32MHz()
{
    CCP = CCP_IOREG_gc;              // disable register security for oscillator update
    OSC.CTRL = OSC_RC32MEN_bm;       // enable 32MHz oscillator
    while(!(OSC.STATUS & OSC_RC32MRDY_bm)); // wait for oscillator to be ready
    CCP = CCP_IOREG_gc;              // disable register security for clock update
    CLK.CTRL = CLK_SCLKSEL_RC32M_gc; // switch to 32MHz clock
}

to set the internal clock to 32 MHz?
 
 View user's profile Send private message  
Reply with quote Back to top
ashigupta
PostPosted: Jul 08, 2013 - 08:28 AM
Rookie


Joined: Dec 03, 2012
Posts: 29
Location: Mumbai, India

Hi,

I am trying to interface Atxmega32 with DS1337(I2C) real time clock for date and time functionality. I am using wire library provided by akafuino for xmega board and DS1337 arduino library for this functionality.

But this is not working. Can u help me how to setup real time clock functionality with xmega32 as i need updated time records even if my controller is not powered.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jul 08, 2013 - 08:39 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 69309
Location: (using avr-gcc in) Finchingfield, Essex, England

Your post is off topic for this thread as it's about configuring the Xmega's internal peripherals not external devices such as DS1337. You will find other threads about using Arduino wire.h and DS1337 in the AVR Forum so suggest you post there.

Moderator.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
ashigupta
PostPosted: Jul 08, 2013 - 09:37 AM
Rookie


Joined: Dec 03, 2012
Posts: 29
Location: Mumbai, India

Ok thank you.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits