#!   /usr/bin/env   python

# Copyright Creotech SA, 2016
# Author: Marek Guminski <marek.guminski@creotech.pl>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org


import spec
import fmcmasterfip as fmc
import utilities as util
import ptsexcept
import sys


def speed_verification( dut ):
    result = {}
    
    speed_string    = ["31.25 Kbps", "1 Mbps", "2.5 Mbps", "5 Mbps" ]
    
    
    mount       = [ "R54, R55", 
                    "R48, R54", 
                    "R47, R55", 
                    "R47, R48" ]
    
    notmount    = [ "R47, R48", 
                    "R47, R55", 
                    "R48, R54", 
                    "R54, R55" ]

    indicators = [ "R65", "R64", "R63", "R62" ]
          
    
    util.section_msg("Verification of speed version")
    
    speed           = dut.fipcore.read_regname('speed')
    
    util.info_msg("Found %s speed version board" % speed_string[speed])
    
    
    if util.ask_user("Detected %s FMC masterFIP speed version.\nPlease verify that %s resistor is mounted" % ( speed_string[speed], indicators[speed] ) ):
        result[ 'Speed specification'] = 1
    else :
        result[ 'Speed specification'] = 0
        
    return result
    
def main (card=None, default_directory='.',suite=None, serial=""):
    
    testname= "Test00: WorldFIP basics"
    util.header_msg( testname, [ "Hardware presence", "Hardware communication", "FMC presence", "FmcMasterFip speed version."] )
    
    box = util.calibration_box_init()
    
    util.section_msg("Hardware initialization")
    abspath = util.find_prj_topdir(util.TOPDIRNAME);
    carrier = spec.spec( abspath );
   
    
    test_results = {}
    
    # FPGA bitstream and FMC presence are verified during initialization
    # any error there causes critical error and test termination
    dut = fmc.fmcmasterfip(carrier, abspath, util.FIRMWARE_PATH)
    test_results['Initialization'] = 1
    
    # verify speed version

    test_results = util.merge_dictionaries( test_results, speed_verification(dut) )
    
    errors = util.summarise_test_results( testname, test_results)
    
    sys.stdout.flush()

    return 0
  

if __name__ == '__main__' :
    main()