From f8eb5bc2759857870b8b251513e975e6d1db4d02 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 25 Feb 2021 12:38:43 -0600 Subject: [PATCH 1/5] Cleanup PIOs and State Machines on soft reset --- ports/raspberrypi/common-hal/board/__init__.c | 12 ++++++++++++ shared-module/board/__init__.c | 4 ++++ shared-module/board/__init__.h | 1 + 3 files changed, 17 insertions(+) diff --git a/ports/raspberrypi/common-hal/board/__init__.c b/ports/raspberrypi/common-hal/board/__init__.c index 3c7f30df22..5fc02d9997 100644 --- a/ports/raspberrypi/common-hal/board/__init__.c +++ b/ports/raspberrypi/common-hal/board/__init__.c @@ -29,6 +29,18 @@ #include "py/runtime.h" #include "py/mphal.h" #include "common-hal/microcontroller/Pin.h" +#include "src/rp2_common/hardware_pio/include/hardware/pio.h" +#include "shared-module/board/__init__.h" // Pins aren't actually defined here. They are in the board specific directory // such as boards/arduino_zero/pins.c. + +// routine to reset both pios +void board_reset_pio() { + for ( uint8_t sm = 0; sm < 4; sm++) { + pio_sm_init (pio0, sm, 0, NULL); + pio_sm_init (pio1, sm, 0, NULL); + } + pio_clear_instruction_memory(pio0); + pio_clear_instruction_memory(pio1); +} diff --git a/shared-module/board/__init__.c b/shared-module/board/__init__.c index 2f1c34e565..fce618de49 100644 --- a/shared-module/board/__init__.c +++ b/shared-module/board/__init__.c @@ -28,6 +28,7 @@ #include "supervisor/shared/translate.h" #include "mpconfigboard.h" #include "py/runtime.h" +#include "shared-module/board/__init__.h" #if CIRCUITPY_BUSIO #include "shared-bindings/busio/I2C.h" @@ -179,4 +180,7 @@ void reset_board_busses(void) { #if BOARD_UART MP_STATE_VM(shared_uart_bus) = NULL; #endif +#if CIRCUITPY_RP2PIO + board_reset_pio(); +#endif } diff --git a/shared-module/board/__init__.h b/shared-module/board/__init__.h index f7eecd4170..2b970710ae 100644 --- a/shared-module/board/__init__.h +++ b/shared-module/board/__init__.h @@ -28,5 +28,6 @@ #define MICROPY_INCLUDED_SHARED_MODULE_BOARD__INIT__H void reset_board_busses(void); +void board_reset_pio(void); #endif // MICROPY_INCLUDED_SHARED_MODULE_BOARD__INIT__H From 6374408c063ee029ed3b256ae432e0242fbcda9c Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Feb 2021 22:28:50 -0600 Subject: [PATCH 2/5] Changed reset_rp2pio_statemachine to clean up any PIO interrupts. --- ports/raspberrypi/common-hal/board/__init__.c | 10 ---------- ports/raspberrypi/common-hal/rp2pio/StateMachine.c | 8 ++++++++ shared-module/board/__init__.c | 6 +++++- shared-module/board/__init__.h | 1 - 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/ports/raspberrypi/common-hal/board/__init__.c b/ports/raspberrypi/common-hal/board/__init__.c index 5fc02d9997..130b4fd692 100644 --- a/ports/raspberrypi/common-hal/board/__init__.c +++ b/ports/raspberrypi/common-hal/board/__init__.c @@ -34,13 +34,3 @@ // Pins aren't actually defined here. They are in the board specific directory // such as boards/arduino_zero/pins.c. - -// routine to reset both pios -void board_reset_pio() { - for ( uint8_t sm = 0; sm < 4; sm++) { - pio_sm_init (pio0, sm, 0, NULL); - pio_sm_init (pio1, sm, 0, NULL); - } - pio_clear_instruction_memory(pio0); - pio_clear_instruction_memory(pio1); -} diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c index 90c48130e1..485f80b65a 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -34,6 +34,7 @@ #include "src/rp2_common/hardware_dma/include/hardware/dma.h" #include "src/rp2_common/hardware_pio/include/hardware/pio_instructions.h" #include "src/rp2040/hardware_structs/include/hardware/structs/iobank0.h" +#include "src/rp2_common/hardware_irq/include/hardware/irq.h" #include "lib/utils/interrupt_char.h" #include "py/obj.h" @@ -101,6 +102,13 @@ void reset_rp2pio_statemachine(void) { _reset_statemachine(pio, j, false); } } + for (uint8_t irq=PIO0_IRQ_0; irq <= PIO1_IRQ_1; irq++) { + irq_handler_t int_handler = irq_get_exclusive_handler(irq); + if (int_handler > 0) { + irq_set_enabled (irq, false); + irq_remove_handler(irq,int_handler); + } + } } STATIC uint32_t _check_pins_free(const mcu_pin_obj_t * first_pin, uint8_t pin_count, bool exclusive_pin_use) { diff --git a/shared-module/board/__init__.c b/shared-module/board/__init__.c index fce618de49..2cbf0b5136 100644 --- a/shared-module/board/__init__.c +++ b/shared-module/board/__init__.c @@ -45,6 +45,10 @@ #include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h" #endif +#if CIRCUITPY_RP2PIO +#include "bindings/rp2pio/StateMachine.h" +#endif + #if BOARD_I2C // Statically allocate the I2C object so it can live past the end of the heap and into the next VM. // That way it can be used by built-in I2CDisplay displays and be accessible through board.I2C(). @@ -181,6 +185,6 @@ void reset_board_busses(void) { MP_STATE_VM(shared_uart_bus) = NULL; #endif #if CIRCUITPY_RP2PIO - board_reset_pio(); + reset_rp2pio_statemachine(); #endif } diff --git a/shared-module/board/__init__.h b/shared-module/board/__init__.h index 2b970710ae..f7eecd4170 100644 --- a/shared-module/board/__init__.h +++ b/shared-module/board/__init__.h @@ -28,6 +28,5 @@ #define MICROPY_INCLUDED_SHARED_MODULE_BOARD__INIT__H void reset_board_busses(void); -void board_reset_pio(void); #endif // MICROPY_INCLUDED_SHARED_MODULE_BOARD__INIT__H From f3515502b736fbbd812536e582ed2c1d0e6c0046 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Feb 2021 22:33:50 -0600 Subject: [PATCH 3/5] Removed unecessary includes --- ports/raspberrypi/common-hal/board/__init__.c | 1 - shared-module/board/__init__.c | 1 - 2 files changed, 2 deletions(-) diff --git a/ports/raspberrypi/common-hal/board/__init__.c b/ports/raspberrypi/common-hal/board/__init__.c index 130b4fd692..532bb1c666 100644 --- a/ports/raspberrypi/common-hal/board/__init__.c +++ b/ports/raspberrypi/common-hal/board/__init__.c @@ -30,7 +30,6 @@ #include "py/mphal.h" #include "common-hal/microcontroller/Pin.h" #include "src/rp2_common/hardware_pio/include/hardware/pio.h" -#include "shared-module/board/__init__.h" // Pins aren't actually defined here. They are in the board specific directory // such as boards/arduino_zero/pins.c. diff --git a/shared-module/board/__init__.c b/shared-module/board/__init__.c index 2cbf0b5136..eebd6e9b57 100644 --- a/shared-module/board/__init__.c +++ b/shared-module/board/__init__.c @@ -28,7 +28,6 @@ #include "supervisor/shared/translate.h" #include "mpconfigboard.h" #include "py/runtime.h" -#include "shared-module/board/__init__.h" #if CIRCUITPY_BUSIO #include "shared-bindings/busio/I2C.h" From 29c89a2487d616e5a9f810915a603b21b7dc12cd Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Feb 2021 22:35:38 -0600 Subject: [PATCH 4/5] Removed more includes --- ports/raspberrypi/common-hal/board/__init__.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/raspberrypi/common-hal/board/__init__.c b/ports/raspberrypi/common-hal/board/__init__.c index 532bb1c666..3c7f30df22 100644 --- a/ports/raspberrypi/common-hal/board/__init__.c +++ b/ports/raspberrypi/common-hal/board/__init__.c @@ -29,7 +29,6 @@ #include "py/runtime.h" #include "py/mphal.h" #include "common-hal/microcontroller/Pin.h" -#include "src/rp2_common/hardware_pio/include/hardware/pio.h" // Pins aren't actually defined here. They are in the board specific directory // such as boards/arduino_zero/pins.c. From 8bf5dd93c1f54b0afb70d3ff529e45f373a2298a Mon Sep 17 00:00:00 2001 From: root Date: Tue, 2 Mar 2021 11:30:16 -0600 Subject: [PATCH 5/5] Removed uneeded call in reset_board_busses() --- shared-module/board/__init__.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/shared-module/board/__init__.c b/shared-module/board/__init__.c index eebd6e9b57..2f1c34e565 100644 --- a/shared-module/board/__init__.c +++ b/shared-module/board/__init__.c @@ -44,10 +44,6 @@ #include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h" #endif -#if CIRCUITPY_RP2PIO -#include "bindings/rp2pio/StateMachine.h" -#endif - #if BOARD_I2C // Statically allocate the I2C object so it can live past the end of the heap and into the next VM. // That way it can be used by built-in I2CDisplay displays and be accessible through board.I2C(). @@ -183,7 +179,4 @@ void reset_board_busses(void) { #if BOARD_UART MP_STATE_VM(shared_uart_bus) = NULL; #endif -#if CIRCUITPY_RP2PIO - reset_rp2pio_statemachine(); -#endif }