hal_entry.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-03-11 Wangyuqiang first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <board.h>
  13. #include "hal_data.h"
  14. #include "ec_master.h"
  15. #define LED_PIN_0 BSP_IO_PORT_14_PIN_3 /* Onboard LED pins */
  16. #define LED_PIN_1 BSP_IO_PORT_14_PIN_0 /* Onboard LED pins */
  17. #define LED_PIN_2 BSP_IO_PORT_14_PIN_1 /* Onboard LED pins */
  18. #define MOTOR_MODE_CSV_CSP 0
  19. #define MOTOR_MODE_CSP 1
  20. #define MOTOR_MODE_CSV 2
  21. volatile uint8_t motor_mode = MOTOR_MODE_CSP;
  22. ec_master_t g_ec_master;
  23. uint8_t cherryecat_eepromdata[2048];
  24. static ec_pdo_entry_info_t coe402_1601[] =
  25. {
  26. { 0x6040, 0x00, 0x10 }, // Control Word
  27. { 0x6072, 0x00, 0x10 }, // Max Torque
  28. { 0x607A, 0x00, 0x20 }, // Target Position
  29. { 0x60B0, 0x00, 0x20 }, // Position Offset
  30. { 0x60B1, 0x00, 0x20 }, // Velocity Offset
  31. { 0x60B2, 0x00, 0x10 }, // Torque Offset
  32. { 0x60B8, 0x00, 0x10 }, // Touch Probe Function
  33. { 0x60E0, 0x00, 0x10 }, // Positive Torque Limit Value
  34. { 0x60E0, 0x00, 0x10 }, // Negative Torque Limit Value
  35. { 0x60FE, 0x01, 0x20 }, // Digital Outputs
  36. };
  37. static ec_pdo_entry_info_t coe402_1a01[] =
  38. {
  39. { 0x603F, 0x00, 0x10 }, // Error Code
  40. { 0x6041, 0x00, 0x10 }, // Status Word
  41. { 0x6064, 0x00, 0x20 }, // Actual Position
  42. { 0x606C, 0x00, 0x20 }, // Actual Velocity
  43. { 0x6077, 0x00, 0x10 }, // Actual Torque
  44. { 0x60B9, 0x00, 0x10 }, // Touch Probe Status
  45. { 0x60BA, 0x00, 0x20 }, // Touch Probe Position 1 Positive Value
  46. { 0x60BC, 0x00, 0x20 }, // Touch Probe Position 2 Positive Value
  47. { 0x60F4, 0x00, 0x20 }, // Following Error Actual Value
  48. { 0x60FD, 0x00, 0x20 }, // Digital Inputs
  49. };
  50. static ec_pdo_info_t cia402_rxpdos[] =
  51. {
  52. { 0x1601, 10, &coe402_1601[0] },
  53. };
  54. static ec_pdo_info_t cia402_txpdos[] =
  55. {
  56. { 0x1a01, 10, &coe402_1a01[0] },
  57. };
  58. static ec_sync_info_t cia402_syncs[] =
  59. {
  60. { 2, EC_DIR_OUTPUT, 1, cia402_rxpdos },
  61. { 3, EC_DIR_INPUT, 1, cia402_txpdos },
  62. };
  63. int ec_start(int argc, const char **argv)
  64. {
  65. static ec_slave_config_t slave_cia402_config;
  66. if (g_ec_master.slave_count == 0)
  67. {
  68. printf("No slave found, please check the connection\r\n");
  69. return -1;
  70. }
  71. slave_cia402_config.dc_assign_activate = 0x300;
  72. slave_cia402_config.dc_sync[0].cycle_time = atoi(argv[1]) * 1000;
  73. slave_cia402_config.dc_sync[0].shift_time = 500000;
  74. slave_cia402_config.dc_sync[1].cycle_time = 0;
  75. slave_cia402_config.dc_sync[1].shift_time = 0;
  76. slave_cia402_config.sync = cia402_syncs;
  77. slave_cia402_config.sync_count = sizeof(cia402_syncs) / sizeof(ec_sync_info_t);
  78. for (uint32_t i = 0; i < g_ec_master.slave_count; i++)
  79. {
  80. if (g_ec_master.slaves[i].sii.vendor_id != 0x00000766)
  81. {
  82. EC_LOG_ERR("Unsupported slave found: vendor_id=0x%08x\n", g_ec_master.slaves[i].sii.vendor_id);
  83. return -1;
  84. }
  85. g_ec_master.slaves[i].config = &slave_cia402_config;
  86. }
  87. ec_master_start(&g_ec_master, atoi(argv[1]));
  88. return 0;
  89. }
  90. MSH_CMD_EXPORT(ec_start, ethercat start);
  91. int ec_stop(int argc, const char **argv)
  92. {
  93. ec_master_stop(&g_ec_master);
  94. return 0;
  95. }
  96. MSH_CMD_EXPORT(ec_stop, ethercat stop);
  97. void hal_entry(void)
  98. {
  99. rt_kprintf("\nHello RT-Thread!\n");
  100. rt_kprintf("==================================================\n");
  101. rt_kprintf("This example project is an ethernet routine!\n");
  102. rt_kprintf("==================================================\n");
  103. ec_master_cmd_init(&g_ec_master);
  104. ec_master_init(&g_ec_master, 0);
  105. while (1)
  106. {
  107. rt_pin_write(LED_PIN_0, PIN_HIGH);
  108. rt_pin_write(LED_PIN_1, PIN_HIGH);
  109. rt_pin_write(LED_PIN_2, PIN_HIGH);
  110. rt_thread_mdelay(1000);
  111. rt_pin_write(LED_PIN_0, PIN_LOW);
  112. rt_pin_write(LED_PIN_1, PIN_LOW);
  113. rt_pin_write(LED_PIN_2, PIN_LOW);
  114. rt_thread_mdelay(1000);
  115. }
  116. }