iec101.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*******************************************************************
  2. Copyright (C):
  3. File name : Iec101.h
  4. DESCRIPTION :
  5. AUTHOR :
  6. Version : 1.0
  7. Date : 2014/07/23
  8. Others :
  9. History :
  10. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  11. 1) Date: 2014/07/23 Author: ChenDajie
  12. content:
  13. *******************************************************************/
  14. #ifndef _IEC101_H
  15. #define _IEC101_H
  16. #include "PRIO_QUEUE_Iec10x.h"
  17. #include "iec10x.h"
  18. #include "project_var.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #ifdef IEC101_STM32
  22. #define ARM_PACK __packed
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. /*
  28. *********************************************************************************************************
  29. * FUNCTION CODE
  30. *********************************************************************************************************
  31. */
  32. /*
  33. * down
  34. * */
  35. #define IEC101_CTRL_RESET_LINK 0
  36. #define IEC101_CTRL_PULSE 2
  37. #define IEC101_CTRL_SEND_USR_DATA_ACK 3
  38. #define IEC101_CTRL_SEND_DATA 4
  39. #define IEC101_CTRL_REQ_LINK_STATUS 9
  40. /*
  41. * up
  42. * */
  43. #define IEC101_CTRL_RES_CONFIRM 0x00
  44. #define IEC101_CTRL_RES_LINK_STATUS 0x0B
  45. /*
  46. * DIR, PRM, FCB, FCV, ACD, DFC
  47. * */
  48. #define IEC101_CTRL_DIR_DOWN 0x01
  49. #define IEC101_CTRL_DIR_UP 0x00
  50. #define IEC101_CTRL_FCB_OPPO_BIT 0x01
  51. #define IEC101_CTRL_FCB_OPPO_NONE 0x00
  52. #define IEC101_CTRL_PRM_MASTER 0x01
  53. #define IEC101_CTRL_PRM_SLAVE 0x00
  54. #define IEC101_CTRL_FCV_ENABLE 0x01
  55. #define IEC101_CTRL_FCV_DISABLE 0x00
  56. #define IEC101_CTRL_ACD_WITH_DATA 0x01
  57. #define IEC101_CTRL_ACD_NONE_DATA 0x00
  58. #define IEC101_CTRL_DFC_CAN_REC 0x00
  59. #define IEC101_CTRL_DFC_CANNOT_REC 0x01
  60. /*
  61. *********************************************************************************************************
  62. * FRAME VALUE
  63. *********************************************************************************************************
  64. */
  65. #define IEC101_STABLE_BEGING 0X10
  66. #define IEC101_STABLE_END 0X16
  67. #define IEC101_STABLE_LEN 0X06
  68. /*
  69. * variable
  70. * */
  71. #define IEC101_VARIABLE_BEGING 0X68
  72. #define IEC101_VARIABLE_END 0X16
  73. /*
  74. * length of variable
  75. * */
  76. #define IEC101_VARIABLE_HEAD_LEN (sizeof(IEC101_68_T) - 1)
  77. #define IEC101_VARIABLE_ASDU_LEN (sizeof(IEC10X_ASDU_T) - 1)
  78. #define IEC101_VARIABLE_INFO_LEN (sizeof(ASDU_INFO_T) - 1)
  79. #define IEC101_VARIABLE_LEN (IEC101_VARIABLE_HEAD_LEN + IEC101_VARIABLE_ASDU_LEN + IEC101_VARIABLE_INFO_LEN + 2) /*add cs+end*/
  80. #pragma pack(1)
  81. #define IEC101_MAX_BUF_LEN 256
  82. /*
  83. * Control Code
  84. * */
  85. typedef __packed struct
  86. {
  87. uint8_t _func : 4; /* function */
  88. uint8_t _fcv : 1;
  89. uint8_t _fcb : 1; /* Frame calculate bit */
  90. uint8_t _prm : 1; /* 1:from start station, 0:from end station */
  91. uint8_t _dir : 1;
  92. } CTRL_DOWN_T;
  93. typedef __packed struct
  94. {
  95. uint8_t _func : 4; /*function*/
  96. uint8_t _dfc : 1;
  97. uint8_t _acd : 1;
  98. uint8_t _prm : 1;
  99. uint8_t _dir : 1;
  100. } CTRL_UP_T;
  101. typedef __packed union
  102. {
  103. CTRL_UP_T up;
  104. CTRL_DOWN_T down;
  105. uint8_t val;
  106. } CTRL_T, *PCTRL_T;
  107. /*
  108. * regular frame head:10
  109. * */
  110. typedef __packed struct
  111. {
  112. uint8_t _begin;
  113. CTRL_T _ctrl;
  114. uint16_t _addr;
  115. uint8_t _cs;
  116. uint8_t _end;
  117. } IEC101_10_T, *PIEC101_10_T;
  118. /*
  119. * variable fram head:68
  120. * */
  121. typedef __packed struct
  122. {
  123. uint8_t _begin;
  124. uint8_t _len;
  125. uint8_t _len_cfm;
  126. uint8_t _begin_cfm;
  127. CTRL_T _ctrl;
  128. uint16_t _addr;
  129. uint8_t _asdu[1];
  130. } IEC101_68_T, *PIEC101_68_T;
  131. #pragma pack()
  132. /* IEC101 STATE-MACHINE */
  133. enum
  134. {
  135. IEC101_FLAG_LINK_CLOSED,
  136. IEC101_FLAG_LINK_IDLE,
  137. IEC101_FLAG_INIT_LINK,
  138. IEC101_FLAG_RESET_LINK,
  139. IEC101_FLAG_REQ_LINK,
  140. IEC101_FLAG_RESET_REMOTE_LINK,
  141. IEC101_FLAG_INIT_FIN,
  142. IEC101_FLAG_CALL_ACT,
  143. IEC101_FLAG_CALL_ACT_FIN,
  144. IEC101_FLAG_CALL_ACT_RET,
  145. IEC101_FLAG_CALL_SIG_TOTAL,
  146. IEC101_FLAG_CALL_DET_TOTAL,
  147. IEC101_FLAG_DELAY_ACT,
  148. IEC101_FLAG_CLOCK_SYS,
  149. IEC101_FLAG_PULSE,
  150. IEC101_FLAG_CALL_GROUP,
  151. IEC101_FLAG_CONNECT_OK,
  152. };
  153. /*
  154. *********************************************************************************************************
  155. * GLOABLE VARIABLE
  156. *********************************************************************************************************
  157. */
  158. extern uint16_t IEC101_Pulse_Cnt;
  159. extern uint8_t IEC101_STATE_FLAG_INIT;
  160. /*
  161. *********************************************************************************************************
  162. * FUNCTION PROTOTYPES
  163. *********************************************************************************************************
  164. */
  165. void Iex101_main(uint8_t *buf, uint16_t len);
  166. uint8_t Iec101_StateMachine(void);
  167. uint8_t IEC101_BuildSignal_Spont(uint8_t TimeFlag, uint8_t signalV, uint16_t addrV);
  168. uint8_t IEC101_BuildDetect_Spont(uint8_t TimeFlag, PIEC10X_DETECT_T detectV, uint16_t addrV);
  169. uint8_t IEC101_BuildDetectF_Spont(uint8_t TimeFlag, float detectV, uint16_t addrV);
  170. #ifdef __cplusplus
  171. }
  172. #endif /*IEC101_STM32*/
  173. #endif
  174. #endif /*_IEC101_H*/