Skip to content

Commit a392bf1

Browse files
committed
mimxrt: Add the documentation for the Encoder/Counter class.
This is the MIMXRT specific version, compliant to the documentation of PR micropython#8072 in the basic methods. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 664bed8 commit a392bf1

File tree

3 files changed

+443
-0
lines changed

3 files changed

+443
-0
lines changed

docs/library/index.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ The following libraries are specific to the Zephyr port.
192192

193193
.. _micropython_lib_extending:
194194

195+
Libraries specific to MIMXRT devices
196+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197+
198+
The following libraries are specific MIMXRT devices.
199+
200+
.. toctree::
201+
:maxdepth: 2
202+
203+
machine.QECNT.rst
204+
195205
Extending built-in libraries from Python
196206
----------------------------------------
197207

docs/library/machine.QECNT.rst

Lines changed: 377 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,377 @@
1+
.. currentmodule:: machine
2+
.. _mimxrt_machine.Encoder:
3+
4+
class Encoder -- Quadrature Encoder for i.MXRT MCUs
5+
====================================================
6+
7+
This class provides the Quadrature Encoder Service.
8+
9+
Example usage::
10+
11+
# Samples for Teensy
12+
#
13+
14+
from machine import Pin, Encoder
15+
16+
qe = Encoder(0, Pin(0), Pin(1)) # create Quadrature Encoder object
17+
qe.value() # get current counter values
18+
qe.value(0) # Set value and cycles to 0
19+
qe.init(cpc=128) # Specify 128 counts/cycle
20+
qe.init(index=Pin(3)) # Specify Pin 3 as Index pulse input
21+
qe.deinit() # turn off the Quadrature Encoder
22+
qe.irq(qe.IRQ_MATCH, value=100, handler=handler)
23+
# Call the function handler at a match event
24+
25+
qe # show the Encoder object properties
26+
27+
Constructors
28+
------------
29+
30+
.. class:: Encoder(id, phase_a, phase_b, *, home, match_pin, filter_ns, cpc, signed, index)
31+
32+
Construct and return a new quadrature encoder object using the following parameters:
33+
34+
- id: The number of the Encoder. The range is board-specific, starting with 0.
35+
For i.MX RT1015 and i.MX RT1021 based boards, this is 0..1, for i.MX RT1052,
36+
i.MX RT106x and i.MX RT11xx based boards it is 0..3.
37+
- *phase_a*. *phase_a* tells to which pin the phase a of the
38+
encoder is connected. This is usually a :ref:`machine.Pin <machine.Pin>`
39+
object, but a port may allow other values, like integers or strings,
40+
which designate a Pin in the machine.PIN class.
41+
- *phase_b*. *phase_b* tells to which pin the phase b of the
42+
encoder is connected. This is usually a :ref:`machine.Pin <machine.Pin>`
43+
object, but a port may allow other values, like integers or strings,
44+
which designate a Pin in the machine.PIN class.
45+
46+
Keyword arguments:
47+
48+
- *phase_a*\=value and *phase_b*\=value may be assigned by keyword arguments as well.
49+
- *home*\=value. A Pin specifier telling to which pin the home pulse is connected.
50+
At a rising slope of the home pulse the position counter is set to the init
51+
value, but the cycles counter is not changed. A *value* of *None* disables the home input.
52+
- *match_pin*\=value. A Pin specifier telling to which pin the match output is connected.
53+
This output will have a high level as long as the position counter matches the
54+
match value. The signal is generated by the encoder logic and requires no
55+
further software support. The pulse width is defined by the input signal frequency
56+
and can be very short, like 20ns, or stay, if the counter stops at the match position.
57+
A *value* of *None* disables the match output.
58+
- *filter_ns*\=value. Specifies a ns-value for the minimal time a signal has to be stable
59+
at the input to be recognized. The code does the best effort to configure the filter.
60+
The largest value is 20400 for the i.MXRT102x and 17000 for the i.MXRT105x/i.MXRT106x
61+
(1000000000 * 2550 * 4 / CPU_CLK). A value of 0 sets the filter off.
62+
- *cpc*\=value. Specify the number of counts per cycle. Since the
63+
Encoder counts all four phases of the input signal, the cpc value has to be four
64+
times the ppr value given in the encoder data sheet. The position counter will count up
65+
from the 0 up to cpc - 1, and then reset to the init value of 0 and increase
66+
the cycles counter by one. The default is: no cpc set. In that case the
67+
position counter overflows at 2**32 - 1. When counting down, the cycles counter changes
68+
at the transition from 0 to cpc - 1.
69+
- *signed*\=False|True tells, whether the value return by Encoder.value() is signed or
70+
unsigned. The default is ``True``.
71+
- *index*\=value. A Pin specifier telling to which pin the index pulse is connected.
72+
At a rising slope of the index pulse the position counter is set to the init value
73+
and the cycles counter is increased by one. A *value* of *None* disables the index input.
74+
75+
The arguments phase_a, phase_b and filter_ns are generic across ports, all other arguments are port-specific.
76+
77+
Methods
78+
-------
79+
80+
.. method:: Encoder.init(*, phase_a, phase_b, home, match_pin, filter_ns, cpc, signed, index)
81+
82+
Modify settings for the Encoder object. See the above constructor for details
83+
about the parameters.
84+
85+
.. method:: Encoder.deinit()
86+
87+
Stops the Encoder, disables interrupts and releases the resources used by the encoder. On
88+
Soft Reset, all instances of Encoder and Counter are deinitialized.
89+
90+
.. method:: value=Encoder.value([value])
91+
92+
Get or set the current position counter of the Encoder as signed or unsigned 32 bit integer,
93+
depending on the signed=xxx keyword option of init().
94+
95+
With no arguments the actual position counter value is returned.
96+
97+
With a single *value* argument the position counter is set to that value and the
98+
cycles counter is cleared. The methods returns the previous value.
99+
100+
.. method:: cycles=Encoder.cycles([value])
101+
102+
Get or set the current cycles counter of the Encoder as signed 16 bit integer.
103+
104+
With no arguments the actual cycles counter value is returned.
105+
106+
With a single *value* argument the cycles counter is set to that value. The
107+
position counter is not changed. The methods returns the previous value.
108+
109+
If the value returned by Encoder.value() is unsigned,
110+
the total position can be calculated as cycles() * cpc + value().
111+
If the total position range is still too small, you can create your own cycles
112+
counter using the irq() callback method.
113+
114+
.. method:: Encoder.irq(trigger=event, value=nnn, handler=handler, hard=False)
115+
116+
Specifies, that the *handler* is called when the respective *event* happens.
117+
118+
*event* may be:
119+
- Encoder.IRQ_MATCH Triggered when the position counter matches the match value.
120+
- Encoder.IRQ_ROLL_OVER Triggered when the position counter rolls over from the highest
121+
to the lowest value.
122+
- Encoder.IRQ_ROLL_UNDER Triggered when the position counter rolls under from the lowest
123+
to the highest value.
124+
125+
The callback is called, when the Encoder is at *value*. For fas signals, the actual counter
126+
value may be different from the trigger value.
127+
The callback function *handler* receives a single argument, which is the Encoder object. All
128+
events share the same callback. The event which triggers the callback can be identified
129+
with the Encoder.status() method. The argument *hard* specifies, whether the callback is called
130+
as a hard interrupt or as regular scheduled function. Hard interrupts have always a short latency,
131+
but are limited in that they must not allocate memory. Regular scheduled functions are not limited
132+
in what can be used, but depending on the load of the device execution may be delayed.
133+
Under low load, the difference in latency is minor.
134+
135+
The default arguments values are trigger=0, handler=None, hard=False. The callback will be
136+
disabled, when called with handler=None.
137+
138+
The position match event is triggered as long as the position and match value are identical.
139+
Therefore the position match callback is run in a one-shot fashion, and has to be enabled
140+
again when the position has changed.
141+
142+
.. method:: Encoder.status()
143+
144+
Returns the event status flags of the recent handled Encoder interrupt as a bitmap.
145+
The assignment of events to the bits are:
146+
147+
- 0: Transition at the HOME signal. (*)
148+
- 1: Transition at the INDEX signal. (*)
149+
- 2: Watchdog event. (*)
150+
- 3 or Encoder.IRQ_MATCH: Position match event.
151+
- 4: Phase_A and Phase_B changed at the same time. (*)
152+
- 5 or Encoder.IRQ_ROLL_OVER: Roll-Over event of the counter.
153+
- 6 or Encoder.IRQ_ROLL_UNDER: Roll-Under event of the counter.
154+
- 7: Direction of the last count. 1 for counting up, 0 for counting down.
155+
156+
(*) These flags are defined, but not (yet) enabled.
157+
158+
.. method:: Encoder.id()
159+
160+
Return the id of the Encoder. That may be helpful for interrupt callback functions.
161+
162+
The Encoder was tested to work up to 25MHz on a Teensy. It may work at
163+
higher frequencies as well, but that was the limit of the test set-up.
164+
165+
166+
.. _mimxrt_machine.Counter:
167+
168+
class Counter-- Signal counter for i.MXRT MCUs
169+
==============================================
170+
171+
This class provides a Counter service using the Quadrature Encoder module
172+
173+
Example usage::
174+
175+
# Samples for Teensy
176+
#
177+
178+
from machine import Pin, Counter
179+
180+
counter = Counter(0, Pin(0)) # create Counter object
181+
counter.value() # get current counter value
182+
counter.value(0) # Set the counter to 0
183+
counter.init(cpc=128) # Specify 128 counts/cycle
184+
counter.deinit() # turn off the Counter
185+
counter.irq(Counter.IRQ_MATCH, handler) # Call the function handler at a counter match
186+
187+
counter # show the Counter object properties
188+
189+
Constructors
190+
------------
191+
192+
.. class:: Counter(id, src, *, direction, match_pin, filter_ns, cpc, match, signed, index)
193+
194+
Construct and return a new Counter object using the following parameters:
195+
196+
- id: The number of the Counter. The range is board-specific, starting with 0.
197+
For i.MX RT1015 and i.MX RT1021 based boards, this is 0..1, for i.MX RT1052,
198+
i.MX RT106x and i.MX RT11xx based boards it is 0..3.
199+
- *src*. *src* tells to which pin the counter
200+
is connected. This is usually a :ref:`machine.Pin <machine.Pin>`
201+
object, but a port may allow other values, like integers or strings,
202+
which designate a Pin in the machine.PIN class.
203+
204+
Keyword arguments:
205+
206+
- *src*\=value may be assigned by a keyword argument as well.
207+
- *direction*\=value. Specifying the direction of counting. Suitable values are:
208+
209+
- Counter.UP: Count up, with a roll-over to 0 at 2**48-1.
210+
- Counter.DOWN: Count down, with a roll-under to 2**48-1 at 0.
211+
- a :ref:`machine.Pin <machine.Pin>` object. The level at that pin controls
212+
the counting direction. Low: Count up, High: Count down.
213+
214+
- *match_pin*\=value. A Pin specifier telling to which pin the match output is connected.
215+
This output will have a high level as long as the lower 32 bit of the counter value
216+
matches the match value. The signal is generated by the encoder logic and
217+
requires no further software support. A *value* of *None* disables the match output.
218+
- *filter_ns*\=value. Specifies a ns-value for the minimal time a signal has to be stable
219+
at the input to be recognized. The code does the best effort to configure the filter.
220+
The largest value is 20400 for the i.MXRT102x and 17000 for the i.MXRT105x/i.MXRT106x
221+
(1000000000 * 2550 * 4 / CPU_CLK). A value of 0 sets the filter off.
222+
- *cpc*\=value. Specify the number of counts per cycle.The counter will count up
223+
from the 0 up to cpc - 1, and then reset to 0 and increase
224+
the cycles counter by one. The default is: no cpc set. In that case the
225+
counter overflows at 2**32 - 1. If the counting direction is DOWN, then the cycles
226+
counter is decreased when counting from 0 to cpc-1.
227+
- *signed*\=False|True tells, whether the value returned by Counter.value() is signed or
228+
unsigned. The default is ``True``.
229+
- *index*\=value. A Pin specifier telling to which pin the index pulse is connected.
230+
At a rising slope of the index pulse the counter is set to 0
231+
and the cycles counter is increased by one. A *value* of *None* disables the index input.
232+
233+
The arguments input, direction and filter are generic across ports, all other arguments are port-specific.
234+
235+
236+
Methods
237+
-------
238+
239+
.. method:: Counter.init( *, src, direction, match_pin, filter_ns, cpc, match, signed, index)
240+
241+
Modify settings for the Counter object. See the above constructor for details
242+
about the parameters.
243+
244+
.. method:: Counter.deinit()
245+
246+
Stops the Counter, disables interrupts and releases the resources used by the encoder. On
247+
Soft Reset, all instances of Encoder and Counter are deinitialized.
248+
249+
.. method:: value=Counter.value([value])
250+
251+
Get or set the current event value of the Counter. The value is returned as a signed or
252+
unsigned 32 bit integer, as defined with the signed=True/False option of init()
253+
254+
With a single *value* argument the counter is set to the lower 32 bits of that value,
255+
and the cycles counter to the bits 32-47 of the supplied number. The methods returns the
256+
previous value.
257+
258+
.. method:: cycles=Counter.cycles([value])
259+
260+
Get or set the current cycles counter of the counter as signed 16 bit integer.
261+
The value represents the overflow or underflow events of the 32bit basic counter.
262+
A total count can be calculated as cycles() * 0x100000000 + value().
263+
If the total count range is still too small, you can create your own overflow
264+
counter using the irq() callback method.
265+
266+
With no arguments the actual cycles counter value is returned.
267+
268+
With a single *value* argument the cycles counter is set to that value. The
269+
base counter is not changed. The methods returns the previous value.
270+
271+
.. method:: Counter.irq(trigger=event, value=nnn, handler=handler, hard=False)
272+
273+
Specifies, that the *handler* is called when the respective *event* happens.
274+
275+
*event* may be:
276+
- Counter.IRQ_COMPARE Triggered when the positions counter matches the match value.
277+
- Counter.IRQ_ROLL_OVER Triggered when the position counter rolls over from the highest
278+
to the lowest value.
279+
- Counter.IRQ_ROLL_UNDER Triggered when the position counter rolls under from the lowest
280+
to the highest value.
281+
282+
The callback is called, when the Counter is at *value*. For fast signals, the actual counter
283+
value may be different from the trigger value.
284+
The callback function *handler* receives a single argument, which is the Counter object. All
285+
events share the same callback. The event which triggers the callback can be identified
286+
with the Counter.status() method. The argument *hard* specifies, whether the callback is called
287+
as a hard interrupt or as regular scheduled function. Hard interrupts have always a short latency,
288+
but are limited in that they must not allocate memory. Regular scheduled functions are not limited
289+
in what can be used, but depending on the load of the device execution may be delayed.
290+
Under low load, the difference in latency is minor.
291+
292+
The default arguments values are trigger=0, handler=None, hard=False. The callback will be
293+
disabled, when called with handler=None.
294+
295+
The counter match event is triggered as long as the lower 32 bit of the counter and match
296+
value match. Therefore the counter match callback is run in a one-shot fashion, and has to be enabled
297+
again when the counter value has changed.
298+
299+
.. method:: Counter.status()
300+
301+
Returns the event status flags of the recent handled Counter interrupt as a bitmap.
302+
The assignment of events to the bits are:
303+
304+
- 0: Transition at the HOME signal. (*)
305+
- 1: Transition at the INDEX signal. (*)
306+
- 2: Watchdog event. (*)
307+
- 3 or Counter.IRQ_MATCH: Position match event.
308+
- 4: Phase_A and Phase_B changed at the same time. (*)
309+
- 5 or Counter.IRQ_ROLL_OVER: Roll-Over event of the counter.
310+
- 6 or Counter.IRQ_ROLL_UNDER: Roll-Under event of the counter.
311+
- 7: Direction of the last count. 1 for counting up, 0 for counting down.
312+
313+
(*) These flags are defined, but not (yet) enabled.
314+
315+
.. method:: Counter.id()
316+
317+
Return the id of the Counter. That may be helpful for interrupt callback functions.
318+
319+
320+
The counter was tested up to 50MHz. It may work at higher frequencies
321+
as well, but that was the limit of the test set-up.
322+
323+
Pin Assignment
324+
--------------
325+
326+
Pins are specified in the same way as for the Pin class. The pins available for an
327+
assignment to the Encoder or Counter are:
328+
329+
**IMXRT1010_EVK**:
330+
331+
Not supported.
332+
333+
**IMXRT1015_EVK**:
334+
335+
J30, pins 1 and 3, with the pin names "ENC1" and "ENC2".
336+
337+
**IMXRT1020_EVK**:
338+
339+
Pins D0 and D1.
340+
341+
**IMXRT1050_EVK**, **IMXRT1050_EVKB**, **IMXRT1060_EVK**, **IMXRT1064_EBK**:
342+
343+
Pins D2, D4, D5, D8, D9, D10, D11, D12, D13, D14, D15, A4, A5.
344+
Depending on the board configuration, not all pins may be wired.
345+
Pins D2, D4 and D5 cannot be used for the match output.
346+
347+
**IMXRT1170_EVK**:
348+
349+
Pins D0, D1, D2.
350+
351+
D2 is connected to the 1G PHY chip as well. So levels may be distorted.
352+
353+
**Teensy 4.0**:
354+
355+
Pins 0, 1, 2, 3, 4, 5, 7, 8, 26, 27, 30, 31, 32, 33. Pin 0 and 5 share the
356+
same signal and cannot be used independently.
357+
Pins 26, 27, 30 and 31 cannot be used for the match output.
358+
359+
**Teensy 4.1**:
360+
361+
Pins 0, 1, 2, 3, 4, 5, 7, 8, 26, 27, 30, 31, 32, 33, 37, 42, 43, 44, 45, 46 and 47.
362+
Pins 26, 27, 30 and 31 cannot be used for the match output.
363+
Some pins are assigned to the same signal and cannot be used independently. These are:
364+
365+
- Pin 0, 5 and 37,
366+
- Pin 2 and 43,
367+
- Pin 3 and 42, and
368+
- Pin 4 and 47.
369+
370+
**Seeed ARCH MIX**
371+
372+
Pins J3_14, J3_15, J4_19, J4_20, J5_15, J5_16, J5_17, J5_22, J5_23, J5_24, J5_25 and J5_26.
373+
Pins J3_14 and J3_15 cannot be used for the match output.
374+
375+
**Olimex RT1010Py**
376+
377+
Not supported.

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