tmedicci commented on code in PR #16491:
URL: https://github.com/apache/nuttx/pull/16491#discussion_r2152197391


##########
arch/risc-v/src/common/espressif/esp_pcnt.c:
##########
@@ -642,8 +646,34 @@ static int esp_pcnt_unit_get_count(struct cap_lowerhalf_s 
*dev, int *ret)
     }
 
   flags = spin_lock_irqsave(&priv->lock);
-  *ret = pcnt_ll_get_count(ctx.dev, priv->unit_id) +
-      priv->accum_value;
+  tmp_count = pcnt_ll_get_count(ctx.dev, priv->unit_id);
+
+  if (priv->config.accum_count)
+    {
+      intr_status = pcnt_ll_get_intr_status(ctx.dev);
+      if (intr_status & PCNT_LL_UNIT_WATCH_EVENT(priv->unit_id))
+        {
+          event_status = pcnt_ll_get_event_status(ctx.dev, priv->unit_id);
+          while (event_status)
+            {
+              int event_id = __builtin_ffs(event_status) - 1;
+              event_status &= (event_status - 1);
+
+              if ((event_id == PCNT_LL_WATCH_EVENT_LOW_LIMIT) && \
+                  (tmp_count >= (priv->config.low_limit / 2)))
+                {
+                  tmp_count += priv->config.low_limit;
+                }
+              else if ((event_id == PCNT_LL_WATCH_EVENT_HIGH_LIMIT) && \
+                       (tmp_count <= (priv->config.high_limit / 2)))
+                {
+                  tmp_count += priv->config.high_limit;
+                }
+            }
+        }
+    }
+
+  *ret = tmp_count + priv->accum_value;

Review Comment:
   Well, let's reorganize the ideas here because we are talking about different 
stuff in the same thread and this is causing our brains to mix information and 
miss important points:
   
   Check the test result 
[here](https://github.com/apache/nuttx/pull/16491#discussion_r2137255677):
   
   > ```
   > qe_main: 710. 1993
   > qe_main: 711. 1000  // !!!
   > qe_main: 712. 2006
   > ```
   
   This happens because 1) we forced a busy loop (which, considering the 
constraints of a RTOS is wrong, but helps us to visualize the race condition) 
and 2) the accumulator's value was `1000` and the counter's `993` in the first 
line. Then, we tried to read the counter value + accumulator value in a 
critical section but the counter overflowed and the interrupt didn't trigger to 
update the accumulator value to `2000` (remember: the counter itself goes to 
`0` after overflow).
   
   The proposed implementation of 
[`esp_pcnt_unit_get_count`](https://github.com/apache/nuttx/pull/16491/files#diff-617e639f68354e6ff6202f44189ce9c0b061e7eab99ff40e8572b7f0d646d501L633)
 **doesn't solve** the issue because 1) we can't tell if the overflow happened 
or will happen soon (the `tmp_count >= (priv->config.low_limit / 2)` logic 
makes no sense as we can't ensure how long has passed from the last overflow or 
if we've already accounted for it) and 2) as soon the critical section exits, 
the interrupt will trigger with the same flags: we'd incremet the accumulator 
twice.
   
   Then, we shouldn't deal with the accumulator in different contexts: this is 
solely a task for the ISR, and that's why I proposed to [check for the 
interrupt 
flags](https://github.com/apache/nuttx/pull/16491#discussion_r2150694039) at 
`esp_pcnt_unit_get_count` during the critical section: if we have an interrupt 
to be serviced (that will update the accumulator value), then we should exit 
the critical section and let it to run.
   
   > I do not see any option to solve problem without checking if we are before 
or after overflow from the position itself. PCNT_Ux_CNT_REG and corresponding 
PCNT_Ux_STATUS_REG are not placed in the single 32-bit aligned word, so there 
is no chance to read them in sync and no chance resolve the problem and race 
time-window.
   
   The problem could only happen if we have a condition where the overflow 
happens too frequently and we don't have time to deal with it on ISR: that 
would mean that either **1)** the high/low limits are too low and/or **2)** the 
system is extremely overloaded (see: the test added a busy loop in a critical 
section to be able to check for the race condition!).
   
   For **1)**, I proposed to set these values dynamically to avoid interrupts 
being triggered too frequently and for **2)**, well, every RTOS can only 
guarantee its _real-time_ performance under certain conditions...
   
   From everything I proposed in this message, I think we'll be able to 
overcome this issue and the race condition. Another (complementary) approach 
would be to save the last position read (counter + accumulator) and if nothing 
has changed in hardware (operation mode, high/low limits etc), check for 
consistency of the read value during the critical section (if the PCNT is 
configured to increase the counter and the read value is lower than the last 
one and nothing changed, it means it overflowed and we need to wait for the ISR 
to updated the accumulator value).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to