8000 ESP32: New hardware PCNT Counter() and Encoder(). by IhorNehrutsa · Pull Request #6639 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ESP32: New hardware PCNT Counter() and Encoder(). #6639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 153 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
153 commits
Select commit Hold shift + click to select a range
c6d6dce
esp32/esp32_pcnt.* : Add files for Pulse counter and Quadrature encoder.
IhorNehrutsa Nov 20, 2020
ca67883
esp32/modesp32.*: Add PCNT() & QUAD() to esp32 momdule.
IhorNehrutsa Nov 20, 2020
402438d
Test
IhorNehrutsa Nov 20, 2020
dd9e5c1
add
IhorNehrutsa Nov 20, 2020
88ade12
EspError, PCNT, QUAD test Ok
IhorNehrutsa Nov 20, 2020
4965c33
Formatting
IhorNehrutsa Nov 20, 2020
3a29d6a
Formatting
IhorNehrutsa Nov 20, 2020
3f47cfc
ESP32 PCNT QUAD: Add documentation and hide some methods and consts.
IhorNehrutsa Nov 22, 2020
a7386a3
ESP32 PCNT QUAD: Fix documentation and delete duplicates in code.
IhorNehrutsa Nov 22, 2020
3deaa23
ESP32 PCNT QUAD: Formatting
IhorNehrutsa Nov 22, 2020
fb1a533
ESP32 PCNT QUAD: Formatting
IhorNehrutsa Nov 22, 2020
d12c849
esp32/esp32_pcnt.c: Use ESP_EXCEPTIONS() macro.
IhorNehrutsa Nov 24, 2020
c9501e8
Merge branch 'master' into feature/esp32_pcnt_quad
IhorNehrutsa Jun 8, 2021
d7bd201
revert gitignore
IhorNehrutsa Sep 21, 2021
305ebe3
Remove esp_error.c and esp_error.h
IhorNehrutsa Sep 21, 2021
1c5cabf
Revert EspError
IhorNehrutsa Sep 21, 2021
e9214ad
Remove EspError
IhorNehrutsa Sep 21, 2021
2fcc1ab
Update esp32.rst
IhorNehrutsa Sep 21, 2021
93cad4b
Update CMakeLists.txt
IhorNehrutsa Sep 21, 2021
fe7872a
Update CMakeLists.txt
IhorNehrutsa Sep 21, 2021
747fd92
Update esp32_pcnt.c
IhorNehrutsa Sep 21, 2021
caa1fac
docs: 64-bit signed counter value
IhorNehrutsa Sep 22, 2021
24b06bf
Del PinPull
IhorNehrutsa Sep 22, 2021
c2789db
Del pull
IhorNehrutsa Sep 22, 2021
7556444
Pin expected
IhorNehrutsa Sep 22, 2021
47abe7a
Update mpprint.h
IhorNehrutsa Sep 23, 2021
ab726fc
py/obj: Add func to get int64 arguments
IhorNehrutsa Sep 28, 2021
d29ed14
Merge pull request #4 from IhorNehrutsa/esp32_pcnt_quad_delete_PinPull
IhorNehrutsa Sep 28, 2021
3912a1c
Remove class:: pcnt.PinPull()
IhorNehrutsa Sep 28, 2021
35646e7
Renames
IhorNehrutsa Sep 30, 2021
1f11157
Renames
IhorNehrutsa Sep 30, 2021
f4bf625
Revert mp_const_obj_t to mp_obj_t
IhorNehrutsa Sep 30, 2021
540894d
Update pcnt.rst
IhorNehrutsa Sep 30, 2021
fa8aa57
wip
IhorNehrutsa Oct 5, 2021
3d7b8e0
move to machine
IhorNehrutsa Oct 6, 2021
cefa67b
Update esp32_pcnt.c
IhorNehrutsa Oct 6, 2021
8000 6e994b1
wip
IhorNehrutsa Oct 10, 2021
eb5b53f
docs
IhorNehrutsa Oct 10, 2021
951d34b
wip
IhorNehrutsa Oct 11, 2021
af61d97
Merge branch 'feature/esp32_pcnt_quad' of https://github.com/IhorNehr…
IhorNehrutsa Oct 11, 2021
b4c37e7
wip docs
IhorNehrutsa Oct 11, 2021
8e3ca2b
move pcnt.rst to esp32
IhorNehrutsa Oct 12, 2021
17cfaf7
docs
IhorNehrutsa Oct 12, 2021
da87088
docs
IhorNehrutsa Oct 12, 2021
619e353
Update general.rst
IhorNehrutsa Oct 13, 2021
dcf8fa1
Update machine.Encoder.rst
IhorNehrutsa Oct 13, 2021
8f83af3
Remove set_value()
IhorNehrutsa Oct 13, 2021
48766b8
Revert "Remove set_value()"
IhorNehrutsa Oct 13, 2021
da5f2ea
wip
IhorNehrutsa Oct 13, 2021
4a3b043
Update esp32_pcnt.c
IhorNehrutsa Oct 13, 2021
5f66d83
Update pcnt.rst
IhorNehrutsa Oct 14, 2021
e994345
esp32/machine_pwm: Add PWM duty_u16() and duty_ns().
IhorNehrutsa Oct 15, 2021
87e9278
esp32/machine_pwm.c: Fix Changing the PWM frequency affects the duty …
IhorNehrutsa Oct 19, 2021
3bf8dff
docs/machine.PWM.rst: Add notes about frequency, duty and resolution.
IhorNehrutsa Oct 19, 2021
d9da9e5
esp32/machine_pwm.c Add support ESP32 S2, S3, C3.
IhorNehrutsa Oct 19, 2021
880ff57
esp32/machine.pwm: Fix rounding in resolution calculation.
IhorNehrutsa Oct 19, 2021
dec63ab
esp32\machine_pwm.c: Fix code formatting.
IhorNehrutsa Oct 20, 2021
4dfdc99
Merge branch 'master' of https://github.com/IhorNehrutsa/micropython
IhorNehrutsa Nov 20, 2021
d083596
esp32/esp32_pcnt.* : Add files for Pulse counter and Quadrature encoder.
IhorNehrutsa Nov 20, 2020
a5dddd4
esp32\mpconfigport.h: Revert after rebase.
IhorNehrutsa Nov 20, 2021
9155b59
py/mpprint.h: Add DBG() macro.
IhorNehrutsa Nov 30, 2021
1d650f2
Merge branch 'micropython:master' into master
IhorNehrutsa Dec 2, 2021
b6d9183
py/mprint.h: Change names to MP_PRN & MP_PRN_LEVEL.
IhorNehrutsa Dec 2, 2021
bc321a6
Merge branch 'master' of https://github.com/micropython/micropython i…
IhorNehrutsa Dec 3, 2021
136cb2e
Merge branch 'micropython-master' into master
IhorNehrutsa Dec 3, 2021
73456a1
esp32/esp32_pcnt.* : Add files for Pulse counter and Quadrature encoder.
IhorNehrutsa Nov 20, 2020
d60e562
esp32\mpconfigport.h: Revert after rebase.
IhorNehrutsa Nov 20, 2021
ef76b1f
Merge branch 'feature/esp32_pcnt_quad' of https://github.com/IhorNehr…
IhorNehrutsa Dec 3, 2021
f933fe9
Merge branch 'feature/esp32_pcnt_quad' of https://github.com/IhorNehr…
IhorNehrutsa Dec 4, 2021
2c471df
Update modesp32.c
IhorNehrutsa Dec 4, 2021
d05fc5c
Merge branch 'feature/esp32_pcnt_quad' of https://github.com/IhorNehr…
IhorNehrutsa Dec 4, 2021
d792e74
wip
IhorNehrutsa Dec 4, 2021
2d13bd3
Update index.rst
IhorNehrutsa Dec 4, 2021
666c4a2
fixes
IhorNehrutsa Dec 4, 2021
e1884e3
Update esp32_pcnt.c
IhorNehrutsa Dec 4, 2021
c36241d
esp32/esp32_pcnt.c: Remove unused functions.
IhorNehrutsa Dec 4, 2021
ce6e012
esp32\esp32_pcnt.*: Rename RAISE/FALL to RISING/FALLING.
IhorNehrutsa Dec 7, 2021
479dea2
esp32: Use 4 as default for x124.
IhorNehrutsa Dec 7, 2021
e1df144
Update esp32_pcnt.c
IhorNehrutsa Dec 8, 2021
c92cd69
esp32/esp32_pcnt.c: Add get_value().
IhorNehrutsa Dec 8, 2021
5fd5a3d
Update esp32_pcnt.c
IhorNehrutsa Dec 8, 2021
f9a1cea
esp32\PCNT: Correct docs.
IhorNehrutsa Dec 9, 2021
1e6e5d9
docs/machine: Remove Counter and Encoder.
IhorNehrutsa Dec 9, 2021
43dade5
esp32\machine_pwm.c: HOTFIX PWM duty.
IhorNehrutsa Dec 10, 2021
9a46918
py\mpprint.h: Change MP_PRN() delimiter to '|'.
IhorNehrutsa Dec 10, 2021
584fd31
Merge branch 'DBG_macro' into HOTFIX_esp32_PWM_duty
IhorNehrutsa Dec 10, 2021
5d79d19
Update pcnt.rst
IhorNehrutsa Dec 10, 2021
951c2ad
FILTER_MAX
IhorNehrutsa Dec 10, 2021
fa831de
Merge branch 'DBG_macro' into feature/esp32_pcnt_quad
IhorNehrutsa Dec 10, 2021
200dc6f
Merge remote-tracking branch 'origin/HOTFIX_esp32_PWM_duty' into feat…
IhorNehrutsa Dec 10, 2021
f111b1e
Update esp32_pcnt.c
IhorNehrutsa Dec 11, 2021
1aea642
esp32/PCNT: Constructor & init.
IhorNehrutsa Dec 12, 2021
23e74bd
Revert "Merge branch 'DBG_macro' into HOTFIX_esp32_PWM_duty"
IhorNehrutsa Dec 12, 2021
e9d0853
esp32/esp32_pcnt.c: Remove MP_PRN() macro.
IhorNehrutsa Dec 12, 2021
6c89fcc
esp32\Encoder: Rename esp32_pcnt to machine_encoder.
IhorNehrutsa Dec 12, 2021
b0321dc
Revert "Merge remote-tracking branch 'origin/HOTFIX_esp32_PWM_duty' i…
IhorNehrutsa Dec 12, 2021
2d5ffb6
esp32\pcnt: Revert after revert.
IhorNehrutsa Dec 12, 2021
56243c5
esp32/pcnt: Revert after revert.
IhorNehrutsa Dec 12, 2021
6492051
Update machine_pwm.c
IhorNehrutsa Dec 12, 2021
9b838a6
esp32\PCNT: ESP32 ESP32S2 ESP32S3 supported only.
IhorNehrutsa Dec 12, 2021
b5ec1c4
docs\esp32\PCNT: Change ref pcnt.Counter to esp32_machine.Counter.
IhorNehrutsa Dec 12, 2021
e9c7ac0
Update intro.rst
IhorNehrutsa Dec 13, 2021
64db6c3
Revert mpprint.h
IhorNehrutsa Dec 13, 2021
1bd26f8
Revert machine_pwm.c
IhorNehrutsa Dec 13, 2021
67dc2f3
Revert machine.rst
IhorNehrutsa Dec 13, 2021
6ef5ea3
esp32/Encoder: Fix GENERIC_C3 board.
IhorNehrutsa Dec 13, 2021
ccdde48
esp32/Encoder: Fix GENERIC_S3 board.
IhorNehrutsa Dec 13, 2021
9a7a8f1
Update quickref.rst
IhorNehrutsa Dec 13, 2021
af3d02d
Update machine_encoder.c
IhorNehrutsa Dec 13, 2021
ad1cf37
esp32/machine_encoder.c: Fix PCNT_UNIT_MAX.
IhorNehrutsa Dec 14, 2021
2c18c7a
esp32/encoder: WIP.
IhorNehrutsa Dec 16, 2021
5bbe52e
WIP
IhorNehrutsa Dec 20, 2021
5bd4a0b
WIP
IhorNehrutsa Dec 20, 2021
3e97b04
WIP
IhorNehrutsa Dec 20, 2021
5f92b4c
esp32/machine_encoder: Formatting.
IhorNehrutsa Dec 21, 2021
5056b9c
py/obj: Revert.
IhorNehrutsa Dec 21, 2021
4252a1d
Update machine_encoder.c
IhorNehrutsa Dec 21, 2021
79fab54
Update machine_encoder.c
IhorNehrutsa Dec 21, 2021
b3ef33e
Update machine_encoder.c
IhorNehrutsa Dec 21, 2021
4021243
Update pcnt.rst
IhorNehrutsa Dec 21, 2021
e1b9f1e
Add COUNTER_UP, COUNTER_DOWN
IhorNehrutsa Dec 21, 2021
4878e8f
WIP
IhorNehrutsa Dec 21, 2021
aa4b7a1
WIP
IhorNehrutsa Dec 22, 2021
dc011f7
Update pcnt.rst
IhorNehrutsa Dec 22, 2021
3be5036
esp32/machine_encoder.c: Fix RIZING, FALLING, counter_clear().
IhorNehrutsa Dec 22, 2021
132bc7d
esp32/machine_encoder: Constants as bitmap.
IhorNehrutsa Dec 22, 2021
39d51ab
esp32/machine_encoder: Fix ESP-IDF events _EVT_ const.
IhorNehrutsa Dec 23, 2021
0e6f8d1
Update machine_encoder.c
IhorNehrutsa Dec 23, 2021
26b3000
esp32/Encoder: Fix Soft Reset macine_encoder_deinit_all() for ESP-IDF…
IhorNehrutsa Dec 23, 2021
4bba75d
Update machine_encoder.c
IhorNehrutsa Dec 23, 2021
b1e6796
Update machine_encoder.c
IhorNehrutsa Dec 23, 2021
fe7ebef
WIP
IhorNehrutsa Dec 23, 2021
f1d8e37
WIP
IhorNehrutsa Dec 23, 2021
5c70097
Update machine_encoder.c
IhorNehrutsa Dec 24, 2021
a6cd7b9
Update pcnt.rst
IhorNehrutsa Dec 24, 2021
7e401ea
esp32/machine_encoder.c: Add pin_or_int() function.
IhorNehrutsa Dec 24, 2021
65106e6
Update machine_encoder.c
IhorNehrutsa Dec 24, 2021
8863da7
Update machine_encoder.h
IhorNehrutsa Dec 24, 2021
2ca2966
Update machine_encoder.c
IhorNehrutsa Dec 24, 2021
c166227
esp32/machine_encoder.c: irq(handler=None, trigger=Counter.IRQ_MATCH1…
IhorNehrutsa Dec 25, 2021
d332e20
esp32/machine_encoder.c: Fix A-B phase sequence.
IhorNehrutsa Dec 25, 2021
9f0e4a8
docs\esp32\pcnt.rst: irq(handler=None, trigger=Counter.IRQ_MATCH1 | C…
IhorNehrutsa Dec 25, 2021
dcbd120
esp32/machine_encoder: Make sure channel 1 is not set.
IhorNehrutsa Dec 27, 2021
de94ced
Update machine_encoder.c
IhorNehrutsa Dec 27, 2021
a7741b6
esp32/machine_encoder.c: Fix irq() method.
IhorNehrutsa Dec 27, 2021
f50055a
Update pcnt.rst
IhorNehrutsa Dec 28, 2021
563ab51
Update general.rst
IhorNehrutsa Dec 28, 2021
ee75e0d
Update general.rst
IhorNehrutsa Dec 11, 2021
50dfed8
Merge branch 'feature/esp32_pcnt_quad' of https://github.com/IhorNehr…
IhorNehrutsa Dec 28, 2021
a190c01
Merge branch 'master' into feature/esp32_pcnt_quad
IhorNehrutsa Dec 28, 2021
8cd53a2
Update pcnt.rst
IhorNehrutsa Jan 24, 2022
92bee8e
Update pcnt.rst
IhorNehrutsa Jan 24, 2022
3600c7f
Update pcnt.rst
IhorNehrutsa Jan 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/esp32/general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ For your convenience, some of technical specifications are provided below:
* SPI: 4 SPI interfaces (one used for FlashROM)
* I2C: 2 I2C (bitbang implementation available on any pins)
* I2S: 2
* CAN bus: 1
* ADC: 12-bit SAR ADC up to 18 channels
* DAC: 2 8-bit DACs
* PCNT: up to 8 channels
* PWM: up to 16 channels
* MCPWM: up to 2 channels
* RMT: 8 channels allowing accurate pulse transmit/receive
* Programming: using BootROM bootloader from UART - due to external FlashROM
and always-available BootROM bootloader, the ESP32 is not brickable
Expand Down
Binary file added docs/esp32/img/quad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
275 changes: 275 additions & 0 deletions docs/esp32/pcnt.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
PCNT - Counter and Encoder
==========================

The Counter and Encoder use the ESP32 Pulse Counter (PCNT) hardware peripheral,
see Espressif's `ESP-IDF Pulse Counter documentation.
<https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/pcnt.html>`_

For the counter not to miss any pulses, the pulse duration should be longer than one ESP32 APB_CLK cycle (1 / 80 MHz = 12.5 ns).
The pulses are sampled on the edges of the APB_CLK clock and may be missed if fall between the edges.
With ideal input signal maximum frequency of measured pulses is APB_CLK / 2 = 80 MHz / 2 = 40 MHz.

The inputs have optional filters that can be used to discard unwanted glitches in the signal.
The length of ignored pulses is provided in APB_CLK clock cycles.
* Note: Filter value is a 10-bit value, so the maximum filter value should be limited to 1023.
Maximum filtered glitches delay is 1023 * 12.5 ns = 12.7875 us.
Big filter make cutbacks the input frequency: 1 / (12.7875 us * 2) = 39.1 kHz.
* Note: Do not neglect circuitry methods to reduce noise (right powering and grounding, filtering, shielding,
short conductors, twisted pair cable, differential signals, etc.).

There is only one interrupt for the peripheral, and that is managed inside the module.
The user has no interrupt interface, and no interrupts are generated on each pulse.
Interrupts arrive when the 16-bit hardware counter buffer overflows, so this module has a tiny interrupt footprint
while providing support for up to 8 simultaneous counters (Encoder or Counter objects).

.. _esp32_machine.Counter:

Counter
=======

The Pulse Counter service.

Constructor
-----------

.. class:: Counter(id, src=None, \*, edge=Counter.RISING, direction=Counter.UP, filter_ns=0, scale=1)

The Counter starts to count immediately. Filtering is disabled.

- *id*. Values of *id* depend on a particular port and its hardware.
Values 0, 1, etc. are commonly used to select hardware block #0, #1, etc.

- *src* is the pulse input :ref:`machine.Pin <machine.Pin>` to be monitored.
*src* is required in the constructor.

- *direction* specifies the direction to count. Values for this include the constants

- Counter.UP - (default value) and
- Counter.DOWN to control the direction by software or
- :ref:`machine.Pin <machine.Pin>` object to control the direction externally. If ``Pin.value()``:
- 0 - count down,
- 1 - count up.

- *edge* specifies which edges of the input signal will be counted by Counter:

- Counter.RISING : raise edges,
- Counter.FALLING : fall edges,
- Counter.RISING | Counter.FALLING : both edges.

- *filter_ns* specifies a ns-value for the minimal time a signal has to be stable
at the input to be recognized. The largest value is 12787ns (1023 * 1000000000 / APB_CLK_FREQ).
The default is 0 – no filter.

- *scale* sets the scale value. The default value is 1. You may treat the scale
factor as **click per count**, **mm pe F438 r count**, **inch per count** etc.

Methods
-------

.. method:: Counter.init(*, src, ...)

Modify the settings of the Counter object. See the **Constructor** for details about the parameters.

.. method:: Counter.deinit()

Stops the Counter, disables interrupts and releases hardware resources used by the counter.
A Soft Reset involve deinitializing all Encoder objects.

.. method:: Counter.filter([value])

Get, and optionally set, the filter value. 0 disable filtering.

.. method:: Counter.value([value])

Get, and optionally set, the counter *value* as a signed 64-bit integer.

.. method:: Counter.scaled([value])

Get, and optionally set, the current scaled value of the Counter as a float.

Pseudocode is::

def scaled(self, scaled=None):
_scaled = self._value * self.scale
if scaled is not None:
self._value = round(scaled / self.scale)
return _scaled

.. method:: Counter.irq(handler=None, trigger=Counter.IRQ_MATCH1 | Counter.IRQ_MATCH2 | Counter.IRQ_ZERO, value=0)

-*handler* specifies a function is called when the respective *trigger* event happens.
The callback function *handler* receives a single argument, which is the Counter object.
All events may share the same callback or have separate callbacks.
The callback will be disabled, when called with handler=None. Counter.irq() disable all callbacks.
The event which triggers the callback can be identified with the ``Counter.status()`` method.
The Counter object which triggers the callback can be identified with the ``Counter.id()`` method.

-*trigger* events may be:

- Counter.IRQ_MATCH1 triggered when the counter matches the match1 value.
- Counter.IRQ_MATCH2 triggered when the counter matches the match2 value.
- Counter.IRQ_ZERO triggered when the counter matches the 0.

The default is - trigger=Counter.IRQ_MATCH1 | Counter.IRQ_MATCH2 | Counter.IRQ_ZERO.
The events are triggered when the counter value and match value are identical, but
callbacks have always a latency.

- *value* sets a counter match1/match2 value. When the counter matches these values,
a callback function can be called. They are 0 by default.

Attention: ``Counter.irq()`` resets counter to 0.

.. method:: Counter.status()

Returns the event status flags of the recent handled Counter interrupt as a bitmap.

.. method:: Counter.id()

Returns id number.

.. method:: Counter.pause()

.. method:: Counter.resume()

Constants
---------

.. data:: Counter.UP
Counter.DOWN

Selects the counter direction.

.. data:: Counter.RISING
Counter.FALLING

Selects the counted edges.

.. data:: Counter.IRQ_MATCH1
Counter.IRQ_MATCH2
Counter.IRQ_ZERO

Selects callback triggers.

::

from machine import Counter, Pin

try:
def irq_handler(self):
print('irq_handler()', self.id(), self.status(), self.value())

cnt = Counter(0, src=Pin(17, mode=Pin.IN), direction=Pin(16, mode=Pin.IN))

cnt.pause()
flt = cnt.filter() # return current filter value.
cnt.filter(10_000) # filter delay is 10ms
c = cnt.value(0) # get current counter value, set the counter value to 0
cnt.irq(irq_handler, Counter.IRQ_ZERO) # set irq handler
cnt.resume()

_c = None
while True:
c = cnt.value() # get the counter value
if _c != c:
_c = c
print('Counter =', c)
finally:
cnt.deinit 10000 () # free the input pins and counter.


.. _esp32_machine.Encoder:

Encoder
=======

This class provides a Quadrature Incremental Encoder service.
See `Quadrature encoder outputs.
<https://en.wikipedia.org/wiki/Incremental_encoder#Quadrature_outputs>`_

.. image:: img/quad.png
:width: 397px

Constructor
-----------

.. class:: Encoder(id, phase_a=None, phase_b=None, \*, x124=4, filter_ns=0, scale=1, match1=0, match2=0)

The Encoder starts to count immediately. Filtering is disabled.

- *id*. Values of *id* depend on a particular port and its hardware.
Values 0, 1, etc. are commonly used to select hardware block #0, #1, etc.

- *phase_a*, *phase_b* are input pins :ref:`machine.Pin <machine.Pin>` for monitoring of quadrature encoder pulses.
They are required in the constructor.

- *x124* is a hardware multiplier, possible values is 1, 2, 4. The default value is 4.
More info in `Quadrature decoder state table <https://en.wikipedia.org/wiki/Incremental_encoder#Quadrature_decoder>`_.
When more Encoder resolution is needed, it is possible for the encoder to count the leading
and trailing edges of the quadrature encoder’s pulse train from one channel,
which doubles (x2) the number of pulses. Counting both leading and trailing edges
of both channels (A and B channels) of a quadrature encoder will quadruple (x4) the number of pulses:

- 1 - count the leading(or trailing) edges from one phase channel.
- 2 - count the leading and trailing edges from one phase channel.
- 4 - count both leading and trailing edges of both phase channels.

- *scale* sets the scale value. The default value is 1. You may treat the scale
factor as **click per impulse**, **revolution per impulse**, **angle per impulse** etc.
Hint: Set scale factor to 1/4 to balance the multiplier x124=4.

These keywords are the same as the Counter keywords, see above:
- *filter_ns*
- *match1*
- *match2*

Methods
-------

.. method:: Encoder.init(*, phase_a, ...)

Modify the settings of the Encoder object. See the **Constructor** for details about the parameters.

The Encoder has the same methods as the Counter and differs only
in the constructor and internal hardware PCNT initialization.

Constants
---------

.. data:: Encoder.IRQ_MATCH1
Encoder.IRQ_MATCH2
Encoder.IRQ_ZERO

Selects callback triggers.

::

from machine import Encoder, Pin

try:
n = 0
def irq_handler1(self):
n -= 1
print('irq_handler1()', self.id(), self.value(), n)

def irq_handler2(self):
n += 1
print('irq_handler2()', self.id(), self.value(), n)

enc = Encoder(0, phase_a=Pin(17, mode=Pin.IN), phase_b=Pin(16, mode=Pin.IN), match1=-1000, match2=1000)

enc.pause()
flt = enc.filter() # return current filter value.
enc.filter(10_000) # filter delay is 10ms
c = enc.value(0) # get current encoder value, set the encoder value to 0
enc.irq(irq_handler1, Encoder.IRQ_MATCH1) # set irq handler
enc.irq(irq_handler2, Encoder.IRQ_MATCH2) # set irq handler
enc.resume()

_c = None
while True:
c = enc.value() # get the encoder value
if _c != c:
_c = c
print('Encoder =', c)
finally:
enc.deinit() # free the input pins and encoder.
39 changes: 39 additions & 0 deletions docs/esp32/quickref.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,45 @@ The RMT is ESP32-specific and allows generation of accurate digital pulses with
# The channel resolution is 100ns (1/(source_freq/clock_div)).
r.write_pulses((1, 20, 2, 40), start=0) # Send 0 for 100ns, 1 for 2000ns, 0 for 200ns, 1 for 4000ns

Counter (Pulse/Edge Counter)
----------------------------

The Counter counts the number of rising and/or falling edges on any input pin.
It is a 64-bit signed hardware-based counter. Counter and Encoder share the same ESP32 PCNT hardware peripheral,
the total summary available number of Counter and Encoder is up to 8.

See :ref:`machine.Counter <esp32_machine.Counter>` for details. Simplest usage is::

from machine import Pin, Counter

cnt = Counter(0, src=Pin(17, mode=Pin.IN), direction=Pin(16, mode=Pin.IN))
_v = None
while True:
v = cnt.value() # get 64-bit signed value
if _v != v:
_v = v
print('Counter value:', v)

Encoder (Quadrature Incremental Encoder)
----------------------------------------

The Encoder counts the quadrature-encoded pulses on pair of input pins (two square wave signals A and B with
~50% duty cycle and ~90-degree phase difference between them).
It is a 64-bit signed hardware-based counter. Counter and Encoder share the same ESP32 PCNT hardware peripheral,
the total summary available number of Counter and Encoder is up to 8.

See :ref:`machine.Encoder <esp32_machine.Encoder>` for details. Simplest usage is::

from machine import Pin, Encoder

enc = Encoder(0, phase_a=Pin(17, mode=Pin.IN), phase_b=Pin(16, mode=Pin.IN))
_v = None
while True:
v = enc.value() # get 64-bit signed value
if _v != v:
_v = v
print('Encoder value:', v)

OneWire driver
--------------

Expand Down
1 change: 0 additions & 1 deletion docs/esp32/tutorial/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ to `<https://www.python.org>`__.

.. toctree::
:maxdepth: 1
:numbered:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< 179B input type="hidden" name="authenticity_token" value="WP_dO_dxeYLU5GXQRxG0ZZ_dTuo5ZSPd2Lsvcey1cZlj4G-2S3hrvRaDquklvhtTcQ2U7n-0JFcsDtmGilCd1A" autocomplete="off" />

This is not a mistake. Numbering is not required here.


intro.rst
pwm.rst
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/boards/GENERIC_C3/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#define MICROPY_HW_ENABLE_SDCARD (0)
#define MICROPY_PY_MACHINE_DAC (0)
#define MICROPY_PY_MACHINE_I2S (0)
#define MICROPY_PY_MACHINE_PCNT (0)
1 change: 1 addition & 0 deletions ports/esp32/boards/GENERIC_C3_USB/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#define MICROPY_HW_ENABLE_SDCARD (0)
#define MICROPY_PY_MACHINE_DAC (0)
#define MICROPY_PY_MACHINE_I2S (0)
#define MICROPY_PY_MACHINE_PCNT (0)
Loading
0