/*
 * Copyright (c) 2020, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  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.
 *
 * *  Neither the name of Texas Instruments Incorporated 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 OWNER 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 "ti_msp_dl_config.h"

/**
 * Timer clock configuration to be sourced by LFCLK running at 32.768 KHz
 * This configures timer to generate 1 sec delay
 * ((32768 Hz / divideRatio / prescale) * TARGET DELAY) = period
 * ((32768 Hz / 1 / 1) * 1s) = 32768 - 1 = 32767 due to N+1 ticks in down mode
 */
#define TIMER_1_SEC_DELAY (32767)

int fault_count=0;
int edge_capture=0;

volatile bool gIsTimerExpired;
int main(void)
{
    bool isRetentionError;
    SYSCFG_DL_init();
    NVIC_EnableIRQ(PWM_0_INST_INT_IRQN);
    DL_SYSCTL_enableExternalClock(
                DL_SYSCTL_CLK_OUT_SOURCE_ULPCLK, DL_SYSCTL_CLK_OUT_DIVIDE_DISABLE);

        DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM47,IOMUX_PINCM47_PF_SYSCTL_CLK_OUT);//pa7
    DL_TimerA_startCounter(PWM_0_INST);


//        DL_TimerA_generateCrossTrigger(PWM_0_INST);//Mechanism to generate Software based Cross-Trigger

    DL_GPIO_setPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
    delay_cycles(1000);
    DL_GPIO_clearPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
    delay_cycles(1000);
    DL_GPIO_setPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);

        /* Enable TIM0 interrupts on device */


    gIsTimerExpired  = false;
    isRetentionError = true;

while(1);
}

void PWM_0_INST_IRQHandler(void)
{
    switch (DL_TimerA_getPendingInterrupt(PWM_0_INST)) {
        case DL_TIMER_IIDX_FAULT:
            fault_count++;
            break;
        case DL_TIMER_IIDX_CC0_UP:
            edge_capture++;
            break;
        default:
            break;
    }
}
