⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stb0899_chip.c

📁 DVB-S STB0899 source code
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <linux/string.h>
#include <linux/i2c.h>

#include "dvb_frontend.h"
#include "stb0899_chip.h"

typedef struct node
{
	STCHIP_Handle_t hChip;
	struct node *pNextNode;
}NODE;

static NODE *pFirstNode = NULL;

static u32 LastBaseAdress=0xffffffff;
static u16 LastPointer=0xffff;

extern int debug;

/*******************************************************************************
 **FUNCTION  ::  I2cWrite
 **ACTION    ::  Write data to the slave
 **PARAMS IN ::  hChip	==> Handle to the chip
 **		 NbData	==> Number of data to write/read
 **		 Data	==> buffer containing data which will be writen
 **PARAMS OUT::	 NONE
 **RETURN    ::   if success return ret  else EREMOTEIO
 ********************************************************************************/

int I2cWrite(STCHIP_Handle_t hChip, u8 *Data, u8 NbData)
{
	int ret;

	struct i2c_msg msg = {
		.addr = hChip->I2cAddr,
		.flags = 0,
		.buf = Data,
		.len = NbData
		};

	ret = i2c_transfer(hChip->I2c_adap, &msg, 1);
	if (ret != 1) {
		return -EREMOTEIO;
	} else {
		return ret;
	}
	return -EIO;
}

/*******************************************************************************
 **FUNCTION  ::  I2cRead
 **ACTION    ::  Read data from the slave
 **PARAMS IN ::  hChip	==> Handle to the chip
 **		 NbData	==> Number of data to write/read
 **PARAMS OUT::	 Data	==> Buffer containing data readed
 **RETURN    ::  if success return ret  else EREMOTEIO
 ********************************************************************************/

int I2cRead(STCHIP_Handle_t hChip, u8 *Data, u8 NbData)
{
	int ret;

	struct i2c_msg msg = {
		.addr = hChip->I2cAddr,
		.flags = I2C_M_RD,
		.buf = Data,
		.len = NbData
		};

	ret = i2c_transfer(hChip->I2c_adap, &msg, 1);
	if (ret != 1) {
		return -EREMOTEIO;
	} else {
		return ret;
	}	
	return -EIO;
 }
#if 0
/* List routines*/
static NODE *AppendNode(STCHIP_Handle_t hChip)
{
	NODE *pNode = pFirstNode;
	
	if(pNode == NULL)
	{   /* Allocation of the first node*/
		pNode = (NODE *)malloc(sizeof(NODE));
		pFirstNode = pNode;
	}
	else
	{   /* Allocation of a new node */
		while(pNode->pNextNode != NULL) /* Search of the last node*/
			pNode = pNode->pNextNode;
			
		/* Memory allocation */
		pNode->pNextNode = (NODE *)malloc(sizeof(NODE));
		
		if(pNode->pNextNode != NULL) /* Check allocation */
			pNode = pNode->pNextNode;
		else
			pNode = NULL;
	}
	
	if(pNode != NULL) /* if allocation ok */
	{
		/* Fill the node */
		pNode->hChip = hChip;
		pNode->pNextNode = NULL;	
	}
	
	return pNode;
}
#endif
/*****************************************************
**FUNCTION	::	ChipGetFirst
**ACTION	::	Retrieve the first chip handle
**PARAMS IN	::	NONE
**PARAMS OUT	::	NONE
**RETURN	::	STCHIP_Handle_t if ok, NULL otherwise
*****************************************************/
#if 0
static STCHIP_Handle_t ChipGetFirst(void)
{
	if((pFirstNode != NULL) && (pFirstNode->hChip != NULL))
		return pFirstNode->hChip;
	else
		return NULL;
}
#endif
/*****************************************************
**FUNCTION	::	ChipFindNode
**ACTION	::	Find that node that contains the chip 
**PARAMS IN	::	NONE
**PARAMS OUT	::	NONE
**RETURN	::	STCHIP_Handle_t if ok, NULL otherwise
*****************************************************/
#if 0
static NODE *ChipFindNode(STCHIP_Handle_t hChip)
{
	NODE *pNode = pFirstNode;
	
	if(pNode != NULL)
	{
		while((pNode->hChip != hChip) && (pNode->pNextNode != NULL))
			pNode = pNode->pNextNode;
		
		if(pNode->hChip != hChip)
			pNode = NULL;
	}
	
	return pNode;
}
#endif
/*****************************************************
**FUNCTION	::	ChipGetNext
**ACTION	::	Retrieve the handle of the next chip
**PARAMS IN	::	hPrevChip==> handle of the previous chip
**PARAMS OUT	::	NONE
**RETURN	::	STCHIP_Handle_t if ok, NULL otherwise
*****************************************************/
#if 0
static STCHIP_Handle_t ChipGetNext(STCHIP_Handle_t hPrevChip)
{
	NODE *pNode;
	
	pNode = ChipFindNode(hPrevChip);
	if((pNode != NULL) && (pNode->pNextNode != NULL))
		return pNode->pNextNode->hChip;
	else
		return NULL;
}
#endif
/*****************************************************
**FUNCTION	::	ChipGetHandleFromName
**ACTION	::	Retrieve the handle of chip with its name
**PARAMS IN	::	Name	==> name of the chip
**PARAMS OUT	::	NONE
**RETURN	::	STCHIP_Handle_t if ok, NULL otherwise
*****************************************************/
#if 0
static STCHIP_Handle_t ChipGetHandleFromName(char *Name)
{
	STCHIP_Handle_t hChip;
	
	hChip = ChipGetFirst();
	while((hChip != NULL) && (strcmp(hChip->Name,Name) != 0))
	{
		hChip = ChipGetNext(hChip);
	}
	
	return hChip;
}
#endif

int
ChipClose(STCHIP_Handle_t hChip)
{
	if (hChip != NULL)
	{
		free(hChip->pFieldMap);
		free(hChip->pRegMap);
		free(hChip);

		return 0;
	}

	return 1;
}

/*****************************************************
**FUNCTION	::	ChipOpen
**ACTION	::	Open a new chip
**PARAMS IN	::	Name	==> Name of the chip
**			I2cAddr	==> I2C address of the chip
**			NbRegs	==> number of register in the chip
**			NbFields==> number of field in the chip
**PARAMS OUT	::	NONE
**RETURN	::	Handle to the chip, NULL if an error occur
*****************************************************/

STCHIP_Handle_t
ChipOpen(STCHIP_Info_t *hChipOpenParams)
{
	STCHIP_Handle_t hChip;
		
	/* Allocation of the chip structure	*/
	hChip = (STCHIP_Handle_t)malloc(sizeof(STCHIP_Info_t));

	memset(hChip, 0, sizeof(STCHIP_Info_t));

	if((hChip != NULL) && (hChipOpenParams != NULL))
	{
		/* Allocation of the register map*/
		hChip->pRegMap = (STCHIP_Register_t *)malloc(hChipOpenParams->NbRegs*sizeof(STCHIP_Register_t));

		memset(hChip->pRegMap, 0, hChipOpenParams->NbRegs*sizeof(STCHIP_Register_t));

		if(hChip->pRegMap != NULL)
		{
			/* Allocation of the field map*/
			hChip->pFieldMap = (STCHIP_Field_t *)malloc(hChipOpenParams->NbFields*sizeof(STCHIP_Field_t));

			memset(hChip->pFieldMap, 0, hChipOpenParams->NbFields*sizeof(STCHIP_Field_t));

			if(hChip->pFieldMap != NULL)
			{
#if 0
				STCHIP_Handle_t hhChip;
				NODE *pNode = NULL;

				hhChip = ChipGetHandleFromName(hChipOpenParams->Name);
				
				if(hhChip==NULL)
					pNode = AppendNode(hChip);

				if ((hhChip==NULL) && (pNode==NULL))
				{
					free(hChip->pFieldMap);
					free(hChip->pRegMap);
					free(hChip);
					hChip = NULL;
				}
				else
#endif
				{
					hChip->I2cAddr = hChipOpenParams->I2cAddr;
					strcpy(hChip->Name,hChipOpenParams->Name);
					hChip->NbRegs = hChipOpenParams->NbRegs;
					hChip->NbFields = hChipOpenParams->NbFields;
					hChip->ChipMode = hChipOpenParams->ChipMode;
					hChip->Repeater = hChipOpenParams->Repeater;
					//?? +38
					hChip->RepeaterHost = hChipOpenParams->RepeaterHost;
					hChip->RepeaterFn = hChipOpenParams->RepeaterFn;
					hChip->WrStart  = hChipOpenParams->WrStart;
					hChip->WrSize   = hChipOpenParams->WrSize;
					hChip->RdStart  = hChipOpenParams->RdStart;
					hChip->RdSize   = hChipOpenParams->RdSize;
					hChip->ChipError = CHIPERR_NO_ERROR;
				}
			}
			else
			{
				free(hChip->pRegMap);
				free(hChip);
				hChip = NULL;
			}
		}
		else
		{
			free(hChip);
			hChip = NULL;	
		}
	}
	return hChip;
}

/*****************************************************
**FUNCTION	::	ChipAddReg
**ACTION	::	Add a new register to the register map
**PARAMS IN	::	hChip	==> Handle to the chip
**			Id	==> Id of the register
**			Name	==> Name of the register
**			Address	==> I2C address of the register
**			Default	==> Default value of the register
**PARAMS OUT	::	NONE
**RETURN	::	Error
*****************************************************/

STCHIP_Error_t
ChipAddReg(STCHIP_Handle_t hChip, STCHIP_RegSize_t Size, u16 RegId, char * Name, u16 Address, u32 Default, STCHIP_Access_t Access, STCHIP_Pointed_t Pointed, u16 PointerRegAddr, u32 BaseAdress)
{
	STCHIP_Register_t *pReg;
	
	if(hChip != NULL)
	{	
		//if((RegId >= 0) && (RegId < hChip->NbRegs))
		if (RegId < hChip->NbRegs)
		{
			pReg = &hChip->pRegMap[RegId];
	
			pReg->Addr = Address;
			pReg->Size = Size;
			pReg->Default = Default;
			pReg->Value = Default;
			pReg->Access = Access;
			strcpy(pReg->Name, Name);
			pReg->Pointed = Pointed;
			pReg->PointerRegAddr = PointerRegAddr;
			pReg->BaseAdress = BaseAdress;
		}
		else
		{
			hChip->ChipError = CHIPERR_INVALID_REG_ID;
		}
	}
	else
	{
		return CHIPERR_INVALID_HANDLE;
	}

	return hChip->ChipError;
}

/*****************************************************
**FUNCTION	::	CreateMask
**ACTION	::	Create a mask according to its number of bits and position 
**PARAMS IN	::	field	==> Id of the field
**PARAMS OUT	::	NONE
**RETURN	::	mask of the field
*****************************************************/

static u32 CreateMask(char NbBits, char Pos)
{
	int i;
	u32 mask=0;
	
	/*Create mask*/
	for (i = 0; i < NbBits; i++)
	{
		mask <<= 1 ;
		mask +=  1 ;
	}
	mask = mask << Pos;
	
	return mask;
}

/*****************************************************
**FUNCTION	::	ChipAddField
**ACTION	::	Add a field to the field map 
**PARAMS IN	::	hChip	==> Handle to the chip
**			RegId	==> Id of the register which contains the field
**			FieldId	==> Id of the field
**			Name	==> Name of the field
**			Pos	==> Position (number of left shifts from LSB position) in the register 
**			NbBits	==> Size (in bits) of the field
**			Type	==> SIGNED or UNSIGNED
**PARAMS OUT	::	NONE
**RETURN	::	Error
*****************************************************/

STCHIP_Error_t
ChipAddField(STCHIP_Handle_t hChip, u16 RegId, u32 FieldId, char * Name, char Pos, char NbBits, STCHIP_FieldType_t Type)
{
	STCHIP_Field_t *pField; 
	
	if(hChip != NULL)
	{
		if(RegId < hChip->NbRegs)
		{
			if(FieldId < (u32)(hChip->NbFields))
			{
				pField = &hChip->pFieldMap[FieldId];
	
				strcpy(pField->Name, Name);
				pField->Reg = RegId;
				pField->Pos = Pos;
				pField->Bits = NbBits;
				pField->Type = Type;

				if(NbBits)
					pField->Mask = CreateMask(NbBits,Pos);
				else
					hChip->ChipError = CHIPERR_INVALID_FIELD_SIZE;
			}
			else
			{
				hChip->ChipError = CHIPERR_INVALID_FIELD_ID;
			}
		}
		else
		{
			hChip->ChipError = CHIPERR_INVALID_REG_ID;
		}
	}
	else 
	{
		return CHIPERR_INVALID_HANDLE;
	}
	
	return hChip->ChipError;
}

/*****************************************************
**FUNCTION	::	ChipSetOneRegister
**ACTION	::	Set the value of one register 
**PARAMS IN	::	hChip	==> Handle to the chip
**			reg_id	==> Id of the register
**			Data	==> Data to store in the register
**PARAMS OUT	::	NONE
**RETURN	::	Error
*****************************************************/

STCHIP_Error_t
ChipSetOneRegister(STCHIP_Handle_t hChip,u16 RegId,u32 Data)   
{	
	if(hChip)
	{
		if(RegId < hChip->NbRegs)
		{
			if(hChip->pRegMap[RegId].Access != STCHIP_ACCESS_R)
			{
				hChip->pRegMap[RegId].Value = Data;
			
				if(hChip->ChipMode != STCHIP_MODE_NOSUBADR)
					ChipSetRegisters(hChip, RegId, 1);
				else
					ChipSetRegisters(hChip, hChip->WrStart, hChip->WrSize);
			}
		}
		else
		{
			hChip->ChipError = CHIPERR_INVALID_REG_ID;
		}
	}
	else
	{
		return CHIPERR_INVALID_HANDLE;
	}
	
	return hChip->ChipError;
}

/*****************************************************
**FUNCTION	::	ChipGetOneRegister
**ACTION	::	Get the value of one register 
**PARAMS IN	::	hChip	==> Handle to the chip
**				reg_id	==> Id of the register
**PARAMS OUT::	NONE
**RETURN	::	Register's value
*****************************************************/

int
ChipGetOneRegister(STCHIP_Handle_t hChip, u16 RegId)
{
	u32 data = 0xFFFFFFFF;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -