oled 1.0
OLED Graphics Control Library
Loading...
Searching...
No Matches
system.h File Reference

Header file with declarations and macros for system clock configuration. More...

#include <avr/io.h>
Include dependency graph for system.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define SYSTEM_CLOCK   CLKCTRL_CLKSEL_OSC20M_gc
 Defines the system main clock source for the microcontroller.
#define SYSTEM_CLOCK_BIT   CLKCTRL_OSC20MS_bm
 Defines the bit mask corresponding to the current system clock source.
#define SYSTEM_PER_CLOCK_PRESCALER   CLKCTRL_PDIV_2X_gc
 Defines the prescaler setting for the peripheral clock (peripheral frequency).

Functions

void system_init (void)
 Initializes the system clock configuration of the microcontroller.

Detailed Description

Header file with declarations and macros for system clock configuration.

This file provides macro definitions, constants, and function prototypes for configuring the system clock and peripheral clock prescaler on AVR microcontrollers. It defines the available clock sources (SYSTEM_CLOCK), the corresponding status bit (SYSTEM_CLOCK_BIT), and the optional peripheral prescaler (SYSTEM_PER_CLOCK_PRESCALER).

Author
g.raf
Date
2025-09-18
Version
1.0 Release
Note
This file is part of a larger embedded systems project and subject to the license specified in the repository. For updates and the complete revision history, see the GitHub repository.
See also
https://github.com/0x007e/avr0 "AVR ATmega GitHub Repository"

Macro Definition Documentation

◆ SYSTEM_CLOCK

#define SYSTEM_CLOCK   CLKCTRL_CLKSEL_OSC20M_gc

Defines the system main clock source for the microcontroller.

This macro selects the active system clock source from the available options in the AVR CLKCTRL module.

Possible values:

  • CLKCTRL_CLKSEL_OSC20M_gc : Internal 20 MHz oscillator
  • CLKCTRL_CLKSEL_OSCULP32K_gc : Internal 32 kHz ultra low power oscillator
  • CLKCTRL_CLKSEL_EXTCLK_gc : External clock source

By default, SYSTEM_CLOCK is set to CLKCTRL_CLKSEL_OSC20M_gc (20 MHz internal oscillator).

Note
Changing the system clock source has an impact on all peripherals that rely on clock timing (e.g., timers, delay functions, UART baud rate).

◆ SYSTEM_CLOCK_BIT

#define SYSTEM_CLOCK_BIT   CLKCTRL_OSC20MS_bm

Defines the bit mask corresponding to the current system clock source.

This macro provides the bit mask that represents the currently selected system clock source. It is typically used when checking or configuring oscillator status bits in the CLKCTRL registers.

Possible values (depending on SYSTEM_CLOCK):

  • CLKCTRL_OSC20MS_bm : Mask for the 20 MHz internal oscillator
  • CLKCTRL_OSC32KS_bm : Mask for the 32 kHz internal oscillator
  • CLKCTRL_EXTS_bm : Mask for the external clock source

By default, SYSTEM_CLOCK_BIT is set to match CLKCTRL_CLKSEL_OSC20M_gcCLKCTRL_OSC20MS_bm.

Note
This macro complements SYSTEM_CLOCK by indicating the appropriate bit mask for low-level register operations.

◆ SYSTEM_PER_CLOCK_PRESCALER

#define SYSTEM_PER_CLOCK_PRESCALER   CLKCTRL_PDIV_2X_gc

Defines the prescaler setting for the peripheral clock (peripheral frequency).

This macro determines how the system clock (F_CPU) is divided down to generate the peripheral clock used by timers, communication peripherals, and other hardware modules.

Possible values (from CLKCTRL module):

  • CLKCTRL_PDIV_2X_gc : F_CPU / 2
  • CLKCTRL_PDIV_4X_gc : F_CPU / 4
  • CLKCTRL_PDIV_8X_gc : F_CPU / 8
  • CLKCTRL_PDIV_16X_gc : F_CPU / 16
  • CLKCTRL_PDIV_32X_gc : F_CPU / 32
  • CLKCTRL_PDIV_64X_gc : F_CPU / 64
  • CLKCTRL_PDIV_6X_gc : F_CPU / 6
  • CLKCTRL_PDIV_10X_gc : F_CPU / 10
  • CLKCTRL_PDIV_24X_gc : F_CPU / 24
  • CLKCTRL_PDIV_48X_gc : F_CPU / 48

If the macro is undefined, the default is no prescaling (F_CPU / 1).

Note
Lowering the peripheral clock frequency can reduce power consumption but may affect timing accuracy and maximum baud rates of communication peripherals.

Function Documentation

◆ system_init()

void system_init ( void )

Initializes the system clock configuration of the microcontroller.

This function configures and enables the main system clock source and (optionally) the peripheral clock prescaler. The initialization sequence includes:

  1. Writing the selected system clock source (SYSTEM_CLOCK) to CLKCTRL.MCLKCTRLA.
  2. Waiting until the new clock source is active and stable, by checking CLKCTRL.MCLKSTATUS against SYSTEM_CLOCK_BIT.
  3. If SYSTEM_PER_CLOCK_PRESCALER is defined, configuring the peripheral clock division ratio via CLKCTRL.MCLKCTRLB.
  4. If the selected system clock is the internal 20 MHz oscillator (CLKCTRL_CLKSEL_OSC20M_gc), enabling the oscillator in standby mode by setting CLKCTRL.OSC20MCTRLA.
Note
  • The function makes use of the Configuration Change Protection (CCP) mechanism (CCP = CCP_IOREG_gc) before writing to protected CLKCTRL registers.
  • Changing the system clock affects all timing-sensitive peripherals (UART, timers, ADC clock, etc.).
  • If no prescaler (SYSTEM_PER_CLOCK_PRESCALER) is defined, the peripheral clock will run at F_CPU / 1.
Warning
This function should be called once during system startup before any timing-dependent peripherals are initialized.