Microchip® Advanced Software Framework

mega/sysclk.h File Reference

Chip-specific system clock management functions.

Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.

#include <board.h>
#include <compiler.h>
#include <parts.h>
#include <conf_clock.h>

Macros

#define ASM   __asm__
 
#define CONFIG_SYSCLK_PSDIV   SYSCLK_PSDIV_8
 
#define PRADC_bm   1 << PRADC
 
#define PRSPI_bm   1 << PRSPI
 
#define PRTIM0_bm   1 << PRTIM0
 
#define PRTIM1_bm   1 << PRTIM1
 
#define PRTIM2_bm   1 << PRTIM2
 
#define PRTWI_bm   1 << PRTWI
 
#define PRUSART0_bm   1 << PRUSART0
 
#define SYSCLK_PSDIV_1   0 /* !< Do not prescale */
 
#define SYSCLK_PSDIV_2   1 /* !< Prescale CLKper4 by 2 */
 
#define SYSCLK_PSDIV_4   2 /* !< Prescale CLKper4 by 4 */
 
#define SYSCLK_PSDIV_8   3 /* !< Prescale CLKper4 by 8 */
 
#define SYSCLK_PSDIV_16   4 /* !< Prescale CLKper4 by 16 */
 
#define SYSCLK_PSDIV_32   5 /* !< Prescale CLKper4 by 32 */
 
#define SYSCLK_PSDIV_64   6 /* !< Prescale CLKper4 by 64 */
 
#define SYSCLK_PSDIV_128   7 /* !< Prescale CLKper4 by 128 */
 
#define SYSCLK_PSDIV_256   8 /* !< Prescale CLKper4 by 256 */
 

Functions

Querying the system clock and its derived clocks
static uint32_t sysclk_get_main_hz (void)
 Return the current rate in Hz of the main system clock To know the clock value at what frequency the main clock is running. More...
 
static uint32_t sysclk_get_source_clock_hz (void)
 Return the current rate in Hz of source clock in Hz. More...
 
static uint32_t sysclk_get_cpu_hz (void)
 Return the current rate in Hz of the CPU clock. More...
 
static uint32_t sysclk_get_rc_osc_hz (void)
 Return the current rate in Hz of the clock from internal oscillator. More...
 
void sysclk_enable_module (enum power_red_id port, uint8_t id)
 Enable the clock to peripheral id on port port. More...
 
void sysclk_disable_module (enum power_red_id port, uint8_t id)
 Disable the clock to peripheral id on port port. More...
 
static void sysclk_enable_peripheral_clock (const volatile void *module)
 Enable a peripherals clock from its base address. More...
 
static void sysclk_disable_peripheral_clock (const volatile void *module)
 Disable a peripheral's clock from its base address. More...
 
static void sysclk_set_prescalers (uint8_t psdiv)
 Set system clock prescaler configuration. More...
 
static uint32_t sysclk_get_peripheral_bus_hz (const volatile void *module)
 Retrieves the current rate in Hz of the Peripheral Bus clock attached to the specified peripheral. More...
 
static bool sysclk_module_is_enabled (enum power_red_id port, uint8_t id)
 Check if the synchronous clock is enabled for a module. More...
 
void sysclk_init (void)
 Function to initialize the clock and disable clock for not required modules. More...
 

#define ASM   __asm__

Referenced by sysclk_set_prescalers().

#define PRADC_bm   1 << PRADC
#define PRTIM0_bm   1 << PRTIM0
#define PRTIM1_bm   1 << PRTIM1
#define PRTIM2_bm   1 << PRTIM2
#define SYSCLK_PSDIV_1   0 /* !< Do not prescale */
#define SYSCLK_PSDIV_128   7 /* !< Prescale CLKper4 by 128 */
#define SYSCLK_PSDIV_16   4 /* !< Prescale CLKper4 by 16 */
#define SYSCLK_PSDIV_2   1 /* !< Prescale CLKper4 by 2 */
#define SYSCLK_PSDIV_256   8 /* !< Prescale CLKper4 by 256 */
#define SYSCLK_PSDIV_32   5 /* !< Prescale CLKper4 by 32 */
#define SYSCLK_PSDIV_4   2 /* !< Prescale CLKper4 by 4 */
#define SYSCLK_PSDIV_64   6 /* !< Prescale CLKper4 by 64 */
#define SYSCLK_PSDIV_8   3 /* !< Prescale CLKper4 by 8 */

void sysclk_disable_module ( enum power_red_id  port,
uint8_t  id 
)

Disable the clock to peripheral id on port port.

Parameters
portID of the port to which the module is connected (one of the power_red_id *definitions).
idThe ID (bit mask) of the peripheral module to be disabled.

References cpu_irq_restore(), and cpu_irq_save().

Referenced by sysclk_disable_peripheral_clock(), and sysclk_get_peripheral_bus_hz().

static void sysclk_disable_peripheral_clock ( const volatile void *  module)
inlinestatic

Disable a peripheral's clock from its base address.

Disables the clock to a peripheral, given its base address.

Parameters
modulePointer to the module's base address.

References Assert, NULL, PRADC_bm, PRSPI_bm, PRTIM0_bm, PRTIM1_bm, PRTIM2_bm, PRTWI_bm, PRUSART0_bm, and sysclk_disable_module().

Referenced by get_temperature(), and tc_disable().

void sysclk_enable_module ( enum power_red_id  port,
uint8_t  id 
)

Enable the clock to peripheral id on port port.

Parameters
portID of the port to which the module is connected (one of the power_red_id *definitions).
idThe ID (bitmask) of the peripheral module to be disabled.*
portID of the port to which the module is connected (one of the power_red_id *definitions).
idThe ID (bitmask) of the peripheral module to be enabled.

References cpu_irq_restore(), and cpu_irq_save().

Referenced by run_loopback_syncmode_test(), spi_master_init(), spi_slave_init(), sysclk_enable_peripheral_clock(), usart_enable_module_clock(), and usart_spi_init().

static void sysclk_enable_peripheral_clock ( const volatile void *  module)
inlinestatic

Enable a peripherals clock from its base address.

Enables the clock to a peripheral, given its base address. If the peripheral has an associated clock on the HSB bus, this will be enabled also.

Parameters
modulePointer to the module's base address.

References Assert, NULL, PRADC_bm, PRSPI_bm, PRTIM0_bm, PRTIM1_bm, PRTIM2_bm, PRTWI_bm, PRUSART0_bm, and sysclk_enable_module().

Referenced by adc_initialisation(), epd_spi_init(), get_temperature(), initialize_EPD_timer(), main(), PHY_Init(), PWM_start_toggle(), run_baudrate_set_test(), run_spi_transfer_test(), run_twi_master_recv_test(), run_twi_master_send_test(), sm_init(), tc_enable(), tfa_init(), timer2_initialisation(), trx_init(), twi_master_setup(), and twi_slave_setup().

static uint32_t sysclk_get_cpu_hz ( void  )
inlinestatic

Return the current rate in Hz of the CPU clock.

Returns
Frequency of the CPU clock, in Hz.

References sysclk_get_source_clock_hz().

Referenced by main(), run_baudrate_test(), run_spi_transfer_test(), run_twi_master_recv_test(), run_twi_master_send_test(), spi_master_setup_device(), and twi_master_setup().

static uint32_t sysclk_get_main_hz ( void  )
inlinestatic

Return the current rate in Hz of the main system clock To know the clock value at what frequency the main clock is running.

Returns
Frequency of the main system clock, in Hz.
Todo:
: please initialize the SYSCLK_SOURCE in conf_clock.h file for configured source clock using fuses. . #define SYSCLK_SOURCE SYSCLK_SRC_RC16MHZ to use internal RC oscillator for clock source.

Referenced by sysclk_get_rc_osc_hz(), and sysclk_get_source_clock_hz().

static uint32_t sysclk_get_peripheral_bus_hz ( const volatile void *  module)
inlinestatic

Retrieves the current rate in Hz of the Peripheral Bus clock attached to the specified peripheral.

Parameters
modulePointer to the module's base address.
Returns
Frequency of the bus attached to the specified peripheral, in Hz.

References Assert, NULL, PRTWI_bm, sysclk_disable_module(), and sysclk_get_source_clock_hz().

Referenced by initialize_EPD_timer(), PWM_start_toggle(), and tmr_init().

static uint32_t sysclk_get_rc_osc_hz ( void  )
inlinestatic

Return the current rate in Hz of the clock from internal oscillator.

Returns
Frequency of the CPU clock, in Hz.

References CONFIG_SYSCLK_PSDIV, sysclk_get_main_hz(), SYSCLK_PSDIV_1, SYSCLK_PSDIV_128, SYSCLK_PSDIV_16, SYSCLK_PSDIV_2, SYSCLK_PSDIV_256, SYSCLK_PSDIV_32, SYSCLK_PSDIV_4, SYSCLK_PSDIV_64, and SYSCLK_PSDIV_8.

static uint32_t sysclk_get_source_clock_hz ( void  )
inlinestatic

Return the current rate in Hz of source clock in Hz.

This clock always runs at the same rate as the CPU clock unless the divider is set.

Returns
Frequency of the system clock, in Hz.

References CONFIG_SYSCLK_PSDIV, sysclk_get_main_hz(), SYSCLK_PSDIV_1, SYSCLK_PSDIV_128, SYSCLK_PSDIV_16, SYSCLK_PSDIV_2, SYSCLK_PSDIV_256, SYSCLK_PSDIV_32, SYSCLK_PSDIV_4, SYSCLK_PSDIV_64, and SYSCLK_PSDIV_8.

Referenced by run_loopback_syncmode_test(), sysclk_get_cpu_hz(), sysclk_get_peripheral_bus_hz(), usart_init_rs232(), and usart_init_spi().

static bool sysclk_module_is_enabled ( enum power_red_id  port,
uint8_t  id 
)
inlinestatic

Check if the synchronous clock is enabled for a module.

Parameters
portID of the port to which the module is connected (one of the SYSCLK_PORT_* definitions).
idThe ID (bitmask) of the peripheral module to check (one of the SYSCLK_* module definitions).
Return values
trueIf the clock for module id on port is enabled.
falseIf the clock for module id on port is disabled.
static void sysclk_set_prescalers ( uint8_t  psdiv)
inlinestatic

Set system clock prescaler configuration.

This function will change the system clock prescaler configuration to match the parameters.

Note
The parameters to this function are device-specific.
Parameters
psbcdivThe prescaler settings (one of the SYSCLK_PSCDIV_* definitions). These determine the clkIO, clkADC and clkCPU frequencies. Note: Prescaler setting is not working with the brain dead un optimised code e.g. avr-gcc -00

References ASM, CONFIG_SYSCLK_PSDIV, cpu_irq_restore(), cpu_irq_save(), SYSCLK_PSDIV_1, SYSCLK_PSDIV_128, SYSCLK_PSDIV_16, SYSCLK_PSDIV_2, SYSCLK_PSDIV_256, SYSCLK_PSDIV_32, SYSCLK_PSDIV_4, SYSCLK_PSDIV_64, and SYSCLK_PSDIV_8.

Referenced by sysclk_init().