Xmega Application Note | |||||
Functions | |
static void | osc_enable_external (uint8_t id) |
static void | osc_enable_internal (uint8_t id) |
Enable internal oscillator id. | |
Oscillator Management | |
| |
static void | osc_disable (uint8_t id) |
Disable oscillator id. | |
static void | osc_enable (uint8_t id) |
Enable oscillator id. | |
static uint32_t | osc_get_rate (uint8_t id) |
Return the frequency of oscillator id in Hz. | |
static bool | osc_is_ready (uint8_t id) |
Determine whether oscillator id is ready. | |
static void | osc_wait_ready (uint8_t id) |
Wait until the oscillator identified by id is ready. | |
XMEGA-Specific Oscillator Features | |
| |
static void | osc_disable_autocalibration (uint8_t id) |
Disable DFLL-based automatic calibration of an internal oscillator. | |
static void | osc_enable_autocalibration (uint8_t id, uint8_t ref_id) |
Enable DFLL-based automatic calibration of an internal oscillator. | |
static void | osc_user_calibration (uint8_t id, uint16_t calib) |
Load a specific calibration value for the specified oscillator. | |
OSC32 mode values | |
| |
#define | OSC32_MODE_EXTERNAL AVR32_PM_OSCCTRL32_MODE_EXT_CLOCK |
External clock connected to XIN32. | |
#define | OSC32_MODE_XTAL AVR32_PM_OSCCTRL32_MODE_CRYSTAL |
Crystal connected to XIN32/XOUT32. Use automatic gain control. | |
OSC32 startup values | |
| |
#define | OSC32_STARTUP_0 AVR32_PM_OSCCTRL32_STARTUP_0_RCOSC |
0 cycles | |
#define | OSC32_STARTUP_128 AVR32_PM_OSCCTRL32_STARTUP_128_RCOSC |
128 cycles (1.1 ms) | |
#define | OSC32_STARTUP_131072 AVR32_PM_OSCCTRL32_STARTUP_131072_RCOSC |
131072 cycles (1.1 s) | |
#define | OSC32_STARTUP_16384 AVR32_PM_OSCCTRL32_STARTUP_16384_RCOSC |
16384 cycles (143 ms) | |
#define | OSC32_STARTUP_262144 AVR32_PM_OSCCTRL32_STARTUP_262144_RCOSC |
262144 cycles (2.3 s) | |
#define | OSC32_STARTUP_524288 AVR32_PM_OSCCTRL32_STARTUP_524288_RCOSC |
524288 cycles (4.6 s) | |
#define | OSC32_STARTUP_65536 AVR32_PM_OSCCTRL32_STARTUP_65536_RCOSC |
65536 cycles (570 ms) | |
#define | OSC32_STARTUP_8192 AVR32_PM_OSCCTRL32_STARTUP_8192_RCOSC |
8192 cycles (72.3 ms) | |
Oscillator identifiers | |
| |
#define | OSC_ID_OSC0 0 |
External Oscillator 0. | |
#define | OSC_ID_OSC1 1 |
External Oscillator 1. | |
#define | OSC_ID_OSC32 2 |
External 32 kHz oscillator. | |
Oscillator identifiers | |
| |
#define | OSC_ID_RC2MHZ OSC_RC2MEN_bm |
2 MHz Internal RC Oscillator | |
#define | OSC_ID_RC32KHZ OSC_RC32KEN_bm |
32 KHz Internal RC Oscillator | |
#define | OSC_ID_RC32MHZ OSC_RC32MEN_bm |
32 MHz Internal RC Oscillator | |
#define | OSC_ID_USBSOF 0xff |
Reference from USB Start Of Frame. | |
#define | OSC_ID_XOSC OSC_XOSCEN_bm |
External Oscillator. | |
OSC0/OSC1 mode values | |
| |
#define | OSC_MODE_EXTERNAL AVR32_PM_MODE_EXT_CLOCK |
External clock connected to XIN. | |
#define | OSC_MODE_XTAL_G0 AVR32_PM_MODE_CRYSTAL_G0 |
Crystal connected to XIN/XOUT. Use oscillator gain G0 (400 kHz to 900 kHz). | |
#define | OSC_MODE_XTAL_G1 AVR32_PM_MODE_CRYSTAL_G1 |
Crystal connected to XIN/XOUT. Use oscillator gain G1 (900 kHz to 3 MHz). | |
#define | OSC_MODE_XTAL_G2 AVR32_PM_MODE_CRYSTAL_G2 |
Crystal connected to XIN/XOUT. Use oscillator gain G2 (3 MHz to 8 MHz). | |
#define | OSC_MODE_XTAL_G3 AVR32_PM_MODE_CRYSTAL_G3 |
Crystal connected to XIN/XOUT. Use oscillator gain G3 (8 MHz and higher). | |
Slow clock frequency limits | |
The slow clock is an internal RC oscillator whose frequency may drift a bit as a result of temperature changes. These definitions provide upper and lower limits which may be used to calculate upper and lower limits of timeouts, derived clock frequencies, etc. | |
#define | OSC_SLOW_MAX_HZ 120000 |
Maximum frequency of the slow clock in Hz. | |
#define | OSC_SLOW_MIN_HZ 100000 |
Minimum frequency of the slow clock in Hz. | |
#define | OSC_SLOW_NOMINAL_HZ AVR32_PM_RCOSC_FREQUENCY |
Nominal frequency of the slow clock in Hz. | |
OSC0/OSC1 startup values | |
| |
#define | OSC_STARTUP_0 AVR32_PM_OSCCTRL0_STARTUP_0_RCOSC |
0 cycles | |
#define | OSC_STARTUP_128 AVR32_PM_OSCCTRL0_STARTUP_128_RCOSC |
128 cycles (1.1 ms) | |
#define | OSC_STARTUP_16384 AVR32_PM_OSCCTRL0_STARTUP_16384_RCOSC |
16384 cycles (142 ms) | |
#define | OSC_STARTUP_2048 AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC |
2048 cycles (18 ms) | |
#define | OSC_STARTUP_4096 AVR32_PM_OSCCTRL0_STARTUP_4096_RCOSC |
4096 cycles (36 ms) | |
#define | OSC_STARTUP_64 AVR32_PM_OSCCTRL0_STARTUP_64_RCOSC |
64 cycles (560 us) | |
#define | OSC_STARTUP_8192 AVR32_PM_OSCCTRL0_STARTUP_8192_RCOSC |
8192 cycles (71 ms) | |
XTAL resonator frequency range | |
| |
#define | XOSC_RANGE_04TO2 OSC_FRQRANGE_04TO2_gc |
0.4 to 2 MHz frequency range | |
#define | XOSC_RANGE_12TO16 |
12 to 16 MHz frequency range | |
#define | XOSC_RANGE_2TO9 OSC_FRQRANGE_2TO9_gc |
2 to 9 MHz frequency range | |
#define | XOSC_RANGE_9TO12 OSC_FRQRANGE_9TO12_gc |
9 to 12 MHz frequency range | |
XTAL resonator start-up cycles | |
| |
#define | XOSC_STARTUP_1024 1 |
1 k cycle start-up time | |
#define | XOSC_STARTUP_16384 2 |
16 k cycle start-up time | |
#define | XOSC_STARTUP_256 0 |
256 cycle start-up time | |
External oscillator types | |
| |
#define | XOSC_TYPE_32KHZ 2 |
32.768 kHz resonator on TOSC | |
#define | XOSC_TYPE_EXTERNAL 0 |
External clock signal. | |
#define | XOSC_TYPE_XTAL 3 |
0.4 to 16 MHz resonator on XTAL |
This group contains functions and definitions related to configuring and enabling/disabling on-chip oscillators. Internal RC-oscillators, external crystal oscillators and external clock generators are supported by this module. What all of these have in common is that they swing at a fixed, nominal frequency which is normally not adjustable.
The following example demonstrates how to enable the external oscillator on XMEGA A and wait for it to be ready to use. The oscillator identifiers are platform-specific, so while the same procedure is used on all platforms, the parameter to osc_enable() will be different from device to device.
If external oscillators are used, the board code must provide the following definitions for each of those:
#define OSC32_MODE_EXTERNAL AVR32_PM_OSCCTRL32_MODE_EXT_CLOCK |
#define OSC32_MODE_XTAL AVR32_PM_OSCCTRL32_MODE_CRYSTAL |
#define OSC32_STARTUP_0 AVR32_PM_OSCCTRL32_STARTUP_0_RCOSC |
#define OSC32_STARTUP_128 AVR32_PM_OSCCTRL32_STARTUP_128_RCOSC |
#define OSC32_STARTUP_131072 AVR32_PM_OSCCTRL32_STARTUP_131072_RCOSC |
#define OSC32_STARTUP_16384 AVR32_PM_OSCCTRL32_STARTUP_16384_RCOSC |
#define OSC32_STARTUP_262144 AVR32_PM_OSCCTRL32_STARTUP_262144_RCOSC |
#define OSC32_STARTUP_524288 AVR32_PM_OSCCTRL32_STARTUP_524288_RCOSC |
#define OSC32_STARTUP_65536 AVR32_PM_OSCCTRL32_STARTUP_65536_RCOSC |
#define OSC32_STARTUP_8192 AVR32_PM_OSCCTRL32_STARTUP_8192_RCOSC |
#define OSC_ID_RC2MHZ OSC_RC2MEN_bm |
2 MHz Internal RC Oscillator
Definition at line 56 of file osc.h.
Referenced by osc_disable_autocalibration(), osc_enable_autocalibration(), osc_get_rate(), osc_user_calibration(), and sysclk_init().
#define OSC_ID_RC32KHZ OSC_RC32KEN_bm |
32 KHz Internal RC Oscillator
Definition at line 60 of file osc.h.
Referenced by osc_enable_autocalibration(), osc_get_rate(), and sysclk_init().
#define OSC_ID_RC32MHZ OSC_RC32MEN_bm |
32 MHz Internal RC Oscillator
Definition at line 58 of file osc.h.
Referenced by osc_disable_autocalibration(), osc_enable_autocalibration(), osc_get_rate(), osc_user_calibration(), and sysclk_init().
#define OSC_ID_USBSOF 0xff |
Reference from USB Start Of Frame.
Definition at line 68 of file osc.h.
Referenced by osc_disable(), osc_enable_autocalibration(), osc_enable_internal(), osc_get_rate(), and osc_is_ready().
#define OSC_ID_XOSC OSC_XOSCEN_bm |
External Oscillator.
Definition at line 62 of file osc.h.
Referenced by osc_enable(), osc_enable_autocalibration(), osc_get_rate(), pll_get_default_rate_priv(), and sysclk_init().
#define OSC_MODE_EXTERNAL AVR32_PM_MODE_EXT_CLOCK |
#define OSC_MODE_XTAL_G0 AVR32_PM_MODE_CRYSTAL_G0 |
#define OSC_MODE_XTAL_G1 AVR32_PM_MODE_CRYSTAL_G1 |
#define OSC_MODE_XTAL_G2 AVR32_PM_MODE_CRYSTAL_G2 |
#define OSC_MODE_XTAL_G3 AVR32_PM_MODE_CRYSTAL_G3 |
#define OSC_SLOW_MAX_HZ 120000 |
#define OSC_SLOW_MIN_HZ 100000 |
#define OSC_SLOW_NOMINAL_HZ AVR32_PM_RCOSC_FREQUENCY |
#define OSC_STARTUP_0 AVR32_PM_OSCCTRL0_STARTUP_0_RCOSC |
#define OSC_STARTUP_128 AVR32_PM_OSCCTRL0_STARTUP_128_RCOSC |
#define OSC_STARTUP_16384 AVR32_PM_OSCCTRL0_STARTUP_16384_RCOSC |
#define OSC_STARTUP_2048 AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC |
#define OSC_STARTUP_4096 AVR32_PM_OSCCTRL0_STARTUP_4096_RCOSC |
#define OSC_STARTUP_64 AVR32_PM_OSCCTRL0_STARTUP_64_RCOSC |
#define OSC_STARTUP_8192 AVR32_PM_OSCCTRL0_STARTUP_8192_RCOSC |
#define XOSC_RANGE_04TO2 OSC_FRQRANGE_04TO2_gc |
#define XOSC_RANGE_2TO9 OSC_FRQRANGE_2TO9_gc |
#define XOSC_RANGE_9TO12 OSC_FRQRANGE_9TO12_gc |
static void osc_disable | ( | uint8_t | id | ) | [inline, static] |
Disable oscillator id.
Definition at line 237 of file osc.h.
References Assert, cpu_irq_restore(), cpu_irq_save(), and OSC_ID_USBSOF.
Referenced by sysclk_init().
00238 { 00239 irqflags_t flags; 00240 00241 Assert(id != OSC_ID_USBSOF); 00242 00243 flags = cpu_irq_save(); 00244 OSC.CTRL &= ~id; 00245 cpu_irq_restore(flags); 00246 }
static void osc_disable_autocalibration | ( | uint8_t | id | ) | [inline, static] |
Disable DFLL-based automatic calibration of an internal oscillator.
id | The ID of the oscillator for which to disable auto-calibration:
|
Definition at line 341 of file osc.h.
References OSC_ID_RC2MHZ, and OSC_ID_RC32MHZ.
00342 { 00343 switch (id) { 00344 case OSC_ID_RC2MHZ: 00345 DFLLRC2M.CTRL = 0; 00346 break; 00347 00348 case OSC_ID_RC32MHZ: 00349 DFLLRC32M.CTRL = 0; 00350 break; 00351 00352 default: 00353 // unhandled_case(id); 00354 break; 00355 } 00356 }
static void osc_enable | ( | uint8_t | id | ) | [inline, static] |
Enable oscillator id.
The startup time and mode value is automatically determined based on definitions in the board code.
Definition at line 384 of file osc.h.
References osc_enable_external(), osc_enable_internal(), and OSC_ID_XOSC.
Referenced by sysclk_init().
00385 { 00386 if (id != OSC_ID_XOSC) { 00387 osc_enable_internal(id); 00388 } else { 00389 osc_enable_external(id); 00390 } 00391 }
static void osc_enable_autocalibration | ( | uint8_t | id, | |
uint8_t | ref_id | |||
) | [inline, static] |
Enable DFLL-based automatic calibration of an internal oscillator.
The XMEGA features two Digital Frequency Locked Loops (DFLLs) which can be used to improve the accuracy of the 2 MHz and 32 MHz internal RC oscillators. The DFLL compares the oscillator frequency with a more accurate reference clock to do automatic run-time calibration of the oscillator.
This function enables auto-calibration for either the 2 MHz or 32 MHz internal oscillator using either the 32.768 kHz calibrated internal oscillator or an external crystal oscillator as a reference. If the latter option is used, the crystal must be connected to the TOSC pins and run at 32.768 kHz.
id | The ID of the oscillator for which to enable auto-calibration:
| |
ref_id | The ID of the oscillator to use as a reference:
|
Definition at line 282 of file osc.h.
References Assert, cpu_irq_restore(), cpu_irq_save(), OSC_ID_RC2MHZ, OSC_ID_RC32KHZ, OSC_ID_RC32MHZ, OSC_ID_USBSOF, and OSC_ID_XOSC.
Referenced by sysclk_init().
00283 { 00284 irqflags_t flags; 00285 00286 flags = cpu_irq_save(); 00287 switch (id) { 00288 case OSC_ID_RC2MHZ: 00289 Assert((ref_id == OSC_ID_RC32KHZ) || (ref_id == OSC_ID_XOSC)); 00290 00291 if (ref_id == OSC_ID_XOSC) { 00292 OSC.DFLLCTRL |= OSC_RC2MCREF_bm; 00293 } else { 00294 OSC.DFLLCTRL &= ~(OSC_RC2MCREF_bm); 00295 } 00296 DFLLRC2M.CTRL |= DFLL_ENABLE_bm; 00297 break; 00298 00299 case OSC_ID_RC32MHZ: 00300 #if XMEGA_USB 00301 Assert((ref_id == OSC_ID_RC32KHZ) || (ref_id == OSC_ID_XOSC) 00302 || (ref_id == OSC_ID_USBSOF)); 00303 OSC.DFLLCTRL &= ~(OSC_RC32MREF_gm); 00304 if (ref_id == OSC_ID_XOSC) { 00305 OSC.DFLLCTRL |= OSC_RC32MREF_XOSC32K_gc; 00306 } else if (ref_id == OSC_ID_USBSOF) { 00307 // Calibrate 32MRC at 48MHz using USB SOF 00308 // 48MHz/1kHz=0xBB80 00309 DFLLRC32M.COMP1=0x80; 00310 DFLLRC32M.COMP2=0xBB; 00311 OSC.DFLLCTRL |= OSC_RC32MREF_USBSOF_gc; 00312 } 00313 #else 00314 Assert((ref_id == OSC_ID_RC32KHZ) || (ref_id == OSC_ID_XOSC)); 00315 if (ref_id == OSC_ID_XOSC) { 00316 OSC.DFLLCTRL |= OSC_RC32MCREF_bm; 00317 } else { 00318 OSC.DFLLCTRL &= ~(OSC_RC32MCREF_bm); 00319 } 00320 #endif 00321 DFLLRC32M.CTRL |= DFLL_ENABLE_bm; 00322 break; 00323 00324 default: 00325 //unhandled_case(id); 00326 break; 00327 } 00328 cpu_irq_restore(flags); 00329 }
static void osc_enable_external | ( | uint8_t | id | ) | [inline, static] |
Definition at line 231 of file osc.h.
Referenced by osc_enable().
static void osc_enable_internal | ( | uint8_t | id | ) | [inline, static] |
Enable internal oscillator id.
For internal use only.
Do not call this function directly. Use osc_enable() instead.
Definition at line 189 of file osc.h.
References Assert, cpu_irq_restore(), cpu_irq_save(), and OSC_ID_USBSOF.
Referenced by osc_enable().
00190 { 00191 irqflags_t flags; 00192 00193 Assert(id != OSC_ID_USBSOF); 00194 00195 flags = cpu_irq_save(); 00196 OSC.CTRL |= id; 00197 cpu_irq_restore(flags); 00198 }
static uint32_t osc_get_rate | ( | uint8_t | id | ) | [inline, static] |
Return the frequency of oscillator id in Hz.
Definition at line 393 of file osc.h.
References Assert, BOARD_XOSC_HZ, OSC_ID_RC2MHZ, OSC_ID_RC32KHZ, OSC_ID_RC32MHZ, OSC_ID_USBSOF, and OSC_ID_XOSC.
Referenced by pll_get_default_rate_priv().
00394 { 00395 Assert(id != OSC_ID_USBSOF); 00396 00397 switch (id) { 00398 case OSC_ID_RC2MHZ: 00399 return 2000000UL; 00400 00401 case OSC_ID_RC32MHZ: 00402 #ifdef CONFIG_OSC_RC32_CAL 00403 return CONFIG_OSC_RC32_CAL; 00404 #else 00405 return 32000000UL; 00406 #endif 00407 00408 case OSC_ID_RC32KHZ: 00409 return 32768UL; 00410 00411 #ifdef BOARD_XOSC_HZ 00412 case OSC_ID_XOSC: 00413 return BOARD_XOSC_HZ; 00414 #endif 00415 00416 default: 00417 // unhandled_case(id); 00418 return 0; 00419 } 00420 }
static bool osc_is_ready | ( | uint8_t | id | ) | [inline, static] |
Determine whether oscillator id is ready.
true | Oscillator id is running and ready to use as a clock source. | |
false | Oscillator id is not running. |
Definition at line 248 of file osc.h.
References Assert, and OSC_ID_USBSOF.
Referenced by osc_wait_ready().
00249 { 00250 Assert(id != OSC_ID_USBSOF); 00251 00252 return OSC.STATUS & id; 00253 }
static void osc_user_calibration | ( | uint8_t | id, | |
uint16_t | calib | |||
) | [inline, static] |
Load a specific calibration value for the specified oscillator.
id | The ID of the oscillator for which to disable auto-calibration:
| |
calib | The specific calibration value required: |
Definition at line 366 of file osc.h.
References LSB, MSB, OSC_ID_RC2MHZ, and OSC_ID_RC32MHZ.
Referenced by sysclk_init().
00367 { 00368 switch (id) { 00369 case OSC_ID_RC2MHZ: 00370 DFLLRC2M.CALA=LSB(calib); 00371 DFLLRC2M.CALB=MSB(calib); 00372 00373 case OSC_ID_RC32MHZ: 00374 DFLLRC32M.CALA=LSB(calib); 00375 DFLLRC32M.CALB=MSB(calib); 00376 00377 default: 00378 // unhandled_case(id); 00379 break; 00380 } 00381 }
static void osc_wait_ready | ( | uint8_t | id | ) | [inline, static] |
Wait until the oscillator identified by id is ready.
This function will busy-wait for the oscillator identified by id to become stable and ready to use as a clock source.
id | A number identifying the oscillator to wait for. |
Definition at line 130 of file osc.h.
References osc_is_ready().
Referenced by sysclk_init().
00131 { 00132 while (!osc_is_ready(id)) { 00133 /* Do nothing */ 00134 } 00135 }
Generated on Fri Oct 22 12:15:26 2010 for AVR1300 Using the Xmega ADC by ![]() |