/*
 * Copyright (c) 2015, 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.
 */

/*
 *  ======== gpiointerrupt.c ========
 *
 */



/* Example/Board Header files */
#include "Board.h"

/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/System.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
/* TI-RTOS Header files */
#include <ti/sysbios/knl/Semaphore.h>


#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_nvic.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_gpio.h"
#include "driverlib/flash.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_can.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/epi.h"
#include "driverlib/ssi.h"
#include "driverlib/i2c.h"
#include "driverlib/pwm.h"
#include "driverlib/qei.h"
#include "driverlib/can.h"
#include <ti/drivers/GPIO.h>


/// WAS GIVING UNRESOLVED SYMBOL ERROR FOR ROM_UpdateUART HENCE COPIED
//THE FOLLOWING DEFINE HERE FRIM rom.h
#define ROM_UpdateUART                                                        \
        ((void (*)(void))ROM_UARTTABLE[21])

/* variable to be read by GUI Composer */
int count = 0;

/*
 *  ======== gpioButtonFxn0 ========
 *  Callback function for the GPIO interrupt on Board_BUTTON0.
 */
void gpioButtonFxn0(unsigned int index)
{

    System_printf("\n entered interrupt count=%d\n",count);
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();
    /* Clear the GPIO interrupt and toggle an LED */
//    GPIO_toggle(Board_LED0);
//
//    if (count++ == 100) {
//        count = 0;
//    }
 // ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);


    GPIO_write(Board_LED0, Board_LED_ON);

    IntMasterDisable();
    ROM_UpdateUART();



//    HWREG(NVIC_DIS0) = 0xffffffff;
//    HWREG(NVIC_DIS1) = 0xffffffff;
//    HWREG(NVIC_DIS2) = 0xffffffff;
//    HWREG(NVIC_DIS3) = 0xffffffff;
//
//    //
//    // Return control to the boot loader.  This is a call to the SVC
//    // handler in the boot loader.
//    //
//   (*((void (*)(void))(*(uint32_t *)0x2c)))(); // terminates exicution
}

/*
 *  ======== gpioButtonFxn1 ========
 *  Callback function for the GPIO interrupt on Board_BUTTON1.
 *  This may not be used for all boards.
 */
//void gpioButtonFxn1(unsigned int index)
//{
//    /* Clear the GPIO interrupt and toggle an LED */
//    GPIO_toggle(Board_LED1);
//
//    if (count++ == 100) {
//        count = 0;
//    }
//}

/*
 *  ======== main ========
 */
int main(void)
{
    /* Call board init functions */
    Board_initGeneral();
    Board_initGPIO();
    Board_initUART();
    //
    // Configure the UART for 115200, n, 8, 1
    //
    UARTConfigSetExpClk(UART0_BASE, 120000000, 115200,
                            (UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE |
                             UART_CONFIG_WLEN_8));        // do i need to configure UART like this????????????????????????

    //
    // Enable the UART operation.
    //
    UARTEnable(UART0_BASE);

    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_OFF);

    System_printf("Starting the GPIO Interrupt example\nSystem provider is set"
                  " to SysMin. Halt the target to view any SysMin contents in"
                  " ROV.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();

    /* install Button callback */
    GPIO_setCallback(Board_BUTTON0, gpioButtonFxn0);

    /* Enable interrupts */
    GPIO_enableInt(Board_BUTTON0);

    /*
     *  If more than one input pin is available for your device, interrupts
     *  will be enabled on Board_BUTTON1.
     */
//    if (Board_BUTTON0 != Board_BUTTON1) {
//        /* install Button callback */
//        GPIO_setCallback(Board_BUTTON1, gpioButtonFxn1);
//        GPIO_enableInt(Board_BUTTON1);
//    }

    /* Start BIOS */
    BIOS_start();

    return (0);
}
