#!   /usr/bin/env   python
#    coding: utf8
# Copyright CERN, 2011
# Author: Matthieu Cattin <matthieu.cattin@cern.ch>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org

import sys
import rr
import time
import os

from ptsexcept import *

import fmc_adc

"""
test07: Test offset DACs

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

NB_CHANNELS = 4
OFFSET_POS = 0xFFFF
OFFSET_NEG = 0x0000
ADC_POS = 0x0000
ADC_MID = 0x8000
ADC_NEG = 0xFFFC
ADC_TOL = 0x150

def main (default_directory='.'):
    """
    path_fpga_loader = '../../../gnurabbit/user/fpga_loader';
    path_firmware = '../firmwares/spec_fmcadc100m14b4cha.bin';

    firmware_loader = os.path.join(default_directory, path_fpga_loader)
    bitstream = os.path.join(default_directory, path_firmware)
    print firmware_loader + ' ' + bitstream
    os.system( firmware_loader + ' ' + bitstream )

    time.sleep(2);
    """

    # Objects declaration
    spec = rr.Gennum() # bind to the SPEC board
    fmc = fmc_adc.CFmcAdc100Ms(spec)

    error = 0

    # All inputs in calibration mode
    for i in range(1, NB_CHANNELS+1):
        fmc.set_input_range(i, "CAL")

    # Disable test pattern (just in case)
    fmc.testpat_dis()

    # Set a positive offset on all channels
    print('Set positive offset: %.4X' % OFFSET_POS)
    for i in range(1, NB_CHANNELS+1):
        fmc.dc_offset_calibr(i, OFFSET_POS)
    time.sleep(1)

    # Read channels current data register
    for i in range(1,NB_CHANNELS+1):
        adc_value = fmc.get_current_adc_value(i)
        print('ADC channel %d value:%d expected value:%d') % (i, adc_value, ADC_POS)
        if(ADC_POS != adc_value):
            print('Channel %d offset circuit is malfunctioning')%(i)
            error += 1
            #raise PtsError('Channel %d offset circuit is malfunctioning'%i)

    # Reset offset DACs
    print('Reset offset')
    fmc.dc_offset_reset()
    time.sleep(1)

    # Read channels current data register
    for i in range(1,NB_CHANNELS+1):
        adc_value = fmc.get_current_adc_value(i)
        print('ADC channel %d value:%d expected value:%d tolerance:%d') % (i, adc_value, ADC_MID, ADC_TOL)
        if((ADC_MID-ADC_TOL > adc_value) | (ADC_MID+ADC_TOL < adc_value)):
            print('Channel %d offset circuit is malfunctioning')%(i)
            error += 1
            #raise PtsError('Channel %d offset circuit is malfunctioning'%i)

    # Set a negative offset on all channels
    print('Set negative offset: %.4X' % OFFSET_NEG)
    for i in range(1, NB_CHANNELS+1):
        fmc.dc_offset_calibr(i, OFFSET_NEG)
    time.sleep(1)

    # Read channels current data register
    for i in range(1,NB_CHANNELS+1):
        adc_value = fmc.get_current_adc_value(i)
        print('ADC channel %d value:%d expected value:%d') % (i, adc_value, ADC_NEG)
        if(ADC_NEG != adc_value):
            print('Channel %d offset circuit is malfunctioning')%(i)
            error += 1
            #raise PtsError('Channel %d offset circuit is malfunctioning'%i)

    # Reset offset DACs
    fmc.dc_offset_reset()

    # Check if an error occured during offset DAC test
    if(error != 0):
        raise PtsError('An error occured during offset DAC test, check log for details.')


if __name__ == '__main__' :
    main()
