Add IncrementalEncoder for mimxrt1011
.. and write a general 'pin change interrupt' facility to power it This uses the same quadrature state machine as atmel-samd, nrf, and rp2040. The 1011 doesn't have a dedicated encoder peripheral, so we go the pin-change + software route.
This commit is contained in:
parent
e1f16472c1
commit
47e1abdbc7
|
@ -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
|
||||
|
|
|
@ -84,6 +84,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;
|
||||
|
@ -116,3 +117,100 @@ void claim_pin(const mcu_pin_obj_t *pin) {
|
|||
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
|
||||
common_hal_reset_pin((mcu_pin_obj_t *)(mcu_pin_globals.map.table[pin_no].value));
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
typedef struct {
|
||||
gpio_change_interrupt_t *func;
|
||||
void *data;
|
||||
} pin_change_interrupt_data;
|
||||
|
||||
volatile static 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
|
||||
|
|
|
@ -45,4 +45,8 @@ extern const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[];
|
|||
// the port-default reset behavior.
|
||||
extern bool mimxrt10xx_board_reset_pin_number(const mcu_pin_obj_t *pin);
|
||||
|
||||
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);
|
||||
|
||||
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_MICROCONTROLLER_PIN_H
|
||||
|
|
|
@ -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 "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);
|
|
@ -20,7 +20,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
|
||||
|
||||
|
|
Loading…
Reference in New Issue