CMSIS-Driver_PIC32CM-PL  
Peripheral Library (PLIB) Documentation
 
Loading...
Searching...
No Matches
OSCCTRL Interface

Driver API for OSCCTRL (plib_oscctrl.h) More...

Macros

#define GCLKIN_CLOCK_FREQ   (12000000UL)
 
#define OSCCTRL_WP_KEY   (0x4F5343U)
 Write protection key for OSCCTRL registers.
 

Typedefs

typedef void(* OSCCTRL_CALLBACK) (void)
 OSCHF ready event callback function type.
 

Enumerations

enum  OSCCTRL_OSCHF_FREQ {
  OSCCTRL_OSCHF_FREQ_1M = 0x00U , OSCCTRL_OSCHF_FREQ_2M = 0x01U , OSCCTRL_OSCHF_FREQ_3M = 0x02U , OSCCTRL_OSCHF_FREQ_4M = 0x03U ,
  OSCCTRL_OSCHF_FREQ_8M = 0x05U , OSCCTRL_OSCHF_FREQ_12M = 0x06U , OSCCTRL_OSCHF_FREQ_16M = 0x07U , OSCCTRL_OSCHF_FREQ_20M = 0x08U ,
  OSCCTRL_OSCHF_FREQ_24M = 0x09U
}
 OSCHF frequency selection enumeration. More...
 

Functions

uint32_t CLOCK_GetPeripheralChannelFreq (uint32_t peripheral_channel_id)
 Fetches the clock frequency for a specific peripheral channel in Hz.
 
void OSCCTRL_OSCHF_Initialize (void)
 Initializes the OSCHF oscillator.
 
void OSCCTRL_OSCHF_SetFrequency (OSCCTRL_OSCHF_FREQ freq_sel)
 This function sets the frequency selection value for the internal OSCHF Oscillator.
 
uint32_t OSCCTRL_OSCHF_GetFrequency (void)
 Returns the current frequency of the internal OSCHF Oscillator.
 
void OSCCTRL_OSCHF_EnableAutotune (void)
 Enables automatic tuning for the OSCHF Oscillator.
 
void OSCCTRL_OSCHF_DisableAutotune (void)
 Disables automatic tuning for the OSCHF Oscillator.
 
bool OSCCTRL_OSCHF_AutotuneIsEnabled (void)
 Checks if the autotune feature is enabled for the OSCHF oscillator.
 
void OSCCTRL_OSCHF_EnableOnDemand (void)
 Enables On-Demand mode for the OSCHF Oscillator.
 
void OSCCTRL_OSCHF_DisableOnDemand (void)
 Disables On-Demand mode for the OSCHF Oscillator.
 
bool OSCCTRL_OSCHF_OnDemandIsEnabled (void)
 Checks if the On-Demand feature is enabled for the OSCHF oscillator.
 
void OSCCTRL_EnableWriteProtection (void)
 Enables write protection for the OSCHF Oscillator registers.
 
void OSCCTRL_DisableWriteProtection (void)
 Disables write protection for the OSCHF Oscillator registers.
 
bool OSCCTRL_WriteProtectionIsEnabled (void)
 Checks if write protection is enabled for the OSCHF oscillator registers.
 
void OSCCTRL_WPCTRL_EnableWriteLock (void)
 Enables the write protection lock for the WPCTRL register.
 
bool OSCCTRL_WPCTRL_WriteLockIsEnabled (void)
 Checks if the Write Protection Lock is enabled for the WPCTRL register.
 
void OSCCTRL_OSCHF_SetManualTune (uint8_t tune_value)
 Sets the manual tuning value for the OSCHF Oscillator. Note that Autotune must be disabled to use manual tuning. Failing to do so will issue a pslverr.
 
uint8_t OSCCTRL_OSCHF_GetFrequencyTune (void)
 Gets the current manual tuning value of the OSCHF oscillator.
 
bool OSCCTRL_OSCHF_IsReady (void)
 Checks if the OSCHF Oscillator is ready.
 
void OSCCTRL_OSCHF_EnableReadyInterrupt (void)
 Enables the ready interrupt for the OSCHF Oscillator.
 
bool OSCCTRL_OSCHF_ReadyInterruptIsEnabled (void)
 Checks if the ready interrupt is enabled for the OSCHF oscillator.
 
void OSCCTRL_OSCHF_DisableReadyInterrupt (void)
 Disables the ready interrupt for the OSCHF Oscillator.
 
bool OSCCTRL_OSCHF_InterruptFlagIsSet (void)
 Checks if the ready interrupt flag is set for the OSCHF oscillator.
 
void OSCCTRL_OSCHF_ClearInterruptFlag (void)
 Clears the ready interrupt flag for the OSCHF oscillator.
 
void OSCCTRL_OSCHF_SetReadyInterruptFlag (void)
 Asserts the ready interrupt flag for the OSCHF Oscillator. It is used for testing purposes to simulate the interrupt condition.
 
void OSCCTRL_OSCHF_SetReadyCallbackHandler (OSCCTRL_CALLBACK callback)
 Registers a callback function for the OSCHF ready interrupt. This function stores a user-provided callback to be called when the OSCHF Oscillator ready interrupt occurs.
 

Variables

uint32_t SystemCoreClock
 System core clock frequency. This global variable holds the current core (CPU) clock frequency in Hertz. Update the value with SystemCoreClockUpdate() whenever the core clock is changed.
 

Description

Driver API for OSCCTRL (plib_oscctrl.h)

Usage Example

The following example demonstrates typical usage of the OSCCTRL peripheral:

/********************************************************************************
* Copyright © 2026 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OF THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/
/******************************************************************************
* @file configure_clock.c
* @brief Clock configuration helper functions (template).
*
* @details This file provides small convenience APIs to configure OSCCTRL/GCLK/
* MCLK for common use-cases.
******************************************************************************/
#include "plib_clock.h"
/* Function prototypes */
uint32_t configure_CLOCK_GetSystemClockFreq(void);
void configure_OSCCTRL_Setup(void);
void configure_XOSC32K_Setup(void);
void configure_GCLK_Setup(void);
void configure_GCLK_EnableSignalOutput(void);
void configure_MCLK_DivideCoreClock(void);
uint32_t configure_CLOCK_GetSystemClockFreq(void)
{
SystemCoreClockUpdate();
}
void configure_OSCCTRL_Setup(void)
{
while(!OSCCTRL_OSCHF_IsReady()) {}
}
void configure_XOSC32K_Setup(void)
{
}
void configure_GCLK_Setup(void)
{
uint16_t freq_divide_factor = 2U;
bool improve_duty_cycle = false;
//improve_duty_cycle = ((freq_divide_factor % 2U) == 0U) ? true : false;
GCLK_SetGeneratorSource(1U, GCLK_GENCTRL_SRC_OSCHF_Val);
GCLK_SetDivisionParameters(1U, freq_divide_factor, GCLK_DIVISION_INTEGER, improve_duty_cycle);
}
void configure_GCLK_EnableSignalOutput(void)
{
}
void configure_MCLK_DivideCoreClock(void)
{
while(!MCLK_ClockIsReady()){}
}
void GCLK_EnableStandbyRun(GCLK_GENERATOR_ID generator_id)
Enables GCLK generator operation in standby mode.
void GCLK_SetOutputOffValue(GCLK_GENERATOR_ID generator_id, uint32_t output_off_value)
Sets the output value when the GCLK generator is off.
void GCLK_SetGeneratorSource(GCLK_GENERATOR_ID generator_id, GCLK_SOURCE source)
Sets the clock source for a specified GCLK generator. Before changing the source in GENERATOR 0,...
void GCLK_SetDivisionParameters(GCLK_GENERATOR_ID generator_id, uint16_t divide_factor, GCLK_DIVIDE_TYPE divide_type, bool improve_duty_cycle)
Sets the division factor and type for a GCLK generator.
void GCLK_GeneratorEnable(GCLK_GENERATOR_ID generator_id)
Enables the specified GCLK generator.
void GCLK_SetOutputEnable(GCLK_GENERATOR_ID generator_id, bool output_enable)
Enables or disables output for a GCLK generator.
@ GCLK_DIVISION_INTEGER
Definition plib_gclk.h:109
void MCLK_SetCPUClockDivider(MCLK_CPU_CLOCK_DIVIDER cpu_div)
Sets the CPU clock division factor.
bool MCLK_ClockIsReady(void)
Checks if the clock is ready (CKRDY flag).
@ MCLK_CPU_CLK_DIV_4
Definition plib_mclk.h:65
void OSC32KCTRL_XOSC32K_SelectClockSource(OSC32KCTRL_XTAL_SOURCE xtal_source)
Selects the external source for the XOSC32K oscillator.
void OSC32KCTRL_OSC32K_EnableOnDemand(void)
Enables On-Demand mode for the OSC32K oscillator.
bool OSC32KCTRL_XOSC32K_IsReady(void)
Checks if the XOSC32K oscillator is ready.
void OSC32KCTRL_XOSC32K_DisableOnDemand(void)
Disables On-Demand mode for the XOSC32K oscillator.
void OSC32KCTRL_EnableInterrupt(OSC32KCTRL_IRQ_SOURCE irq_source)
Enables the specified OSC32KCTRL interrupt source.
void OSC32KCTRL_XOSC32K_Enable(void)
Enables the XOSC32K oscillator.
void OSC32KCTRL_XOSC32K_SetStartUpTime(OSC32KCTRL_CSUT startup_time)
Sets the startup time for the XOSC32K oscillator.
void OSC32KCTRL_CFD_SetOsc32kPrescaler(OSC32K_CFD_PRESCALER prescaler)
Sets the OSC32K prescaler for CFD.
@ XOSC23K_32K_CYCLES
Definition plib_osc32kctrl.h:91
@ OSC32K_DIV_1
Definition plib_osc32kctrl.h:81
@ OSC32KCTRL_IRQ_CLKFAIL
Definition plib_osc32kctrl.h:102
@ XOSC32K_CRYSTAL
Definition plib_osc32kctrl.h:73
void OSCCTRL_OSCHF_DisableOnDemand(void)
Disables On-Demand mode for the OSCHF Oscillator.
bool OSCCTRL_OSCHF_IsReady(void)
Checks if the OSCHF Oscillator is ready.
void OSCCTRL_OSCHF_EnableAutotune(void)
Enables automatic tuning for the OSCHF Oscillator.
void OSCCTRL_OSCHF_SetFrequency(OSCCTRL_OSCHF_FREQ freq_sel)
This function sets the frequency selection value for the internal OSCHF Oscillator.
uint32_t SystemCoreClock
System core clock frequency. This global variable holds the current core (CPU) clock frequency in Her...
@ OSCCTRL_OSCHF_FREQ_8M
Definition plib_oscctrl.h:76

Macro Definition Documentation

◆ GCLKIN_CLOCK_FREQ

#define GCLKIN_CLOCK_FREQ   (12000000UL)

◆ OSCCTRL_WP_KEY

#define OSCCTRL_WP_KEY   (0x4F5343U)

Write protection key for OSCCTRL registers.

Typedef Documentation

◆ OSCCTRL_CALLBACK

typedef void(* OSCCTRL_CALLBACK) (void)

OSCHF ready event callback function type.

Parameters
None.

Enumeration Type Documentation

◆ OSCCTRL_OSCHF_FREQ

OSCHF frequency selection enumeration.

Enumerator
OSCCTRL_OSCHF_FREQ_1M 
OSCCTRL_OSCHF_FREQ_2M 
OSCCTRL_OSCHF_FREQ_3M 
OSCCTRL_OSCHF_FREQ_4M 
OSCCTRL_OSCHF_FREQ_8M 
OSCCTRL_OSCHF_FREQ_12M 
OSCCTRL_OSCHF_FREQ_16M 
OSCCTRL_OSCHF_FREQ_20M 
OSCCTRL_OSCHF_FREQ_24M 

Function Documentation

◆ CLOCK_GetPeripheralChannelFreq()

uint32_t CLOCK_GetPeripheralChannelFreq ( uint32_t peripheral_channel_id)

Fetches the clock frequency for a specific peripheral channel in Hz.

This function calculates and returns the frequency of a peripheral channel (GCLK peripheral control) by reading the assigned GCLK generator, its source, and division settings. It uses existing PLIB GCLK and OSCCTRL APIs to determine the actual frequency.

Parameters
peripheral_channel_idThe peripheral channel ID (e.g., SERCOM0_GCLK_ID_CORE, TC0_GCLK_ID). This must be a valid peripheral channel identifier from the device header.
Note
The peripheral channel must be enabled (via GCLK_EnablePeripheralChannel) and properly configured before calling this function. If the channel is disabled, this function may return 0.
Returns
The peripheral channel clock frequency in Hz. Returns 0 if the channel is disabled or if the frequency cannot be determined.

◆ OSCCTRL_DisableWriteProtection()

void OSCCTRL_DisableWriteProtection ( void )

Disables write protection for the OSCHF Oscillator registers.

Parameters
None.
Returns
None.

◆ OSCCTRL_EnableWriteProtection()

void OSCCTRL_EnableWriteProtection ( void )

Enables write protection for the OSCHF Oscillator registers.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_AutotuneIsEnabled()

bool OSCCTRL_OSCHF_AutotuneIsEnabled ( void )

Checks if the autotune feature is enabled for the OSCHF oscillator.

Parameters
None.
Returns
true if automatic tuning is enabled, false otherwise.

◆ OSCCTRL_OSCHF_ClearInterruptFlag()

void OSCCTRL_OSCHF_ClearInterruptFlag ( void )

Clears the ready interrupt flag for the OSCHF oscillator.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_DisableAutotune()

void OSCCTRL_OSCHF_DisableAutotune ( void )

Disables automatic tuning for the OSCHF Oscillator.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_DisableOnDemand()

void OSCCTRL_OSCHF_DisableOnDemand ( void )

Disables On-Demand mode for the OSCHF Oscillator.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_DisableReadyInterrupt()

void OSCCTRL_OSCHF_DisableReadyInterrupt ( void )

Disables the ready interrupt for the OSCHF Oscillator.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_EnableAutotune()

void OSCCTRL_OSCHF_EnableAutotune ( void )

Enables automatic tuning for the OSCHF Oscillator.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_EnableOnDemand()

void OSCCTRL_OSCHF_EnableOnDemand ( void )

Enables On-Demand mode for the OSCHF Oscillator.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_EnableReadyInterrupt()

void OSCCTRL_OSCHF_EnableReadyInterrupt ( void )

Enables the ready interrupt for the OSCHF Oscillator.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_GetFrequency()

uint32_t OSCCTRL_OSCHF_GetFrequency ( void )

Returns the current frequency of the internal OSCHF Oscillator.

Parameters
None.
Returns
The current frequency as per the frequency selection.

◆ OSCCTRL_OSCHF_GetFrequencyTune()

uint8_t OSCCTRL_OSCHF_GetFrequencyTune ( void )

Gets the current manual tuning value of the OSCHF oscillator.

Parameters
None.
Returns
The current manual tuning value.

◆ OSCCTRL_OSCHF_Initialize()

void OSCCTRL_OSCHF_Initialize ( void )

Initializes the OSCHF oscillator.

Parameters
None.
Returns
None.

◆ OSCCTRL_OSCHF_InterruptFlagIsSet()

bool OSCCTRL_OSCHF_InterruptFlagIsSet ( void )

Checks if the ready interrupt flag is set for the OSCHF oscillator.

Parameters
None.
Returns
true if interrupt flag is set, false otherwise.

◆ OSCCTRL_OSCHF_IsReady()

bool OSCCTRL_OSCHF_IsReady ( void )

Checks if the OSCHF Oscillator is ready.

Parameters
None.
Returns
true if the oscillator is ready, false otherwise.

◆ OSCCTRL_OSCHF_OnDemandIsEnabled()

bool OSCCTRL_OSCHF_OnDemandIsEnabled ( void )

Checks if the On-Demand feature is enabled for the OSCHF oscillator.

Parameters
None.
Returns
true if On-Demand mode is enabled, false otherwise.

◆ OSCCTRL_OSCHF_ReadyInterruptIsEnabled()

bool OSCCTRL_OSCHF_ReadyInterruptIsEnabled ( void )

Checks if the ready interrupt is enabled for the OSCHF oscillator.

Parameters
None.
Returns
true if interrupt is enabled, false otherwise.

◆ OSCCTRL_OSCHF_SetFrequency()

void OSCCTRL_OSCHF_SetFrequency ( OSCCTRL_OSCHF_FREQ freq_sel)

This function sets the frequency selection value for the internal OSCHF Oscillator.

Parameters
freq_selFrequency selection value to be set.
Returns
None.

◆ OSCCTRL_OSCHF_SetManualTune()

void OSCCTRL_OSCHF_SetManualTune ( uint8_t tune_value)

Sets the manual tuning value for the OSCHF Oscillator. Note that Autotune must be disabled to use manual tuning. Failing to do so will issue a pslverr.

Parameters
tune_valueThe manual tuning value to be set (2's complement format).
Returns
None

◆ OSCCTRL_OSCHF_SetReadyCallbackHandler()

void OSCCTRL_OSCHF_SetReadyCallbackHandler ( OSCCTRL_CALLBACK callback)

Registers a callback function for the OSCHF ready interrupt. This function stores a user-provided callback to be called when the OSCHF Oscillator ready interrupt occurs.

Parameters
callbackPointer to the callback function to be registered.
Returns
None.

◆ OSCCTRL_OSCHF_SetReadyInterruptFlag()

void OSCCTRL_OSCHF_SetReadyInterruptFlag ( void )

Asserts the ready interrupt flag for the OSCHF Oscillator. It is used for testing purposes to simulate the interrupt condition.

Parameters
None.
Returns
None.

◆ OSCCTRL_WPCTRL_EnableWriteLock()

void OSCCTRL_WPCTRL_EnableWriteLock ( void )

Enables the write protection lock for the WPCTRL register.

Parameters
None.
Returns
None.

◆ OSCCTRL_WPCTRL_WriteLockIsEnabled()

bool OSCCTRL_WPCTRL_WriteLockIsEnabled ( void )

Checks if the Write Protection Lock is enabled for the WPCTRL register.

Parameters
None.
Returns
true if write protection lock is enabled, false otherwise.

◆ OSCCTRL_WriteProtectionIsEnabled()

bool OSCCTRL_WriteProtectionIsEnabled ( void )

Checks if write protection is enabled for the OSCHF oscillator registers.

Parameters
None.
Returns
true if write protection is enabled, false otherwise.

Variable Documentation

◆ SystemCoreClock

uint32_t SystemCoreClock
extern

System core clock frequency. This global variable holds the current core (CPU) clock frequency in Hertz. Update the value with SystemCoreClockUpdate() whenever the core clock is changed.