You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

161 rivejä
5.7 KiB

  1. #include "plsr_self_test.h"
  2. #include "plc_device.h"
  3. #include "plsr_core.h"
  4. #include "plsr_job.h"
  5. #include <string.h>
  6. /* 上电自测(验证后可删除):
  7. * - AB 模式使用 Q0(A)/Q1(B),其余用出厂默认参数(K1)
  8. * - 任务:3 段完整 AB 周期(H00 完成,顺序衔接):
  9. * 段1:2000Hz / +1000 周期(A 超前 B)
  10. * 段2:5000Hz / +6000 周期(A 超前 B)
  11. * 段3:1000Hz / -500 周期(B 超前 A,验证反向)
  12. * 数据源为静态数组,仅自测使用(正式 D 设备适配器见 Modbus 阶段)。 */
  13. #define SELF_TEST_WORD_CAPACITY (64U)
  14. #define SELF_TEST_S0_BASE (10U)
  15. #define SELF_TEST_S1_BASE (60U)
  16. #define SELF_TEST_DIR_POINT (4U)
  17. static uint16_t SelfTestWords[3][SELF_TEST_WORD_CAPACITY];
  18. static uint8_t SelfTestValidateWords(void *context,
  19. PLSR_DEVICE_TYPE device,
  20. uint32_t firstAddress,
  21. uint32_t wordCount)
  22. {
  23. (void)context;
  24. if ((device > PLSR_DEVICE_FD) || (wordCount == 0UL))
  25. {
  26. return 0U;
  27. }
  28. return (((uint64_t)firstAddress + wordCount)
  29. <= SELF_TEST_WORD_CAPACITY)
  30. ? 1U
  31. : 0U;
  32. }
  33. static uint8_t SelfTestReadWord(void *context,
  34. PLSR_DEVICE_TYPE device,
  35. uint32_t address,
  36. uint16_t *value)
  37. {
  38. (void)context;
  39. if ((device > PLSR_DEVICE_FD) || (value == NULL)
  40. || (address >= SELF_TEST_WORD_CAPACITY))
  41. {
  42. return 0U;
  43. }
  44. *value = SelfTestWords[device][address];
  45. return 1U;
  46. }
  47. static uint8_t SelfTestReadBit(void *context,
  48. PLSR_DEVICE_TYPE device,
  49. uint32_t address,
  50. uint8_t *value)
  51. {
  52. (void)context;
  53. (void)device;
  54. (void)address;
  55. *value = 0U;
  56. return 1U;
  57. }
  58. static void SelfTestWriteDword(PLSR_DEVICE_TYPE device,
  59. uint32_t address,
  60. uint32_t value)
  61. {
  62. SelfTestWords[device][address] = (uint16_t)(value & 0xFFFFUL);
  63. SelfTestWords[device][address + 1UL] = (uint16_t)(value >> 16U);
  64. }
  65. static void SelfTestWriteSfdDword(uint16_t address, uint32_t value)
  66. {
  67. (void)PlcDeviceWriteSfd(address, (uint16_t)(value & 0xFFFFUL));
  68. (void)PlcDeviceWriteSfd((uint16_t)(address + 1U),
  69. (uint16_t)(value >> 16U));
  70. }
  71. PLSR_RESULT PlsrSelfTestQueue(void)
  72. {
  73. PLSR_CALL call;
  74. (void)memset(SelfTestWords, 0, sizeof(SelfTestWords));
  75. /* 方向端子 Y4(PULSE/DIR 模式必需,接线参数)。 */
  76. (void)PlcDeviceWriteSfd(906U, SELF_TEST_DIR_POINT);
  77. /* S0:3 段,H00 完成,顺序跳转。 */
  78. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE, 3U);
  79. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 10U, 2000UL);
  80. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 12U, 1000UL);
  81. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 20U, 5000UL);
  82. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 22U, 6000UL);
  83. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 30U, 1000UL);
  84. SelfTestWriteDword(PLSR_DEVICE_D,
  85. SELF_TEST_S0_BASE + 32U,
  86. (uint32_t)(int32_t)-500);
  87. /* S1:相对模式,起始段 0(=段1)。 */
  88. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S1_BASE, 0U);
  89. (void)memset(&call, 0, sizeof(call));
  90. call.sequence = 0xA5A5UL;
  91. call.source.context = NULL;
  92. call.source.validateWords = SelfTestValidateWords;
  93. call.source.readWord = SelfTestReadWord;
  94. call.source.readBit = SelfTestReadBit;
  95. call.s0.device = PLSR_DEVICE_D;
  96. call.s0.address = SELF_TEST_S0_BASE;
  97. call.s1.device = PLSR_DEVICE_D;
  98. call.s1.address = SELF_TEST_S1_BASE;
  99. call.s2.type = PLSR_OPERAND_CONSTANT;
  100. call.s2.constant = 1;
  101. call.dAxis = 0U;
  102. call.outputModeOverride = PLSR_OUTPUT_AB;
  103. return PlsrPostCall(&call);
  104. }
  105. PLSR_RESULT PlsrEquivalentSelfTestQueue(void)
  106. {
  107. PLSR_CALL call;
  108. (void)memset(SelfTestWords, 0, sizeof(SelfTestWords));
  109. /* SFD900 Bit10~8=001(1um当量);3脉冲/2单位。 */
  110. (void)PlcDeviceWriteSfd(900U, (1U << 8U));
  111. SelfTestWriteSfdDword(902U, 3UL);
  112. SelfTestWriteSfdDword(904U, 2UL);
  113. (void)PlcDeviceWriteSfd(906U, SELF_TEST_DIR_POINT);
  114. (void)PlcDeviceWriteSfd(907U, 10U);
  115. /* 当量换算后物理最高速度=90000Hz,不超过硬件100kHz。 */
  116. SelfTestWriteSfdDword(956U, 60000UL);
  117. /* 板端可观察版本:两段各1001工程单位。
  118. * 3脉冲/2单位带余数换算后分别输出1501、1502脉冲,累计3003脉冲;
  119. * 1500Hz附近持续约2s,避免原3脉冲自检在逻辑分析仪启动前已经结束。 */
  120. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE, 2U);
  121. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 10U, 1000UL);
  122. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 12U, 1001UL);
  123. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 20U, 1000UL);
  124. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S0_BASE + 22U, 1001UL);
  125. SelfTestWriteDword(PLSR_DEVICE_D, SELF_TEST_S1_BASE, 0U);
  126. (void)memset(&call, 0, sizeof(call));
  127. call.sequence = 0xA5A6UL;
  128. call.source.context = NULL;
  129. call.source.validateWords = SelfTestValidateWords;
  130. call.source.readWord = SelfTestReadWord;
  131. call.source.readBit = SelfTestReadBit;
  132. call.s0.device = PLSR_DEVICE_D;
  133. call.s0.address = SELF_TEST_S0_BASE;
  134. call.s1.device = PLSR_DEVICE_D;
  135. call.s1.address = SELF_TEST_S1_BASE;
  136. call.s2.type = PLSR_OPERAND_CONSTANT;
  137. call.s2.constant = 1;
  138. call.dAxis = 0U;
  139. call.outputModeOverride = PLSR_OUTPUT_PULSE_DIR;
  140. return PlsrPostCall(&call);
  141. }