From 30f31639d3b4a8309d5b035c99608c04a294b3a3 Mon Sep 17 00:00:00 2001 From: gamblor21 Date: Sun, 25 Apr 2021 11:58:22 -0500 Subject: [PATCH] Added statemachine deinit --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 278c0cd662..95e38d6522 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -62,10 +62,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, self->len = 0; save_self = self; - // Set everything up. - rp2pio_statemachine_obj_t state_machine; - - bool ok = rp2pio_statemachine_construct(&state_machine, + bool ok = rp2pio_statemachine_construct(&self->state_machine, pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]), 1000000, NULL, 0, @@ -80,29 +77,26 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, false, true, 32, true, // RX auto-push every 32 bits false); // claim pins - pio_sm_set_enabled(state_machine.pio,state_machine.state_machine, false); - self->state_machine.pio = state_machine.pio; - self->state_machine.state_machine = state_machine.state_machine; - self->state_machine.sm_config = state_machine.sm_config; - self->state_machine.offset = state_machine.offset; + + pio_sm_set_enabled(self->state_machine.pio,self->state_machine.state_machine, false); pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine); last_level = self->idle_state; level_count = 0; result = 0; buf_index = 0; - pio_sm_set_in_pins(state_machine.pio,state_machine.state_machine,pin->number); - common_hal_rp2pio_statemachine_set_interrupt_handler(&state_machine,&common_hal_pulseio_pulsein_interrupt,NULL,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS); + pio_sm_set_in_pins(self->state_machine.pio,self->state_machine.state_machine,pin->number); + common_hal_rp2pio_statemachine_set_interrupt_handler(&(self->state_machine),&common_hal_pulseio_pulsein_interrupt,NULL,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS); // exec a set pindirs to 0 for input - pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080); + pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0xe080); // exec the appropriate wait for pin if (self->idle_state == true) { pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020); } else { pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0); } - pio_sm_set_enabled(state_machine.pio, state_machine.state_machine, true); + pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { @@ -114,8 +108,9 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { return; } pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false); - pio_sm_unclaim(self->state_machine.pio, self->state_machine.state_machine); + common_hal_rp2pio_statemachine_deinit(&self->state_machine); m_free(self->buffer); + reset_pin_number(self->pin); self->pin = NO_PIN; }