// serial download test
// define this for a dedicated direct serial connection
#define DIRECT_SERIAL
//REDIRECT RUNTIME STDIO TO PORT A, YOU CAN USE THE DIAG CONNECTOR ON THE PROGRAMMING CABLE TO READ IN ON A PC
//#define DL_DEBUG debug
#define STDIO_DEBUG_SERIAL  SADR
#define STDIO_DEBUG_BAUD	115200L
#define	STDIO_DEBUG_ADDCR

#define SERIAL_LOADER "P:/SHDesigns/SofTools/Serdownl/ZW-Loaders/SDL-Generic-BD-Dbg.bin"
#define BAUD_RATE 115200
// define the buffer sizes, we have lots of mem so use large buff to fit full packet
// data is used on RAM loader
// change 'B' to port used.
//#define SERB_USEPORTD
#define USE_SERIAL_B

// init I/O ports in DLM.
#define SERDL_IOINIT {PDDR,0x1,PDDDR,0x1,PEDR,(char)~0x40,PEDDR,0x40,0}

#use serdownl.lib

main()
{
	printf("SEGSIZE=%x\n",RdPortI(SEGSIZE));
	printf("MMIDR=%x\n",RdPortI(MMIDR));
	printf("MECR=%x\n",RdPortI(MECR));
	printf("MTCR=%x\n",RdPortI(MTCR));
	printf("MB0CR=%x\n",MB0CRShadow);
	printf("MB1CR=%x\n",MB1CRShadow);
	printf("MB2CR=%x\n",MB2CRShadow);
	printf("MB3CR=%x\n",MB3CRShadow);
	printf("GCSR=%x\n",GCSRShadow);
	printf("GCSRIO=%x\n",RdPortI(GCSR));
	printf("STACKSEG=%x\n",RdPortI(STACKSEG));
	printf("DATASEG=%x\n",RdPortI(DATASEG));
	printf("RAMCS=%x\n",CS_RAM);
	SerDL_Init("Serial download test - Using Generic Loaders.");
		/*  1. Convert the I/O ports.  Disable slave port which makes
	 *     Port A an output, and PORT E not have SCS signal.
	 */
//	WrPortI(SPCR, & SPCRShadow, 0x84);

	/*  2. Read function shadow and set PE1 and PE7 as normal I/O.
	 *     LED's are conencted to PE1 and PE7, make them outputs.
	 *     Using shadow register preserves function of other Port E bits.
	 */
	WrPortI(PEFR,  & PEFRShadow,  ~((1<<7)|(1<<1)) & PEFRShadow);
	WrPortI(PEDDR, & PEDDRShadow, (1<<7)|(1<<1));

	/*  3. Turn on DS2 (0 -> PE1) and turn off DS3 (1 -> PE7).
	 */
	WrPortI(PEDR, & PEDRShadow, (1<<7));

	printf("Ready to download over me!\n");

   for(;;) {					// endless loop

     // First task will flash LED DS2 for 200ms once per second.
     costate {

		 WrPortI(PEB1R, NULL, 0);      			// Control DS3. Output 0 to turn LED on.
       waitfor(DelayMs(200));						// wait 200 milliseconds
       WrPortI(PEB1R, NULL, 1<<1);				// turn LED off
       waitfor(DelayMs(800)); 					// wait 800 milliseconds
     }
     costate
     {
			SerDL_Tick();
//		SerLoad(2, 2400, "APHD REMOTE UPDATE (PHONE)", "AT\rAT&F\rATS0=0\r", "4079499049");
		}
	}
}
