Content-Length: 1135762 | pFad | http://github.com/micropython/micropython/commit/7417c6ac91e47798b9c9eba2e7cd8f3dc1bbcc57

04 mimxrt/machine_pin: Implement pin.irq() functionality. · micropython/micropython@7417c6a · GitHub
Skip to content

Commit 7417c6a

Browse files
robert-hhdpgeorge
authored andcommitted
mimxrt/machine_pin: Implement pin.irq() functionality.
1 parent 689476c commit 7417c6a

File tree

13 files changed

+220
-2
lines changed

13 files changed

+220
-2
lines changed

Diff for: ports/mimxrt/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ SRC_C = \
151151
lib/mp-readline/readline.c \
152152
lib/libc/string0.c \
153153
lib/utils/gchelper_native.c \
154+
lib/utils/mpirq.c \
154155
lib/utils/printf.c \
155156
lib/utils/pyexec.c \
156157
lib/utils/stdout_helpers.c \
@@ -271,6 +272,7 @@ SRC_QSTR += \
271272
modmimxrt.c \
272273
moduos.c \
273274
pin.c \
275+
lib/utils/mpirq.c \
274276
lib/utils/sys_stdio_mphal.c \
275277
extmod/modonewire.c \
276278
$(GEN_PINS_SRC) \

Diff for: ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin))
1010
#define BOARD_FLASH_CONFIG_HEADER_H "evkmimxrt1010_flexspi_nor_config.h"
1111

12+
#define MICROPY_HW_NUM_PIN_IRQS (2 * 32)
13+
1214
// Define mapping logical UART # to hardware UART #
1315
// LPUART1 on D0/D1 -> 1
1416
// LPUART3 on A0/D4 -> 3

Diff for: ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin))
1111
#define BOARD_FLASH_CONFIG_HEADER_H "evkmimxrt1020_flexspi_nor_config.h"
1212

13+
#define MICROPY_HW_NUM_PIN_IRQS (3 * 32)
14+
1315
// Define mapping logical UART # to hardware UART #
1416
// D3/D5 LPUART1 Not usable, Since D3 is blocked.
1517
// D0/D1 LPUART2 -> 1

Diff for: ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin))
1010
#define BOARD_FLASH_CONFIG_HEADER_H "evkmimxrt1050_flexspi_nor_config.h"
1111

12+
#define MICROPY_HW_NUM_PIN_IRQS (4 * 32 + 3)
13+
1214
// Define mapping logical UART # to hardware UART #
1315
// LPUART3 on D0/D1 -> 1
1416
// LPUART2 on D7/D6 -> 2

Diff for: ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin))
1010
#define BOARD_FLASH_CONFIG_HEADER_H "evkmimxrt1060_flexspi_nor_config.h"
1111

12+
#define MICROPY_HW_NUM_PIN_IRQS (4 * 32 + 3)
13+
1214
// Define mapping logical UART # to hardware UART #
1315
// LPUART3 on D0/D1 -> 1
1416
// LPUART2 on D7/D6 -> 2

Diff for: ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin))
88
#define BOARD_FLASH_CONFIG_HEADER_H "evkmimxrt1064_flexspi_nor_config.h"
99

10+
#define MICROPY_HW_NUM_PIN_IRQS (4 * 32 + 3)
11+
1012
// Define mapping logical UART # to hardware UART #
1113
// LPUART3 on D0/D1 -> 1
1214
// LPUART2 on D7/D6 -> 2

Diff for: ports/mimxrt/boards/TEENSY40/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin))
1010
#define BOARD_FLASH_CONFIG_HEADER_H "teensy40_flexspi_nor_config.h"
1111

12+
#define MICROPY_HW_NUM_PIN_IRQS (4 * 32 + 3)
13+
1214
// UART config: 7 UARTs at the pins for Teensy 4.0
1315
#define MICROPY_HW_UART_NUM (sizeof(uart_index_table) / sizeof(uart_index_table)[0])
1416
#define MICROPY_HW_UART_INDEX { 0, 6, 4, 2, 3, 8, 1, 7 }

Diff for: ports/mimxrt/boards/TEENSY41/mpconfigboard.h

100755100644
+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_low(pin))
1010
#define BOARD_FLASH_CONFIG_HEADER_H "teensy41_flexspi_nor_config.h"
1111

12+
#define MICROPY_HW_NUM_PIN_IRQS (4 * 32 + 3)
13+
1214
// UART config: 8 UARTs at the pins for Teensy 4.1
1315
#define MICROPY_HW_UART_NUM (sizeof(uart_index_table) / sizeof(uart_index_table)[0])
1416
#define MICROPY_HW_UART_INDEX { 0, 6, 4, 2, 3, 8, 1, 7, 5 }

Diff for: ports/mimxrt/machine_pin.c

+192-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2020 Philipp Ebensberger
7+
* Copyright (c) 2021 Robert Hammelrath
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -30,8 +31,8 @@
3031

3132
#include "py/runtime.h"
3233
#include "py/mphal.h"
34+
#include "lib/utils/mpirq.h"
3335
#include "pin.h"
34-
#include "mphalport.h"
3536

3637
// Local functions
3738
STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
@@ -68,6 +69,95 @@ const mp_obj_type_t machine_pin_board_pins_obj_type = {
6869
.locals_dict = (mp_obj_t)&machine_pin_board_pins_locals_dict,
6970
};
7071

72+
STATIC const mp_irq_methods_t machine_pin_irq_methods;
73+
74+
static GPIO_Type *gpiobases[] = GPIO_BASE_PTRS;
75+
STATIC const uint16_t GPIO_combined_low_irqs[] = GPIO_COMBINED_LOW_IRQS;
76+
STATIC const uint16_t GPIO_combined_high_irqs[] = GPIO_COMBINED_HIGH_IRQS;
77+
STATIC const uint16_t IRQ_mapping[] = {kGPIO_NoIntmode, kGPIO_IntRisingEdge, kGPIO_IntFallingEdge, kGPIO_IntRisingOrFallingEdge};
78+
#define GET_PIN_IRQ_INDEX(gpio_nr, pin) ((gpio_nr - 1) * 32 + pin)
79+
80+
int GPIO_get_instance(GPIO_Type *gpio) {
81+
int gpio_nr;
82+
for (gpio_nr = 0; gpio_nr < ARRAY_SIZE(gpiobases); gpio_nr++) {
83+
if (gpio == gpiobases[gpio_nr]) {
84+
return gpio_nr;
85+
}
86+
}
87+
return 0;
88+
}
89+
90+
void call_handler(GPIO_Type *gpio, int gpio_nr, int pin) {
91+
uint32_t mask = 1 << pin;
92+
uint32_t isr = gpio->ISR & gpio->IMR;
93+
for (int i = 0; i < 16; i++, pin++, mask <<= 1) {
94+
// Did the ISR fire? Consider only the bits that are enabled.
95+
if (isr & mask) {
96+
gpio->ISR = mask; // clear the ISR flag
97+
int index = GET_PIN_IRQ_INDEX(gpio_nr, pin);
98+
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[index]);
99+
if (irq != NULL) {
100+
irq->flags = irq->trigger;
101+
mp_irq_handler(&irq->base);
102+
}
103+
}
104+
}
105+
}
106+
107+
// 10 GPIO IRQ handlers, each covering 16 bits.
108+
109+
void GPIO1_Combined_0_15_IRQHandler(void) {
110+
call_handler(gpiobases[1], 1, 0);
111+
}
112+
113+
void GPIO1_Combined_16_31_IRQHandler(void) {
114+
call_handler(gpiobases[1], 1, 16);
115+
}
116+
117+
void GPIO2_Combined_0_15_IRQHandler(void) {
118+
call_handler(gpiobases[2], 2, 0);
119+
}
120+
121+
void GPIO2_Combined_16_31_IRQHandler(void) {
122+
call_handler(gpiobases[2], 2, 16);
123+
}
124+
125+
void GPIO3_Combined_0_15_IRQHandler(void) {
126+
call_handler(gpiobases[3], 3, 0);
127+
}
128+
129+
void GPIO3_Combined_16_31_IRQHandler(void) {
130+
call_handler(gpiobases[3], 3, 16);
131+
}
132+
133+
void GPIO4_Combined_0_15_IRQHandler(void) {
134+
call_handler(gpiobases[4], 4, 0);
135+
}
136+
137+
void GPIO4_Combined_16_31_IRQHandler(void) {
138+
call_handler(gpiobases[4], 4, 16);
139+
}
140+
141+
void GPIO5_Combined_0_15_IRQHandler(void) {
142+
call_handler(gpiobases[5], 5, 0);
143+
}
144+
145+
void GPIO5_Combined_16_31_IRQHandler(void) {
146+
call_handler(gpiobases[5], 5, 16);
147+
}
148+
149+
// Deinit all pin IRQ handlers.
150+
void machine_pin_irq_deinit(void) {
151+
for (int i = 0; i < ARRAY_SIZE(MP_STATE_PORT(machine_pin_irq_objects)); ++i) {
152+
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[i]);
153+
if (irq != NULL) {
154+
machine_pin_obj_t *self = MP_OBJ_TO_PTR(irq->base.parent);
155+
GPIO_PortDisableInterrupts(self->gpio, 1U << self->pin);
156+
MP_STATE_PORT(machine_pin_irq_objects[i]) = NULL;
157+
}
158+
}
159+
}
160+
71161
// Simplified mode setting used by the extmod modules
72162
void machine_pin_set_mode(const machine_pin_obj_t *self, uint8_t mode) {
73163
gpio_pin_config_t pin_config = {kGPIO_DigitalInput, 1, kGPIO_NoIntmode};
@@ -234,6 +324,69 @@ STATIC mp_obj_t machine_pin_init(size_t n_args, const mp_obj_t *args, mp_map_t *
234324
}
235325
MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_init);
236326

327+
// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING, hard=False)
328+
STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
329+
enum { ARG_handler, ARG_trigger, ARG_hard };
330+
static const mp_arg_t allowed_args[] = {
331+
{ MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
332+
{ MP_QSTR_trigger, MP_ARG_INT, {.u_int = 3} },
333+
{ MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} },
334+
};
335+
machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
336+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
337+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
338+
339+
// Get the IRQ object.
340+
uint32_t gpio_nr = GPIO_get_instance(self->gpio);
341+
uint32_t index = GET_PIN_IRQ_INDEX(gpio_nr, self->pin);
342+
if (index >= ARRAY_SIZE(MP_STATE_PORT(machine_pin_irq_objects))) {
343+
mp_raise_ValueError(MP_ERROR_TEXT("IRQ not supported on given Pin"));
344+
}
345+
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[index]);
346+
347+
// Allocate the IRQ object if it doesn't already exist.
348+
if (irq == NULL) {
349+
irq = m_new_obj(machine_pin_irq_obj_t);
350+
irq->base.base.type = &mp_irq_type;
351+
irq->base.methods = (mp_irq_methods_t *)&machine_pin_irq_methods;
352+
irq->base.parent = MP_OBJ_FROM_PTR(self);
353+
irq->base.handler = mp_const_none;
354+
irq->base.ishard = false;
355+
MP_STATE_PORT(machine_pin_irq_objects[index]) = irq;
356+
}
357+
358+
if (n_args > 1 || kw_args->used != 0) {
359+
// Configure IRQ.
360+
uint32_t irq_num = self->pin < 16 ? GPIO_combined_low_irqs[gpio_nr] : GPIO_combined_high_irqs[gpio_nr];
361+
362+
// Disable all IRQs from the affected source while data is updated.
363+
DisableIRQ(irq_num);
364+
GPIO_PortDisableInterrupts(self->gpio, 1U << self->pin);
365+
366+
// Update IRQ data.
367+
irq->base.handler = args[ARG_handler].u_obj;
368+
irq->base.ishard = args[ARG_hard].u_bool;
369+
irq->flags = 0;
370+
if (args[ARG_trigger].u_int >= ARRAY_SIZE(IRQ_mapping)) {
371+
mp_raise_ValueError(MP_ERROR_TEXT("IRQ mode not supported"));
372+
}
373+
irq->trigger = IRQ_mapping[args[ARG_trigger].u_int];
374+
375+
// Enable IRQ if a handler is given.
376+
if (args[ARG_handler].u_obj != mp_const_none) {
377+
// Set the pin mode
378+
GPIO_PinSetInterruptConfig(self->gpio, self->pin, irq->trigger);
379+
// Enable the specific Pin interrupt
380+
GPIO_PortEnableInterrupts(self->gpio, 1U << self->pin);
381+
}
382+
// Enable LEVEL1 interrupt again
383+
EnableIRQ(irq_num);
384+
}
385+
386+
return MP_OBJ_FROM_PTR(irq);
387+
}
388+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_irq_obj, 1, machine_pin_irq);
389+
237390
STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
238391
// instance methods
239392
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_off_obj) },
@@ -242,6 +395,7 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
242395
{ MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&machine_pin_on_obj) },
243396
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
244397
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) },
398+
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) },
245399
// class attributes
246400
{ MP_ROM_QSTR(MP_QSTR_board), MP_ROM_PTR(&machine_pin_board_pins_obj_type) },
247401
{ MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&machine_pin_cpu_pins_obj_type) },
@@ -265,6 +419,9 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
265419
{ MP_ROM_QSTR(MP_QSTR_POWER_5), MP_ROM_INT(PIN_DRIVE_POWER_5) }, // R0/6
266420
{ MP_ROM_QSTR(MP_QSTR_POWER_6), MP_ROM_INT(PIN_DRIVE_POWER_6) }, // R0/7
267421

422+
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(1) },
423+
{ MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(2) },
424+
268425
};
269426
STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
270427

@@ -285,3 +442,37 @@ const mp_obj_type_t machine_pin_af_type = {
285442
.make_new = mp_pin_make_new,
286443
.locals_dict = (mp_obj_dict_t *)&machine_pin_locals_dict,
287444
};
445+
446+
STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) {
447+
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
448+
uint32_t gpio_nr = GPIO_get_instance(self->gpio);
449+
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[GET_PIN_IRQ_INDEX(gpio_nr, self->pin)]);
450+
uint32_t irq_num = self->pin < 16 ? GPIO_combined_low_irqs[gpio_nr] : GPIO_combined_high_irqs[gpio_nr];
451+
DisableIRQ(irq_num);
452+
irq->flags = 0;
453+
irq->trigger = new_trigger;
454+
// Configure the interrupt.
455+
GPIO_PinSetInterruptConfig(self->gpio, self->pin, irq->trigger);
456+
// Enable LEVEL1 interrupt.
457+
EnableIRQ(irq_num);
458+
// Enable the specific pin interrupt.
459+
GPIO_PortEnableInterrupts(self->gpio, 1U << self->pin);
460+
return 0;
461+
}
462+
463+
STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) {
464+
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
465+
uint32_t gpio_nr = GPIO_get_instance(self->gpio);
466+
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[GET_PIN_IRQ_INDEX(gpio_nr, self->pin)]);
467+
if (info_type == MP_IRQ_INFO_FLAGS) {
468+
return irq->flags;
469+
} else if (info_type == MP_IRQ_INFO_TRIGGERS) {
470+
return irq->trigger;
471+
}
472+
return 0;
473+
}
474+
475+
STATIC const mp_irq_methods_t machine_pin_irq_methods = {
476+
.trigger = machine_pin_irq_trigger,
477+
.info = machine_pin_irq_info,
478+
};

Diff for: ports/mimxrt/main.c

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "ticks.h"
3737
#include "tusb.h"
3838
#include "led.h"
39+
#include "modmachine.h"
3940

4041
extern uint8_t _sstack, _estack, _gc_heap_start, _gc_heap_end;
4142

@@ -90,6 +91,7 @@ int main(void) {
9091

9192
soft_reset_exit:
9293
mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n");
94+
machine_pin_irq_deinit();
9395
gc_sweep_all();
9496
mp_deinit();
9597
}

Diff for: ports/mimxrt/modmachine.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern const mp_obj_type_t machine_rtc_type;
3535
extern const mp_obj_type_t machine_uart_type;
3636

3737
void machine_adc_init(void);
38+
void machine_pin_irq_deinit(void);
3839
void machine_timer_init_PIT(void);
3940

4041
#endif // MICROPY_INCLUDED_MIMXRT_MODMACHINE_H

Diff for: ports/mimxrt/mpconfigport.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ extern const struct _mp_obj_module_t mp_module_utime;
167167

168168
#define MICROPY_PORT_ROOT_POINTERS \
169169
const char *readline_hist[8]; \
170-
struct _machine_timer_obj_t *timer_table[MICROPY_HW_PIT_NUM_CHANNELS];
170+
struct _machine_timer_obj_t *timer_table[MICROPY_HW_PIT_NUM_CHANNELS]; \
171+
void *machine_pin_irq_objects[MICROPY_HW_NUM_PIN_IRQS]; \
171172

172173
#define MP_STATE_PORT MP_STATE_VM
173174

Diff for: ports/mimxrt/pin.h

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <stdint.h>
3131
#include "py/obj.h"
32+
#include "lib/utils/mpirq.h"
3233
#include "fsl_gpio.h"
3334

3435
// ------------------------------------------------------------------------------------------------------------------ //
@@ -118,6 +119,12 @@ typedef struct {
118119
const machine_pin_adc_obj_t *adc_list; // pointer to list with ADC options
119120
} machine_pin_obj_t;
120121

122+
typedef struct _machine_pin_irq_obj_t {
123+
mp_irq_obj_t base;
124+
uint32_t flags;
125+
uint32_t trigger;
126+
} machine_pin_irq_obj_t;
127+
121128
// ------------------------------------------------------------------------------------------------------------------ //
122129

123130
extern const mp_obj_type_t machine_pin_type;

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/commit/7417c6ac91e47798b9c9eba2e7cd8f3dc1bbcc57

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy