#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_gpio.h"
#include "inc/hw_types.h"
#include "inc/hw_sysctl.h"
#include "driverlib/debug.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "module.h"
#include "M3util.h"

int main(void) {
    // Disable Protection
    HWREG(SYSCTL_MWRALLOW) =  0xA5A5A5A5;

    // Sets up PLL, M3 running at 100MHz and C28 running at 100MHz
    SysCtlClockConfigSet(SYSCTL_USE_PLL | (SYSCTL_SPLLIMULT_M & 0xA) |
                         SYSCTL_SYSDIV_1 | SYSCTL_M3SSDIV_1 |
                         SYSCTL_XCLKDIV_4);

    // Enable clock supply for GPIOC
//    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);

    // Disable clock supply for the watchdog modules
    SysCtlPeripheralDisable(SYSCTL_PERIPH_WDOG1);
    SysCtlPeripheralDisable(SYSCTL_PERIPH_WDOG0);

    // Setup hardware UART pins.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
	GPIOPinUnlock(GPIO_PORTJ_BASE, GPIO_PIN_7 | GPIO_PIN_6);
    GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_7 | GPIO_PIN_6);

    // Enable processor interrupts.
    IntMasterEnable();

    int state = 0;
    while(1){
        GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_7, state);
        delay(100);
        GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_6, ~state);
        delay(100);
        state ^= 0xffff;
    }
}
