From 8e15f36baada85345f8356ffcc8f8d48612a0c5e Mon Sep 17 00:00:00 2001 From: root Date: Wed, 24 Feb 2021 16:58:29 -0600 Subject: [PATCH 01/21] pulseio (pulsein) for RP2040 --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 231 ++++++++++++++++++ .../raspberrypi/common-hal/pulseio/PulseIn.h | 53 ++++ .../raspberrypi/common-hal/pulseio/PulseOut.c | 97 ++++++++ .../raspberrypi/common-hal/pulseio/PulseOut.h | 44 ++++ .../raspberrypi/common-hal/pulseio/__init__.c | 1 + .../common-hal/rp2pio/StateMachine.c | 2 + .../common-hal/rp2pio/StateMachine.h | 2 + ports/raspberrypi/mpconfigport.mk | 2 +- 8 files changed, 431 insertions(+), 1 deletion(-) create mode 100644 ports/raspberrypi/common-hal/pulseio/PulseIn.c create mode 100644 ports/raspberrypi/common-hal/pulseio/PulseIn.h create mode 100644 ports/raspberrypi/common-hal/pulseio/PulseOut.c create mode 100644 ports/raspberrypi/common-hal/pulseio/PulseOut.h create mode 100644 ports/raspberrypi/common-hal/pulseio/__init__.c diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c new file mode 100644 index 0000000000..c642561c89 --- /dev/null +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -0,0 +1,231 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" +#include "src/rp2_common/hardware_irq/include/hardware/irq.h" + +#include + +#include "background.h" +#include "mpconfigport.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/pulseio/PulseIn.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "supervisor/shared/translate.h" +#include "bindings/rp2pio/StateMachine.h" +#include "common-hal/pulseio/PulseIn.h" + +pulseio_pulsein_obj_t* save_self; + +#define NO_PIN 0xff + +const uint16_t pulsein_program[] = { +// set pindirs, 0 ; For input + 0xe080, +// wait 0 pin, 0 ; Wait for first low to start + 0x2020, +// irq wait 0 ; set IRQ 0 and wait + 0xc020, +// .bitloop +// in pins, 1 [1] ; sample every 3 cycles (2 instructions, 1 delay) + 0x4101, +// jmp bitloop + 0x0003, +}; + +void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, + const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { + + self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); + if (self->buffer == NULL) { + mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); + } + self->pin = pin->number; + self->maxlen = maxlen; + self->idle_state = idle_state; + self->start = 0; + self->len = 0; + save_self = self; + + // Set everything up. + rp2pio_statemachine_obj_t state_machine; + + bool ok = rp2pio_statemachine_construct(&state_machine, + pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]), + 125000 * 4, + NULL, 0, + NULL, 0, + pin, 1, + NULL, 0, + NULL, 0, + 1 << self->pin, false, true, + false, 8, false, // TX, unused + true, 32, true, // RX iauto-push every 32 bits + false); // claim pins + self->pio = state_machine.pio; + self->sm = state_machine.state_machine; + self->sm_cfg = state_machine.sm_config; + self->offset = state_machine.offset; + if ( self->pio == pio0 ) { + self->pio_interrupt = PIO0_IRQ_0; + } else { + self->pio_interrupt = PIO1_IRQ_0; + } + + pio_sm_set_in_pins(self->pio,self->sm,pin->number); + pio_sm_set_enabled(self->pio, self->sm, false); + irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt); + irq_set_enabled(self->pio_interrupt, true); + hw_clear_bits(&self->pio->inte0, 1u << self->sm); + hw_set_bits(&self->pio->inte0, 1u << (self->sm+8)); + pio_sm_set_enabled(self->pio, self->sm, true); + +} + +bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { + return self->pin == NO_PIN; +} + +void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { + if (common_hal_pulseio_pulsein_deinited(self)) { + return; + } + irq_set_enabled(self->pio_interrupt, false); + pio_sm_set_enabled(self->pio, self->sm, false); + pio_sm_unclaim (self->pio, self->sm); + m_free(self->buffer); + self->pin = NO_PIN; +} + +void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { + pio_sm_set_enabled(self->pio, self->sm, false); +} + +void common_hal_pulseio_pulsein_interrupt() { + + pulseio_pulsein_obj_t* self = save_self; +// clear interrupt + hw_clear_bits(&self->pio->inte0, 1u << self->sm); + self->pio->irq = 1u << self->sm; + irq_clear(self->pio_interrupt); + pio_sm_clear_fifos(self->pio,self->sm); + bool last_level = true; + uint level_count = 0; + uint16_t result = 0; + uint16_t buf_index = 0; + while ( buf_index < self->maxlen ) { + uint32_t rxfifo = 0; + rxfifo = pio_sm_get_blocking(self->pio, self->sm); + // translate from fifo to buffer + for (uint i = 0; i < 32; i++) { + bool level = (rxfifo & (1 << i)) >> i; + if (level == last_level ) { + level_count ++; + } else { + result = level_count * 6; + last_level = level; + level_count = 1; + // ignore pulses that are too long and too short + if (result < 10000 && result > 10) { + self->buffer[buf_index] = result; + buf_index++; + self->len++; + } + } + } + // check for a pulse thats too long (20ms) + if ( level_count > 3000 ) { + break; + } + } + pio_sm_set_enabled(self->pio, self->sm, false); + pio_sm_init(self->pio, self->sm, self->offset, &self->sm_cfg); + pio_sm_restart(self->pio,self->sm); + pio_sm_set_enabled(self->pio, self->sm, true); + irq_set_enabled(self->pio_interrupt, true); +} +void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, + uint16_t trigger_duration) { + // Send the trigger pulse. + if (trigger_duration > 0) { + gpio_set_function(self->pin ,GPIO_FUNC_SIO); + gpio_set_dir(self->pin,true); + gpio_put(self->pin, false); + common_hal_mcu_delay_us((uint32_t)trigger_duration); + gpio_set_function(self->pin ,GPIO_FUNC_PIO0); + } + + // Reconfigure the pin for PIO + common_hal_mcu_delay_us(100); + gpio_set_function(self->pin, GPIO_FUNC_PIO0); + pio_sm_set_enabled(self->pio, self->sm, true); + pio_sm_exec(self->pio,self->sm,0x20a0); +} + +void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { + self->start = 0; + self->len = 0; +} + +uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { + if (self->len == 0) { + mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_PulseIn); + } + uint16_t value = self->buffer[self->start]; + self->start = (self->start + 1) % self->maxlen; + self->len--; + if (self->len == 0 ) { + // reset buffer pointer + self->start = 0; + } + return value; +} + +uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) { + return self->maxlen; +} + +uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) { + return self->len; +} + +bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) { + return true; +} + +uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, + int16_t index) { + if (index < 0) { + index += self->len; + } + if (index < 0 || index >= self->len) { + mp_raise_IndexError_varg(translate("%q index out of range"), MP_QSTR_PulseIn); + } + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; + return value; +} diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.h b/ports/raspberrypi/common-hal/pulseio/PulseIn.h new file mode 100644 index 0000000000..35f7309eef --- /dev/null +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.h @@ -0,0 +1,53 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H + +#include "common-hal/microcontroller/Pin.h" +#include "src/rp2_common/hardware_pio/include/hardware/pio.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t pin; + uint16_t* buffer; + uint16_t maxlen; + bool idle_state; + volatile uint16_t start; + volatile uint16_t len; + pio_sm_config sm_cfg; + PIO pio; + uint8_t sm; + uint8_t offset; + uint16_t pio_interrupt; +} pulseio_pulsein_obj_t; + +void pulsein_reset(void); +void common_hal_pulseio_pulsein_interrupt(); + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/raspberrypi/common-hal/pulseio/PulseOut.c b/ports/raspberrypi/common-hal/pulseio/PulseOut.c new file mode 100644 index 0000000000..57c516afd0 --- /dev/null +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.c @@ -0,0 +1,97 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * SPDX-FileCopyrightText: Copyright (c) 2016 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "common-hal/pulseio/PulseOut.h" + +#include + +#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" + +#include "mpconfigport.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/pulseio/PulseOut.h" +#include "supervisor/shared/translate.h" + +static uint8_t refcount = 0; + + +static uint16_t *pulse_buffer = NULL; +static volatile uint16_t pulse_index = 0; +static uint16_t pulse_length; +static volatile uint32_t current_compare = 0; + +void pulse_finish(void) { + pulse_index++; + + // Always turn it off. + if (pulse_index >= pulse_length) { + return; + } + current_compare = (current_compare + pulse_buffer[pulse_index] * 3 / 4) & 0xffff; +} + +void pulseout_reset() { + refcount = 0; +} + +void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, + const pwmio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle) { + if (!carrier || pin || frequency) { + mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. Construct and pass a PWMOut Carrier instead")); + } + + refcount++; + + self->pin = carrier->pin->number; + +} + +bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) { + return self->pin == NO_PIN; +} + +void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { + if (common_hal_pulseio_pulseout_deinited(self)) { + return; + } + + + refcount--; + self->pin = NO_PIN; +} + +void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { + pulse_buffer = pulses; + pulse_index = 0; + pulse_length = length; + + current_compare = pulses[0] * 3 / 4; + +} diff --git a/ports/raspberrypi/common-hal/pulseio/PulseOut.h b/ports/raspberrypi/common-hal/pulseio/PulseOut.h new file mode 100644 index 0000000000..e3f17e2d66 --- /dev/null +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.h @@ -0,0 +1,44 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +#define NO_PIN 0xff + +typedef struct { + mp_obj_base_t base; + uint8_t pin; +} pulseio_pulseout_obj_t; + +void pulseout_reset(void); +void pulseout_interrupt_handler(uint8_t index); + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/raspberrypi/common-hal/pulseio/__init__.c b/ports/raspberrypi/common-hal/pulseio/__init__.c new file mode 100644 index 0000000000..2bee925bc7 --- /dev/null +++ b/ports/raspberrypi/common-hal/pulseio/__init__.c @@ -0,0 +1 @@ +// No pulseio module functions. diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c index 90c48130e1..da85748a9d 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -193,6 +193,7 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, if (program_offset == 32) { program_offset = pio_add_program(self->pio, &program_struct); } + self->offset = program_offset; _current_program_id[pio_index][state_machine] = program_id; _current_program_len[pio_index][state_machine] = program_len; _current_program_offset[pio_index][state_machine] = program_offset; @@ -262,6 +263,7 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, self->in_shift_right = in_shift_right; sm_config_set_fifo_join(&c, join); + self->sm_config = c; pio_sm_init(self->pio, self->state_machine, program_offset, &c); pio_sm_set_enabled(self->pio, self->state_machine, true); diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.h b/ports/raspberrypi/common-hal/rp2pio/StateMachine.h index 6b70b6b5b5..289e91b7d7 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.h +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.h @@ -43,6 +43,8 @@ typedef struct { bool out_shift_right; bool in_shift_right; uint32_t actual_frequency; + pio_sm_config sm_config; + uint8_t offset; } rp2pio_statemachine_obj_t; void reset_rp2pio_statemachine(void); diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index ab85c59f9a..6f411387f5 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -26,13 +26,13 @@ endif CIRCUITPY_FULL_BUILD = 1 CIRCUITPY_BITOPS = 1 CIRCUITPY_PWMIO = 1 +CIRCUITPY_PULSEIO = 1 # Things that need to be implemented. CIRCUITPY_COUNTIO = 0 # Use PWM interally CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally CIRCUITPY_I2CPERIPHERAL = 0 CIRCUITPY_NVM = 0 -CIRCUITPY_PULSEIO = 0 # Use PIO interally CIRCUITPY_ROTARYIO = 0 # Use PIO interally CIRCUITPY_WATCHDOG = 1 From 17ff5dcc9934e6ed442bfa4d6cac2a435093e4aa Mon Sep 17 00:00:00 2001 From: root Date: Wed, 24 Feb 2021 17:04:05 -0600 Subject: [PATCH 02/21] Return NotImplementedError for PulseOut --- ports/raspberrypi/common-hal/pulseio/PulseOut.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseOut.c b/ports/raspberrypi/common-hal/pulseio/PulseOut.c index 57c516afd0..f3f7121434 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseOut.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.c @@ -63,9 +63,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, const mcu_pin_obj_t* pin, uint32_t frequency, uint16_t duty_cycle) { - if (!carrier || pin || frequency) { - mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. Construct and pass a PWMOut Carrier instead")); - } + mp_raise_NotImplementedError(translate("Unsupported operation")); refcount++; From 86e60f5f351659bfff4ff2cce37df36dc74e8352 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 24 Feb 2021 17:26:35 -0600 Subject: [PATCH 03/21] Clean up formatting --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index c642561c89..bee4398a2d 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -143,25 +143,25 @@ void common_hal_pulseio_pulsein_interrupt() { rxfifo = pio_sm_get_blocking(self->pio, self->sm); // translate from fifo to buffer for (uint i = 0; i < 32; i++) { - bool level = (rxfifo & (1 << i)) >> i; - if (level == last_level ) { - level_count ++; - } else { - result = level_count * 6; - last_level = level; - level_count = 1; + bool level = (rxfifo & (1 << i)) >> i; + if (level == last_level ) { + level_count ++; + } else { + result = level_count * 6; + last_level = level; + level_count = 1; // ignore pulses that are too long and too short - if (result < 10000 && result > 10) { - self->buffer[buf_index] = result; - buf_index++; - self->len++; - } + if (result < 10000 && result > 10) { + self->buffer[buf_index] = result; + buf_index++; + self->len++; + } } - } - // check for a pulse thats too long (20ms) - if ( level_count > 3000 ) { - break; - } + } + // check for a pulse thats too long (20ms) + if ( level_count > 3000 ) { + break; + } } pio_sm_set_enabled(self->pio, self->sm, false); pio_sm_init(self->pio, self->sm, self->offset, &self->sm_cfg); @@ -181,7 +181,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, } // Reconfigure the pin for PIO - common_hal_mcu_delay_us(100); + common_hal_mcu_delay_us(100); gpio_set_function(self->pin, GPIO_FUNC_PIO0); pio_sm_set_enabled(self->pio, self->sm, true); pio_sm_exec(self->pio,self->sm,0x20a0); From 79429321767492c79db4c6cce45d681261d78c66 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Feb 2021 14:14:55 -0600 Subject: [PATCH 04/21] Requested changes --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 59 ++++++++++--------- .../raspberrypi/common-hal/pulseio/PulseIn.h | 8 +-- .../raspberrypi/common-hal/pulseio/PulseOut.h | 2 +- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index bee4398a2d..8841cb9266 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017-2018 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2017-2021 Scott Shawcroft for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -45,8 +45,6 @@ pulseio_pulsein_obj_t* save_self; #define NO_PIN 0xff const uint16_t pulsein_program[] = { -// set pindirs, 0 ; For input - 0xe080, // wait 0 pin, 0 ; Wait for first low to start 0x2020, // irq wait 0 ; set IRQ 0 and wait @@ -55,7 +53,7 @@ const uint16_t pulsein_program[] = { // in pins, 1 [1] ; sample every 3 cycles (2 instructions, 1 delay) 0x4101, // jmp bitloop - 0x0003, + 0x0002, }; void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, @@ -85,25 +83,27 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, NULL, 0, 1 << self->pin, false, true, false, 8, false, // TX, unused - true, 32, true, // RX iauto-push every 32 bits + true, 32, true, // RX auto-push every 32 bits false); // claim pins - self->pio = state_machine.pio; - self->sm = state_machine.state_machine; - self->sm_cfg = state_machine.sm_config; - self->offset = state_machine.offset; - if ( self->pio == pio0 ) { + 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; + if ( self->state_machine.pio == pio0 ) { self->pio_interrupt = PIO0_IRQ_0; } else { self->pio_interrupt = PIO1_IRQ_0; } - pio_sm_set_in_pins(self->pio,self->sm,pin->number); - pio_sm_set_enabled(self->pio, self->sm, false); + pio_sm_set_in_pins(self->state_machine.pio,self->state_machine.state_machine,pin->number); + pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false); irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt); irq_set_enabled(self->pio_interrupt, true); - hw_clear_bits(&self->pio->inte0, 1u << self->sm); - hw_set_bits(&self->pio->inte0, 1u << (self->sm+8)); - pio_sm_set_enabled(self->pio, self->sm, true); + hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); + hw_set_bits(&self->state_machine.pio->inte0, 1u << (self->state_machine.state_machine+8)); + // exec a set pindirs to 0 for input + pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0xe080); + pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); } @@ -116,31 +116,31 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { return; } irq_set_enabled(self->pio_interrupt, false); - pio_sm_set_enabled(self->pio, self->sm, false); - pio_sm_unclaim (self->pio, self->sm); + 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); m_free(self->buffer); self->pin = NO_PIN; } void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { - pio_sm_set_enabled(self->pio, self->sm, false); + pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false); } void common_hal_pulseio_pulsein_interrupt() { pulseio_pulsein_obj_t* self = save_self; // clear interrupt - hw_clear_bits(&self->pio->inte0, 1u << self->sm); - self->pio->irq = 1u << self->sm; + hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); + self->state_machine.pio->irq = 1u << self->state_machine.state_machine; irq_clear(self->pio_interrupt); - pio_sm_clear_fifos(self->pio,self->sm); + pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine); bool last_level = true; uint level_count = 0; uint16_t result = 0; uint16_t buf_index = 0; while ( buf_index < self->maxlen ) { uint32_t rxfifo = 0; - rxfifo = pio_sm_get_blocking(self->pio, self->sm); + rxfifo = pio_sm_get_blocking(self->state_machine.pio, self->state_machine.state_machine); // translate from fifo to buffer for (uint i = 0; i < 32; i++) { bool level = (rxfifo & (1 << i)) >> i; @@ -163,10 +163,10 @@ void common_hal_pulseio_pulsein_interrupt() { break; } } - pio_sm_set_enabled(self->pio, self->sm, false); - pio_sm_init(self->pio, self->sm, self->offset, &self->sm_cfg); - pio_sm_restart(self->pio,self->sm); - pio_sm_set_enabled(self->pio, self->sm, true); + 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); irq_set_enabled(self->pio_interrupt, true); } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, @@ -175,7 +175,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, if (trigger_duration > 0) { gpio_set_function(self->pin ,GPIO_FUNC_SIO); gpio_set_dir(self->pin,true); - gpio_put(self->pin, false); + gpio_put(self->pin, !self->idle_state); common_hal_mcu_delay_us((uint32_t)trigger_duration); gpio_set_function(self->pin ,GPIO_FUNC_PIO0); } @@ -183,8 +183,9 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, // Reconfigure the pin for PIO common_hal_mcu_delay_us(100); gpio_set_function(self->pin, GPIO_FUNC_PIO0); - pio_sm_set_enabled(self->pio, self->sm, true); - pio_sm_exec(self->pio,self->sm,0x20a0); + pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); + // exec a wait for the selected pin to go high + pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0); } void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.h b/ports/raspberrypi/common-hal/pulseio/PulseIn.h index 35f7309eef..6be13ebb1c 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2017-2021 Scott Shawcroft for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,6 +29,7 @@ #include "common-hal/microcontroller/Pin.h" #include "src/rp2_common/hardware_pio/include/hardware/pio.h" +#include "common-hal/rp2pio/StateMachine.h" #include "py/obj.h" @@ -40,10 +41,7 @@ typedef struct { bool idle_state; volatile uint16_t start; volatile uint16_t len; - pio_sm_config sm_cfg; - PIO pio; - uint8_t sm; - uint8_t offset; + rp2pio_statemachine_obj_t state_machine; uint16_t pio_interrupt; } pulseio_pulsein_obj_t; diff --git a/ports/raspberrypi/common-hal/pulseio/PulseOut.h b/ports/raspberrypi/common-hal/pulseio/PulseOut.h index e3f17e2d66..10c1a1756e 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseOut.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2017-2021 Scott Shawcroft for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal From 6046d37f31c85ab90e3073c3a9ba5ec185082667 Mon Sep 17 00:00:00 2001 From: DavePutz Date: Tue, 2 Mar 2021 21:23:45 -0600 Subject: [PATCH 05/21] Update PulseIn.c --- ports/raspberrypi/common-hal/pulseio/PulseIn.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 8841cb9266..cd466dd93d 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -81,6 +81,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, pin, 1, NULL, 0, NULL, 0, + 1, self->pin, 1 << self->pin, false, true, false, 8, false, // TX, unused true, 32, true, // RX auto-push every 32 bits From 58fb7b9b327fa2532a123df9660669a0385f0b30 Mon Sep 17 00:00:00 2001 From: DavePutz Date: Tue, 2 Mar 2021 21:27:46 -0600 Subject: [PATCH 06/21] Update mpconfigport.mk to get builds working --- ports/raspberrypi/mpconfigport.mk | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index 6f411387f5..63a58b5a17 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -26,6 +26,8 @@ endif CIRCUITPY_FULL_BUILD = 1 CIRCUITPY_BITOPS = 1 CIRCUITPY_PWMIO = 1 + + CIRCUITPY_PULSEIO = 1 # Things that need to be implemented. From 30d491e136b0606ca6e38dd84869aab1e7f2f44f Mon Sep 17 00:00:00 2001 From: DavePutz Date: Tue, 2 Mar 2021 21:30:49 -0600 Subject: [PATCH 07/21] Update mpconfigport.mk --- ports/raspberrypi/mpconfigport.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index 63a58b5a17..f0ed852c00 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -34,7 +34,7 @@ CIRCUITPY_PULSEIO = 1 CIRCUITPY_COUNTIO = 0 # Use PWM interally CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally CIRCUITPY_I2CPERIPHERAL = 0 -CIRCUITPY_NVM = 0 +CIRCUITPY_NVM = 1 CIRCUITPY_ROTARYIO = 0 # Use PIO interally CIRCUITPY_WATCHDOG = 1 From 9b96bae66883f10895506e8917639936495ed9b8 Mon Sep 17 00:00:00 2001 From: DavePutz Date: Tue, 2 Mar 2021 21:36:00 -0600 Subject: [PATCH 08/21] Update mpconfigport.mk --- ports/raspberrypi/mpconfigport.mk | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index f0ed852c00..78aab307f1 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -27,14 +27,12 @@ CIRCUITPY_FULL_BUILD = 1 CIRCUITPY_BITOPS = 1 CIRCUITPY_PWMIO = 1 - -CIRCUITPY_PULSEIO = 1 - # Things that need to be implemented. CIRCUITPY_COUNTIO = 0 # Use PWM interally CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally CIRCUITPY_I2CPERIPHERAL = 0 CIRCUITPY_NVM = 1 +CIRCUITPY_PULSEIO = 1 # Use PIO interally CIRCUITPY_ROTARYIO = 0 # Use PIO interally CIRCUITPY_WATCHDOG = 1 From ced707ed329e83749d3474ea6f391f03f5bd1bdf Mon Sep 17 00:00:00 2001 From: DavePutz Date: Wed, 3 Mar 2021 10:55:08 -0600 Subject: [PATCH 09/21] Update call to rp2pio_statemachine_construct --- ports/raspberrypi/common-hal/pulseio/PulseIn.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index cd466dd93d..ea0d24f869 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -84,6 +84,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, 1, self->pin, 1 << self->pin, false, true, false, 8, false, // TX, unused + false, true, 32, true, // RX auto-push every 32 bits false); // claim pins self->state_machine.pio = state_machine.pio; From 88353f2f5bbe86879bd38f89dc73f3317ce288af Mon Sep 17 00:00:00 2001 From: DavePutz Date: Wed, 3 Mar 2021 11:00:58 -0600 Subject: [PATCH 10/21] Update mpconfigport.mk --- ports/raspberrypi/mpconfigport.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index 78aab307f1..8bf31208f9 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -32,7 +32,7 @@ CIRCUITPY_COUNTIO = 0 # Use PWM interally CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally CIRCUITPY_I2CPERIPHERAL = 0 CIRCUITPY_NVM = 1 -CIRCUITPY_PULSEIO = 1 # Use PIO interally +CIRCUITPY_PULSEIO = 1 CIRCUITPY_ROTARYIO = 0 # Use PIO interally CIRCUITPY_WATCHDOG = 1 From 2d941b070f348ef2f104825c7c6db9b22da49e83 Mon Sep 17 00:00:00 2001 From: DavePutz Date: Thu, 4 Mar 2021 13:51:46 -0600 Subject: [PATCH 11/21] Changed interrupt to per-word basis; cleaned up other small items --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 124 ++++++++++-------- .../raspberrypi/common-hal/pulseio/PulseIn.h | 2 +- 2 files changed, 67 insertions(+), 59 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index ea0d24f869..7053b55248 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017-2021 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2021 Dave Putz for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,9 +29,6 @@ #include -#include "background.h" -#include "mpconfigport.h" -#include "py/gc.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/pulseio/PulseIn.h" @@ -43,17 +40,19 @@ pulseio_pulsein_obj_t* save_self; #define NO_PIN 0xff +volatile bool last_level; +volatile uint16_t level_count = 0; +volatile uint16_t result = 0; +volatile uint16_t buf_index = 0; -const uint16_t pulsein_program[] = { -// wait 0 pin, 0 ; Wait for first low to start - 0x2020, -// irq wait 0 ; set IRQ 0 and wait - 0xc020, -// .bitloop -// in pins, 1 [1] ; sample every 3 cycles (2 instructions, 1 delay) - 0x4101, -// jmp bitloop - 0x0002, +uint16_t pulsein_program[] = { + 0x2020, // 0: wait 0 pin, 0 + 0xe03f, // 1: set x, 31 + 0x4001, // 2: in pins, 1 + 0x0042, // 3: jmp x--, 2 + 0x8060, // 4: push iffull block + 0xc020, // 5: irq wait 0 + 0x0001, // 6: jmp 1 }; void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, @@ -69,19 +68,23 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, self->start = 0; self->len = 0; save_self = self; + // change initial state machine wait if idle_state is false + if (idle_state == false) { + pulsein_program[0] = 0x20a0; + } // Set everything up. rp2pio_statemachine_obj_t state_machine; bool ok = rp2pio_statemachine_construct(&state_machine, pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]), - 125000 * 4, + 1000000 * 3, NULL, 0, NULL, 0, pin, 1, NULL, 0, NULL, 0, - 1, self->pin, + 1, 0, 1 << self->pin, false, true, false, 8, false, // TX, unused false, @@ -96,17 +99,22 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, } else { self->pio_interrupt = PIO1_IRQ_0; } + 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(self->state_machine.pio,self->state_machine.state_machine,pin->number); - pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false); + pio_sm_set_in_pins(state_machine.pio,state_machine.state_machine,pin->number); + pio_sm_set_enabled(state_machine.pio,state_machine.state_machine, false); irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt); - irq_set_enabled(self->pio_interrupt, true); - hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); - hw_set_bits(&self->state_machine.pio->inte0, 1u << (self->state_machine.state_machine+8)); - // exec a set pindirs to 0 for input - pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0xe080); - pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); + hw_clear_bits(&state_machine.pio->inte0, 1u << state_machine.state_machine); + hw_set_bits(&state_machine.pio->inte0, 1u << (state_machine.state_machine+8)); + // exec a set pindirs to 0 for input + pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080); + irq_set_enabled(self->pio_interrupt, true); + pio_sm_set_enabled(state_machine.pio, state_machine.state_machine, true); } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { @@ -131,48 +139,48 @@ void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { void common_hal_pulseio_pulsein_interrupt() { pulseio_pulsein_obj_t* self = save_self; -// clear interrupt - hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); - self->state_machine.pio->irq = 1u << self->state_machine.state_machine; - irq_clear(self->pio_interrupt); - pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine); - bool last_level = true; - uint level_count = 0; - uint16_t result = 0; - uint16_t buf_index = 0; - while ( buf_index < self->maxlen ) { - uint32_t rxfifo = 0; - rxfifo = pio_sm_get_blocking(self->state_machine.pio, self->state_machine.state_machine); - // translate from fifo to buffer - for (uint i = 0; i < 32; i++) { - bool level = (rxfifo & (1 << i)) >> i; - if (level == last_level ) { - level_count ++; - } else { - result = level_count * 6; - last_level = level; - level_count = 1; + uint32_t rxfifo = 0; + + rxfifo = pio_sm_get_blocking(self->state_machine.pio, self->state_machine.state_machine); + // translate from fifo to buffer + for (uint i = 0; i < 32; i++) { + bool level = (rxfifo & (1 << i)) >> i; + if (level == last_level ) { + level_count ++; + } else { + result = level_count; + last_level = level; + level_count = 1; // ignore pulses that are too long and too short - if (result < 10000 && result > 10) { + if (result < 2000 && result > 10) { self->buffer[buf_index] = result; buf_index++; self->len++; } } } - // check for a pulse thats too long (20ms) - if ( level_count > 3000 ) { - break; - } + gpio_put(pin_GPIO15.number, true); +// clear interrupt + irq_clear(self->pio_interrupt); + hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); + self->state_machine.pio->irq = 1u << self->state_machine.state_machine; +// check for a pulse thats too long (2000 us) and reset + if ( level_count > 2000 ) { + 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); + irq_set_enabled(self->pio_interrupt, true); } - 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); - irq_set_enabled(self->pio_interrupt, true); } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) { + // 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,0x20a0); + } else { + pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020); + } // Send the trigger pulse. if (trigger_duration > 0) { gpio_set_function(self->pin ,GPIO_FUNC_SIO); @@ -186,8 +194,6 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, common_hal_mcu_delay_us(100); gpio_set_function(self->pin, GPIO_FUNC_PIO0); pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); - // exec a wait for the selected pin to go high - pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0); } void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { @@ -202,9 +208,11 @@ 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 ) { - // reset buffer pointer self->start = 0; + buf_index = 0; + level_count = 0; } return value; } diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.h b/ports/raspberrypi/common-hal/pulseio/PulseIn.h index 6be13ebb1c..e99e1ff822 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017-2021 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2021 Dave Putz for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal From b7f03f9487608c413d30aaa43446afad0df3ad51 Mon Sep 17 00:00:00 2001 From: DavePutz Date: Thu, 4 Mar 2021 13:52:40 -0600 Subject: [PATCH 12/21] Update PulseOut.h --- ports/raspberrypi/common-hal/pulseio/PulseOut.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseOut.h b/ports/raspberrypi/common-hal/pulseio/PulseOut.h index 10c1a1756e..4f1bb9fa7a 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseOut.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017-2021 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2021 Dave Putz for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal From c5c297d2c06dbeb1754ea6aeb595c13ef41ad3eb Mon Sep 17 00:00:00 2001 From: DavePutz Date: Thu, 4 Mar 2021 13:53:19 -0600 Subject: [PATCH 13/21] Update PulseOut.c --- ports/raspberrypi/common-hal/pulseio/PulseOut.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseOut.c b/ports/raspberrypi/common-hal/pulseio/PulseOut.c index f3f7121434..e284cd46a2 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseOut.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * SPDX-FileCopyrightText: Copyright (c) 2016 Damien P. George + * Copyright (c) 2021 Dave Putz for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal From 88650bc4d04e8fd7da7760aca1530a4f5eec10d7 Mon Sep 17 00:00:00 2001 From: DavePutz Date: Mon, 8 Mar 2021 22:48:57 -0600 Subject: [PATCH 14/21] Removed leftover debug code --- ports/raspberrypi/common-hal/pulseio/PulseIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 7053b55248..79d8e7ee5b 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -1,4 +1,4 @@ -/* +b/* * This file is part of the MicroPython project, http://micropython.org/ * * The MIT License (MIT) From 3db0fec267902cddb4d88aaca55e7ae7f59dc9a8 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 8 Mar 2021 23:20:57 -0600 Subject: [PATCH 15/21] Added check for maxlen --- ports/raspberrypi/common-hal/pulseio/PulseIn.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 7053b55248..b9052f3c60 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -152,20 +152,19 @@ void common_hal_pulseio_pulsein_interrupt() { last_level = level; level_count = 1; // ignore pulses that are too long and too short - if (result < 2000 && result > 10) { + if (result < 4000 && result > 10) { self->buffer[buf_index] = result; buf_index++; self->len++; } } } - gpio_put(pin_GPIO15.number, true); // clear interrupt irq_clear(self->pio_interrupt); hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); self->state_machine.pio->irq = 1u << self->state_machine.state_machine; -// check for a pulse thats too long (2000 us) and reset - if ( level_count > 2000 ) { +// check for a pulse thats too long (2000 us) or maxlen reached, and reset + if (( level_count > 4000 ) || (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); From 576da73a17bd6847e93efcf4260a787813ccdf45 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 8 Mar 2021 23:23:51 -0600 Subject: [PATCH 16/21] Added check for maxlen --- ports/raspberrypi/common-hal/pulseio/PulseIn.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index fb05876b95..6377fa7a9c 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -1,4 +1,4 @@ -b/* +/* * This file is part of the MicroPython project, http://micropython.org/ * * The MIT License (MIT) @@ -163,7 +163,7 @@ void common_hal_pulseio_pulsein_interrupt() { irq_clear(self->pio_interrupt); hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); self->state_machine.pio->irq = 1u << self->state_machine.state_machine; -// check for a pulse thats too long (2000 us) or maxlen reached, and reset +// check for a pulse thats too long (4000 us) or maxlen reached, and reset if (( level_count > 4000 ) || (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); From f0e3274a33f8fa7dd109925d185c1c1dc758f63e Mon Sep 17 00:00:00 2001 From: DavePutz Date: Tue, 9 Mar 2021 11:13:09 -0600 Subject: [PATCH 17/21] Update to agree with modified StateMachine.h --- ports/raspberrypi/common-hal/pulseio/PulseIn.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 6377fa7a9c..1b433d79c3 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -82,6 +82,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, NULL, 0, NULL, 0, pin, 1, + 0,0, NULL, 0, NULL, 0, 1, 0, From 0c58cc47726bb8ca0b43af81263382a2a6fffef4 Mon Sep 17 00:00:00 2001 From: DavePutz Date: Tue, 9 Mar 2021 16:37:29 -0600 Subject: [PATCH 18/21] Moved pin wait to an initial exec --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 1b433d79c3..0013aece49 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -46,13 +46,12 @@ volatile uint16_t result = 0; volatile uint16_t buf_index = 0; uint16_t pulsein_program[] = { - 0x2020, // 0: wait 0 pin, 0 - 0xe03f, // 1: set x, 31 - 0x4001, // 2: in pins, 1 - 0x0042, // 3: jmp x--, 2 - 0x8060, // 4: push iffull block - 0xc020, // 5: irq wait 0 - 0x0001, // 6: jmp 1 + 0xe03f, // 0: set x, 31 + 0x4001, // 1: in pins, 1 + 0x0041, // 2: jmp x--, 2 + 0x8060, // 3: push iffull block + 0xc020, // 4: irq wait 0 + 0x0000, // 5: jmp 1 }; void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, @@ -68,10 +67,6 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, self->start = 0; self->len = 0; save_self = self; - // change initial state machine wait if idle_state is false - if (idle_state == false) { - pulsein_program[0] = 0x20a0; - } // Set everything up. rp2pio_statemachine_obj_t state_machine; @@ -91,6 +86,7 @@ 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; @@ -107,15 +103,20 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, buf_index = 0; pio_sm_set_in_pins(state_machine.pio,state_machine.state_machine,pin->number); - pio_sm_set_enabled(state_machine.pio,state_machine.state_machine, false); irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt); hw_clear_bits(&state_machine.pio->inte0, 1u << state_machine.state_machine); hw_set_bits(&state_machine.pio->inte0, 1u << (state_machine.state_machine+8)); // exec a set pindirs to 0 for input pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080); - irq_set_enabled(self->pio_interrupt, true); + //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); + irq_set_enabled(self->pio_interrupt, true); } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { @@ -177,9 +178,9 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) { // 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,0x20a0); - } else { 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); } // Send the trigger pulse. if (trigger_duration > 0) { @@ -191,7 +192,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, } // Reconfigure the pin for PIO - common_hal_mcu_delay_us(100); + common_hal_mcu_delay_us(200); gpio_set_function(self->pin, GPIO_FUNC_PIO0); pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); } From a3c3e8a0fa5e06910747f1a95a12b899562a618d Mon Sep 17 00:00:00 2001 From: DavePutz Date: Tue, 9 Mar 2021 22:41:08 -0600 Subject: [PATCH 19/21] fix trailing whitespace --- ports/raspberrypi/common-hal/pulseio/PulseIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 0013aece49..8ddeab9308 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -109,7 +109,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, // exec a set pindirs to 0 for input pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080); - //exec the appropriate wait for pin + //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 { From 6628f49852a4963cb2aeaa08fa281eb0e994415e Mon Sep 17 00:00:00 2001 From: DavePutz Date: Fri, 12 Mar 2021 16:06:57 -0600 Subject: [PATCH 20/21] Use StateMachine.c interrupt setup and simplify SM program --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 37 +++++++++++++++++++ .../raspberrypi/common-hal/pulseio/PulseIn.h | 3 ++ 2 files changed, 40 insertions(+) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 8ddeab9308..1a30373fcc 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -25,7 +25,10 @@ */ #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" +<<<<<<< HEAD +======= #include "src/rp2_common/hardware_irq/include/hardware/irq.h" +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d #include @@ -46,12 +49,16 @@ volatile uint16_t result = 0; volatile uint16_t buf_index = 0; uint16_t pulsein_program[] = { +<<<<<<< HEAD + 0x4001, // 1: in pins, 1 +======= 0xe03f, // 0: set x, 31 0x4001, // 1: in pins, 1 0x0041, // 2: jmp x--, 2 0x8060, // 3: push iffull block 0xc020, // 4: irq wait 0 0x0000, // 5: jmp 1 +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d }; void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, @@ -73,7 +80,11 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, bool ok = rp2pio_statemachine_construct(&state_machine, pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]), +<<<<<<< HEAD + 1000000, +======= 1000000 * 3, +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d NULL, 0, NULL, 0, pin, 1, @@ -91,11 +102,14 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, 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; +<<<<<<< HEAD +======= if ( self->state_machine.pio == pio0 ) { self->pio_interrupt = PIO0_IRQ_0; } else { self->pio_interrupt = PIO1_IRQ_0; } +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine); last_level = self->idle_state; level_count = 0; @@ -103,9 +117,13 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, buf_index = 0; pio_sm_set_in_pins(state_machine.pio,state_machine.state_machine,pin->number); +<<<<<<< HEAD + common_hal_rp2pio_statemachine_set_interrupt_handler(&state_machine,&common_hal_pulseio_pulsein_interrupt,NULL,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS); +======= irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt); hw_clear_bits(&state_machine.pio->inte0, 1u << state_machine.state_machine); hw_set_bits(&state_machine.pio->inte0, 1u << (state_machine.state_machine+8)); +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d // exec a set pindirs to 0 for input pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080); @@ -116,7 +134,10 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, 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); +<<<<<<< HEAD +======= irq_set_enabled(self->pio_interrupt, true); +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { @@ -127,7 +148,10 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { if (common_hal_pulseio_pulsein_deinited(self)) { return; } +<<<<<<< HEAD +======= irq_set_enabled(self->pio_interrupt, false); +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d 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); m_free(self->buffer); @@ -161,17 +185,23 @@ void common_hal_pulseio_pulsein_interrupt() { } } } +<<<<<<< HEAD +======= // clear interrupt irq_clear(self->pio_interrupt); hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); self->state_machine.pio->irq = 1u << self->state_machine.state_machine; +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d // check for a pulse thats too long (4000 us) or maxlen reached, and reset if (( level_count > 4000 ) || (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); +<<<<<<< HEAD +======= irq_set_enabled(self->pio_interrupt, true); +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d } } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, @@ -189,10 +219,17 @@ 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); +<<<<<<< HEAD + common_hal_mcu_delay_us(225); + } + + // Reconfigure the pin for PIO +======= } // Reconfigure the pin for PIO common_hal_mcu_delay_us(200); +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d gpio_set_function(self->pin, GPIO_FUNC_PIO0); pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); } diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.h b/ports/raspberrypi/common-hal/pulseio/PulseIn.h index e99e1ff822..39199e5249 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.h @@ -42,7 +42,10 @@ typedef struct { volatile uint16_t start; volatile uint16_t len; rp2pio_statemachine_obj_t state_machine; +<<<<<<< HEAD +======= uint16_t pio_interrupt; +>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d } pulseio_pulsein_obj_t; void pulsein_reset(void); From 96ce43e6d6eaad1ef4d7ab455f594ed64169bc9d Mon Sep 17 00:00:00 2001 From: DavePutz Date: Fri, 12 Mar 2021 16:18:33 -0600 Subject: [PATCH 21/21] Added the correct files --- .../raspberrypi/common-hal/pulseio/PulseIn.c | 57 ------------------- .../raspberrypi/common-hal/pulseio/PulseIn.h | 4 -- 2 files changed, 61 deletions(-) diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c index 1a30373fcc..e05b35536c 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -25,10 +25,6 @@ */ #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" -<<<<<<< HEAD -======= -#include "src/rp2_common/hardware_irq/include/hardware/irq.h" ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d #include @@ -49,16 +45,7 @@ volatile uint16_t result = 0; volatile uint16_t buf_index = 0; uint16_t pulsein_program[] = { -<<<<<<< HEAD 0x4001, // 1: in pins, 1 -======= - 0xe03f, // 0: set x, 31 - 0x4001, // 1: in pins, 1 - 0x0041, // 2: jmp x--, 2 - 0x8060, // 3: push iffull block - 0xc020, // 4: irq wait 0 - 0x0000, // 5: jmp 1 ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d }; void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, @@ -80,11 +67,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, bool ok = rp2pio_statemachine_construct(&state_machine, pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]), -<<<<<<< HEAD 1000000, -======= - 1000000 * 3, ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d NULL, 0, NULL, 0, pin, 1, @@ -102,14 +85,6 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, 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; -<<<<<<< HEAD -======= - if ( self->state_machine.pio == pio0 ) { - self->pio_interrupt = PIO0_IRQ_0; - } else { - self->pio_interrupt = PIO1_IRQ_0; - } ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine); last_level = self->idle_state; level_count = 0; @@ -117,13 +92,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, buf_index = 0; pio_sm_set_in_pins(state_machine.pio,state_machine.state_machine,pin->number); -<<<<<<< HEAD common_hal_rp2pio_statemachine_set_interrupt_handler(&state_machine,&common_hal_pulseio_pulsein_interrupt,NULL,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS); -======= - irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt); - hw_clear_bits(&state_machine.pio->inte0, 1u << state_machine.state_machine); - hw_set_bits(&state_machine.pio->inte0, 1u << (state_machine.state_machine+8)); ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d // exec a set pindirs to 0 for input pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080); @@ -134,10 +103,6 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, 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); -<<<<<<< HEAD -======= - irq_set_enabled(self->pio_interrupt, true); ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { @@ -148,10 +113,6 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { if (common_hal_pulseio_pulsein_deinited(self)) { return; } -<<<<<<< HEAD -======= - irq_set_enabled(self->pio_interrupt, false); ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d 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); m_free(self->buffer); @@ -185,23 +146,12 @@ void common_hal_pulseio_pulsein_interrupt() { } } } -<<<<<<< HEAD -======= -// clear interrupt - irq_clear(self->pio_interrupt); - hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine); - self->state_machine.pio->irq = 1u << self->state_machine.state_machine; ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d // check for a pulse thats too long (4000 us) or maxlen reached, and reset if (( level_count > 4000 ) || (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); -<<<<<<< HEAD -======= - irq_set_enabled(self->pio_interrupt, true); ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d } } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, @@ -219,17 +169,10 @@ 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); -<<<<<<< HEAD common_hal_mcu_delay_us(225); } // Reconfigure the pin for PIO -======= - } - - // Reconfigure the pin for PIO - common_hal_mcu_delay_us(200); ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d gpio_set_function(self->pin, GPIO_FUNC_PIO0); pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); } diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.h b/ports/raspberrypi/common-hal/pulseio/PulseIn.h index 39199e5249..c83a86fca0 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.h @@ -42,10 +42,6 @@ typedef struct { volatile uint16_t start; volatile uint16_t len; rp2pio_statemachine_obj_t state_machine; -<<<<<<< HEAD -======= - uint16_t pio_interrupt; ->>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d } pulseio_pulsein_obj_t; void pulsein_reset(void);