From 9d5fbea7a3601381d10501d24548c5f48d278585 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 11 Jul 2021 21:02:28 -0500 Subject: [PATCH 1/3] Fix for Issue #4983 - stop state machine before restarting --- ports/raspberrypi/common-hal/rp2pio/StateMachine.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c index ab70a27fcd..340c7d7f08 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -500,6 +500,7 @@ void common_hal_rp2pio_statemachine_restart(rp2pio_statemachine_obj_t *self) { pio_sm_restart(self->pio, self->state_machine); uint8_t pio_index = pio_get_index(self->pio); + common_hal_rp2pio_statemachine_stop(self); uint32_t pins_we_use = _current_sm_pins[pio_index][self->state_machine]; pio_sm_set_pins_with_mask(self->pio, self->state_machine, self->initial_pin_state, pins_we_use); pio_sm_set_pindirs_with_mask(self->pio, self->state_machine, self->initial_pin_direction, pins_we_use); From 490f263a41c766b33dd68f4d93428cf85ae8bde3 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 12 Jul 2021 14:09:11 -0500 Subject: [PATCH 2/3] Added code to reset SM program counter during a restart --- ports/raspberrypi/common-hal/rp2pio/StateMachine.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c index 340c7d7f08..9ea66c0c26 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -498,7 +498,9 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, void common_hal_rp2pio_statemachine_restart(rp2pio_statemachine_obj_t *self) { pio_sm_restart(self->pio, self->state_machine); - + // Reset program counter to the original offset. A JMP is 0x0000 plus + // the desired offset, so we can just use self->offset. + pio_sm_exec(self->pio, self->state_machine,self->offset); uint8_t pio_index = pio_get_index(self->pio); common_hal_rp2pio_statemachine_stop(self); uint32_t pins_we_use = _current_sm_pins[pio_index][self->state_machine]; From 5c9823d8cb2b754f1cf7ca7845085aaa0e175b5b Mon Sep 17 00:00:00 2001 From: root Date: Tue, 13 Jul 2021 11:17:03 -0500 Subject: [PATCH 3/3] Change order of operations in restart --- ports/raspberrypi/common-hal/rp2pio/StateMachine.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c index 9ea66c0c26..9b254d9c0c 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -497,12 +497,12 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, } void common_hal_rp2pio_statemachine_restart(rp2pio_statemachine_obj_t *self) { - pio_sm_restart(self->pio, self->state_machine); + common_hal_rp2pio_statemachine_stop(self); // Reset program counter to the original offset. A JMP is 0x0000 plus // the desired offset, so we can just use self->offset. pio_sm_exec(self->pio, self->state_machine,self->offset); + pio_sm_restart(self->pio, self->state_machine); uint8_t pio_index = pio_get_index(self->pio); - common_hal_rp2pio_statemachine_stop(self); uint32_t pins_we_use = _current_sm_pins[pio_index][self->state_machine]; pio_sm_set_pins_with_mask(self->pio, self->state_machine, self->initial_pin_state, pins_we_use); pio_sm_set_pindirs_with_mask(self->pio, self->state_machine, self->initial_pin_direction, pins_we_use);