Merge pull request #7690 from jepler/mimxrt10xx-rotaryio
Add IncrementalEncoder for mimxrt1011
This commit is contained in:
commit
3c64b5212f
6
main.c
6
main.c
|
@ -1111,6 +1111,8 @@ void gc_collect(void) {
|
|||
// have lost their references in the VM even though they are mounted.
|
||||
gc_collect_root((void **)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
|
||||
|
||||
port_gc_collect();
|
||||
|
||||
background_callback_gc_collect();
|
||||
|
||||
#if CIRCUITPY_ALARM
|
||||
|
@ -1143,6 +1145,10 @@ void gc_collect(void) {
|
|||
gc_collect_end();
|
||||
}
|
||||
|
||||
// Ports may provide an implementation of this function if it is needed
|
||||
MP_WEAK void port_gc_collect() {
|
||||
}
|
||||
|
||||
void NORETURN nlr_jump_fail(void *val) {
|
||||
reset_into_safe_mode(SAFE_MODE_NLR_JUMP_FAIL);
|
||||
while (true) {
|
||||
|
|
|
@ -137,6 +137,7 @@ SRC_C += \
|
|||
peripherals/mimxrt10xx/$(CHIP_FAMILY)/clocks.c \
|
||||
peripherals/mimxrt10xx/$(CHIP_FAMILY)/periph.c \
|
||||
peripherals/mimxrt10xx/$(CHIP_FAMILY)/pins.c \
|
||||
peripherals/mimxrt10xx/pins.c \
|
||||
reset.c \
|
||||
supervisor/flexspi_nor_flash_ops.c
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
|
||||
#define IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5 5U
|
||||
|
||||
STATIC void pin_config(const mcu_pin_obj_t *pin, bool open_drain, digitalio_pull_t pull) {
|
||||
void pin_config(const mcu_pin_obj_t *pin, bool open_drain, digitalio_pull_t pull) {
|
||||
IOMUXC_SetPinConfig(0, 0, 0, 0, pin->cfg_reg,
|
||||
IOMUXC_SW_PAD_CTL_PAD_HYS(1)
|
||||
| IOMUXC_SW_PAD_CTL_PAD_PUS((pull == PULL_UP) ? 2 : 0)
|
||||
|
|
|
@ -40,4 +40,6 @@ typedef struct {
|
|||
digitalio_pull_t pull;
|
||||
} digitalio_digitalinout_obj_t;
|
||||
|
||||
void pin_config(const mcu_pin_obj_t *pin, bool open_drain, digitalio_pull_t pull);
|
||||
|
||||
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_DIGITALIO_DIGITALINOUT_H
|
||||
|
|
|
@ -29,6 +29,8 @@
|
|||
#include "shared-bindings/microcontroller/Pin.h"
|
||||
#include "shared-bindings/microcontroller/__init__.h"
|
||||
|
||||
#include "py/gc.h"
|
||||
|
||||
STATIC bool claimed_pins[IOMUXC_SW_PAD_CTL_PAD_COUNT];
|
||||
STATIC bool never_reset_pins[IOMUXC_SW_PAD_CTL_PAD_COUNT];
|
||||
|
||||
|
@ -85,6 +87,7 @@ void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
|
|||
return;
|
||||
}
|
||||
|
||||
disable_pin_change_interrupt(pin);
|
||||
never_reset_pins[pin->mux_idx] = false;
|
||||
claimed_pins[pin->mux_idx] = false;
|
||||
*(uint32_t *)pin->mux_reg = pin->mux_reset;
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Nick Moore for Adafruit Industries
|
||||
* Copyright (c) 2023 Jeff Epler for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "common-hal/digitalio/DigitalInOut.h"
|
||||
#include "common-hal/rotaryio/IncrementalEncoder.h"
|
||||
#include "shared-module/rotaryio/IncrementalEncoder.h"
|
||||
#include "shared-bindings/microcontroller/Pin.h"
|
||||
#include "shared-bindings/rotaryio/IncrementalEncoder.h"
|
||||
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "sdk/drivers/igpio/fsl_gpio.h"
|
||||
static void encoder_change(void *self_in) {
|
||||
rotaryio_incrementalencoder_obj_t *self = self_in;
|
||||
|
||||
bool value_a = GPIO_PinRead(self->pin_a->gpio, self->pin_a->number);
|
||||
bool value_b = GPIO_PinRead(self->pin_b->gpio, self->pin_b->number);
|
||||
uint8_t new_state = (value_a << 1) | value_b;
|
||||
shared_module_softencoder_state_update(self, new_state);
|
||||
}
|
||||
|
||||
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self,
|
||||
const mcu_pin_obj_t *pin_a, const mcu_pin_obj_t *pin_b) {
|
||||
|
||||
self->pin_a = pin_a;
|
||||
self->pin_b = pin_b;
|
||||
|
||||
// GPIO is always IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5 until proven otherwise
|
||||
#define IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5 5U
|
||||
IOMUXC_SetPinMux(pin_a->mux_reg, IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5, 0, 0, 0, 0);
|
||||
IOMUXC_SetPinMux(pin_b->mux_reg, IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5, 0, 0, 0, 0);
|
||||
|
||||
const gpio_pin_config_t config = { kGPIO_DigitalInput, 0, kGPIO_IntRisingOrFallingEdge };
|
||||
GPIO_PinInit(pin_a->gpio, pin_a->number, &config);
|
||||
GPIO_PinInit(pin_b->gpio, pin_b->number, &config);
|
||||
|
||||
enable_pin_change_interrupt(pin_a, encoder_change, self);
|
||||
enable_pin_change_interrupt(pin_b, encoder_change, self);
|
||||
|
||||
pin_config(pin_a, false, PULL_UP);
|
||||
pin_config(pin_b, false, PULL_UP);
|
||||
|
||||
claim_pin(pin_a);
|
||||
claim_pin(pin_b);
|
||||
}
|
||||
|
||||
bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t *self) {
|
||||
return !self->pin_a;
|
||||
}
|
||||
|
||||
void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t *self) {
|
||||
if (common_hal_rotaryio_incrementalencoder_deinited(self)) {
|
||||
return;
|
||||
}
|
||||
disable_pin_change_interrupt(self->pin_a);
|
||||
disable_pin_change_interrupt(self->pin_b);
|
||||
|
||||
common_hal_reset_pin(self->pin_a);
|
||||
common_hal_reset_pin(self->pin_b);
|
||||
|
||||
self->pin_a = NULL;
|
||||
self->pin_b = NULL;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
||||
* Copyright (c) 2023 Jeff Epler for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
const mcu_pin_obj_t *pin_a, *pin_b;
|
||||
uint8_t state; // <old A><old B>
|
||||
int8_t sub_count; // count intermediate transitions between detents
|
||||
int8_t divisor; // Number of quadrature edges required per count
|
||||
mp_int_t position;
|
||||
} rotaryio_incrementalencoder_obj_t;
|
||||
|
||||
|
||||
void incrementalencoder_interrupt_handler(uint8_t channel);
|
|
@ -24,7 +24,8 @@ CIRCUITPY_I2CTARGET = 0
|
|||
CIRCUITPY_NVM = 0
|
||||
CIRCUITPY_PARALLELDISPLAY = 0
|
||||
CIRCUITPY_PULSEIO = 0
|
||||
CIRCUITPY_ROTARYIO = 0
|
||||
CIRCUITPY_ROTARYIO = 1
|
||||
CIRCUITPY_ROTARYIO_SOFTENCODER = 1
|
||||
CIRCUITPY_USB_MIDI = 1
|
||||
LONGINT_IMPL = MPZ
|
||||
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
#include "peripherals/mimxrt10xx/pins.h"
|
||||
|
||||
typedef struct {
|
||||
gpio_change_interrupt_t *func;
|
||||
void *data;
|
||||
} pin_change_interrupt_data;
|
||||
|
||||
/* Array of GPIO peripheral base address. */
|
||||
static GPIO_Type *const s_gpioBases[] = GPIO_BASE_PTRS;
|
||||
static uint32_t GPIO_GetInstance(GPIO_Type *base) {
|
||||
uint32_t instance;
|
||||
|
||||
/* Find the instance index from base address mappings. */
|
||||
for (instance = 0U; instance < ARRAY_SIZE(s_gpioBases); instance++)
|
||||
{
|
||||
if (s_gpioBases[instance] == base) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert(instance < ARRAY_SIZE(s_gpioBases));
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
/* to find IRQ based on GPIO */
|
||||
static const IRQn_Type low_irqs[] = GPIO_COMBINED_LOW_IRQS;
|
||||
static const IRQn_Type high_irqs[] = GPIO_COMBINED_HIGH_IRQS;
|
||||
|
||||
static volatile pin_change_interrupt_data pcid[MP_ARRAY_SIZE(s_gpioBases)][32];
|
||||
|
||||
void enable_pin_change_interrupt(const mcu_pin_obj_t *pin, gpio_change_interrupt_t func, void *data) {
|
||||
int instance = GPIO_GetInstance(pin->gpio);
|
||||
volatile pin_change_interrupt_data *pci = &pcid[instance][pin->number];
|
||||
common_hal_mcu_disable_interrupts();
|
||||
pci->data = data;
|
||||
pci->func = func;
|
||||
IRQn_Type irq = pin->number < 16 ? low_irqs[instance] : high_irqs[instance];
|
||||
if (irq != NotAvail_IRQn) {
|
||||
EnableIRQ(irq);
|
||||
}
|
||||
pin->gpio->IMR |= (1 << pin->number);
|
||||
common_hal_mcu_enable_interrupts();
|
||||
}
|
||||
|
||||
void disable_pin_change_interrupt(const mcu_pin_obj_t *pin) {
|
||||
volatile pin_change_interrupt_data *pci = &pcid[GPIO_GetInstance(pin->gpio)][pin->number];
|
||||
common_hal_mcu_disable_interrupts();
|
||||
pin->gpio->IMR &= ~(1 << pin->number);
|
||||
pci->data = NULL;
|
||||
pci->func = NULL;
|
||||
pin->gpio->ISR = (1 << pin->number); // acknowledge any pending interrupt
|
||||
common_hal_mcu_enable_interrupts();
|
||||
}
|
||||
|
||||
static void pin_change_interrupt_common(uint32_t isr, volatile pin_change_interrupt_data *pcr) {
|
||||
for (uint32_t i = 0; i < 32; i++) {
|
||||
if (isr & (1 << i)) {
|
||||
pin_change_interrupt_data cb = pcr[i];
|
||||
if (cb.func) {
|
||||
cb.func(cb.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define GPIO_INTERRUPT_HANDLER(name, ptr, instance, offset) \
|
||||
void name(void); \
|
||||
__attribute__((used)) void name(void) { \
|
||||
uint32_t isr = ptr->ISR; \
|
||||
ptr->ISR = isr; \
|
||||
pin_change_interrupt_common(isr, pcid[instance]); \
|
||||
}
|
||||
|
||||
#if defined(GPIO1)
|
||||
GPIO_INTERRUPT_HANDLER(GPIO1_Combined_0_15_IRQHandler, GPIO1, 1, 0);
|
||||
GPIO_INTERRUPT_HANDLER(GPIO1_Combined_16_31_IRQHandler, GPIO1, 1, 16);
|
||||
#endif
|
||||
#if defined(GPIO2)
|
||||
GPIO_INTERRUPT_HANDLER(GPIO2_Combined_0_15_IRQHandler, GPIO2, 2, 0);
|
||||
GPIO_INTERRUPT_HANDLER(GPIO2_Combined_16_31_IRQHandler, GPIO2, 2, 16);
|
||||
#endif
|
||||
#if defined(GPIO3)
|
||||
GPIO_INTERRUPT_HANDLER(GPIO3_Combined_0_15_IRQHandler, GPIO3, 3, 0);
|
||||
GPIO_INTERRUPT_HANDLER(GPIO3_Combined_16_31_IRQHandler, GPIO3, 3, 16);
|
||||
#endif
|
||||
#if defined(GPIO4)
|
||||
GPIO_INTERRUPT_HANDLER(GPIO4_Combined_0_15_IRQHandler, GPIO4, 4, 0);
|
||||
GPIO_INTERRUPT_HANDLER(GPIO4_Combined_16_31_IRQHandler, GPIO4, 4, 16);
|
||||
#endif
|
||||
#if defined(GPIO5)
|
||||
GPIO_INTERRUPT_HANDLER(GPIO5_Combined_0_15_IRQHandler, GPIO5, 5, 0);
|
||||
GPIO_INTERRUPT_HANDLER(GPIO5_Combined_16_31_IRQHandler, GPIO5, 5, 16);
|
||||
#endif
|
||||
#if defined(GPIO6)
|
||||
GPIO_INTERRUPT_HANDLER(GPIO6_Combined_0_15_IRQHandler, GPIO6, 6, 0);
|
||||
GPIO_INTERRUPT_HANDLER(GPIO6_Combined_16_31_IRQHandler, GPIO6, 6, 16);
|
||||
#endif
|
|
@ -72,6 +72,10 @@ typedef struct {
|
|||
.pad_reset = p_pad_reset, \
|
||||
}
|
||||
|
||||
typedef void (gpio_change_interrupt_t)(void *data);
|
||||
void disable_pin_change_interrupt(const mcu_pin_obj_t *pin);
|
||||
void enable_pin_change_interrupt(const mcu_pin_obj_t *pin, gpio_change_interrupt_t func, void *data);
|
||||
|
||||
#ifdef MIMXRT1011_SERIES
|
||||
#include "MIMXRT1011/pins.h"
|
||||
#elif defined(MIMXRT1021_SERIES)
|
||||
|
|
|
@ -57,7 +57,7 @@ STATIC mp_obj_t countio_counter_make_new(const mp_obj_type_t *type, size_t n_arg
|
|||
const countio_edge_t edge = validate_edge(args[ARG_edge].u_obj, MP_QSTR_edge);
|
||||
const digitalio_pull_t pull = validate_pull(args[ARG_pull].u_obj, MP_QSTR_pull);
|
||||
// Make long-lived because some implementations use a pointer to the object as interrupt-handler data.
|
||||
countio_counter_obj_t *self = m_new_ll_obj(countio_counter_obj_t);
|
||||
countio_counter_obj_t *self = m_new_ll_obj_with_finaliser(countio_counter_obj_t);
|
||||
self->base.type = &countio_counter_type;
|
||||
|
||||
common_hal_countio_counter_construct(self, pin, edge, pull);
|
||||
|
@ -134,6 +134,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(countio_counter_reset_obj, countio_counter_reset);
|
|||
STATIC const mp_rom_map_elem_t countio_counter_locals_dict_table[] = {
|
||||
// Methods
|
||||
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&countio_counter_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&countio_counter_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&countio_counter___exit___obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&countio_counter_count_obj) },
|
||||
|
|
|
@ -89,7 +89,7 @@ STATIC mp_obj_t pulseio_pulsein_make_new(const mp_obj_type_t *type, size_t n_arg
|
|||
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj, MP_QSTR_pin);
|
||||
|
||||
// Make object long-lived to avoid moving between imports
|
||||
pulseio_pulsein_obj_t *self = m_new_ll_obj(pulseio_pulsein_obj_t);
|
||||
pulseio_pulsein_obj_t *self = m_new_ll_obj_with_finaliser(pulseio_pulsein_obj_t);
|
||||
self->base.type = &pulseio_pulsein_type;
|
||||
|
||||
common_hal_pulseio_pulsein_construct(self, pin, args[ARG_maxlen].u_int,
|
||||
|
@ -277,6 +277,7 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va
|
|||
STATIC const mp_rom_map_elem_t pulseio_pulsein_locals_dict_table[] = {
|
||||
// Methods
|
||||
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pulsein_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&pulseio_pulsein_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pulsein___exit___obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&pulseio_pulsein_pause_obj) },
|
||||
|
|
|
@ -76,7 +76,7 @@ STATIC mp_obj_t rotaryio_incrementalencoder_make_new(const mp_obj_type_t *type,
|
|||
const mcu_pin_obj_t *pin_b = validate_obj_is_free_pin(args[ARG_pin_b].u_obj, MP_QSTR_pin_b);
|
||||
|
||||
// Make long-lived because some implementations use a pointer to the object as interrupt-handler data.
|
||||
rotaryio_incrementalencoder_obj_t *self = m_new_ll_obj(rotaryio_incrementalencoder_obj_t);
|
||||
rotaryio_incrementalencoder_obj_t *self = m_new_ll_obj_with_finaliser(rotaryio_incrementalencoder_obj_t);
|
||||
self->base.type = &rotaryio_incrementalencoder_type;
|
||||
|
||||
common_hal_rotaryio_incrementalencoder_construct(self, pin_a, pin_b);
|
||||
|
@ -171,6 +171,7 @@ MP_PROPERTY_GETSET(rotaryio_incrementalencoder_position_obj,
|
|||
STATIC const mp_rom_map_elem_t rotaryio_incrementalencoder_locals_dict_table[] = {
|
||||
// Methods
|
||||
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rotaryio_incrementalencoder_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&rotaryio_incrementalencoder_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&rotaryio_incrementalencoder___exit___obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_position), MP_ROM_PTR(&rotaryio_incrementalencoder_position_obj) },
|
||||
|
|
|
@ -128,4 +128,8 @@ void port_post_boot_py(bool heap_valid);
|
|||
// A default weak implementation is provided that does nothing.
|
||||
void port_boot_info(void);
|
||||
|
||||
// Some ports want to mark additional pointers as gc roots.
|
||||
// A default weak implementation is provided that does nothing.
|
||||
void port_gc_collect(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SUPERVISOR_PORT_H
|
||||
|
|
Loading…
Reference in New Issue