"""
  TI Confidential - NDA Restrictions

  Copyright 2020 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user who
  downloaded the software, his/her employer (which must be your employer) and
  Texas Instruments Incorporated (the "License"). You may not use this Software
  unless you agree to abide by the terms of the License. The License limits your
  use, and you acknowledge, that the Software may not be modified, copied or
  distributed unless embedded on a Texas Instruments microcontroller which is
  integrated into your product. Other than for the foregoing purpose, you may
  not use, reproduce, copy, prepare derivative works of, modify, distribute,
  perform, display or sell this Software and/or its documentation for any
  purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL TEXAS
  INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL
  EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT
  LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL
  DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS,
  TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT
  LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com.

"""
############################
# DS90UB947-Q1 Init Code Example v1.0
# 947->948, Dual FPD-Link STP, 1920x1080@60Hz
############################

import time
import sys

# System dependant variables - adjust according to system 
UB947 = 0x18        # 947 I2C Address
UB948 = 0x68        # Remote 948 Address
StartAmbTemp = 25   # Example temperature measured by local sensor on PCB (Ambient temp around 947). In real system this should be read back from temp sensor during initialization 
Hres = 1920         # Expected horizontal active resolution
Vres = 1080         # Expected vertical active resolution 

def pollstate():
    count=0
    board.WriteI2C(UB947,0x23,0x80)
    board.WriteI2C(UB947,0x24,0x80)
    mask = int('11111',2)
    state = board.ReadI2C(UB947,0x24,1)
    while (state & mask) != 27:   #27 = 5'b11011
        time.sleep(0.01)  #10ms delay
        state = board.ReadI2C(UB947,0x24,1)
        count = count +1
        if count == 10:
            print "Error: Serializer is not in Normal state"
            return(1)
    
    board.WriteI2C(UB947,0x23,0x0)
    board.WriteI2C(UB947,0x24,0x0)

        

def initsequence():

    board.WriteI2C(UB947,0x03,0xDA) # Set I2C passthrough
    
    linked = 0
    retry = 0
    mask = int('10000000',2)
    while linked == 0:
        STS = board.ReadI2C(UB947,0x5A,1)
        if ((STS & mask) != 0):
            linked = 1
            print "Deserializer Detected"
        else:
            time.sleep(0.1)
            retry = retry + 1
            if retry == 10:
                print "Error: No Deserializer Detected"
                return()

    board.WriteI2C(UB948,0x4A,0x01) # Disable OLDI outputs on 948 to prevent screen glitches from appearing during the init process 

    ##### Init A Sequence

    # Errata #5 and 6
    board.WriteI2C(UB947,0x5B,0x03) # Force dual FPD-Link, disable reset on PLL frequency change 

    # Errata #10
    board.WriteI2C(UB947,0x16,0x02) # Adjust BCC watchdog timer to minimum

    # Errata #11
    board.WriteI2C(UB947,0x04,0x90) # Prevent AVMUTE for non-HDCP systems



    state_error = pollstate()  # Poll 947 state machine prior to applying init B errata

    if state_error == 1:
        return()

    ##### Init B Sequence 

    # Errata #2 
    board.WriteI2C(UB947,0x40,0x10)
    board.WriteI2C(UB947,0x41,0x4A)
    board.WriteI2C(UB947,0x42,0x3F)
    board.WriteI2C(UB947,0x41,0x4B)

    if StartAmbTemp < 10:
        board.WriteI2C(UB947,0x42,0x88)
    else:
        board.WriteI2C(UB947,0x42,0x89)
    board.WriteI2C(UB947,0x41,0x49)
    board.WriteI2C(UB947,0x42,0x10)
    board.WriteI2C(UB947,0x42,0x00)
    board.WriteI2C(UB947,0x40,0x14)
    board.WriteI2C(UB947,0x41,0x4A)
    board.WriteI2C(UB947,0x42,0x3F)
    board.WriteI2C(UB947,0x41,0x4B)
    if StartAmbTemp < 10:
        board.WriteI2C(UB947,0x42,0x88)
    else:
        board.WriteI2C(UB947,0x42,0x89)
    board.WriteI2C(UB947,0x41,0x49)
    board.WriteI2C(UB947,0x42,0x10)
    board.WriteI2C(UB947,0x42,0x00)

    # Errata #3 
    board.WriteI2C(UB947,0x40,0x10)
    board.WriteI2C(UB947,0x41,0x49)
    board.WriteI2C(UB947,0x42,0x16)
    board.WriteI2C(UB947,0x41,0x47)
    board.WriteI2C(UB947,0x42,0x20)
    board.WriteI2C(UB947,0x42,0xA0)
    board.WriteI2C(UB947,0x42,0x20)
    board.WriteI2C(UB947,0x42,0x00)
    board.WriteI2C(UB947,0x41,0x49)
    board.WriteI2C(UB947,0x42,0x00)

    time.sleep(0.01)

    ##### Timing Confirmation 

    retry = 0
    Hconfirm = 0
    Vconfirm = 0
    while (retry < 3):
        board.WriteI2C(UB948,0x68,0x08) # Enable PATGEN BIST
        time.sleep(0.1) # Critical time delay - do not adjust to a shorter value 
        board.WriteI2C(UB948,0x68,0x19) # H active High monitor
        Hhigh = board.ReadI2C(UB948, 0x69, 1)
        board.WriteI2C(UB948,0x68,0x09) # H active Low monitor
        Hlow = board.ReadI2C(UB948, 0x69, 1)
        board.WriteI2C(UB948,0x68,0x39) # V active High monitor
        Vhigh = board.ReadI2C(UB948, 0x69, 1)
        board.WriteI2C(UB948,0x68,0x29) # V active Low monitor
        Vlow = board.ReadI2C(UB948, 0x69, 1)
        board.WriteI2C(UB948,0x68,0x00) # Disable PATGEN BIST

        mask = int('00111111',2)

        hlowmask = Hlow & mask
        hhighmask = Hhigh & mask
        vlowmask = Vlow & mask
        vhighmask = Vhigh & mask


        hhighmask = hhighmask << 6
        vhighmask = vhighmask << 6

        Hactive = hhighmask | hlowmask
        Vactive = vhighmask | vlowmask

        if Hactive != Hres:
            retry  = retry + 1
            board.WriteI2C(UB947,0x40,0x10) # Reset OLDI PLL
            board.WriteI2C(UB947,0x41,0x49)
            board.WriteI2C(UB947,0x42,0x16)
            board.WriteI2C(UB947,0x41,0x47)
            board.WriteI2C(UB947,0x42,0x20)
            board.WriteI2C(UB947,0x42,0xA0)
            board.WriteI2C(UB947,0x42,0x20)
            board.WriteI2C(UB947,0x42,0x00)
            board.WriteI2C(UB947,0x41,0x49)
            board.WriteI2C(UB947,0x42,0x00)
            time.sleep(0.1)
        else:
            Hconfirm = 1
        if Vactive != Vres:
            retry  = retry + 1
            board.WriteI2C(UB947,0x40,0x10) # Reset OLDI PLL
            board.WriteI2C(UB947,0x41,0x49)
            board.WriteI2C(UB947,0x42,0x16)
            board.WriteI2C(UB947,0x41,0x47)
            board.WriteI2C(UB947,0x42,0x20)
            board.WriteI2C(UB947,0x42,0xA0)
            board.WriteI2C(UB947,0x42,0x20)
            board.WriteI2C(UB947,0x42,0x00)
            board.WriteI2C(UB947,0x41,0x49)
            board.WriteI2C(UB947,0x42,0x00)
            time.sleep(0.1)
        else:
            Vconfirm = 1
        if (Hconfirm == 1) and (Vconfirm == 1):
            break

    if (retry > 2):
        print "Error: Timing confirmation failed. Double check expected video resolution and OLDI video input"  
        return()
    else:
        print "Initialization Complete"
        board.WriteI2C(UB948,0x01,0x01) # Digital reset to restart AEQ 
        time.sleep(0.1) # Allow time for 948 to relock 
        board.WriteI2C(UB948,0x4A,0x00) # Enable OLDI outputs on 948

initsequence()