Advanced use case - Peripheral Bus Clock Management (XMEGA)
This section will present a more advanced use case for the System Clock Management service. This use case will configure the main system clock to 32MHz, using an internal PLL module to multiply the frequency of a crystal attached to the microcontroller. The peripheral bus clocks will run at the same speed as the CPU clock, and the USB clock will be configured to use the internal 32MHz (nominal) RC oscillator calibrated to 48MHz with the USB Start-of-Frame as the calibration reference.
Prerequisites
Initialization code
Add to the application initialization code:
Workflow
- Configure the system clocks according to the settings in conf_clock.h:
Example code
Add or uncomment the following in your conf_clock.h header file, commenting out all other definitions of the same symbol(s):
#define CONFIG_SYSCLK_SOURCE SYSCLK_SRC_PLL
#define CONFIG_PLL0_SOURCE PLL_SRC_XOSC
#define CONFIG_PLL0_MUL (32000000UL / BOARD_XOSC_HZ)
#define CONFIG_PLL0_DIV 1
#define CONFIG_SYSCLK_PSADIV SYSCLK_PSADIV_1
#define CONFIG_SYSCLK_PSBCDIV SYSCLK_PSBCDIV_1_1
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_RCOSC
#define CONFIG_OSC_RC32_CAL 48000000UL
#define CONFIG_OSC_AUTOCAL OSC_ID_RC32MHZ
#define CONFIG_OSC_AUTOCAL_REF_OSC OSC_ID_USBSOF
Workflow
- Configure the main system clock to use the output of the PLL module as its source:
#define CONFIG_SYSCLK_SOURCE SYSCLK_SRC_PLL
- Configure the PLL0 module to use external crystal oscillator XOSC as its source:
#define CONFIG_PLL0_SOURCE PLL_SRC_XOSC
- Configure the PLL0 module to multiply the external oscillator XOSC frequency up to 32MHz:
#define CONFIG_PLL0_MUL (32000000UL / BOARD_XOSC_HZ)
#define CONFIG_PLL0_DIV 1
- Note
- For user boards,
BOARD_XOSC_HZ
should be defined in the board conf_board.h
configuration file as the frequency of the crystal attached to XOSC.
- Configure the main CPU and peripheral bus clocks to run at 32MHz:
#define CONFIG_SYSCLK_PSADIV SYSCLK_PSADIV_1
#define CONFIG_SYSCLK_PSBCDIV SYSCLK_PSBCDIV_1_2
- Note
- Some dividers are powers of two, while others are integer division factors. Refer to the formulas in the conf_clock.h template commented above each division define.
- Configure the USB module clock to use the internal fast (32MHz) RC oscillator:
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_RCOSC
- Note
- When the internal RC oscillator is used for the USB module, it must be recalibrated to 48MHz for the USB peripheral to function. If this oscillator is then used as the main system clock source, the clock must be divided down via the peripheral and CPU bus clock division constants to ensure that the maximum allowable CPU frequency is not exceeded.
- Configure the internal fast (32MHz) RC oscillator to calibrate to 48MHz using the USB Start of Frame (SOF) as the calibration reference:
#define CONFIG_OSC_RC32_CAL 48000000UL
#define CONFIG_OSC_AUTOCAL OSC_ID_RC32MHZ
#define CONFIG_OSC_AUTOCAL_REF_OSC OSC_ID_USBSOF