/*
	BL17PBUS.C

	Use with BL1700 boards and XP8100, XP8500 (ADC4), XP8700 (UART), and
	XP8800 (Stepper Motor) PLCBus	expansion boards.
	
	This sample program shows how to find the four expansion boards (listed
	above) connected on the PLCBus.  
		
	Instructions:
	Connect any and the limited quantity of the four listed expansion boards
	on the PLCbus.
	Compile and run this program.
*/

#use vdriver.lib
#use eziobl17.lib
#use eziopbdv.lib
#use ezioplc2.lib
#use aascurt2.lib
#use step2.lib

#define XP81_LIMIT 8
#define XP85_LIMIT 4
#define XP87_LIMIT 4
#define PAL0 	0				// installed PAL default number
int ID = 0;
char TITLE[] = {"Expansion Board Detection"};

main()
{
	auto int boardnum;
	auto unsigned long expboard;
	
	VdInit();
	eioBrdInit(0);												// initialize board
	printf("%s\n\n", TITLE);
	eioResetPlcBus();											//	reset the PLCBus
	eioPlcRstWait();											//	delay ensures the PLCBus boards reset
	
	// look for eight XP8100 boards
	for (boardnum=0; boardnum<XP81_LIMIT; boardnum++)			
		{
		if (plcXP81In(boardnum*32) != -1)
			printf("Located XP8100 board %d\n", boardnum);
		}
		
	// look for four XP8500 boards
	for (boardnum=0; boardnum<XP85_LIMIT; boardnum++)
		{
		expboard = _eioPlcADC4Addr(PAL0*4+boardnum);		// find board & transform address
		eioPlcAdr12((unsigned)expboard);									// now send the three BUSADRx
		if ((_eioReadD0()&1)==0)
			printf("Located XP8500 board %d\n", boardnum);
		}
	
	// look for four XP8700 boards
	for (boardnum=0; boardnum<XP87_LIMIT; boardnum++)
		{
	   expboard = 0x040000|(boardnum*8);					//15-bit uart board address
	   set24adr(expboard);
   	if ((read24data0(expboard)&1)==0)
			printf("Located XP8700 board %d\n", boardnum);
		}

	// look for sixteen XP8800 boards
    expboard = sm_find_boards();							// find total boards connected
    if (expboard != 0)
    	{
   	 for(boardnum=0; boardnum<expboard; boardnum++)
    		{
        	printf("Located XP8800 board %d for Board address %x \n",
        			boardnum, sm_addr[boardnum]);
        	sm_led( ID, 1);
        	}
    	}
}
