#!/usr/bin/python
#===============================================================================
# CERN (BE-CO-HT)
# Retrieve gateware version from CONV-TTL-BLO board
#===============================================================================
# author: Theodor Stana (t.stana@cern.ch)
#
# date of creation:
#
# version: 1.0
#
# description:
#   This script retrieves the value of the CONV-TTL-BLO status register and
#   retrieves the GWVERS field of the register.
#
#===============================================================================
# GNU LESSER GENERAL PUBLIC LICENSE
#===============================================================================
# This source file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version. This source is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details. You should have
# received a copy of the GNU Lesser General Public License along with this
# source; if not, download it from http://www.gnu.org/licenses/lgpl-2.1.html
#===============================================================================
# last changes:
#    2014-05-02    Theodor Stana        Changed to print the bits of the SR
#                                       in turn.
#===============================================================================
#  TODO: - 
#===============================================================================
import sys
sys.path.append("../ei2c")
from ei2c import *
import ei2cdefine


if __name__ == "__main__":

    # Get the IP, user and password for the ELMA crate from ei2cdefine.py
    ip   = ei2cdefine.HNAME
    user = ei2cdefine.USER
    pwd  = ei2cdefine.PWD
    testelma = EI2C(ip, user, pwd)
    testelma.open()

    # Ask for slot number
    while 1:
        try:
            slot = raw_input("Slot no.: ")
            slot = int(slot)
            break
        except TypeError as e:
            print("Please input a decimal slot number.")
        except SlotError as e:
            print(e.strerror)
        except KeyboardInterrupt:
            sys.exit();
        except:
            print("Unexpected error: ", sys.exc_info()[0])

    # Get status register value and print
    v = testelma.read(slot, 0x4)
    print("Status register    : 0x%08x" % v)
    print("===============================")

    # Strip GWVERS and print
    maj = (v & 0xf0) >> 4
    min = v & 0x0f
    print("Gateware version   : v%d.%d (0x%02x)" % (maj, min, v & 0xff))

    # Strip SWITCHES and print
    switches = (v & 0xff00) >> 8
    print("Switches           : 0x%02x" % switches)

    # Negate in case g/w version is less than 3.0, or golden less than 0.2.
    # Gateware versions below these two had the switches active-low.
    # This negation is done to allow correct printing of ON or OFF states.
    if ((maj == 0) and (min < 2)) or (maj < 3):
        switches ^= 0xff
    if (switches & 0x80):
        print("  TTL repetition   : on")
    else:
        print("  TTL repetition   : off")
    if (switches & 0x01):
        print("  Glitch filter    : on")
    else:
        print("  Glitch filter    : off")

    # Strip RTM and print
    rtm  = (v & 0x3f0000) >> 16
    rtmm = rtm & 0x7
    rtmp = (rtm & 0x3f) >> 3
    if ((maj == 0) and (min < 2)) or (maj < 3):
        print("-------------------------------")
        print("Note: negated w.r.t. latest user guide version")
    print("RTM detection      : 0x%02x" % rtm)
    print("  RTMM[2:0]        : %s" % ("{0:#05b}".format(rtmm)[2:]))
    print("  RTMP[2:0]        : %s" % ("{0:#05b}".format(rtmp)[2:]))
    if ((maj == 0) and (min < 2)) or (maj < 3):
        print("-------------------------------")

    # Strip the other status bits and print
    print("I2C timeout error  : %s" % ("{0:#01b}".format(int(bool(v & (1 << 22))))[2]))
    print("White Rabbit pres. : %s" % ("{0:#01b}".format(int(bool(v & (1 << 23))))[2]))
    print("I2C address error  : %s" % ("{0:#01b}".format(int(bool(v & (1 << 24))))[2]))
    print("Pulse miss error   : 0x%02x" % ((v & 0x7e000000) >> 24))
    print("  CH1              : %s" % ("{0:#01b}".format(int(bool(v & (1 << 25))))[2]))
    print("  CH2              : %s" % ("{0:#01b}".format(int(bool(v & (1 << 26))))[2]))
    print("  CH3              : %s" % ("{0:#01b}".format(int(bool(v & (1 << 27))))[2]))
    print("  CH4              : %s" % ("{0:#01b}".format(int(bool(v & (1 << 28))))[2]))
    print("  CH5              : %s" % ("{0:#01b}".format(int(bool(v & (1 << 29))))[2]))
    print("  CH6              : %s" % ("{0:#01b}".format(int(bool(v & (1 << 30))))[2]))

    testelma.close()
