ec_master.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef EC_MASTER_H
  7. #define EC_MASTER_H
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include "ec_config.h"
  13. #include "ec_util.h"
  14. #include "ec_list.h"
  15. #include "ec_errno.h"
  16. #include "ec_log.h"
  17. #include "esc_register.h"
  18. #include "ec_def.h"
  19. #include "ec_osal.h"
  20. #include "ec_port.h"
  21. #include "ec_timestamp.h"
  22. #include "ec_version.h"
  23. #include "ec_datagram.h"
  24. #include "ec_common.h"
  25. #include "ec_sii.h"
  26. #include "ec_slave.h"
  27. #include "ec_mailbox.h"
  28. #include "ec_coe.h"
  29. #include "ec_foe.h"
  30. #include "ec_eoe.h"
  31. #include "ec_cmd.h"
  32. typedef struct {
  33. unsigned int timeouts;
  34. unsigned int corrupted;
  35. unsigned int unmatched;
  36. } ec_master_stats_t;
  37. typedef enum {
  38. EC_UNKNOWN, /**< Unknown phase. No Ethernet device is attached. */
  39. EC_IDLE, /**< Idle phase. An Ethernet device is attached, but the master is not in use, yet. */
  40. EC_OPERATION /**< Operation phase. The master was requested by a realtime application. */
  41. } ec_master_phase_t;
  42. typedef struct {
  43. ec_dlist_t queue;
  44. ec_datagram_t datagrams[CONFIG_EC_MAX_NETDEVS];
  45. #if CONFIG_EC_MAX_NETDEVS > 1
  46. uint8_t *send_buffer;
  47. #endif
  48. uint32_t expected_working_counter;
  49. ec_slave_t *slave;
  50. } ec_pdo_datagram_t;
  51. typedef struct ec_master {
  52. uint8_t index;
  53. ec_netdev_t *netdev[CONFIG_EC_MAX_NETDEVS];
  54. bool link_state[CONFIG_EC_MAX_NETDEVS];
  55. uint32_t slaves_working_counter[CONFIG_EC_MAX_NETDEVS];
  56. ec_slave_state_t slaves_state[CONFIG_EC_MAX_NETDEVS];
  57. bool started; /**< Master is started. */
  58. bool scan_done; /**< Slave scan is done. */
  59. bool rescan_request; /**< Rescan requested. */
  60. ec_master_stats_t stats;
  61. ec_master_phase_t phase;
  62. ec_datagram_t main_datagram; /**< Main datagram for slave scan & state change & config & sii */
  63. ec_dlist_t datagram_queue; /**< Queue of pending datagrams*/
  64. ec_dlist_t pdo_datagram_queue; /**< Queue of pdo datagrams*/
  65. uint8_t datagram_index;
  66. ec_slave_t *dc_ref_clock; /**< DC reference clock slave. */
  67. ec_datagram_t dc_ref_sync_datagram; /**< Datagram used for synchronizing the reference clock to the master clock. */
  68. ec_datagram_t dc_all_sync_datagram; /**< Datagram used for synchronizing all slaves to the dc ref clock. */
  69. bool dc_sync_with_dc_ref_enable; /**< true: Sync the reference clock by dc ref clock, false: by master */
  70. uint32_t cycle_time; /**< Cycle time [ns]. */
  71. int32_t shift_time; /**< Shift time [ns]. */
  72. int64_t dc_sync_integral; /**< DC integral value. */
  73. uint64_t interval;
  74. ec_slave_t *slaves;
  75. uint32_t slave_count;
  76. bool perf_enable;
  77. uint64_t last_start_time;
  78. uint32_t min_period_ns;
  79. uint32_t max_period_ns;
  80. uint64_t total_period_ns;
  81. uint64_t period_count;
  82. uint32_t min_exec_ns;
  83. uint32_t max_exec_ns;
  84. uint64_t total_exec_ns;
  85. uint64_t exec_count;
  86. int32_t min_offset_ns;
  87. int32_t max_offset_ns;
  88. ec_osal_mutex_t scan_lock;
  89. ec_osal_thread_t scan_thread;
  90. ec_osal_thread_t nonperiod_thread;
  91. ec_osal_sem_t nonperiod_sem;
  92. struct ec_osal_timer *linkdetect_timer;
  93. bool nonperiod_suspend;
  94. uint8_t pdo_buffer[CONFIG_EC_MAX_NETDEVS][CONFIG_EC_MAX_PDO_BUFSIZE];
  95. uint32_t actual_pdo_size; /**< Actual PDO size for current setting. */
  96. uint32_t expected_working_counter; /**< Expected working counter for PDO datagrams. */
  97. uint32_t actual_working_counter; /**< Actual working counter for PDO datagrams. */
  98. } ec_master_t;
  99. int ec_master_init(ec_master_t *master, uint8_t master_index);
  100. void ec_master_deinit(ec_master_t *master);
  101. int ec_master_start(ec_master_t *master);
  102. int ec_master_stop(ec_master_t *master);
  103. int ec_master_queue_ext_datagram(ec_master_t *master, ec_datagram_t *datagram, bool wakep_poll, bool waiter);
  104. uint8_t *ec_master_get_slave_domain(ec_master_t *master, uint32_t slave_index);
  105. uint8_t *ec_master_get_slave_domain_output(ec_master_t *master, uint32_t slave_index);
  106. uint8_t *ec_master_get_slave_domain_input(ec_master_t *master, uint32_t slave_index);
  107. uint32_t ec_master_get_slave_domain_size(ec_master_t *master, uint32_t slave_index);
  108. uint32_t ec_master_get_slave_domain_osize(ec_master_t *master, uint32_t slave_index);
  109. uint32_t ec_master_get_slave_domain_isize(ec_master_t *master, uint32_t slave_index);
  110. int ec_master_find_slave_sync_info(uint32_t vendor_id,
  111. uint32_t product_code,
  112. uint32_t revision_number,
  113. uint8_t cia402_mode,
  114. ec_sync_info_t **syncs,
  115. uint8_t *sync_count);
  116. #endif