/*	Title	All-Pro Diagnostics*/
/******************************************************************************/
/*				     Diagalog				      */
/*			       All-pro Diagnostic Analog control test	      */
/*									      */
/* Module ...................... DIAGALOG				      */
/* Title ....................... All-pro Diagnostic Analog control test       */
/* Developing Engineer ......... Steven T. Harr 			      */
/* Company ..................... Logical Devices			      */
/* Revision Level .............. 1.00					      */
/* Date of Last Revision ....... 16-FEB-86				      */
/* Last Revision by ............ STH					      */
/*									      */
/*									      */
/******************************************************************************/
/*	Subttl	Includes*/
#include <stdio.h>
#include <ctype.h>
#include "pindrivr.h"
#include "diag2.h"
#include "windows.h"
#include "window.h"

/*	Subttl	defines */
#define SUPPLY1DAC		SUPPLY1_POWER_DAC
#define THRESHOLDDAC		THRESH_HOLD_DAC
#define SLEWRATEDAC		SLEW_RATE_DAC
#define PUCONTROLDAC		PU_CONTROL_DAC
#define CURRENTDAC		CURRENT_DAC
#define MAX_PINS		40
#define PASS	    1
#define FAIL	    0


/*	Subttl	Externals */
extern unsigned char analog_id;
extern unsigned int phys_pin_offset;
extern unsigned int pin_base_address[];
extern se6440_flag;
extern allpro88_flag;
extern int selected_zif_row;
extern int selected_zif_col;
unsigned char module_id;
int func_null ();
int func_help ();



/*	Subttl Analog Calibration */

struct menu_struc cal_vr2 [] =
{
    { '0',*func_null,  4, 'C', " ALLPRO DIAGNOSTIC CALIBRATION " },
    { '0',*func_null,  5, 'C', "ANALOG CONTROL U3 DAC REFERENCE VR2" },
    { '0',*func_null,  10, 'L', " Place volt meter from U3 pin 4 to ground " },
    { '0',*func_null,  11, 'L', " Adjust analog control R4 to measure 5.00 volts " },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc cal_se6440_vr2 [] =
{
    { '0',*func_null,  4, 'C', " SE6440 DIAGNOSTIC CALIBRATION " },
    { '0',*func_null,  5, 'C', "ANALOG CONTROL VREF (DAC REFERENCE)" },
    { '0',*func_null,  10, 'L', " Place volt meter from TP2 to ground " },
    { '0',*func_null,  11, 'L', " Adjust analog control POT1 to measure 5.020 volts " },
    { '\0',*func_null, 0,  0, "" }
};


struct menu_struc cal_vth [] =
{
    { '0',*func_null,  4, 'C', " ALLPRO DIAGNOSTIC CALIBRATION " },
    { '0',*func_null,  5, 'C', "SYSTEM READ THRESHOLD VOLTAGE" },
    { '0',*func_null,  10, 'L', " Place volt meter from U7 pin 16 to ground" },
    { '0',*func_null,  11, 'L', " Adjust analog control R8 to measure 19.20 volts" },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc cal_se6440_vth [] =
{
    { '0',*func_null,  4, 'C', " SE6440 DIAGNOSTIC CALIBRATION " },
    { '0',*func_null,  5, 'C', "SOCKET READ THRESHOLD VOLTAGE" },
    { '0',*func_null,  10, 'L', " Place volt meter from TP6 to ground" },
    { '0',*func_null,  11, 'L', " Adjust analog control POT2 to measure 19.15 volts " },
    { '\0',*func_null, 0,  0, "" }
};


struct menu_struc cal_vref [] =
{
    { '0',*func_null,  4, 'C', "ALLPRO DIAGNOSTIC CALIBRATION" },
    { '0',*func_null,  5, 'C', "NEGATIVE DAC REFERENCE VOLTAGE" },
    { '0',*func_null,  10, 'L', "Place volt meter from U5 pin 8 to ground." },
    { '0',*func_null,  11, 'L', "Adjust analog control R19 to measure -5.00 volts." },
    { '\0',*func_null, 0,  0, "" }
};


/*	Subttl Analog Calibration */
calibrate_vth ()
{
    reset_allpro();
    if(se6440_flag)
    {
	print_menu ( cal_se6440_vth );
	udi_write_mem (ANALOG_CONTROL,GREEN_LED_OFF_BIT); /* turn off all led's */
	select_zif(selected_zif_row, selected_zif_col);
    }
    else print_menu ( cal_vth );
    udi_write_bit (DAC_REFERENCE_BIT, ON);			/* make sure dac reference on			*/
    udi_write_dac (SUPPLY1_POWER_DAC, 2500);
    while (keyboard_status () == -1)
    {
	udi_write_dac (THRESH_HOLD_DAC, 1920);			    /* set threshold to 19.2 volts		    */
    }
    reset_allpro();
}


calibrate_vref ()
{
    print_menu ( cal_vref );
    reset_allpro();
    while (keyboard_status () == -1)
    {
	udi_write_bit (DAC_REFERENCE_BIT, ON);
    }
    reset_allpro();
}


calibrate_vr2 ()
{
    reset_allpro();
    if(se6440_flag)
    {
	print_menu ( cal_se6440_vr2 );
	udi_write_mem (ANALOG_CONTROL,GREEN_LED_OFF_BIT); /* turn off all led's */
	select_zif(selected_zif_row, selected_zif_col);
    }
    else print_menu ( cal_vr2 );
    while (keyboard_status () == -1)
    {
	udi_write_bit (DAC_REFERENCE_BIT, ON);
    }
    reset_allpro();
}




struct menu_struc menu_calibrate [] =
{
    { '?',*func_help,  2, 'C', " ALLPRO CALIBRATION PROCEDURES " },
    { '?',*func_help,  4, 'C', " Make the following adjustment to the ANALOG board " },
    { '1',*calibrate_vr2,  7,  'L', " 1. Adjust VR2 for +5 volts " },
    { '2',*calibrate_vref, 9,  'L', " 2. Adjust VREF to -5 volts " },
    { '3',*calibrate_vth, 11,  'L', " 3. Adjust VTH to 19.20 volts " },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_se6440_calibrate [] =
{
    { '?',*func_help,  2, 'C', " SE6440 CALIBRATION PROCEDURES " },
    { '?',*func_help,  4, 'C', " Make the following adjustment to the ANALOG board " },
    { '1',*calibrate_vr2,  7,  'L', " 1. Adjust VREF for +5.050 volts " },
    { '2',*calibrate_vth,  9,  'L', " 2. Adjust VTH to 19.15 volts " },
    { '\0',*func_null, 0,  0, "" }
};



calibrate ()
{
    if(se6440_flag) select_command ( menu_se6440_calibrate );
    else select_command ( menu_calibrate );
}


/*	Subttl	Dac linearity tests */

ramp_dac (dac)
int dac;
{
    int i;
    int dac_address = SLEWRATEDAC;

    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);
    }

    write_dac (SUPPLY1DAC, 255);
    udi_write_bit (DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT, ON);			    /* make sure dac reference on		    */

    switch (dac)
    {
	case SUPPLY1_POWER_DAC:
	{
	    dac_address = SUPPLY1DAC;
	    break;
	}
	case PU_CONTROL_DAC:
	{
	    dac_address = PUCONTROLDAC;
	    break;
	}
	case SLEW_RATE_DAC:
	{
	    dac_address = SLEWRATEDAC;
	    break;
	}
	case THRESH_HOLD_DAC:
	{
	    dac_address = THRESHOLDDAC;
	    break;
	}
	case CURRENT_DAC:
	{
	    dac_address = CURRENTDAC;
	    break;
	}
	case VIPV_DAC:
	{
	    dac_address = VIPV_DAC;
	    break;
	}
	case VIPI_DAC:
	{
	    dac_address = VIPI_DAC;
	    break;
	}
    }
    while (keyboard_status () == -1)
    {
	write_dac (dac_address, 128);
	for (i = 0; i <= 128; i++)
	{
	    write_dac (dac_address, 127 + i);
	    write_dac (dac_address, 128 - i);
	}
	for (i = 128; i >= 0; i--)
	{
	    write_dac (dac_address, 127 + i);
	    write_dac (dac_address, 128 - i);
	}
	delay_ms (10);
    }
    reset_allpro ();
}



struct menu_struc menu_thresh_dac [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST " },
    { '?',*func_help,          5, 'C', "READ THRESHOLD CONTROL DAC" },
    { '?',*func_help,          7, 'L', " Place scope probe on VTH, observe diamond waveform " },
    { '?',*func_help,          8, 'L', " Type any key to stop test " },
    { '\0',*func_null, 0,  0, "" }
};



ramp_thresh_dac ()
{
    print_menu (menu_thresh_dac);
    ramp_dac (THRESH_HOLD_DAC);
}



struct menu_struc menu_slew_rate_dac [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST " },
    { '?',*func_help,          5, 'C', "SLEW RATE CONTROL DAC" },
    { '?',*func_help,          7, 'L', " Place scope probe on VSR, observe diamond waveform " },
    { '?',*func_help,          8, 'L', " Type any key to stop test " },
    { '\0',*func_null, 0,  0, "" }
};



ramp_slew_rate_dac ()
{
    print_menu (menu_slew_rate_dac);
    ramp_dac (SLEW_RATE_DAC);
}



struct menu_struc menu_power_dac [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST " },
    { '?',*func_help,          5, 'C', "SUPPLY POWER DAC" },
    { '?',*func_help,          7, 'L', " Place scope probe on VADJ, observe diamond waveform " },
    { '?',*func_help,          8, 'L', " Type any key to stop test " },
    { '\0',*func_null, 0,  0, "" }
};


ramp_power_dac ()
{
    print_menu (menu_power_dac);
    ramp_dac (SUPPLY1_POWER_DAC);
}



struct menu_struc menu_pullup_dac [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST " },
    { '?',*func_help,          5, 'C', "PULL-UP CONTROL DAC" },
    { '?',*func_help,          7, 'L', " Place scope probe on VPU, observe diamond waveform " },
    { '?',*func_help,          8, 'L', " Type any key to stop test " },
    { '\0',*func_null, 0,  0, "" }
};
ramp_pu_dac ()
{
    print_menu (menu_pullup_dac);
    ramp_dac (PU_CONTROL_DAC);
}


struct menu_struc menu_current_dac [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST " },
    { '?',*func_help,          5, 'C', "CURRENT CONTROL DAC" },
    { '?',*func_help,          7, 'L', " Place scope probe on U3 pin 19, observe diamond waveform " },
    { '?',*func_help,          8, 'L', " Type any key to stop test" },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_vipi_dac [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST" },
    { '?',*func_help,          5, 'C', "VIPI DAC" },
    { '?',*func_help,          7, 'L', " Place scope probe on Vipi, observe diamond waveform " },
    { '?',*func_help,          8, 'L', " Type any key to stop test " },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_vipv_dac [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST " },
    { '?',*func_help,          5, 'C', "VIPV DAC" },
    { '?',*func_help,          7, 'L', " Place scope probe on Vipv, observe diamond waveform " },
    { '?',*func_help,          8, 'L', " Type any key to stop test " },
    { '\0',*func_null, 0,  0, "" }
};


ramp_current_dac ()
{
    print_menu (menu_current_dac);
    ramp_dac (CURRENT_DAC);
}

ramp_vipv_dac ()
{
    print_menu (menu_vipv_dac);
    write_dac (VIPI_DAC, 255);
    write_dac (SLEWRATEDAC, 100);
    set_pin_cnf(1,CUR_DRIVER_ON);
    set_pin_cnf(40,GD_DRIVER_ON);
    ramp_dac (VIPV_DAC);

}

ramp_vipi_dac ()
{
    print_menu (menu_vipi_dac);
    write_dac (VIPV_DAC, 255);
    write_dac (SLEWRATEDAC, 100);
    set_pin_cnf(1,CUR_DRIVER_ON);
    set_pin_cnf(40,GD_DRIVER_ON);
    ramp_dac (VIPI_DAC);
}



struct menu_struc menu_ramp_dacs [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST " },
    { '1',*ramp_thresh_dac,    7,  'L', " 1. Ramp Threshold DAC " },
    { '2',*ramp_slew_rate_dac, 8,  'L', " 2. Ramp Slew-rate DAC " },
    { '3',*ramp_power_dac,     9,  'L', " 3. Ramp Supply1 power DAC " },
    { '4',*ramp_current_dac,  10,  'L', " 4. Ramp Current DAC " },
    { '5',*ramp_pu_dac,       11,  'L', " 5. Ramp Pull-up DAC " },
    { '6',*ramp_vipv_dac,     12,  'L', " 6. Ramp Vipv DAC " },
    { '7',*ramp_vipi_dac,     13,  'L', " 7. Ramp Vipi DAC " },
    { '\0',*func_null, 0,  0, "" }
};

struct menu_struc menu_se6440_ramp_dacs [] =
{
    { '?',*func_help,          4, 'C', " ANALOG CONTROL DAC LINEARITY TEST " },
    { '1',*ramp_thresh_dac,    7,  'L', " 1. Ramp Threshold DAC" },
    { '2',*ramp_slew_rate_dac, 8,  'L', " 2. Ramp Slew-rate DAC" },
    { '3',*ramp_power_dac,     9,  'L', " 3. Ramp Supply power DAC " },
    { '5',*ramp_pu_dac,       11,  'L', " 5. Ramp Pull-up DAC " },
    { '6',*ramp_vipv_dac,     12,  'L', " 6. Ramp Vipv DAC " },
    { '7',*ramp_vipi_dac,     13,  'L', " 7. Ramp Vipi DAC " },
    { '\0',*func_null, 0,  0, "" }
};



func_linearity ()
{
    if(se6440_flag) select_command ( menu_se6440_ramp_dacs );
    else select_command ( menu_ramp_dacs );
}


/*	Subttl Analog Control Tests */

func_analog_id ()
{
    pos_write_window(WND_ANALOG,21, 2, "Analog ID code = ");
    while(keyboard_status() == -1)
    {
	get_analog_id ();
	pos_write_window(WND_ANALOG,21, 20, "%2xh",analog_id);
    }
    clear_window_line(WND_ANALOG,21);
}


func_module_id ()
{
    pos_write_window(WND_ANALOG,21, 2, "Module ID code = ");
    while (keyboard_status () == -1)
    {
	get_module_id ();
	pos_write_window(WND_ANALOG,21, 20, "%2xh",module_id);
    }
    clear_window_line(WND_ANALOG,21);
}

func_seq_test()
{
    int i, j, k, pin;
    int pass_fail;
    int max_pins;
    reset_allpro();
    setup_supplies(1200,200,600,50);

    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);
	max_pins = 40;
    }
    if(allpro88_flag) max_pins = 88;
    write_dac (SUPPLY1DAC, 1200);
    udi_write_bit (DAC_REFERENCE_BIT | GREEN_LED_OFF_BIT, ON);			    /* make sure dac reference on		    */

    pass_fail = PASS;
    crt_pos_cursor(20, 10, 0);
    printf("pass");

    udi_write_mem(SEQUENCER_COUNTER,0);
    for(i=1;i<=max_pins;i++)
    {
	udi_write_mem(SEQUENCER_RAM,pin_base_address[i+phys_pin_offset]>>4);
    }

    j = 0x55;
    while (keyboard_status () == -1)
    {
	for(i=0;i<max_pins;i+=8)
	{
	    udi_write_mem(SEQUENCER_COUNTER,i);
	    udi_write_mem(SEQUENCER_SHIFTER,j);
	}

	for(pin=1;pin<=max_pins;pin++)
	{
	    crt_pos_cursor(20, 10, 0);
	    k = read_zif_pin(pin);
	    if(k)
	    {
		if((j == 0x55 && (pin & 1) == 1) || (j == 0xaa && (pin & 1) == 0))
		{
		    if(pass_fail == FAIL) printf("pass");
		    pass_fail = PASS;
		}
		else
		{
		    if(pass_fail == PASS) printf("fail");
		    pass_fail = FAIL;
		}
	    }
	    else
	    {
		if((j == 0xaa && (pin & 1) == 1) || (j == 0x55 && (pin & 1) == 0))
		{
		    if(pass_fail == FAIL) printf("pass");
		    pass_fail = PASS;
		}
		else
		{
		    if(pass_fail == PASS) printf("fail");
		    pass_fail = FAIL;
		}
	    }

	}
	j ^= 0xff;
    }
    unpump();
}

func_vlin_test()
{
    reset_allpro();
    setup_supplies(2500,200,600,10);
    write_dac (VIPV_DAC, 0);
    write_dac (VIPI_DAC, 0);

    write_dac (VIPV_DAC, 0);
    write_dac (VIPI_DAC, 90);

    set_pin_cnf(1,CUR_DRIVER_ON);
    set_pin_cnf(40,GD_DRIVER_ON);
    while(keyboard_status() == -1)
    {
	delay_ms(2);
	write_dac (VIPV_DAC, 150);
	set_pin_cnf(2,GD_DRIVER_ON);
	set_pin_cnf(2,0);
	write_dac (VIPV_DAC, 0);
    }
    write_dac (VIPV_DAC, 0);
    write_dac (VIPI_DAC, 0);
    set_pin_cnf(1,DISCONNECT);

    reset_allpro();
}

func_ilin_test()
{
    reset_allpro();
    setup_supplies(2500,200,600,50);

    write_dac (VIPI_DAC, 0);
    write_dac (VIPV_DAC, 250);

    set_pin_cnf(1,CUR_DRIVER_ON);
    set_pin_cnf(40,GD_DRIVER_ON);
    while(keyboard_status() == -1)
    {
	delay_ms(2);
	write_dac (VIPI_DAC, 75);
	set_pin_cnf(2,GD_DRIVER_ON);
	set_pin_cnf(2,0);
	write_dac (VIPI_DAC, 0);
    }
    write_dac (VIPI_DAC, 0);
    write_dac (VIPV_DAC, 0);
    set_pin_cnf(1,DISCONNECT);

    reset_allpro();
}

func_iswtch_test()
{
    reset_allpro();
    setup_supplies(2500,200,600,50);

    write_dac (VIPI_DAC, 75);
    write_dac (VIPV_DAC, 200);

    set_pin_cnf(40,GD_DRIVER_ON);
    set_pin_cnf(1,GD_DRIVER_ON);
    while(keyboard_status() == -1)
    {
	delay_ms(2);
	set_pin_cnf(1,CUR_DRIVER_ON | GD_DRIVER_ON);
	set_pin_cnf(1,CUR_DRIVER_ON);
	set_pin_cnf(2,GD_DRIVER_ON);
	set_pin_cnf(2,0);
	set_pin_cnf(1,CUR_DRIVER_ON | GD_DRIVER_ON);
	set_pin_cnf(1,GD_DRIVER_ON);
    }
    write_dac (VIPI_DAC, 0);
    write_dac (VIPV_DAC, 0);
    set_pin_cnf(1,DISCONNECT);
    set_pin_cnf(40,DISCONNECT);

    reset_allpro();
}



struct menu_struc menu_analog_cont [] =
{
    { '?',*func_help,        4, 'C', " ANALOG CONTROL TEST and CALIBRATION " },
    { '1',*func_analog_id,   7,  'L', " 1. Read Analog ID " },
    { '2',*calibrate,        9,  'L', " 2. Calibrate DACs " },
    { '3',*func_linearity,  11,  'L', " 3. DAC Linearity " },
    { '4',*func_module_id,  13,  'L', " 4. Read Module ID " },
    { '5',*func_seq_test,   14,  'L', " 5. Test Sequencer "},
    { '6',*func_vlin_test,  16,  'L', " 6. VLIM Test "},
    { '7',*func_ilin_test,  17,  'L', " 7. ILIM Test "},
    { '8',*func_iswtch_test,18,  'L', " 8. ISWTCH Test "},
    { '\0',*func_null, 0,  0, "" }
};



/*	Subttl	Analog Control Tests main entry */

func_analog_cont ()
{
    select_command ( menu_analog_cont );
}

