/* 
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 = ON           // 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_24         // 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 = PS1048576        // 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     //Assign the SRS to level 7 priority handlers

#include <xc.h>
#include <sys/attribs.h>
#include <sys/kmem.h>
#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 "UART.h"
#include "SystemMenu.h"
#include "Binary2ASCIIBCD.h"
#include "Delay.h"
#include "App_Screen.h"
#include "Strings.h"
#include "Interrupts.h"
#include "SplashImage.h"
#include "USB.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);
    
    // Prefetch-cache: Enable prefetch for cacheable PFM instructions
    CHECONbits.PREFEN = 1;

    //Disable JTAG Port
    DDPCONbits.JTAGEN = 0;
        
    //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;
    
    //Comm LEDs
    TRISFbits.TRISF2 = 0;
    TRISFbits.TRISF8 = 0;
    PORTFbits.RF2 = 1;
    PORTFbits.RF8 = 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(0);
                
    //RTC_Init
    RTC_Init();
    
    //Init the UART
    UART1_Init();
    
    //Wait for peripherals to initialize
    //RDY pin
//    while(!PORTAbits.RA7)
//    {
//        LED_Port(3);
//    }
    //Set the CP0 Status IE bit to turn on interrupts globally 
    __builtin_enable_interrupts();
    
    //enable /INTR interrupt
    IEC0bits.INT1IE = 0;
    
    REN71V30_RD(0x3ff);
    
    //enable INT2 interrupt
    IEC0bits.INT2IE = 0;

    //Clear the Screen
    HX8357B_CLRSCN(0xffff);
    
    //Turn on the Display
    HX8357B_DISPON();
    scrn_cnt = 1;

//************************************************************
    //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 then continues
    int wait_seconds = 5;   
    int current_seconds = RTCTIMEbits.SEC01;
    
    current_seconds = current_seconds + wait_seconds;
            
    while(current_seconds > RTCTIMEbits.SEC01);
    
//**********************************************************     
    
    //debug
    Flash_WR(0, 0);
    //while(1);
    
    getit = 0;
    
    //Clear the Screen
    HX8357B_CLRSCN(0xffff);
      //////////////////////////////////////////////
     //System GUI Begins                         //
    //////////////////////////////////////////////
    Header();    
    
    //Enter Menu System
    Home_Screen();
    
    return 0;
}
