From f0cbde9013dcc6e93afaf2e432a1587f7d390e44 Mon Sep 17 00:00:00 2001 From: algin Date: Fri, 6 Oct 2023 17:50:38 +0300 Subject: [PATCH] =?UTF-8?q?fix(SF-60):=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=B1=D0=B0=D0=B3=20=D1=81=D0=B5?= =?UTF-8?q?=D0=BC=D0=B0=D1=84=D0=BE=D1=80=D0=B0=20=D0=B8=20=D0=B2=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=BE=20=D0=BF=D1=80=D0=BE=D1=84?= =?UTF-8?q?=D0=B8=D0=BB=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=B5=D0=BC=D0=B0=20ethercat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ethernet_industry/eth_ecat_telegram.cpp | 33 ++++++++++++++----- .../ethernet_industry/eth_ecat_telegram.hpp | 15 ++++++++- .../free_rtos/profiler/quick_profiler.hpp | 23 +++++++------ components/free_rtos/timer/timer.cpp | 5 ++- components/free_rtos/timer/timer.hpp | 7 +++- 5 files changed, 60 insertions(+), 23 deletions(-) diff --git a/components/free_rtos/ethernet_industry/eth_ecat_telegram.cpp b/components/free_rtos/ethernet_industry/eth_ecat_telegram.cpp index 4185e3a..17e620c 100644 --- a/components/free_rtos/ethernet_industry/eth_ecat_telegram.cpp +++ b/components/free_rtos/ethernet_industry/eth_ecat_telegram.cpp @@ -96,8 +96,12 @@ uint8_t* EcatTelegram::unpack(uint8_t *raw) { } bool EcatTelegram::transfer() { + MAKE_AUTO_PROFILER(transfer, 32); + auto first = datagram_queue_.get_first(); uint32_t transfer_attempts = 0; + uint32_t tick_counter_start; + uint32_t tick_counter_diff; bool status = (first == nullptr); int32_t sts; @@ -112,7 +116,7 @@ bool EcatTelegram::transfer() { first->reset_all_wkc(); while(1) { - ecat_timer_.Wait(); + tick_counter_start = ecat_timer_.Wait(); transfer_attempts++; status = (transfer_attempts <= max_transfer_attempts_); @@ -124,12 +128,13 @@ bool EcatTelegram::transfer() { status_.result = EcatTelegramResult::FATAL_ERROR; DebugP_log((char*)"%s: %d\r\n", status_.get_description().string, max_transfer_attempts_); + DebugP_log((char*)"Tick counter diff: %d/%d\r\n", tick_counter_diff, counter_timeout_ticks_); + DebugP_log((char*)"Last profiler item %lld cycles\r\n", GET_LAST_PROFILER_ITEM(transfer)); + FORCE_DUMP_PROFILER(transfer); break; } - //START_PROFILER_STATIC_ITEM(Test_static, 128, 0); - // status = eth_stack_.send_pkt(port_id_, ETH_PROT_ECAT_LE, 1); status = tx_flow_.send(port_id_, this, 1); @@ -142,13 +147,25 @@ bool EcatTelegram::transfer() { continue; } - do { - sts = rx_sem_.pend(connection_timeout_ticks_); - } while(sts == SystemP_FAILURE); + { + MAKE_PROFILER_AUTO_ITEM(transfer, 32, 0); - //STOP_PROFILER_STATIC_ITEM(Test_static, 128, 0); + do { + sts = rx_sem_.pend(semaphore_timeout_ticks_); - if(sts == SystemP_TIMEOUT) { + tick_counter_diff = diff(tick_counter_start, ecat_timer_.GetTickCounter()); + + if((sts != SystemP_SUCCESS) && (tick_counter_diff < counter_timeout_ticks_)) { + //DebugP_log((char*)"rx_sem_ fake timeout detected !\r\n", ); + + continue; + } + + break; + } while(1); + } + + if(sts != SystemP_SUCCESS) { status_.transfer_errors++; status_.result = EcatTelegramResult::WARNING_TIMEOUT_ERROR; diff --git a/components/free_rtos/ethernet_industry/eth_ecat_telegram.hpp b/components/free_rtos/ethernet_industry/eth_ecat_telegram.hpp index 160d729..6694e70 100644 --- a/components/free_rtos/ethernet_industry/eth_ecat_telegram.hpp +++ b/components/free_rtos/ethernet_industry/eth_ecat_telegram.hpp @@ -73,11 +73,24 @@ public: eth_.getEthStackPtr()->Register(ETH_PROT_ECAT_LE, this); } + static uint32_t diff(uint32_t tick_counter_start, uint32_t tick_counter_current) { + uint32_t tick_counter_diff; + + if(tick_counter_current >= tick_counter_start) { + tick_counter_diff = tick_counter_current - tick_counter_start; + }else{ + tick_counter_diff = tick_counter_current + (0xFFFFFFFF - tick_counter_start); + } + + return tick_counter_diff; + } + bool transfer(datagram::IEcatDatagram& next); bool transfer(queue::Queue& next); private: - static constexpr uint32_t connection_timeout_ticks_ = 70; + static constexpr uint32_t semaphore_timeout_ticks_ = 2; + static constexpr uint32_t counter_timeout_ticks_ = 8; static constexpr uint32_t max_transfer_attempts_ = 6; Eth& eth_; diff --git a/components/free_rtos/profiler/quick_profiler.hpp b/components/free_rtos/profiler/quick_profiler.hpp index d711402..30ef7af 100644 --- a/components/free_rtos/profiler/quick_profiler.hpp +++ b/components/free_rtos/profiler/quick_profiler.hpp @@ -56,8 +56,9 @@ class QuickProfilerStaticItem; template class QuickProfiler { public: - QuickProfiler(const char *name) - : name_{name} { }; + QuickProfiler(const char *name, bool auto_dump = false) + : name_{name} + , auto_dump_{auto_dump} { }; QuickProfilerAutoItem makeProfilerAutoItem() { return QuickProfilerAutoItem{*this}; @@ -67,7 +68,7 @@ public: return QuickProfilerStaticItem{*this}; } - void collect(uint64_t diff, bool toDump) { + void collect(uint64_t diff) { uint32_t counter = counter_; buffer_[counter] = diff; @@ -76,7 +77,7 @@ public: if(counter >= size) { counter = 0; - if(toDump == true) { + if(auto_dump_ == true) { dump(); } } @@ -111,7 +112,7 @@ public: uint32_t min{0xFFFFFFFF}; uint32_t max{0x00000000}; - for(uint32_t value : buffer_) { + for(uint64_t value : buffer_) { average += value/size; if(value < min) { @@ -126,12 +127,12 @@ public: } DebugP_log((char *)"\r\n"); - DebugP_log((char *)"Profiler %s average: %lld min: %d max: %d\r\n", name_, average, min, max); } private: const char *name_; + const bool auto_dump_{false}; size_t counter_{0}; std::array buffer_; @@ -140,7 +141,7 @@ private: uint32_t min{0xFFFFFFFF}; uint32_t max{0x00000000}; - for(uint32_t value : buffer_) { + for(uint64_t value : buffer_) { average += value/size; if(value < min) { @@ -152,9 +153,7 @@ private: } } - if(average > 60000) { - DebugP_log((char *)"Profiler %s average: %lld min: %d max: %d\r\n", name_, average, min, max); - } + DebugP_log((char *)"Profiler %s average: %lld min: %d max: %d\r\n", name_, average, min, max); } }; @@ -177,7 +176,7 @@ public: diff = (0xFFFFFFFFU - start_cycle) + end_cycle; } - profiler_.collect(diff, true); + profiler_.collect(diff); } private: @@ -206,7 +205,7 @@ public: diff = (0xFFFFFFFFU - start_cycle) + end_cycle; } - profiler_.collect(diff, true); + profiler_.collect(diff); } private: diff --git a/components/free_rtos/timer/timer.cpp b/components/free_rtos/timer/timer.cpp index e885c6f..ce10559 100644 --- a/components/free_rtos/timer/timer.cpp +++ b/components/free_rtos/timer/timer.cpp @@ -17,6 +17,7 @@ void free_rtos::timer_isr_callback(void * arg) Timer * const p_timer = (Timer * const)arg; p_timer->sem_.post(); + ++p_timer->tick_counter_; TimerP_clearOverflowInt(p_timer->base_addr_); HwiP_clearInt(p_timer->int_num_); @@ -60,8 +61,10 @@ bool free_rtos::Timer::Init(Settings& sett) return (status == SystemP_SUCCESS); } -void free_rtos::Timer::Wait() { +uint32_t free_rtos::Timer::Wait() { sem_.pend(); + + return tick_counter_; } void free_rtos::Timer::Start() { diff --git a/components/free_rtos/timer/timer.hpp b/components/free_rtos/timer/timer.hpp index 68cb080..d3d500f 100644 --- a/components/free_rtos/timer/timer.hpp +++ b/components/free_rtos/timer/timer.hpp @@ -35,11 +35,15 @@ public: /** * �������� ������������ �������. �����������. */ - void Wait(); + uint32_t Wait(); void Start(); void Stop(); + + uint32_t GetTickCounter() { + return tick_counter_; + } private: friend void timer_isr_callback(void * arg); @@ -49,6 +53,7 @@ private: HwiP_Object hwi_obj_; /// ���������� ������������ �� ������� Semaphore sem_; + uint32_t tick_counter_{0x00000000}; }; }