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

📄 dsp_modem.c

📁 DSP 5409 plc应用程序,调试通过,是用在电力线通讯上的演示程序.
💻 C
📖 第 1 页 / 共 5 页
字号:
		memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));	// Fill User Data buffer with zeroes
		*(u32*)&txUserDataArray[0] = ulClosestSlaveAddr; // Slave addr
		*(u32*)&txUserDataArray[2] = ulMyAddr;			// Master addr
		txUserDataArray[4] = SC_AUTOPOLL;		// Polling command number.
		uTransmitPacketReady = 1;				// Signal packet is ready to go.
		uPlcState = PLC_RECEIVE_POLLING;		// Set state to receive polling response.
		ulAutoPollCounter = 0L;					// Reset counter for periodic polling.
		ulAutoPollResponseTimeoutCounter = 0L;	// Used to detect missing response.
	}
}


//==========================================================================================
// Function:		CheckTimeout()		
//
// Description:		See if we timed out while waiting for data.
//
// Revision History:
//==========================================================================================
void CheckTimeouts(void)
{
	u16		i;				// loop index

	// MASTER Check command response timeout.  Slave never responded to command.
	if( (uPlcState == PLC_RECEIVE_RESPONSE)
		&& (ulPlcResponseTimeoutCounter >= ((T0_INTS_PER_SEC>>2)*3)) ) 	// 3/4 second. 
	{																// Less than matlab timeout.
		WriteUARTValue(UART_TARGET_HOST, ERR_SLAVE_RESPONSE_TIMEOUT);
		uPlcState = PLC_IGNORE;
		uUartState = UART_RECEIVE_COMMAND;
		uUartCommandIndex = 0;
		ulPlcResponseTimeoutCounter = 0L;
		ulAutoPollCounter = 0L;
		uAutoPollPause=0;		// Release AutoPoll.
	}

	// MASTER Check autopoll response timeout.  Slave never responded to autopoll command.
	else if( (uPlcState == PLC_RECEIVE_POLLING)
	    && (ulAutoPollResponseTimeoutCounter >= (PACKET_TIME*4)) ) 			// Shouldn't be much more than 2 packet
	{														// times as there's not much processing
		uPlcState = PLC_IGNORE;								// required.
		uUartState = UART_RECEIVE_COMMAND;
		uUartCommandIndex = 0;
		ulLastRxCounter = 0L;
		ulAutoPollCounter = 0L;
		ulAutoPollResponseTimeoutCounter = 0L;
	}

	// MASTER Check UART command timeout.  Started receiving command characters from the host
	// but didn't receive an entire packet.
	else if ( (uUartState == UART_RECEIVE_COMMAND) 
		   && (ulUartCounter >= (T0_INTS_PER_SEC >> 2)) 	// 1/4 second. 16 bytes @ 9600 baud = ~0.02 seconds
		   && (uUartCommandIndex > 0) )
	{
		if(uFindSlaves == 1) // if not in find slaves mode
		{
			uPlcState = PLC_IGNORE;
		}
		uUartState = UART_RECEIVE_COMMAND;
		uUartCommandIndex = 0;
		ulUartCounter = 0L;
		ulAutoPollCounter = 0L;
		uAutoPollPause = 0;		// Release AutoPoll.
	}

	// MASTER Check UART data timeout.  Started receiving characters that are meant to be part of
	// a packet for the slave, but didn't receive an entire packet.
	else if ( (uUartState == UART_RECEIVE_DATA) 
		   && (ulUartCounter >= (T0_INTS_PER_SEC >> 1)) 	// 1/2 seconds. 128 bytes @ 9600 baud = ~0.13 seconds
		   && (uUartDataIndex > 0) )
	{
		WriteUARTValue(UART_TARGET_HOST, ERR_RECV_DATA_TIMEOUT);
		uPlcState = PLC_IGNORE;
		uUartState = UART_RECEIVE_COMMAND;
		uUartDataIndex = 0;
		ulUartCounter = 0L;
		ulAutoPollCounter = 0L;
		uAutoPollPause=0;		// Release AutoPoll.
	}

	// MASTER Check UART response timeout.  Should be receiving characters from the emeter, but
	// never received an end of string char signalling the end of response.
	else if( (uBoard == BOARD_MASTER)
		  && (uUartState == UART_RECEIVE_RESPONSE)
		  && (ulUartCounter >= (T0_INTS_PER_SEC >> 1)) )	 	// Give the emeter 1/2 second to respond.
	{														// Should be less than matlab timeout.
		WriteUARTValue(UART_TARGET_HOST, ERR_MASTER_EMETER_TIMEOUT);
		uPlcState = PLC_IGNORE;
		uUartState = UART_RECEIVE_COMMAND;
		uUartCommandIndex = 0;
		ulUartCounter = 0L;
		ulAutoPollCounter = 0L;
		uAutoPollPause=0;		// Release AutoPoll.
	}

	// MASTER check flash write timeout.  Sent command to write flash.  uFlashWriteComplete
	// never got set.
	else if( (uBoard == BOARD_MASTER)
	      && (uFlashWriteStatus == FLASH_STATUS_WRITING)
	      && (ulFlashWriteCounter >= (2L*T0_INTS_PER_SEC)) )
	{
		uFlashWriteStatus = FLASH_STATUS_TIMEOUT;	// Timeout error occurred.
		ulFlashWriteCounter = 0L;					// Reset timeout counter
	}

	// SLAVE Check UART response timeout.  Should be receiving characters from the emeter, but
	// never received an end of string char signalling the end of response.
	else if( (uBoard == BOARD_SLAVE)
		  && (uUartState == UART_RECEIVE_RESPONSE)
		  && (ulUartCounter >= (T0_INTS_PER_SEC >> 1)) ) 	 // Give the emeter 2 seconds to respond.
	{
		txUserDataArray[0] = ERR_SLAVE_EMETER_TIMEOUT;
		for(i=1; i<126/2; i++)
		{
			txUserDataArray[i] = 0;
		}
		uTransmitPacketReady = 1;

		uPlcState = PLC_RECEIVE_COMMAND;
		uUartState = UART_IDLE;
		uUartCommandIndex = 0;
		ulUartCounter = 0L;
	}

	return;
}


//==========================================================================================
// Function:		CheckForClosestSlave() 		
//
// Description:		This function is used to find the SLAVE with the closest address to the
//					MASTER's.  It is called from the main loop every pass until a slave is
//					found.  It calculated the next address to try and issues the polling
//					command to that address.  If a response is received, the proper
//					variables and flags will be updated in ReceivePLC(), and this function
//					will no longer be called.
//
// Revision History:
//==========================================================================================
void CheckForClosestSlave(void)
{
	static u16		uFirstPass = 1;
	static s32 		slNextAddr;
	static u32		i = 0L;
	static s16		sSign=-1;
	static s32		slMaxSlaveAddr;
	static s32		slMinSlaveAddr;

	if (uFirstPass == 1)
	{
		uFirstPass = 0;
		slNextAddr = (s32)ulMyAddr;
		ulAutoPollCounter = (PACKET_TIME*3);		// Make sure we go right into next conditional.
		slMinSlaveAddr = ulMyAddr - MAX_SLAVE_ADDR_RANGE;

		// Check for valid boundaries [0000:0003 - 7FFF:FFFF]

		// Not going to test addresses 0-2.
		//   0 used for closest slave
		//   1 used for broadcast to slaves
		//   2 used for broadcast to masters
		if(slMinSlaveAddr < 3)
		{
			slMinSlaveAddr = 3;
		}
		slMaxSlaveAddr = ulMyAddr + MAX_SLAVE_ADDR_RANGE;
		if(slMaxSlaveAddr < 0)		// Address rolled with addition.
		{
			slMaxSlaveAddr = 0x7fffffffL;
		}
	}
	
	if( (uPlcState == PLC_FIND_CLOSEST_SLAVE)
	 && (uTransmitPacketReady == 0)				// Make sure the last one went out.
	 && (ulAutoPollCounter>=(PACKET_TIME*3)) )	// Wait 3 packet times for response.
	{
		// Not going to test addresses 0-2, or ulMyAddr.
		//   0 used for closest slave
		//   1 used for broadcast to slaves
		//   2 used for broadcast to masters
		if (++i > (MAX_SLAVE_ADDR_RANGE*2))			// If we hit this, we've gone through all
		{										// the addresses.  Might as well keep going
			i = 1;								// though, since there's nothing else to do...
			sSign = -1;
			slNextAddr = (s32)ulMyAddr;
		}
		sSign *= -1;

		slNextAddr += i*sSign;

		// Don't issue polling command if test address is out of range.
		if((slNextAddr >= slMinSlaveAddr) && (slNextAddr <= slMaxSlaveAddr))
		{
			// Issue polling command
			memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));	// Fill User Data buffer with zeroes
			*(u32*)&txUserDataArray[0] = slNextAddr;	// Test slave addr
			*(u32*)&txUserDataArray[2] = ulMyAddr;		// Master addr
			txUserDataArray[4] = 6;						// Polling command number.
			uTransmitPacketReady = 1;					// Signal packet is ready to go.
			ulAutoPollCounter = 0L;						// Reset counter for use as timeout.
		}
	}

	return;
}


//==========================================================================================
// Function:		FindSlaves() 		
//
// Description:		This function is used to find to SLAVES.  It is initiated by a host
//					command which sets the flag uFindSlaves to 1 which allows this routine
//					to be called and sets the boundaries for the search using two global
//					variabes ulFindSlaveAddr and ulFindSlaveAddrMax.
//
//					Three things will cause this finding of slaves to stop:
//						1) Finding a slave - the receive PLC function will then respond
//							with the address of the found slave.
//						2) Passing ulFindSlaveAddrMax without finding a slave.
//						3) Receiving an AbortFindSlaves command.
//
// Revision History:
//==========================================================================================
void FindSlaves(void)
{
	if( (uPlcState == PLC_FIND_ALL_SLAVES)
	 && (uTransmitPacketReady == 0)				// Make sure the last one went out.
	 && (ulAutoPollCounter>=(PACKET_TIME*3)) )	// Wait 3 packet times for response.
	{
		if (ulFindSlaveAddr > ulFindSlaveAddrMax)
		{
			uFindSlaves = 0;
			uPlcState = PLC_IGNORE;
			ulAutoPollCounter = 0L;
			uAutoPollPause = 0;
			WriteUART(UART_TARGET_HOST, 2, (u16*) &ulFindSlaveAddr);
		}
		else
		{
			// Issue polling command
			memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));	// Fill User Data buffer with zeroes
			*(u32*)&txUserDataArray[0] = ulFindSlaveAddr++;	// Slave addr - increment here for next pass.
			*(u32*)&txUserDataArray[2] = ulMyAddr;		// Master addr
			txUserDataArray[4] = 6;						// Polling command number.
			uTransmitPacketReady = 1;					// Signal packet is ready to go.
			ulAutoPollCounter = 0L;						// Reset counter for use as timeout.
		}
	}

	return;
}


//==========================================================================================
// Function:		CheckFlash()		
//
// Description:		Handles flash writing.
//
//					The variable uFlashWriteStatus is the main indicator that flash data is
//						ready.  When a complete line has been received, ReceiveUART()
//						sets uFlashWriteStatus to FLASH_STATUS_WRITING.
//					When flash writing is complete this routine will change uFlashWriteStatus 
//						to FLASH_STATUS_COMPLETE, or FLASH_STATUS_XXX if an error occurred.
//
//					The global variable uFlashTarget determines where the data is 
//						intended to go.  ProcessMasterCommand() sets it to FLASH_TARGET_XXX
//						based on the command received from the host.
//							XXX = PROG  Flash data intended for DSP program space
//							XXX = DATA	Flash data intended for DSP data space
//							XXX = MSP Flash data intended for the MSP430
//
//					The data to be written is located at uUartDataArray and in a packed
//						byte format (two ascii bytes per word) has the Intel form:
//							10204E0029D00229D6022A1402287D02284302270B
//							^ ^   ^ ^                               ^
//							| |   | |                               |
//							| |   | |       checksum----------------+
//							| |   | +-------data bytes
//							| |   +---------record type (00=data, 01=end of file) 
//							| +-------------address for this line of data
//							+---------------number of bytes of data in this line
//
// Revision History:
//==========================================================================================
void CheckFlash(void)
{
	if( uBoard == BOARD_MASTER )                       // if this board is a master
    {
		if( uFlashWriteStatus == FLASH_STATUS_WRITING )
		{
	    	if( !GetTargetStatus( uFlashTarget ) )     // if intended target is open for access
	        {                                          // get state machine rolling on this target
	        	uFlashWriteStatus = ParseRecord( uFlashTarget );
	        }
		}

		if( uFlashWriteStatus == FLASH_STATUS_ERASING )
		{
	    	if( !GetTargetStatus( uFlashTarget ) )     // if intended target is open for access
	        {                                          // get state machine rolling on this target
	        	uFlashWriteStatus = FlashErase( uFlashTarget );
	        }
		}
    }

    FlashStateMachine( );   // execute current state of flash management state machine
    
	return;
}


//==========================================================================================
// Function:		InitEverything()
//
// Description: 	This function sets up DSP memory config registers, program variables,
// 					MCBSP ports and DMA engines, and periodic timer.
// 					Enable interrupts.
//
// Revision History:
//==========================================================================================
void InitEverything(void)

⌨️ 快捷键说明

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