#!   ./python
#    coding: utf8

# Copyright CERN, 2013
# Author: Matthieu Cattin <matthieu.cattin@cern.ch>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
# Last modifications: 18/12/2013

# Import system modules
import sys
import time
import os

# Import specific modules
from fmc_adc_svec import *
from fmc_adc import *
from numpy import *

# Import common modules
from ptsexcept import *
from rr2vv import *


"""
svec_test36: Test SDB records.

Note: Requires test00.py to run first to load the gateware!
"""


def main (default_directory='.'):

    # Constants declaration
    LUN = 0
    TEST_NB = 36
    FMC_ADC_BITSTREAM = '../../gatewares/svec_fmcadc100m14b4cha.bin'
    FMC_ADC_BITSTREAM = os.path.join(default_directory, FMC_ADC_BITSTREAM)
    EXPECTED_BITSTREAM_TYPE = 0x0

    # Calibration box vendor and product IDs
    BOX_USB_VENDOR_ID = 0x10c4 # Cygnal Integrated Products, Inc.
    BOX_USB_PRODUCT_ID = 0xea60 # CP210x Composite Device


    start_test_time = time.time()
    print "================================================================================"
    print "Test%02d start\n" % TEST_NB

    # SVEC object declaration
    print "Loading hardware access library and opening device.\n"
    bus = VME_rr_compatible(LUN)
    print "Initialising device.\n"
    bus.vv_init()

    # Load FMC ADC gateware
    print "Loading FMC ADC gateware: %s\n" % FMC_ADC_BITSTREAM
    ret = bus.vv_load(FMC_ADC_BITSTREAM, 1)
    print('')
    time.sleep(2)

    # Carrier object declaration (SPEC board specific part)
    # Used to check that the gateware is loaded.
    try:
        carrier = CFmcAdc100mSvec(bus, EXPECTED_BITSTREAM_TYPE)
    except FmcAdc100mSvecOperationError as e:
        raise PtsCritical("Carrier init failed, test stopped: %s" % e)

    # Mezzanines object declaration (FmcAdc100m14b4cha board specific part)
    fmc = []
    for i in range(2):
        try:
            print('\n-------------------------------------------------------------')
            print('[FMC slot %d]'%(i+1))
            mezz_offset = 0x2000+i*0x4000
            print('Mezzanine offset: 0x%08X'%(mezz_offset))
            fmc.append(CFmcAdc100m(bus, mezz_offset))
        except FmcAdc100mOperationError as e:
            raise PtsCritical("Mezzanine %d init failed, test stopped: %s" % (i+1, e))

    try:

        # Dump carrier's sdb fields
        print("\n\n\n")
        print("=====================================================")
        print("======== CARRIER SDB")
        carrier.sdb_dump()

        # Dump mezzanine's sdb fields
        for i in range(2):
            print("\n\n\n")
            print("=====================================================")
            print("======== MEZZANINE %d SDB"%i)
            fmc[i].sdb_dump()


    except(FmcAdc100mSvecOperationError, FmcAdc100mOperationError) as e:
        raise PtsError("Test failed: %s" % e)


    print ""
    print "==> End of test%02d" % TEST_NB
    print "================================================================================"
    end_test_time = time.time()
    print "Test%02d elapsed time: %.2f seconds\n" % (TEST_NB, end_test_time-start_test_time)


if __name__ == '__main__' :
    main()
