/*
  * \file
  *
  * \brief Example code for testing crystal stability and robustness. The
  * firmware will output the crystal clock on a I/O pin that can be probed
  * with a standard 10X oscilloscope probe. DO NOT probe the crystal directly
  * if you do not have very high impedance probes indeded for crystal
  * measurements. 
  *
  * Copyright (C) 2009 Atmel Corporation. All rights reserved.
  *
  * \page License
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
  *
  * 1. Redistributions of source code must retain the above copyright notice,
  * this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  * this list of conditions and the following disclaimer in the documentation
  * and/or other materials provided with the distribution.
  *
  * 3. The name of Atmel may not be used to endorse or promote products derived
  * from this software without specific prior written permission.
  *
  * 4. This software may only be redistributed and used in connection with an
  * Atmel AVR product.
  *
  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * DAMAGE.
  *
  */

// Include header files for GCC/IAR
#include "driver/avr_compiler.h"

// Include Clock system driver from application note AVR1003
#include "driver/clksys_driver.h"

int main(void)
{
    // Output system clock to port on PC7
    PORTC.DIR = 0x80;
    PORTCFG.CLKEVOUT |= PORTCFG_CLKOUT0_bm;

    // Select 32 kHz crystal and low power mode
    OSC.XOSCCTRL |= OSC_X32KLPM_bm;
    OSC.XOSCCTRL = (OSC.XOSCCTRL & ~OSC_XOSCSEL_gm) | OSC_XOSCSEL_32KHz_gc;

    // Switch to external crystal and disable 2 MHz RC oscillator
    CLKSYS_Enable(OSC_XOSCEN_bm);
    do
    {
    } while (CLKSYS_IsReady(OSC_XOSCRDY_bm) == 0);
    CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_XOSC_gc);
    CLKSYS_Disable(OSC_RC2MEN_bm);

    // Endless while loop
    while (1)
    {
    }
}