Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Oscillator Management

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.

Example: Enabling an oscillator

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.

osc_enable(OSC_ID_XOSC);
osc_wait_ready(OSC_ID_XOSC);

Board-specific Definitions

If external oscillators are used, the board code must provide the following definitions for each of those:

Macros

#define OSC0_GAIN_VALUE   AVR32_SCIF_OSCCTRL0_GAIN_G3
 
#define OSC0_MODE_VALUE   OSC_MODE_XTAL
 Board-dependent value written to the MODE bitfield of PM_OSCCTRL(0) More...
 
#define OSC0_STARTUP_TIMEOUT   320
 Number of slow clock cycles to wait for OSC0 to start. More...
 
#define OSC0_STARTUP_VALUE   OSC_STARTUP_256
 Board-dependent value written to the STARTUP bitfield of PM_OSCCTRL(0) More...
 
#define OSC1_MODE_VALUE   UNDEFINED
 Board-dependent value written to the MODE bitfield of PM_OSCCTRL(1) More...
 
#define OSC1_STARTUP_TIMEOUT   UNDEFINED
 Number of slow clock cycles to wait for OSC1 to start. More...
 
#define OSC1_STARTUP_VALUE   UNDEFINED
 Board-dependent value written to the STARTUP bitfield of PM_OSCCTRL(1) More...
 
#define OSC32_MODE_VALUE   OSC32_MODE_XTAL
 
#define OSC32_STARTUP_VALUE   OSC32_STARTUP_8192
 

Functions

void osc_priv_disable_osc0 (void)
 
void osc_priv_disable_osc1 (void)
 
void osc_priv_disable_osc32 (void)
 
void osc_priv_disable_rc120m (void)
 
void osc_priv_disable_rc8m (void)
 
void osc_priv_enable_osc0 (void)
 
void osc_priv_enable_osc1 (void)
 
void osc_priv_enable_osc32 (void)
 
void osc_priv_enable_rc120m (void)
 
void osc_priv_enable_rc8m (void)
 
bool osc_priv_osc0_is_ready (void)
 
bool osc_priv_osc1_is_ready (void)
 
bool osc_priv_osc32_is_ready (void)
 
bool osc_priv_rc120m_is_ready (void)
 
bool osc_priv_rc8m_is_ready (void)
 

Oscillator Management

static void osc_wait_ready (uint8_t id)
 Wait until the oscillator identified by id is ready. More...
 
static void osc_enable (uint8_t id)
 Enable oscillator id. More...
 
static void osc_disable (uint8_t id)
 Disable oscillator id. More...
 
static bool osc_is_ready (uint8_t id)
 Determine whether oscillator id is ready. More...
 
static uint32_t osc_get_rate (uint8_t id)
 Return the frequency of oscillator id in Hz. More...
 

Oscillator identifiers

#define OSC_ID_OSC0   0
 External Oscillator 0. More...
 
#define OSC_ID_OSC1   1
 External Oscillator 1. More...
 
#define OSC_ID_RC8M   2
 Internal 8 MHz RC oscillator. More...
 
#define OSC_ID_OSC32   3
 External 32 kHz oscillator. More...
 
#define OSC_ID_RC120M   4
 Internal 120 MHz RC oscillator. More...
 
#define OSC_ID_RCSYS   5
 Internal System RC oscillator. More...
 

OSC0/OSC1 mode values

#define OSC_MODE_EXTERNAL   AVR32_SCIF_OSCCTRL_MODE_EXT_CLOCK
 External clock connected to XIN. More...
 
#define OSC_MODE_XTAL   AVR32_SCIF_OSCCTRL_MODE_CRYSTAL
 Crystal connected to XIN/XOUT. More...
 

OSC32 mode values

#define OSC32_MODE_EXTERNAL   AVR32_SCIF_OSCCTRL32_MODE_EXT_CLOCK
 External clock connected to XIN32. More...
 
#define OSC32_MODE_XTAL   AVR32_SCIF_OSCCTRL32_MODE_CRYSTAL_AGC
 Crystal connected to XIN32/XOUT32. More...
 
#define OSC32_MODE_XTAL_HC   AVR32_SCIF_OSCCTRL32_MODE_CRYSTAL_NO_AGC
 Crystal connected to XIN32/XOUT32 in high current mode. More...
 

OSC0/OSC1 startup values

#define OSC_STARTUP_0   AVR32_SCIF_OSCCTRL0_STARTUP_0_RCOSC
 0 cycles More...
 
#define OSC_STARTUP_4   AVR32_SCIF_OSCCTRL0_STARTUP_4_RCOSC
 4 cycles (35 us) More...
 
#define OSC_STARTUP_8   AVR32_SCIF_OSCCTRL0_STARTUP_8_RCOSC
 8 cycles (70 us) More...
 
#define OSC_STARTUP_16   AVR32_SCIF_OSCCTRL0_STARTUP_16_RCOSC
 16 cycles (140 us) More...
 
#define OSC_STARTUP_32   AVR32_SCIF_OSCCTRL0_STARTUP_32_RCOSC
 32 cycles (280 us) More...
 
#define OSC_STARTUP_64   AVR32_SCIF_OSCCTRL0_STARTUP_64_RCOSC
 64 cycles (560 us) More...
 
#define OSC_STARTUP_128   AVR32_SCIF_OSCCTRL0_STARTUP_128_RCOSC
 128 cycles (1.1 ms) More...
 
#define OSC_STARTUP_256   AVR32_SCIF_OSCCTRL0_STARTUP_256_RCOSC
 256 cycles (2.2 ms) More...
 
#define OSC_STARTUP_512   AVR32_SCIF_OSCCTRL0_STARTUP_512_RCOSC
 512 cycles (4.5 ms) More...
 
#define OSC_STARTUP_1024   AVR32_SCIF_OSCCTRL0_STARTUP_1024_RCOSC
 1024 cycles (9.0 ms) More...
 
#define OSC_STARTUP_2048   AVR32_SCIF_OSCCTRL0_STARTUP_2048_RCOSC
 2048 cycles (18 ms) More...
 
#define OSC_STARTUP_4096   AVR32_SCIF_OSCCTRL0_STARTUP_4096_RCOSC
 4096 cycles (36 ms) More...
 
#define OSC_STARTUP_8192   AVR32_SCIF_OSCCTRL0_STARTUP_8192_RCOSC
 8192 cycles (71 ms) More...
 
#define OSC_STARTUP_16384   AVR32_SCIF_OSCCTRL0_STARTUP_16384_RCOSC
 16384 cycles (142 ms) More...
 

OSC32 startup values

#define OSC32_STARTUP_0   AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC
 0 cycles More...
 
#define OSC32_STARTUP_128   AVR32_SCIF_OSCCTRL32_STARTUP_128_RCOSC
 128 cycles (1.1 ms) More...
 
#define OSC32_STARTUP_8192   AVR32_SCIF_OSCCTRL32_STARTUP_8192_RCOSC
 8192 cycles (72.3 ms) More...
 
#define OSC32_STARTUP_16384   AVR32_SCIF_OSCCTRL32_STARTUP_16384_RCOSC
 16384 cycles (143 ms) More...
 
#define OSC32_STARTUP_65536   AVR32_SCIF_OSCCTRL32_STARTUP_65536_RCOSC
 65536 cycles (570 ms) More...
 
#define OSC32_STARTUP_131072   AVR32_SCIF_OSCCTRL32_STARTUP_131072_RCOSC
 131072 cycles (1.1 s) More...
 
#define OSC32_STARTUP_262144   AVR32_SCIF_OSCCTRL32_STARTUP_262144_RCOSC
 262144 cycles (2.3 s) More...
 
#define OSC32_STARTUP_524288   AVR32_SCIF_OSCCTRL32_STARTUP_524288_RCOSC
 524288 cycles (4.6 s) More...
 

Board-specific configuration parameters

The following definitions must be provided by the board code for all working oscillators on the board.

#define BOARD_OSC0_HZ   16000000
 Clock frequency of OSC0 in Hz. More...
 
#define BOARD_OSC0_STARTUP_US   2000
 Startup time of OSC0 in microseconds. More...
 
#define BOARD_OSC0_IS_XTAL   true
 OSC0 uses a crystal, not an external clock. More...
 
#define BOARD_OSC32_HZ   32768
 Clock frequency of OSC32 in Hz. More...
 
#define BOARD_OSC32_STARTUP_US   71000
 Startup time of OSC32 in microseconds. More...
 
#define BOARD_OSC32_IS_XTAL   true
 OSC32 uses a crystal, not an external clock. More...
 

RC oscillator frequency limits

The frequency of the internal RC oscillators 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_RCSYS_NOMINAL_HZ   115000
 Nominal frequency of RCSYS in Hz. More...
 
#define OSC_RCSYS_MIN_HZ   100000
 Minimum frequency of RCSYS in Hz. More...
 
#define OSC_RCSYS_MAX_HZ   120000
 Maximum frequency of RCSYS in Hz. More...
 
#define OSC_RC8M_NOMINAL_HZ   8000000
 Nominal frequency of RC8M in Hz. More...
 
#define OSC_RC120M_NOMINAL_HZ   120000000
 Nominal frequency of RC120M in Hz. More...
 

#define BOARD_OSC0_HZ   16000000

Clock frequency of OSC0 in Hz.

Referenced by osc_get_rate(), sysclk_get_main_hz(), and sysclk_init().

#define BOARD_OSC0_IS_XTAL   true

OSC0 uses a crystal, not an external clock.

#define BOARD_OSC0_STARTUP_US   2000

Startup time of OSC0 in microseconds.

#define BOARD_OSC32_HZ   32768

Clock frequency of OSC32 in Hz.

Referenced by osc_get_rate().

#define BOARD_OSC32_IS_XTAL   true

OSC32 uses a crystal, not an external clock.

#define BOARD_OSC32_STARTUP_US   71000

Startup time of OSC32 in microseconds.

#define OSC0_GAIN_VALUE   AVR32_SCIF_OSCCTRL0_GAIN_G3
#define OSC0_MODE_VALUE   OSC_MODE_XTAL

Board-dependent value written to the MODE bitfield of PM_OSCCTRL(0)

#define OSC0_STARTUP_TIMEOUT   320

Number of slow clock cycles to wait for OSC0 to start.

This is the number of slow clock cycles corresponding to OSC0_STARTUP_VALUE with an additional 25% safety margin. If the oscillator isn't running when this timeout has expired, it is assumed to have failed to start.

#define OSC0_STARTUP_VALUE   OSC_STARTUP_256

Board-dependent value written to the STARTUP bitfield of PM_OSCCTRL(0)

#define OSC1_MODE_VALUE   UNDEFINED

Board-dependent value written to the MODE bitfield of PM_OSCCTRL(1)

#define OSC1_STARTUP_TIMEOUT   UNDEFINED

Number of slow clock cycles to wait for OSC1 to start.

This is the number of slow clock cycles corresponding to OSC1_STARTUP_VALUE with an additional 25% safety margin. If the oscillator isn't running when this timeout has expired, it is assumed to have failed to start.

#define OSC1_STARTUP_VALUE   UNDEFINED

Board-dependent value written to the STARTUP bitfield of PM_OSCCTRL(1)

#define OSC32_MODE_EXTERNAL   AVR32_SCIF_OSCCTRL32_MODE_EXT_CLOCK

External clock connected to XIN32.

#define OSC32_MODE_VALUE   OSC32_MODE_XTAL
#define OSC32_MODE_XTAL   AVR32_SCIF_OSCCTRL32_MODE_CRYSTAL_AGC

Crystal connected to XIN32/XOUT32.

#define OSC32_MODE_XTAL_HC   AVR32_SCIF_OSCCTRL32_MODE_CRYSTAL_NO_AGC

Crystal connected to XIN32/XOUT32 in high current mode.

#define OSC32_STARTUP_0   AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC

0 cycles

#define OSC32_STARTUP_128   AVR32_SCIF_OSCCTRL32_STARTUP_128_RCOSC

128 cycles (1.1 ms)

#define OSC32_STARTUP_131072   AVR32_SCIF_OSCCTRL32_STARTUP_131072_RCOSC

131072 cycles (1.1 s)

#define OSC32_STARTUP_16384   AVR32_SCIF_OSCCTRL32_STARTUP_16384_RCOSC

16384 cycles (143 ms)

#define OSC32_STARTUP_262144   AVR32_SCIF_OSCCTRL32_STARTUP_262144_RCOSC

262144 cycles (2.3 s)

#define OSC32_STARTUP_524288   AVR32_SCIF_OSCCTRL32_STARTUP_524288_RCOSC

524288 cycles (4.6 s)

#define OSC32_STARTUP_65536   AVR32_SCIF_OSCCTRL32_STARTUP_65536_RCOSC

65536 cycles (570 ms)

#define OSC32_STARTUP_8192   AVR32_SCIF_OSCCTRL32_STARTUP_8192_RCOSC

8192 cycles (72.3 ms)

#define OSC32_STARTUP_VALUE   OSC32_STARTUP_8192
#define OSC_ID_OSC0   0
#define OSC_ID_OSC1   1
#define OSC_ID_OSC32   3

External 32 kHz oscillator.

Referenced by genclk_enable_source(), osc_disable(), osc_enable(), osc_get_rate(), and osc_is_ready().

#define OSC_ID_RC120M   4

Internal 120 MHz RC oscillator.

Referenced by osc_disable(), osc_enable(), osc_get_rate(), osc_is_ready(), and sysclk_init().

#define OSC_ID_RC8M   2
#define OSC_ID_RCSYS   5

Internal System RC oscillator.

Referenced by osc_disable(), osc_enable(), osc_get_rate(), and osc_is_ready().

#define OSC_MODE_EXTERNAL   AVR32_SCIF_OSCCTRL_MODE_EXT_CLOCK

External clock connected to XIN.

#define OSC_MODE_XTAL   AVR32_SCIF_OSCCTRL_MODE_CRYSTAL

Crystal connected to XIN/XOUT.

#define OSC_RC120M_NOMINAL_HZ   120000000

Nominal frequency of RC120M in Hz.

Referenced by osc_get_rate(), and sysclk_get_main_hz().

#define OSC_RC8M_NOMINAL_HZ   8000000

Nominal frequency of RC8M in Hz.

Referenced by osc_get_rate(), and sysclk_get_main_hz().

#define OSC_RCSYS_MAX_HZ   120000

Maximum frequency of RCSYS in Hz.

#define OSC_RCSYS_MIN_HZ   100000

Minimum frequency of RCSYS in Hz.

#define OSC_RCSYS_NOMINAL_HZ   115000

Nominal frequency of RCSYS in Hz.

Referenced by osc_get_rate(), and sysclk_get_main_hz().

#define OSC_STARTUP_0   AVR32_SCIF_OSCCTRL0_STARTUP_0_RCOSC

0 cycles

#define OSC_STARTUP_1024   AVR32_SCIF_OSCCTRL0_STARTUP_1024_RCOSC

1024 cycles (9.0 ms)

#define OSC_STARTUP_128   AVR32_SCIF_OSCCTRL0_STARTUP_128_RCOSC

128 cycles (1.1 ms)

#define OSC_STARTUP_16   AVR32_SCIF_OSCCTRL0_STARTUP_16_RCOSC

16 cycles (140 us)

#define OSC_STARTUP_16384   AVR32_SCIF_OSCCTRL0_STARTUP_16384_RCOSC

16384 cycles (142 ms)

#define OSC_STARTUP_2048   AVR32_SCIF_OSCCTRL0_STARTUP_2048_RCOSC

2048 cycles (18 ms)

#define OSC_STARTUP_256   AVR32_SCIF_OSCCTRL0_STARTUP_256_RCOSC

256 cycles (2.2 ms)

#define OSC_STARTUP_32   AVR32_SCIF_OSCCTRL0_STARTUP_32_RCOSC

32 cycles (280 us)

#define OSC_STARTUP_4   AVR32_SCIF_OSCCTRL0_STARTUP_4_RCOSC

4 cycles (35 us)

#define OSC_STARTUP_4096   AVR32_SCIF_OSCCTRL0_STARTUP_4096_RCOSC

4096 cycles (36 ms)

#define OSC_STARTUP_512   AVR32_SCIF_OSCCTRL0_STARTUP_512_RCOSC

512 cycles (4.5 ms)

#define OSC_STARTUP_64   AVR32_SCIF_OSCCTRL0_STARTUP_64_RCOSC

64 cycles (560 us)

#define OSC_STARTUP_8   AVR32_SCIF_OSCCTRL0_STARTUP_8_RCOSC

8 cycles (70 us)

#define OSC_STARTUP_8192   AVR32_SCIF_OSCCTRL0_STARTUP_8192_RCOSC

8192 cycles (71 ms)

void osc_enable ( uint8_t  id)
inlinestatic

Enable oscillator id.

The startup time and mode value is automatically determined based on definitions in the board code.

References OSC_ID_OSC0, OSC_ID_OSC1, OSC_ID_OSC32, OSC_ID_RC120M, OSC_ID_RC8M, OSC_ID_RCSYS, osc_priv_enable_osc0(), osc_priv_enable_osc1(), osc_priv_enable_osc32(), osc_priv_enable_rc120m(), and osc_priv_enable_rc8m().

Referenced by genclk_enable_source(), pll_enable_source(), and sysclk_init().

uint32_t osc_get_rate ( uint8_t  id)
inlinestatic

Return the frequency of oscillator id in Hz.

Todo:
RC8M may run at either 8 MHz or 1 MHz. Currently, we assume it's always running at 8 MHz.

References BOARD_OSC0_HZ, BOARD_OSC32_HZ, OSC_ID_OSC0, OSC_ID_OSC1, OSC_ID_OSC32, OSC_ID_RC120M, OSC_ID_RC8M, OSC_ID_RCSYS, OSC_RC120M_NOMINAL_HZ, OSC_RC8M_NOMINAL_HZ, and OSC_RCSYS_NOMINAL_HZ.

Referenced by pll_config_init().

osc_is_ready ( uint8_t  id)
inlinestatic

Determine whether oscillator id is ready.

Return values
trueOscillator id is running and ready to use as a clock source.
falseOscillator id is not running.

References OSC_ID_OSC0, OSC_ID_OSC1, OSC_ID_OSC32, OSC_ID_RC120M, OSC_ID_RC8M, and OSC_ID_RCSYS.

Referenced by genclk_enable_source(), osc_wait_ready(), and pll_enable_source().

void osc_priv_disable_osc0 ( void  )

Referenced by osc_disable().

void osc_priv_disable_osc1 ( void  )

Referenced by osc_disable().

void osc_priv_disable_osc32 ( void  )

Referenced by osc_disable().

void osc_priv_disable_rc120m ( void  )

References cpu_irq_restore(), and cpu_irq_save().

Referenced by osc_disable().

void osc_priv_disable_rc8m ( void  )

References cpu_irq_restore(), and cpu_irq_save().

Referenced by osc_disable().

void osc_priv_enable_osc0 ( void  )

Referenced by osc_enable().

void osc_priv_enable_osc1 ( void  )

Referenced by osc_enable().

void osc_priv_enable_osc32 ( void  )

Referenced by osc_enable().

void osc_priv_enable_rc120m ( void  )

References cpu_irq_restore(), and cpu_irq_save().

Referenced by osc_enable().

void osc_priv_enable_rc8m ( void  )

References cpu_irq_restore(), and cpu_irq_save().

Referenced by osc_enable().

bool osc_priv_osc0_is_ready ( void  )
bool osc_priv_osc1_is_ready ( void  )
bool osc_priv_osc32_is_ready ( void  )
bool osc_priv_rc120m_is_ready ( void  )
bool osc_priv_rc8m_is_ready ( void  )
static void osc_wait_ready ( uint8_t  id)
inlinestatic

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.

Parameters
idA number identifying the oscillator to wait for.

References osc_is_ready().

Referenced by genclk_enable_source(), pll_enable_source(), and sysclk_init().