#!   /usr/bin/env   python
#    coding: utf8

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

# Import system modules
import sys
import time
import os

# Import specific modules
from fmc_masterfip_proto import *

# Import common modules
from ptsexcept import *
import rr

"""

"""

MASTERFIP_PROTO_PCIE_ADDR = '1a39:0004/1a39:0004@000B:0000'
MASTERFIP_PROTO_BITSTREAM = '../gatewares/spec_masterfip_proto.bin'
MASTERFIP_PROTO_BITSTREAM = os.path.join(default_directory, MASTERFIP_PROTO_BITSTREAM)

IDENT_VAR_ADDR = 0x3
CONSUM_VAR_ADDR = 0x3

NB_BOURRAGE = 0x4B


def main (default_directory='.'):

    start_test_time = time.time()
    print "================================================================================"
    print "Test start\n"

    # SPEC object declaration
    print "Loading hardware access library and opening device.\n"
    spec = rr.Gennum()

    # Bind SPEC object to masterFip card
    #for name, value in spec.parse_addr(MASTERFIP_PROTO_PCIE_ADDR).iteritems():
    #    print "%9s:0x%04X"%(name, value)

    #spec.bind(MASTERFIP_PROTO_PCIE_ADDR)

    # Load MasterFip Proto gateware
    print "Loading gateware: %s\n" % MASTERFIP_PROTO_BITSTREAM
    if(os.path.isfile(MASTERFIP_PROTO_BITSTREAM)):
        spec.load_gateware(MASTERFIP_PROTO_BITSTREAM)
        time.sleep(2)
    else:
        raise PtsCritical("Gateware file \"%s\" is missing, test stopped." % MASTERFIP_PROTO_BITSTREAM)

    """
    # Carrier object declaration (SPEC board specific part)
    try:
        carrier = CFmcAdc100mSpec(spec)
    except FmcAdc100mSpecOperationError as e:
        raise PtsCritical("Carrier init failed, test stopped: %s" % e)
    """

    # Mezzanine object declaration (FmcAdc100m14b4cha board specific part)
    try:
        fmc = CFmcMasterFipProto(spec)
    except FmcMasterFipProtoOperationError as e:
        raise PtsCritical("Mezzanine init failed, test stopped: %s" % e)

    # Perform simple test (controls LEDs on nanoFIP test board)
    try:

        fmc.set_core_config(IDENT_VAR_ADDR, CONS_VAR_ADDR, NB_BOURRAGE)
        #time.sleep(1)
        fmc.print_core_config()
        fmc.continuous_loop_enable()

        print "\n\n ----> Start loop\n"
        fmc.start_loop()
        #time.sleep(1)
        i = 0
        while(1):
            print "\nIteration %d\n"%i
            data = 1
            for d in range(8):
                fmc.send_cons_rp_dat(data)
                data = (data << 1) + 1
                time.sleep(0.5)
            data = 1
            for d in range(8):
                fmc.send_cons_rp_dat(data << 8)
                data = (data << 1) + 1
                time.sleep(0.5)
            for d in range(2):
                fmc.send_cons_rp_dat(0x0000)
                time.sleep(0.3)
                fmc.send_cons_rp_dat(0xFFFF)
                time.sleep(0.3)
            for d in range(2):
                fmc.send_cons_rp_dat(0x0200)
                time.sleep(0.3)
                fmc.send_cons_rp_dat(0x0000)
                time.sleep(0.3)
            fmc.print_ident)rd_dat()
            i++

    except FmcMasterFipProtoOperationError as e:
        raise PtsError("Test failed: %s" % e)

    print ""
    print "==> End of test"
    print "================================================================================"
    end_test_time = time.time()
    print "Elapsed time: %.2f seconds\n" % (end_test_time-start_test_time)

if __name__ == '__main__' :
    main()
