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

📄 task_command.c

📁 CC1000 Rf modem C codes for philips sLpc213X ARM MCU.
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "config_GD61.h"
#include "include_GD61.h"
#include <stdio.h>

////////////////////////////////////////////////////////////////
uint16 AlarmHostAddress;		//Host address, for alarm system

uint16 Line_Num;		//number of line.
uint16 SourceAddr;		//source address.
uint16 DestinationAddr;	//destination address.
uint16 Pkg_SerialNum;	//serial number of package.

uint16 ExecutedPkgID;	//serial number of package.
uint8  PkgExecutedCnt;	//package be executed how many time counter.
uint16 PriorityPkgID;

uint8  RoutingFlag;		//function code.
uint8  RouteTableLen;	//byte counter.
uint16 RouteAddress;	//next jump address.

uint8  ActionCode;		//action code just like function code.
uint8  ActionValueLen;	//action value corresponding to action code.
uint16 ActionValue;		//action value corresponding to action code.
uint16 PassWord;		//action value corresponding to action code.

uint8  SendByteCounter;
uint8  RFSendByteCounter;

uint8  AlarmInfoSendCnt;
uint16 CrcCheckCode;

////////////////////////////////////////////////////
uint16 ModuleAddress;		//=0x5555;	//module address.
uint16 PowerLine_ID;		//number of line.

uint16 TubeNumber;

uint16 TransferKKK;
uint16 ControlTime;
uint16 MaxInputLimitII;

uint16 ScanningAddress;
uint8  CommandClrCount;

///////////////////////////////////////////////////
ROUTETABLE UpRouteTable[MAX_BRANCH_LINE_NUM];	//route table array,[i] i indicates subline
ROUTETABLE DnRouteTable[MAX_BRANCH_LINE_NUM];	//route table array,[i] i indicates subline
uint8 AnalysisBuf[RX_BUFFER_LEN];				// command analysis buffer

extern ROUTEDATA routeData[MAX_BRANCH_LINE_NUM];	//there is a routedata for each branchLine.

#ifdef RP_RELAY_ERROR
extern ROUTEDATA relayErrorData[MAX_BRANCH_LINE_NUM];
#endif
 
extern volatile CALENDAR SettingTime;
extern volatile CALENDAR CurrentTime;

extern volatile uint16 enTimerLightCnt;			// enable timer automatic function 
extern volatile uint16 PW_AlarmEnTimer;
extern volatile uint16 LD_AlarmEnTimer;
extern volatile uint16 LD_AlarmCounter;

extern volatile char   HeartBeetTimeSecTick;		// for display of time

extern volatile uint8  r_buf0[RX_BUFFER_LEN];		// UART0  transmit/receive buffer
extern volatile uint8  t_buf0[TX_BUFFER_LEN];

extern volatile uint8  r_buf1[RX_BUFFER_LEN];		// UART1  transmit/receive buffer
extern volatile uint8  t_buf1[TX_BUFFER_LEN];

extern volatile uint8  RF_Rx_Buffer[RX_BUFFER_LEN];	// CC1000 transmit/receivebuffer
extern volatile uint8  RF_Tx_Buffer[TX_BUFFER_LEN];

extern volatile uint16 ADC_Period_mS;
extern volatile uint16 ADCmS_Counter;
extern volatile uint16 ADC_ChannelTimer;

extern volatile uint16 SysReadyTimer;
extern volatile uint16 OutReadyTimer;

extern volatile uint16 MP_FailedmSecondCnt;			// for main power failed detector
extern volatile uint8  ADC_Flag;

extern volatile uint32* TraceAddr_PT;
extern uint8 PrtDebugInfo;

extern uint8 MaxInputIICnt;
extern uint8 AutoLightCntMode;
//
extern TIMEACTION ActionTime[MAX_CONTROL_TABLE_NUM];
extern uint16 ADC_EventCounter;

/*
extern int32  CurInput_II;		//after calibration
extern int32  CurInput_UU;
extern int32  CurInput_WP;
extern int32  CurInput_QP;
extern int32  CurInput_SP;
extern int32  CurInput_PF;		//PF = P/S

extern int32  CurInputULevel;
extern int32  CurInputILevel;

extern int32  CurOutput_I;
extern int32  CurOutput_U;
extern int32  CurOutput_WP;
extern int32  CurOutput_QP;
extern int32  CurOutput_SP;
extern int32  CurOutput_PF;

extern int32  CurOutputULevel;
extern int32  CurOutputILevel;
*/

extern int32  ADC_IIK_Value;	//calibrate K for I
extern int32  ADC_UUK_Value;	//calibrate K for U
extern int32  ADC_WPK_Value;	//calibrate K for WP
extern int32  ADC_QPK_Value;	//calibrate K for QP
extern int32  ADC_SPK_Value;	//calibrate K for SP

extern uint8  SysRunState;
extern uint16 TestDrvState;
extern uint16 ADC_FilterP;			// pointer for the filter buffer above
extern uint16 CC1000ResetTimer;		//CC1000 reset timer

extern uint16 AlarmSenderAddr1;
extern uint16 AlarmSenderAddr2;
extern uint16 AlarmSenderAddr3;
extern uint16 AlarmSenderAddr4;

/////////////////////////////////////////////////////
void  SysInitGD61(void);
void  TbufCurrentState(void);
void  TbufAlarmMsg(void);				//make alarm message frame
void  AlarmFunction(void);
void  TbufResponsePackage(void);
void  EnableControlTable(void);
void  DisableControlTable(void);
void  TbufDnRouteTable(void);
void  TbufUpRouteTable(void);
void  GetUpRouteTable(uint8 *table_pt);
void  GetDnRouteTable(uint8 *table_pt);
void  GetCalendar(uint8 *table_pt);
void  GetControlTable(uint8 *table_pt);
void  TbufControlTable(void);
void  TbufCalendar(void);
void  TbufI_U_WP_QP_SP_PF(void);
void  RF_ClrCommand(uint8 ClearCounter);
void  HeartBeat(void);
void  RepDispUIP_(void);
void  RF_CommandScaning(void);
uint8 RF_CommandAnalysis(uint8 *analysis_pt);
uint8 RF_FunExecute(uint8 ActionType,uint8 *buff_pt);
void  InitializeDevice(void);
void  PrintCurState(void);


/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
//
void InitializeDevice(void)
{
	
		
	IRQDisable();
	SetDefaultUart0();					// UART0 assigned to IRQ 			slot0.	ok.
	EINT0_Initialize();					// 分配外部中断0到向量中断4			slot4   ok.
	TimerInitialize();					// 置定时器0中断					slot5   ok.
	EINT2_Initialize();					// 分配外部中断2到向量中断6			slot6   ok.
	IRQEnable();						// 打开中断
}


/////////////////////////////////////////////////////////////
// copy r_buff to AnalysisBuf and check if a command is ready
// status: ok
void RF_CommandScaning(void)
{
uint16 dest_p;
uint16 sour_p;

	for(ScanningAddress=0;ScanningAddress<RX_BUFFER_LEN;ScanningAddress++){

#ifdef EN_RF_COMMAND_CHANNEL
		if(RF_Rx_Buffer[ScanningAddress]!=(uint8)RF_START_FLAG) continue;	//don't find RF_START_FLAG
#else
		if(r_buf0[ScanningAddress]!=(uint8)RF_START_FLAG) continue;	//don't find RF_START_FLAG
//		if(r_buf1[ScanningAddress]!=(uint8)RF_START_FLAG) continue;	//don't find RF_START_FLAG
#endif

		for(dest_p=0;dest_p<RX_BUFFER_LEN;dest_p++){
			sour_p = (ScanningAddress+dest_p)%RX_BUFFER_LEN;

#ifdef EN_RF_COMMAND_CHANNEL
			AnalysisBuf[dest_p]=RF_Rx_Buffer[sour_p];	// point to RF channel
#else
			AnalysisBuf[dest_p]=r_buf0[sour_p];			// point to Uart0 channel
//			AnalysisBuf[dest_p]=r_buf1[sour_p];			// point to Uart1 channel
#endif
		}

#ifndef EN_PRT_DEBUG_INFO
		PrtDebugInfo=DISABLE_FLAG;
#endif
		RF_CommandAnalysis(AnalysisBuf);	//// RF protocol
	}
}

///////////////////////////////////////////////////////////
uint8 RF_CommandAnalysis(uint8 *analysis_pt)
{
uint16 dataIn;
uint8 result;

#ifdef EN_PKG_LOOP_KILLER
char databuf[TX_BUFFER_LEN];
#endif

	Trace_FunAddr((void*)RF_CommandAnalysis);
	
//// check starting semaphore
	dataIn=(*analysis_pt<<8)+*(analysis_pt+1);
	if(dataIn!=RF_START_FLAG){
		return NOTHING_TO_DO;	//don't find starting flag
	}

//// get information from the data package
	Line_Num		=(*(analysis_pt+0+RF_DATA_ADDR_BIAS)<<8)+*(analysis_pt+1+RF_DATA_ADDR_BIAS);	//get line number
	SourceAddr		=(*(analysis_pt+2+RF_DATA_ADDR_BIAS)<<8)+*(analysis_pt+3+RF_DATA_ADDR_BIAS);	//get source address
	DestinationAddr	=(*(analysis_pt+4+RF_DATA_ADDR_BIAS)<<8)+*(analysis_pt+5+RF_DATA_ADDR_BIAS);	//get dest addr
	Pkg_SerialNum	=(*(analysis_pt+6+RF_DATA_ADDR_BIAS)<<8)+*(analysis_pt+7+RF_DATA_ADDR_BIAS);	//get the package serial number

	RoutingFlag		=*(analysis_pt+8+RF_DATA_ADDR_BIAS);	// get routing flag, which indicates routing direction.
	RouteTableLen	=*(analysis_pt+9+RF_DATA_ADDR_BIAS);	// byte counter

	if(RouteTableLen==0x03){
		RouteAddress=(*(analysis_pt+11+RF_DATA_ADDR_BIAS)<<8)+*(analysis_pt+12+RF_DATA_ADDR_BIAS);
	}
//	else{						// modified by Travis Feb.9,2006
//		RouteTableLen=0;		// support only one route address
//		RouteAddress =0;
//	}

////
	ActionCode	   =*(analysis_pt+10+RouteTableLen+RF_DATA_ADDR_BIAS);
	ActionValueLen =*(analysis_pt+11+RouteTableLen+RF_DATA_ADDR_BIAS);

	if(ActionValueLen==0x02){		// if ValueLength==0x02, then get the ActionValue
		ActionValue=(*(analysis_pt+12+RouteTableLen+RF_DATA_ADDR_BIAS)<<8)+
					 *(analysis_pt+13+RouteTableLen+RF_DATA_ADDR_BIAS);	
		PassWord   =0x00;			//clear the pass word
	}

	else if(ActionValueLen==0x04){	// if ValueLength==0x04, then get the ActionValue and passed word
		ActionValue	=(*(analysis_pt+12+RouteTableLen+RF_DATA_ADDR_BIAS)<<8)+
					  *(analysis_pt+13+RouteTableLen+RF_DATA_ADDR_BIAS);	

		PassWord	=(*(analysis_pt+14+RouteTableLen+RF_DATA_ADDR_BIAS)<<8)+
					  *(analysis_pt+15+RouteTableLen+RF_DATA_ADDR_BIAS);	
	}
	
////
//	14 =	2	+2	+2	+2,	+1	+1,	+1	+1,	+2		// modified by Travis Feb.9,2006
//		    L	S	D	SN	RF	RL	AC	AL	CRC		// package length
	CommandClrCount	=14+RouteTableLen+ActionValueLen+RF_DATA_ADDR_BIAS;

//// check CRC code	
	CrcCheckCode   	=(*(analysis_pt+12+RouteTableLen+ActionValueLen+RF_DATA_ADDR_BIAS)<<8)+
					  *(analysis_pt+13+RouteTableLen+ActionValueLen+RF_DATA_ADDR_BIAS);
	dataIn=	Uart0CheckCRC16(analysis_pt,(CommandClrCount-2));				//calaulate the CRC check code
	if((CrcCheckCode != dataIn)&&(CrcCheckCode != RF_ENDING_FLAG)){	//!!!available after debugging
		return NOTHING_TO_DO;									//check the CRC code or ending flag
	}

//// CRC OK,deal with unsupported frame_1.
	if(RouteTableLen != 0x03 ){									// modified by Travis Feb.9,2006
		Uart0SendString("RouteTableLen!=0x03,unsupported command.");
		RF_ClrCommand(CommandClrCount);	//the package from other power_line(GD60)
		return NOTHING_TO_DO;			//check the power line id
	}

//// a data package ready to treatment.
	if(Line_Num != PowerLine_ID){			//Line_id must be matched
		Uart0SendString("a command from other PowerLine");
		RF_ClrCommand(CommandClrCount);	//the package from other power_line(GD60)
		return NOTHING_TO_DO;			//check the power line id
	}
	
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////// Line_ID is correct //////////////////

	enTimerLightCnt  = EN_TIMER_SCHEDULER_TIME;	// reset the timer_control time
	CC1000ResetTimer = CC1000_TIMER_RESET_TIME;
	TestDrvState	 = TEST_DRV_STATE_STOP;
//
#ifndef EN_TIMER_LIGHT_CONTROL
	for(dataIn=0;dataIn<MAX_CONTROL_TABLE_NUM;dataIn++){
		ActionTime[dataIn].ActionFlag=DISABLE_FLAG;
	}
#endif

////
	Uart0SendEnter();
	if((RoutingFlag&0x80)==0x80){
		Uart0SendString("Command from Up direction.");
	}else{
		Uart0SendString("Command from Dn direction.");
	}
	
#ifdef EN_MAIN_POWER_ALARM
//	if((IO0PIN&PW_INT_P0_15)==PW_INT_P0_15){			//level detecting mode
	if(MP_FailedmSecondCnt>=MAIN_POWER_FAILED_MSECOND){		//pulse detecting mode
		Uart0SendString("System in alarm state");
//		RF_ClrCommand(CommandClrCount);	//the package from other power_line(GD60)
//		return NOTHING_TO_DO;			//check the power line id
	}
#endif
	
////////////////////////////////////////////////////////////////////////////////
//state_1_1;	package function execution state.
	if(RouteAddress == ModuleAddress){			//Line_ID and RouteAddress matched
		if((DestinationAddr == ModuleAddress)||(DestinationAddr == RF_BROAD_CAST_ADDR)){
								//Line_ID,DestinationAddress and RouteAddress matched
			Uart0SendString("in executing state, to stopRelayPackage.");
			StopRelayBranch();		// stop relay packages in the same branch.
			GetAlarmSenderAddr();	// catch the alarm sender address
			
			if(DestinationAddr == RF_BROAD_CAST_ADDR){
				AlarmHostAddress = SourceAddr;			//for alarm system
				Uart0SendString("Get host address for alarm system.");
			}

#ifdef EN_PKG_LOOP_KILLER
//// check if the package be executed.			// modified by Travis Feb.9,2006
			if(ExecutedPkgID == Pkg_SerialNum)	// the same package can be executed only once
			{									//the same package cann't be relay firstly, then be executed then.
				sprintf(databuf,"ExecutedPkgID=%x,kill looping package.",ExecutedPkgID);
				Uart0SendString(databuf);

				Uart0SendString("sending RFstopRoutingPkg");
				RF_SendingOver();
				MakeRFstopRoutePkg();
				RF_SendAddCrc(RFSendByteCounter);	// sending data in buffer
				RF_ClrCommand(CommandClrCount);
				return NOTHING_TO_DO;
			}

//// find a new package (new package ID)
			ExecutedPkgID = Pkg_SerialNum;
			PkgExecutedCnt = 1;
#endif
			ClearRouteData();	//clear other routing task, at the same time only one is available
			GetRouteData(analysis_pt+10+RouteTableLen+RF_DATA_ADDR_BIAS);			//from action code
	
			DelayMS_(8);
			Uart0SendString("Startup RF_FunExecute");
			RF_FunExecute(ActionCode,analysis_pt);	//send reply message
			RF_ClrCommand(CommandClrCount);
			return result;							//something done, return 1; otherwise return 0.
		}

//state_1_2;	//package relay state.
		else if(DestinationAddr != ModuleAddress){	//Line_ID and RouteAddress matched, but DestinationAddress not matched
			Uart0SendString("in relaying state, to stopRelayPackage.");
			StopRelayBranch();	//kill the other relay task for the same branch.
			GetAlarmSenderAddr();	// catch the alarm sender address
						
#ifdef EN_PKG_LOOP_KILLER
//// check if the package be executed.		// modified by Travis Feb.9,2006
			if((ExecutedPkgID == Pkg_SerialNum)&&(PkgExecutedCnt >= 4))  //the same package can be passed 5 times maximum.
			{
				sprintf(databuf,"ExecutedPkgID=%x,kill looping package.",ExecutedPkgID); 
				Uart0SendString(databuf);
				
				Uart0SendString("sending RFstopRoutingPkg");
				RF_SendingOver();
				MakeRFstopRoutePkg();
				RF_SendAddCrc(RFSendByteCounter);		// sending data in buffer

				RF_ClrCommand(CommandClrCount);
				return NOTHING_TO_DO;
			}
//// package relay one more time.			
			if(ExecutedPkgID == Pkg_SerialNum){
				PkgExecutedCnt++;
			}else{ 
				ExecutedPkgID=Pkg_SerialNum;
				PkgExecutedCnt=1;
			}
#endif
			ClearRouteData();	//clear other routing task, at the same time only one type is available
			GetRouteData(analysis_pt+10+RouteTableLen+RF_DATA_ADDR_BIAS);		//from action code

			DelayMS_(9);
			Uart0SendString("Startup relayPackage");
			MakeRelayPackage(KEEP_ROUTE_DIR);		//if module_address!=destination address, relay the massage(data package).
			RF_ClrCommand(CommandClrCount);
			return RELAY_PACKAGE;				// something done, return 1; otherwise return 0.
		}
	}

//state_2_1;
	else{	// listen package state.	// Line_ID matched, but RouteAddress not matched
			DelayMS_(9);
			Uart0SendString("in listenning state, to stopRelayPackage.");
			StopRelayBranch();			// kill the other relay task
			GetAlarmSenderAddr();		// catch the alarm sender address

#ifdef EN_ON_OFF_PRIORITY				// modified by Travis Feb.9,2006
			if((DestinationAddr==ModuleAddress)||(DestinationAddr==RF_BROAD_CAST_ADDR)){

				if(PriorityPkgID != Pkg_SerialNum){			// the same package can be executed only once
					Uart0SendString("Command be executed in higt priority.");
					if(ActionCode==VOLTAGE_CONTROL){		// this segment can be executed many times.
						CommandLightControl(ActionValue);	// executed light control in higt priority.
						PriorityPkgID = Pkg_SerialNum;
						Uart0SendString("CommandLightControl(ActionValue)");
					}else{
						Uart0SendString("But nothing to do.");
					}
				}else{
					Uart0SendString("Cancel executed command in higt priority.");
				}
			}

⌨️ 快捷键说明

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