ec_master.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_perf.h"
  22. #include "ec_timestamp.h"
  23. #include "ec_version.h"
  24. #include "ec_datagram.h"
  25. #include "ec_common.h"
  26. #include "ec_sii.h"
  27. #include "ec_slave.h"
  28. #include "ec_mailbox.h"
  29. #include "ec_coe.h"
  30. #include "ec_foe.h"
  31. #include "ec_eoe.h"
  32. #include "ec_cmd.h"
  33. /** Netdev statistics.
  34. */
  35. typedef struct {
  36. uint64_t tx_count; /**< Number of frames sent. */
  37. uint64_t last_tx_count; /**< Number of frames sent of last statistics cycle. */
  38. uint64_t rx_count; /**< Number of frames received. */
  39. uint64_t last_rx_count; /**< Number of frames received of last statistics cycle. */
  40. uint64_t tx_bytes; /**< Number of bytes sent. */
  41. uint64_t last_tx_bytes; /**< Number of bytes sent of last statistics cycle. */
  42. uint64_t rx_bytes; /**< Number of bytes received. */
  43. uint64_t last_rx_bytes; /**< Number of bytes received of last statistics cycle. */
  44. uint64_t last_loss; /**< Tx/Rx difference of last statistics cycle. */
  45. int32_t tx_frame_rates[EC_RATE_COUNT]; /**< Transmit rates in frames/s for different statistics cycle periods.*/
  46. int32_t rx_frame_rates[EC_RATE_COUNT]; /**< Receive rates in frames/s for different statistics cycle periods.*/
  47. int32_t tx_byte_rates[EC_RATE_COUNT]; /**< Transmit rates in byte/s for different statistics cycle periods. */
  48. int32_t rx_byte_rates[EC_RATE_COUNT]; /**< Receive rates in byte/s for different statistics cycle periods. */
  49. int32_t loss_rates[EC_RATE_COUNT]; /**< Frame loss rates for different statistics cycle periods. */
  50. uint64_t last_jiffies; /**< Jiffies of last statistic cycle. */
  51. } ec_netdev_stats_t;
  52. /** Cyclic statistics.
  53. */
  54. typedef struct {
  55. unsigned int timeouts; /**< datagram timeouts */
  56. unsigned int corrupted; /**< corrupted frames */
  57. unsigned int unmatched; /**< unmatched datagrams (received, but not queued any longer) */
  58. unsigned long output_jiffies; /**< time of last output */
  59. } ec_stats_t;
  60. typedef enum {
  61. EC_ORPHANED, /**< Orphaned phase. The master has no Ethernet device attached. */
  62. EC_IDLE, /**< Idle phase. An Ethernet device is attached, but the master is not in use, yet. */
  63. EC_OPERATION /**< Operation phase. The master was requested by a realtime application. */
  64. } ec_master_phase_t;
  65. typedef struct {
  66. ec_dlist_t queue;
  67. ec_datagram_t datagrams[CONFIG_EC_MAX_NETDEVS];
  68. #if CONFIG_EC_MAX_NETDEVS > 1
  69. uint8_t *send_buffer;
  70. #endif
  71. uint32_t expected_working_counter;
  72. ec_slave_t *slave;
  73. } ec_pdo_datagram_t;
  74. typedef struct ec_master {
  75. uint8_t index;
  76. ec_netdev_t *netdev[CONFIG_EC_MAX_NETDEVS];
  77. bool link_state[CONFIG_EC_MAX_NETDEVS];
  78. uint32_t slaves_responding[CONFIG_EC_MAX_NETDEVS];
  79. ec_slave_state_t slaves_state[CONFIG_EC_MAX_NETDEVS];
  80. ec_netdev_stats_t netdev_stats;
  81. ec_stats_t stats;
  82. ec_master_phase_t phase;
  83. bool active; /**< Master is started. */
  84. bool scan_done; /**< Slave scan is done. */
  85. ec_datagram_t main_datagram; /**< Main datagram for slave scan & state change & config & sii */
  86. ec_dlist_t datagram_queue; /**< Queue of pending datagrams*/
  87. ec_dlist_t pdo_datagram_queue; /**< Queue of pdo datagrams*/
  88. uint8_t datagram_index;
  89. ec_slave_t *dc_ref_clock; /**< DC reference clock slave. */
  90. ec_datagram_t dc_ref_sync_datagram; /**< Datagram used for synchronizing the reference clock to the master clock. */
  91. ec_datagram_t dc_all_sync_datagram; /**< Datagram used for synchronizing all slaves to the dc ref clock. */
  92. ec_datagram_t systime_diff_mon_datagram; /**< Datagram used for reading the system time difference between master and reference clock. */
  93. uint32_t min_systime_diff;
  94. uint32_t max_systime_diff;
  95. uint32_t curr_systime_diff;
  96. uint32_t systime_diff_count;
  97. uint64_t total_systime_diff;
  98. bool systime_diff_enable;
  99. uint64_t interval;
  100. ec_slave_t *slaves;
  101. uint32_t slave_count;
  102. #ifdef CONFIG_EC_PERF_ENABLE
  103. ec_perf_t perf;
  104. #endif
  105. ec_osal_mutex_t scan_lock;
  106. ec_osal_thread_t scan_thread;
  107. ec_osal_thread_t nonperiod_thread;
  108. ec_osal_sem_t nonperiod_sem;
  109. struct ec_osal_timer *linkdetect_timer;
  110. bool nonperiod_suspend;
  111. uint8_t pdo_buffer[CONFIG_EC_MAX_NETDEVS][CONFIG_EC_MAX_PDO_BUFSIZE];
  112. uint32_t actual_pdo_size;
  113. uint32_t expected_working_counter;
  114. uint32_t actual_working_counter;
  115. } ec_master_t;
  116. int ec_master_init(ec_master_t *master, uint8_t master_index);
  117. void ec_master_deinit(ec_master_t *master);
  118. int ec_master_start(ec_master_t *master, uint32_t period_us);
  119. int ec_master_stop(ec_master_t *master);
  120. int ec_master_queue_ext_datagram(ec_master_t *master, ec_datagram_t *datagram, bool wakep_poll, bool waiter);
  121. uint8_t *ec_master_get_slave_domain(ec_master_t *master, uint32_t slave_index);
  122. uint8_t *ec_master_get_slave_domain_output(ec_master_t *master, uint32_t slave_index);
  123. uint8_t *ec_master_get_slave_domain_input(ec_master_t *master, uint32_t slave_index);
  124. uint32_t ec_master_get_slave_domain_size(ec_master_t *master, uint32_t slave_index);
  125. uint32_t ec_master_get_slave_domain_osize(ec_master_t *master, uint32_t slave_index);
  126. uint32_t ec_master_get_slave_domain_isize(ec_master_t *master, uint32_t slave_index);
  127. #endif