Microchip® Advanced Software Framework

osc8_calib.c File Reference

SAM OSC8MHz Calibration Application.

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

#include <asf.h>
#include "conf_example.h"

Macros

#define CALIBRATION_RESOLUTION   CONF_CALIBRATION_RESOLUTION
 Resolution of the calibration binary divider; lower powers of two will reduce the calibration resolution. More...
 
#define COMM_CAL_MAX   0x7F
 
#define COMM_CAL_MIN   0x00
 
#define FRANGE_CAL_MAX   0x03
 
#define FRANGE_CAL_MIN   0x00
 OSC8M calibration info. More...
 
#define REFERENCE_CLOCK   GCLK_GENERATOR_3
 Calibration reference clock generator index. More...
 
#define REFERENCE_CLOCK_HZ   32768
 Frequency of the calibration reference clock in Hz. More...
 
#define TARGET_FREQUENCY   8000000
 Target OSC8M calibration frequency. More...
 
#define TEMP_CAL_MAX   0x1F
 
#define TEMP_CAL_MIN   0x00
 
#define TEMP_CAL_OFFSET   0x07
 

Functions

static uint32_t get_osc_frequency (void)
 Retrieves the current system clock frequency, computed from the reference clock. More...
 
int main (void)
 
static void setup_clock_out_pin (void)
 Set up the clock output pin so that the current system clock frequency can be monitored via an external frequency counter or oscilloscope. More...
 
static void setup_events (struct events_resource *event)
 Set up the event system, linking the measurement and comparison timers so that events generated from the reference timer are linked to the measurement timer. More...
 
static void setup_tc_channels (void)
 Set up the measurement and comparison timers for calibration. More...
 
static void setup_tc_events (void)
 Set up the measurement and comparison timer events. More...
 
static void setup_usart_channel (void)
 Set up the USART for transmit-only communication at a fixed baud rate. More...
 

Variables

static struct tc_module tc_calib
 Software instance of the TC module used for calibration measurement. More...
 
static struct tc_module tc_comp
 Software instance of the TC module used for calibration comparison. More...
 
static struct usart_module usart_edbg
 Software instance of the USART upon which to transmit the results. More...
 

#define CALIBRATION_RESOLUTION   CONF_CALIBRATION_RESOLUTION

Resolution of the calibration binary divider; lower powers of two will reduce the calibration resolution.

Referenced by get_osc_frequency(), and setup_tc_channels().

#define COMM_CAL_MAX   0x7F

Referenced by main().

#define COMM_CAL_MIN   0x00

Referenced by main().

#define FRANGE_CAL_MAX   0x03

Referenced by main().

#define FRANGE_CAL_MIN   0x00

OSC8M calibration info.

Referenced by main().

#define REFERENCE_CLOCK   GCLK_GENERATOR_3

Calibration reference clock generator index.

Referenced by setup_tc_channels().

#define REFERENCE_CLOCK_HZ   32768

Frequency of the calibration reference clock in Hz.

Referenced by get_osc_frequency().

#define TARGET_FREQUENCY   8000000

Target OSC8M calibration frequency.

Referenced by main().

#define TEMP_CAL_MAX   0x1F

Referenced by main().

#define TEMP_CAL_MIN   0x00

Referenced by main().

#define TEMP_CAL_OFFSET   0x07

Referenced by main().

static uint32_t get_osc_frequency ( void  )
static

Retrieves the current system clock frequency, computed from the reference clock.

Returns
Current system clock frequency in Hz.

References CALIBRATION_RESOLUTION, REFERENCE_CLOCK_HZ, tc_calib, tc_clear_status(), tc_comp, TC_COMPARE_CAPTURE_CHANNEL_0, tc_get_capture_value(), tc_get_status(), tc_start_counter(), and TC_STATUS_CHANNEL_0_MATCH.

Referenced by main().

static void setup_clock_out_pin ( void  )
static

Set up the clock output pin so that the current system clock frequency can be monitored via an external frequency counter or oscilloscope.

References system_pinmux_config::mux_position, system_pinmux_get_config_defaults(), and system_pinmux_pin_set_config().

Referenced by main().

static void setup_events ( struct events_resource *  event)
static

Set up the event system, linking the measurement and comparison timers so that events generated from the reference timer are linked to the measurement timer.

Referenced by main().

static void setup_tc_channels ( void  )
static

Set up the measurement and comparison timers for calibration.

  • Configure the measurement timer to run from the CPU clock, in capture mode.
  • Configure the reference timer to run from the reference clock, generating a capture every time the calibration resolution count is met.

References CALIBRATION_RESOLUTION, tc_config::clock_prescaler, tc_config::clock_source, tc_16bit_config::compare_capture_channel, tc_config::counter_16_bit, tc_config::counter_size, tc_config::enable_capture_on_channel, REFERENCE_CLOCK, tc_calib, TC_CLOCK_PRESCALER_DIV1, tc_comp, TC_COUNTER_SIZE_16BIT, TC_COUNTER_SIZE_32BIT, tc_get_config_defaults(), tc_init(), TC_WAVE_GENERATION_NORMAL_FREQ, and tc_config::wave_generation.

Referenced by main().

static void setup_tc_events ( void  )
static

Set up the measurement and comparison timer events.

  • Configure the reference timer to generate an event upon comparison match with channel 0.
  • Configure the measurement timer to trigger a capture when an event is received.

References tc_events::generate_event_on_compare_channel, tc_events::on_event_perform_action, tc_calib, tc_comp, tc_enable(), and tc_enable_events().

Referenced by main().

static void setup_usart_channel ( void  )
static

struct tc_module tc_calib
static

Software instance of the TC module used for calibration measurement.

Referenced by get_osc_frequency(), setup_tc_channels(), and setup_tc_events().

struct tc_module tc_comp
static

Software instance of the TC module used for calibration comparison.

Referenced by get_osc_frequency(), setup_tc_channels(), and setup_tc_events().

struct usart_module usart_edbg
static

Software instance of the USART upon which to transmit the results.

Referenced by setup_usart_channel().