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.

191 lines
5.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "main.hpp"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include "main.h"
  5. #include "project.hpp"
  6. //
  7. // #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp"
  8. #include "sdk/components/iflytop_can_slave_modules/idcard_reader_service.hpp"
  9. #include "sdk/components/single_axis_motor_control/single_axis_motor_control.hpp"
  10. #include "sdk/hal/zhal.hpp"
  11. #include "sdk\components\iflytop_can_slave_modules\io_control_service.hpp"
  12. #include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp"
  13. #include "sdk\components\m3078\m3078_code_scaner.hpp"
  14. #include "sdk\components\tmc\ic\ztmc4361A.hpp"
  15. #include "sdk\components\tmc\ic\ztmc5130.hpp"
  16. //
  17. #include "hardware.hpp"
  18. #include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp"
  19. #include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp"
  20. #include "sdk\components\zcan_module\zcan_basic_order_module.hpp"
  21. #include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp"
  22. #include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp"
  23. #define TAG "main"
  24. namespace iflytop {
  25. Main gmain;
  26. };
  27. using namespace iflytop;
  28. /*******************************************************************************
  29. * TOOLS *
  30. *******************************************************************************/
  31. static uint8_t *hex_str_to_bytes(char *data, int32_t len, int32_t &bytelen) {
  32. /**
  33. * @brief
  34. * data:
  35. * 12 34 56 78 90 ab cd ef
  36. *
  37. */
  38. static uint8_t bytes_cache[1024] = {0};
  39. static uint8_t data_cache[1024] = {0};
  40. int32_t data_len = 0;
  41. memset(bytes_cache, 0, sizeof(bytes_cache));
  42. memset(data_cache, 0, sizeof(data_cache));
  43. for (int32_t i = 0; i < len; i++) {
  44. if (data[i] == ' ') continue;
  45. if (data[i] == '\r' || data[i] == '\n') break;
  46. data_cache[i] = data[i];
  47. data_len++;
  48. }
  49. if (data_len % 2 != 0) {
  50. ZLOGE(TAG, "data_len %d", data_len);
  51. return NULL;
  52. }
  53. for (int32_t i = 0; i < data_len; i += 2) {
  54. char c1 = data_cache[i];
  55. char c2 = data_cache[i + 1];
  56. if (c1 >= '0' && c1 <= '9') {
  57. c1 = c1 - '0';
  58. } else if (c1 >= 'a' && c1 <= 'f') {
  59. c1 = c1 - 'a' + 10;
  60. } else if (c1 >= 'A' && c1 <= 'F') {
  61. c1 = c1 - 'A' + 10;
  62. } else {
  63. ZLOGE(TAG, "c1 %c", c1);
  64. return NULL;
  65. }
  66. if (c2 >= '0' && c2 <= '9') {
  67. c2 = c2 - '0';
  68. } else if (c2 >= 'a' && c2 <= 'f') {
  69. c2 = c2 - 'a' + 10;
  70. } else if (c2 >= 'A' && c2 <= 'F') {
  71. c2 = c2 - 'A' + 10;
  72. } else {
  73. ZLOGE(TAG, "c2 %c", c2);
  74. return NULL;
  75. }
  76. bytes_cache[i / 2] = (c1 << 4) | c2;
  77. }
  78. bytelen = data_len / 2;
  79. return bytes_cache;
  80. }
  81. void dumphexdata(uint8_t *data, int32_t len) {
  82. for (int32_t i = 0; i < len; i++) {
  83. printf("%02X ", data[i]);
  84. }
  85. printf("\n");
  86. }
  87. /*******************************************************************************
  88. * GLOBAL *
  89. *******************************************************************************/
  90. IflytopCanProtocolStackProcesser m_protocolStack;
  91. Hardware m_hardware;
  92. ZGPIO debuglight;
  93. ZCanReceiver m_canReceiver;
  94. /*******************************************************************************
  95. * MESSAGE_HANDLER *
  96. *******************************************************************************/
  97. /**
  98. * @brief CAN接收到消息
  99. */
  100. void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) {
  101. // ZLOGI(TAG, "onRceivePacket from %d %d", rxbuf->id, len);
  102. static uint8_t rxdata[1024] = {0};
  103. memset(rxdata, 0, sizeof(rxdata));
  104. Cmdheader_t *cmdheader = (Cmdheader_t *)packet;
  105. bool match = false;
  106. int32_t receipt_size = 0;
  107. int32_t ecode = m_hardware.process_rx_packet(Hardware::from_where_t::kcan, packet, len, rxdata, receipt_size, match);
  108. if (match) {
  109. if (ecode != 0) {
  110. m_canReceiver.sendErrorAck(cmdheader, ecode);
  111. } else {
  112. m_canReceiver.sendAck(cmdheader, rxdata, sizeof(rxdata));
  113. }
  114. }
  115. }
  116. /**
  117. * @brief
  118. */
  119. static void processUartRX(uint8_t *packet, size_t len) {
  120. static uint8_t rxdata[1024] = {0};
  121. int32_t receipt_size = 0;
  122. bool match = false;
  123. memset(rxdata, 0, sizeof(rxdata));
  124. //
  125. int32_t bytelen = 0;
  126. uint8_t *hexbytes = hex_str_to_bytes((char *)packet, len, bytelen);
  127. if (hexbytes == NULL) {
  128. ZLOGE(TAG, "hex_str_to_bytes failed");
  129. return;
  130. }
  131. dumphexdata(hexbytes, bytelen);
  132. m_hardware.process_rx_packet(Hardware::kuart, packet, len, rxdata, receipt_size, match);
  133. }
  134. /*******************************************************************************
  135. * MAIN *
  136. *******************************************************************************/
  137. void Main::run() {
  138. ZHALCORE::cfg_t oscfg = {
  139. .delayhtim = &DELAY_US_TIMER,
  140. .debuguart = &DEBUG_UART,
  141. };
  142. ZHALCORE::getInstance()->initialize(oscfg);
  143. ZLOGI(TAG, "zapp:%s", VERSION);
  144. printf("int32_t %d int %d longint %d\n", sizeof(int32_t), sizeof(int), sizeof(long int));
  145. debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
  146. ZHAL_CORE_REG(200, { debuglight.toggleState(); });
  147. m_hardware.initialize();
  148. static ZUART uartreceiver;
  149. static ZUART::cfg_t uartreceiver_cfg = {
  150. .name = "uartreceiver",
  151. .huart = &DEBUG_UART,
  152. .rxbuffersize = 512,
  153. .rxovertime_ms = 30,
  154. };
  155. uartreceiver.initialize(&uartreceiver_cfg);
  156. uartreceiver.setrxcb([this](uint8_t *data, size_t len) { processUartRX(data, len); });
  157. ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID);
  158. m_canReceiver.init(cfg);
  159. m_canReceiver.registerListener(this);
  160. ZLOGI(TAG, "init done");
  161. while (1) {
  162. ZHALCORE::getInstance()->loop();
  163. m_hardware.loop();
  164. }
  165. }