#!   /usr/bin/env	python
# coding: utf8
# Copyright CERN, 2011
# Author: Ross Millar <ross.millar@cern.ch>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org

import sys
import rr
import random
import time
import spi
import csr
import i2c
import gn4124
import fmc_adc_test_suite
#from pylab import *
from ptsexcept import *

"""
Test03
ADC digital signal test : Tests all flags and conrol lines to both ADC chips on the FMC

"""

def main (default_directory='.'):
  # Objects declaration
    spec = rr.Gennum() # bind to the SPEC board
    fmc = fmc_adc_test_suite.CFmcAdc100ks(spec)
	
  # Run Acqusition Test
    print "Running test acquisition - Checking if state machine becomes stuck waiting for ADC flags"
    Error_state = fmc.do_test_acq()    	
    error_cnt = 0
    
    if Error_state!=0:
    	error_cnt=error_cnt +1
    
    
    if Error_state == 0:
    	print "No errors found  : CONVST, BUSY1, BUSY2, FIRSTDATA1, FIRSTDATA2  -  All fully functional "
    elif Error_state ==3:
    	print "Error: One of the Busy lines not responding  - "
    elif Error_state ==4:
    	print "Error: Busy Line on ADC chip 1 tied high"
    elif Error_state ==5:
    	print "Error: First Data high on ADC chip 1 not responding, possible bad solder connection"
    elif Error_state ==7:
    	print "Error: Busy Line on ADC chip 2 tied high"
    elif Error_state ==8:
    	print "Error: First Data line on ADC chip 2 not responding, possible bad solder connection"
    else:
    	print "Error: stuck in an unexpected state"
    

    if Error_state == 3:    
    	busy_lines = fmc.check_busy_lines()
    	if busy_lines == 1:
    		print "Busy line not raised on ADC 2 (bottom as facing board)"
    		print "Possible bad solder connection for this chip"
    	elif busy_lines == 2:
    		print "Busy line not raised on ADC 1 (top as facing board)"
    		print "Possible bad solder connection for this chip"
    	elif busy_lines ==3:
    		print "Both busy appear to have been raised"
    	elif busy_lines==0:
    		print "Neither Busy line 1 or 2 were raised as expected"
    		print "Potential problems :"
    		print "1. Flip-flop solder connection problem - meaning CONVST line was never raised"
    		print "2. Both ADC chips are faulty or have not been soldered correctly to the board"	
    	else:
   		print "Values not writen to CSR correctly"
     
    elif Error_state == 4:
    	busy_lines = fmc.check_busy_lines()
     	if busy_lines == 1:
     		print "Busy line 1 tied high - Possible short to a power supply"
     		print "Busy line 2 did not respond - Possible bad solder connection on CNVST line to ADC chip 2,"
     		print "or on busy line to LPC connector"
     		
     	elif busy_lines == 2:
     		print "Busy line 2 tied high - Possible short to a power supply"
     		print "Busy line 1 did not respond - Possible bad solder connection on CNVST line to ADC chip 1,"
     		print "or on busy line to LPC connector"
     		
     		
   # Check range pin on ADC 1   	
    range_working =  fmc.check_range_pin(1)    
    if range_working ==1:
    	print "Range pin fully working on ADC chip 1"
    elif range_working ==0:
    	print "Error: Range pin not working on ADC chip 1"
    	print "Possible Cause: Possible bad solder connection on ADC chip 1 - (Top chip as facing Board)"
    	error_cnt=error_cnt+1	



   # Check range pin on ADC 2
    range_working = fmc.check_range_pin(2)
    if range_working ==1:
    	print "Range pin fully working on ADC chip 2"
    elif range_working ==0:
    	print"Error: Range pin not working on ADC chip 2"
    	print"Possible Cause: Possible bad solder connection on ADC chip 1 - (Bottom chip as facing Board)"
	print"Please ensure test box is properly connected"  
	error_cnt=error_cnt+1
	
    if error_cnt!=0:
    	raise PtsError("ADC Digital I/Os Failed - See log for details of fail")
    	
if __name__ == '__main__':
    main()        
    
    
