/*
 * Copyright (c) 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o 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.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 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 "fsl_device_registers.h"
#include <stdio.h>


#define TIE	2
#define TEN	1
uint32_t c;
uint32_t b;
uint32_t preva;
uint32_t prevb;
uint32_t a;

uint32_t TimerValue = 0xfff;
static int i = 0;

int main(void)
{
    /* Write your code here */
	SIM_SCGC5 |=  SIM_SCGC5_PORTB_MASK;  //assign clock to PortB
	PORTB_PCR3 = PORT_PCR_MUX(1); //Enable GPIO0
	GPIOB_PDDR = (1 << 3); //set as output pin
				SIM_SCGC6 |= (1 << 29);  	//	enable clock to RTC

					  	  		RTC_CR |= (1 << 0); //software reset
					  	  		RTC_CR = 0;
					  	  		RTC_CR |= (1 << 8);			// Enable Oscillator for clock.
					  	  		RTC_TSR = 111111; //init to something
					  	  		RTC_TPR = 11111; //init to something
					  	  		RTC_SR |= (1 << 4);			// Enable Time Counter

					  	  	prevb = (RTC_TSR <<16) & RTC_TPR; //set start time point

    /* This for loop should be replaced. By default this loop allows a single stepping. */
    for (;;) {
    	i++;
    	b = (RTC_TSR <<16) | RTC_TPR;
    	if (b > prevb + 589) {

    			GPIOB_PTOR = (1 << 3); //generate toggle of GPIO0 on the K64 TWR board
    			prevb = b;
    			for (c=0;c < 500;c++) a=b+c;
    			GPIOB_PTOR = (1 << 3); //generate toggle of GPIO0 on the K64 TWR board
    	}

    }
    /* Never leave main */
    return 0;
}
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////
