#include "main.h"

/**
 * 0 - Power down all supplies, pull PDB low, wait 5 ms, then bring up 1.8 V, wait 5 ms,
 *     bring up 1.1 V, wait 5 ms, release PDB (pull high), wait 5 ms (I2C ready), then jump to 1.
 * 1 - Perform FPD-Link Init A, mode select, EDID configuration, remote I2C setup, then jump to 2.
 * 2 - Monitor interrupt pin level; when an interrupt occurs, go to 3.
 * 3 - Identify interrupt source; if a downstream interrupt is detected, go to 4.
 * 4 - Wait for HDMI cable insertion, then go to 5.
 * 5 - Check whether the HDMI TMDS frequency is stable; once stable, go to 6.
 * 6 - Perform Init B (HDMI PLL and FPD PLL resets).
 * 7 - Monitor for HDMI cable removal; when removed, go back to 4.
 */
volatile uint8_t state_machine = 0;
volatile uint8_t timer_1ms_flag = 0;
volatile uint8_t timer_500ms_flag = 0;
uint8_t datarx = 0;

void SystemClock_Config(void);


void main(void)
{
    HAL_Init();
    SystemClock_Config();
       
    MX_DMA_Init();
    MX_USART1_UART_Init();

    printf("system clk frq:%d\r\n",HAL_RCC_GetSysClockFreq());
    
    MX_GPIO_Init();
    MX_TIM14_Init();
    HAL_TIM_Base_Start_IT(&htim14);
    printf("tim14 start it\r\n");
    MX_I2C2_Init();

    MX_ADC1_Init();

    static uint8_t timer_counter;

    while (1)
    {
        #if 1
        if (timer_1ms_flag == 1)
        {
            timer_1ms_flag = 0;
            switch (state_machine)
            {
                
            case 0:
                if (timer_counter == 0)                                                             //pull down pwr&pdb
                {

                    HAL_GPIO_WritePin(FPD_PDB_GPIO_Port,FPD_PDB_Pin,GPIO_PIN_RESET);
                    HAL_GPIO_WritePin(EN_VCC_1V8_GPIO_Port,EN_VCC_1V8_Pin,GPIO_PIN_RESET);
                    HAL_GPIO_WritePin(EN_VCC_1V8_GPIO_Port,EN_VCC_1V8_Pin,GPIO_PIN_RESET);
                }else if (timer_counter == 4)                                                       //enable 1.8v
                {
                    HAL_GPIO_WritePin(EN_VCC_1V8_GPIO_Port,EN_VCC_1V8_Pin,GPIO_PIN_SET);
                }else if (timer_counter == 9)                                                       //enable 1.1v
                {
                    HAL_GPIO_WritePin(EN_VCC_1V1_GPIO_Port,EN_VCC_1V1_Pin,GPIO_PIN_SET);
                }else if (timer_counter == 19)                                                      //enable pdb
                {
                    HAL_GPIO_WritePin(FPD_PDB_GPIO_Port,FPD_PDB_Pin,GPIO_PIN_SET);                  
                    while (datarx != 0x18)
                    {
                        HAL_I2C_Mem_Read(&hi2c2,SER_DEV_ADDR,0x00,1,&datarx,1,100);                 //wait for i2c ready
                        printf("i2c2_ready:%d\r\n",datarx);
                    } 
                }else if(timer_counter == 24)
                {
                    timer_counter = 0;
                    state_machine = 1;
                    break;
                }
                timer_counter++;
                break;
            case 1:
                ds90ub929_mode_init();                                                              //mode config
                ds90ub929_remote_i2c_init();                                                        //remote i2c config
                ds90ub929_open_intb(0b11111111);                                                    //config interrupt of 929
                ds90ub929_edid_sram_write(edid_061,sizeof(edid_061));                               //config edid
                ds90ub929_init_a();                                                                 //init a
                state_machine = 2;
                break;
            case 2:                                                                                 //detect interrupt in mcu's interrupt
                break;
            case 3:                                                                                 //check the interrupt
                datarx = ds90ub929_intb_source_check();
                if (((datarx >> 6)&1) == 1)                                                         //check the downsteam interrupt occure
                {
                    printf("state machine = %d, 0xc7 = %d\r\n", state_machine, datarx);
                    printf("downstream detect\r\n");
                    state_machine = 4;
                }else{
                    state_machine = 2;
                }
                break;
            case 4:
                datarx = ds90ub929_bridge_sts_check();
                if (((datarx >> 7)&1) == 1)                                                         //hdmi's +5v detected
                {
                    printf("state machine = %d, 0x50 = %d\r\n", state_machine, datarx);
                    printf("hdmi detect +5v\r\n");
                    state_machine = 5;
                }
                break;
            case 5:                                                                                 //HDMI frequence check
                datarx = ds90ub929_fpd3_sts_check();
                printf("state machine = %d, 0x5A = %d\r\n", state_machine, datarx);
                if ((datarx >> 0)&1 == 1)                                                           //hdmi freq stable
                {
                    printf("state machine = %d, 0x5A = %d\r\n", state_machine, datarx);
                    printf("hdmi frequence stable\r\n");
                    state_machine = 6;
                }
                break;  
            case 6:                                                                                 //init B
                if (timer_counter == 4)
                {

                    ds90ub929_init_b();
                    timer_counter = 0;
                    state_machine = 7;
                    break;
                }
                timer_counter++;
                break;
            case 7:
                datarx = ds90ub929_bridge_sts_check();
                if (((datarx >> 7)&1) == 0)                                                         //det hdmi +5v out
                {
                    printf("state machine = %d, 0x50 = %d\r\n", state_machine, datarx);
                    printf("hdmi not detect\r\n");
                    state_machine = 4;
                }

                break;
            default:
                break;
            }
        }
        #endif
    }
    
}



