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

# Copyright CERN, 2011 - 2013 (INCAA Computers BV)
# Author: Samuel Iglesias Gonsalvez <siglesia@cern.ch>
# Author: INCAA Computers BV (info@incaacomputers.com)
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
# Website: http://www.incaacomputers.com
# Version: 2.0 (Last modifications: 13/06/2013)
#
# Description
#  This test overwrites GENNUM EEPROM with VENDOR ID, DEVICE ID of SPEC Board.

import sys
import rr
import time
import os

from eeprom import EEPROM_GENNUM

from ptsexcept import *

"""
test12: overwrites GENNUM EEPROM with VENDOR ID, DEVICE ID of SPEC Board.
"""

##-------------------------------------------------------------------------------------------------
##                                             main                                              --
##-------------------------------------------------------------------------------------------------

def main (default_directory='.'):
    # Load board library and open the corresponding device
    print "Loading hardware access library and opening devices\n"
    spec = rr.Gennum();
    eeprom = EEPROM_GENNUM(spec);

    EEPROM_VID_FILE = default_directory + "/eeprom_vid.dat"

    print "Test12 Start"
    init_test_time = time.time()
    ret_error = None

    # This EEPROM_VID_FILE file contains: 
    # SPEC PCI Device ID: 0x018D and CERN PCI Vendor ID: 0x10DC which are located at EEPROM address 0xF000 data [31:16] and [15:0]
    # The design revision (the same as the PCB_VER from the Carrier_CSR) is located at EEPROM address 0xF008 data [7:0]
    # The class code is also located at 0xF008 data [31:8] and should be 0x000000.
    eeprom.file_dump_to_eeprom(EEPROM_VID_FILE);
    eeprom.eeprom_dump_to_screen();
    ret_error = eeprom.compare_eeprom_with_file(EEPROM_VID_FILE);

    end_test_time = time.time()
    print "End of Test12"
    print "RESULT: [{}]".format("FAIL" if ret_error else "OK")
    print 'Test12 elapsed time: {:.2f} seconds'.format(end_test_time-init_test_time)
    if ret_error:
        raise PtsError(ret_error)

if __name__ == '__main__' :
    main();
