Skip to content

Commit 3c64b52

Browse files
authored
Merge pull request #7690 from jepler/mimxrt10xx-rotaryio
Add IncrementalEncoder for mimxrt1011
2 parents 5ae3be1 + df916e0 commit 3c64b52

File tree

16 files changed

+259
-5
lines changed

16 files changed

+259
-5
lines changed

main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,8 @@ void gc_collect(void) {
11111111
// have lost their references in the VM even though they are mounted.
11121112
gc_collect_root((void **)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
11131113

1114+
port_gc_collect();
1115+
11141116
background_callback_gc_collect();
11151117

11161118
#if CIRCUITPY_ALARM
@@ -1143,6 +1145,10 @@ void gc_collect(void) {
11431145
gc_collect_end();
11441146
}
11451147

1148+
// Ports may provide an implementation of this function if it is needed
1149+
MP_WEAK void port_gc_collect() {
1150+
}
1151+
11461152
void NORETURN nlr_jump_fail(void *val) {
11471153
reset_into_safe_mode(SAFE_MODE_NLR_JUMP_FAIL);
11481154
while (true) {

ports/mimxrt10xx/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ SRC_C += \
137137
peripherals/mimxrt10xx/$(CHIP_FAMILY)/clocks.c \
138138
peripherals/mimxrt10xx/$(CHIP_FAMILY)/periph.c \
139139
peripherals/mimxrt10xx/$(CHIP_FAMILY)/pins.c \
140+
peripherals/mimxrt10xx/pins.c \
140141
reset.c \
141142
supervisor/flexspi_nor_flash_ops.c
142143

ports/mimxrt10xx/common-hal/digitalio/DigitalInOut.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
#define IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5 5U
4242

43-
STATIC void pin_config(const mcu_pin_obj_t *pin, bool open_drain, digitalio_pull_t pull) {
43+
void pin_config(const mcu_pin_obj_t *pin, bool open_drain, digitalio_pull_t pull) {
4444
IOMUXC_SetPinConfig(0, 0, 0, 0, pin->cfg_reg,
4545
IOMUXC_SW_PAD_CTL_PAD_HYS(1)
4646
| IOMUXC_SW_PAD_CTL_PAD_PUS((pull == PULL_UP) ? 2 : 0)

ports/mimxrt10xx/common-hal/digitalio/DigitalInOut.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ typedef struct {
4040
digitalio_pull_t pull;
4141
} digitalio_digitalinout_obj_t;
4242

43+
void pin_config(const mcu_pin_obj_t *pin, bool open_drain, digitalio_pull_t pull);
44+
4345
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_DIGITALIO_DIGITALINOUT_H

ports/mimxrt10xx/common-hal/microcontroller/Pin.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "shared-bindings/microcontroller/Pin.h"
3030
#include "shared-bindings/microcontroller/__init__.h"
3131

32+
#include "py/gc.h"
33+
3234
STATIC bool claimed_pins[IOMUXC_SW_PAD_CTL_PAD_COUNT];
3335
STATIC bool never_reset_pins[IOMUXC_SW_PAD_CTL_PAD_COUNT];
3436

@@ -85,6 +87,7 @@ void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
8587
return;
8688
}
8789

90+
disable_pin_change_interrupt(pin);
8891
never_reset_pins[pin->mux_idx] = false;
8992
claimed_pins[pin->mux_idx] = false;
9093
*(uint32_t *)pin->mux_reg = pin->mux_reset;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Nick Moore for Adafruit Industries
7+
* Copyright (c) 2023 Jeff Epler for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "common-hal/digitalio/DigitalInOut.h"
29+
#include "common-hal/rotaryio/IncrementalEncoder.h"
30+
#include "shared-module/rotaryio/IncrementalEncoder.h"
31+
#include "shared-bindings/microcontroller/Pin.h"
32+
#include "shared-bindings/rotaryio/IncrementalEncoder.h"
33+
34+
#include "py/runtime.h"
35+
36+
#include "sdk/drivers/igpio/fsl_gpio.h"
37+
static void encoder_change(void *self_in) {
38+
rotaryio_incrementalencoder_obj_t *self = self_in;
39+
40+
bool value_a = GPIO_PinRead(self->pin_a->gpio, self->pin_a->number);
41+
bool value_b = GPIO_PinRead(self->pin_b->gpio, self->pin_b->number);
42+
uint8_t new_state = (value_a << 1) | value_b;
43+
shared_module_softencoder_state_update(self, new_state);
44+
}
45+
46+
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self,
47+
const mcu_pin_obj_t *pin_a, const mcu_pin_obj_t *pin_b) {
48+
49+
self->pin_a = pin_a;
50+
self->pin_b = pin_b;
51+
52+
// GPIO is always IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5 until proven otherwise
53+
#define IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5 5U
54+
IOMUXC_SetPinMux(pin_a->mux_reg, IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5, 0, 0, 0, 0);
55+
IOMUXC_SetPinMux(pin_b->mux_reg, IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5, 0, 0, 0, 0);
56+
57+
const gpio_pin_config_t config = { kGPIO_DigitalInput, 0, kGPIO_IntRisingOrFallingEdge };
58+
GPIO_PinInit(pin_a->gpio, pin_a->number, &config);
59+
GPIO_PinInit(pin_b->gpio, pin_b->number, &config);
60+
61+
enable_pin_change_interrupt(pin_a, encoder_change, self);
62+
enable_pin_change_interrupt(pin_b, encoder_change, self);
63+
64+
pin_config(pin_a, false, PULL_UP);
65+
pin_config(pin_b, false, PULL_UP);
66+
67+
claim_pin(pin_a);
68+
claim_pin(pin_b);
69+
}
70+
71+
bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t *self) {
72+
return !self->pin_a;
73+
}
74+
75+
void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t *self) {
76+
if (common_hal_rotaryio_incrementalencoder_deinited(self)) {
77+
return;
78+
}
79+
disable_pin_change_interrupt(self->pin_a);
80+
disable_pin_change_interrupt(self->pin_b);
81+
82+
common_hal_reset_pin(self->pin_a);
83+
common_hal_reset_pin(self->pin_b);
84+
85+
self->pin_a = NULL;
86+
self->pin_b = NULL;
87+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
7+
* Copyright (c) 2023 Jeff Epler for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#pragma once
29+
30+
#include "common-hal/microcontroller/Pin.h"
31+
32+
#include "py/obj.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
const mcu_pin_obj_t *pin_a, *pin_b;
37+
uint8_t state; // <old A><old B>
38+
int8_t sub_count; // count intermediate transitions between detents
39+
int8_t divisor; // Number of quadrature edges required per count
40+
mp_int_t position;
41+
} rotaryio_incrementalencoder_obj_t;
42+
43+
44+
void incrementalencoder_interrupt_handler(uint8_t channel);

ports/mimxrt10xx/common-hal/rotaryio/__init__.c

Whitespace-only changes.

ports/mimxrt10xx/common-hal/rotaryio/__init__.h

Whitespace-only changes.

ports/mimxrt10xx/mpconfigport.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ CIRCUITPY_I2CTARGET = 0
2424
CIRCUITPY_NVM = 0
2525
CIRCUITPY_PARALLELDISPLAY = 0
2626
CIRCUITPY_PULSEIO = 0
27-
CIRCUITPY_ROTARYIO = 0
27+
CIRCUITPY_ROTARYIO = 1
28+
CIRCUITPY_ROTARYIO_SOFTENCODER = 1
2829
CIRCUITPY_USB_MIDI = 1
2930
LONGINT_IMPL = MPZ
3031

0 commit comments

Comments
 (0)