Skip to content

Commit 61bbd78

Browse files
robert-hhdpgeorge
authored andcommitted
mimxrt: Implement Quadrature Encoder and Counter classes.
These classes are based on the Quadrature Encoder blocks of the i.MXRT MCUs. The i.MXRT 102x has two encoders, the other ones four. The i.MXRT 101x does not support this function. It is implemented as two classes, Encoder and Counter. The number of pins that can be uses as inputs is limited by the MCU architecture and the board schematics. The Encoder class supports: - Defining the module. - Defining the input pins. - Defining a pin for an index signal. - Defining a pin for a reset signal. - Defining an output pin showing the compare match signal. - Setting the number of cycles per revolution (min/max). - Setting the initial value for the position. - Setting the counting direction. - Setting a glitch filter. - Defining callbacks for getting to a specific position, overrun and underrun (starting the next revolution). These callbacks can be hard interrupts to ensure short latency. The encoder counts all phases of a cycle. The span for the position is 2**32, for the revolution is 2**16. The highest input frequency is CPU-Clock/24. Note that the "phases" argument is emulated at the API level (the hardware will always count all phases). The Counter mode counts single pulses on input A of the Encoder. The configuration supports: - Defining the module. - Defining the input pin. - Defining the counting direction, either fixed or controlled by the level of an input pin. - Defining a pin for an index signal. - Defining an output pin showing the compare match signal. - Setting the counter value. - Setting the glitch filter. - Defining a callback which is called at a certain value. - Settings for MIMXRT1015. The MIMXRT1015 MCU has only one encoder/counter unit. The counting range is 0 - 2**32-1 and a 16 bit overrun counter. The highest input frequency is CPU-Clock/12. The implementation of the `.irq()` method uses the common code from `shared/runtime/mpirq.c`, including the `irq().flags()` and `irq().trigger()` methods. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent c3ca843 commit 61bbd78

13 files changed

Lines changed: 921 additions & 14 deletions

File tree

extmod/modmachine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ extern const mp_obj_type_t machine_timer_type;
217217
extern const mp_obj_type_t machine_uart_type;
218218
extern const mp_obj_type_t machine_usbd_type;
219219
extern const mp_obj_type_t machine_wdt_type;
220+
#if MICROPY_PY_MACHINE_QECNT
221+
extern const mp_obj_type_t machine_encoder_type;
222+
extern const mp_obj_type_t machine_counter_type;
223+
#endif
220224

221225
#if MICROPY_PY_MACHINE_SOFTI2C
222226
extern const mp_obj_type_t mp_machine_soft_i2c_type;

ports/mimxrt/Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,16 @@ endif
190190
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES), MIMXRT1015 MIMXRT1021 MIMXRT1052 MIMXRT1062 MIMXRT1064 MIMXRT1176))
191191
SRC_HAL_IMX_C += \
192192
$(MCUX_SDK_DIR)/drivers/qtmr_1/fsl_qtmr.c \
193+
$(MCUX_SDK_DIR)/drivers/enc/fsl_enc.c \
194+
$(MCUX_SDK_DIR)/drivers/xbara/fsl_xbara.c \
193195
$(MCU_DIR)/drivers/fsl_romapi.c
194196

195-
INC_HAL_IMX += -I$(TOP)/$(MCUX_SDK_DIR)/drivers/qtmr_1
197+
INC_HAL_IMX += \
198+
-I$(TOP)/$(MCUX_SDK_DIR)/drivers/enc \
199+
-I$(TOP)/$(MCUX_SDK_DIR)/drivers/xbara \
200+
-I$(TOP)/$(MCUX_SDK_DIR)/drivers/qtmr_1
201+
202+
CFLAGS += -DMICROPY_PY_MACHINE_QECNT=1
196203
endif
197204

198205
# If not empty, then it is 10xx.
@@ -256,6 +263,7 @@ SRC_C += \
256263
machine_i2c.c \
257264
machine_led.c \
258265
machine_pin.c \
266+
machine_encoder.c \
259267
machine_rtc.c \
260268
machine_sdcard.c \
261269
machine_spi.c \

ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@
8282
I2S_GPIO(1, WS, TX, GPIO_07, IOMUXC_GPIO_07_SAI1_TX_SYNC), \
8383
I2S_GPIO(1, SD, TX, GPIO_04, IOMUXC_GPIO_04_SAI1_TX_DATA00), \
8484
}
85+
86+
#define XBARA1 XBARA

ports/mimxrt/boards/MIMXRT1011_af.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ GPIO_AD_03,LPSPI1_SDI,PIT_TRIGGER3,FLEXPWM1_PWM2_B,KPP_ROW2,GPT2_CLK,GPIO1_IO17,
2020
GPIO_AD_04,LPSPI1_SDO,PIT_TRIGGER2,FLEXPWM1_PWM2_A,KPP_COL2,GPT2_COMPARE1,GPIO1_IO18,SNVS_VIO_5_CTL,,,,ADC1_IN4,,ALT5
2121
GPIO_AD_05,LPSPI1_PCS0,PIT_TRIGGER1,FLEXPWM1_PWM3_B,KPP_ROW1,GPT2_CAPTURE1,GPIO1_IO19,,,,,ADC1_IN5,,ALT5
2222
GPIO_AD_06,LPSPI1_SCK,PIT_TRIGGER0,FLEXPWM1_PWM3_A,KPP_COL1,GPT2_COMPARE2,GPIO1_IO20,LPI2C1_HREQ,,,,ADC1_IN6,,ALT5
23-
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
23+
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
2424
GPIO_AD_08,LPI2C2_SCL,LPUART3_TXD,ARM_CM7_TXEV,LPUART2_CTS_B,GPT2_COMPARE3,GPIO1_IO22,EWM_OUT_B,JTAG_TRSTB,,,ADC1_IN8,,ALT7
2525
GPIO_AD_09,LPSPI2_SDI,FLEXPWM1_PWM3_X,KPP_ROW2,ARM_TRACE_SWO,FLEXIO1_IO21,GPIO1_IO23,REF_CLK_32K,JTAG_TDO,,,ADC1_IN9,,ALT7
2626
GPIO_AD_10,LPSPI2_SDO,FLEXPWM1_PWM2_X,KPP_COL2,PIT_TRIGGER3,FLEXIO1_IO22,GPIO1_IO24,USB_OTG1_ID,JTAG_TDI,,,ADC1_IN10,,ALT7
2727
GPIO_AD_11,LPSPI2_PCS0,FLEXPWM1_PWM1_X,KPP_ROW1,PIT_TRIGGER2,FLEXIO1_IO23,GPIO1_IO25,WDOG1_B,JTAG_MOD,,,ADC1_IN11,,ALT7
2828
GPIO_AD_12,LPSPI2_SCK,FLEXPWM1_PWM0_X,KPP_COL1,PIT_TRIGGER1,FLEXIO1_IO24,GPIO1_IO26,USB_OTG1_PWR,JTAG_TCK,,,ADC1_IN12,,ALT7
2929
GPIO_AD_13,LPI2C1_SDA,LPUART3_RTS_B,KPP_ROW0,LPUART4_RTS_B,FLEXIO1_IO25,GPIO1_IO27,ARM_NMI,JTAG_TMS,,,ADC1_IN13,,ALT7
30-
GPIO_AD_14,LPI2C1_SCL,LPUART3_CTS_B,KPP_COL0,LPUART4_CTS_B,FLEXIO1_IO26,GPIO1_IO28,REF_CLK_24M,XBAR1_INOUT02,,,ADC1_IN14,,ALT5
30+
GPIO_AD_14,LPI2C1_SCL,LPUART3_CTS_B,KPP_COL0,LPUART4_CTS_B,FLEXIO1_IO26,GPIO1_IO28,REF_CLK_24M,XBAR_INOUT02,,,ADC1_IN14,,ALT5
3131
GPIO_SD_00,FLEXSPI_B_SS0_B,SAI3_TX_SYNC,ARM_CM7_RXEV,CCM_STOP,FLEXIO1_IO06,GPIO2_IO00,SRC_BT_CFG2,,,,,,ALT5
3232
GPIO_SD_01,FLEXSPI_B_DATA1,SAI3_TX_BCLK,FLEXPWM1_PWM0_B,CCM_CLKO2,FLEXIO1_IO07,GPIO2_IO01,SRC_BT_CFG1,,,,,,ALT5
3333
GPIO_SD_02,FLEXSPI_B_DATA2,SAI3_TX_DATA,FLEXPWM1_PWM0_A,CCM_CLKO1,FLEXIO1_IO08,GPIO2_IO02,SRC_BT_CFG0,,,,,,ALT5

ports/mimxrt/boards/MIMXRT1015_EVK/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@
8787
I2S_GPIO(1, WS, TX, GPIO_EMC_27, IOMUXC_GPIO_EMC_27_SAI1_TX_SYNC), \
8888
I2S_GPIO(1, SD, TX, GPIO_EMC_25, IOMUXC_GPIO_EMC_25_SAI1_TX_DATA00), \
8989
}
90+
91+
#define XBARA1 XBARA

ports/mimxrt/boards/MIMXRT1015_EVK/pins.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ A4,GPIO_AD_B1_15
2222
A5,GPIO_AD_B1_14
2323
RX,GPIO_EMC_33
2424
TX,GPIO_EMC_32
25+
ENC1,GPIO_EMC_06
26+
ENC2,GPIO_EMC_07
2527
SDA,GPIO_AD_B1_15
2628
SCL,GPIO_AD_B1_14
2729
SCK,GPIO_AD_B0_10

ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,5 @@
181181
{ IOMUXC_GPIO_AD_B0_15_ENET_TDATA01, 0, 0xB0E9u }, \
182182
{ IOMUXC_GPIO_EMC_40_ENET_MDIO, 0, 0xB0E9u }, \
183183
{ IOMUXC_GPIO_EMC_41_ENET_MDC, 0, 0xB0E9u },
184+
185+
#define XBARA1 XBARA

ports/mimxrt/boards/OLIMEX_RT1010/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@
8888
I2S_GPIO(3, WS, TX, GPIO_SD_00, IOMUXC_GPIO_SD_00_SAI3_TX_SYNC), /* pin D9 */ \
8989
I2S_GPIO(3, SD, TX, GPIO_SD_02, IOMUXC_GPIO_SD_02_SAI3_TX_DATA) /* pin D11 */ \
9090
}
91+
92+
#define XBARA1 XBARA

0 commit comments

Comments
 (0)