dp_user.c
上传用户:hualang
上传日期:2022-04-11
资源大小:104k
文件大小:61k
开发平台:

C/C++

  1. ///////////////////////////////////////////////////////////////
  2. // 文 件 名 : dp_user.c
  3. // 文件功能 : vpc3用户接口部分
  4. // 作    者 : 530
  5. // 创建时间 : 
  6. // 项目名称 : Blackfin
  7. // 操作系统 : 
  8. // 备    注 :
  9. // 历史记录 : 
  10. ///////////////////////////////////////////////////////////////
  11. #include "......includeLmcInc.h"
  12. #include "..E5_Commu_PtlProfibus.h"
  13. extern void vpc3_neta_isr() ;
  14. extern void vpc3_netb_isr() ; 
  15. extern void show_buffer(UBYTE dev_num,char * msg_ptr, UBYTE * ptr, UWORD length);
  16. extern int msac_c1_alarm_quit_wiscom(UBYTE dev_num, UBYTE sap, 
  17. UBYTE slot_nr, UBYTE  alarm_type, UBYTE seq_nr);
  18. #ifdef DP_TIMESTAMP
  19. /************************************************************************/
  20. /*  时间戳                                                              */
  21. /************************************************************************/
  22. // One-based array of days in year at month start
  23. static const int16 _MonthDays[13] =
  24. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
  25. //判断是否是闰年
  26. /*
  27. 闰年有两种情况,设年份为year,则:
  28. (1)当year是400的整倍数时为闰年,条件表示为:
  29. year%400= =0
  30. (2)当year是4的整倍数,但不是100的整倍数时为闰年,条件为:
  31.     year%4= =0 && year%100 != 0
  32. */
  33. #define IsLeapYear(nYear) ((((nYear) & 3) == 0) && (((nYear) % 100) != 0 || ((nYear) % 400) == 0))
  34. //计算某年某月有多少天
  35. #define GetDaysInMonth(nYear, nMon) (_MonthDays[nMon] - _MonthDays[(nMon)-1] + (((nMon) == 2 && IsLeapYear(nYear)) ? 1 : 0))
  36. ///////////////////////////////////////////////////////////////
  37. // 函 数 名 : _SecondsFromTime
  38. // 函数功能 : 计算dt所对应时间离1900.1.1有多少秒
  39. // 处理过程 : 
  40. // 备    注 : 
  41. // 作    者 : 仲伟
  42. // 时    间 : 2006年1月6日
  43. // 返 回 值 : int32
  44. // 参数说明 : TDateTime dt
  45. ///////////////////////////////////////////////////////////////
  46. // static uint32 _SecondsFromTime(const TDateTime * dt)
  47. // {
  48. //  //计算从1900.1.1到现在有多少天, 2000.1.1为1
  49. //  uint32 nDate = 365L*(dt->y-1900) + (dt->y-1900)/4 - (dt->y-1900)/100 + (dt->y-1900)/400 +
  50. //  _MonthDays[dt->mon-1] + dt->d;
  51. // 
  52. //  //  If leap year and it's before March, subtract 1:
  53. //  if (dt->mon <= 2 && IsLeapYear(dt->y))
  54. //  --nDate;
  55. // 
  56. //  //计算多少秒
  57. //  return (nDate*86400L+3600L*dt->h+60L*dt->min+dt->s);
  58. // }
  59. ///////////////////////////////////////////////////////////////
  60. /// brief     : 从秒得到当前完整时间日期
  61. /// param int32 nS
  62. /// return TFullDateTime
  63. ///
  64. /// author    : 仲伟
  65. /// date      : 2006年12月31日
  66. /// note      : 
  67. ///////////////////////////////////////////////////////////////
  68. static TDateTime _TimeFromSeconds(uint32 nS)
  69. {
  70. TDateTime dt={1900, 1, 1, 0, 0, 0, 0};
  71. uint32 nYDays; //一年多少天
  72. uint32 nYSecs; //一天多少秒
  73. uint32 nMDays;
  74. while (1)
  75. {
  76. //计算本年有多少天,闰年加一天
  77. nYDays = 365 + (IsLeapYear(dt.y)?1:0);
  78. nYSecs = nYDays * 86400L;
  79. if(nYSecs > nS) //大于剩余秒数
  80. {
  81. //时间在本年内
  82. break;
  83. }
  84. else
  85. {
  86. nS -=  nYSecs; //时间进位到下一年
  87. dt.y ++;
  88. }
  89. }
  90. //计算日期
  91. nYDays = nS / 86400L; //本年第几天
  92. for (dt.mon=1;dt.mon<=12;dt.mon++)
  93. {
  94. //计算一个月几天
  95. nMDays = _MonthDays[dt.mon]-_MonthDays[dt.mon-1];
  96. if(dt.mon == 2 && IsLeapYear(dt.y))
  97. {
  98. nMDays ++;
  99. }
  100. if(nYDays>=nMDays)
  101. {
  102. nYDays -= nMDays;
  103. //进入下一个月
  104. }
  105. else
  106. {
  107. //时间就在本月
  108. break;
  109. }
  110. }
  111. dt.d = 1+nYDays; //日
  112. //计算时间
  113. nS %= 86400L;
  114. dt.h = nS / 3600L; //时
  115. nS %= 3600L;
  116. dt.min = nS / 60; //分
  117. dt.s = nS % 60; //秒
  118. return dt;
  119. }
  120. #endif //DP_TIMESTAMP
  121. /*---------------------------------------------------------------------------*/
  122. /* defines, structures                                                       */
  123. /*---------------------------------------------------------------------------*/
  124. // -- defines for user alarm state
  125. #define USER_AL_STATE_CLOSED ((UBYTE)0x00)
  126. #define USER_AL_STATE_OPEN ((UBYTE)0x01)
  127. // -- defines for user state
  128. #define USER_STATE_CLEAR ((UBYTE)0x00)
  129. #define USER_STATE_RUN ((UBYTE)0x01)
  130. // -- defines for diagnostics
  131. #define USER_TYPE_DPV0              ((UBYTE)0xFA)
  132. #define USER_TYPE_PRM_OK            ((UBYTE)0xFB)
  133. #define USER_TYPE_PRM_NOK           ((UBYTE)0xFC)
  134. #define USER_TYPE_CFG_OK            ((UBYTE)0xFD)
  135. #define USER_TYPE_CFG_NOK           ((UBYTE)0xFE)
  136. #define USER_TYPE_APPL_RDY          ((UBYTE)0xFF)
  137. // -- status message data (coded as device diagnosis) -------------------------
  138. // -- Modulstatus -------------------------------------------------------------
  139. typedef struct
  140. {
  141.     UBYTE   sign_len;       // fix to 0x07
  142.     UBYTE   status_type;    // fix to 0x82
  143.     UBYTE   slot_number;    // fix to 0x00
  144.     UBYTE   specifier;      // fix to 0x00
  145.     UBYTE   user_data[2];
  146. } tStatusDiag;
  147. #define sizeof_StatusDiag 6
  148. //SECTION_DEF_CommuData static tStatusDiag status_diag;
  149. #define MaxAlarmLength  64 
  150. typedef struct
  151. {
  152.     UBYTE  state;
  153.     UBYTE  user_diag_active;
  154.     UWORD  old_diag;
  155.     UBYTE  user_diag[DIAG_BUFSIZE];
  156. UWORD event;
  157. UBYTE  DPV1;
  158. UBYTE  application_ready;
  159. CFG_STRUCT  real_cfg; //real cfg data
  160.     struct                   //alarm state machine data
  161.     {
  162.         UBYTE  state;
  163. UBYTE  buffer_used[ALARM_MAX_FIFO];
  164. UBYTE  user_alarm_data[ALARM_MAX_FIFO][MaxAlarmLength];
  165. ALARM_STATUS_PDU alarm_buffers[ALARM_MAX_FIFO];
  166.     }al;
  167. #ifdef DP_FDL
  168. UBYTE  dpv1_rw_buffer[ C1_LEN  ];
  169. #ifdef DPV1_IM_SUPP
  170. UWORD awImIndex[0x10];
  171. #endif//#ifdef DPV1_IM_SUPP
  172. #endif//#ifdef DP_FDL
  173. }USER_STRUC;
  174. static int     g_nPrmCount[2];
  175. static int     g_nCheckCfgCount[2];
  176. static int     g_nC1ReadCount[2];
  177. static int     g_nC1WriteCount[2];
  178. /*---------------------------------------------------------------------------*/
  179. /* global user data definitions                                              */
  180. /*---------------------------------------------------------------------------*/
  181. SECTION_DEF_CommuData VPC3_STRUC_ERRCB                vpc3_errcb[MAX_DEV_NUM]; // error structure
  182. SECTION_DEF_CommuData USER_STRUC                      user[MAX_DEV_NUM];    // user system structure
  183. /*---------------------------------------------------------------------------*/
  184. /* defines, structures and variables for our demo application                */
  185. /*---------------------------------------------------------------------------*/
  186. //default configuration data for startup
  187. //SECTION_DEF_CommuData static UBYTE DefCfg[64] = {0x59,0x17,0x18,0x93,0x93,0x25};//WDZ-5233
  188. //#define CfgDataLength 6
  189. #ifdef DPV1_IM_SUPP
  190. SECTION_DEF_CommuData static ROMCONST__ UBYTE IM_DEFAULT[64] = {  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Header, 10 Octets
  191.       0x00, 0x2A,  // Manufacturer ID, 2 Octets
  192.   0x50, 0x41, 0x30, 0x30, 0x36, 0x33, 0x30, 0x30, 0x20, 0x20, // Order ID, 20 Octets, PA006300
  193.   0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  194.   0x30, 0x30, 0x30, 0x31, 0x31, 0x32, 0x20, 0x20, 0x20, 0x20, // SerialNumber, 16 Octets, 000112
  195.   0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  196.   0x00, 0x01, // HardwareRevision, 2 Octets, 1
  197.   0x56, 0x05, 0x00, 0x00, // SoftwareRevision, 4 Octets, V500
  198.   0x00, 0x00, // RevisionCounter 
  199.   0x00, 0x00,  // PROFILE ID, 2 Octets
  200.   0x00, 0x00,  // PROFILE SPECIFIC TYPE, 2 Octets
  201.   0x01, 0x01,  // IM Version, 2 Octets
  202.   0x00, 0x01   // IM Supported, 2 Octets
  203.  };
  204. #endif//#ifdef DPV1_IM_SUPP
  205. // //counter module
  206. // SECTION_DEF_CommuData static UWORD    counter_value[MAX_DEV_NUM];
  207. // SECTION_DEF_CommuData static UBYTE    counter_timebase[MAX_DEV_NUM];
  208. // SECTION_DEF_CommuData static UBYTE    counter_upper_limit[MAX_DEV_NUM];
  209. // SECTION_DEF_CommuData static UBYTE    counter_lower_limit[MAX_DEV_NUM];
  210. #define  MAX_OUTPUT_COUNT  32
  211. #define  MAX_INPUT_COUNT   128
  212. typedef struct
  213. {
  214. UBYTE Data[MAX_OUTPUT_COUNT];
  215. } STRUC_OUTPUT;
  216. typedef struct
  217. {
  218. UBYTE Data[MAX_INPUT_COUNT];
  219. } STRUC_INPUT;
  220. typedef struct
  221. {
  222. UBYTE  NrOfModules;
  223. STRUC_INPUT input;
  224. STRUC_OUTPUT output;
  225. } STRUC_SYSTEM;
  226. SECTION_DEF_CommuData static STRUC_SYSTEM sSystem[MAX_DEV_NUM];
  227. //初始化实际配置
  228. SECTION_DEF_CommuCode static void InitRealCfgData(UBYTE dev_num)
  229. {
  230. uint8* pRealCfgBuff;
  231. uint8 nRealCfgLen;
  232. pRealCfgBuff = GetRealCfgBuffer(dev_num, &nRealCfgLen);
  233. user[dev_num].real_cfg.length = nRealCfgLen; 
  234. memcpy( &user[dev_num].real_cfg.cfg_data[0], &pRealCfgBuff[0], user[dev_num].real_cfg.length );
  235. }
  236. //组装周期性输入数据
  237. SECTION_DEF_CommuCode static void AssembleInput(UBYTE dev_num)
  238. {
  239. int i;
  240. uint8 nUserInputLen;
  241.     uint8* pDataSrc = GetInputBuffer(dev_num, &nUserInputLen);
  242.   UBYTE *pData = sSystem[dev_num].input.Data;
  243. for (i=0; i<nUserInputLen; i++)
  244. {
  245. pData[i] = pDataSrc[i];
  246. }
  247. SendProcess(dev_num,nUserInputLen);
  248. user[dev_num].event |= VPC3_EV_NEW_INPUT_DATA;
  249. }
  250. /*--------------------------------------------------------------------------*/
  251. /* user_alarm_init function                                                 */
  252. /*--------------------------------------------------------------------------*/
  253. SECTION_DEF_CommuCode void user_alarm_init(UBYTE dev_num)
  254. {
  255.     memset(&user[dev_num].al.buffer_used[0], 0, ALARM_MAX_FIFO );
  256. }
  257. /*--------------------------------------------------------------------------*/
  258. /* user_alarm_alloc function                                                */
  259. /*--------------------------------------------------------------------------*/
  260. SECTION_DEF_CommuCode ALARM_STATUS_PDU_PTR user_alarm_alloc(UBYTE dev_num)
  261. {
  262. ALARM_STATUS_PDU_PTR ptr;
  263. UBYTE                i;
  264.     
  265.     ptr = NULL;
  266.     for( i = 0; i < ALARM_MAX_FIFO; i++ )
  267.     {
  268.         if(user[dev_num].al.buffer_used[i] == FALSE)
  269.         {
  270.             ptr = &(user[dev_num].al.alarm_buffers[i]);
  271.             user[dev_num].al.buffer_used[i] = TRUE;
  272.             ptr->header        = 0;
  273.             ptr->type          = 0;
  274.             ptr->slot          = 0;
  275.             ptr->spec          = 0;
  276.             ptr->user_data_len = 0;
  277.             ptr->user_data_ptr = &(user[dev_num].al.user_alarm_data[i][0]);
  278.             break;
  279.         }
  280.     }
  281.     return ptr;
  282. }
  283. /*--------------------------------------------------------------------------*/
  284. /* user_free_alarm_buffer function                                          */
  285. /*--------------------------------------------------------------------------*/
  286. SECTION_DEF_CommuCode void user_free_alarm_buffer(UBYTE dev_num,ALARM_STATUS_PDU_PTR alarm_ptr)
  287. {
  288. ALARM_STATUS_PDU_PTR ptr;
  289. UBYTE                i;
  290.     for( i = 0; i <= ALARM_MAX_FIFO; i++ )
  291.     {
  292.         if( user[dev_num].al.buffer_used[i] == TRUE )
  293.         {
  294. ptr = &(user[dev_num].al.alarm_buffers[i]);
  295.             if( ptr == alarm_ptr )
  296.             {
  297. user[dev_num].al.buffer_used[i] = FALSE;
  298. break;
  299. }
  300.         }
  301.     }
  302. }
  303. /*---------------------------------------------------------------------------*/
  304. /* function: user_alarm (is also called from alarm state machine !!!! )     */
  305. /*---------------------------------------------------------------------------*/
  306. SECTION_DEF_CommuCode UBYTE user_alarm(UBYTE dev_num, UBYTE alarm_type, UBYTE seq_nr, ALARM_STATUS_PDU_PTR alarm_ptr, UBYTE check_diag_flag )
  307. {
  308. VPC3_UNION_DIAG_PTR tmp_diag;
  309. UBYTE  ret_value;
  310. UBYTE               ext_diag;
  311. UBYTE               len_diag;
  312. UBYTE  error;
  313. UWORD  diag;
  314.     ret_value = 0x00;
  315. diag = ((UWORD)(alarm_type << 8) | ((UWORD)seq_nr) );
  316.     if((diag != user[dev_num].old_diag) && (FALSE == user[dev_num].user_diag_active))
  317.     {
  318.         memset( &user[dev_num].user_diag[0], 0x00, sizeof( user[dev_num].user_diag ) );
  319.         tmp_diag.byte_ptr = user[dev_num].user_diag;
  320.         ext_diag = 0x00;
  321.         len_diag = 0x00;
  322.         
  323.         switch( alarm_type )
  324.         {
  325.             case USER_TYPE_CFG_OK:
  326.             {
  327.                 //ext_diag = STAT_DIAG_SET;
  328.                 break;
  329.             }
  330.             case USER_TYPE_CFG_NOK:
  331.             {
  332.                 //modul_status diagnose
  333.                 //memcpy( tmp_diag.byte_ptr, &status_diag.sign_len, sizeof_StatusDiag );
  334.                 // ext_diag = EXT_DIAG_SET;
  335.                 //len_diag = sizeof_StatusDiag;
  336.                 break;
  337.             }
  338.             case ALARM_TYPE_DIAGNOSTIC:
  339.             {
  340. //                ext_diag = EXT_DIAG_SET;                
  341.                 memcpy( tmp_diag.byte_ptr+len_diag, (UBYTE *)&alarm_ptr->header, szAlarmHeader );
  342.                 len_diag += szAlarmHeader;
  343.                 memcpy( tmp_diag.byte_ptr+len_diag, alarm_ptr->user_data_ptr, alarm_ptr->user_data_len );
  344.                 len_diag += alarm_ptr->user_data_len;
  345.                 break;
  346.             }
  347.             case ALARM_TYPE_PROCESS: //增加过程报警类型
  348.             {
  349. //                ext_diag = EXT_DIAG_SET;                
  350.                 memcpy( tmp_diag.byte_ptr+len_diag, (UBYTE *)&alarm_ptr->header, szAlarmHeader );
  351.                 len_diag += szAlarmHeader;
  352.                 memcpy( tmp_diag.byte_ptr+len_diag, alarm_ptr->user_data_ptr, alarm_ptr->user_data_len );
  353.                 len_diag += alarm_ptr->user_data_len;
  354.                 break;
  355.             }
  356.             case USER_TYPE_DPV0:
  357.             {
  358. // ext_diag = EXT_DIAG_SET;
  359. memcpy( tmp_diag.byte_ptr+len_diag, (UBYTE *)&alarm_ptr->header, szAlarmHeader );
  360. len_diag += szAlarmHeader;
  361. memcpy( tmp_diag.byte_ptr+len_diag, alarm_ptr->user_data_ptr, alarm_ptr->user_data_len );
  362. len_diag += alarm_ptr->user_data_len;
  363.              break;
  364.             }
  365.             
  366.          case USER_TYPE_APPL_RDY:
  367.             case USER_TYPE_PRM_NOK:
  368.             case USER_TYPE_PRM_OK:
  369.             default:
  370.             {
  371.                 ext_diag = 0x00;
  372.                 len_diag = 0x00;
  373.                 break;
  374.             }
  375.         }
  376.         user[dev_num].user_diag_active = TRUE;
  377.             
  378.         error = set_diagnosis(dev_num, tmp_diag, len_diag, ext_diag, check_diag_flag  );
  379.         if( error == DP_OK )
  380.         {
  381.             user[dev_num].old_diag = diag;
  382.             if( alarm_type >= USER_TYPE_PRM_OK )
  383.             {
  384.                 user[dev_num].user_diag_active = FALSE;
  385.             }
  386.             ret_value = DP_OK;
  387.         }
  388.         else
  389.         {
  390.          user[dev_num].user_diag_active = FALSE;
  391.             ret_value = error;
  392.         }
  393.     }
  394.     return ret_value;
  395. }
  396. SECTION_DEF_CommuCode UBYTE user_demo_diagnostic(UBYTE dev_num)
  397. {
  398. static UBYTE diagnostic_alarm_seq_nr;
  399. ALARM_STATUS_PDU_PTR p_alarm;
  400. UBYTE ret_value;
  401. UBYTE callback;
  402. UBYTE diag_nr = 1;//test diagnostic
  403. uint8 *pData;
  404. ret_value = TRUE;
  405. p_alarm = user_alarm_alloc(dev_num);
  406.     if( p_alarm != NULL )
  407.     {
  408.      callback = FALSE;
  409. switch(diag_nr) 
  410. {
  411. case 0:
  412. case 1:
  413. diagnostic_alarm_seq_nr = (diagnostic_alarm_seq_nr + 1) & (MAX_SEQ_NR - 1);
  414. p_alarm->header         = szAlarmHeader + 10; //假设10个用户诊断数据
  415. p_alarm->type           = ALARM_TYPE_DIAGNOSTIC;
  416. p_alarm->slot           = 1;
  417. p_alarm->spec           = (1 == 1)?SPEC_APPEARS:SPEC_DISAPPEARS;
  418. p_alarm->spec           += (diagnostic_alarm_seq_nr << SPEC_SEQ_START);
  419. p_alarm->user_data_len  = 10;//假设10个用户诊断数据
  420. pData = p_alarm->user_data_ptr;
  421. *(pData + 0) = diag_nr;
  422. *(pData + 1) = 0x02;
  423. *(pData + 2) = 0x03;
  424. *(pData + 3) = 0x04;
  425. *(pData + 4) = 0x05;
  426. callback = TRUE;
  427. break;
  428. default:
  429. break;
  430. }
  431.             
  432.         if(user[dev_num].DPV1)
  433.         {
  434.         if( set_alarm(dev_num, p_alarm, callback ) != SET_ALARM_OK )
  435.             {
  436.                 user_free_alarm_buffer(dev_num, p_alarm );
  437.              ret_value = FALSE;
  438.             }
  439.         }
  440.         else
  441.         {
  442. //user_alarm(dev_num, USER_TYPE_DPV0, p_alarm->spec, p_alarm, FALSE );
  443. if((user_alarm(dev_num, USER_TYPE_DPV0, p_alarm->spec, p_alarm, FALSE )) != DP_OK)
  444. {
  445. ret_value = FALSE;
  446. }
  447.             user_free_alarm_buffer(dev_num, p_alarm );
  448.         }
  449.     }
  450. return ret_value;
  451. }
  452. //组装动作SOE中的测量值信息,返回报文长度
  453. static uint16 _MeasureInSOEMsg(uint8 * pAddr, const TActionSOE * pSoe)
  454. {
  455. uint16 k=0;
  456. int i;
  457. for (i=0;i<NUM_MEASINSOE;i++) {
  458. if(pSoe->measure.Meas[i].MeasType!=MEAS_NONE)
  459. {
  460. //测量值类型
  461. pAddr[k++]=pSoe->measure.Meas[i].MeasType + 1; //测值ID+1
  462. //值L
  463. pAddr[k++]=LOBYTE(pSoe->measure.Meas[i].Measure); 
  464. //值H
  465. pAddr[k++]=HIBYTE(pSoe->measure.Meas[i].Measure);
  466. }
  467. }
  468. return k;
  469. }
  470. SECTION_DEF_CommuCode uint8 SendUserDiagnostic(uint8 dev_num, uint8 nDiagType, const void * pDiagData, uint8 bAlarm)
  471. {
  472. static UBYTE diagnostic_alarm_seq_nr;
  473. ALARM_STATUS_PDU_PTR p_alarm;
  474. UBYTE ret_value;
  475. UBYTE callback;
  476. uint16 nMs;
  477. // uint8 *pData;
  478. const TActionSOE * pActSOE;
  479. const TDigitalSOE * pDISOE;
  480. TDateTime dt;
  481. int nCounter;
  482. ret_value = TRUE;
  483. p_alarm = user_alarm_alloc(dev_num);
  484.     if( p_alarm != NULL )
  485.     {
  486.      callback = FALSE;
  487. switch(nDiagType) 
  488. {
  489. case ALARM_ACTION:
  490. {
  491. nCounter = 0;
  492. pActSOE = (const TActionSOE *)pDiagData;
  493. diagnostic_alarm_seq_nr = (diagnostic_alarm_seq_nr + 1) & (MAX_SEQ_NR - 1);
  494. p_alarm->header         = szAlarmHeader + nCounter; //用户诊断数据长度预留,最后填充
  495. p_alarm->type           = ( user[dev_num].DPV1 == DPV1_STATUS_1_DPV1_ENABLE ) ? ALARM_TYPE_DIAGNOSTIC : STATUS_MESSAGE;//老400V使用诊断报警,照以前方式,500按过程报警上送
  496. p_alarm->slot           = 1;
  497. p_alarm->spec           = (1)?SPEC_APPEARS:SPEC_DISAPPEARS;
  498. p_alarm->spec           += (diagnostic_alarm_seq_nr << SPEC_SEQ_START);
  499. p_alarm->user_data_len  = nCounter;//用户诊断数据长度预留,最后填充
  500. // pData = p_alarm->user_data_ptr;
  501.                 //用户诊断数据
  502. //11 与GSD文件的诊断块定义对应,value从1开始
  503. p_alarm->user_data_ptr[0] = pActSOE->type;
  504. //12 //预留
  505. p_alarm->user_data_ptr[1] = 0;
  506. //13 //预留
  507. p_alarm->user_data_ptr[2] = 0;
  508. //14 信息类型=1---保护动作
  509. p_alarm->user_data_ptr[3] = 1;
  510. //15 报警状态 1---动作 0---复归
  511. p_alarm->user_data_ptr[4] = 1;
  512. //16 时
  513. p_alarm->user_data_ptr[5] = pActSOE->time.h;
  514. //17 分
  515. p_alarm->user_data_ptr[6] = pActSOE->time.min;
  516. //18 毫秒低字节
  517. //19 毫秒高字节
  518. nMs = pActSOE->time.ms + pActSOE->time.s*1000;
  519. p_alarm->user_data_ptr[7] = LOBYTE(nMs);
  520. p_alarm->user_data_ptr[8] = HIBYTE(nMs);
  521. //SET_WORD_DATA_(&p_alarm->user_data_ptr[7],);
  522. //20 故障相别
  523. p_alarm->user_data_ptr[9] = 0;
  524. //21 故障量个数n≤14
  525. p_alarm->user_data_ptr[10] = 0; //在后面填写故障量个数
  526. //22 故障量1类型
  527. //23 故障量1低字节
  528. //24 故障量1高字节
  529. nCounter = _MeasureInSOEMsg(p_alarm->user_data_ptr + 11, pActSOE);
  530. p_alarm->user_data_ptr[10] = nCounter/3; //在后面填写故障量个数
  531. //长度
  532. p_alarm->user_data_len = 11 + nCounter;
  533. p_alarm->header        = p_alarm->user_data_len+4;
  534. if(!bAlarm)//使用状态消息
  535. p_alarm->type = STATUS_MESSAGE;
  536. callback = TRUE;
  537. }
  538. break;
  539. case ALARM_DI:
  540. {
  541. nCounter = 0;
  542. pDISOE = (const TDigitalSOE *)pDiagData;
  543. diagnostic_alarm_seq_nr = (diagnostic_alarm_seq_nr + 1) & (MAX_SEQ_NR - 1);
  544. p_alarm->header         = szAlarmHeader + nCounter; //用户诊断数据长度预留,最后填充
  545. p_alarm->type           = ( user[dev_num].DPV1 == DPV1_STATUS_1_DPV1_ENABLE ) ? ALARM_TYPE_DIAGNOSTIC : STATUS_MESSAGE;
  546. p_alarm->slot           = 1;
  547. p_alarm->spec           = (pDISOE->state)?SPEC_APPEARS:SPEC_DISAPPEARS;
  548. p_alarm->spec           += (diagnostic_alarm_seq_nr << SPEC_SEQ_START);
  549. p_alarm->user_data_len  = nCounter;//用户诊断数据长度预留,最后填充
  550. // pData = p_alarm->user_data_ptr;
  551.                 //用户诊断数据
  552. //11 与GSD文件的诊断块定义对应,value从1开始
  553. p_alarm->user_data_ptr[0] = pDISOE->type;
  554. //12 //预留
  555. p_alarm->user_data_ptr[1] = pDISOE->di;
  556. //13 //预留
  557. p_alarm->user_data_ptr[2] = 0;
  558. //14 信息类型=2---异常/开入变位
  559. p_alarm->user_data_ptr[3] = 2;
  560. //15 报警状态 1---动作 0---复归
  561. p_alarm->user_data_ptr[4] = pDISOE->state;
  562. //16 时
  563. p_alarm->user_data_ptr[5] = pDISOE->time.h;
  564. //17 分
  565. p_alarm->user_data_ptr[6] = pDISOE->time.min;
  566. //18 毫秒低字节
  567. //19 毫秒高字节
  568. nMs = pDISOE->time.ms + pActSOE->time.s*1000;
  569. p_alarm->user_data_ptr[7] = LOBYTE(nMs);
  570. p_alarm->user_data_ptr[8] = HIBYTE(nMs);
  571. //长度
  572. p_alarm->user_data_len = 9;
  573. p_alarm->header        = p_alarm->user_data_len+4;
  574. if(!bAlarm)//使用状态消息
  575. p_alarm->type = STATUS_MESSAGE;
  576. callback = TRUE;
  577. }
  578. break;
  579. case ALARM_V0_DIAG:
  580. {
  581. diagnostic_alarm_seq_nr = (diagnostic_alarm_seq_nr + 1) & (MAX_SEQ_NR - 1);
  582. p_alarm->header         = szAlarmHeader + MAX_DIAG_DATA_LEN; //用户诊断数据长度
  583. p_alarm->type           = STATUS_MESSAGE;
  584. p_alarm->slot           = 1;
  585. p_alarm->spec           = SPEC_GENERAL + (diagnostic_alarm_seq_nr << SPEC_SEQ_START);
  586. p_alarm->user_data_len  = MAX_DIAG_DATA_LEN;
  587. // pData = p_alarm->user_data_ptr;
  588. memset(p_alarm->user_data_ptr+szAlarmHeader, 0, p_alarm->user_data_len);
  589. memcpy( p_alarm->user_data_ptr, &g_ProfibusRam.V0_DiagData[0], MAX_DIAG_DATA_LEN );
  590. callback = TRUE;
  591. }
  592. break ;
  593. case ALARM_WAVE: //录波通知
  594. {
  595. diagnostic_alarm_seq_nr = (diagnostic_alarm_seq_nr + 1) & (MAX_SEQ_NR - 1);
  596. p_alarm->header         = szAlarmHeader + 4; 
  597. p_alarm->type           = ( user[dev_num].DPV1 == DPV1_STATUS_1_DPV1_ENABLE ) ? ALARM_TYPE_DIAGNOSTIC : STATUS_MESSAGE;
  598. p_alarm->slot           = 1;
  599. p_alarm->spec           = SPEC_APPEARS;
  600. p_alarm->spec           += (diagnostic_alarm_seq_nr << SPEC_SEQ_START);
  601. p_alarm->user_data_len  = 0;
  602.                 //用户诊断数据
  603. //*(pData + 0) = 0x00;
  604. //*(pData + 1) = 0x00;
  605. //*(pData + 2) = 0x00;
  606. //*(pData + 3) = ALARM_WAVE;
  607. //11 
  608. p_alarm->user_data_ptr[0] = 0;
  609. //12 //预留
  610. p_alarm->user_data_ptr[1] = 0;
  611. //13 //预留
  612. p_alarm->user_data_ptr[2] = 0;
  613. //14 
  614. p_alarm->user_data_ptr[3] = ALARM_WAVE;
  615. //TDateTime dtTime; //故障时间,不等于第一区段的时间
  616. //uint16 nFirstFAN; //第一个故障序号(第一个区段的故障序号)
  617. //uint16 nNOF; //电网故障序号
  618. //uint8  nSOF; //故障的状态
  619. dt = WaveRecord.Config.Start[0].Time;
  620. p_alarm->user_data_ptr[4] = LOBYTE(dt.y);
  621. p_alarm->user_data_ptr[5] = HIBYTE(dt.y);
  622. p_alarm->user_data_ptr[6] = dt.mon;
  623. p_alarm->user_data_ptr[7] = dt.d;
  624. p_alarm->user_data_ptr[8] = dt.h;
  625. p_alarm->user_data_ptr[9] = dt.min;
  626. nMs = dt.s * 1000 + dt.ms;
  627. p_alarm->user_data_ptr[10] = LOBYTE(nMs);
  628. p_alarm->user_data_ptr[11] = HIBYTE(nMs);
  629. p_alarm->user_data_ptr[12] = LOBYTE(WaveRecord.wavenumber.fan);
  630. p_alarm->user_data_ptr[13] = HIBYTE(WaveRecord.wavenumber.fan);
  631. p_alarm->user_data_ptr[14] = LOBYTE(WaveRecord.wavenumber.nof );
  632. p_alarm->user_data_ptr[15] = HIBYTE(WaveRecord.wavenumber.nof);
  633. p_alarm->user_data_ptr[16] = LOBYTE(WaveRecord.wavenumber.sof);
  634. //长度
  635. p_alarm->user_data_len = 17;
  636. p_alarm->header        = p_alarm->user_data_len+4;
  637. if(!bAlarm)//使用状态消息
  638. p_alarm->type = STATUS_MESSAGE;
  639. callback = TRUE;
  640. }
  641. break ;
  642. default:
  643. break;
  644. }
  645.         
  646.         if(user[dev_num].DPV1 && bAlarm)//增加只有需要报警时才走报警通道
  647.         {
  648.         if( set_alarm(dev_num, p_alarm, callback ) != SET_ALARM_OK )
  649.             {
  650. //TRACE("set_alarm return not SET_ALARM_OK...");
  651.                 user_free_alarm_buffer(dev_num, p_alarm );
  652.              ret_value = FALSE;
  653.             }
  654.         }
  655.         else
  656.         {
  657. //user_alarm(dev_num, USER_TYPE_DPV0, p_alarm->spec, p_alarm, FALSE );
  658. if((user_alarm(dev_num, USER_TYPE_DPV0, p_alarm->spec, p_alarm, FALSE )) != DP_OK)
  659. {
  660. ret_value = FALSE;
  661. }
  662.             user_free_alarm_buffer(dev_num, p_alarm );
  663.         }
  664.     }
  665. else
  666. {
  667.         //TRACE("user_alarm_alloc return null...");
  668. ret_value = FALSE;
  669. }
  670. return ret_value;
  671. }
  672. SECTION_DEF_CommuCode void read_input_data(UBYTE dev_num)
  673. {
  674. VPC3_UNSIGNED8_PTR  input_buf_ptr;  // pointer to input buffer
  675. int nBlackfinLoop;
  676. uint16* pBlackfinAddr;
  677. // uint16* pSrcAddr;
  678.     // write DIN data to VPC3
  679.     if(input_buf_ptr = vpc3_get_dinbufptr (dev_num))
  680.     {
  681.         //memcpy( input_buf_ptr, sSystem[dev_num].input.Data, dp_sys[dev_num].inp_data_len );
  682. pBlackfinAddr = (uint16*)input_buf_ptr;
  683. for (nBlackfinLoop=0; nBlackfinLoop<dp_sys[dev_num].inp_data_len; nBlackfinLoop++)
  684. {
  685. pBlackfinAddr[nBlackfinLoop] = sSystem[dev_num].input.Data[nBlackfinLoop];
  686. }
  687.         dp_sys[dev_num].vpc3_con = VPC3_INPUT_UPDATE(dev_num);
  688.     }
  689. }
  690. SECTION_DEF_CommuCode void application_ready(UBYTE dev_num)
  691. {
  692. #ifdef DPV1_IM_SUPP
  693. user[dev_num].awImIndex[ 0x03 ] = 0x0000;
  694. #endif//#ifdef DPV1_IM_SUPP
  695.     read_input_data(dev_num);
  696.     user_alarm(dev_num, USER_TYPE_APPL_RDY, 0, (ALARM_STATUS_PDU_PTR) NULL_PTR, FALSE );
  697.     user[dev_num].application_ready = TRUE;
  698. }
  699. SECTION_DEF_CommuCode bool16 InitVPC3(int dev_num, uint8 nAddr)
  700. {
  701. DP_ERROR_CODE error;
  702. //用户数据结构
  703. memset( &user[dev_num], 0, sizeof(USER_STRUC) ); 
  704.     user[dev_num].user_diag_active  = FALSE;
  705. user[dev_num].application_ready = FALSE;
  706.     user[dev_num].state = USER_STATE_CLEAR;
  707.     //VPC3 使用的数据结构
  708. memset( &sSystem[dev_num], 0, sizeof(STRUC_SYSTEM) );
  709. //初始化实际配置信息
  710. InitRealCfgData(dev_num);
  711. //复位芯片
  712. ResetVPC3(dev_num);
  713.     //芯片及协议栈初始化
  714. error = vpc3_initialization(dev_num, nAddr, user[dev_num].real_cfg );
  715.     
  716.     if( DP_OK == error )
  717.     {
  718.          START_VPC3(dev_num);
  719.     }
  720.     else
  721.     {
  722.         vpc3_errcb[dev_num].error_code = error;
  723.         fatal_error(dev_num, _DP_USER, __LINE__, &vpc3_errcb[dev_num] );
  724.     }
  725. return (error == DP_OK);
  726. }
  727. SECTION_DEF_CommuCode int GetProfibusBaudRate(int dev_num)
  728. {
  729.    return VPC3_GET_BAUDRATE(dev_num);
  730. }
  731. SECTION_DEF_CommuCode bool16 IsInDPV1Status(uint8 nDevID)
  732. {
  733. return user[nDevID].DPV1 == DPV1_STATUS_1_DPV1_ENABLE;
  734. }
  735. SECTION_DEF_CommuCode int GetProfibusPrmCount(int dev_num)
  736. {
  737. return g_nPrmCount[dev_num];
  738. }
  739. SECTION_DEF_CommuCode int GetProfibusCheckCfgCount(int dev_num)
  740. {
  741. return g_nCheckCfgCount[dev_num];
  742. }
  743. SECTION_DEF_CommuCode int GetProfibusMasterAddr(int dev_num)
  744. {
  745. return vpc3_get_master(dev_num);
  746. }
  747. SECTION_DEF_CommuCode int GetProfibusDPState(int dev_num)
  748. {
  749. return VPC3_GET_DP_STATE(dev_num);
  750. }
  751. SECTION_DEF_CommuCode int GetProfibusC1ReadCount(int dev_num)
  752. {
  753. return g_nC1ReadCount[dev_num];
  754. }
  755. SECTION_DEF_CommuCode int GetProfibusC1WriteCount(int dev_num)
  756. {
  757. return g_nC1WriteCount[dev_num];
  758. }
  759. /*---------------------------------------------------------------------------*/
  760. /* user main function                                                        */
  761. /*---------------------------------------------------------------------------*/
  762. SECTION_DEF_CommuCode void user_main(int if_num)
  763. {
  764. VPC3_UNSIGNED8_PTR  output_buf_ptr; // pointer to output buffer
  765. // DP_ERROR_CODE error;
  766. UBYTE            outp_state;  // state of output data
  767. UBYTE dev_num = (UBYTE)if_num;
  768. UBYTE               dp_state;  
  769. // UBYTE i=0;
  770.   // UWORD cnt=0;
  771.   int nBlackfinLoop;
  772.  // uint16* pBlackfinAddr;
  773.   uint16* pSrcAddr;
  774. // int nBaudRate = VPC3_GET_BAUDRATE(dev_num) ;
  775.     if( 1 )
  776.     {
  777.         //toggle user watchdog
  778.         VPC3_RESET_USER_WD(dev_num);
  779. vpc3_isr(dev_num);
  780.         if( user[dev_num].state == USER_STATE_CLEAR )
  781.         {
  782.    memset(sSystem[dev_num].input.Data , 0, MAX_INPUT_COUNT);
  783. memset(sSystem[dev_num].output.Data , 0, MAX_OUTPUT_COUNT);
  784.             user[dev_num].state = USER_STATE_RUN;
  785.         }
  786.        vpc3_dpv1_statemachine(dev_num);
  787.        dp_state = VPC3_GET_DP_STATE(dev_num);
  788.   
  789.    switch(dp_state)
  790.        {
  791.             case WAIT_PRM:
  792.             case WAIT_CFG:
  793.             case DP_ERROR:
  794.             {          
  795. if( user[dev_num].event & VPC3_EV_DX_OUT )
  796. {
  797. user[dev_num].event &= ~VPC3_EV_DX_OUT;       // clear event
  798. }
  799. SetDataExchangeLED(FALSE);
  800.                 break;
  801.             }
  802.             case DATA_EX:
  803.             {
  804. if((user[dev_num].application_ready == TRUE) && (user[dev_num].state == USER_STATE_RUN))
  805. {
  806. SetDataExchangeLED(TRUE);
  807. //处理周期性输出
  808. if( user[dev_num].event & VPC3_EV_DX_OUT )
  809. {
  810. user[dev_num].event &= ~VPC3_EV_DX_OUT;
  811.              if( output_buf_ptr = vpc3_get_doutbufptr (dev_num,&outp_state) )
  812.              {
  813.                      //memcpy( sSystem[dev_num].output.Data, output_buf_ptr, dp_sys[dev_num].outp_data_len);
  814. pSrcAddr = (uint16*)output_buf_ptr;
  815. for (nBlackfinLoop=0; nBlackfinLoop<dp_sys[dev_num].outp_data_len; nBlackfinLoop++)
  816. {
  817. sSystem[dev_num].output.Data[nBlackfinLoop] = pSrcAddr[nBlackfinLoop];
  818. }
  819.                  //处理输出数据
  820. SetOutputBuffer(dev_num, sSystem[dev_num].output.Data, dp_sys[dev_num].outp_data_len);
  821. ReceiveProcess(dev_num,dp_sys[dev_num].outp_data_len);
  822.                  }
  823.          }
  824.         
  825.          //处理诊断
  826. AlarmProcess(dev_num);
  827.          //处理输入信息
  828. AssembleInput(dev_num);
  829. if( user[dev_num].event & VPC3_EV_NEW_INPUT_DATA )
  830. {
  831. read_input_data(dev_num);
  832. user[dev_num].event &= ~VPC3_EV_NEW_INPUT_DATA;
  833. }
  834. //其他空闲任务处理
  835. DoEventInDxState(dev_num);
  836. //CS中断
  837. if (user[dev_num].event & VPC3_EV_MAC_RESET)
  838. {
  839. user[dev_num].event &= ~VPC3_EV_MAC_RESET;       // clear event
  840. //print_cs_buffer();
  841. }
  842. }
  843.                 break;
  844.             }
  845.             default:
  846.             {
  847. SetDataExchangeLED(FALSE);
  848.                 vpc3_errcb[dev_num].error_code = VPC3_GET_DP_STATE(dev_num);
  849.                 fatal_error(dev_num, _DP_USER, __LINE__, &vpc3_errcb[dev_num] );
  850.                 break;
  851.             }
  852.         }
  853.         //handle here profibus interrupt events 
  854.      if( user[dev_num].event & VPC3_EV_NEW_CFG_DATA )
  855.      {
  856.          application_ready(dev_num);
  857.          user[dev_num].event &= ~VPC3_EV_NEW_CFG_DATA;
  858.      }
  859.     }
  860. }
  861. /*---------------------------------------------------------------------------*/
  862. /*                                                                           */
  863. /*   function:     fatal_error                                               */
  864. /*                                                                           */
  865. /*---------------------------------------------------------------------------*/
  866. SECTION_DEF_CommuCode void fatal_error(UBYTE dev_num, DP_ERROR_FILE file, UWORD line, VPC3_ERRCB_PTR errcb_ptr)
  867. {
  868. }
  869. /*---------------------------------------------------------------------------*/
  870. /* function: user_isr_new_prm_data                                           */
  871. /*                                                                           */
  872. /* todo  : check the prm-data                                                */
  873. /* return: DP_OK  - prm-data OK                                              */
  874. /*         DP_NOK - prm-data not OK                                          */
  875. /*---------------------------------------------------------------------------*/
  876. #if( ISR_ENABLE_VPC3_INT_NEW_PRM_DATA == 1 )
  877. SECTION_DEF_CommuCode UBYTE user_chk_new_prm_data(UBYTE dev_num, VPC3_UNSIGNED8_PTR prm_ptr, UBYTE len )
  878. {
  879. typedef struct
  880. {
  881. UBYTE len;
  882. UBYTE slot_nr;
  883. UBYTE user_data;
  884. }STRUC_MODULE_PRM_BLOCK;
  885. #define VPC3_STRUC_MODULE_PRM_BLOCK_PTR  STRUC_MODULE_PRM_BLOCK PTR_ATTR *
  886. // VPC3_STRUC_MODULE_PRM_BLOCK_PTR module_block;
  887. VPC3_STRUC_PRM_PTR  prm_data;
  888. UBYTE               ret_value;
  889.     UWORD Clock_Sync_Interval;
  890. UBYTE i;
  891. UBYTE theUserPrmDataBuff[64];  
  892.     user[dev_num].DPV1 = 0x00;
  893.     ret_value = DP_OK;
  894. #ifdef DP_TIMESTAMP
  895. //设置时钟同步报文间隔
  896.         Clock_Sync_Interval = 100; //10ms单位, 1S校时一次
  897. p_Vpc3[dev_num]->cs.clock.sync_interval[0] = HIBYTE(Clock_Sync_Interval);
  898. p_Vpc3[dev_num]->cs.clock.sync_interval[1] = LOBYTE(Clock_Sync_Interval);
  899. #endif
  900. if( len == 0x0B )
  901.     {
  902. g_nPrmCount[dev_num]++; //参数化计数器 
  903.         prm_data = (VPC3_STRUC_PRM_PTR)prm_ptr;
  904.         //DPV1 Statusbyte 1
  905.         user[dev_num].DPV1 = (prm_data->dpv1_status_1 & DPV1_STATUS_1_DPV1_ENABLE );
  906. g_ProfibusRam.bDPV1Enable = !!user[dev_num].DPV1;//DPV1启用标志
  907. if( user[dev_num].DPV1 == DPV1_STATUS_1_DPV1_ENABLE )
  908. {
  909. // DPV1 enabled
  910. if(    (( prm_data->dpv1_status_1 & 0x18 ) != 0x00 )
  911. || (( prm_data->dpv1_status_2 & 0x03 ) != 0x00 )
  912. || ((prm_data->dpv1_status_3 & 0xFF ) != 0x00 )
  913. )
  914. {
  915. ret_value = DP_PRM_DPV1_STATUS;
  916. }
  917. }//if( user[dev_num].DPV1 == DPV1_STATUS_1_DPV1_ENABLE )
  918. else
  919. {
  920. //DP-Mode
  921. if(    (( prm_data->dpv1_status_1 & 0x18 ) != 0x00 )
  922. || (( prm_data->dpv1_status_2 & 0xFF ) != 0x00 )
  923. || (( prm_data->dpv1_status_3 & 0xFF ) != 0x00 )
  924. )
  925. {
  926. ret_value = DP_PRM_DPV1_STATUS;
  927. }
  928. }//else of if( user[dev_num].DPV1 == DPV1_STATUS_1_DPV1_ENABLE )
  929. //用户参数化数据
  930. for(i=0; i<len-10; i++)
  931. {
  932. theUserPrmDataBuff[i] = *( ((BLACKFINDATATYPE*)&prm_data->user_data) + i);
  933. }
  934. //检查用户参数化数据
  935. if(SetPrmBuffer(dev_num, theUserPrmDataBuff, len-10))
  936. {
  937. ret_value = DP_OK;
  938. }
  939. else
  940. {
  941. ret_value = DP_NOK;
  942. }
  943.     }
  944.     else
  945.     {
  946.         ret_value = DP_PRM_LEN_ERROR;
  947.     }
  948.     if( ( VPC3_GET_DP_STATE(dev_num) == DATA_EX ) && (ret_value == DP_OK ) )
  949.     {
  950.         //don't send diagnostic here
  951.     }
  952.     else
  953.     {
  954.         user_alarm(dev_num, USER_TYPE_PRM_OK, 0, (ALARM_STATUS_PDU_PTR) NULL_PTR, FALSE );
  955.     }
  956. return ret_value;
  957. }
  958. #endif
  959. /*---------------------------------------------------------------------------*/
  960. /* function: user_isr_new_ext_prm_data                                       */
  961. /*                                                                           */
  962. /* todo  : check the prm-data                                                */
  963. /* return: DP_OK  - prm-data OK                                              */
  964. /*         DP_NOK - prm-data not OK                                          */
  965. /*---------------------------------------------------------------------------*/
  966. #if( ISR_ENABLE_VPC3_INT_NEW_EXT_PRM_DATA == 1 )
  967. SECTION_DEF_CommuCode UBYTE user_chk_new_ext_prm_data(UBYTE dev_num, VPC3_STRUC_PRM_BLOCK_PTR prmblock_ptr, UBYTE len )
  968. {
  969. UBYTE ret_value;
  970. ret_value = DP_OK;
  971. len = len;
  972. // TRACE("user_chk_new_ext_prm_data len:%d cmd:%d slot:%d index:%d ",
  973. // prmblock_ptr->len&0xff,
  974. // prmblock_ptr->command&0xff,
  975. // prmblock_ptr->slot_nr&0xff,
  976. // prmblock_ptr->index&0xff
  977. // );
  978. // 
  979. //  if(    (prmblock_ptr->command == PBC_USER_PRM_DATA )
  980. //  && (prmblock_ptr->len     == 0x0C )
  981. //  && (prmblock_ptr->slot_nr == 0x00 )
  982. //  && (prmblock_ptr->index   == 0x00 )
  983. //  )
  984. //  {
  985. //  //copy counter parameter data
  986. //  memcpy( &sSystem[dev_num].prm, &prmblock_ptr->user_data+1, 7 );
  987. // 
  988. //  //check parameter of counter module
  989. //         if(    (sSystem[dev_num].prm.alarm     &  0xFC ) //check alarm
  990. //             || (sSystem[dev_num].prm.mode      >  0x01 ) //check counter mode
  991. //             || ( sSystem[dev_num].prm.time_base >  0x05 ) //check counter timebase
  992. //           )
  993. //         {
  994. //          //error
  995. //          ret_value = DP_PRM_ENTRY_ERROR;
  996. //         }//if(    (counter.prm.len != 9 )  ... 
  997. //  }
  998. //  else
  999. //  {
  1000. //  ret_value = DP_PRM_BLOCK_ERROR;
  1001. //  }
  1002. return ret_value;
  1003. }
  1004. #endif
  1005. /*---------------------------------------------------------------------------*/
  1006. /* function: user_isr_new_cfg_data   */
  1007. /*                                          */
  1008. /* todo  : check cfg data                                             */
  1009. /*                                          */
  1010. /* return: VPC3_CFG_OK      0  */
  1011. /* VPC3_CFG_FAULT   1  */
  1012. /* VPC3_CFG_UPDATE  2                  */
  1013. /*---------------------------------------------------------------------------*/
  1014. #if( ISR_ENABLE_VPC3_INT_NEW_CFG_DATA == 1 )
  1015. SECTION_DEF_CommuCode UBYTE user_chk_new_cfg_data(UBYTE mdev_num)
  1016. {
  1017. VPC3_UNSIGNED8_PTR  cfg_ptr;   // pointer check config buffer
  1018. UBYTE               ret_value = VPC3_CFG_OK;
  1019. UBYTE               cfg_len;
  1020. UBYTE               i;
  1021. UBYTE               bBU_DO_ModuleExist;
  1022.   UBYTE               bBU_DI_ModuleExist;
  1023. UBYTE               dev_num = mdev_num;
  1024. UBYTE               real_cfg_len;
  1025.     cfg_ptr  = VPC3_GET_CFG_BUF_PTR(dev_num);
  1026.     cfg_len = VPC3_GET_CFG_LEN(dev_num)&0xff;
  1027.                
  1028. if(!g_ProfibusRam.bSiemensCompatiable)
  1029. {
  1030. //和公司主控通讯时
  1031. real_cfg_len = user[dev_num].real_cfg.length&0xff; 
  1032. if( (cfg_len) != real_cfg_len )
  1033. {
  1034. ret_value = VPC3_CFG_FAULT;
  1035. }//if( cfg_len != real_cfg_len )
  1036. else
  1037. {
  1038. if( xmemcmp( &user[dev_num].real_cfg.cfg_data[0], cfg_ptr, user[dev_num].real_cfg.length&0xff ) == 0 ) 
  1039. {
  1040. SetHaveSOEModule(1);//默认只使用1条SOE的空间
  1041. ret_value = VPC3_CFG_UPDATE;//更新配置
  1042. }
  1043. else
  1044. {
  1045. SetHaveSOEModule(0);
  1046. ret_value = VPC3_CFG_FAULT;
  1047. }
  1048. }
  1049. }
  1050. else
  1051. {
  1052. //和其他单位主控通讯时
  1053. bBU_DO_ModuleExist = FALSE;
  1054. bBU_DI_ModuleExist = FALSE;
  1055. if(cfg_len % 4)
  1056. {
  1057. //先检查长度是否合理,1个模建配置数据为4个字节
  1058. ret_value = VPC3_CFG_FAULT;
  1059. }
  1060. else
  1061. {
  1062. //解析模件配置信息
  1063. g_ProfibusRam.nModuleCount = cfg_len/4;
  1064. if(g_ProfibusRam.nModuleCount > MaxModule)
  1065. {
  1066. ret_value = VPC3_CFG_FAULT;
  1067. }
  1068. else
  1069. {
  1070. for (i=0; i<g_ProfibusRam.nModuleCount; i++)
  1071. {
  1072. //检查配置配置信息是否合理
  1073. g_ProfibusRam.CfgModules[i].nModuleID = cfg_ptr[3]&0xff; //模件ID号
  1074. if(g_ProfibusRam.CfgModules[i].nModuleID == ID_MODULE_BUDIDO) //判段是否含有基本输入输出模件
  1075. {
  1076. bBU_DO_ModuleExist = TRUE;
  1077. bBU_DI_ModuleExist = TRUE;
  1078. }
  1079. else if(g_ProfibusRam.CfgModules[i].nModuleID == ID_MODULE_SOE)//判断是否含有SOE模件
  1080. SetHaveSOEModule(1);//默认只使用1条SOE的空间
  1081. if((cfg_ptr[0]&0xff) == 0x82) //输出模件
  1082. {
  1083. g_ProfibusRam.CfgModules[i].nModuleInputBytes = 0;
  1084. g_ProfibusRam.CfgModules[i].nModuleOutputBytes = cfg_ptr[1] & 0x3f;
  1085. }
  1086. else if ((cfg_ptr[0]&0xff) == 0x42)//输入模件
  1087. {
  1088. g_ProfibusRam.CfgModules[i].nModuleInputBytes = cfg_ptr[1] & 0x3f;
  1089. g_ProfibusRam.CfgModules[i].nModuleOutputBytes = 0;
  1090. }
  1091. else if((cfg_ptr[0]&0xff) == 0xC1)//输入输出模件,BU及SOE模件均含输入输出
  1092. {
  1093. g_ProfibusRam.CfgModules[i].nModuleInputBytes = cfg_ptr[1] & 0x3f;
  1094. g_ProfibusRam.CfgModules[i].nModuleOutputBytes = cfg_ptr[2] & 0x3f;
  1095. }
  1096. else  
  1097. {
  1098. //其他的不支持
  1099. ret_value = VPC3_CFG_FAULT;
  1100. }
  1101. cfg_ptr += 4;
  1102. }
  1103. //如果基本开入开出模件不存在,认为是配置出错
  1104. if (!bBU_DO_ModuleExist || !bBU_DI_ModuleExist)
  1105. {
  1106. ret_value = VPC3_CFG_FAULT;
  1107. }
  1108. }
  1109. //更新本地配置数据信息
  1110. if (ret_value != VPC3_CFG_FAULT)
  1111. {
  1112. ret_value = VPC3_CFG_UPDATE;
  1113. }
  1114. }
  1115. }
  1116.     if( ret_value == VPC3_CFG_OK || ret_value == VPC3_CFG_UPDATE)
  1117.     {
  1118. g_nCheckCfgCount[dev_num]++; //累计检查配置次数
  1119.         user_alarm(dev_num, USER_TYPE_CFG_OK, 0, (ALARM_STATUS_PDU_PTR) NULL_PTR, FALSE );
  1120.         user[dev_num].event |= VPC3_EV_NEW_CFG_DATA;
  1121.     }
  1122.     /*
  1123.     else
  1124.         {
  1125.             user_alarm(dev_num, USER_TYPE_CFG_NOK, status_diag.user_data[ 0 ], (ALARM_STATUS_PDU_PTR) NULL_PTR, FALSE );
  1126.         }//else of if( ret_value == VPC3_CFG_UPDATE )*/
  1127.     
  1128.     user_alarm_init(dev_num);
  1129. return ret_value;
  1130. }
  1131. #endif
  1132. /*---------------------------------------------------------------------------*/
  1133. /* function: user_isr_go_leave_data_ex                                       */
  1134. /*---------------------------------------------------------------------------*/
  1135. #if( ISR_ENABLE_VPC3_INT_GO_LEAVE_DATA_EX == 1 )
  1136. SECTION_DEF_CommuCode void user_isr_go_leave_data_ex(UBYTE dev_num)
  1137. {
  1138. if( VPC3_GET_DP_STATE(dev_num) != DATA_EX )
  1139.     {
  1140.         user[dev_num].state = USER_STATE_CLEAR;
  1141.     user[dev_num].application_ready = FALSE;
  1142.     }
  1143. // TRACE("user_isr_go_leave_data_ex %d", dev_num);
  1144. }
  1145. #endif
  1146. /*---------------------------------------------------------------------------*/
  1147. /* function: user_isr_dx_out                                                 */
  1148. /*---------------------------------------------------------------------------*/
  1149. #if( ISR_ENABLE_VPC3_INT_DX_OUT == 1 )
  1150. SECTION_DEF_CommuCode void user_isr_dx_out(UBYTE dev_num)
  1151. {
  1152. user[dev_num].event |= VPC3_EV_DX_OUT;
  1153. }
  1154. #endif
  1155. /*---------------------------------------------------------------------------*/
  1156. /* function: user_isr_diag_buf_changed                                       */
  1157. /*---------------------------------------------------------------------------*/
  1158. #if( ISR_ENABLE_VPC3_INT_DIAG_BUF_CHANGED == 1 )
  1159. SECTION_DEF_CommuCode void user_isr_diag_buf_changed(UBYTE dev_num)
  1160. {
  1161.     // diagnosis buffer has been changed
  1162.     user[dev_num].user_diag_active = FALSE;
  1163.     // Fetch new diagnosis buffer
  1164.     dp_sys[dev_num].diag_buf_ptr.byte_ptr = vpc3_get_diagbufptr(dev_num);
  1165. }
  1166. #endif
  1167. /*---------------------------------------------------------------------------*/
  1168. /* function: user_isr_new_wd_timeout                                         */
  1169. /*---------------------------------------------------------------------------*/
  1170. #if( ISR_ENABLE_VPC3_INT_WD_DP_TIMEOUT == 1 )
  1171. SECTION_DEF_CommuCode void user_isr_new_wd_dp_timeout(UBYTE dev_num)
  1172. {
  1173. reset_diagnostic_buffer(dev_num);
  1174. }
  1175. #endif
  1176. /*---------------------------------------------------------------------------*/
  1177. /* function: user_isr_mac_reset                                              */
  1178. /*---------------------------------------------------------------------------*/
  1179. #if( ISR_ENABLE_VPC3_INT_MAC_RESET == 1 )
  1180. #define CS_SET_TIME             0x01
  1181. #define CS_Clock_Sync_Violation 0X02 
  1182. SECTION_DEF_CommuCode void user_isr_mac_reset(UBYTE dev_num)
  1183. {
  1184. #ifdef DP_TIMESTAMP
  1185. TDateTime fullDt;
  1186. TClockSync* pCS = &p_Vpc3[dev_num]->cs;
  1187. uint64  ullVal;
  1188. uint32 nRcvDelay;//接收延时
  1189. uint32 nTotalSec;
  1190. uint32 nTotalMs;
  1191. user[dev_num].event |= VPC3_EV_MAC_RESET;
  1192. //用于时钟同步功能
  1193.     if (p_Vpc3[dev_num]->mode_reg0_L & 0x80)//启用了时钟同步
  1194.     {
  1195. if (g_ProfibusRam.bClockSyncEnabled)
  1196. {
  1197. //判断时钟状态
  1198. if (p_Vpc3[dev_num]->cs.clock.status &  CS_SET_TIME)
  1199. {
  1200. nTotalSec = MAKEDWORD(MAKEWORD(pCS->clock.cv_time_event[3],pCS->clock.cv_time_event[2]),MAKEWORD(pCS->clock.cv_time_event[1],pCS->clock.cv_time_event[0]));
  1201. ullVal = MAKEDWORD(MAKEWORD(pCS->clock.cv_time_event[3+4],pCS->clock.cv_time_event[2+4]),MAKEWORD(pCS->clock.cv_time_event[1+4],pCS->clock.cv_time_event[0+4])) ;
  1202. ullVal *= 1000000;
  1203. nRcvDelay = MAKEDWORD(MAKEWORD(pCS->clock.rd_time[3],pCS->clock.rd_time[2]),MAKEWORD(pCS->clock.rd_time[1],pCS->clock.rd_time[0]));
  1204. nTotalMs = ( (ullVal >> 32) + nRcvDelay) / 1000;
  1205. if(nTotalMs > 1000)
  1206. {
  1207. nTotalSec+=nTotalMs/1000;
  1208. nTotalMs%=1000;
  1209. }
  1210. //将Profibus时间戳格式转换成全时间格式
  1211. fullDt = _TimeFromSeconds(nTotalSec);
  1212. fullDt.ms = nTotalMs;
  1213. //校正系统时钟 
  1214. fullDt.y %= 100; //2000年开始范围 <= 99
  1215. SetDeviceSystemTime(&fullDt);
  1216. }
  1217. else if(p_Vpc3[dev_num]->cs.clock.status &  CS_Clock_Sync_Violation)
  1218. {
  1219. // TRACE("Clock_Sync_Violation");
  1220. }
  1221. }
  1222.     }
  1223. else
  1224. {
  1225. // TRACE("user_isr_mac_reset");
  1226. }
  1227. #endif
  1228. }
  1229. #endif
  1230. /*---------------------------------------------------------------------------*/
  1231. /* function: user_isr_baudrate_detect                                        */
  1232. /*---------------------------------------------------------------------------*/
  1233. #if( ISR_ENABLE_VPC3_INT_BAUDRATE_DETECT == 1 )
  1234. SECTION_DEF_CommuCode void user_isr_baudrate_detect(UBYTE dev_num)
  1235. {
  1236. //not used in our application
  1237. }
  1238. #endif
  1239. /*---------------------------------------------------------------------------*/
  1240. /* function: user_isr_new_gc_command                                         */
  1241. /*---------------------------------------------------------------------------*/
  1242. #if( ISR_ENABLE_VPC3_INT_NEW_GC_COMMAND == 1 )
  1243. SECTION_DEF_CommuCode void user_isr_new_gc_command(UBYTE dev_num)
  1244. {
  1245. //not used in our application
  1246. }
  1247. #endif
  1248. /*---------------------------------------------------------------------------*/
  1249. /* function: user_isr_new_ssa_data                                           */
  1250. /*---------------------------------------------------------------------------*/
  1251. #if( ISR_ENABLE_VPC3_INT_NEW_SSA_DATA == 1 )
  1252. SECTION_DEF_CommuCode void user_isr_new_ssa_data(UBYTE dev_num)
  1253. {
  1254. //not used in our application
  1255. VPC3_STRUC_SSA_BLOCK_PTR ssa_ptr;
  1256. ssa_ptr = (VPC3_STRUC_SSA_BLOCK_PTR) VPC3_GET_SSA_BUF_PTR(dev_num);
  1257. //一般不使用该功能
  1258. SetSlaveAddressReq(ssa_ptr->address);
  1259. }
  1260. #endif
  1261. /*---------------------------------------------------------------------------*/
  1262. /* function: user_isr_user_timer_clock (10ms)                                */
  1263. /*---------------------------------------------------------------------------*/
  1264. #if( ISR_ENABLE_VPC3_INT_USER_TIMER_CLOCK == 1 )
  1265. SECTION_DEF_CommuCode void user_isr_user_timer_clock(UBYTE dev_num)
  1266. {
  1267. //not used in our application
  1268. }
  1269. #endif
  1270. #ifdef DP_SUBSCRIBER
  1271. /*---------------------------------------------------------------------------*/
  1272. /* function: user_isr_dxb_out                                                */
  1273. /*---------------------------------------------------------------------------*/
  1274. #if( ISR_ENABLE_VPC3_INT_DXB_OUT == 1 )
  1275. SECTION_DEF_CommuCode void user_isr_dxb_out(UBYTE dev_num)
  1276. {
  1277. user[dev_num].event |= VPC3_EV_DXB_OUT;
  1278. }
  1279. #endif
  1280. /*---------------------------------------------------------------------------*/
  1281. /* function: user_isr_dxb_link_error                                         */
  1282. /*---------------------------------------------------------------------------*/
  1283. #if( ISR_ENABLE_VPC3_INT_DXB_LINK_ERROR == 1 )
  1284. SECTION_DEF_CommuCode void user_isr_dxb_link_error(UBYTE dev_num)
  1285. {
  1286. user[dev_num].event |= VPC3_EV_DXB_LINK_ERROR;
  1287. }
  1288. #endif
  1289. #endif//#ifdef DP_SUBSCRIBER
  1290. #ifdef DP_ALARM
  1291. /*--------------------------------------------------------------------------*/
  1292. /* user_alarm_state_info (UBYTE dev_num,called by DPV1)                                   */
  1293. /*--------------------------------------------------------------------------*/
  1294. SECTION_DEF_CommuCode void user_alarm_state_info(UBYTE dev_num, UBYTE alarm_type_bit_field, UBYTE sequence_depth )
  1295. {
  1296. UBYTE i;
  1297.     sequence_depth = sequence_depth;
  1298.     if( alarm_type_bit_field == ALARM_TYPE_NONE_VALUE )
  1299.     {
  1300.         //alarmstatemachine stopped, remove pending alarms
  1301.         for( i = 0; i <= ALARM_MAX_FIFO; i++ )
  1302.         {
  1303.             if( user[dev_num].al.buffer_used[i] == TRUE )
  1304.             {
  1305.                 user[dev_num].al.buffer_used[i] = FALSE;
  1306.             }//if( user[dev_num].al.buffer_used[i] == TRUE )
  1307.         }//for( i = 0; i <= ALARM_MAX_FIFO; i++ )
  1308.         clear_alarm(dev_num, ALARM_TYPE_ALL_VALUE, SEQUENCE_NUMBER_ALL );
  1309.         user[dev_num].al.state = USER_AL_STATE_CLOSED;
  1310.     }//if( alarm_type_bit_field == 0 )
  1311.     else
  1312.     {
  1313.      //enable setted alarms
  1314.         user[dev_num].al.state = USER_AL_STATE_OPEN;
  1315.     }
  1316. }
  1317. /*--------------------------------------------------------------------------*/
  1318. /* user_alarm_ack_req (UBYTE dev_num,called by DPV1)                                      */
  1319. /*--------------------------------------------------------------------------*/
  1320. SECTION_DEF_CommuCode void user_alarm_ack_req(UBYTE dev_num, ALARM_STATUS_PDU_PTR alarm_ptr )//SOE ACK---DPV1
  1321. {
  1322.     logMsg("receive user_alarm_ack_req!n",0,0,0,0,0,0);
  1323.     // alarm_ack from master received
  1324.     // todo: do your alarm-acknowledge handling here
  1325. user_free_alarm_buffer(dev_num, alarm_ptr );
  1326. AlarmACK(dev_num);//报警确认
  1327. reset_diagnostic_buffer(dev_num);
  1328. }
  1329. #endif // #ifdef DP_ALARM
  1330. #ifdef DP_MSAC_C2
  1331. /*--------------------------------------------------------------------------*/
  1332. /* msac_c2_initiate_req (UBYTE dev_num,called by DPV1)                                    */
  1333. /*--------------------------------------------------------------------------*/
  1334. SECTION_DEF_CommuCode DPV1_RET_VAL user_c2_initiate_req(UBYTE dev_num, UBYTE sap, DPV1_PTR pdu )
  1335. {
  1336. MSAC_C2_INITIATE_SUB_PARAM PTR_ATTR * d_addr_ptr;
  1337. DPV1_RET_VAL ret_value;
  1338.     sap = sap;  //avoid warning
  1339. #ifdef DPV1_IM_SUPP
  1340. user[dev_num].awImIndex[ (sap & 0x0F ) ] = 0x0000;
  1341. #endif//#ifdef DPV1_IM_SUPP
  1342. #ifdef _KEIL_C51
  1343.     d_addr_ptr=(MSAC_C2_INITIATE_SUB_PARAM PTR_ATTR * ) &(pdu->initiate_req.addr_data + BFWORD2BYTE(pdu->initiate_req.s_len)); //C51
  1344. #else
  1345.     d_addr_ptr=(MSAC_C2_INITIATE_SUB_PARAM PTR_ATTR* )(pdu->initiate_req.addr_data + BFWORD2BYTE(pdu->initiate_req.s_len)); //166
  1346. #endif //#ifdef _KEIL_C51
  1347.     if( BFWORD2BYTE(d_addr_ptr->api) == 0 )
  1348.     {
  1349.         // in this example only API 0 is supported */
  1350.         if( msac_c2_initiate_req_to_res(dev_num,pdu) == DPV1_OK)
  1351.         {
  1352. //如果界面允许可以跟踪子站所接收的连接情况
  1353. EDD_C2_InitiateReq(dev_num);
  1354.             // Initiate-Request-PDU has been changed to Initiate-Response-PDU */
  1355.             ret_value = DPV1_OK;
  1356.         }
  1357.         else
  1358.         {
  1359.             // D-Typ is no endpoint of connection 
  1360.             // user must handle Initiate-PDU (no change by MSAC_C2)
  1361.             // in this example slave must always be endpoint
  1362.             // create negative response 
  1363.             pdu->neg.err_code1 = DPV1_ERRCL_APPLICATION | DPV1_ERRCL_APP_NOTSUPP;
  1364.             pdu->neg.err_code2 = 0x00;
  1365.             ret_value = DPV1_NOK;
  1366.         }
  1367.     }
  1368.     else
  1369.     {
  1370.         /* create ABORT-PDU */
  1371.         pdu->abort.subnet = MSAC_C2_SUBNET_NO;
  1372.         pdu->abort.instance_reason = MSAC_C2_INSTANCE_MSAC_C2 | MSAC_C2_ABT_IA;
  1373.         ret_value = DPV1_ABORT;
  1374.     }
  1375.     return ret_value;
  1376. }//DPV1_RET_VAL user_c2_initiate_req(UBYTE dev_num, UBYTE sap, DPV1_PTR pdu )
  1377. /*--------------------------------------------------------------------------*/
  1378. /* user_c2_data_transport_req (UBYTE dev_num,called by DPV1)                              */
  1379. /*--------------------------------------------------------------------------*/
  1380. SECTION_DEF_CommuCode DPV1_RET_VAL user_c2_data_transport_req(UBYTE dev_num, UBYTE sap, DPV1_PTR pdu )
  1381. {
  1382. DPV1_RET_VAL ret_value;
  1383. int nBlackfinLoop;
  1384. //uint16* pBlackfinAddr;
  1385. uint16* pSrcAddr;
  1386.     if( sap == DP_C1_RD_WR_SAP_NR )
  1387.     {
  1388.      pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_PARAM;
  1389.         pdu->neg.err_code2 = 0x00;
  1390.         ret_value = DPV1_NOK;
  1391. }//if( sap == DP_C1_RD_WR_SAP_NR )
  1392. else
  1393. {
  1394. if( BFWORD2BYTE( pdu->transport.length) > (C2_LEN-4) )
  1395. {
  1396. pdu->transport.length = (C2_LEN-4);
  1397. }
  1398.      if( BFWORD2BYTE(pdu->transport.slot_num) == 0x00 )
  1399.      {
  1400.          if( BFWORD2BYTE(pdu->transport.index) == 0x02 )
  1401.          {
  1402.              //memcpy( &user[dev_num].dpv1_rw_buffer[0], &pdu->transport.pdu_data[0], pdu->transport.length );
  1403. pSrcAddr = (uint16*)&pdu->transport.pdu_data[0];
  1404. for (nBlackfinLoop=0; nBlackfinLoop<BFWORD2BYTE(pdu->transport.length); nBlackfinLoop++)
  1405. {
  1406. user[dev_num].dpv1_rw_buffer[nBlackfinLoop] = pSrcAddr[nBlackfinLoop];
  1407. }
  1408.              ret_value = DPV1_OK;
  1409.          }//if( pdu->transport.index == 0x00 )
  1410.          else
  1411.          {
  1412.              pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_INDEX;
  1413.              pdu->neg.err_code2 = 0x00;
  1414.              ret_value = DPV1_NOK;
  1415.          }//else of if( pdu->v.index == 0x00 )
  1416.      }//if( pdu->transport.slot_num == 0x00 )
  1417.      else
  1418.      {
  1419.          pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_SLOT;
  1420.          pdu->neg.err_code2 = 0x00;
  1421.          ret_value = DPV1_NOK;
  1422.      }//else of if( pdu->transport.slot_num == 0x00 )
  1423. }//else of if( sap == DP_C1_RD_WR_SAP_NR )
  1424.     return ret_value;
  1425. }//DPV1_RET_VAL user_c2_data_transport_req(UBYTE dev_num,UBYTE sap, DPV1_PTR pdu)
  1426. /*--------------------------------------------------------------------------*/
  1427. /* dpv1_abort_ind (UBYTE dev_num,called by DPV1)                                          */
  1428. /*--------------------------------------------------------------------------*/
  1429. SECTION_DEF_CommuCode DPV1_RET_VAL user_c2_abort_ind(UBYTE dev_num,UBYTE sap, DPV1_PTR pdu)
  1430. {
  1431.     sap = sap;  //avoid warning
  1432.     pdu = pdu;  //avoid warning
  1433. #ifdef DPV1_IM_SUPP
  1434. user[dev_num].awImIndex[ (sap & 0x0F ) ] = 0x0000;
  1435. #endif//#ifdef DPV1_IM_SUPP
  1436. EDD_C2_AbortInd(dev_num);
  1437.     return DPV1_OK;
  1438. }//DPV1_RET_VAL user_c2_abort_ind(UBYTE dev_num,UBYTE sap, DPV1_PTR pdu)
  1439. #endif //#ifdef DP_MSAC_C2
  1440. #ifdef DP_FDL
  1441. /*--------------------------------------------------------------------------*/
  1442. /* user_dpv1_read_req (called by DPV1)                                      */
  1443. /*--------------------------------------------------------------------------*/
  1444. SECTION_DEF_CommuCode DPV1_RET_VAL user_dpv1_read_req(UBYTE dev_num, UBYTE sap, DPV1_PTR pdu)//dpv1 read complete the task data from user to buffer 
  1445. {
  1446. DPV1_RET_VAL  ret_value;
  1447. UBYTE         max_length=0;
  1448. //int nBlackfinLoop;
  1449. //uint16* pBlackfinAddr;
  1450.    int i;
  1451.    uint8* pDataRet;
  1452.    uint8 nDataLenRet;
  1453.    uint8 nErrCode1, nErrCode2;
  1454.    g_nC1ReadCount[dev_num]++;
  1455.    ret_value = DPV1_OK;
  1456. #ifdef DPV1_IM_SUPP
  1457. psIMCALL ps_ImCall;
  1458. #endif//#ifdef DPV1_IM_SUPP
  1459. #ifdef DPV1_IM_SUPP
  1460. if( BFWORD2BYTE(pdu->read.index) == 255 )
  1461. {
  1462. if( BFWORD2BYTE(pdu->read.slot_num) == 0 )
  1463. {
  1464. //check_extended function number, reserved byte and IM_INDEX
  1465. if( user[dev_num].awImIndex[ ( sap & 0x0F ) ] == IM0_INDEX )
  1466. {
  1467. ps_ImCall = (psIMCALL)&pdu->read.pdu_data[0];
  1468. ps_ImCall->bExtendedFunctionNumber = IM_FN_CALL;  
  1469. ps_ImCall->bReserved = 0x00;
  1470. ps_ImCall->wIndex = BFWORD2LONG(user[dev_num].awImIndex[ ( sap & 0x0F ) ]);
  1471.     //memcpy( &pdu->read.pdu_data[4], &IM_DEFAULT[0], SizeOfIM0 );
  1472.                     pBlackfinAddr = (uint16*)&pdu->read.pdu_data[4];
  1473. for (nBlackfinLoop=0; nBlackfinLoop<SizeOfIM0; nBlackfinLoop++)
  1474. {
  1475. pBlackfinAddr[nBlackfinLoop] = IM_DEFAULT[nBlackfinLoop];
  1476. }
  1477.      if( BFWORD2BYTE(pdu->read.length) > (4 + SizeOfIM0) )
  1478.      {
  1479.     pdu->read.length = 4 + SizeOfIM0;
  1480.      }
  1481. }//if( user.awImIndex[ ( sap & 0x0F ) ] == IM0_INDEX )
  1482. else
  1483. {
  1484.           pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_PARAM;
  1485.           pdu->neg.err_code2 = 0x00;
  1486.           ret_value = DPV1_NOK;
  1487. }//else of if( user.awImIndex[ ( sap & 0x0F ) ] == IM0_INDEX )  
  1488. }//if( pdu->read.slot_num == 0 )
  1489. else
  1490. {
  1491.          pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_SLOT;
  1492.          pdu->neg.err_code2 = 0x00;
  1493.          ret_value = DPV1_NOK;
  1494.          }//else of if( pdu->read.slot_num == 0 )
  1495. }
  1496. #else
  1497. if( BFWORD2BYTE(pdu->read.index) == 255 )
  1498. {
  1499.          pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_INDEX;
  1500.          pdu->neg.err_code2 = 0x00;
  1501.          ret_value = DPV1_NOK;
  1502. }
  1503. #endif//#ifdef DPV1_IM_SUPP
  1504. //应用处理部分
  1505. else if(ret_value == DPV1_OK)
  1506. {
  1507. switch(BFWORD2BYTE( pdu->read.slot_num ))
  1508. {
  1509.             case SlotNum_Read_BhPara:
  1510.             case SlotNum_Read_Energy:
  1511.             case SlotNum_Read_WaveConfig:
  1512.             case SlotNum_Read_WaveData_File_Zone0:
  1513.             case SlotNum_Read_WaveData_File_Zone1:
  1514.             case SlotNum_Read_WaveData_File_Zone2:
  1515.             case SlotNum_Read_WaveData_File_Zone3:
  1516.             case SlotNum_Read_WaveData_Sample_Zone0:
  1517.             case SlotNum_Read_WaveData_Sample_Zone1:
  1518.             case SlotNum_Read_WaveData_Sample_Zone2:
  1519.             case SlotNum_Read_WaveData_Sample_Zone3:
  1520.             case SlotNum_Read_WaveData_Sample_Zone4:
  1521.             case SlotNum_Read_WaveData_Sample_Zone5:
  1522.             case SlotNum_Read_BhPara_Zone0:
  1523.             case SlotNum_Read_BhPara_Zone1:
  1524.             case SlotNum_Read_BhPara_Zone2:
  1525.             case SlotNum_Read_BhPara_Zone3:
  1526.             case SlotNum_Read_BhPara_Zone4:
  1527.             case SlotNum_Read_BhPara_Zone5:
  1528. case SlotNum_Read_DeviceInfo:
  1529. case SlotNum_Read_WaveData_Comtrade_Zone0:
  1530. case SlotNum_Read_WaveData_Comtrade_Zone1:
  1531. case SlotNum_Read_WaveData_Comtrade_Zone2:
  1532. case SlotNum_Read_WaveData_Comtrade_Zone3:
  1533. case SlotNum_Read_WaveData_Comtrade_Zone4:
  1534. case SlotNum_Read_WaveData_Comtrade_Zone5:
  1535. case SlotNum_Read_WaveData_Comtrade_Zone6:
  1536. case SlotNum_Read_WaveData_Comtrade_Zone7:
  1537. case SlotNum_Read_WaveData_Comtrade_Zone8:
  1538. case SlotNum_50:
  1539. case SlotNum_51:
  1540. case SlotNum_52:
  1541. case SlotNum_53:
  1542. case SlotNum_54:
  1543. case SlotNum_55:
  1544. {
  1545. ret_value = DPV1_ReadReq(dev_num, pdu->read.slot_num&0xff, pdu->read.index&0xff, pdu->read.length&0xff, &pDataRet, &nDataLenRet,
  1546. &nErrCode1, &nErrCode2);
  1547. if(ret_value == DPV1_OK && pDataRet)
  1548. {
  1549. for(i=0; i<mmin(MAX_DPV1_DU_COUNT, nDataLenRet); i++)
  1550. {
  1551. pdu->read.pdu_data[i] = pDataRet[i];
  1552. }
  1553. max_length = nDataLenRet;
  1554. if( BFWORD2BYTE(pdu->read.length) > max_length )
  1555. {
  1556. pdu->read.length = max_length;
  1557. }
  1558. }
  1559. else
  1560. {
  1561. pdu->neg.err_code1 = nErrCode1;
  1562. pdu->neg.err_code2 = nErrCode2;
  1563. }
  1564. }
  1565. break;
  1566. default:
  1567. {
  1568. pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_SLOT;
  1569. pdu->neg.err_code2 = 0x00;
  1570. ret_value = DPV1_NOK;
  1571. break;
  1572. }
  1573. }
  1574. }
  1575.     return ret_value;
  1576. }//DPV1_RET_VAL user_dpv1_read_req(UBYTE sap, DPV1_PTR pdu)
  1577. /*--------------------------------------------------------------------------*/
  1578. /* user_dpv1_write_req (called by DPV1)                                     */
  1579. /*--------------------------------------------------------------------------*/
  1580. SECTION_DEF_CommuCode DPV1_RET_VAL user_dpv1_write_req(UBYTE dev_num, UBYTE sap, DPV1_PTR pdu)
  1581. {
  1582. DPV1_RET_VAL ret_value;
  1583. //UBYTE        max_length;
  1584. #ifdef DPV1_IM_SUPP
  1585. psIMCALL ps_ImCall;
  1586. #endif//#ifdef DPV1_IM_SUPP
  1587.    int i;
  1588.    uint8 nErrCode1, nErrCode2;
  1589.    SECTION_DEF_CommuData static uint8 WriteBuffer[240];
  1590.    g_nC1WriteCount[dev_num]++;
  1591.    ret_value = DPV1_OK;
  1592. #ifdef DPV1_IM_SUPP
  1593. if( BFWORD2BYTE(pdu->write.index) == 255 )
  1594. {
  1595. if( BFWORD2BYTE(pdu->write.slot_num) == 0 )
  1596. {
  1597. //check_extended function number, reserved byte and IM_INDEX
  1598. ps_ImCall = (psIMCALL)&pdu->write.pdu_data[0];
  1599. if(    ( BFWORD2BYTE(ps_ImCall->bExtendedFunctionNumber) == IM_FN_CALL )
  1600. && ( BFWORD2BYTE(ps_ImCall->bReserved) == 0x00                     )
  1601.   )
  1602. {
  1603. //check IM_INDEX
  1604. if( BFLONG2WORD(ps_ImCall->wIndex) == IM0_INDEX )
  1605. {
  1606. //OK, save IM_INDEX
  1607. user[dev_num].awImIndex[ ( sap & 0x0F ) ] = IM0_INDEX;
  1608. }//if( ps_ImCall->wIndex == IM0_INDEX )
  1609. else
  1610. {
  1611.           pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_APP_NOTSUPP;
  1612.           pdu->neg.err_code2 = 0x00;
  1613.           ret_value = DPV1_NOK;
  1614. }//else of if( ps_ImCall->wIndex == IM0_INDEX )
  1615. }//if(    ( ps_ImCall->bExtendedFunctionNumber == IM_FN_CALL ) ...
  1616. else
  1617. {
  1618.           pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_PARAM;
  1619.           pdu->neg.err_code2 = 0x00;
  1620.           ret_value = DPV1_NOK;
  1621. }//else of if(    ( ps_ImCall->bExtendedFunctionNumber == IM_FN_CALL ) ...   
  1622. }//if( pdu->write.slot_num == 0 )
  1623. else
  1624. {
  1625.          pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_SLOT;
  1626.          pdu->neg.err_code2 = 0x00;
  1627.          ret_value = DPV1_NOK;
  1628.          }//if( pdu->write.slot_num == 0 )
  1629. }
  1630. #else
  1631. if( BFWORD2BYTE(pdu->write.index) == 255 )
  1632. {
  1633.          pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_INDEX;
  1634.          pdu->neg.err_code2 = 0x00;
  1635.          ret_value = DPV1_NOK;
  1636. }
  1637. #endif//#ifdef DPV1_IM_SUPP
  1638.         else 
  1639. {
  1640. switch( BFWORD2BYTE(pdu->write.slot_num ))
  1641. {
  1642.             case SlotNum_Write_BhPara:
  1643.             case SlotNum_Write_StartWave:
  1644.             case SlotNum_Write_ModifyTime:
  1645.             case SlotNum_Write_CtrlSelect:
  1646.             case SlotNum_Write_CtrlExecute:
  1647. case SlotNum_Write_CtrlCancel:
  1648.             case SlotNum_Write_ResetSignal:
  1649.             case SlotNum_Write_BhPara_Zone0:
  1650. case SlotNum_Write_BhPara_Zone1:
  1651.             case SlotNum_Write_BhPara_Zone2:
  1652.             case SlotNum_Write_BhPara_Zone3:
  1653.             case SlotNum_Write_BhPara_Zone4:
  1654. case SlotNum_Write_BhPara_Zone5:
  1655. case SlotNum_50:
  1656. case SlotNum_51:
  1657. case SlotNum_52:
  1658. case SlotNum_53:
  1659. case SlotNum_54:
  1660. case SlotNum_55:
  1661. {
  1662. //转换成uint8型数据
  1663. for(i=0; i<BFWORD2BYTE(pdu->write.length); i++)
  1664. {
  1665. WriteBuffer[i] = pdu->write.pdu_data[i];
  1666. }
  1667. ret_value = DPV1_WriteReq(dev_num, pdu->write.slot_num&0xff, pdu->write.index&0xff, pdu->write.length&0xff, WriteBuffer, &nErrCode1, &nErrCode2);
  1668. if(ret_value == DPV1_NOK)
  1669. {
  1670. pdu->neg.err_code1 = nErrCode1;
  1671. pdu->neg.err_code2 = nErrCode2;
  1672. }
  1673. }
  1674. break;
  1675. default:
  1676. {
  1677. pdu->neg.err_code1 = DPV1_ERRCL_ACCESS | DPV1_ERRCL_ACC_INV_SLOT;
  1678. pdu->neg.err_code2 = 0x00;
  1679. ret_value = DPV1_NOK;
  1680. break;
  1681. }
  1682. }
  1683. }
  1684. return ret_value;
  1685. }//DPV1_RET_VAL user_dpv1_write_req(UBYTE sap, DPV1_PTR pdu)
  1686. #endif // #ifdef DP_FDL