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

📄 dnet.c

📁 关于DeviceNet协议的仅限组2的从站代码
💻 C
📖 第 1 页 / 共 2 页
字号:

/*****************************************************************************
 *
 *              Microchip DeviceNet Stack (DeviceNet Object)
 *
 *****************************************************************************
 * FileName:        dnet.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 DeviceNet object described in section 5-5 of 
 * volume 1 of the DeviceNet specification.
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Ross Fosler			04/03/03	...	
 * 
 *****************************************************************************/

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

#include	"dnet.h"			// Internal prototypes
#include	"usrdnet.h"			// External prototypes

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

#include	"conn.h"
#include	"route.h"			// Global symbols defined by the router



/*********************************************************************
 * DeviceNet internal definitions
 ********************************************************************/
#define		_DNET_REVISION				0x02

#define		_ATTRIB_REVISION			0x01

#define		_ATTRIB_MAC_ID				0x01
#define		_ATTRIB_BAUD_RATE			0x02
#define		_ATTRIB_BOI					0x03
#define		_ATTRIB_BO_COUNT			0x04
#define		_ATTRIB_ALLOC_INFO			0x05
#define		_ATTRIB_MAC_SW_CH			0x06
#define		_ATTRIB_BAUD_SW_CH			0x07
#define		_ATTRIB_MAC_SW_VAL			0x08
#define		_ATTRIB_BAUD_SW_VAL			0x09

#define		_DNET_ERR_CONFLICT			0x01
#define		_DNET_ERR_INVALID_PARAM		0x02
#define		_DNET_ERR_INVALID_SERVICE	0x03
#define		_DNET_ERR_RSRC_NOT_AVAIL	0x04


/*********************************************************************
 * DeviceNet global parameters
 ********************************************************************/
DNET uDNet;




/*********************************************************************
 * Function:        unsigned char DNetExplMsgHandler(void)
 *
 * PreCondition:    The path (aService, aClassID, aInstanceID) must
 *					be loaded prior to using this function. If service
 *					indicates any type of IO then the buffer pointers
 *					must be initialized.
 *
 * Input:       	aHeader, aClassID, aInstanceID, aService
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *					aOutBufLen, aInBufLen		
 *                  
 * Output:      	aHeader, aService	
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *				
 *			
 *
 * Side Effects:    
 *
 * Overview:        DeviceNet object explicit messaging handler. This
 *					fucntion is called by the Router object. It decodes
 *					the instance and service and performs the requested
 *					function.  
 *
 * Note:            None
 ********************************************************************/
unsigned char _DNetExplMsgHandler(void)
{
	switch (mRouteGetInstanceID())
	{
		// The DeviceNet object, class specific 
		case 0:
			switch(mRouteGetServiceID())
			{
				case SRVS_GET_ATTRIB_SINGLE:
					return (_DNetInst0GetAttrib());
				default:
					mRoutePutError(ERR_SERVICE_NOT_SUPPORTED);
					break;
			}
			break;
			
		// The DeviceNet object, instance 1 only
		case 1:
			switch(mRouteGetServiceID())
			{
				case SRVS_GET_ATTRIB_SINGLE:
					return (_DNetInst1GetAttrib());
				case SRVS_SET_ATTRIB_SINGLE:
					return (_DNetInst1SetAttrib());
				case SRVS_ALLOCATE_CONN:
					return (_DNetAllocateConnection());
				case SRVS_RELEASE_CONN:
					return (_DNetReleaseConnection());
				default:
					mRoutePutError(ERR_SERVICE_NOT_SUPPORTED);
					break;
			}
			break;
			
		// Handle all other invalid instances		
		default:	
			mRoutePutError(ERR_OBJECT_DOES_NOT_EXIST);			
			break;
	}
		
	return (1);
}			  



/*********************************************************************
 * Function:        unsigned char DNetInst0GetAttrib(void)
 *
 * PreCondition:    The path (aService, aClassID, aInstanceID) must
 *					be loaded prior to using this function. If service
 *					indicates any type of IO then the buffer pointers
 *					must be initialized.
 *
 * Input:       	aHeader, aClassID, aInstanceID, aService
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *					aOutBufLen, aInBufLen		
 *                  
 * Output:      	aHeader, aService	
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *				
 *			
 *
 * Side Effects:    
 *
 * Overview:        Get attribute service for instance 0.  
 *
 * Note:            None
 ********************************************************************/
unsigned char _DNetInst0GetAttrib(void)
{	
	switch (mRouteGetAttributeID())
	{
		case _ATTRIB_REVISION:
			mRoutePutByte(_DNET_REVISION & 0xFF);
			mRoutePutByte((_DNET_REVISION & 0xFF00) >> 8);
			break;
				
		default:
			mRoutePutError(ERR_ATTRIB_NOT_SUPPORTED);
			break;
	}
  	return (1);
}



/*********************************************************************
 * Function:        unsigned char DNetInst1GetAttrib(void)
 *
 * PreCondition:    The path (aService, aClassID, aInstanceID) must
 *					be loaded prior to using this function. If service
 *					indicates any type of IO then the buffer pointers
 *					must be initialized.
 *
 * Input:       	aHeader, aClassID, aInstanceID, aService
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *					aOutBufLen, aInBufLen		
 *                  
 * Output:      	aHeader, aService	
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *				
 *			
 *
 * Side Effects:    
 *
 * Overview:        Get attribute service for instance 1.  
 *
 * Note:            None
 ********************************************************************/
unsigned char _DNetInst1GetAttrib(void)
{
	switch (mRouteGetAttributeID())
	{
		#if	ALLOW_MAC_ID
		case _ATTRIB_MAC_ID:
			mRoutePutByte(uDNet.MACID);
			break;
		#endif
	
		#if	ALLOW_BAUD_RATE
		case _ATTRIB_BAUD_RATE:
			mRoutePutByte(uDNet.BaudRate);
			break;
		#endif
				
		#if	ALLOW_BOI
		case _ATTRIB_BOI:
			mRoutePutByte(uDNet.BOI);
			break;
		#endif
				
		#if	ALLOW_BUS_OFF_COUNT
		case _ATTRIB_BO_COUNT:
			mRoutePutByte(uDNet.BusOffCount);
			break;
		#endif
				
		case _ATTRIB_ALLOC_INFO:
			mRoutePutByte(uDNet.AllocInfo.AllocChoice.byte);
			mRoutePutByte(uDNet.AllocInfo.MasterMACID);
			break;
									
		#if	ALLOW_MAC_ID_SW_CH
		case _ATTRIB_MAC_SW_CH:
			mRoutePutByte(uDNet.MACSwChange);
			break;
		#endif
				
		#if	ALLOW_BAUD_RATE_SW_CH
		case _ATTRIB_BAUD_SW_CH:
			mRoutePutByte(uDNet.BaudSwChange);
			break;
		#endif
				
		#if	ALLOW_MAC_ID_SW_VAL
		case _ATTRIB_MAC_SW_VAL:
			mRoutePutByte(uDNet.MACSwValue);
			break;
		#endif
				
		#if	ALLOW_BAUD_RATE_SW_VAL
		case _ATTRIB_BAUD_SW_VAL:
			mRoutePutByte(uDNet.BaudSwValue);
			break;
		#endif
				
		default:
			mRoutePutError(ERR_ATTRIB_NOT_SUPPORTED);
			break;
	}
		
	return (1);
}




/*********************************************************************
 * Function:        unsigned char DNetInst1SetAttrib(void)
 *
 * PreCondition:    The path (aService, aClassID, aInstanceID) must
 *					be loaded prior to using this function. If service
 *					indicates any type of IO then the buffer pointers
 *					must be initialized.
 *
 * Input:       	aHeader, aClassID, aInstanceID, aService
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *					aOutBufLen, aInBufLen		
 *                  
 * Output:      	aHeader, aService	
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *				
 *			
 *
 * Side Effects:    
 *
 * Overview:        Set attribute service for instance 1.  
 *
 * Note:            None
 ********************************************************************/
unsigned char _DNetInst1SetAttrib(void)
{
	// Ignore the first byte (it is actually the attribute ID) 
	mRouteGetByte();

	switch (mRouteGetAttributeID())
	{
		#if ALLOW_MAC_ID && SETTABLE_MAC_ID
		case _ATTRIB_MAC_ID:
			if (mRouteTestValidInputDataLen(1))
			{
				// Perform system functions to be defined by the application
				UsrDNetSetAttribEvent();	
			}
			break;
		#endif
				
		#if ALLOW_BAUD_RATE && SETTABLE_BAUD_RATE
		case _ATTRIB_BAUD_RATE:
			if (mRouteTestValidInputDataLen(1))
			{
				// Perform system functions to be defined by the application
				UsrDNetSetAttribEvent();
			}
			break;
		#endif
			
		#if ALLOW_BOI && SETTABLE_BOI
		case _ATTRIB_BOI:
			if (mRouteTestValidInputDataLen(1))
			{
				// Perform system functions to be defined by the application
				UsrDNetSetAttribEvent();
			}
			break;
		#endif
				
		#if ALLOW_BUS_OFF_COUNT && SETTABLE_BUS_OFF_COUNT
		case _ATTRIB_BO_COUNT:
			if (mRouteTestValidInputDataLen(1))
			{
				uDNet.BusOffCount = 0;
			}
			break;
		#endif
		
				
		#if ALLOW_MAC_ID && !SETTABLE_MAC_ID
		case _ATTRIB_MAC_ID:
		#endif
		#if ALLOW_BAUD_RATE && !SETTABLE_BAUD_RATE
		case _ATTRIB_BAUD_RATE:
		#endif
		#if ALLOW_BOI && !SETTABLE_BOI
		case _ATTRIB_BOI:
		#endif
		#if ALLOW_BUS_OFF_COUNT && !SETTABLE_BUS_OFF_COUNT
		case _ATTRIB_BO_COUNT:
		#endif
		case _ATTRIB_ALLOC_INFO:
		#if ALLOW_MAC_ID_SW_CH
		case _ATTRIB_MAC_SW_CH:
		#endif
		#if ALLOW_BAUD_RATE_SW_CH
		case _ATTRIB_BAUD_SW_CH:
		#endif
		#if ALLOW_MAC_ID_SW_VAL
		case _ATTRIB_MAC_SW_VAL:
		#endif
		#if ALLOW_BAUD_RATE_SW_VAL
		case _ATTRIB_BAUD_SW_VAL:
		#endif
			mRoutePutError(ERR_ATTRIB_NOT_SETTABLE);
			break;
			
		default:
			mRoutePutError(ERR_ATTRIB_NOT_SUPPORTED);
			break;
	}

	return (1);
}



/*********************************************************************
 * Function:        unsigned char DNetAllocateConnection(void)
 *
 * PreCondition:    The path (aService, aClassID, aInstanceID) must
 *					be loaded prior to using this function. If service
 *					indicates any type of IO then the buffer pointers
 *					must be initialized.
 *
 * Input:       	aHeader, aClassID, aInstanceID, aService
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen
 *					aOutBufLen, aInBufLen		
 *                  
 * Output:      	aHeader, aService	
 *					*pOutBuf, *pInBuf, aOutBufDataLen, aInBufDataLen	
 *
 * Side Effects:    
 *
 * Overview:        Allocate predefined master/slave connection set.  
 *
 * Note:            None
 ********************************************************************/
unsigned char _DNetAllocateConnection(void)
{
	unsigned char allocMAC;
	BYTE allocChoice;

	// Pull the data from the buffer for validation
	allocChoice.byte = mRouteGetByte();
	allocMAC = mRouteGetByte();  
	 
	// Connection not allocated
	if (uDNet.AllocInfo.MasterMACID == 0xFF) 
	{
		// Since the device is not allocated then no connections 
		uDNet.AllocInfo.AllocChoice.byte = 0;

		// A NULL allocation choice is not valid
		if (allocChoice.byte == 0)
		{
			mRoutePutError(ERR_INVALID_ATTRIB_VALUE);
			mRoutePutByte(_DNET_ERR_INVALID_PARAM);

			return (1);
		}

		// Check for connection support
		if (_DNetAllocNoSupportChk(allocChoice))
	   	{
	   		mRoutePutError(ERR_RESOURCE_UNAVAILABLE);
	   		mRoutePutByte(_DNET_ERR_INVALID_PARAM);

			return (1);
	   	}

		// Cannot allocate both COS and cyclic
	   	if ((allocChoice.byte & 0x30) == 0x30)
	   	{
	   		mRoutePutError(ERR_INVALID_ATTRIB_VALUE);
	   		mRoutePutByte(_DNET_ERR_INVALID_PARAM);

			return (1);
	   	}

		// Cannot support acknowledge supression if COS or cyclic not enabled

⌨️ 快捷键说明

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