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

📄 ad_util.c

📁 基于EthernetIP协议的应用程序,可以读取AB公司Controllogix系列Ethernetip协议PLC数据. 此软件代码可用于工业控制.
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*****************************************************************************
**
** File Name
** ---------
**
** AD_Util.C
**
*****************************************************************************
*****************************************************************************
**
** Description
** -----------
**
** Application Data Area manager. Generic Demo version.
**
*****************************************************************************
*****************************************************************************
**
** Source Change Indices
** ---------------------
**
** Porting: <none>--2--<major>         Customization: <none>----4<major>
**
*****************************************************************************
*****************************************************************************
**                                                                         **
** ETHERNET/IP EXAMPLE CODE                                                **
** COPYRIGHT (c) 2000-2005 ODVA (Open DeviceNet Vendor Association)		   **
**						   & ControlNet International Ltd.                 **
**                                                                         **
** All rights reserved, except as specifically licensed in writing.        **
** Use of the Ethernet/IP Example Protocol Software is subject to          **
** ODVA's and ControlNet International's Terms of Use Agreement.           **
** The following work constitutes example program code and is intended     **
** merely to illustrate useful programming techniques.  The user is        **
** responsible for applying the code correctly.  The code is provided      **
** AS IS without warranty and is in no way guaranteed to be error-free.    **
**                                                                         **
*****************************************************************************
*****************************************************************************
*/

/****************************************************************************
*****************************************************************************
**
** Change Log
** ----------
**
**
*****************************************************************************
****************************************************************************/

#include "ab_env.h"

#include "cd.h"         /* Communications Device public interfaces         */
#include "ad.h"         /* Application Data Area manager public interfaces */
#include "bf.h"         /* Buffer Fragment mapper public interfaces        */
#include "ad_util.h"    /* Application Data Area manager private secrets   */
#include "mr.h"         /* Message router component public interfaces      */
#include "id.h"
#include "id_obj.h"     /* Identity Object private interfaces */

/*
** start edits: October,17th 2005, H.F.
*/
#include "sy.h"         /* Assembly Object public interfaces               */
#include "sy_obj.h"     /* Assembly Object private interfaces              */
/*
** end edits: October,17th 2005, H.F.
*/

/*
** start edits: November,7th 2005, H.F.
*/
#include "cd_util.h"
/*
** end edits: November,7th 2005, H.F.
*/


#include "ECUserDefined.h"

/****************************************************************************
*****************************************************************************
**
** Public Globals
**
*****************************************************************************
****************************************************************************/

GS_MsgQueueType  AD_xQid;



/****************************************************************************
*****************************************************************************
**
** Private Globals
**
*****************************************************************************
****************************************************************************/

ad_DataType ad_s;



/****************************************************************************
*****************************************************************************
**
** Public Services
**
*****************************************************************************
****************************************************************************/

/*---------------------------------------------------------------------------
** AD_Init()
**---------------------------------------------------------------------------
*/

StatusType AD_Init( UINT16 iOptions )
{
   BF_RegisterTrrblType  *pTrrbl;
   GS_TaskSeedType        sSeed;
   GS_TimerType           xTimer;
	unsigned int i;

   /*
   ** Allocate/Create all resources (except for tasks).
   */

   if( iOptions & AB_CREATE_RESOURCE )
   {
      /*
      ** Create the task's request queue.
      */

      AD_xQid = GS_NewMsgQueue();

      /*
      ** Create a timer to wake the application task periodically.
      ** Do this to emulate a periodic application.
      */

      xTimer = GS_NewBitMsgTimer( TIMER_CONTINUOUS, 10000L, 1000L, AD_xQid, AD_BIT_MSG_TIMER );
      GS_StartTimer( xTimer );

   }

   /*
   ** Initialize all internal data structures.
   */

   if( iOptions & AB_INIT_RESOURCE )
   {
   }

   /*
   ** Create the task & associate it with the message queue created earlier.
   */

   if( iOptions & AB_CREATE_TASK )
   {
      sSeed.pRoutine   = ad_Task;
      sSeed.pParameter = NULL;
      sSeed.pStack     = NULL;
      sSeed.iStackSize = AD_STACK_SIZE;
      sSeed.nPrio      = AD_TASK_PRIO;
      sSeed.pTaskName  = "AD  ";

      GS_AssociateTaskAndQueues( GS_NewTask( &sSeed ), AD_xQid, GS_NO_QUEUE );
   }

   /*
   ** Final initialization after tasks are running.
   */

   if( iOptions & AB_INIT_TASK )
   {
      /*
      ** Register all application data areas with the buffer fragment mapper.
      */

      /* ! */
      if( gUserDefinedAssembly!=NULL )
      	for( i=0; i<gUserDefinedAssembly->itsInstancesCount; i++ ){
           
      	   pTrrbl = GS_NewTrrbl( BF_RegisterTrrblType );
           
      	   pTrrbl->eRequest           = TREQ_REGISTER;
      	   pTrrbl->eAppDataAreaId     = gUserDefinedAssembly->itsInstances[i].itsAppDataAreaId;
      	   pTrrbl->pBaseAddress       = ad_s.asDataArea[ gUserDefinedAssembly->itsInstances[i].itsAppDataAreaId ].pabBaseAddress;
      	   pTrrbl->iSize              = ad_s.asDataArea[ gUserDefinedAssembly->itsInstances[i].itsAppDataAreaId ].iSize;
           
      	   pTrrbl->xMNotifyQueue      = AD_xQid;
      	   pTrrbl->iMNotifyBitMsg     = 0;
      	   pTrrbl->xMNotifySemaphore  = GS_NO_SEMAPHORE;
           
      	   switch( gUserDefinedAssembly->itsInstances[i].itsSYType ){
      	      case CONFIG:
      	      case STATUSOUT:
      	      case CONSUMER:
      	         pTrrbl->xSNotifyQueue  = AD_xQid;
      	         pTrrbl->iSNotifyBitMsg = AD_BIT_MSG_SCHEDULED_CON;
      	         break;
      	      case STATUSIN:
      	      case PRODUCER:
      	         pTrrbl->xSNotifyQueue  = GS_NO_QUEUE;
      	         pTrrbl->iSNotifyBitMsg =           0;
      	         break;
      	   }
           
      	   pTrrbl->xSNotifySemaphore  = GS_NO_SEMAPHORE;
           
      	   pTrrbl->lCpuTimePerMessage    = 0L;
      	   pTrrbl->lCpuTimePerEntry      = 0L;
      	   pTrrbl->lCpuTimePerByte       = 0L;
      	   pTrrbl->lCpuTimeMaxPerSecArea = 1L;
           
      	   GS_PutTrrbl( BF_xQid, pTrrbl );
      	}
      
   }

   return( SUCCESS );

} /* end of AD_Init() */



/****************************************************************************
*****************************************************************************
**
** Private Services
**
*****************************************************************************
****************************************************************************/

/*---------------------------------------------------------------------------
** ad_DemoLoopback()
**---------------------------------------------------------------------------
*/

extern id_DataType       id_s;
void ad_DemoLoopback( ad_DemoLoopType eLoopType )
{
	unsigned int i; /* index in for-loop, September,19th 2005, H.F. */


	/*
	** start edits: September,19th 2005, H.F.
	**
	** check, state of the Assembly Instances: after Power Up
	** they transition from Non-Existent to Inactive
	*/

	/* Non-Existent? */
	if(	!(EC_Status & 0x00F0) && (id_s.iStatus & 0x00F0) )
	{
		/* Transition to Inactive */
		for( i=0; i<gUserDefinedAssembly->itsInstancesCount; i++ )
		{
			if( !(gUserDefinedAssembly->itsInstances[i].itsState & 0x0006) )
			{
				gUserDefinedAssembly->itsInstances[i].itsState |= 0x0002; /* inactive */

				/*
				** start edits: September,23rd 2005, H.F.
				*/

				EC_AssemblyStateCB( 1 /* inactive */, gUserDefinedAssembly->itsInstances[i].itsInstanceNo );

				/*
				** end edits: September,23rd 2005, H.F.
				*/
			}
		}
	}

	/*
	** end edits: September,19th 2005, H.F.
	*/


	/* ! Show status to the ExampleCodeApplication */
	EC_Status           = id_s.iStatus;

	/* ! Get Identity Information from ExampleCodeApplication */
	id_s.iVendorId      = EC_VendorId;
	id_s.iProductCode   = EC_ProductCode;
	id_s.iProductType   = EC_ProductType;
	id_s.bMajorRev      = EC_MajorRev;
	id_s.bMinorRev      = EC_MinorRev;

	ID_SetProductName( EC_ProductName );

	id_s.lSerialNumber  = EC_SerialNumber;
	id_s.plSerialNumber = (UINT32* const)&id_s.lSerialNumber;

} /* end of ad_DemoLoopback() */



/*---------------------------------------------------------------------------
** ad_ProcessAccessRequest()
**---------------------------------------------------------------------------
*/

void ad_ProcessAccessRequest( BF_AccessTrrblType *pTrrbl )
{
   UINT8            *pabAppData;

   /*
   ** start edits: October,17th 2005, H.F.
   */

   t_UserDefinedAssemblyInstance   *aUserDefinedAssemblyInstance;
   ID_TrrblType					   *pIdConfigTrrbl;

   /*
   ** end edits: October,17th 2005, H.F.
   */


   /*
   ** Move the requested chunk of data in the appropriate direction.
   */

   /*********************************************/
   /*** PORTING ALERT!                        ***/
   /*** ADD APPROPRIATE HANDSHAKING AND OTHER ***/
   /*** PLATFORM SPECIFIC COPY STUFF HERE     ***/
   /*** BEFORE/DURING/AFTER COPY AS NEEDED.   ***/
   /*********************************************/


   pabAppData = ad_s.asDataArea[ pTrrbl->eAppDataAreaId ].pabBaseAddress + pTrrbl->iAppDataAreaOffset + pTrrbl->iMappedOffset;

   if( pTrrbl->fSet )
   {
	  /*
	  ** start edits: August,26th 2005, H.F.
	  ** 
	  ** offset for CONCUMER, CONFIG and STATUSOUT
	  **
	  */

	  if( pTrrbl->fCon )
		  pabAppData += 4;
	  /*
	  ** end edits: August,26th 2005, H.F.
	  */
	  
      CB_StripComBuf( pTrrbl->pComBuf, pabAppData, pTrrbl->iSize );


	  /*
	  ** start edits: October,17th 2005, H.F.
	  **
	  ** if new configuration differs from "out-of-box" default,
	  ** mark this CONFIG Assembly as configured
	  **
	  */

	  /* get Assembly */
	  if( (aUserDefinedAssemblyInstance=EC_FindUserDefinedAssemblyInstance( pTrrbl->iInstance ))
		  /* check, if CONFIG Assembly */
		  && (aUserDefinedAssemblyInstance->itsSYType == CONFIG) )
	  {
		  /* check, if configuration differs from "out-of-box"-default */
		  if( memcmp( ad_s.asDataArea[ pTrrbl->eAppDataAreaId ].pabBaseAddress,
			   		  SY_DefaultConfigArea[pTrrbl->eAppDataAreaId-AD_APP_DATA_AREA_EC_BASE].Data,
					  SY_DefaultConfigArea[pTrrbl->eAppDataAreaId-AD_APP_DATA_AREA_EC_BASE].iSize ) )
		  {
			  /* set associated ConfigArea */
			  SY_ConfigArea[pTrrbl->eAppDataAreaId-AD_APP_DATA_AREA_EC_BASE] = 1;
		  }
		  else
		  {
			  /* reset associated ConfigArea */
			  SY_ConfigArea[pTrrbl->eAppDataAreaId-AD_APP_DATA_AREA_EC_BASE] = 0;
		  }

		  /* Generate an event indicating that the configured State might have changed */
		  pIdConfigTrrbl = GS_NewTrrbl( ID_TrrblType );
		  pIdConfigTrrbl->eRequest    = TREQ_UPDATE_STATUS;
		  pIdConfigTrrbl->iStatusMask = ID_STATUS_CONFIGURED_MASK;
		  pIdConfigTrrbl->iStatus     = ID_STATUS_UNCONFIGURED;
		  GS_PutTrrbl( ID_xQid, pIdConfigTrrbl );

		  /* call CallBack */
		  EC_UserDefinedAssemblyInstanceConfigDataCB( aUserDefinedAssemblyInstance->itsInstanceNo );
	  }
	  
	  /*
	  ** end edits: October,17th 2005, H.F.
	  */

   }
   else
   {
	  /*
	  ** start edits: August,26th 2005, H.F.
	  ** 
	  ** offset for CONCUMER, CONFIG and STATUSOUT
	  **
	  */

	  if( pTrrbl->fCon )
		  pabAppData += 4;
	  /*
	  ** end edits: August,26th 2005, H.F.
	  */

      CB_PrependComBuf( pTrrbl->pComBuf, pabAppData, pTrrbl->iSize );
   }

} /* end of ad_ProcessAccessRequest() */



/*---------------------------------------------------------------------------
** ad_ResyncCommAndAppBufs()
**---------------------------------------------------------------------------
*/
void ad_ResyncCommAndAppBufs( AD_AppDataAreaIdType eAppDataAreaId, BOOL fCon )
{
   UINT16                        iEntries;
   UINT16                        iEntriesTotal;
   UINT16                        iTransportId;
   UINT16                        iGetTransportId; /* November,7th 2005, H.F. */
   UINT16                        iPort;
   volatile BF_ActiveEntryType  *psActiveEntry;
   volatile BF_ActiveEntryType  *psActiveEntryFirst;
   StatusType                    eStatus;

   /*
   ** start edits: September,27th 2005, H.F.
   */

   unsigned int					 aProducerAssemblyInstanceNo;
   t_UserDefinedAssemblyInstance *aProducerAssemblyInstance;
   BOOL							 bRunIdleState;

   /*
   ** end edits: September,27th 2005, H.F.
   */


   /*
   ** This routine assumes it is running in the context of an
   ** obnoxiously high priority task. It also assumes all data is
   ** intact and prepared when it starts, and will remain intact throughout
   ** this function's lifetime. (It is the responsibility of the

⌨️ 快捷键说明

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