Clock Manager Usage Scenarios#

Typical Use Case: Peripheral Clock Configuration#

This example shows how to configure and verify clock settings for timer peripherals in precision timing applications:

#include "sl_clock_manager.h"
#include "sl_device_peripheral.h"
#include "sl_hal_timer.h"

// Configure high-precision timer with verified clock frequency
sl_status_t configure_precision_timer(void) {
  sl_status_t status;
  uint32_t timer_frequency;
  sl_clock_branch_t timer_clock_branch;
  sl_hal_timer_handle_t timer_handle;
  sl_hal_timer_config_t timer_config = SL_HAL_TIMER_CONFIG_DEFAULT;
  
  // Step 1: Enable timer bus clock.
  status = sl_clock_manager_enable_bus_clock(SL_BUS_CLOCK_TIMER0);
  if (status != SL_STATUS_OK) {
    return status;
  }
  
  // Step 2: Get timer's clock branch and verify frequency.
  timer_clock_branch = sl_device_peripheral_get_clock_branch(SL_PERIPHERAL_TIMER0);
  status = sl_clock_manager_get_clock_branch_frequency(timer_clock_branch, &timer_frequency);
  if (status != SL_STATUS_OK) {
    return status;
  }
  
  // Step 3: Configure timer with known frequency.
  timer_config.prescale = SL_HAL_TIMER_PRESCALE_1; // No prescaling.
  timer_config.mode = SL_HAL_TIMER_MODE_UP;
  
  status = sl_hal_timer_init(&timer_handle, SL_HAL_TIMER_INSTANCE_0, &timer_config);
  if (status != SL_STATUS_OK) {
    return status;
  }
  
  // Step 4: Calculate and set timer values for precise timing.
  uint32_t timer_counts = timer_frequency / 1000; // 1 ms timer.
  status = sl_hal_timer_set_compare(&timer_handle, 0, timer_counts);
  
  printf("Timer configured: %lu Hz, %lu counts per ms\n", 
         timer_frequency, timer_counts);
  
  return status;
}

For comprehensive usage examples including peripheral clock configuration, oscillator calibration, clock export, advanced calibration techniques, and debugging scenarios, refer to the Extended API Examples section.