Skip to content

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

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions docs/library/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ The following libraries are specific to the Zephyr port.

.. _micropython_lib_extending:

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
378 changes: 378 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("D0"), Pin("D1")) # 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
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 is available at all MIMXRT devices except 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("D0")) # 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 Counter is hardware based. It is available at all MIMXRT devices except 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
4 changes: 4 additions & 0 deletions extmod/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ extern const mp_obj_type_t machine_timer_type;
extern const mp_obj_type_t machine_uart_type;
extern const mp_obj_type_t machine_usbd_type;
extern const mp_obj_type_t machine_wdt_type;
#if MICROPY_PY_MACHINE_QECNT
extern const mp_obj_type_t machine_encoder_type;
extern const mp_obj_type_t machine_counter_type;
#endif

#if MICROPY_PY_MACHINE_SOFTI2C
extern const mp_obj_type_t mp_machine_soft_i2c_type;
Expand Down
5 changes: 5 additions & 0 deletions ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,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_enc.c \
$(MCU_DIR)/drivers/fsl_xbara.c \
$(MCU_DIR)/drivers/fsl_romapi.c

CFLAGS += -DMICROPY_PY_MACHINE_QECNT=1
endif

ifeq ($(MCU_SERIES), MIMXRT1176)
Expand Down Expand Up @@ -198,6 +202,7 @@ SRC_C += \
machine_i2c.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 @@ -82,3 +82,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,,ALT5
GPIO_AD_08,LPI2C2_SCL,LPUART3_TXD,ARM_CM7_TXEV,LPUART2_CTS_B,GPT2_COMPARE3,GPIO1_IO22,EWM_OUT_B,JTAG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FG1,,,,,,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 @@ -87,3 +87,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 @@ -181,3 +181,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 @@ -88,3 +88,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: 4 additions & 2 deletions ports/mimxrt/boards/make-pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
r"IOMUXC_(?P<pin>GPIO_SNVS_\d\d_DIG)_(?P<function>\w+) (?P<muxRegister>\w+), (?P<muxMode>\w+), (?P<inputRegister>\w+), (?P<inputDaisy>\w+), (?P<configRegister>\w+)",
]

SUPPORTED_AF_FNS = {"GPIO", "USDHC", "FLEXPWM", "TMR"}
SUPPORTED_AF_FNS = {"GPIO", "USDHC", "FLEXPWM", "TMR", "XBAR"}


class MimxrtPin(boardgen.Pin):
Expand Down Expand Up @@ -183,7 +183,7 @@ def load_inputs(self, out_source):
# the CMD pin of the USDHC1 function on the GPIO_SD_B0_00 pin.
def print_module_instances(self, out_header):
print(file=out_header)
for match_fn in ("USDHC", "FLEXPWM", "TMR"):
for match_fn in ("USDHC", "FLEXPWM", "TMR", "XBAR"):
module_instances = defaultdict(list)
for pin in self.available_pins():
for i, (_af_idx, _input_reg, _input_daisy, instance, fn, af) in enumerate(
Expand All @@ -200,6 +200,8 @@ def print_module_instances(self, out_header):
print("#define {:s}_AVAIL (1)".format(k), file=out_header)
if match_fn == "FLEXPWM":
print("#define {:s} {:s}".format(k, k[-4:]), file=out_header)
if match_fn == "XBAR":
print("#define {:s} {:s}A1".format(k, k[:4]), file=out_header)
for i in v:
print(i, file=out_header)

Expand Down
Loading
Loading