phantom_api.c

来自「QPSK Tuner details, for conexant chipset」· C语言 代码 · 共 1,923 行 · 第 1/5 页

C
1,923
字号
	if (reg_value != 1UL) /* Not ready */
	{
		PHANTOM_DBG_SET_ERROR(PHANTOM_LNB_BUSY);
		return(False);
	}

	/* Get LNB messages length */
	if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_BC_LNB_RX_MSG_LENGTH, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
	{
		return(False);
	}

	*p_received_len = (unsigned char)reg_value;

	if (*p_received_len > PHANTOM_MAX_LNB_REPLY_BYTE)
	{
		*p_received_len = PHANTOM_MAX_LNB_REPLY_BYTE;
	}

	/* Get parity error info */
	if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_D6_LNB_REPLY_PARITY, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
	{
		return(False);
	}

	*p_parity_errors = (unsigned char)reg_value;

	/* Get reply errors */
	if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_BD_LNB_RX_ERROR, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
	{
		return(False);
	}

	*p_reply_errors = (unsigned char)reg_value;

	if (buffer_len < *p_received_len)
	{
		*p_received_len = buffer_len;
	}

	/* Extract the bytes from the demod */
	for (byte_count = 0; byte_count < *p_received_len; byte_count++)
	{
		switch (byte_count)
		{
		case 0:
			if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_BE_LNB_REPLY_BYTE0, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
			{
				return(False);
			}
			break;
		case 1:
			if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_BF_LNB_REPLY_BYTE1, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
			{
				return(False);
			}
			break;
		case 2:
			if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_C0_LNB_REPLY_BYTE2, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
			{
				return(False);
			}
			break;
		case 3:
			if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_C1_LNB_REPLY_BYTE3, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
			{
				return(False);
			}
			break;
		case 4:
			if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_C2_LNB_REPLY_BYTE4, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
			{
				return(False);
			}
			break;
		case 5:
			if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_C3_LNB_REPLY_BYTE5, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
			{
				return(False);
			}
			break;
		case 6:
			if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_C4_LNB_REPLY_BYTE6, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
			{
				return(False);
			}
			break;
		case 7:
			if (PHANTOM_RegisterRead(p_nim, PHANTOM_P0_C5_LNB_REPLY_BYTE7, &reg_value, PHANTOM_USE_DEMOD_HANDLE) == False)  
			{
				return(False);
			}
			break;
		default:
			return (True);
		}

		p_buffer[byte_count] = (unsigned char)reg_value;
	}
	
	/* Discard the last byte if it has errors */
	if ((*p_received_len != 0) && (*p_reply_errors != 0))
	{
		if (((0x01 << (*p_received_len-1)) & *p_reply_errors) == *p_reply_errors) 
		{
			*p_received_len = (unsigned char)(*p_received_len - 0x01);
		}
	}
	return (True);
}


/*******************************************************************************************************
 * PHANTOM_DiseqcSendMessage
 * Function to send diseqc message.
 * Parameters:
 * I/O	Parameters	      Descriptions
 * --------------------------------------------------------------------------------
 * IN	p_nim	          Pointer to PHANTOM_NIM structure allocated by application.
 * IN	p_message	      Message to send.
 * IN   message_length    Length in BYTEs of message to send
 * IN   last_message      Indicates if current message is the last message (TRUE means last message).
 * IN   reply_mode        Reply mode: interrogation/no reply/quick reply.
 * IN   burst_type        Burst tone at last-message: (modulated = ToneB; Un-modulated = ToneA). 
 *******************************************************************************************************/
BOOL          
PHANTOM_DiseqcSendMessage(PHANTOM_NIM      *p_nim,                  
                          unsigned char    *p_message,            
                          unsigned char     message_length,        
                          BOOL              last_message,  						  
			              PHANTOM_RXMODE    reply_mode,
                          PHANTOM_LNB_BURST burst_tone)            
{
	unsigned char llf_not_last_message;
	
	unsigned char  outlen;
    unsigned char  last_len;
	unsigned char  sa_sb;   //Tone A or Tone B

	unsigned char long_message;	
	int i;
	long_message = FALSE;

	/* Test for valid nim */
	PHANTOM_DBG_VALIDATE_NIM(p_nim);

	/* Input validation */
	if (p_message == 0 || 
		message_length == 0 || 
		message_length < PHANTOM_MIN_LNB_MSG)// ||  /* minimum of PHANTOM_MIN_LNB_MSG in a message */
		//message_length > PHANTOM_MAX_LNB_MSG)    /* maximum of PHANTOM_MAX_LNB_MSG in a message */
	{
		PHANTOM_DBG_SET_ERROR(PHANTOM_BAD_PARM);
		return(False);
	}

	if(PHANTOM_DRIVER_IsDiseqcTxRdy(p_nim) == False)
	{
		return(False);
	}


	llf_not_last_message = (unsigned char)((last_message == True) ? 0x00 : 0x01);	
	
	sa_sb = (unsigned char)((burst_tone == PHANTOM_LNB_BURST_UNMODULATED) ? 0x00 : 0x01);	
	
	if (reply_mode != PHANTOM_RXMODE_NOREPLY)
	{
        unsigned short register_bitfield;
	
        /* clear the reply buffer in page0 */
        for (register_bitfield = PHANTOM_P0_BE_LNB_REPLY_BYTE0; register_bitfield <= PHANTOM_P0_C5_LNB_REPLY_BYTE7; register_bitfield++)
        {
		    if (PHANTOM_RegisterWrite(p_nim, register_bitfield, 0x00UL, PHANTOM_USE_DEMOD_HANDLE) == False)  
		    {
			    return(False);
		    }
        }
		/* clear the Rx ready flag */
		if (PHANTOM_RegisterWrite(p_nim, PHANTOM_P0_BC_LNB_RX_READY, 0x00UL, PHANTOM_USE_DEMOD_HANDLE) == False)  
		{
			return(False);
		}
	}

	if(message_length <= 6)
	{
		if (PHANTOM_LLF_LNBSend(p_nim, 
					sa_sb, 
					(unsigned char)reply_mode, 
					llf_not_last_message, 
					long_message,
					message_length, 							
					p_message[0],
					p_message[1],
					p_message[2],
					p_message[3],
					p_message[4],
					p_message[5]) == False)
		{
			return (False);
		}
	}
	else //if Long message
	{
		long_message = True;
		outlen = 6; //(unsigned char)(3+(message_length%4));
		if (PHANTOM_LLF_LNBSend(p_nim, 
                    sa_sb, 
                    (unsigned char)reply_mode, 
                    llf_not_last_message, 
					long_message,
                    outlen, 							
                    p_message[0],
					p_message[1],
					p_message[2],
					p_message[3],
					p_message[4],
					p_message[5]) == False)
		{
			return (False);
		}


		while (outlen < message_length)
		{
			unsigned char message[6];

			last_len = (unsigned char)((message_length - outlen) > 6 ? 6 : (message_length - outlen));
		      
			/* determine if this is the last message (lastmessage == False if this is the last message) */
			long_message = True;
			if ((last_len+outlen) >= message_length)  long_message = False;
						
			memset(message, 0, sizeof(message));
			for(i = 0; ((i < 6) && (i < last_len)); i++)
				message[i] = p_message[outlen + i];

			if(PHANTOM_DRIVER_IsDiseqcTxRdy(p_nim) == False)
			{
				return(False);
			}

			if (PHANTOM_LLF_LNBSend(p_nim, 
						sa_sb, 
						(unsigned char)reply_mode, 
						llf_not_last_message, 
						long_message,
						last_len, 							
						message[0],
						message[1],
						message[2],
						message[3],
						message[4],
						message[5]) == False)
			{
				return (False);
			}
			outlen = (unsigned char)(outlen + last_len);
		}
	}

	return (True);
}

/*******************************************************************************************************
 * PHANTOM_DiseqcSendToneBurst
 * Function to send a tone burst message.
 * Parameters:
 * I/O	Parameters	      Descriptions
 * --------------------------------------------------------------------------------
 * IN	p_nim	          Pointer to PHANTOM_NIM structure allocated by application.
 * IN   burst_mode         Burst type: (modulated = ToneB; Un-modulated = ToneA). 
 *******************************************************************************************************/

BOOL PHANTOM_DiseqcSendToneBurst(PHANTOM_NIM *p_nim, PHANTOM_LNB_BURST burst_mode)
{
	unsigned char sa_sb; //Tone Burst Type A or B

	/* Test for valid nim */
	PHANTOM_DBG_VALIDATE_NIM(p_nim);

	if(PHANTOM_DRIVER_IsDiseqcTxRdy(p_nim) == False)
	{
		return(False);
	}

	sa_sb = (unsigned char)((burst_mode == PHANTOM_LNB_BURST_UNMODULATED) ? 0x00 : 0x01);	

	if (PHANTOM_LLF_LNBSendToneBurst(p_nim, sa_sb)	== False)					
	{
		return (False);
	}

	return (True);
}
#endif /* PHANTOM_INCLUDE_DISEQC2 */


/*******************************************************************************************************
 * PHANTOM_DownloadFirmware() 
 * Function to do a software download.
 * This function called by the STB application downloads software XPROG (0-3FFF) of 8051 礐抯 memory 
 * regions. 
 * This function returns TRUE on success FALSE on failure.
 * Parameters:
 * I/O	Parameters	      Descriptions
 * --------------------------------------------------------------------------------
 * IN	p_nim	          Pointer to PHANTOM_NIM structure allocated by application.
 * IN	p_download	      Pointer to software download structure allocated by application.
 *
 *******************************************************************************************************/
BOOL
PHANTOM_DownloadFirmware(PHANTOM_NIM* p_nim, PHANTOM_SW_DOWNLOAD *p_download) 
{
	unsigned long status;

    /* If image length is 0, no download will happen. */
	if (!(p_download->code_length))
	{
		PHANTOM_DBG_SET_ERROR(PHANTOM_FIRMWARE_DOWNLOAD_BAD_LENGTH);
		return(False);
	}

	/* "1" is not valid image length since the last byte is suppose to be CRC. */
	if (p_download->code_length == 1)
	{
		PHANTOM_DBG_SET_ERROR(PHANTOM_FIRMWARE_DOWNLOAD_BAD_LENGTH);
		return(False);
	}

	/* reset Phantom. */
	(*p_nim->SBWrite)(p_nim->demod_handle,
					  PHANTOM_SC_RESET_CNTL,
					  PHANTOM_UC8051_RESET_HIGH,
					  &status);
	if (status)
	{
		return(False);
	}

    if (p_download->code_length)
	{
		/* Check code pointer. */
		if (!p_download->p_code)
		{
			PHANTOM_DBG_SET_ERROR(PHANTOM_FIRMWARE_DOWNLOAD_BAD_IMAGE);
			return(False);
		}

		/* select/enable code download. */
		(*p_nim->SBWrite)(p_nim->demod_handle,
						  PHANTOM_SC_DOWNLOAD_CONTROL,
						  PHANTOM_UC8051_XPROG_LOAD,
						  &status);
		if (status)
		{
			return(False);
		}

        // Download XPROG */
        if (PHANTOM_DRIVER_Download(p_nim, p_download->code_length, p_download->p_code) == False)
        {
		return (False);
        }
    
	} /* if (p_download->code_length) */
	/* Release reset of the 8051 module */ 
	(*p_nim->SBWrite)(p_nim->demod_handle,
					  PHANTOM_SC_RESET_CNTL,
					  PHANTOM_UC8051_RESET_LOW,
					  &status);
	if (status)
	{
		return(False);
	} 

    return (True);
 }


#if PHANTOM_INCLUDE_BER
/**************************************

⌨️ 快捷键说明

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