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

📄 dps2spc3.c

📁 profibus通信协议
💻 C
📖 第 1 页 / 共 2 页
字号:

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

	公司名称:	 扬州恒春电子
	模块名	:	 Profibus DP从站接口程序 
	创建人	:	                                         
	日期	:	 2007/03/01	                                         
	功能描述:   利用ATMEG64L对SPC3进行配置,实现Profibus通讯                                
	其它说明:   编译环境为Keil uVision2                                            
	版本	:	 V1.0

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

#include "spc3dps2.h"

#define ASS_AUX_BUF    3
/*-- masks for config-data --*/


#define DPS_CFG_IS_BYTE_FORMAT          (UBYTE)0x30
#define DPS_CFG_BF_LENGTH               (UBYTE)0x0f
#define DPS_CFG_LENGTH_IS_WORD_FORMAT   (UBYTE)0x40
#define DPS_CFG_BF_INP_EXIST            (UBYTE)0x10
#define DPS_CFG_BF_OUTP_EXIST           (UBYTE)0x20
#define DPS_CFG_SF_OUTP_EXIST           (UBYTE)0x80
#define DPS_CFG_SF_INP_EXIST            (UBYTE)0x40
#define DPS_CFG_SF_LENGTH               (UBYTE)0x3f

#define SPC3_BUF_START ((UBYTE)(((UWORD)(dps2_base_ptr -\
			 (UBYTE *)&spc3)) >> 3))

   UBYTE _dps2_x;              			 /* dummy-byte */

   static DPS2_BUFINIT dps2_binit;       /* storeage for initialization data */
   static WORD dps2_buf_len = 0;         /* used bufferspace */
   //static UBYTE  *dps2_base_ptr =(UBYTE *)0; /* buffer base address */ 
   /*volatile static */UBYTE  *dps2_base_ptr=(UBYTE *)0;   /****/
   
   static DPS2_IO_DATA_LEN io_data_len;  /* Struct. f. IO-lens */
   UBYTE  OfflineFlag =0;



   /*
   +------------------------------------------------------------------------+
   | Function:   a s s i g n _ a u x _ b u f ( )                            |
   +------------------------------------------------------------------------+
   | Description:                                                           |
   |  Based on a list of bufferlens the best len of the aux-buffers is      |
   |  calculated. The list's lens will be adjusted, the assignment-list     |
   |  will be calculated.                                                   |
   +------------------------------------------------------------------------+
   | Parameters:                                                            |
   | -lens:    Pointer to a list of buffer-lens                             |
   |           The calculated lens are inserted in this list.               |
   | -count:   length of list (2 to ASS_AUX_BUF)                            |
   | -assign:  pointer to an assignment-byte                                |
   |           For each len there is a corresponding bit for the            |
   |           assignment to AUX-buffer 1 or 2. 0 means AUX-buffer 1,       |
   |           1 means AUX-Buffer 2. Bit 0 ist for the first, Bit 7         |
   |           for the 8th length.                                          |
   | -aux_len: pointer to a 2-byte-array for AUX-buffer-lens                |
   |           The calculated lens of AUX-buffer 1 and 2 are stored here.   |
   |                                                                        |
   | Returnvalue: UWORD: total amount of used bytes (0 = error)             |
   +------------------------------------------------------------------------+
   */

   UWORD assign_aux_buf(UBYTE  *lens, UBYTE count, UBYTE
    *assign, WORD  *aux_len)
   {
	  UBYTE pos[ASS_AUX_BUF];     /* storage for former position of lens    */
	  UBYTE lensx[ASS_AUX_BUF];   /* working-array for calculated lens      */
	  UBYTE step;                 /* counter for done step                  */
	  UBYTE lx;                   /* temp-var for lenarray-sorting          */
	  UBYTE px;                   /* temp-var dor position-sorting          */
	  UWORD min_len = 0xffff;     /* calculated min-len                     */
	  UBYTE min_step = 0;         /* step at reached min-len                */
	  BYTE i,j;

	  if((count < 2) || (count > ASS_AUX_BUF))
	  {
		 return 0;
	  }
	  /* init position-array */
	  for(i = 0; i < count; i++)
	  {
		 pos[i] = i;
	  }
	  /* init working-array */
	  for(i = 0; i < count; i++)
	  {
		 lensx[i] = lens[i];
	  }
	  /* round up lens to SPC3-lens (8-byte-granularity) */
	  for(i = 0; i < count; i++)
	  {
		 lensx[i] = (lensx[i] + 7) & 0xf8;
	  }
	  /* sorting of lens: gratest len to index 0 */
	  for(i = 0; i < count-1; i++)
	  {
		 for(j = i+1; j < count; j++)
		 {
			if(lensx[i] < lensx[j])
			{
			   /* greater len found */
			   lx = lensx[i];          /* xchg lens */
			   lensx[i] = lensx[j];
			   lensx[j] = lx;
			   px = pos[i];            /* xchg position */
			   pos[i] = pos[j];
			   pos[j] = px;
			}
		 }
	  }
	  /* remove NULL-lens of list */
	//           
	  for(i = count-1; i >= 0; i--)
	  {
		 if(lensx[i] == 0)
		 {
			count--;
		 }
		 if(!i)       break;
	  }
	  if(count == 0)
	  {
		 min_len = 0;     /* error: no lens specified */
	  }
	  /* stepwise assignment to the AUX-buffers */
	  for(step = 0; step < count; step++)
	  {
		 /* determine total len for AUX-buffer 1 */
		 aux_len[0] = 0;

		 for(i = step; i < count; i++)
		 {
			if(aux_len[0] < lensx[i])
			{
			   aux_len[0] = lensx[i];
			}
		 }
		 aux_len[0] = aux_len[0] * (count - step + 1);
		 /* determine total len for AUX-buffer 2 */
		 aux_len[1] = 0;

		 for(i = 0; i < step; i++)
		 {
			if(aux_len[1] < lensx[i])
			{
			   aux_len[1] = lensx[i];
			}
		 }
		 aux_len[1] = aux_len[1] * (step + 1);

		 if((aux_len[0] + aux_len[1]) < min_len)
		 {
			/* neue Minimal-Laenge gefunden */
			min_len = aux_len[0] + aux_len[1];
			min_step = step;
		 }
	  }

	  /*  calculation of len for AUX-buffer 1 */
	  aux_len[0] = 0;

	  for(i = min_step; i < count; i++)
	  {
		 if(aux_len[0] < lensx[i])
		 {
			aux_len[0] = lensx[i];
		 }
	  }
	  /*  setup lens for AUX-buffer 1 */
	  for(i = min_step; i < count; i++)
	  {
		 lens[pos[i]] = aux_len[0];
	  }
	  /*  calculation of len for AUX-buffer 2 */
	  aux_len[1] = 0;

	  for(i = 0; i < min_step; i++)
	  {
		 if(aux_len[1] < lensx[i])
		 {
			aux_len[1] = lensx[i];
		 }
	  }
	  /*  setup lens for AUX-buffer 2 */
	  /*  setup assignment-list */
	  *assign = 0;    /* initial all buffers assigned to AUX-buffer 1 */

	  for(i = 0; i < min_step; i++)
	  {
		 lens[pos[i]] = aux_len[1];
		 *assign |= 0x1 << pos[i];
	  }
	  return min_len;
   }

   /*
   +------------------------------------------------------------------------+
   | Funktion:  d p s 2 _ c a l c u l a t e _ i n p _ o u t p _ l e n       |
   +------------------------------------------------------------------------+
   | Description:                                                           |
   |  This function calculates the Input/Outputdata-lens based on the       |
   |  specified config-data and returns a pointer to the calculated lens.   |
   |  (original from DPS).                                                  |
   +------------------------------------------------------------------------+
   | parameters:                                                            |
   |  -cfg_ptr: pointer to config-data                                      |
   |  -cfg_len: config-data len                                             |
   |                                                                        |
   | returnvalue:                                                           |
   |  -pointer to lens                                                      |
   +------------------------------------------------------------------------+
   */
   DPS2_IO_DATA_LEN  *dps2_calculate_inp_outp_len (UBYTE
   * cfg_ptr, UWORD cfg_len)
   {
	  UBYTE temp_inp_data_len;
	  UBYTE temp_outp_data_len;
	  UBYTE length;
	  UBYTE count;
	  UBYTE specific_data_length;
	  UBYTE result_ok;

	  result_ok = TRUE;
	  temp_inp_data_len  = 0;
	  temp_outp_data_len = 0;

	  if ((cfg_len >  0) && (cfg_len <= dps2_binit.cfg_buf_len))
	  {
		 for ( ; (cfg_len > 0) && result_ok; cfg_len -= count)
		 {
			count = 0;

			if (*cfg_ptr & DPS_CFG_IS_BYTE_FORMAT)
			{
			   count++;

			   /* cfg_ptr points to ID-byte, CFG_BF means "CFG_IS_BYTE_FORMAT" */
			   length = (UBYTE)( (*cfg_ptr & DPS_CFG_BF_LENGTH) + 1);

			   if (*cfg_ptr & DPS_CFG_LENGTH_IS_WORD_FORMAT)
			   {
				  length *= 2;
			   }
			   if (*cfg_ptr & DPS_CFG_BF_OUTP_EXIST)
			   {
				  temp_outp_data_len = temp_outp_data_len + length;
			   }
			   if (*cfg_ptr & DPS_CFG_BF_INP_EXIST)
			   {
				  temp_inp_data_len = temp_inp_data_len + length;
			   }
			   cfg_ptr++;
			}
			else
			{
			   /* cfg_ptr points to the headerbyte of special ID-format */
			   /* CFG_SF means "CFG_IS_SPECIAL_FORMAT" */
			   if (*cfg_ptr & DPS_CFG_SF_OUTP_EXIST)
			   {
				  count++;                /* next byte contains the length of ou
										  tp_data */
				  length = (UBYTE)((*(cfg_ptr + count) & DPS_CFG_SF_LENGTH) +1
				  );

				  if (*(cfg_ptr + count) & DPS_CFG_LENGTH_IS_WORD_FORMAT)
				  {
					 temp_outp_data_len = temp_outp_data_len + (UBYTE)(2*length
					 );
				  }
				  else
				  {
					 temp_outp_data_len = temp_outp_data_len + length;
				  }
			   }
			   if (*cfg_ptr & DPS_CFG_SF_INP_EXIST)
			   {
				  count++;                /* next byte contains the length of in
										  p_data */
				  length = (UBYTE)((*(cfg_ptr + count) & DPS_CFG_SF_LENGTH) +1
				  );

				  if (*(cfg_ptr + count) & DPS_CFG_LENGTH_IS_WORD_FORMAT)
				  {
					 temp_inp_data_len = temp_inp_data_len + (UBYTE)(2*length);
				  }
				  else
				  {
					 temp_inp_data_len = temp_inp_data_len + length;
				  }
			   }
			   specific_data_length = (UBYTE)(*cfg_ptr & DPS_CFG_BF_LENGTH);

			   if (specific_data_length != 15)
			   {
				  count = (UBYTE)(count + 1 + specific_data_length);
				  cfg_ptr = cfg_ptr + count;
			   }
			   else
			   {
				  result_ok = FALSE;
			   }
			}
		 }

		 if ( (cfg_len != 0) ||
				(
				(((UWORD)temp_inp_data_len + 7) & 0xfff8 +
				((UWORD)temp_outp_data_len + 7) & 0xfff8)
				> dps2_binit.din_dout_buf_len) )
		 {
			result_ok = FALSE;
		 }
	  }
	  else
	  {
		 result_ok = FALSE;
	  }
	  if (result_ok)
	  {
		 io_data_len.inp_data_len  = temp_inp_data_len;
		 io_data_len.outp_data_len = temp_outp_data_len;
		 return (&io_data_len);
	  }
	  else
	  {
		 return ((DPS2_IO_DATA_LEN *) 0);
	  }
   }



   /*
   +------------------------------------------------------------------------+
   | Function:  d p s 2 _ b u f _ i n i t ( )                               |
   +------------------------------------------------------------------------+
   | Description:                                                           |
   |  The specified lens arechecked. If they are ok, the neccesarry         |
   |  memory is calculated. If there is enough memory, the buffers are      |
   |  armed.                                                                |
   |  In dps2_buf_len the amount of used Memory is stored.                  |
   |  The buffers are armed at the begin of the user-area in the following  |
   |  way:                                                                  |
   |   Outputdata    |  total len of the                                    |
   |   Inputdata     |   Input-/Outputbuffers * 3                           |
   |   Diagnosticbuffer (2*)                                                |
   |   Config-buffer                                                        |
   |   Read-Config-buffer                                                   |
   |   Parameterbuffer                                                      |
   |   Aux-buffer 0                                                         |
   |   Aux-buffer 1 (if used)                                               |
   |   Set-Slave-Address-buffer (if used)                                   |
   +------------------------------------------------------------------------+
   | parameters:                                                            |
   |  -b_ptr:     Pointer to start of buffer-area                           |
   |  -dps2_bptr: Pointer to initialisierungs-structure                     |
   |  -fdl_init:  TRUE, if FDL is used                                      |
   |  -spec_prm:  TRUE, if spec param-buffer is used                        |
   |                                                                        |
   | returncode:                                                            |
   |  SPC3_INIT_OK:            SPC3 initialized correctly                   |
   |  SPC3_INITF_LESS_MEM:     not enough memory                            |
   |  SPC3_INITF_NOFF:         SPC3 is not offline                          |
   |  DPS2_INITF_DIN_DOUT_LEN: allowed: 0 - 488                             |
   |  DPS2_INITF_DIAG_LEN:      - " - : 6 - 244                             |
   |  DPS2_INITF_PRM_LEN:       - " - : 7 - 244                             |
   |  DPS2_INITF_CFG_LEN:       - " - : 1 - 244                             |
   |  DPS2_INITF_SSA_LEN:       - " - : 0 bzw. 4 - 244                      |
   +------------------------------------------------------------------------+
   */

   /* position of bits in AUX-buffer-assignment */

⌨️ 快捷键说明

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