/******************************************************************************
 *
 * Copyright 2013, Silicon Image, Inc.  All rights reserved.
 * No part of this work may be reproduced, modified, distributed, transmitted,
 * transcribed, or translated into any language or computer format, in any form
 * or by any means without written permission of
 * Silicon Image, Inc., 1140 East Arques Avenue, Sunnyvale, California 94085
 *
 *****************************************************************************/
/**
 * file si_platform.c
 *
 * @brief SII9679 platform implementation example
 *
 *****************************************************************************/

/***** #include statements ***************************************************/

//#include <time.h>
//#include <windows.h>
//#include <unistd.h>
//#include <string.h>
#include "si_datatypes.h"
#include "sii9777_platform_api.h"
#include "sii9777rx_api.h"
#include "x_timer.h"
#include "x_os.h"

/***** local macro definitions ***********************************************/

/***** public functions ******************************************************/
extern void x_thread_delay(UINT32 u4Ms);
extern void HAL_GetRawTime(HAL_RAW_TIME_T* pRawTime);
void SiiPlatformSleepMsec( uint16_t mSec )
{
    x_thread_delay(mSec);
}
extern UINT16 SIF_X_Write(UINT8 u1CHannelID, UINT16 u2ClkDiv, UINT8 u1DevAddr, UINT8 u1WordAddrNum, UINT32 u4WordAddr,
                   UINT8 *pu1Buf, UINT16 u2ByteCnt);
extern UINT16 SIF_X_Read(UINT8 u1CHannelID, UINT16 u2ClkDiv, UINT8 u1DevAddr, UINT8 u1WordAddrNum, UINT32 u4WordAddr,
                  UINT8 *pu1Buf, UINT16 u2ByteCnt);
uint16_t SiiHalI2cBlockWrite(uint8_t hwId,uint16_t addr,const uint8_t *pData,uint16_t size )
{
	UINT16 u2Ret = 0;
	UINT8 *u1PData = NULL;
	u1PData = x_mem_alloc(size);
	if(u1PData)
	{
		x_memcpy(u1PData, pData, size);
	}
	else
	{
		printf("switch i2c blockwrite : alloc memory error, return.\n");
		return u2Ret;
	}
    u2Ret = SIF_X_Write(0, 0x100, 0x40, 2, addr, u1PData, size);
	x_mem_free(u1PData);
	return u2Ret;
}
uint16_t SiiHalI2cBlockRead(uint8_t hwId,uint16_t addr,uint8_t *pData,uint16_t size )
{
	UINT16 u2Ret = 0;
	if(size == 0)
	{
		return 0;
	}
    u2Ret = SIF_X_Read(0, 0x100, 0x40, 2, addr, pData, size);	
	return u2Ret;
}

uint16_t SiiPlatformHostBlockWrite( uint8_t hwId, uint16_t addr, const uint8_t *pData, uint16_t size )
{
	if(size == 0)
	{
		return 0;
	}
    return SiiHalI2cBlockWrite(hwId, addr, pData, size);
}

uint16_t SiiPlatformHostBlockRead( uint8_t hwId, uint16_t addr, uint8_t *pData, uint16_t size )
{
   return SiiHalI2cBlockRead(hwId, addr, pData, size);
}
uint32_t SiiPlatformTimeMilliGet( void )
{
	HAL_TIME_T rTime;
	HAL_GetTime(&rTime);
    return (rTime.u4Seconds*1000 + rTime.u4Micros/1000);
}
uint32_t SiiPlatformTimeMsecQuery( void )
{
	HAL_TIME_T rTime;
	HAL_GetTime(&rTime);
    return (rTime.u4Seconds*1000 + rTime.u4Micros/1000);	
}
void SiiPlatformDeviceResetSet( uint8_t devId, bool_t bOn )
{
 //   SiiHalAardvarkDeviceResetSet(bOn);
}
SiiPlatformStatus_t SiiPlatformSemaphoreCreate( const char *pName, uint32_t maxCount, uint32_t initialValue, SiiPlatformSemaphore_t *pSemId )
{
    pName = pName;
    maxCount = maxCount;
    initialValue = initialValue;
    pSemId = pSemId;
    return SII_PLATFORM_STATUS__SUCCESS;
}

SiiPlatformStatus_t SiiPlatformSemaphoreDelete( SiiPlatformSemaphore_t semId )
{
    semId = semId;
    return SII_PLATFORM_STATUS__SUCCESS;
}

SiiPlatformStatus_t SiiPlatformSemaphoreGive( SiiPlatformSemaphore_t semId )
{
    semId = semId;
    return SII_PLATFORM_STATUS__SUCCESS;
}

SiiPlatformStatus_t SiiPlatformSemaphoreTake( SiiPlatformSemaphore_t semId, int32_t timeMsec )
{
    semId = semId;
    timeMsec = timeMsec;
    return SII_PLATFORM_STATUS__SUCCESS;
}

int_t SiiPlatformIpcSemaphoreTake( uint32_t maxBlockTime )
{
    maxBlockTime = maxBlockTime;
    return 0;
}

void SiiPlatformIpcSemaphoreGive( void )
{
}

void SiiPlatformLogPutString( const char* pStr )
{
	printf("%s",pStr);
}
void SiiPlatformLogPrint( const char* pStr )
{
	printf("%s",pStr);
}

