Skip to content

mimxrt: Add Quadrature Encoder and Pulse Counter classes. #7911

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions docs/library/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ The following libraries are specific to the Zephyr port.

zephyr.rst


Libraries specific to MIMXRT devices
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following libraries are specific MIMXRT devices.

.. toctree::
:maxdepth: 2

machine.QECNT.rst

Extending built-in libraries from Python
----------------------------------------

Expand Down
377 changes: 377 additions & 0 deletions docs/library/machine.QECNT.rst

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions docs/mimxrt/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,62 @@ port and LAN(1) for the 1G port.

For details of the network interface refer to the class :ref:`network.LAN <network.LAN>`.

class Encoder -- Quadrature Encoder for i.MXRT MCUs
---------------------------------------------------

This class provides the Quadrature Encoder Service.

Example usage::

# Samples for Teensy
#

from machine import Pin, Encoder

qe = Encoder(0, Pin(0), Pin(1)) # create Quadrature Encoder object
qe.value() # get current counter values
qe.value(0) # Set value and cycles to 0
qe.init(cpc=128) # Specify 128 counts/cycle
qe.init(index=Pin(3)) # Specify Pin 3 as Index pulse input
qe.deinit() # turn off the Quadrature Encoder
qe.init(match=64) # Set a match event at count 64

Choose a reason for hiding this comment

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

I flashed my teensy 4.1 with this branch's build. I am following this doc and can see values. When I attempt to use this match keyword I receive an error.

>>> e0.init(match=100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for having a look at that class. It's always good if other people look at that. Here, the documentation has to be updated, since the match=xxx argument was removed from the init() call. That was done as result of trying to harmonize the various implementations. The same applies to the reversekeyword argument.

qe.irq(qe.IRQ_MATCH, value=100, handler=handler)
# Call the function handler at a match event

qe # show the Encoder object properties

The Quadrature Encoder is hardware based. It available on all MIMXRT devices exept the ones
based on the i.MX RT 1010 MCU. For details about using the Encoder with a MIMXRT board
see :ref:`machine.Encoder <mimxrt_machine.Encoder>`:


class Counter-- Signal counter for i.MXRT MCUs
----------------------------------------------

This class provides a Counter service using the Quadrature Encoder module

Example usage::

# Samples for Teensy
#

from machine import Pin, Counter

counter = Counter(0, Pin(0)) # create Counter object
counter.value() # get current counter value
counter.value(0) # Set the counter to 0
counter.init(cpc=128) # Specify 128 counts/cycle
counter.deinit() # turn off the Counter
counter.init(match=1000) # Create a match event at count 1000
counter.irq(Counter.IRQ_MATCH, handler) # Call the function handler at a counter match

counter # show the Counter object properties

The Quadrature Encoder is hardware based. It available on all MIMXRT devices exept the ones
based on the i.MX RT 1010 MCU. For details about using the Counter with a MIMXRT board
see :ref:`machine.Counter <mimxrt_machine.Counter>`:


Transferring files
------------------

Expand Down
7 changes: 6 additions & 1 deletion ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ endif

ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES), MIMXRT1015 MIMXRT1021 MIMXRT1052 MIMXRT1062 MIMXRT1064 MIMXRT1176))
SRC_HAL_IMX_C += \
$(MCU_DIR)/drivers/fsl_qtmr.c
$(MCU_DIR)/drivers/fsl_qtmr.c \
$(MCU_DIR)/drivers/fsl_enc.c \
$(MCU_DIR)/drivers/fsl_xbara.c

CFLAGS += -DMICROPY_PY_MACHINE_QECNT=1
endif

ifeq ($(MCU_SERIES), MIMXRT1176)
Expand Down Expand Up @@ -175,6 +179,7 @@ SRC_C += \
machine_i2s.c \
machine_led.c \
machine_pin.c \
machine_encoder.c \
machine_rtc.c \
machine_sdcard.c \
machine_spi.c \
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@
I2S_GPIO(1, WS, TX, GPIO_07, IOMUXC_GPIO_07_SAI1_TX_SYNC), \
I2S_GPIO(1, SD, TX, GPIO_04, IOMUXC_GPIO_04_SAI1_TX_DATA00), \
}

#define XBARA1 XBARA
4 changes: 2 additions & 2 deletions ports/mimxrt/boards/MIMXRT1011_af.csv
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ GPIO_AD_03,LPSPI1_SDI,PIT_TRIGGER3,FLEXPWM1_PWM2_B,KPP_ROW2,GPT2_CLK,GPIO1_IO17,
GPIO_AD_04,LPSPI1_SDO,PIT_TRIGGER2,FLEXPWM1_PWM2_A,KPP_COL2,GPT2_COMPARE1,GPIO1_IO18,SNVS_VIO_5_CTL,,,,ADC1_IN4,,ALT5
GPIO_AD_05,LPSPI1_PCS0,PIT_TRIGGER1,FLEXPWM1_PWM3_B,KPP_ROW1,GPT2_CAPTURE1,GPIO1_IO19,,,,,ADC1_IN5,,ALT5
GPIO_AD_06,LPSPI1_SCK,PIT_TRIGGER0,FLEXPWM1_PWM3_A,KPP_COL1,GPT2_COMPARE2,GPIO1_IO20,LPI2C1_HREQ,,,,ADC1_IN6,,ALT5
GPIO_AD_07,LPI2C2_SDA,LPUART3_RXD,ARM_CM7_RXEV,LPUART2_RTS_B,GPT2_CAPTURE2,GPIO1_IO21,OCOTP_FUSE_LATCHED,XBAR1_INOUT03,,,ADC1_IN7,,ALT5
GPIO_AD_07,LPI2C2_SDA,LPUART3_RXD,ARM_CM7_RXEV,LPUART2_RTS_B,GPT2_CAPTURE2,GPIO1_IO21,OCOTP_FUSE_LATCHED,XBAR_INOUT03,,,ADC1_IN7,,AL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
GPIO_SD_01,FLEXSPI_B_DATA1,SAI3_TX_BCLK,FLEXPWM1_PWM0_B,CCM_CLKO2,FLEXIO1_IO07,GPIO2_IO01,SRC_BT_CFG1,,,,,,ALT5
GPIO_SD_02,FLEXSPI_B_DATA2,SAI3_TX_DATA,FLEXPWM1_PWM0_A,CCM_CLKO1,FLEXIO1_IO08,GPIO2_IO02,SRC_BT_CFG0,,,,,,ALT5
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1015_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@
I2S_GPIO(1, WS, TX, GPIO_EMC_27, IOMUXC_GPIO_EMC_27_SAI1_TX_SYNC), \
I2S_GPIO(1, SD, TX, GPIO_EMC_25, IOMUXC_GPIO_EMC_25_SAI1_TX_DATA00), \
}

#define XBARA1 XBARA
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1015_EVK/pins.csv
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ A4,GPIO_AD_B1_15
A5,GPIO_AD_B1_14
RX,GPIO_EMC_33
TX,GPIO_EMC_32
ENC1,GPIO_EMC_06
ENC2,GPIO_EMC_07
SDA,GPIO_AD_B1_15
SCL,GPIO_AD_B1_14
SCK,GPIO_AD_B0_10
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,5 @@
{ IOMUXC_GPIO_AD_B0_15_ENET_TDATA01, 0, 0xB0E9u }, \
{ IOMUXC_GPIO_EMC_40_ENET_MDIO, 0, 0xB0E9u }, \
{ IOMUXC_GPIO_EMC_41_ENET_MDC, 0, 0xB0E9u },

#define XBARA1 XBARA
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/OLIMEX_RT1010/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@
I2S_GPIO(3, WS, TX, GPIO_SD_00, IOMUXC_GPIO_SD_00_SAI3_TX_SYNC), /* pin D9 */ \
I2S_GPIO(3, SD, TX, GPIO_SD_02, IOMUXC_GPIO_SD_02_SAI3_TX_DATA) /* pin D11 */ \
}

#define XBARA1 XBARA
6 changes: 5 additions & 1 deletion ports/mimxrt/boards/make-pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import csv
import re

SUPPORTED_AFS = {"GPIO", "USDHC", "FLEXPWM", "TMR"}
SUPPORTED_AFS = {"GPIO", "USDHC", "FLEXPWM", "TMR", "XBAR"}
MAX_AF = 10 # AF0 .. AF9
ADC_COL = 11

Expand Down Expand Up @@ -302,6 +302,8 @@ def print_header(self, hdr_filename):
module_instance_factory(self.cpu_pins, hdr_file, "USDHC")
module_instance_factory(self.cpu_pins, hdr_file, "FLEXPWM")
module_instance_factory(self.cpu_pins, hdr_file, "TMR")
# module_instance_factory(self.cpu_pins, hdr_file, "SEMC")
module_instance_factory(self.cpu_pins, hdr_file, "XBAR")


def module_instance_factory(pins, output_file, name):
Expand All @@ -326,6 +328,8 @@ def module_instance_factory(pins, output_file, name):
output_file.write(f"#define {k}_AVAIL (1)\n")
if name == "FLEXPWM":
output_file.write(f"#define {k} {k[-4:]}\n")
if name == "XBAR":
output_file.write(f"#define {k} {k[:4]}A1\n")
for i in v:
output_file.write(i + "\n")

Expand Down
Loading