/*****************************************************************************

   >>>>  Internal use.  Technical Support eyes only <<<<<

	rndownload.c
   Z-World Inc 2003

   This program will download and overwrite to a specified RabbitNet
   board.  It will first try to match the SPI port on the controller
   and the RabbitNet device.  If it cannot find the device, it will
   proceed on and ask for a physical node address which should be
   entered in octal form as 0xyz:

   			x = port number, 0 or 1;
            y = first level downstream ports, 0 to 6;
            z = second level downstream ports, 0 to 6;

   Make search criteria changes and path and s-record filename changes in
   the section below.

*****************************************************************************/
#class auto

//////
//	match search on port number and product id here
#define MATCHFLAG  RN_MATCH_PORT|RN_MATCH_PRDID
#define MATCHPORT 0           //port 0 or port 1 on the controller
#define MATCHPID RN1100			//RN1100 DI/0 board; RN1200 for AD board
////////////////////////////////////////////////////////

//////
// change path and S-record name here
#define SRECORDPATH	"I:\\Projects\\RabbitNet_DIGIO\\ST7Microcode\\rndio.S19"
////////////////////////////////////////////////////////

//////
// for debug purposes only
//#undef RNPRINTDEBUG
//#define RNPRINTDEBUG 1

#ximport SRECORDPATH  tempfile

/////
/// milli-second delay
/////
nodebug
void msDelay(unsigned int delay)
{
	auto unsigned long done_time;

	done_time = MS_TIMER + delay;
   while( (long) (MS_TIMER - done_time) < 0 );
}

/////
// waits for Yes or No user response
////
nodebug
void response()
{
	auto char yn;

   do {
    	yn = getchar();
   } while (yn != 'Y' && yn != 'y' && yn != 'N' && yn != 'n');

   if (yn=='N' || yn=='n')
   {
     	exit(0);
   }
}


main()
{
	auto int msg, device0;
   auto char buf[4];
	auto rn_devstruct *devaddr;
	auto rn_search newdev;

	brdInit();						//initialize controller
   rn_init(RN_PORTS, 1);      //intialize SPI ports

   //search for device match on
	newdev.flags = MATCHFLAG;
	newdev.ports = MATCHPORT;
	newdev.productid = MATCHPID;
  	if ((device0 = rn_find(&newdev)) == -1)
   {
  		printf("\nCannot find matching device %x. \n", device0);
	  	printf("\nDisregard and continue with download? Y or N \n");
      response();
   }

  	devaddr = (rn_devstruct *)device0;  //check table for match
   if (device0 != devaddr->handle)
   {
	  	printf("\nDevice handles do not match. Disregard and continue with download? Y or N \n");
		response();

	   device0 = devaddr->pna;
		printf("\nEnter physical node address in octal (ie: 0000)  ");
 		gets(buf);
 	   device0 = 0;
    	device0 = (buf[1]-0x30) << 6;
     	device0 |= (buf[2]-0x30) << 3;
	  	device0 |= (buf[1]-0x30);
   }

  	printf("\nReady to download %s\n", SRECORDPATH);
  	printf("\nThis will overwrite device code.  Continue? Y or N\n");
   response();

	printf("\nDownloading, please wait...........\n");
   msg = _rn_download(device0, tempfile);
   printf("\nMsg%02x Download complete wait 2 seconds for reset\n", msg);

   msDelay(2000);
   printf("\nDone\n");

}

