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

📄 uartcommand.c.svn-base

📁 最新火热的CX32 源代码
💻 SVN-BASE
字号:
     
#include "TUartlow.h"
#include "UartCommand.h"
#include "IOctrl.h"
#include "tvdebug.h"
#include "Ram.h"
#include "TVKey.h"
#include "..\data\n009_k.h"

#ifdef _USE_STi7710_
extern Gdata GlobalData g_Data;
Byte Packetbuff[28];
Word ATVAck_TransID;
Byte ucReceiveCnt = 0;
Byte ucACCERRORP = 0;
Byte ucSendCnt = 0;


static Code unsigned short tusCRC16[256] =
{
   0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
   0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
   0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
   0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
   0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
   0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
   0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
   0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
   0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
   0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
   0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
   0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
   0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
   0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
   0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
   0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
   0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
   0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
   0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
   0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
   0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
   0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
   0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
   0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
   0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
   0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
   0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
   0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
   0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
   0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
   0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
   0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
};

#endif

/*****************************************************************************\
  DESCRIPTION
    Calculate 16 bit CRC

  ARGUMENTS
    void *start_ptr     - <IN> Start address of data for crc'ing.
    unsigned int length - <IN> Block size to be crc'ed.

  RETURN VALUES
    unsigned short - 16 bit CRC code.

  COMMENTS
    Calculates the "standard" PC 16 bit CRC from a start address for the size
    specified. This the algorithm gives the same result as used by PC archiving
    programs that use 16 bit CRCs e.g. PKARC, LHA, LHARC, LZH etc.

\****************************************************************************/
Word CRC16_process(unsigned char*start_ptr, int length)
{
#ifdef _USE_STi7710_
  unsigned char   *data_ptr;
  WORD crc_value=0xffff;
  WORD crc_value1,crc_value2,crc_value3,crc_value5;
  Byte crc_value4;
  /* init the start value */
  data_ptr  = start_ptr;
   
  /* calculate CRC */
  while (length--)
  {
    crc_value1 = (WORD)crc_value >> 8;
    crc_value2 = (WORD)crc_value1 & 0xFF;
    crc_value4 = crc_value ^ (unsigned char)*data_ptr++;
    crc_value5 = tusCRC16[crc_value4 & 0xFF];
    crc_value = (WORD)crc_value2 ^ crc_value5;
   // crc_value = (((crc_value >> 8) & 0xFF) ^ tusCRC16[((crc_value ^ (unsigned char) *data_ptr++) & 0xFF)]);
  }
  return (crc_value);
 #else
  return 0;
 #endif
}

Byte  HandleUartCommand(Byte ucVirtualKey)
{
	IByte i;
	Byte PreloadPara[4];
#ifdef _USE_STi7710_
     switch(ucVirtualKey)
     {
		case _VK_RED_:
		    SendUartCommand(_DTVOP_TV_KEY_EVENT_,_SUBOP_IR_KEY_MENU_,0x0000,0x0000,_NULL_,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_1_://TEST TO SET CC
			PreloadPara[0]= _CCD_DIGITAL1_;
		    SendUartCommand(_DTVOP_TV_CCD_,_SUBOP_CCD_Set_Service_,0x0000,0x0001,PreloadPara,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_7_://TEST TO SET TV RATING
			for(i = 0;i < 4;i++)
				PreloadPara[i]= ((DWORD)_RATING_MPAA_G_)>>(i*8);
		    SendUartCommand(_DTVOP_TV_RATING_,_SUBOP_Rationg_Set_Rating_,0x0000,0x0004,PreloadPara,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_9_://TEST TO CLEAR TV RATING
			for(i = 0;i < 4;i++)
				PreloadPara[i]= ((DWORD)_RATING_MPAA_G_)>>(i*8);
		    SendUartCommand(_DTVOP_TV_RATING_,_SUBOP_Rationg_Clear_Rating_,0x0000,0x0004,PreloadPara,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_2_:
		    SendUartCommand(_DTVOP_TV_KEY_EVENT_,_SUBOP_IR_KEY_ARROWUP_,0x0000,0x0000,_NULL_,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_5_:
		    SendUartCommand(_DTVOP_TV_KEY_EVENT_,_SUBOP_IR_KEY_SELECT_,0x0000,0x0000,_NULL_,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_8_:
		    SendUartCommand(_DTVOP_TV_KEY_EVENT_,_SUBOP_IR_KEY_ARROWDOWN_,0x0000,0x0000,_NULL_,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_4_:
		    SendUartCommand(_DTVOP_TV_KEY_EVENT_,_SUBOP_IR_KEY_ARROWLEFT_,0x0000,0x0000,_NULL_,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_6_:
		    SendUartCommand(_DTVOP_TV_KEY_EVENT_,_SUBOP_IR_KEY_ARROWRIGHT_,0x0000,0x0000,_NULL_,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_0_:
			SendUartCommand(_DTVOP_TV_KEY_EVENT_,_SUBOP_IR_KEY_NUM0_,0x0000,0x0000,_NULL_,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_SEND_ACK_:
		    SendUartCommand(_DTVOP_TV_ACK_,_SUBOP_NONE_,ATVAck_TransID,0x0000,_NULL_,ucVirtualKey);
			ucVirtualKey = _NULL_;
		break;
		case _VK_UART_RESEND_:
			if(s_SysUARTStruct.bDtvAcked == _FALSE_)
			{
				s_SysUARTStruct.bResendCmding = _TRUE_;
				s_SysUARTStruct.wBakTransID--;
				if(s_SysUARTStruct.wBakTransID == 0x8011)
				s_SysUARTStruct.wBakTransID = 0xFFFE;
			    tdAddVirtualKey(s_SysUARTStruct.ucResendCmd);
			}
			ucVirtualKey = _NULL_;
		break;
		default:
		break;
     }
#endif
	return ucVirtualKey;
}

Void SendUartCommandinit(void)
{
#ifdef _USE_STi7710_
	UART1_ENABLE_RINT;
	s_SysUARTStruct.PackData.TranID = (0x8012 - 1);
	s_SysUARTStruct.wBakTransID	= (0x8012 - 1);
	s_SysUARTStruct.Packetlength = 0x0000;
	s_SysUARTStruct.bDtvAcked = _TRUE_;
	s_SysUARTStruct.ucResendCmdTimer = 0;
	s_SysUARTStruct.ucResendTryTimes = 0;
	s_UARTReceiveStruct.Packetlength = 0x0000;
#endif
}

Void SendUartCommand(Word DTVOper,Word SubOper,Word TranID,Word Payloadsize,APByte Payload,Byte ucVirtualKey)
{
#ifdef _USE_STi7710_
	int ii,k,k1;
	Byte Payloadvalue[20];
	APByte p=(APByte )&s_SysUARTStruct.PackData;
	k1 = 0;
	//if(!g_Data.bUartReceiveProcessing)
	{
		//UART1_DISABLE_RECEIVE;
		tdSleep(20);
		g_Data.bUartSentProcessing = _TRUE_;
		s_SysUARTStruct.PackData.Magic = 0xEFBE;
		s_SysUARTStruct.PackData.Cmdtype = 0x19;
		s_SysUARTStruct.PackData.Framesize = 0x0008 + Payloadsize;
		s_SysUARTStruct.PackData.CRC = 0x0000;
		s_SysUARTStruct.PackData.Dtvoper = DTVOper;
		s_SysUARTStruct.PackData.Suboper = SubOper;
		if(TranID == 0)
		{
			s_SysUARTStruct.wBakTransID++;
			s_SysUARTStruct.PackData.TranID = s_SysUARTStruct.wBakTransID;
			if(s_SysUARTStruct.PackData.TranID >= 0xffff)
				s_SysUARTStruct.PackData.TranID = 0x8012;
			s_SysUARTStruct.wBakTransID = s_SysUARTStruct.PackData.TranID;
			s_SysUARTStruct.bDtvAcked = _FALSE_;
	 		s_SysUARTStruct.ucResendCmd = ucVirtualKey;
//			s_SysUARTStruct.ucResendCmdTimer = 2;//YYM_06_07_17 delete for[]
			if(s_SysUARTStruct.bResendCmding == _FALSE_)
			{
				s_SysUARTStruct.ucResendCmdTimer = 3;
				s_SysUARTStruct.ucResendTryTimes = 3;
			}
			else
				s_SysUARTStruct.ucResendCmdTimer = 2;
		}
		else//for atv to send ack
			s_SysUARTStruct.PackData.TranID = TranID;

		s_SysUARTStruct.PackData.Payloadsize= Payloadsize;
		s_SysUARTStruct.PackData.Payload= Payload;
		if (Payloadsize!=0)
		{
			for(k=0;k<Payloadsize;k++)
			{
				Payloadvalue[k] = *Payload++;
				*(p+0x0f+k) = Payloadvalue[k];
			}
		}
		s_SysUARTStruct.PackData.CRC = CRC16_process((unsigned char*)p,0x0f + Payloadsize); 
		ucReceiveCnt = 0;
#if 0 //--------YYM_06_07_17 for[]
        tdGotoXY(1, 7);
        tdPuts((RPByte)"SEND:");
        tdGotoXY(1, 8);
#endif
//--------YYM_06_07_17 added for[]
		
//added end
		for(ii=0; ii< 0x0f + Payloadsize; ii++)  
		{
			if (ii< 0x0f)
			{
#if 1 //--------YYM_06_06_20 for[]
				putchar(*p++); 
#else
				putchar(*p); 
		        tdPrintNumber(_ID_HEX_, 3, *p, _TRUE_);
        		tdPuts((RPByte)" ");
				p++;
#endif
			}
			else if((Payloadsize!= 0x0000)&&(ii>=0x0f))
			{
#if 1 //--------YYM_06_06_20 for[]
				putchar(Payloadvalue[k1++]);
#else
				putchar(Payloadvalue[k1]);
		        tdPrintNumber(_ID_HEX_, 3, Payloadvalue[k1], _TRUE_);
        		tdPuts((RPByte)" ");
				k1++;
#endif
			}
		} 
		ucSendCnt = ii;
		g_Data.bUartSentProcessing = _FALSE_;
		s_SysUARTStruct.Packetlength = 0x0000;
		s_UARTReceiveStruct.Packetlength = 0x0000;//YYM_06_07_17 delete for[]
		//UART1_ENABLE_RECEIVE;
	}
#endif
}

Void ReceiveUartCommand(Void)
{
#ifdef _USE_STi7710_
#if 1 //--------YYM_06_06_19 for[]
	IByte Acc;
	APByte p1 = (Byte *)&Packetbuff;
	Acc = SC1CR;
	Acc &= 0x1c;
	if(Acc == 0)
	{
		ucReceiveCnt++;
		g_Data.bUartReceiveProcessing = _TRUE_;
		Packetbuff[s_UARTReceiveStruct.Packetlength] = SC1BUF;
		s_UARTReceiveStruct.Packetlength++;
		if((s_UARTReceiveStruct.Packetlength == 0x02)&&((Packetbuff[0] != 0xBE)||(Packetbuff[1]!=0xEF)))
		{
			//UART1_DISABLE_RECEIVE;
#ifdef _DEBUG_STI7710_
			tdGotoXY(10, 5);
	        tdPuts((RPByte)"magic error");
#endif
			s_UARTReceiveStruct.Packetlength = 0x0000;
		}
		if(s_UARTReceiveStruct.Packetlength == 0x000f) 
		{
			s_UARTReceiveStruct.PackData.Payloadsize = (WORD)Packetbuff[0x0d] + ((WORD)(Packetbuff[0x0e]))*256;
		}
		if(s_UARTReceiveStruct.Packetlength >= (0x000f + s_UARTReceiveStruct.PackData.Payloadsize))
		{
			s_UARTReceiveStruct.PackData.Magic = 0xEFBE;
			s_UARTReceiveStruct.PackData.Cmdtype = 0x19;
			s_UARTReceiveStruct.PackData.Framesize = 0x0008 + s_SysUARTStruct.PackData.Payloadsize;
			s_UARTReceiveStruct.PackData.CRC = 0x0000;
			s_UARTReceiveStruct.PackData.Dtvoper = Packetbuff[0x07]+((WORD)(Packetbuff[0x08]))*256;
			s_UARTReceiveStruct.PackData.Suboper = Packetbuff[0x09]+((WORD)(Packetbuff[0x0a]))*256;
			s_UARTReceiveStruct.PackData.TranID = Packetbuff[0x0b]+((WORD)(Packetbuff[0x0c]))*256;
			s_UARTReceiveStruct.Packetlength = 0x0000;
			g_Data.bUartReceiveProcessing = _FALSE_;

			//if not a ack,add virtual key to send ack here
			//UART1_DISABLE_RECEIVE;
			if(s_UARTReceiveStruct.PackData.Dtvoper != 0x39)
			{
#ifdef _DEBUG_STI7710_
	        	tdGotoXY(10, 5);
	        	tdPuts((RPByte)"Not an ack,send ack to dtv");
#endif
#if 0 //--------YYM_06_07_19 for[]
				tdAddVirtualKey(_VK_SEND_ACK_);
				ATVAck_TransID = s_UARTReceiveStruct.PackData.TranID;
#endif
			}
			else
			{
				s_SysUARTStruct.bDtvAcked = _TRUE_;
				s_SysUARTStruct.ucResendCmdTimer = 0;
				s_SysUARTStruct.ucResendTryTimes = 0;
				s_SysUARTStruct.bResendCmding = _FALSE_;
#ifdef _DEBUG_STI7710_
	        	tdGotoXY(10, 6);
	        	tdPuts((RPByte)"is  an ack");
#endif
			}
		}
		g_Data.bUartReceiveProcessing = _FALSE_;
	}
	else
	{
		ucReceiveCnt++;
		ucACCERRORP = ucReceiveCnt;
		Packetbuff[s_UARTReceiveStruct.Packetlength] = SC1BUF;
		s_UARTReceiveStruct.Packetlength++;
/*--------YYM_06_07_18 delete for[]
		s_UARTReceiveStruct.Packetlength = 0x0000;
		s_SysUARTStruct.bDtvAcked = _TRUE_;
		s_SysUARTStruct.ucResendCmdTimer = 0;
		s_SysUARTStruct.ucResendTryTimes = 0;
		s_SysUARTStruct.bResendCmding = _FALSE_;
*///delete end
#ifdef _DEBUG_STI7710_
    	tdGotoXY(10, 6);
		tdPrintNumber(_ID_HEX_, 3, Acc, _TRUE_);
#endif
    	//tdPuts((RPByte)"acc error");
	}
		
#else
	APByte p1 = (Byte *)&Packetbuff;
    ucReceiveCnt++;
	//if(!g_Data.bUartSentProcessing)
	{
		g_Data.bUartReceiveProcessing = _TRUE_;
		Packetbuff[s_UARTReceiveStruct.Packetlength] = SC1BUF;
		s_UARTReceiveStruct.Packetlength++;
		if((s_UARTReceiveStruct.Packetlength == 0x02)&&((Packetbuff[0] != 0xBE)||(Packetbuff[1]!=0xEF)))
		{
			//UART1_DISABLE_RECEIVE;
			tdGotoXY(10, 5);
	        tdPuts((RPByte)"magic error");
			s_UARTReceiveStruct.Packetlength = 0x0000;
		}
		if(s_UARTReceiveStruct.Packetlength == 0x000f) 
		{
			s_UARTReceiveStruct.PackData.Payloadsize = (WORD)Packetbuff[0x0d] + ((WORD)(Packetbuff[0x0e]))*256;
		}
		if(s_UARTReceiveStruct.Packetlength >= (0x000f + s_UARTReceiveStruct.PackData.Payloadsize))
		{
			s_UARTReceiveStruct.PackData.Magic = 0xEFBE;
			s_UARTReceiveStruct.PackData.Cmdtype = 0x19;
			s_UARTReceiveStruct.PackData.Framesize = 0x0008 + s_SysUARTStruct.PackData.Payloadsize;
			s_UARTReceiveStruct.PackData.CRC = 0x0000;
			s_UARTReceiveStruct.PackData.Dtvoper = Packetbuff[0x07]+((WORD)(Packetbuff[0x08]))*256;
			s_UARTReceiveStruct.PackData.Suboper = Packetbuff[0x09]+((WORD)(Packetbuff[0x0a]))*256;
			s_UARTReceiveStruct.PackData.TranID = Packetbuff[0x0b]+((WORD)(Packetbuff[0x0c]))*256;
			s_UARTReceiveStruct.Packetlength = 0x0000;
			g_Data.bUartReceiveProcessing = _FALSE_;

			//if not a ack,add virtual key to send ack here
			//UART1_DISABLE_RECEIVE;
			if(s_UARTReceiveStruct.PackData.Dtvoper != 0x39)
			{
	        	tdGotoXY(10, 5);
	        	tdPuts((RPByte)"Not an ack,send ack to dtv");
				tdAddVirtualKey(_VK_SEND_ACK_);
				ATVAck_TransID = s_UARTReceiveStruct.PackData.TranID;
			}
			else
			{
				s_SysUARTStruct.bDtvAcked = _TRUE_;
				s_SysUARTStruct.ucResendCmdTimer = 0;
				s_SysUARTStruct.ucResendTryTimes = 0;
				s_SysUARTStruct.bResendCmding = _FALSE_;
	        	tdGotoXY(10, 6);
	        	tdPuts((RPByte)"is  an ack");
			}
		}
		g_Data.bUartReceiveProcessing = _FALSE_;
	}
#endif
#endif
}


⌨️ 快捷键说明

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