/* 
Main.c
Larry Knight
MainBrain
Revision 1.0 2021
PIC32MX795F512L-80I/PF
*/

#pragma config FMIIEN = OFF             // Ethernet RMII/MII Enable (MII Enabled)
#pragma config FETHIO = OFF             // Ethernet I/O Pin Select (Default Ethernet I/O)
#pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by USB Module)
#pragma config FNOSC = PRIPLL           // Oscillator Selection Bits 
#pragma config POSCMOD = EC             // Primary Oscillator Configuration
#pragma config FPLLIDIV = DIV_5         // PLL Input Divider
#pragma config FSOSCEN = ON             // Secondary Oscillator Enable (Disabled)
#pragma config FPLLMUL = MUL_20         // PLL Multiplier 
#pragma config FPLLODIV = DIV_1         // System PLL Output Clock Divider 
#pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor 
#pragma config UPLLIDIV = DIV_5         // USB PLL Input Divider
#pragma config UPLLEN = ON              // USB PLL Enable (Enabled)
#pragma config IESO = OFF               // Internal/External Switch Over (Enabled)
#pragma config OSCIOFNC = ON            // CLKO Output Signal Active on the OSCO Pin 
#pragma config WDTPS = PS1              // Watchdog Timer Postscaler (1:1048576)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Clock Switch Enable, FSCM Enabled)
#pragma config DEBUG = OFF              // Background Debugger
#pragma config FSRSSEL = PRIORITY_7     // All interrupt priorities are assigned to a shadow register set

//Required Headers
#include <xc.h>
#include <sys/attribs.h>

//Application Headers
#include "ADC.h"
#include "Binary2ASCIIHex.h"
#include "Globals.h"
#include "HX8357B.h"
#include "LED_Port.h"
#include "PMP.h"
#include "REN71V30.h"
#include "RTC.h"
#include "Screens.h"
#include "Flash.h"
#include "Timers.h"
#include "Binary2ASCIIBCD.h"
#include "Delay.h"
#include "Strings.h"
#include "Interrupts.h"
#include "SplashImage.h"

int main()
{
    //Wait for PLL Lock
    while(!OSCCONbits.SLOCK);
    
    SYSKEY = 0x0;               // ensure OSCCON is locked
    SYSKEY = 0xAA996655;        // Write Key1 to SYSKEY
    SYSKEY = 0x556699AA;        // Write Key2 to SYSKEY

    //enable secondary oscillator
    OSCCONbits.SOSCEN = 1;    
    //wait for warm up
    while(OSCCONbits.SOSCRDY == 0);
    
    //turn on USB power
    U1PWRCbits.USBPWR = 1;
    
    //Data Memory SRAM wait states: Default Setting = 1; set it to 0
    BMXCONbits.BMXWSDRM = 0;

    //Flash PM Wait States: MX Flash runs at 2 wait states @ 80 MHz
    CHECONbits.PFMWS = 2;

    //Prefetch-cache: Enable prefetch for cacheable PFM instructions
    CHECONbits.PREFEN = 1;

    //Disable JTAG Port
    DDPCONbits.JTAGEN = 0;
    
    //Set the Core Timer compare value
    //interrupts every 2 clock cycles
    _CP0_SET_COMPARE(4800000);
    
    //SetComm LEDs
    TRISFbits.TRISF2 = 0;
    TRISFbits.TRISF8 = 0;  
    //Turn OFF LEDs
    PORTFbits.RF2 = 1;
    PORTFbits.RF8 = 1;

    //Setup the GP I/O pins as inputs
    //GP4
    TRISAbits.TRISA14 = 1;
    //GP3
    TRISAbits.TRISA15 = 1;
    //GP2
    TRISDbits.TRISD8 = 1;
    //GP1
    TRISAbits.TRISA6 = 1;
    //GP0
    TRISAbits.TRISA7 = 1;
    
    //Initialize the LED Data Port
    LED_Port_Init();

    //Initialize the Interrupts
    Interrupts_Init();

    //Initialize Dual Port RAM
    REN71V30_Init();
    
    memtest_71V30(); 
        
    //Initialize the ADC
    ADC_Init();
   
    //Initialize Timers
    Timer2_Init();
    Timer3_Init();
    Timer4_Init();
    Timer5_Init();
    
    //Initialize the Parallel Master Port
    PMP_Init();
    
    //Initialize Flash Memory
    Flash_Init();

    //get number of used bytes
    unsigned d = PMADDR;
    Flash_Get_Bytes_Used();
    PMADDR = d;
    
    //Initialize the Display
    HX8357B_Init();            
    
    //control is with a P-channel MOSFET
    //so the higher the number, the dimmer
    //the screen
    Backlight_Control(5);
                
    //RTC_Init
    RTC_Init();
    
    //Clear the Core Timer
    _CP0_SET_COUNT(0);
    
    //Global Enable IUnterrupts
    __builtin_enable_interrupts();  
    
    //Clear the Screen
    HX8357B_CLRSCN(0xffff);
    
    //Turn on the Display
    HX8357B_DISPON();

    //Motion_Screen();
    
        //Load Splash Image
        HX8357B_CASET(0, 479);
        HX8357B_RASET(120, 170);
        HX8357B_RAMWR();

        //Data
        //RF3 = D/C 1=Data, 0=Command
        PORTFbits.RF3 = 1;                    
        for(int i=0;i<=23040; i++)
        {

            PMDIN = ~SplashImage[i*2];
            while(PMMODEbits.BUSY == 1);    
        }

        //Select Display (/CS)    
        PORTFbits.RF13 = 0;
    
   //waits for 5 seconds
    int wait_seconds = 5;   
    int current_seconds = RTCTIMEbits.SEC01;
    
    current_seconds = current_seconds + wait_seconds;
            
    while(current_seconds > RTCTIMEbits.SEC01);
    
//**********************************************************   
    
    //Clear the Screen
    HX8357B_CLRSCN(0xffff);
    
//    //Turn off the Display
//    HX8357B_DISPOFF();
    
       U1PWRCbits.USBPWR = 1;
   
    //wait for PLL lock
    while(OSCCONbits.ULOCK == 0);       

    //Module Enable
    U1CONbits.USBEN = 1;
    
    REN71V30_WR(0x0, 5);
    REN71V30_RD(0x0);
    LED_Port(mdata_71V30);
    
    Home_Screen();
    
    while(1);
    
    return 0;
}
