#!   /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 csr


"""
test00: Load firmware and test mezzanine presence line.
"""

CARRIER_CSR = 0x30000

CSR_TYPE_VER = 0x00
CSR_BSTM_TYPE = 0x04
CSR_BSTM_DATE = 0x08
CSR_STATUS = 0x0C
CSR_CTRL = 0x10

PCB_VER_MASK = 0x000F
CARRIER_TYPE_MASK = 0xFFFF0000

STATUS_FMC_PRES = (1<<0)
STATUS_P2L_PLL_LCK = (1<<1)
STATUS_SYS_PLL_LCK = (1<<2)
STATUS_DDR3_CAL_DONE = (1<<3)

CTRL_LED_GREEN = (1<<0)
CTRL_LED_RED = (1<<1)
CTRL_DAC_CLR_N = (1<<2)

def main (default_directory='.'):

    path_fpga_loader = '../../../gnurabbit/user/fpga_loader';
    path_firmware = '../firmwares/spec_fmcadc100m14b4cha_test.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
    carrier_csr = csr.CCSR(spec, CARRIER_CSR)

    # Check bitsteam type
    bitstream_type = carrier_csr.rd_reg(CSR_BSTM_TYPE)
    print('bitstream type:%.8X') % bitstream_type
    if(bitstream_type == 0xFFFFFFFF):
        raise PtsCritical ("Firmware not properly loaded.")
    if(bitstream_type != 0x1):
        raise PtsCritical ("Wrong bitstream type.")

    # Dump carrier CSR to log
    print("PCB version    : %d") % (PCB_VER_MASK & carrier_csr.rd_reg(CSR_TYPE_VER))
    print("Carrier type   : %d") % ((CARRIER_TYPE_MASK & carrier_csr.rd_reg(CSR_TYPE_VER))>>16)
    print("Bitstream type : 0x%.8X") % (carrier_csr.rd_reg(CSR_BSTM_TYPE))
    print("Bitstream date : 0x%.8X") % (carrier_csr.rd_reg(CSR_BSTM_DATE))
    print("Status         : 0x%.8X") % (carrier_csr.rd_reg(CSR_STATUS))
    print("Control        : 0x%.8X") % (carrier_csr.rd_reg(CSR_CTRL))

    # Check mezzanine presence flag
    status = carrier_csr.rd_reg(CSR_STATUS)
    print('carrier csr:%.8X') % status
    if(status & STATUS_FMC_PRES):
        raise PtsCritical ("Mezzanine not present or PRSNT_M2C_L faulty.")


if __name__ == '__main__' :
    main()
