//******************************************************************************
//
//  KS10 Console Microcontroller
//
//! Sysclk object
//!
//! This file contains the sysclk implementation.
//!
//! \file
//!    sysclk.cpp
//!
//! \author
//!    Rob Doyle - doyle (at) cox (dot) net
//
//******************************************************************************
//
// Copyright (C) 2013 Rob Doyle
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//******************************************************************************
//! \addtogroup sysclk_api
//! @{
//

#include "stdio.h"
#include "sysclk.hpp"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/inc/hw_ints.h"
#include "driverlib/inc/hw_memmap.h"

//
//! sysclk state variable
//

static unsigned long time = 0;

//
//! Timer 0 Interrupt Handler
//!
//! This interrupt gets called every millisecond.  It merely increments the 
//! system timer.
//

void timer0IntHandler(void) {
    time++;
    ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
}

//
//! Constructor
//!
//! This function configures the timer 0 hardware to generate an interrupt every
//! millisecond.
//

sysclk_t::sysclk_t(void) {
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER);
    ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet() / HZ);
    ROM_TimerEnable(TIMER0_BASE, TIMER_A);
    ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    ROM_IntEnable(INT_TIMER0A);
}

//
//! Get the system timer contents. 
//!
//! \returns
//!    System time in units of "HZ"
//

unsigned long sysclk_t::time(void) {
    return ::time;
}

//
//! static constructor
//!
//! This creates a static sysclk at startup.
//

sysclk_t sysclk;

//
//! @}
//
