Skip to content

Commit ca8a6bb

Browse files
committed
docs/machine.Counter: Document new Counter class.
Document a new Counter API, currently only implemented for the ESP32 port. Also add port-specific API to ESP32 quickref document.
1 parent ccc41d8 commit ca8a6bb

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

docs/esp32/quickref.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,98 @@ ESP32 specific ADC class method reference:
303303
- ``ADC.WIDTH_11BIT``: 11 bit data
304304
- ``ADC.WIDTH_12BIT``: 12 bit data - this is the default configuration
305305

306+
Counter (pin pulse/edge counting)
307+
---------------------------------
308+
309+
The ESP32 provides up to 8 pulse counter peripherals depending on the hardware,
310+
with id 0..7. These can be configured to count rising and/or falling edges on
311+
any input pin.
312+
313+
Use the :ref:`machine.Counter <machine.Counter>` class::
314+
315+
from machine import Pin, Counter
316+
317+
counter = Counter(0, pin=Pin(2), rising=Counter.INCREMENT) # create counter
318+
counter.start() # start counter
319+
count = counter.value() # read count, -32768..32767
320+
counter.value(0) # reset counter
321+
count = counter.value(0) # read and reset
322+
323+
ESP32 specific Counter reference:
324+
325+
.. class:: Counter(id, *, pin, rising, falling, ...)
326+
327+
The constructor and ``init`` method support an additional set of
328+
keyword arguments over-and-above the basic
329+
:ref:`machine.Counter <machine.Counter>` API:
330+
331+
- ``mode_pin``: ESP32 pulse counters support monitoring a second pin and
332+
altering the behaviour of the counter based on its level - set this
333+
keyword to any input Pin
334+
- ``mode_low``: set to either ``Counter.HOLD`` or ``Counter.REVERSE`` to
335+
either suspend counting or reverse the direction of the counter (i.e.,
336+
``Counter.INCREMENT`` behaves as ``Counter.DECREMENT`` and vice versa)
337+
when ``mode_pin`` is low
338+
- ``mode_high``: as ``mode_low`` but for the behaviour when ``mode_pin``
339+
is high
340+
- ``filter``: set to a value 1..1023, in ticks of the 80MHz clock, to
341+
enable the pulse width filter
342+
- ``minimum``: set to the minimum level of the counter value when
343+
decrementing (-32768..0)
344+
- ``maximum``: set to the maximum level of the counter value when
345+
decrementing (0..32767)
346+
- ``threshold0``: sets the counter value for the
347+
``Counter.IRQ_THRESHOLD0`` event (see ``irq`` method)
348+
- ``threshold1``: sets the counter value for the
349+
``Counter.IRQ_THRESHOLD1`` event (see ``irq`` method)
350+
- ``channel``: see description below
351+
352+
Each pulse counter unit supports two channels, 0 and 1. By default, the
353+
constructor and ``init()`` method configure the 0 channel. Call the
354+
``init()`` method with ``channel=1`` to configure the other channel. Both
355+
channels update the same counter value, but are able to monitor different
356+
pins with different edge behaviour. The ``pin``, ``rising``, ``falling``,
357+
``mode_pin``, ``mode_low`` and ``mode_high`` keywords are per-channel, all
358+
other keyword arguments are per-unit and specifying them will override any
359+
previous configuration.
360+
361+
The second channel can be used to configure 4X quadrature decoding with a
362+
single counter unit::
363+
364+
pin_a = Pin(2, Pin.INPUT, pull=Pin.PULL_UP)
365+
pin_b = Pin(3, Pin.INPUT, pull=Pin.PULL_UP)
366+
rotary = Counter(0, pin=pin_a, falling=Counter.INCREMENT, rising=Counter.DECREMENT, mode_pin=pin_b, mode_low=Counter.REVERSE)
367+
rotary.init(channel=1, pin=pin_b, falling=Counter.DECREMENT, rising=Counter.INCREMENT, mode_pin=pin_a, mode_low=Counter.REVERSE)
368+
rotary.start()
369+
370+
.. method:: Counter.value([value])
371+
372+
Call this method with no arguments to return the current counter value or
373+
pass the value 0 to reset the counter and return the value before reset.
374+
ESP32 pulse counters do not support being set to any value other than 0.
375+
Reset and return is not atomic and so it is possible for a pulse to be
376+
missed.
377+
378+
.. method:: Counter.irq(target=Counter.IRQ_ZERO, handler=None)
379+
380+
ESP32 pulse counters support interrupts on these counter events:
381+
382+
- ``Counter.IRQ_ZERO``: the counter has reset to zero
383+
- ``Counter.IRQ_MINIMUM``: the counter has hit the ``minimum`` value
384+
- ``Counter.IRQ_MAXIMUM``: the counter has hit the ``maximum`` value
385+
- ``Counter.IRQ_THRESHOLD0``: the counter has hit the ``threshold0`` value
386+
- ``Counter.IRQ_THRESHOLD1``: the counter has hit the ``threshold1`` value
387+
388+
``target`` should be the desired events or'ed together and ``handler``
389+
should be a callable taking a single argument, which will be the Counter
390+
object. Only one handler can be in place per-unit. Set ``handler`` to
391+
``None`` to disable the event interrupt (or call ``irq`` with no arguments).
392+
393+
.. Note::
394+
ESP32 pulse counters reset to *zero* when reaching the minimum or maximum
395+
value. Thus the ``IRQ_ZERO`` event will also trigger when either of these
396+
events occurs.
397+
306398
Software SPI bus
307399
----------------
308400

docs/library/machine.Counter.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.. currentmodule:: machine
2+
.. _machine.Counter:
3+
4+
class Counter -- Pulse/Edge counter
5+
===================================
6+
7+
The Counter class provides hardware support for counting transitions on an input
8+
pin.
9+
10+
Example usage::
11+
12+
from machine import Pin, Counter
13+
14+
inp = Pin(2, Pin.INPUT)
15+
counter = Counter(0, pin=inp, rising=Counter.INCREMENT) # create a Counter on pin inp
16+
counter.start() # start counting transitions
17+
count = counter.value() # get the current count
18+
19+
.. Note::
20+
The Counter class is currently only implemented for the ESP32 port.
21+
22+
Constructor
23+
-----------
24+
25+
.. class:: Counter(id, *, pin, rising, falling)
26+
27+
Create a new Counter object with number ``id``. All ports should support
28+
at least one pulse counter with id 0, but may support a number of
29+
independent counters. ``pin`` is the :ref:`machine.Pin <machine.Pin>`
30+
to be monitored. For all ports, one of either the ``rising`` or
31+
``falling`` keyword arguments may be specified with either the
32+
``Counter.INCREMENT`` or ``Counter.DECREMENT`` constant to specify the
33+
edge and direction to count.
34+
35+
Counters begin in a stopped state, with count 0, and need to be started
36+
before they will count transitions of the pin.
37+
38+
Methods
39+
-------
40+
41+
.. method:: Counter.init(*, ...)
42+
43+
Reinitialize the Counter. See Constructor for keyword arguments.
44+
45+
.. method:: Counter.deinit()
46+
47+
Deinitialize the Counter.
48+
49+
.. method:: Counter.start()
50+
51+
Start the counter, i.e., monitor the pin for transitions.
52+
53+
.. method:: Counter.stop()
54+
55+
Stop the counter, i.e., finish monitoring the pin for transitions. The
56+
current counter value will be retained while the counter is stopped.
57+
58+
.. method:: Counter.value([value])
59+
60+
Returns the current counter value and optionally sets the counter value
61+
to the argument. All ports should support at least resetting the counter to
62+
0. A read and reset operation should be as close to atomic as possible for
63+
the hardware.
64+
65+
Constants
66+
---------
67+
68+
.. data:: Counter.INCREMENT
69+
70+
to increment the counter on an edge
71+
72+
.. data:: Counter.DECREMENT
73+
74+
to decrement the counter on an edge

docs/library/machine.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Classes
200200
machine.Signal.rst
201201
machine.ADC.rst
202202
machine.PWM.rst
203+
machine.Counter.rst
203204
machine.UART.rst
204205
machine.SPI.rst
205206
machine.I2C.rst

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy