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

📄 command.c

📁 DSP 5409 plc应用程序,调试通过,是用在电力线通讯上的演示程序.
💻 C
📖 第 1 页 / 共 5 页
字号:
/*Master*/		default:
/*Master*/			//==============================================================================
/*Master*/			// Unrecognized command
/*Master*/			// Respond with unknown master command error code...
/*Master*/			//==============================================================================
/*Master*/			WriteUARTValue(UART_TARGET_HOST, ERR_M_UNKNOWN_COMMAND);
/*Master*/			ulAutoPollCounter = 0L;
/*Master*/			uAutoPollPause=0;		// Release AutoPoll.
/*Master*/	}
/*Master*/
/*Master*/	return;
}


//==========================================================================================
// Function:		ProcessSlaveCommand()
//
// Description: 	This function interprets and executes a command from the PL for the
//					SLAVE.  Commands must either be executed quickly, or set up state
//					variables which allow additional processing later.  They may not
//					tie up the processor for extended processing.
//
//					A command is received in a 128 byte PL packet with the following format:
//						parm	description
//						0		destination address - high
//						1		destination address - low
//						2		souce address - high
//						3		souce address - low
//						4		command number
//						5-63	optional command parameters (described for each cmd below)
//
//					Each command is responded to with a 128 byte response.
//					The first four parms of the response are the standard packet header
//					containing the source and destination addresses.  The remaining
//					60 parms are dependent on the command received.
//
//
// Revision History:
//==========================================================================================
void ProcessSlaveCommand(void)
{
/*Slave*/	u16		i;			// Loop index
/*Slave*/	u32		ulBerSequence;
/*Slave*/	u16		emeterStringIndex;
/*Slave*/	u32		ulAddr;				// 32-bit address formed from received data
/*Slave*/
/*Slave*/	switch( rxUserDataCopy[4] )			// Action depends on command received
/*Slave*/	{
/*Slave*/		case SC_GET_PARM:
/*Slave*/			//==============================================================================
/*Slave*/			// Command:  Get slave parm.
/*Slave*/			//
/*Slave*/			// rxUserDataCopy[0] = dest addr - high
/*Slave*/			// rxUserDataCopy[1] = dest addr - low
/*Slave*/			// rxUserDataCopy[2] = source addr - high
/*Slave*/			// rxUserDataCopy[3] = source addr - low
/*Slave*/			// rxUserDataCopy[4] = command number
/*Slave*/			// rxUserDataCopy[5] = index into configuration table.
/*Slave*/			//
/*Slave*/			// Return:
/*Slave*/			// txUserDataArray[0] = Master addr - high
/*Slave*/			// txUserDataArray[1] = Master addr - low
/*Slave*/			// txUserDataArray[2] = MyAddr - high
/*Slave*/			// txUserDataArray[3] = MyAddr - low
/*Slave*/			// txUserDataArray[4] = Status / Error
/*Slave*/			// txUserDataArray[5] = Data (high word if 32-bit parm).
/*Slave*/			// txUserDataArray[6] = Data (low word if 32-bit parm).
/*Slave*/			//==============================================================================
/*Slave*/			// Fill User Data buffer with zeroes
/*Slave*/			memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));	
/*Slave*/
/*Slave*/			txUserDataArray[0] = rxUserDataCopy[2];	// Master address
/*Slave*/			txUserDataArray[1] = rxUserDataCopy[3];	// Master address
/*Slave*/			*(u32*)&txUserDataArray[2] = ulMyAddr;
/*Slave*/
/*Slave*/			// ***** Error checking for parm number < 40
/*Slave*/			txUserDataArray[4] = SUCCESS;	// command status
/*Slave*/			if(rxUserDataCopy[5] < 30)	// request for 16-bit parm
/*Slave*/			{
/*Slave*/				txUserDataArray[5] = *(uppParms16[rxUserDataCopy[5]]);
/*Slave*/			}
/*Slave*/			else		// request for 32-bit parm
/*Slave*/			{
/*Slave*/				txUserDataArray[5] = *((u16*)(uppParms32[rxUserDataCopy[5]-30]));		// high word
/*Slave*/				txUserDataArray[6] = *((u16*)(uppParms32[rxUserDataCopy[5]-30]) + 1);	// low word
/*Slave*/			}
/*Slave*/			uTransmitPacketReady = 1;
/*Slave*/			uPlcState = PLC_RECEIVE_COMMAND;
/*Slave*/			break;
/*Slave*/
/*Slave*/		case SC_SET_PARM:
/*Slave*/			//==============================================================================
/*Slave*/			// Set slave parm.
/*Slave*/			//
/*Slave*/			// rxUserDataCopy[0] = dest addr - high
/*Slave*/			// rxUserDataCopy[1] = dest addr - low
/*Slave*/			// rxUserDataCopy[2] = source addr - high
/*Slave*/			// rxUserDataCopy[3] = source addr - low
/*Slave*/			// rxUserDataCopy[4] = command number
/*Slave*/			// rxUserDataCopy[5] = index into configuration table.
/*Slave*/			// rxUserDataCopy[6] = Data (high word if 32-bit parm).
/*Slave*/			// rxUserDataCopy[7] = Data (low word if 32-bit parm).
/*Slave*/			//
/*Slave*/			// Return:
/*Slave*/			// txUserDataArray[0] = Master addr - high
/*Slave*/			// txUserDataArray[1] = Master addr - low
/*Slave*/			// txUserDataArray[2] = MyAddr - high
/*Slave*/			// txUserDataArray[3] = MyAddr - low
/*Slave*/			// txUserDataArray[4] = Status / Error
/*Slave*/			//==============================================================================
/*Slave*/			// Fill User Data buffer with zeroes
/*Slave*/			memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));	
/*Slave*/
/*Slave*/			txUserDataArray[0] = rxUserDataCopy[2];	// Master address
/*Slave*/			txUserDataArray[1] = rxUserDataCopy[3];	// Master address
/*Slave*/			*(u32*)&txUserDataArray[2] = ulMyAddr;
/*Slave*/
/*Slave*/			// ***** Error checking for parm number < 40
/*Slave*/			txUserDataArray[4] = SUCCESS;	// command status
/*Slave*/			if(rxUserDataCopy[5] < 30)	// request for 16-bit parm
/*Slave*/			{
/*Slave*/				*(uppParms16[rxUserDataCopy[5]]) = rxUserDataCopy[6];
/*Slave*/			}
/*Slave*/			else		// request for 32-bit parm
/*Slave*/			{
/*Slave*/				*((u16*)(uppParms32[rxUserDataCopy[5]-30])) = rxUserDataCopy[6];		// high word
/*Slave*/				*((u16*)(uppParms32[rxUserDataCopy[5]-30]) + 1) = rxUserDataCopy[7];	// low word
/*Slave*/			}
/*Slave*/			uTransmitPacketReady = 1;
/*Slave*/			uPlcState = PLC_RECEIVE_COMMAND;
/*Slave*/			break;
/*Slave*/
/*Slave*/		case SC_GET_STATUS:
/*Slave*/			//==============================================================================
/*Slave*/			// Command:  Get status.
/*Slave*/			// rxUserDataCopy[0] = dest addr - high
/*Slave*/			// rxUserDataCopy[1] = dest addr - low
/*Slave*/			// rxUserDataCopy[2] = source addr - high
/*Slave*/			// rxUserDataCopy[3] = source addr - low
/*Slave*/			// rxUserDataCopy[4] = command number
/*Slave*/			//
/*Slave*/			// Return:
/*Slave*/			// txUserDataArray[0] = Master addr - high
/*Slave*/			// txUserDataArray[1] = Master addr - low
/*Slave*/			// txUserDataArray[2] = MyAddr - high
/*Slave*/			// txUserDataArray[3] = MyAddr - low
/*Slave*/			// txUserDataArray[4] = Status / Error
/*Slave*/			//
/*Slave*/			// return:
/*Slave*/			// 		If the requested configuration parameter is valid, SUCCESS is returned
/*Slave*/			//		for an acknowledge followed by the status array values.
/*Slave*/			//==============================================================================
/*Slave*/			// Fill User Data buffer with zeroes
/*Slave*/			memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));	
/*Slave*/
/*Slave*/			txUserDataArray[0] = rxUserDataCopy[2];	// Master address
/*Slave*/			txUserDataArray[1] = rxUserDataCopy[3];	// Master address
/*Slave*/			*(u32*)&txUserDataArray[2] = ulMyAddr;
/*Slave*/			txUserDataArray[4] = SUCCESS;	// command status
/*Slave*/
/*Slave*/			// return parms 5-34
/*Slave*/			for(i=0; i<30; i++)		// 16-bit parms
/*Slave*/			{
/*Slave*/				txUserDataArray[5+i] = *(uppParms16[i]);
/*Slave*/			}
/*Slave*/			// return parms 35-54
/*Slave*/			for(i=0; i<10; i++)		// 32-bit parms
/*Slave*/			{
/*Slave*/				txUserDataArray[35+i*2] = *((u16*)(uppParms32[i]));		// high word
/*Slave*/				txUserDataArray[35+i*2+1] = *((u16*)(uppParms32[i]) + 1);	// low word
/*Slave*/			}
/*Slave*/
/*Slave*/			uTransmitPacketReady = 1;
/*Slave*/			uPlcState = PLC_RECEIVE_COMMAND;
/*Slave*/			break;
/*Slave*/
/*Slave*/	 	case SC_BER:
/*Slave*/	 		//==============================================================================
/*Slave*/	 		// BER test.
/*Slave*/			// rxUserDataCopy[0] = dest addr - high
/*Slave*/			// rxUserDataCopy[1] = dest addr - low
/*Slave*/			// rxUserDataCopy[2] = source addr - high
/*Slave*/			// rxUserDataCopy[3] = source addr - low
/*Slave*/			// rxUserDataCopy[4] = command number
/*Slave*/			// rxUserDataCopy[5] = BER count (high word).
/*Slave*/			// rxUserDataCopy[6] = BER count (low word).
/*Slave*/			// 
/*Slave*/			// When a Bit Error Rate (BER) packet is received, the slave keeps track of
/*Slave*/			// received packets, missed packets and parity errors.
/*Slave*/			// 
/*Slave*/			// Additionally if the BER count (an incrementing sequence from the Master)
/*Slave*/			// is 0, the slave resets all of its counters.
/*Slave*/	 		//==============================================================================
/*Slave*/			// Get the passed sequence number.
/*Slave*/	 		ulBerSequence = (u32) rxUserDataCopy[5]*65536L + (u32) rxUserDataCopy[6];
/*Slave*/	 		uBerTestInProgress = 1;
/*Slave*/				
/*Slave*/			// If the sequence number = 0, reset the counters.
/*Slave*/	 		if(ulBerSequence == 0L)
/*Slave*/			{
/*Slave*/				uBerMissedPackets = 0;
/*Slave*/				uBerErrorCounter = 0;
/*Slave*/				ulBerCounter = 0;
/*Slave*/			}
/*Slave*/			// The received 'ulBerSequence' should be equal to our local counter,
/*Slave*/			// 'ulBerCounter' plus the number of packets we missed due to CRC 
/*Slave*/			// errors, 'uBerErrorCounter', plus the number of packets we completely
/*Slave*/			// missed, 'uBerMissedPackets.
/*Slave*/			// If the received sequence number is greater than the expected sum,
/*Slave*/			// we missed the last packet, so increment the missed counter.
/*Slave*/			if( ulBerCounter++ + uBerMissedPackets + uBerErrorCounter < ulBerSequence)
/*Slave*/			{
/*Slave*/				uBerMissedPackets++;
/*Slave*/			}
/*Slave*/			// If the calculated sum is greater than the sequence number, somehow
/*Slave*/			// we must have incremented the CRC counter due to something other than
/*Slave*/			// an expected BER packet - most likely an error receiving the status
/*Slave*/			// request packet.  (Possibly traffic from another platform??)
/*Slave*/			// That CRC error shouldn't be included in the bit error rate, so take
/*Slave*/			// it back out (it's not included in the total, so don't include it in
/*Slave*/			// the errors).
/*Slave*/			// -1 becuse we incremented ulBerCounter in the previous conditional.
/*Slave*/			else if( (ulBerCounter + uBerMissedPackets + uBerErrorCounter -1) > ulBerSequence)
/*Slave*/			{
/*Slave*/				uBerErrorCounter--;
/*Slave*/			}
/*Slave*/			
/*Slave*/			uCommandBlock = 0;		// Re-enable packet reception.
/*Slave*/									// This gets re-enabled in SendPLC() for most other
/*Slave*/									// commands.
/*Slave*/	 		break;
/*Slave*/	 
/*Slave*/	 	case SC_ABORT_BER:
/*Slave*/	 		//==============================================================================
/*Slave*/	 		// Abort BER test.
/*Slave*/			// rxUserDataCopy[0] = dest addr - high
/*Slave*/			// rxUserDataCopy[1] = dest addr - low
/*Slave*/			// rxUserDataCopy[2] = source addr - high
/*Slave*/			// rxUserDataCopy[3] = source addr - low
/*Slave*/			// rxUserDataCopy[4] = command number
/*Slave*/	 		//==============================================================================
/*Slave*/	 		uBerTestInProgress = 0;
/*Slave*/			uCommandBlock = 0;		// Re-enable packet reception.
/*Slave*/									// This gets re-enabled in SendPLC() for most other
/*Slave*/									// commands.
/*Slave*/	 		break;
/*Slave*/
/*Slave*/		case SC_AUTOPOLL:
/*Slave*/			//==============================================================================
/*Slave*/			// Poll for slave - Someone was looking for us.
/*Slave*/			// rxUserDataCopy[0] = dest addr - high
/*Slave*/			// rxUserDataCopy[1] = dest addr - low
/*Slave*/			// rxUserDataCopy[2] = source addr - high
/*Slave*/			// rxUserDataCopy[3] = source addr - low
/*Slave*/			// rxUserDataCopy[4] = command number
/*Slave*/			//
/*Slave*/			// Return:
/*Slave*/			// txUserDataArray[0] = Master addr - high
/*Slave*/			// txUserDataArray[1] = Master addr - low

⌨️ 快捷键说明

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