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

📄 chip.c

📁 STV0299 Minituner driver, for ST chipset
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <stdlib.h>
#include <string.h>
//#include <userint.h>
//#include <ansi_c.h> 

#ifndef NO_GUI
	#include <stdio.h>
	#include "windows.h"
	#include "Pnl_Trace.h"
	#include "Appl.h"
	static HANDLE ChipMutex = NULL;   
#endif

#include "gen_macros.h"
//#include "i2c.h"
#include "chip.h"
#include "miniter.h"
typedef struct node 
{
	STCHIP_Handle_t hChip;
	struct node *pNextNode;
}NODE;

static NODE *pFirstNode = NULL;

/* List routines	*/
static NODE *AppendNode(STCHIP_Handle_t hChip)
{
	NODE *pNode = pFirstNode;
	
	if(pNode == NULL)
	{   /* Allocation of the first node	*/
		pNode = calloc(1,sizeof(NODE));
		pFirstNode = pNode;
	}
	else
	{   /* Allocation of a new node */
		while(pNode->pNextNode != NULL)	/* Search of the last node	*/
			pNode = pNode->pNextNode;
			
		pNode->pNextNode = calloc(1,sizeof(NODE));	/* Memory allocation */
		
		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;
}
#if (0)
static NODE *FindNode(NODE* pSearchNode)
{
	NODE *pNode = pFirstNode;
	
	if(pNode == NULL)
	{
		while((pNode != pSearchNode) && (pNode->pNextNode != NULL))
			pNode = pNode->pNextNode;
			
		if(pNode != pSearchNode)
			pNode = NULL;
	}
	
	return pNode;
}
#endif
static void DeleteNode(NODE *pNode)
{
	NODE *pPrevNode = pFirstNode;
	
	if(pNode != NULL)
	{
		if(pNode == pFirstNode) 
		{
			/* Delete the first node	*/
			pFirstNode = pNode->pNextNode;
		}
		else
		{
			/* Delete a non-first node	*/
			while(pPrevNode->pNextNode != pNode)	/* search the node before the node to delete */
				pPrevNode = pPrevNode->pNextNode;
				
			if(pNode->pNextNode == NULL)
				pPrevNode->pNextNode = NULL;		/* the node to delete is the last */
			else	
				pPrevNode->pNextNode = pPrevNode->pNextNode->pNextNode;	/* the node to delete is not the last */
		}
		
		free(pNode);	/* memory deallocation */
	}
}


/*****************************************************
**FUNCTION	::	ChipGetFirst
**ACTION	::	Retrieve the first chip handle
**PARAMS IN	::	NONE
**PARAMS OUT::	NONE
**RETURN	::	STCHIP_Handle_t if ok, NULL otherwise
*****************************************************/
STCHIP_Handle_t	ChipGetFirst(void)
{
	if((pFirstNode != NULL) && (pFirstNode->hChip != NULL))
		return pFirstNode->hChip;	
	else
		return NULL;
}

/*****************************************************
**FUNCTION	::	ChipFindNode
**ACTION	::	Find that node that contains the chip 
**PARAMS IN	::	NONE
**PARAMS OUT::	NONE
**RETURN	::	STCHIP_Handle_t if ok, NULL otherwise
*****************************************************/
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;
}

/*****************************************************
**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
*****************************************************/
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; 
}

/*****************************************************
**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
*****************************************************/
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;
}
#ifndef NO_GUI
/*****************************************************
**FUNCTION	::	ChipToHtmlFile
**ACTION	::	Create an Html file of the register map 
**PARAMS IN	::	hChip	==> chip handle
**				FileName==> name of the html file
**PARAMS OUT::	NONE
**RETURN	::	CHIPERR_NO_ERROR if ok, CHIPERR_INVALID_HANDLE otherwise
*****************************************************/
STCHIP_Error_t ChipToHtmlFile(STCHIP_Handle_t hChip,char *FileName)
{
	FILE	*hHtmlFile;
	
	S32		r=0,
			f=0,
			nbBits;
			
	U32		color;
			
	char	string[300];
	
	if(hChip)
	{
		/* HTML file creation */
		hHtmlFile = fopen (FileName, "w+");  									/* Open/Create Html file */
		/* HTML header */
		fprintf(hHtmlFile,"<html>\r\n");

		/* HTML document head */
		fprintf(hHtmlFile,"\t<head>\r\n");
		fprintf(hHtmlFile,"\t\t<title>%s</title>\r\n",hChip->Name);					/* Name of the HTML document */
		fprintf(hHtmlFile,"\t\t<style type=\"text/css\">\r\n");					/* HTML font style */
		fprintf(hHtmlFile,"\t\t\ttd {font-family:arial; font-size:8pt;}\r\n");
		fprintf(hHtmlFile,"\t\t</style>\r\n");
		fprintf(hHtmlFile,"\t</head>\r\n");

		/* HTML document body */
		fprintf(hHtmlFile,"\r\n\t<body>\r\n");

		/* Table */
		fprintf(hHtmlFile,"\t\t<table border=\"1\" bgcolor=\"#BBBBBB\">\r\n");	/* table with dark grey background */
		fprintf(hHtmlFile,"\t\t\t<tr>\r\n");									/* Heading row */ 
		fprintf(hHtmlFile,"\t\t\t\t<th>Name</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Address</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Bit8</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Bit7</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Bit6</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Bit5</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Bit4</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Bit3</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Bit2</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Bit1</th>\r\n");    
		fprintf(hHtmlFile,"\t\t\t\t<th>Value</th>\r\n"); 
		fprintf(hHtmlFile,"\t\t\t</tr>\r\n");    

		for(r=0;r<hChip->NbRegs;r++)												
		{
			/* fill rows */
			fprintf(hHtmlFile,"\t\t\t<tr>\r\n");
			fprintf(hHtmlFile,"\t\t\t\t<td>%s</td>\r\n",hChip->pRegMap[r].Name);	/* register name */
			fprintf(hHtmlFile,"\t\t\t\t<td bgcolor=\"#EEEEEE\">0x%02x</td>\r\n",hChip->pRegMap[r].Addr);	/* register address */
	
			color = 0xBBBBBB;

			nbBits = 8;
	
			do
			{
				/* Fill columns with fields data */
				if((hChip->pFieldMap[f].Bits + hChip->pFieldMap[f].Pos) != nbBits)
					fprintf(hHtmlFile,"\t\t\t\t<td bgcolor=\"red\" colspan=\"%d\"></td>\r\n",nbBits-(hChip->pFieldMap[f].Bits + hChip->pFieldMap[f].Pos));	/* empty fields at the beginning of the register */
		
				nbBits = hChip->pFieldMap[f].Pos;
		
				if(hChip->pFieldMap[f].Bits>1)
					fprintf(hHtmlFile,"\t\t\t\t<td bgcolor=\"#%06x\" align=\"center\" colspan=\"%d\">%s</td>\r\n",color,hChip->pFieldMap[f].Bits,hChip->pFieldMap[f].Name); /* Field with size > 1 bit */
				else
					fprintf(hHtmlFile,"\t\t\t\t<td bgcolor=\"#%06x\" align=\"center\">%s</td>\r\n",color,hChip->pFieldMap[f].Name);	/* Field with size = 1 bit */
		
				f++;															/* Next field */
		
				if(color == 0xBBBBBB)											/* change from dark grey to light grey */
					color = 0xEEEEEE;
				else
					color = 0xBBBBBB;
			}while(hChip->pFieldMap[f].Reg == r);
	
			if(nbBits)
				fprintf(hHtmlFile,"\t\t\t\t<td bgcolor=\"red\" colspan=\"%d\"></td>\r\n",nbBits);	/* empty fields at the end of the register */

			fprintf(hHtmlFile,"\t\t\t\t<td bgcolor=\"#EEEEEE\">0x%02x</td>\r\n",hChip->pRegMap[r].Value);	/* register value */
			fprintf(hHtmlFile,"\t\t\t</tr>\r\n");
		}

		fprintf(hHtmlFile,"\t\t</table>\r\n");
		fprintf(hHtmlFile,"\t</body>\r\n");
		fprintf(hHtmlFile,"</html>\r\n");

		fclose(hHtmlFile);
	}
	else
		return CHIPERR_INVALID_HANDLE;
		
	return CHIPERR_NO_ERROR;
}
#endif
/*****************************************************
**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;
	
	#ifndef NO_GUI
		char mutex_name[20];
	#endif
	
	hChip = calloc (1,sizeof(STCHIP_Info_t));					/* Allocation of the chip structure	*/
	
	if((hChip != NULL) && (hChipOpenParams != NULL))
	{
		hChip->pRegMap = calloc(hChipOpenParams->NbRegs,sizeof(STCHIP_Register_t));		/* Allocation of the register map	*/
		
		if(hChip->pRegMap != NULL)
		{
			hChip->pFieldMap = calloc(hChipOpenParams->NbFields,sizeof(STCHIP_Field_t));	/* Allocation of the field map	*/
			
			if(hChip->pFieldMap != NULL)
			{
				if((ChipGetHandleFromName(hChipOpenParams->Name)==NULL) && (AppendNode(hChip)!=NULL)) 
				{
					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;
					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->Error = CHIPERR_NO_ERROR;
					
					#ifndef NO_GUI
						if(ChipMutex == NULL)
						{
							sprintf(mutex_name,"I2C_%04x",Appl.Cfg.PPortAdr);	//Create mutex name
							ChipMutex = CreateMutex(NULL,FALSE,mutex_name);		//Create mutex or use existing one
						}
					#endif
				}
				else
				{
					free(hChip->pFieldMap);
					free(hChip->pRegMap);
					free(hChip);
					hChip = NULL;	
				}
			}
			else
			{
				free(hChip->pRegMap);
				free(hChip);
				hChip = NULL;
			}
		}
		else
		{
			free(hChip);
			hChip = NULL;	
		}
	}
	
	return hChip;
}

/*****************************************************
**FUNCTION	::	ChipClose
**ACTION	::	Close a chip
**PARAMS IN	::	hChip	==> handle to the chip
**PARAMS OUT::	NONE
**RETURN	::	CHIPERR_NO_ERROR if ok, CHIPERR_INVALID_HANDLE otherwise
*****************************************************/
STCHIP_Error_t	ChipClose(STCHIP_Handle_t hChip)
{
	STCHIP_Error_t error = CHIPERR_NO_ERROR;
	NODE *node = NULL;
	
	if(hChip != NULL)
	{
		node = ChipFindNode(hChip);
		
		#ifndef NO_GUI
			if(node == pFirstNode)
			{
				CloseHandle(ChipMutex);
				ChipMutex = NULL;
			}
		#endif
		
		DeleteNode(node);
		free(hChip->pFieldMap);
		free(hChip->pRegMap);
		free(hChip);
	}
	else
		error = CHIPERR_INVALID_HANDLE;
	
	return error; 
}

/*****************************************************
**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,U16 RegId,char * Name,U16 Address,U8 Default,STCHIP_Access_t Access)
{
	STCHIP_Register_t *pReg;
	
	if(hChip != NULL)
	{	
		if((RegId >= 0) && (RegId < hChip->NbRegs))
		{
			pReg=&hChip->pRegMap[RegId];
	
			pReg->Addr	= Address;
			pReg->Default	= Default;
			pReg->Value	= Default;
			pReg->Access = Access;
			strcpy(pReg->Name,Name);
		}
		else
			hChip->Error = CHIPERR_INVALID_REG_ID;

⌨️ 快捷键说明

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