Skip to content

ESP32: Add Quadrature Encoder and Pulse Counter classes.#19239

Draft
IhorNehrutsa wants to merge 6 commits into
micropython:masterfrom
IhorNehrutsa:ESP32_PCNT_Encoder_Counter
Draft

ESP32: Add Quadrature Encoder and Pulse Counter classes.#19239
IhorNehrutsa wants to merge 6 commits into
micropython:masterfrom
IhorNehrutsa:ESP32_PCNT_Encoder_Counter

Conversation

@IhorNehrutsa
Copy link
Copy Markdown
Contributor

I did not use generative AI tools when creating this PR.

Co-Authored-By: robert-hh <robert@hammelrath.com>
Co-Authored-By: Jonathan Hogg <me@jonathanhogg.com>

Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
@IhorNehrutsa IhorNehrutsa marked this pull request as draft May 18, 2026 13:38
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 18, 2026

Code size report:

Reference:  docs/library/rp2.DMA: Correct and add pack_ctrl defaults. [8f3a416]
Comparison: Update machine_encoder.c [merge of 5512719]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32:  -932 -0.053% ESP32_GENERIC[incl -1672(data) +24(bss)]
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@IhorNehrutsa IhorNehrutsa force-pushed the ESP32_PCNT_Encoder_Counter branch from 927084c to 6044972 Compare May 18, 2026 14:00
@IhorNehrutsa IhorNehrutsa force-pushed the ESP32_PCNT_Encoder_Counter branch 3 times, most recently from 1205c14 to 8f64762 Compare May 19, 2026 07:59
@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.47%. Comparing base (1c63211) to head (5512719).
⚠️ Report is 35 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19239   +/-   ##
=======================================
  Coverage   98.47%   98.47%           
=======================================
  Files         176      176           
  Lines       22845    22845           
=======================================
  Hits        22497    22497           
  Misses        348      348           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@IhorNehrutsa IhorNehrutsa force-pushed the ESP32_PCNT_Encoder_Counter branch 3 times, most recently from d486b81 to b7a8ded Compare May 19, 2026 11:23
Update machine_encoder.c
@IhorNehrutsa IhorNehrutsa force-pushed the ESP32_PCNT_Encoder_Counter branch 2 times, most recently from f8dc3e9 to 82e2bfe Compare May 19, 2026 11:50
@IhorNehrutsa
Copy link
Copy Markdown
Contributor Author

@robert-hh
May you test on the MIMXRT port?
tests/extmod_hardware/machine_counter.py

@robert-hh
Copy link
Copy Markdown
Contributor

@IhorNehrutsa
The test has to be changed. Line 142, 143 have to be:

        self.counter.init(in_pin, direction=Counter.UP, match=100)
        self.counter.irq(handler=callback, trigger=Counter.IRQ_MATCH)

@IhorNehrutsa IhorNehrutsa force-pushed the ESP32_PCNT_Encoder_Counter branch from 82e2bfe to 676b293 Compare May 19, 2026 13:03
@IhorNehrutsa
Copy link
Copy Markdown
Contributor Author

@robert-hh

with the irq.flags() method. The argument *hard* specifies, whether the callback is called

Does irq.flags() mean the Counter.status() and Encoder.status()?

@IhorNehrutsa
Copy link
Copy Markdown
Contributor Author

>>> %run -c $EDITOR_CONTENT
test_connections (__main__.TestConnections) ... ok
test_count_rising_UP (__main__.TestCounter) ... ok
test_count_rising_DOWN (__main__.TestCounter) ... ok
test_count_falling_UP_DOWN (__main__.TestCounter) ... ok
test_count_pin_direction (__main__.TestCounter) ...
 callback counter value:     50000, counter.status():4, IRQ_MATCH:True, IRQ_ZERO:False

 callback counter value:     50000, counter.status():4, IRQ_MATCH:True, IRQ_ZERO:False

 callback counter value:         0, counter.status():64, IRQ_MATCH:False, IRQ_ZERO:True
 ok
test_irq (__main__.TestCounter) ...
 callback counter value:     50000, counter.status():4, IRQ_MATCH:True, IRQ_ZERO:False

 callback counter value:    -50000, counter.status():4, IRQ_MATCH:True, IRQ_ZERO:False
 ok
----------------------------------------------------------------------
Ran 6 tests

OK
>>> 

@IhorNehrutsa IhorNehrutsa force-pushed the ESP32_PCNT_Encoder_Counter branch from c7195db to b3527aa Compare May 20, 2026 07:28
@robert-hh
Copy link
Copy Markdown
Contributor

Does irq().flags() mean the Counter.status() and Encoder.status()

There is no Counter.status() or Encoder.status() method. irq().flags() returns the flag which caused the IRQ to happen. And that may be different for Counter and Encoder.

@IhorNehrutsa
Copy link
Copy Markdown
Contributor Author

@robert-hh
Oh, thanks, I was watching the old version. :(
May you give a small example of using irq().flags() instead of xxx.status() in tests\extmod_hardware\machine_counter.py?

@robert-hh
Copy link
Copy Markdown
Contributor

Try to call Counter.irq().flags() instead of Counter.status().

@IhorNehrutsa
Copy link
Copy Markdown
Contributor Author

@robert-hh
It seems better to set the handler after disabling interrupts

self->mp_irq_obj->handler = handler;

after line
ENC_DisableInterrupts(self->instance, ENCODER_ALL_INTERRUPTS);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants