/*	Title	All-Pro Diagnostics*/
/******************************************************************************/
/*				     Diagmisc				      */
/*			       All-pro Diagnostic Pin driver tests	      */
/*									      */
/* Module ...................... DIAGMISC				      */
/* Title ....................... All-pro Diagnostic Pin driver tests	      */
/* Developing Engineer ......... Steven T. Harr 			      */
/* Company ..................... Logical Devices			      */
/* Revision Level .............. 1.00					      */
/* Date of Last Revision ....... 17-FEB-87				      */
/* Last Revision by ............ STH					      */
/*									      */
/*									      */
/*  Revision History							      */
/*  v3.0		STH	Convert for Allpro 88.			      */
/*									      */
/*									      */
/******************************************************************************/
/*	Subttl	Includes*/
#include <stdio.h>
#include <ctype.h>
#include "pindrivr.h"
#include "keybrd.h"
#include "diag2.h"
#include "windows.h"
#include "window.h"


#define TOTAL_PINS		    88
#define ERR_PULLDOWN		    0
#define ERR_PULL_UP		    1
#define ERR_GROUND		    2
#define ERR_TTL 		    3
#define ERR_SOURCE		    4
#define ERR_CURRENT		    5
#define ERR_READ		    6
#define ERR_ANALOG		    7
#define ERR_MAX 		    8

/*#define TOL			      3*/
#define TOL	((volt_expected[pin]/50)+2)

unsigned char error_record [TOTAL_PINS+1] [ERR_MAX];
extern unsigned int max_pins;

struct ic_pin_coord_struc
{
    char row;
    char col;
} pin_coords [] =
{
    0,0,
    21,18, 20,18, 19,18, 18,18, 17,18, 16,18, 15,18, 14,18, 13,18,
    12,18, 11,18, 10,18,  9,18,  8,18,	7,18,  6,18,  5,18,  4,18,  3,18,  2,18,

     2, 4,  3, 4,  4, 4,  5, 4,  6, 4,	7, 4,  8, 4,  9, 4, 10, 4,
    11, 4, 12, 4, 13, 4, 14, 4, 15, 4, 16, 4, 17, 4, 18, 4, 19, 4, 20, 4, 21,4,

     2,32,  3,32,  4,32,  5,32,  6,32,	7,32,  8,32,  9,32, 10,32,
    11,32, 12,32, 13,32, 14,32, 15,32, 16,32, 17,32, 18,32, 19,32, 20,32, 21,32,
     2,46,  3,46,  4,46,  5,46,  6,46,	7,46,  8,46,  9,46, 10,46,
    11,46, 12,46, 13,46, 14,46, 15,46, 16,46, 17,46, 18,46, 19,46, 20,46, 21,46,
     2,60,  3,60,  4,60,  5,60,  6,60,	7,60,  8,60,  9,60, 10,60,
    11,60, 12,60, 13,60, 14,60, 15,60, 16,60, 17,60, 18,60, 19,60, 20,60, 21,60
};



/* externals */
extern func_null ();
extern func_help ();
extern int cursor_top;
extern int cursor_bottom;
extern int vid_mode;
extern int real_pin [];
extern unsigned int phys_pin_offset;
extern allpro88_flag;
extern se6440_flag;

    unsigned char left_pin = 182;
    unsigned char right_pin = 199;
    unsigned char top_ic [16]  = {201,205,205,205,205,205,184, 32,213,205,205,205,205,205,187,0};
    unsigned char bott_ic [16] = {200,205,205,205,205,205,205,205,205,205,205,205,205,205,188,0};

int selected_zif_row = 0;
int selected_zif_col = 0;


/*	Subttl	Burn in test */
/******************************************************************************/
/*  The burn in test will exersize all of the pin driver as follows.	      */
/*   Note: This test requires a test header with 10 ohm resistors across each */
/*	   pair of pins  (i.e. pins 1 to 40, pins 2 to 39 etc ).	      */
/*	1. Disconnect test, test all pins for disconnect.		      */
/*	2. Pull up test, pull up pins and read voltage. 		      */
/*	3. Ground test, with pull-ups on ground pins and read voltage.	      */
/*	4. TTL hi test, turn on and read voltage, turn off and read voltage.  */
/*	5. Source driver test, step through levels and read voltage.	      */
/*	6. Read test, test accuracy of read drivers.			      */
/*	7. Current limit test, test source driver self limit.		      */
/*	8. Test analog board.						      */
/******************************************************************************/
burn_in_test ()
{
    int i;
    int j;
    int pin;
    int error_stop;
    long pass;
    unsigned int pin_voltage;

    for (i = 1; i <= max_pins; i++)
    {
	for (j = 0; j < ERR_MAX; j++)
	{
	    error_record [i] [j] = '.';
	}
    }
    reset_allpro();
    udi_write_mem (ANALOG_CONTROL, DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);			/* make sure dac reference on			*/
    udi_write_dac (SUPPLY1_POWER_DAC, 2200);
    udi_write_dac (SLEW_RATE_DAC, 100); 			/* use maximum slew				*/
    udi_write_dac (PU_CONTROL_DAC, 600);

    delay_ms (400);						/* allow switchers to come up to power		*/

    video_clear_screen ();
    pos_printf (10, 15, "Running Burn-in Test");

    for (error_stop = FALSE, pass = 0; !error_stop && keyboard_status () == -1; pass++)
    {
	pos_printf (1, 1, "pass ");
	printf ("%ld", pass);

	/* disconnect test */
	for (i = 1;i <= max_pins; i++)
	{
	    set_pin_cnf (i, PD_DRIVER_ON);
	    if (read_pin_voltage (i) > 50)
	    {
		if (error_record [i] [ERR_PULLDOWN] != 'F')
		{
		    error_stop = TRUE;
		}
		error_record [i] [ERR_PULLDOWN] = 'F';
	    }
	}

	/* pull-up test */
	for (i = 1; i <= max_pins; i++)
	{
	    set_pin_cnf (i, PU_DRIVER_ON);
	    if ((pin_voltage = read_pin_voltage (i)) < 370 || pin_voltage > 430)
	    {
		if (error_record [i] [ERR_PULL_UP] != 'F')
		{
		    error_stop = TRUE;
		}
		error_record [i] [ERR_PULL_UP] = 'F';
	    }
	    set_pin_cnf (i, PD_DRIVER_ON);
	}

	/* ground driver test */
	for (i = 1; i <= max_pins; i++)
	{
	    set_pin_cnf (i, PU_DRIVER_ON | GD_DRIVER_ON);
	    if (read_pin_voltage (i) > 50)
	    {
		if (error_record [i] [ERR_GROUND] != 'F')
		{
		    error_stop = TRUE;
		}
		error_record [i] [ERR_GROUND] = 'F';
	    }
	    set_pin_cnf (i, PD_DRIVER_ON);
	}

	/* ttl driver test */
	for (i = 1; i <= max_pins; i++)
	{
	    set_pin_cnf (i, TTL_HI_DRIVER_ON);
	    if ((pin_voltage = read_pin_voltage (i)) < 350 || pin_voltage > 500)
	    {
		if (error_record [i] [ERR_TTL] != 'F')
		{
		    error_stop = TRUE;
		}
		error_record [i] [ERR_TTL] = 'F';
	    }
	    set_pin_cnf (i, PD_DRIVER_ON);
	}

	/* source driver test */
	for (i = 1; i <= max_pins; i++)
	{
	    set_pin_cnf (i, SRC_DRIVER_ON);
	    for (j = 0x0011; j < 0x0110; j <<= 1)
	    {
		set_pin_dac (i, j * 10 - 80);
		delay_us (500);
		if ((pin_voltage = read_pin_voltage (i) / 10) < (j - 3) || pin_voltage > (j + 3))
		{
		if (error_record [i] [ERR_SOURCE] != 'F')
		{
		    error_stop = TRUE;
		}
		    error_record [i] [ERR_SOURCE] = 'F';
		}
	    }
	    set_pin_cnf (i, PD_DRIVER_ON);
	    set_pin_dac (i, 0);
	}

	/* read test */

	/* current test */
	for (i = 1; i <= max_pins; i++)
	{
	    set_pin_cnf (i, SRC_DRIVER_ON);
	    set_pin_cnf (max_pins + 1 - i, GD_DRIVER_ON);
	    set_pin_dac (i, 1000);
	    pin_voltage = read_pin_voltage (i);
	    set_pin_dac (i, 0);
	    set_pin_cnf (max_pins + 1 - i, PD_DRIVER_ON);
	    set_pin_cnf (i, PD_DRIVER_ON);
	    if (pin_voltage > 600)
	    {
		if (error_record [i] [ERR_CURRENT] != 'F')
		{
		    error_stop = TRUE;
		}
		error_record [i] [ERR_CURRENT] = 'F';
	    }
	}

	/* analog test */

	if (pass == 0)
	{
	    error_stop = FALSE;
	}
    }

    udi_write_dac (SUPPLY1_POWER_DAC, 0);			/* shut down all power dacs			*/
    udi_write_mem (ANALOG_CONTROL,0);			  /* make sure dac reference is shut down	  */
    reset_allpro ();
    video_clear_screen ();
    pos_printf (0, 7, "|-------------- PULL-DOWN");
    pos_printf (1, 7, "| |------------ PULL-UP");
    pos_printf (2, 7, "| | |---------- GROUND");
    pos_printf (3, 7, "| | | |-------- TTL");
    pos_printf (0, 40, "ANALOG --------------|");
    pos_printf (1, 40, "READ --------------| |");
    pos_printf (2, 40, "CURRENT LIMIT ---| | |");
    pos_printf (3, 40, "SOURCE --------| | | |");
    crt_pos_cursor (1, 5, 0);
    for (i = 1; i <= max_pins / 2; i++)
    {
	crt_pos_cursor (i + 3, 0, 0);
	printf ("pin %2d ", i);
	for (j = 0; j < ERR_MAX; j++)
	{
	    printf ("%c ", error_record [i] [j]);
	}
	crt_pos_cursor (i + 3, 40, 0);
	printf ("pin %2d ", max_pins + 1 - i);
	for (j = 0; j < ERR_MAX; j++)
	{
	    printf ("%c ", error_record [max_pins + 1 - i] [j]);
	}
    }

    keybrd_getc ();
}





/*	Subttl	calabrate pins */
/******************************************************************************/
/*									      */
/* Call:    calibrate_pins ()						      */
/*									      */
/* Return:  display pin voltages					      */
/******************************************************************************/


calibrate_pins( voltage )
int voltage;
{
    int pin;
    int test_count;
    int break_status;
    int row;
    int col;
    int page;
    int volt_table [TOTAL_PINS+1];
    int volt_expected [TOTAL_PINS+1];
    int volt_reg[TOTAL_PINS+1];
    int pass;
    int volt_diff;
    int increment = FALSE;
    int regulate = FALSE;
    unsigned int temp;
    unsigned int pass_number;

    hide_cursor();
    setup_window(WND_FUNC_SELECT);
    reset_allpro();
    if (se6440_flag)
    {
	udi_write_mem (ANALOG_CONTROL,GREEN_LED_OFF_BIT); /* turn off all led's */
	select_zif(selected_zif_row, selected_zif_col);
    }
    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
    udi_write_dac (SLEW_RATE_DAC, 100); 			/* use maximum slew				*/
    udi_write_dac (PU_CONTROL_DAC, 600);


    if (voltage == -1)
    {
	udi_write_dac (SUPPLY1_POWER_DAC, 500); /* initialize power dacs		    */
	for ( pin = 1; pin <= max_pins; pin++)
	{
	    set_pin_cnf (pin, SRC_DRIVER_ON | PD_DRIVER_ON);
	    set_pin_dac (pin, 500);
	    volt_expected[pin] = 50;
	    volt_reg[pin] = 50;
	}
	delay_ms(200);
	regulate = TRUE;
    }
    else if (voltage == 1)
    {
	udi_write_dac (SUPPLY1_POWER_DAC, 2500); /* initialize power dacs		     */
	for ( pin = 1; pin <= max_pins; pin++)
	{
	    set_pin_cnf (pin, SRC_DRIVER_ON | PD_DRIVER_ON);
	    set_pin_dac (pin, 100);
	    volt_expected[pin] = 10;
	}
	increment = 10;
    }
    else if (voltage == 2)
    {
	udi_write_dac (SUPPLY1_POWER_DAC, 50); /* initialize power dacs 		   */
	for ( pin = 1; pin <= max_pins; pin++)
	{
	    set_pin_cnf (pin, SRC_DRIVER_ON | PD_DRIVER_ON);
	    set_pin_dac (pin, 50);
	    volt_expected[pin] = 5;
	}
    }
    else if (voltage)
    {
	udi_write_dac (SUPPLY1_POWER_DAC, voltage * 10); /* initialize power dacs		     */
	for ( pin = 1; pin <= max_pins; pin++)
	{
	    set_pin_cnf (pin, SRC_DRIVER_ON | PD_DRIVER_ON);
	    set_pin_dac (pin, voltage * 10);
	    volt_expected[pin] = voltage;
	}
    }
    else
    {
	udi_write_dac (SUPPLY1_POWER_DAC, 2500); /* initialize power dacs		     */
	for ( pin = 1; pin <= max_pins; pin++)
	{
	    set_pin_cnf (pin, SRC_DRIVER_ON | PD_DRIVER_ON);
	    if (allpro88_flag) volt_expected[pin] = pin * 2;
	    else volt_expected[pin] = pin * 5;
	    set_pin_dac (pin, volt_expected[pin] * 10);
	}
    }

	delay_ms (400);      /* allow switchers to come up to power	     */


    scroll_window(WND_FUNC_SELECT,0);
/*
    pos_write_window(WND_TEST_RESULT,1,12,"%s",top_ic);
    row = 2;
    for ( pin = 1; pin <= 20; pin++ )
    {
	pos_write_window (WND_TEST_RESULT,row,12,"%c %2d       %2d %c", left_pin, pin, 41 - pin , right_pin  );
	row++;
    }
    pos_write_window(WND_TEST_RESULT,pin_coords[pin].row,pin_coords[pin].col,"%s",bott_ic);
*/
    for(pin = 1;pin<=max_pins;pin++)
    {
	pos_write_window(WND_FUNC_SELECT,pin_coords[pin].row,pin_coords[pin].col,"Pin %02d=",pin);
    }


/*
    if(allpro88_flag)
    {
	for ( row = 1,pin = 41; pin <= 64; pin++ )
	{
	    crt_pos_cursor ( row, 52, page);
	    printf ("%c %2d       %2d %c", left_pin, pin, 88 - (pin-41) , right_pin  );
	    row++;
	}
    }
*/
    break_status = FALSE;			   /* check keyboard status for break		   */
    pass_number = 0;

    while (break_status == FALSE)
    {
	for ( pin = 1; pin <= max_pins && break_status == FALSE; pin++ )
	{
	    volt_table [pin] = read_voltage_at_pin(pin) / 10;
	}

/*
	crt_pos_cursor ( 24, 1, 0 );
	pass_number += 1;
	printf ("Pass %5d", pass_number );
	for (row = 1; row <= 20 && break_status == FALSE; row++)
	{
	    crt_pos_cursor ( row + 1, 7, page );
	    if ( volt_table [row] < 255 )
	    {
	pos_write_window(WND_TEST_RESULT,row+1,7,"%2d.%1d",
		       volt_table [row] / 10,
		       volt_table [row] % 10);
		if ((volt_table [row] > (volt_expected[row]+TOL)) ||
		    (volt_table [row] < (volt_expected[row]-TOL)))
		{
		    pos_write_window(WND_TEST_RESULT,row+1,16,"*");
		}
	    }
	    crt_pos_cursor ( row + 1, 29, page );
	    if ( volt_table [41-row] < 255 )
	    {
	pos_write_window(WND_TEST_RESULT,row+1,29,"%2d.%1d",
		       volt_table [41-row] / 10,
		       volt_table [41-row] % 10);
		if ((volt_table [41-row] > (volt_expected[41-row]+TOL)) ||
		    (volt_table [41-row] < (volt_expected[41-row]-TOL)))
		{
		    pos_write_window(WND_TEST_RESULT,row+1,22,"*");
		}
	    }
	}
*/
	    for(pin=1;pin<=max_pins;pin++)
	    {
		pos_write_window(WND_FUNC_SELECT,pin_coords[pin].row,pin_coords[pin].col+7,
				"%02d.%1d",volt_table[pin]/10,volt_table[pin]%10);
		if ((volt_table [pin] > (volt_expected[pin]+TOL)) ||
		    (volt_table [pin] < (volt_expected[pin]-TOL)))
		{
		    printf("*");
		}
	    }
	    if ( keyboard_status () != -1 )
	    {
		break_status = TRUE;
	    }
/*
	for (row = 41; allpro88_flag && row <= 64 && break_status == FALSE; row++)
	{
	    crt_pos_cursor ( row - 40, 47, page );
	    if ( volt_table [row] < 255 )
	    {
		printf ("%2d.%1d",
		       volt_table [row] / 10,
		       volt_table [row] % 10);
		if ((volt_table [row] > (volt_expected[row]+TOL)) ||
		    (volt_table [row] < (volt_expected[row]-TOL)))
		{
		    crt_pos_cursor ( row-40, 56, page);
		    printf ("*");
		}
	    }
	    crt_pos_cursor ( row - 40, 69, page );
	    if ( volt_table [88-(row-41)] < 255 )
	    {
		printf ("%2d.%1d",
		       volt_table [88-(row-41)] / 10,
		       volt_table [88-(row-41)] % 10);
		if ((volt_table [88-(row-41)] > (volt_expected[88-(row-41)]+TOL)) ||
		    (volt_table [88-(row-41)] < (volt_expected[88-(row-41)]-TOL)))
		{
		    crt_pos_cursor ( row-40, 62, page);
		    printf ("*");
		}
	    }
	    if ( keyboard_status () != -1 )
	    {
		break_status = TRUE;
	    }
	}
*/
	if(increment)
	{
	    if(++increment >= 250) increment = 10;
	    if((increment % 10) == 0)
	    {
		for ( pin = 1; pin <= MAX_DEVICE_PINS; pin++)
		{
		    set_pin_dac (pin, (increment/10) * 100);
		    volt_expected[pin] = (increment/10) * 10;
		}
	    }
	}
	if(regulate)
	{
	    for ( pin = 1; pin <= MAX_DEVICE_PINS; pin++)
	    {
		volt_diff = volt_expected[pin] - volt_table[pin];
		if(volt_expected[pin]-volt_reg[pin] <= 10 && volt_expected[pin]-volt_reg[pin] >= -10)
		{
		    if(volt_diff != 0)
			set_pin_dac (pin, (volt_reg[pin] + volt_diff) * 10);
			volt_reg[pin] += volt_diff;
		}
	    }
	}
    }
    udi_write_dac (SUPPLY1_POWER_DAC, 0);			/* shut down all power dacs			*/
    udi_write_mem (ANALOG_CONTROL,0);			  /* make sure dac reference is shut down	  */
    show_cursor();
}







/*	Subttl	Step Pin Test*/
/******************************************************************************/
/* Step pin thru all of its seperate pin drivers.  This will use different    */
/* voltages (described below) for each of the pin drivers.  It will also      */
/* enable the next pin driver before shutting off the previous pin.  The      */
/* sequencing and voltages  are as follows:				      */
/* high power		18 volts with current slew			      */
/* medium power 	14 volts with current slew			      */
/* low power		10 volts					      */
/* pull-up		5 volts 					      */
/* ground driver	0 volts 					      */
/*									      */
/* Note: slew rate is set for maximum slew.				      */
/*									      */
/* This stepping occurs at the best speed possible from CPU.		      */
/*									      */
/* Enter:   start_pin		= starting pin number to step		      */
/*	    end_pin		= last pin number to step		      */
/*									      */
/* Call:    step_pin (start_pin, end_pin)				      */
/*									      */
/* Return:  pin(s) stepped, (no returned value) 			      */
/******************************************************************************/

step_pin (start_pin, end_pin)
int start_pin;
int end_pin;
{
    int pin;
    int test_count;
    int break_status;

    reset_allpro();
    if (se6440_flag)
    {
	udi_write_mem (ANALOG_CONTROL,GREEN_LED_OFF_BIT); /* turn off all led's */
	select_zif(selected_zif_row, selected_zif_col);
    }
    udi_write_mem(ANALOG_CONTROL, DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
    udi_write_dac (SLEW_RATE_DAC, 100); 			/* use maximum slew				*/
    udi_write_dac (SUPPLY1_POWER_DAC, 2550);			/* initialize all power dacs			*/
    udi_write_dac (PU_CONTROL_DAC, 500);

    delay_ms (400);						/* allow switchers to come up to power		*/

    break_status = keyboard_status ();				/* check keyboard status for break		*/

    while (break_status == -1)
    {
	for (pin = start_pin; (pin <= end_pin) && ((break_status = keyboard_status ()) == -1); pin++)
	{
	    if (real_pin [pin])
	    {
		udi_write_mem (PCLOCK,0x12); /* set clock to MHz */
		udi_write_dac (SLEW_RATE_DAC,30);
		udi_write_dac (PU_CONTROL_DAC, 1000 );
		write_pin_dac (pin, 2200);
		delay_us (200);

		udi_write_pin (pin | CLK0_DRIVER_ON);		      /* show clock drivers		    */
		udi_write_pin (pin | SRC_DRIVER_ON);		     /* start sequencing		   */
		udi_write_pin (pin | PD_DRIVER_ON);		       /* check for decouple driver stuck    */
		udi_write_pin (pin | PU_DRIVER_ON);
		udi_write_pin (pin | GD_DRIVER_ON | PU_DRIVER_ON);
		udi_write_pin (pin | TTL_HI_DRIVER_ON | TTL_LOW_DRIVER_ON);
		udi_write_pin (pin | TTL_LOW_DRIVER_ON);
		udi_write_pin (pin | GD_DRIVER_ON);

		write_pin_dac (pin, 00);
		udi_write_dac (PU_CONTROL_DAC, 00 );
		udi_write_pin (pin | SRC_DRIVER_ON | PD_DRIVER_ON);    /* start sequencing		     */
		udi_write_dac (SLEW_RATE_DAC,00);
		write_pin_dac (pin, 1200);
		delay_us(50);
		write_pin_dac (pin, 00);
		delay_us(50);
		udi_write_pin (pin | PU_DRIVER_ON | PD_DRIVER_ON);
		udi_write_dac (PU_CONTROL_DAC, 1200 );
		delay_us(50);
		udi_write_dac (PU_CONTROL_DAC, 00 );
		write_pin_dac (pin, 1200);
		delay_us(50);

		udi_write_pin (pin | GD_DRIVER_ON);			/* all pin drivers off				*/

		decouple_pin(pin,1);
		udi_write_pin (pin | SRC_DRIVER_ON);
		udi_write_pin (pin | PD_DRIVER_ON);
		delay_us(50);
		decouple_pin(pin,0);
		udi_write_pin (pin | SRC_DRIVER_ON);
		udi_write_pin (pin | PD_DRIVER_ON);
	    }
	}
    }

    udi_write_dac (SUPPLY1_POWER_DAC, 0);			/* shut down all power dacs			*/
    udi_write_mem (ANALOG_CONTROL,0);
    reset_allpro();
}

/*	Subttl	Read Pin Voltage*/
/******************************************************************************/
/* Read universal device pin voltage.  This performs a successive	      */
/* approximation upon the pin.	The threshold DAC is initially set to the     */
/* mid-point value and then the pin is read from the specified pin.  The pin  */
/* number is used as a reference into the read_pin_table to derive the mask   */
/* and device address and the pin is read and ANDed with its mask. If this    */
/* value is a one, one half of the last guess is added to the guess other-    */
/* wise, one half of the last guess is subtracted from the last guess.	The   */
/* new last guess is used as the new value for the threshold DAC and is       */
/* repeated until the last guess offset is zero.  This value of the approx-   */
/* imation is return.							      */
/*									      */
/* Enter:   pin 		= pin number to read			      */
/*									      */
/* Call:    read_pin_voltage (pin)					      */
/*									      */
/* Return:  approximate voltage of the specified pin			      */
/******************************************************************************/

read_pin_voltage (pin)
unsigned int pin;
{
    unsigned int approx;
    unsigned int guess;

    approx = 0; 						/* initial approximation			*/

    for (guess = 0x80; guess > 0; guess >>= 1)			/* addend and control				*/
    {
	udi_write_mem (THRESH_HOLD_DAC, approx + guess);	/* set comp drive				*/

	if (udi_read_bit (pin + phys_pin_offset))		/* was it too low?				*/
	{
	    approx = approx + guess;				/* yes - restore AND add			*/
	}
    }

    return (approx * 10);					/* return pinvalue				*/
}


read_pins_12 ()
{
    calibrate_pins (120);
}

read_pins_regulate()
{
    calibrate_pins(-1);
}


read_pins_1 ()
{
    calibrate_pins (1);
}

read_pins_2 ()
{
    calibrate_pins (2);
}

read_pins_var ()
{
    int voltage;
    int term_key;
    char buffer[10];

    pos_write_window(WND_FUNC_SELECT,20,3,"Enter Voltage in Tenths:");
    term_key = keybrd_gets (buffer, 5, 0, 0xff);
    voltage = 255;
    if(term_key == KEY_CR) sscanf(buffer,"%d",&voltage);
    if(voltage > 247) return();
    calibrate_pins (voltage);
}

read_pins_inc ()
{
    calibrate_pins (0);
}



struct menu_struc menu_pin_driver_cal [] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER READ PINS TEST" },
    { '1',*read_pins_inc,               7,  'L', "1. At different voltages." },
    { '2',*read_pins_12,                8,  'L', "2. At 12 volts." },
    { '3',*read_pins_2,                 9,  'L', "3. At 0.5 volts." },
    { '4',*read_pins_1,                 10, 'L', "4. At incrementing voltage." },
    { '5',*read_pins_var,               11, 'L', "5. At Variable voltage." },
    { '6',*read_pins_regulate,          12, 'L', "6. At 5.0v software compensated." },
    { '\0',*func_null, 0,  0, "" }
};




pin_driver_test_step ()
{
    int pin;
    int col;
    int row;

    video_clear_screen ();
    crt_pos_cursor (4, 30, 0);
    printf ("STEPPING PINS");

    col = 1;
    row = 7;
    crt_pos_cursor (row, col, 0);

    for (pin = 1; pin <= max_pins; pin++)
    {
	if (real_pin [pin])
	{
	    printf ("%2d,", pin);
	    col += 4;
	    if (col > 80)
	    {
		row += 2;
		col = 1;
		crt_pos_cursor (row, col, 0);
	    }
	}
    }
    step_pin (1, MAX_DEVICE_PINS);
}

pin_driver_test_cal ()
{
    select_command (menu_pin_driver_cal);
}



pin_driver_test_ovc ()
{
    reset_allpro();
    udi_write_mem(ANALOG_CONTROL,DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
    keybrd_getc ();
    reset_allpro();
}

pin_router_test ()
{
    unsigned char data;

    video_clear_screen ();
    crt_pos_cursor (4, 30, 0);
    printf ("STEPPING DECOUPLE");

    while (keyboard_status () == -1)
    {
	udi_write_mem (0x280, data);
	data <<= 1;
	if (!data)
	{
	    data = 1;
	}
    }
    udi_write_mem (0x280, 0);
}

clock_test()
{
    int i;
    reset_allpro();
    udi_write_mem(ANALOG_CONTROL,DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT);
    udi_write_dac (SUPPLY1_POWER_DAC, 1000);
    udi_write_dac (SLEW_RATE_DAC, 100); 			/* use maximum slew				*/
    udi_write_dac (PU_CONTROL_DAC, 600);
    udi_write_mem( PCLOCK,0x12);
    for(i=1;i<=MAX_DEVICE_PINS;i += 8)
    {
	set_pin_cnf(i,CLK0_DRIVER_ON);
    }
    keybrd_getc ();
    reset_allpro();

}


struct menu_struc menu_pin_driver_test_misc [] =
{
    { '?',*func_help,  4, 'C', "PIN DRIVER TESTS" },
/*  { '1',*pin_driver_test_ovc,         7,  'L', "1. OVC test." },*/
    { '2',*pin_driver_test_cal,         8,  'L', "2. Read pins." },
    { '3',*pin_driver_test_step,        9,  'L', "3. Step pins." },
    { '4',*clock_test,                  10, 'L', "4. Clock Driver Test."},
/*  { '5',*pin_router_test,             12, 'L', "5. Pin router test."},*/
    { '\0',*func_null, 0,  0, "" }
};


pin_driver_test_misc ()
{
    select_command ( menu_pin_driver_test_misc );
    reset_allpro ();
}

