From 893fc66d167df7bd74dd6fc19ea5427cb74a4b0d Mon Sep 17 00:00:00 2001 From: Michael Wisslead Date: Tue, 3 May 2022 04:00:58 +0000 Subject: [PATCH 1/2] Use common_hal_rp2pio_statemachine_construct for PulseIn --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 71633fcef7..557cb090c7 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -40,7 +40,7 @@ #define MAX_PULSE 65535 #define MIN_PULSE 10 -uint16_t pulsein_program[] = { +static const uint16_t pulsein_program[] = { 0x4001, // 1: in pins, 1 }; @@ -57,29 +57,23 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, self->start = 0; self->len = 0; - bool ok = rp2pio_statemachine_construct(&self->state_machine, - pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]), - 1000000, - NULL, 0, - NULL, 0, - pin, 1, - 0,0, - NULL, 0, - NULL, 0, - 1, 0, - NULL, // jump pin - 1 << self->pin, false, true, - false, 8, false, // TX, unused - false, - true, 32, true, // RX auto-push every 32 bits - false, // claim pins - false, // Not user-interruptible. - false, // No sideset enable - 0, -1); // wrap settings - - if (!ok) { - mp_raise_RuntimeError(translate("All state machines in use")); - } + common_hal_rp2pio_statemachine_construct(&self->state_machine, + pulsein_program, MP_ARRAY_SIZE(pulsein_program), + 1000000, // frequency + NULL, 0, // init, init_len + NULL, 0, 0, 0, // first out pin, # out pins, initial_out_pin_state + pin, 1, 0, 0, // first in pin, # in pins + NULL, 0, 0, 0, // first set pin + NULL, 0, 0, 0, // first sideset pin + false, // No sideset enable + NULL, PULL_NONE, // jump pin, jmp_pull + 0, // wait gpio pins + true, // exclusive pin usage + false, 8, false, // TX, setting we don't use + false, // wait for TX stall + true, 32, true, // RX auto pull every 32 bits. shift left to output msb first + false, // Not user-interruptible. + 0, -1); // wrap settings 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); From 2b0518233b3312845ee99b7b93a218f277154aed Mon Sep 17 00:00:00 2001 From: Michael Wisslead Date: Wed, 18 May 2022 17:34:25 +0000 Subject: [PATCH 2/2] rp2040 PulseIn improvements --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 55 ++++--------------- .../raspberrypi/common-hal/pulseio/PulseIn.h | 2 +- 2 files changed, 11 insertions(+), 46 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 557cb090c7..d30ef79b5f 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -38,7 +38,7 @@ #define NO_PIN 0xff #define MAX_PULSE 65535 -#define MIN_PULSE 10 +#define MIN_PULSE 0 static const uint16_t pulsein_program[] = { 0x4001, // 1: in pins, 1 @@ -75,24 +75,11 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, false, // Not user-interruptible. 0, -1); // wrap settings - 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); - self->last_level = self->idle_state; - self->level_count = 0; - self->buf_index = 0; + common_hal_pulseio_pulsein_pause(self); - 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,self,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS); - // exec a set pindirs to 0 for input - 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(self->state_machine.pio, self->state_machine.state_machine, true); + common_hal_pulseio_pulsein_resume(self, 0); } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { @@ -113,9 +100,10 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) { pio_sm_restart(self->state_machine.pio, self->state_machine.state_machine); 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); self->last_level = self->idle_state; self->level_count = 0; - self->buf_index = 0; + self->paused = true; } void common_hal_pulseio_pulsein_interrupt(void *self_in) { pulseio_pulsein_obj_t *self = self_in; @@ -134,7 +122,7 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) { } else { uint32_t result = self->level_count; self->last_level = level; - self->level_count = 0; + self->level_count = 1; // Pulses that are longer than MAX_PULSE will return MAX_PULSE if (result > MAX_PULSE) { result = MAX_PULSE; @@ -148,28 +136,15 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) { } else { self->start = (self->start + 1) % self->maxlen; } - if (self->buf_index < self->maxlen) { - self->buf_index++; - } else { - self->start = 0; - self->buf_index = 0; - } } } } } - -// check for a pulse thats too long (MAX_PULSE us) or maxlen reached, and reset - if ((self->level_count > MAX_PULSE) || (self->buf_index >= self->maxlen)) { - pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false); - pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config); - pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine); - pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); - self->buf_index = 0; - } } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t trigger_duration) { + + common_hal_pulseio_pulsein_pause(self); // Send the trigger pulse. if (trigger_duration > 0) { gpio_set_function(self->pin,GPIO_FUNC_SIO); @@ -177,11 +152,8 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, gpio_put(self->pin, !self->idle_state); common_hal_mcu_delay_us((uint32_t)trigger_duration); gpio_set_function(self->pin,GPIO_FUNC_PIO0); - common_hal_mcu_delay_us(125); } - // Reconfigure the pin for PIO - gpio_set_function(self->pin, GPIO_FUNC_PIO0); // exec a wait for the selected pin to change state if (self->idle_state == true) { pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020); @@ -189,12 +161,11 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0); } pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); + self->paused = false; } void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) { - self->start = 0; self->len = 0; - self->buf_index = 0; } uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) { @@ -204,12 +175,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) { uint16_t value = self->buffer[self->start]; self->start = (self->start + 1) % self->maxlen; self->len--; - // if we are empty reset buffer pointer and counters - if (self->len == 0) { - self->start = 0; - self->buf_index = 0; - self->level_count = 0; - } return value; } @@ -222,7 +187,7 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) { } bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) { - return true; + return self->paused; } uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.h b/ports/raspberrypi/common-hal/pulseio/PulseIn.h index f2c4fc0d06..8694f75e62 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.h @@ -37,13 +37,13 @@ typedef struct { mp_obj_base_t base; uint8_t pin; bool idle_state; + bool paused; uint16_t maxlen; uint16_t *buffer; volatile bool last_level; volatile uint32_t level_count; volatile uint16_t len; volatile uint16_t start; - volatile uint16_t buf_index; rp2pio_statemachine_obj_t state_machine; } pulseio_pulsein_obj_t;