/**
 * @file Hv_Mw_Bcon.c
 * @brief middlware bcon cpp file.
 * @details This file provides the following functions: \n
 *          (1) Initialization and de-initialization functions \n
 *          (2) Start and stop functions \n
 *          (3) Feed functions \n
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-23
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                        <th>Date         <th>Change Description
 * <tr><td>HiView SoC Software Team      <td>2022-08-23   <td>init
 * </table>
*/

#include"hv_drv_Bcon.h"
#include"hv_drv_HY8603.h"
#include"hv_drv_XP7008.h"

VOID Hv_Mw_Bcon_Init(UCHAR8 ucVendor, UINT32 uiClk)
{
    Hv_Drv_Bcon_Init(ucVendor, uiClk);
    if(3 == ucVendor)
    {
        Hv_Drv_Bcon_HYDataType();
    }
    return;
}

VOID Hv_Mw_Bcon_HY8603_Init()
{
    Hv_Drv_HY8603_1stInit(); 

    return;
}

VOID Hv_Mw_Bcon_HY8603BlDataPrep(UCHAR8 ucPort, USHORT16 *ausBlData, UINT32 uiLen)
{
    Hv_Drv_HY8603_BlDataPrep(ucPort, ausBlData, uiLen);
    //Hv_Drv_Bcon_WriteData(ucPort, (USHORT16*)ausBlData, uiLen);
    //Hv_Drv_Bcon_SetDataLength(ucPort, uiLen);//send uiBconOutLen 16bits
    return;
}

VOID Hv_Mw_Bcon_HY8603BlDataSend()
{
    Hv_Drv_HY8603_BlDataSend();
    //Hv_Drv_Bcon_SwitchSram();
    //Hv_Drv_Bcon_Start(0x1ff);

    return;
}

VOID Hv_Mw_Bcon_HY8603SetLanes(UCHAR8 ucDisLane, USHORT16 usDipLanes)
{
    Hv_Drv_HY8603_SetDisLane(ucDisLane);
    Hv_Drv_HY8603_SetDipLanes(usDipLanes);
    return;
}


VOID Hv_Mw_Bcon_XP7008_Init(UINT32 uiLanes)
{
    Hv_Drv_XC7008_Init(uiLanes); 
    return;
}


USHORT16 Hv_Mw_XP7008_PrepCmd(UCHAR8 ucStart, UCHAR8 ucLen, UCHAR8 *regs, USHORT16* pusOutData)
{
    return Hv_Drv_XP7008_PrepCmd(ucStart, ucLen, regs, pusOutData);
}


USHORT16 Hv_Mw_XP7008_PrepData(UCHAR8 ucStart, UCHAR8 ucLen, USHORT16 *pusPwm, USHORT16 *pusOutData)
{
    return Hv_Drv_XP7008_PrepData(ucStart, ucLen, pusPwm, pusOutData);
}

VOID Hv_Mw_Bcon_XP7008BlDataPrep(UCHAR8 ucPort, USHORT16 *ausBlData, UINT32 uiLen)
{
    //Hv_Drv_HY8603_BlDataPrep(ucPort, ausBlData, uiLen);
    Hv_Drv_Bcon_WriteData(ucPort, (USHORT16*)ausBlData, uiLen);
    Hv_Drv_Bcon_SetDataLength(ucPort, uiLen);//send uiBconOutLen 16bits
    return;
}

VOID Hv_Mw_Bcon_XP7008_SendData()
{
    Hv_Drv_XP7008_SendData();
    return;
}

VOID Hv_Mw_Bcon_ReadData(UCHAR8 ucLane, USHORT16 usLen, USHORT16 *pusBuf)
{
    Hv_Drv_Bcon_ReadData(ucLane, usLen, pusBuf);
    return;
}

USHORT16 ausCmd[128] = {0};
USHORT16 ausSend[200] = {0};


VOID Hv_Mw_Bcon_HP9309_Init(UINT32 uiLanes)
{
    UCHAR8 ucIndex = 0;
    USHORT16 uiLen = 0;
    UINT32 uiIndex = 0;
    Hv_Drv_Bcon_HP9309_Init(uiLanes);

    for(ucIndex = 0; ucIndex < INIT_TIME_3; ucIndex++)
    {
        ausCmd[0] = 0x6000;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x004f, 0xf840, &ausCmd[0], 0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        { 
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }
        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
    }
    for(ucIndex = 0; ucIndex < INIT_TIME_3; ucIndex++)
    {
        ausCmd[0] = 0xA5C9;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x0078, 0xf840, &ausCmd[0], 0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        { 
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
    }
    for(ucIndex = 0; ucIndex < INIT_TIME_3; ucIndex++)
    {
        ausCmd[0] = 0xA5C9;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x007C, 0xf840, &ausCmd[0], 0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        { 
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }
        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
    }
    for(ucIndex = 0; ucIndex < INIT_TIME_3*5; ucIndex++)
    {
        ausCmd[0] = 0x0000;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0xb0, 0xff00, 0x002f, 0xf840, &ausCmd[0],0x4000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        { 
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0000;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_ADDRESS<<8)|0xb0, 0x0301, 0x0000, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
    }

    for(ucIndex = 0; ucIndex < INIT_TIME_3; ucIndex++)
    {
        ausCmd[0] = 0x2100;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x0044, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0019;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x004a, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0c28;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x004c, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0000;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x0041, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x803c;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x0048, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0008;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x0047, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x1f00;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x004e, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);

        ausCmd[0] = 0x0112;
        ausCmd[1] = 0x0144;
        ausCmd[2] = 0x0176;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0050, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0115;
        ausCmd[1] = 0x0147;
        ausCmd[2] = 0x0179;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0051, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0118;
        ausCmd[1] = 0x014a;
        ausCmd[2] = 0x017c;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0052, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x011b;
        ausCmd[1] = 0x014d;
        ausCmd[2] = 0x017f;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0053, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x011e;
        ausCmd[1] = 0x0150;
        ausCmd[2] = 0x0182;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0054, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);

        ausCmd[0] = 0x0121;
        ausCmd[1] = 0x0153;
        ausCmd[2] = 0x0185;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0055, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0124;
        ausCmd[1] = 0x0156;
        ausCmd[2] = 0x0188;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0056, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }


        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x0127;
        ausCmd[1] = 0x0159;
        ausCmd[2] = 0x018b;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0057, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }


        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);

        ausCmd[0] = 0x6000;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x004f, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }


        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x1f00;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x004e, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x803c;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x0048, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);

        ausCmd[0] = 0x00ff;
        ausCmd[1] = 0x00ff;
        ausCmd[2] = 0x00ff;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_CONFIG<<8)|0x10, 0x0201, 0x0040, 0xf840, &ausCmd[0],0x0002, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);

    }

    for(ucIndex = 0; ucIndex < INIT_TIME_3; ucIndex++)
    {
        ausCmd[0] = 0x8842;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x0046, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }

        Hv_Drv_Bcon_HP9309_Send();

        Hv_Vos_MSleep(10);
        ausCmd[0] = 0x003f;
        uiLen = Hv_Drv_Bcon_HP9309_DataPrepare((OSPB_CMD_TYPE_BROADCAST<<8)|0x10, 0xff00, 0x0060, 0xf840, &ausCmd[0],0x0000, ausSend);
        for (uiIndex = 0; uiIndex < MAX_LANE_NUM_HY; uiIndex++)
        {
            if((1 << uiIndex) & uiLanes)
            {
                Hv_Drv_Bcon_HP9309_WriteData(uiIndex, ausSend, uiLen);
            }
        }
        Hv_Drv_Bcon_HP9309_Send();
        Hv_Vos_MSleep(10);

   }
}

UINT32 Hv_Mw_Bcon_HP9309_PrepData(USHORT16 usHead, USHORT16 usAddr, USHORT16 usSubAddr, USHORT16 usFeedBack, USHORT16 *pusData, USHORT16 usDataLen, USHORT16* pusOut)
{
    return Hv_Drv_Bcon_HP9309_DataPrepare(usHead, usAddr, usSubAddr, usFeedBack, pusData, usDataLen, pusOut);
}


VOID Hv_Mw_Bcon_HP9309_WriteData(UCHAR8 ucLane,USHORT16 *pusData, UINT32 uiLen)
{
   Hv_Drv_Bcon_HP9309_WriteData(ucLane, pusData,  uiLen);
}
