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

📄 dps2spc3.c

📁 从事Profibus的开发人员可以利用改源代码理解profibus协议。添加除通讯以外的代码
💻 C
📖 第 1 页 / 共 3 页
字号:
dps2_calculate_inp_outp_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 SPC3_DATA_ATTR *dps2_calculate_inp_outp_len (UBYTE
   SPC3_PTR_ATTR* 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);
	  }
   }


/*~
dps2_buf_init()
~*/
   /*
   +------------------------------------------------------------------------+
   | 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                      |
   +------------------------------------------------------------------------+
   */
   /*------------------------------ Literals ------------------------------*/
   /* position of bits in AUX-buffer-assignment */
#define RBL_PRM                 0
#define RBL_CFG                 1
#define RBL_SSA                 2

   enum SPC3_INIT_RET dps2_buf_init(UBYTE SPC3_PTR_ATTR* b_ptr, DPS2_BUFINIT
   SPC3_DATA_ATTR *dps2_bptr, UBYTE fdl_init, UBYTE spec_prm)
   {
	  UBYTE real_buf_len[ASS_AUX_BUF];
	  WORD  aux_buf_len[2];      /* calculated lens of AUX-buffers */
	  UBYTE aux_ass;             /* var. for AUX-buffer-assignment */
	  WORD  r_din_dout_buf_len;  /* var. for real IO-data-lens */
	  UBYTE SPC3_PTR_ATTR *spc_uptr;  /* helppointer, 'cos of C166 compiler */
	  enum SPC3_INIT_RET ret = SPC3_INIT_OK;

	  dps2_buf_len = 0;

	  if (SPC3_GET_OFF_PASS())
	  {
		 return SPC3_INITF_NOFF; /*==== END: SPC3 is not Offline ====*/
	  }
	  dps2_binit = *dps2_bptr;    /* store init-data */
	  dps2_base_ptr = b_ptr;      /* start of memory */

	  if(dps2_binit.din_dout_buf_len > 488)
	  {
		 ret = DPS2_INITF_DIN_DOUT_LEN;
	  }
	  if((dps2_binit.diag_buf_len < 6) || (dps2_binit.diag_buf_len > 244))
	  {
		 ret = DPS2_INITF_DIAG_LEN;
	  }
	  if((dps2_binit.prm_buf_len < 7) || (dps2_binit.prm_buf_len > 244))
	  {
		 ret = DPS2_INITF_PRM_LEN;
	  }
	  if((dps2_binit.cfg_buf_len < 1) || (dps2_binit.cfg_buf_len > 244))
	  {
		 ret = DPS2_INITF_PRM_LEN;
	  }
	  if(dps2_binit.ssa_buf_len == 0)
	  {
		 DPS2_SET_ADD_CHG_DISABLE()
	  }
	  else
	  {
		 /* SSA-buffer is used */
		 if((dps2_binit.ssa_buf_len < 4) || (dps2_binit.ssa_buf_len > 244))
		 {
			ret = DPS2_INITF_SSA_LEN;
		 }
	  }
	  if(ret != SPC3_INIT_OK)
	  {
		 /* error occured */
		 dps2_buf_len = 0;     /* no memory used */
		 return ret;           /*===== T H E   E N D =====*/
	  }
	  /* Bufferlens seem to be ok --> calculate total amount of memory */
	  if(spec_prm)
	  {
		 real_buf_len[RBL_PRM] = 0; /* Prm. via AUX */
	  }
	  else
	  {
		 real_buf_len[RBL_PRM] = dps2_binit.prm_buf_len;
	  }
	  real_buf_len[RBL_CFG] = dps2_binit.cfg_buf_len;
	  real_buf_len[RBL_SSA] = dps2_binit.ssa_buf_len;
	  dps2_buf_len = assign_aux_buf(real_buf_len, sizeof(real_buf_len), &aux_ass
	  , aux_buf_len);
	  dps2_buf_len += real_buf_len[RBL_CFG]; /* wg. Read-Config */
	  /* amount for IO-data, diagnosticbuffer and spec.Prm.buffer */
	  r_din_dout_buf_len = (dps2_binit.din_dout_buf_len + 7) & 0xfff8;
	  dps2_buf_len += ((dps2_binit.diag_buf_len + 7) & 0xf8) * 2;

	  if(spec_prm)
	  {
		 /* spec_prm-buffer is used */
		 real_buf_len[RBL_PRM] = (dps2_binit.prm_buf_len + 7) & 0xf8;
		 dps2_buf_len += real_buf_len[RBL_PRM];
		 spc3.r_len_spec_prm_buf = dps2_binit.prm_buf_len;
	  }
	  else
	  {
		 spc3.r_len_spec_prm_buf = 0;
	  }
	  dps2_buf_len += r_din_dout_buf_len * 3;

	  if(dps2_buf_len > sizeof(spc3.user))
	  {
		 /* not enough memory */
		 dps2_buf_len = 0;
		 dps2_base_ptr = 0;
		 return SPC3_INITF_LESS_MEM;
	  }
	  /* arm the pointers */
	  spc3.r_aux_buf_sel = aux_ass;   /* assign AUX-buffers */
	  spc3.r_diag_buf_ptr[0] = SPC3_BUF_START + ((r_din_dout_buf_len * 3)>>3);
	  spc3.r_diag_buf_ptr[1] = spc3.r_diag_buf_ptr[0] + (((
	  dps2_binit.diag_buf_len + 7) & 0xf8)>>3);
	  spc3.r_cfg_buf_ptr = spc3.r_diag_buf_ptr[1] + (((dps2_binit.diag_buf_len +
	   7) & 0xf8)>>3);
	  spc3.r_read_cfg_buf_ptr = spc3.r_cfg_buf_ptr + (real_buf_len[RBL_CFG]>>3);
	  spc3.r_prm_buf_ptr = spc3.r_read_cfg_buf_ptr + (real_buf_len[RBL_CFG]>>3);
	  spc3.r_aux_buf_ptr[0] = spc3.r_prm_buf_ptr + (real_buf_len[RBL_PRM]>>3);
	  spc3.r_aux_buf_ptr[1] = spc3.r_aux_buf_ptr[0] + (aux_buf_len[0]>>3);

⌨️ 快捷键说明

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