/******************************************************************************
* @file    ms933x_mpi.h
* @author  
* @version V1.0.0
* @date    15-Nov-2014
* @brief   MacroSilicon Programming Interface.
*
* Copyright (c) 2009-2014, MacroSilicon Technology Co.,Ltd.
******************************************************************************/
#ifndef __MACROSILICON_MS933X_PROGRAMMING_INTERFACE_H__
#define __MACROSILICON_MS933X_PROGRAMMING_INTERFACE_H__

#include "ms933x_config.h"

#ifdef __cplusplus
extern "C" {
#endif


/***************************************************************
*  Function name: ms933x_HAL_SetChipAddr
*  Description:   change I2C slave u8_address 
*  Input parameters: UINT8 u8_address: chip slave u8_address
*                    
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_SetChipAddr(UINT8 u8_address);

/***************************************************************
*  Function name: ms933x_HAL_ReadByte
*  Description: read back 8 bits register value with 16 bits specified index
*  Input parameters: UINT16 u16_index: 16 bits register index
*  Output parameters: None
*  Returned value: UINT8 type register value
***************************************************************/
MS933X_DRV_API UINT8 ms933x_HAL_ReadByte(UINT16 u16_index);


/***************************************************************
*  Function name: ms933x_HAL_WriteByte
*  Description: write 8 bits register value to 16 bits specified index
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8  u8_value: 8 bits rgister value
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_WriteByte(UINT16 u16_index, UINT8 u8_value);


/***************************************************************
*  Function name: ms933x_HAL_ReadWord
*  Description: equal to "ms933x_HAL_ReadByte(u16_index) + 
*                        (UINT16)(ms933x_HAL_ReadByte(u16_index + 1) << 8)"
*  Input parameters: UINT16 u16_index: 16 bits register index
*  Output parameters: None
*  Returned value: UINT16 type register value
***************************************************************/
MS933X_DRV_API UINT16 ms933x_HAL_ReadWord(UINT16 u16_index);


/***************************************************************
*  Function name:  ms933x_HAL_WriteWord
*  Description: equal to "ms933x_HAL_WriteByte(u16_index, (UINT8)u16_value);
                          ms933x_HAL_WriteByte(u16_index + 1, (UINT8)(u16_value >> 8))"
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT16 u16_value: 16 bits rgister value
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_WriteWord(UINT16 u16_index, UINT16 u16_value);


/***************************************************************
*  Function name: ms933x_HAL_ModBits
*  Description: modify register value with bit mask (high active) for 16 bits index and 8 bits value
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8 u8_mask: 8 bits mask 
*                                   can be set by bit mask macro or compsite of macros
*                    UINT8 u8_value: 8 bits value, value with masked bits will be ignored
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_ModBits(UINT16 u16_index, UINT8 u8_mask, UINT8 u8_value);


/***************************************************************
*  Function name: ms933x_HAL_SetBits
*  Description: set bits with bit mask (high active) for 16 bits index and 8 bits value
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8 u8_mask: 8 bits mask 
*                                      can be set by bit mask macro or compsite of macros
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_SetBits(UINT16 u16_index, UINT8 u8_mask);

/***************************************************************
*  Function name:  ms933x_HAL_ClrBits
*  Description: clear bits with bit mask (high active) for 16 bits index and 8 bits value
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8 u8_mask: 8 bits mask 
*                                      can be set by bit mask macro or compsite of macros
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_ClrBits(UINT16 u16_index, UINT8 u8_mask);

/***************************************************************
*  Function name:  ms933x_HAL_ToggleBits
*  Description: toggle bits with bit mask (high active) for 16 bits index and 8 bits value
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8 u8_mask: 8 bits mask 
*                                      can be set by bit mask macro or compsite of macros
*                            BOOL b_set, if true set to 1 else 0
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_ToggleBits(UINT16 u16_index, UINT8 u8_mask, BOOL b_set);

//
/***************************************************************
*  Function name:  ms933x_HAL_ReadDWord
*  Description: read 32 bits value 
*  Input parameters: UINT16 u16_index: 16 bits register index
*  Output parameters: None
*  Returned value: 32 bits value
***************************************************************/
MS933X_DRV_API UINT32 ms933x_HAL_ReadDWord(UINT16 u16_index);

/***************************************************************
*  Function name:  ms933x_HAL_WriteDWord
*  Description: write 32 bits value 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT32 u32_value: 32 bits value
*  Output parameters: None
*  Returned value: 32 bits value
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_WriteDWord(UINT16 u16_index, UINT32 u32_value);

//I2C read/write with length
/***************************************************************
*  Function name:  ms933x_HAL_ReadBytes
*  Description: burst mode read 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT16 u16_length: 16 bits length
*                    UINT8 *p_u8_value: data buffer to read
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_ReadBytes(UINT16 u16_index, UINT16 u16_length, UINT8 *p_u8_value);

/***************************************************************
*  Function name:  ms933x_HAL_WriteBytes
*  Description: burst mode write 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT16 u16_length: 16 bits length
*                    UINT8 *p_u8_value: data buffer to write
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_WriteBytes(UINT16 u16_index, UINT16 u16_length, UINT8 *p_u8_value);

/***************************************************************
*  Function name: UINT32 ms933x_HAL_ReadRange(UINT16 u16_index, UINT8 u8_bitpos, UINT8 u8_length)
*  Description:   read back 32 bits register value by specified index with start bit and length
*                 in case of 16 bits index and 8 bits value
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8 u8_bitpos: start bit of fisrt index register, should be less than 8
*                    UINT8 u8_length: register length , should less than 33
*  Output parameters: None
*  Returned value: UINT32 type register value
***************************************************************/
MS933X_DRV_API UINT32 ms933x_HAL_ReadRange(UINT16 u16_index, UINT8 u8_bitpos, UINT8 u8_length);

/***************************************************************
*  Function name: VOID ms933x_HAL_WriteRange(UINT16 u16_index, UINT8 u8_bitpos, UINT8 u8_length, UINT32 u32_value)
*  Description:   write 32 bits register value to specified index with start bit and length
*                 in case of 16 bits index and 8 bits value
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8 u8_bitpos: start bit of fisrt index register, should be less than 8
*                    UINT8 u8_length: register length , should less than 33
*                    UINT32 u32_value: 32 bits register value
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_WriteRange(UINT16 u16_index, UINT8 u8_bitpos, UINT8 u8_length, UINT32 u32_value);



//extend APIs is for HDMI RX register access only

/***************************************************************
*  Function name: ms933x_HAL_ModBits_Ex
*  Description:   write 32 bits register value to specified index 
*                 with mask
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT32 u32_mask: mask value
*                    UINT32 u32_value: 32 bits register value
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_ModBits_Ex(UINT16 u16_index, UINT32 u32_mask, UINT32 u32_value);

/***************************************************************
*  Function name: ms933x_HAL_ModBits_Ex
*  Description:   set 32bit mask bits 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT32 u32_mask: mask value
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_SetBits_Ex(UINT16 u16_index, UINT32 u32_mask);

/***************************************************************
*  Function name: ms933x_HAL_ClrBits_Ex
*  Description:   clear 32bit mask bits 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT32 u32_mask: mask value
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_ClrBits_Ex(UINT16 u16_index, UINT32 u32_mask);

/***************************************************************
*  Function name: ms933x_HAL_ToggleBits_Ex
*  Description:   toggle 32bit mask bits 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT32 u32_mask: mask value
*                    BOOL b_set: if TRUE toggle to 1 else 0
*  Output parameters: None
*  Returned value: None
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_ToggleBits_Ex(UINT16 u16_index, UINT32 u32_mask, BOOL b_set);

/***************************************************************
*  Function name: ms933x_HAL_ReadDWord_Ex
*  Description:   Read DWORD at once 
*  Input parameters: UINT16 u16_index: 16 bits register index
*  Output parameters: None
*  Returned value: UINT32
***************************************************************/
MS933X_DRV_API UINT32 ms933x_HAL_ReadDWord_Ex(UINT16 u16_index);

/***************************************************************
*  Function name: ms933x_HAL_WriteDWord_Ex
*  Description:   write DWORD at once 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT32 u32_value: value
*  Output parameters: None
*  Returned value: UINT32
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_WriteDWord_Ex(UINT16 u16_index, UINT32 u32_value);

/***************************************************************
*  Function name: ms933x_HAL_ReadRange_Ex
*  Description:   read register with start bits & length 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8 u8_bitpos: start bit of fisrt index register
*                    UINT8 u8_length: register length
*                    UINT32 u32_value: 32 bits register value
*  Output parameters: None
*  Returned value: UINT32
***************************************************************/
MS933X_DRV_API UINT32 ms933x_HAL_ReadRange_Ex(UINT16 u16_index, UINT8 u8_bitpos, UINT8 u8_length);

/***************************************************************
*  Function name: ms933x_HAL_WriteRange_Ex
*  Description:   write register with start bits & length 
*  Input parameters: UINT16 u16_index: 16 bits register index
*                    UINT8 u8_bitpos: start bit of fisrt index register
*                    UINT8 u8_length: register length
*                    UINT32 u32_value: 32 bits register value
*  Output parameters: None
*  Returned value: UINT32
***************************************************************/
MS933X_DRV_API VOID ms933x_HAL_WriteRange_Ex(UINT16 u16_index, UINT8 u8_bitpos, UINT8 u8_length, UINT32 u32_value);



//
#define ms933x_chip_reset mculib_chip_reset

//
#define Delay_ms mculib_delay_ms
#define Delay_us mculib_delay_us

//
#define HAL_SetChipAddr     ms933x_HAL_SetChipAddr
#define HAL_GetChipAddr     ms933x_HAL_GetChipAddr
#define HAL_ReadByte        ms933x_HAL_ReadByte
#define HAL_WriteByte       ms933x_HAL_WriteByte
#define HAL_ReadWord        ms933x_HAL_ReadWord
#define HAL_WriteWord       ms933x_HAL_WriteWord
#define HAL_ModBits         ms933x_HAL_ModBits
#define HAL_SetBits         ms933x_HAL_SetBits
#define HAL_ClrBits         ms933x_HAL_ClrBits
#define HAL_ToggleBits      ms933x_HAL_ToggleBits
#define HAL_ReadDWord       ms933x_HAL_ReadDWord
#define HAL_WriteDWord      ms933x_HAL_WriteDWord
#define HAL_ReadBytes       ms933x_HAL_ReadBytes
#define HAL_WriteBytes      ms933x_HAL_WriteBytes
#define HAL_ReadRange       ms933x_HAL_ReadRange
#define HAL_WriteRange      ms933x_HAL_WriteRange
#define HAL_ModBits_Ex      ms933x_HAL_ModBits_Ex
#define HAL_SetBits_Ex      ms933x_HAL_SetBits_Ex
#define HAL_ClrBits_Ex      ms933x_HAL_ClrBits_Ex
#define HAL_ToggleBits_Ex   ms933x_HAL_ToggleBits_Ex
#define HAL_ReadDWord_Ex    ms933x_HAL_ReadDWord_Ex
#define HAL_WriteDWord_Ex   ms933x_HAL_WriteDWord_Ex
#define HAL_ReadRange_Ex    ms933x_HAL_ReadRange_Ex
#define HAL_WriteRange_Ex   ms933x_HAL_WriteRange_Ex


//
#if MS933X_DEBUG_LOG

#ifndef _PLATFORM_WINDOWS_
#define mculib_log printf
#endif

#define MS933X_PRINTF       mculib_log
#define MS933X_LOG(X)       mculib_uart_log((UINT8*)X)
#define MS933X_LOG1(X, Y)   mculib_uart_log1((UINT8*)X, Y)
#define MS933X_LOG2(X, Y)   mculib_uart_log2((UINT8*)X, Y)

#else
#define MS933X_PRINTF
#define MS933X_LOG(X)
#define MS933X_LOG1(X, Y)
#define MS933X_LOG2(X, Y)
#endif

#ifdef __cplusplus
}
#endif

#endif  // __MACROSILICON_MS933X_PROGRAMMING_INTERFACE_H__
