/*
	 SM_DEMO2.C

    Sample program for the IAB239 (XP8800/Stepper Motor Expansion Board).

    This program does the following:

    (a) Finds all the available IAB239 (XP8800) boards.
    (b) Sets each board to generate 4000 pulses starting with low speed and
        ramping up to high speed, in each direction. Operates on high speed
        until the ramp down point of 600 pulses is reached. The last 593
        pulses are traversed at low speed.
    (c) Enables SD+- detection so that when SD+- is tied to low during high
        speed operation the PCL-AK automatically ramps down to low speed.
        If SD+- is released, the PCL-AK ramps back to high speed.
    (d) Shows the status ending the operation of each IAB239 (XP8800).

    For the interrupt driven version, run sample program sm_demo3.c.
*/

#if ((BOARD_TYPE == L_STAR) || (BOARD_TYPE == CPLC_BOARD))
#use cplc.lib
#endif

main()
{
	int total_boards, i, direction;
	int status0;

#if ((BOARD_TYPE == L_STAR) || (BOARD_TYPE == CPLC_BOARD))
	uplc_init();
#endif

	reset_pbus();
	reset_pbus_wait();

	total_boards = sm_find_boards();
	if(total_boards == 0)
	{
		printf("No IAB239 connected!!!\n");
		printf("Cannot proceed with the Sample Program!!!\n");
		while(1)
		{
			runwatch();
		}
	}

	printf("Found the following boards:\n");
	for(i = 0; i < total_boards; i++)
	{
		sm_board_reset(i);		// complete reset for all boards
		printf("Index %d for Board address %x \n", i, sm_addr[i]);
	}

	for(i = 0; i < total_boards; i++)
	{
		for(direction = 1; direction >= 0; --direction)
		{
			printf("Testing Board %d (%x) '%s' direction.\n", i, sm_addr[i],
			       direction ? "+" : "-");
			printf("Tie SD%s low to see automatic ramp down!!!\n",
			       direction ? "+" : "-");
			sm_drvoe(i,1);			// enable the driver
			sm_led(i, 1);			// turn on LED for current board
			smc_softreset(i);		// soft reset the PCL-AK
			smc_setmove(i, 4000L, 50, 400, 500, 600, 732);
					// Generate 4000 counts ramping from 50pps to
					// 400pps in ( 400 - 50) x 500 x 1/6000000 secs.
					// Ramp down from 400pps to 50pps when 600 counts
					// is left and traverse the last 593 steps at low speed.
			smc_cmd(i, direction ? 0x46 : 0x4E);
					// Preset enable, +- direction, SD enabled.
					// With SD enabled, when SD+- is low the PCL-AK
					// will automatically ramp down to the lower speed
					// if it is running at the higher speed. If SD+- is
					// released, the PCL-AK will ramp up to the higher
					// speed again.
			smc_cmd(i, 0x15);
                // start move, high speed, choose high speed to show
                // ramping
			while(smc_stat0(i) & 0x40)
			{
				runwatch();
			}
			status0 =  smc_stat0(i);
					// check what caused the operation to stop

			if(status0 & 0x01)
			{
				// can only happen in '-' direction
				printf(">>>>>EL- signal low detected.\n");
			}

			if(status0 & 0x02)
			{
				// can only happen in '+' direction
				printf(">>>>>EL+ signal low detected.\n");
			}

			if(status0 & 0x08)
			{
				// done this direction!
				printf(">>>>>PCL-AK counter output is zero.\n");
			}

			sm_board_reset(i);			// complete reset
		}
	}
}
