/*
**************************************************************************************************
* File: MainDemo.c
* Dependency: MainDemo.h
*
* Description:
* This program demonstrates simple IO control.
* Online debug with PICKit2/3 or ICD2/3 is also possible.
* For stand-alone application, uncomment the line 
* #define PROGRAMMABLE_WITH_USB_HID_BOOTLOADER
* in MainDemo.h file to make the output HEX of this project to be 
* able to be bootloaded using the HID bootloader.
*
* Programmer: John leung
* Company: TechToys Co. (www.TechToys.com.hk)
* Date: 7th Oct 2010
**************************************************************************************************
*/

#include "MainDemo.h"
		//details about these pragmas can be found at C:\MCC18\doc\hlpPIC18ConfigSet html help file
        #pragma config XINST    = OFF   	// Extended instruction set
        #pragma config STVREN   = ON      	// Stack overflow reset
        #pragma config PLLDIV   = 3         // (12 MHz crystal used on this board)
        #pragma config WDTEN    = OFF      	// Watch Dog Timer (WDT)
        #pragma config CP0      = OFF      	// Code protect
        #pragma config CPUDIV   = OSC1      // OSC1 = divide by 1 mode
        #pragma config IESO     = OFF      	// Internal External (clock) Switchover
        #pragma config FCMEN    = OFF      	// Fail Safe Clock Monitor
        #pragma config FOSC     = HSPLL     // Firmware must also set OSCTUNE<PLLEN> to start PLL!
        #pragma config WDTPS    = 32768
        #pragma config MSSPMSK  = MSK5
        #pragma config CCP2MX   = DEFAULT

/** PRIVATE PROTOTYPES *********************************************/

void YourHighPriorityISRCode(void);
void YourLowPriorityISRCode(void);


/** VECTOR REMAPPING ***********************************************/
#if defined(__18CXX)
	//On PIC18 devices, addresses 0x00, 0x08, and 0x18 are used for
	//the reset, high priority interrupt, and low priority interrupt
	//vectors.  However, the current Microchip USB bootloader 
	//examples are intended to occupy addresses 0x00-0x7FF or
	//0x00-0xFFF depending on which bootloader is used.  Therefore,
	//the bootloader code remaps these vectors to new locations
	//as indicated below.  This remapping is only necessary if you
	//wish to program the hex file generated from this project with
	//the USB bootloader.  If no bootloader is used, comment out 
	//the following define from MainDemo.h :
	//#define PROGRAMMABLE_WITH_USB_HID_BOOTLOADER
	
	#if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)
		#define REMAPPED_RESET_VECTOR_ADDRESS			0x1000
		#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS	0x1008
		#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS	0x1018
	#else	
		#define REMAPPED_RESET_VECTOR_ADDRESS			0x00
		#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS	0x08
		#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS	0x18
	#endif
	
	#if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)
	extern void _startup (void);        // See c018i.c in your C18 compiler dir
	#pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
	void _reset (void)
	{
	    _asm goto _startup _endasm
	}		
	#endif

	#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
	void Remapped_High_ISR (void)
	{
	     _asm goto YourHighPriorityISRCode _endasm
	}
	#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
	void Remapped_Low_ISR (void)
	{
	     _asm goto YourLowPriorityISRCode _endasm
	}
	
	#if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)
	#pragma code HIGH_INTERRUPT_VECTOR = 0x08
	void High_ISR (void)
	{
	     _asm goto REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS _endasm
	}
	#pragma code LOW_INTERRUPT_VECTOR = 0x18
	void Low_ISR (void)
	{
	     _asm goto REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS _endasm
	}
	#endif	//end of "#if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)"

	#pragma code
	
	
	//These are your actual interrupt handling routines.
	#pragma interrupt YourHighPriorityISRCode
	void YourHighPriorityISRCode()
	{
		//Check which interrupt flag caused the interrupt.
		//Service the interrupt
		//Clear the interrupt flag
		//Etc.
        #if defined(USB_INTERRUPT)
	        USBDeviceTasks();
        #endif
	
	}	//This return will be a "retfie fast", since this is in a #pragma interrupt section 
	#pragma interruptlow YourLowPriorityISRCode
	void YourLowPriorityISRCode()
	{
		//Check which interrupt flag caused the interrupt.
		//Service the interrupt
		//Clear the interrupt flag
		//Etc.
	
	}	//This return will be a "retfie", since this is in a #pragma interruptlow section 



#endif

#define swDelay1MS 		Delay1KTCYx(12);	//only true for a MCU clock at 48MHz

void swDelay(unsigned int msec)
{
	while(msec--)
		swDelay1MS;
}

void main (void)
{
	unsigned int pll_startup_counter = 600;
	OSCTUNEbits.PLLEN = 1;	//Enable the PLL and wait 2+ms until the PLL locks before enabling USB module
	while(pll_startup_counter--);

	mInitAllLEDs();
	batteryMonInit();
	usbSenseInit();

	while(1){
		Nop();
		mLED_b_On();		//visual indicator 
		swDelay(500);
		mLED_g_On();
		swDelay(500);
		mLED_r_On();
		swDelay(500);
		mLED_All_Off();
		swDelay(500);
		batteryMonCtrl = 1;	//On battery monitor, use a DMM to measure batterylevel/2 at J5 test point
		swDelay(100);
		batteryMonCtrl = 0;	//Off battery monitor
		swDelay(100);
		while (usbSenseInput == 1) mLED_All_Off();
		swDelay(100);
		while(softPowerKey == 0) mLED_All_Off();
		swDelay(100);
	} //end of while(1) infinite loop
}


