"""
  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.

"""
############################
# DS90UH949-Q1 Init Code Example v1.0
# 949->948, Dual FPD-Link STP, 1920x1080@60Hz
############################

import time
import sys

# System dependant variables - adjust according to system 
UB949 = 0x18        # 949 I2C Address
UB948 = 0x68        # Remote 948 Address

def pollstate():
    count=0
    board.WriteI2C(UB949,0x23,0x80)
    board.WriteI2C(UB949,0x24,0x80)
    mask = int('11111',2)
    state = board.ReadI2C(UB949,0x24,1)
    while (state & mask) != 27:   #27 = 5'b11011
        time.sleep(0.01)  #10ms delay
        state = board.ReadI2C(UB949,0x24,1)
        count = count +1
        if count == 10:
            print "Error: Serializer is not in Normal state"
            return(1)
    
    board.WriteI2C(UB949,0x23,0x0)
    board.WriteI2C(UB949,0x24,0x0)

        

def initsequence():

    board.WriteI2C(UB949,0x03,0xDA) # Set I2C passthrough
    
    linked = 0
    retry = 0
    mask = int('10000000',2)
    while linked == 0:
        STS = board.ReadI2C(UB949,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 #2, 3, 4
    board.WriteI2C(UB949,0x5B,0x03) # Force dual FPD-Link, disable reset on PLL frequency change 

    # Errata #7
    board.WriteI2C(UB949,0x16,0x02) # Adjust BCC watchdog timer to minimum

    state_error = pollstate()  # Poll 949 state machine prior to applying init B errata

    if state_error == 1:
        return()

    ##### Init B Sequence

    # Reset HDMI PLL after TMDS clock has stabilized 
    board.WriteI2C(UB949,0x40,0x10)
    board.WriteI2C(UB949,0x41,0x49)
    board.WriteI2C(UB949,0x42,0x10)
    board.WriteI2C(UB949,0x42,0x00)

    # Reset FPD PLL 
    board.WriteI2C(UB949,0x40,0x14)
    board.WriteI2C(UB949,0x41,0x49)
    board.WriteI2C(UB949,0x42,0x10)
    board.WriteI2C(UB949,0x42,0x00)

    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()