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

📄 conn.c

📁 DEVICENET规范及例程 很详细
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 *
 * Microchip DeviceNet Stack (Connection Object Source)
 *
 *****************************************************************************
 * FileName:        conn.c
 * Dependencies:    
 * Processor:       PIC18F with CAN
 * Compiler:       	C18 02.20.00 or higher
 * Linker:          MPLINK 03.40.00 or higher
 * Company:         Microchip Technology Incorporated
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the "Company") is intended and supplied to you, the Company's
 * customer, for use solely and exclusively with products manufactured
 * by the Company. 
 *
 * The software is owned by the Company and/or its supplier, and is 
 * protected under applicable copyright laws. All rights are reserved. 
 * Any use in violation of the foregoing restrictions may subject the 
 * user to criminal sanctions under applicable laws, as well as to 
 * civil liability for the breach of the terms and conditions of this 
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * This file contains the Connection Object managing functions. Based on a 
 * variety of conditions, events are generated for specific instances. Refer
 * to section 5-4 of Volume 1 of the DeviceNet specification.
 * 
 *
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Ross Fosler			04/28/03	...	
 * 
 *****************************************************************************/




#include	"dnet.def"			// Global definitions file
#include 	"typedefs.h"

#include	"conn.h"			// Internal definitions

#include	"services.h"		// Service codes
#include	"errors.h"			// Error codes
#include	"class.h"			// Class codes

#include	"route.h"			// Global symbols defined by the Router Obj
#include	"dnet.h"			// Global symbols defined by the DeviceNet Obj

#include	"CAN.h"				// CAN driver



#define	_CONN_REVISION		0x01
#define	_ATTRIB_REVISION	1


/*********************************************************************
 * Flag registers for read and write control
 ********************************************************************/		
#if USE_ACCESS == TRUE
#pragma	udata access	_A_CONN_REGISTERS
#endif
NEAR FLAGS _rxFlag;			// Receive control and indication
NEAR FLAGS _txFlag;			// Transmit control and indication
NEAR FLAGS _txFinFlags;		// Transmit finish control and indication
NEAR FLAGS _existentFlags;	// Indicates the existance
NEAR FLAGS _establishFlags;	// Indicates established connections
NEAR unsigned char _deSer;	// Control register to de-serialize transmissions





#pragma	udata
/*********************************************************************
 * Function:        void _ConnInit(void)
 *
 * PreCondition:    The CAN (or other I/O) driver must be initialized
 *					prior to calling this function.
 *
 * Input:       	none		
 *                  
 * Output:      	none	
 *
 * Side Effects:    none
 *
 * Overview:        Initialize the connection object.
 *
 * Note:            During this process the Unconnected Explicit 
 *					Messaging and Duplicate ID Messaging connections
 *					are created.
 ********************************************************************/
void _ConnInit(void)
{
	// Initialize all global connection related variables
	_txFlag.byte = 0;
	_rxFlag.byte = 0;
	_txFinFlags.byte = 0;
	_existentFlags.byte = 0;
	_establishFlags.byte = 0;
	_deSer = 0;

	// Automatically create the duplicate ID and 
	// unconnected explicit messaging connections
	_Conn6Create();
	_Conn7Create();

	// Initialize all connections to there non-existent state
	uConn1.attrib.state == _STATE_NON_EXISTENT;

	#if SUPPORT_POLLED || SUPPORT_COS_BOTH_DIR
	uConn2.attrib.state == _STATE_NON_EXISTENT;
	#endif

	#if SUPPORT_BIT_STROBED
	uConn3.attrib.state == _STATE_NON_EXISTENT;
	#endif

	#if SUPPORT_COS || SUPPORT_CYCLIC 
	uConn4.attrib.state == _STATE_NON_EXISTENT;
	#endif

	#if SUPPORT_MULTICAST_POLL
	uConn5.attrib.state == _STATE_NON_EXISTENT;
	#endif
}


/*********************************************************************
 * Function:        unsigned char _ConnCreate(enum T_CONTYPE connType)
 *
 * PreCondition:    
 *
 * Input:       		
 *                  
 * Output:      	
 *
 * Side Effects:    
 *
 * Overview:        Returns a handle to the connection 
 *
 * Note:            Although a handle is returned, it is not required
 *					to be stored since this is a static model, where
 *					the instance numbers are fixed.
 ********************************************************************/
//unsigned char _ConnCreate(CONTYPE connType)
//{
//	switch (connType)
//	{
//		case CONTYPE_EXPLICIT: 		return(_Conn1Create()); // Explicit messaging connection
//		
//		#if SUPPORT_POLLED || SUPPORT_COS_BOTH_DIR
//		case CONTYPE_POLL: 			return(_Conn2Create());	// Poll I/O connection
//		#endif
//
//		#if SUPPORT_BIT_STROBED
//		case CONTYPE_BIT_STROBE: 	return(_Conn3Create());	// Bit-Strobe I/O connection
//		#endif
//
//		#if SUPPORT_COS || SUPPORT_CYCLIC
//		case CONTYPE_COS_CYCLIC: 	return(_Conn4Create());	// COS/Cyclic I/O connection
//		#endif
//
//		#if SUPPORT_MULTICAST_POLL
//		case CONTYPE_MULTICAST: 	return(_Conn5Create());	// Multicast Poll connection
//		#endif
//
//		case CONTYPE_UNC_EXPLICIT: 	return(_Conn6Create());	// Unconnected Explicit messaging connection
//		case CONTYPE_DUPLICATE_ID: 	return(_Conn7Create());	// Duplicate ID check message
//		default: return (0);
//	}
//}




/*********************************************************************
 * Function:        void _ConnClose(void)
 *
 * PreCondition:    
 *
 * Input:       		
 *                  
 * Output:      	
 *
 * Side Effects:    
 *
 * Overview:        Closes the specified connection 
 *
 * Note:            None
 ********************************************************************/
//void _ConnClose(unsigned char hInstance)
//{
//	switch (hInstance)
//	{
//		case 1:	_Conn1Close(); break;
//		
//		#if SUPPORT_POLLED || SUPPORT_COS_BOTH_DIR
//		case 2:	_Conn2Close(); break;
//		#endif
//
//		#if SUPPORT_BIT_STROBED
//		case 3:	_Conn3Close(); break;
//		#endif
//
//		#if SUPPORT_COS || SUPPORT_CYCLIC
//		case 4: _Conn4Close(); break;
//		#endif
//
//		#if SUPPORT_MULTICAST_POLL
//		case 5: _Conn5Close(); break;
//		#endif
//
//		case 6:	_Conn6Close(); break;
//		case 7:	_Conn7Close(); break;
//	}
//}


/*********************************************************************
 * Function:        unsigned char _ConnReadRdy(unsigned char hInstance)
 *
 * PreCondition:    
 *
 * Input:       	unsigned char handle to the instance
 *                  
 * Output:      	unsigned char 
 *
 * Side Effects:    none
 *
 * Overview:        Returns true if there is data in the read buffer 
 *					and the connection is established.
 *
 * Note:            None
 ********************************************************************/
unsigned char _ConnReadRdy(unsigned char hInstance)
{
	switch (hInstance)
	{
		case 1:	return (_rxFlag.bits.expl && _establishFlags.bits.expl);
		
		#if SUPPORT_POLLED || SUPPORT_COS_BOTH_DIR
		case 2:	return (_rxFlag.bits.poll && _establishFlags.bits.poll);
		#endif

		#if SUPPORT_BIT_STROBED
		case 3:	return (_rxFlag.bits.strobe && _establishFlags.bits.strobe);
		#endif

		#if SUPPORT_COS || SUPPORT_CYCLIC
		case 4: return (_rxFlag.bits.cos && _establishFlags.bits.cos);
		#endif

		#if SUPPORT_MULTICAST_POLL
		case 5: return (_rxFlag.bits.multi && _establishFlags.bits.multi);
		#endif

		case 6:	return (_rxFlag.bits.uexpl);
		case 7:	return (_rxFlag.bits.dupid);
		default: return (0);
	}
}



/*********************************************************************
 * Function:        unsigned char _ConnWriteRdy(unsigned char hInstance)
 *
 * PreCondition:    
 *
 * Input:       	unsigned char handle to instance
 *                  
 * Output:      	unsigned char
 *
 * Side Effects:    
 *
 * Overview:        Returns true if the buffer is available. 
 *
 * Note:            The application must call this function prior to 
 *					loading the buffer. Otherwise any message already
 *					queued to send will be corrupted.
 ********************************************************************/
unsigned char _ConnWriteRdy(unsigned char hInstance)	
{
	switch (hInstance)
	{
		case 1:	return (!_txFlag.bits.expl && _establishFlags.bits.expl);
	
		#if SUPPORT_POLLED || SUPPORT_COS_BOTH_DIR
		case 2:	return (!_txFlag.bits.poll && _establishFlags.bits.poll);
		#endif

		#if SUPPORT_BIT_STROBED
		case 3:	return (!_txFlag.bits.strobe && _establishFlags.bits.strobe);
		#endif

		#if SUPPORT_COS || SUPPORT_CYCLIC
		case 4: return (!_txFlag.bits.cos && _establishFlags.bits.cos);
		#endif

		#if SUPPORT_MULTICAST_POLL
		case 5: return (!_txFlag.bits.multi && _establishFlags.bits.multi);
		#endif

		case 6:	return (!_txFlag.bits.uexpl);
		case 7:	return (!_txFlag.bits.dupid);
		default: return (0);
	}	
}


/*********************************************************************
 * Function:        unsigned char _ConnWriteFin(unsigned char hInstance)
 *
 * PreCondition:    
 *
 * Input:       	unsigned char handle to instance	
 *                  
 * Output:      	unsigned char
 *
 * Side Effects:    
 *
 * Overview:        Returns true if the buffer has placed data on the bus. 
 *
 * Note:            None
 ********************************************************************/
unsigned char _ConnWriteFin(unsigned char hInstance)	
{
	unsigned char temp;
	
	switch (hInstance)
	{
		case 1:	
			temp = _txFinFlags.bits.expl;
			_txFinFlags.bits.expl = 0;
			return (temp);

		#if SUPPORT_POLLED || SUPPORT_COS_BOTH_DIR
		case 2:	
			temp = _txFinFlags.bits.poll;
			_txFinFlags.bits.poll = 0;
			return (temp);
		#endif

		#if SUPPORT_BIT_STROBED
		case 3:	
			temp = _txFinFlags.bits.strobe;
			_txFinFlags.bits.strobe = 0;
			return (temp);
		#endif

		#if SUPPORT_COS || SUPPORT_CYCLIC
		case 4: 
			temp = _txFinFlags.bits.cos;
			_txFinFlags.bits.cos = 0;
			return (temp);
		#endif

		#if SUPPORT_MULTICAST_POLL
		case 5: 
			temp = _txFinFlags.bits.multi;
			_txFinFlags.bits.multi = 0;
			return (temp);
		#endif

		case 6:	
			temp = _txFinFlags.bits.uexpl;
			_txFinFlags.bits.uexpl = 0;
			return (temp);
			
		case 7:	
			temp = _txFinFlags.bits.dupid;
			_txFinFlags.bits.dupid = 0;
			return (temp);
			
		default: return (0);
	}	
}

/*********************************************************************
 * Function:        void _ConnRead(unsigned char hInstance)
 *
 * PreCondition:  	Function _ConnReadRdy() should be executed prior
 *					to calling this function to determine if data has
 *					been received.  
 *
 * Input:       	unsigned char handle to instance		
 *                  
 * Output:      	none	
 *
 * Side Effects:    
 *
 * Overview:        Indicates to the Connection Object that the data 
 *					has been read and the buffer is free to take 
 *					another message.
 *
 * Note:            The function is only executed successfully if
 *					the connection is established.
 ********************************************************************/
void _ConnRead(unsigned char hInstance)
{
	switch (hInstance)
	{
		case 1:	if (_establishFlags.bits.expl) _rxFlag.bits.expl = 0; break;
 
		#if SUPPORT_POLLED || SUPPORT_COS_BOTH_DIR
		case 2:	if (_establishFlags.bits.poll) _rxFlag.bits.poll = 0; break;
		#endif

		#if SUPPORT_BIT_STROBED
		case 3:	if (_establishFlags.bits.strobe) _rxFlag.bits.strobe = 0; break;
		#endif

		#if SUPPORT_COS || SUPPORT_CYCLIC
		case 4: if (_establishFlags.bits.cos) _rxFlag.bits.cos = 0; break;
		#endif

		#if SUPPORT_MULTICAST_POLL
		case 5: if (_establishFlags.bits.multi) _rxFlag.bits.multi = 0; break;
		#endif

		case 6:	_rxFlag.bits.uexpl = 0; break;
		case 7:	_rxFlag.bits.dupid = 0; break;
	}
}	



/*********************************************************************
 * Function:        void _ConnWrite(unsigned char hInstance)
 *
 * PreCondition:	Execute _ConnWriteRdy() to determine if the buffer
 *					is available before executing this function. Also
 *					load the buffer prior to sending.     
 *
 * Input:       	unsigned char handle to instance	
 *                  
 * Output:      	none
 *
 * Side Effects:    
 *
 * Overview:        Indicates to the Connection object instance that 
 * 					all data has been loaded and the connection
 *					is free to transmit. 
 *
 * Note:            The queue bit for the specified connection is set.
 *					
 ********************************************************************/
void _ConnWrite(unsigned char hInstance)	
{
	switch (hInstance)
	{
		case 1:	if (_establishFlags.bits.expl) _txFlag.bits.expl = 1; break;	

		#if SUPPORT_POLLED || SUPPORT_COS_BOTH_DIR
		case 2:	if (_establishFlags.bits.poll) _txFlag.bits.poll = 1; break;
		#endif

		#if SUPPORT_BIT_STROBED
		case 3:	if (_establishFlags.bits.strobe) _txFlag.bits.strobe = 1; break;	
		#endif

		#if SUPPORT_COS || SUPPORT_CYCLIC
		case 4: if (_establishFlags.bits.cos) _txFlag.bits.cos = 1; break;
		#endif

		#if SUPPORT_MULTICAST_POLL
		case 5: if (_establishFlags.bits.multi) _txFlag.bits.multi = 1; break;		
		#endif

		case 6:	_txFlag.bits.uexpl = 1; break;
		case 7:	_txFlag.bits.dupid = 1; break;
	}
}


/*********************************************************************

⌨️ 快捷键说明

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