diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.c b/ports/raspberrypi/common-hal/pulseio/PulseIn.c new file mode 100644 index 0000000000..e05b35536c --- /dev/null +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.c @@ -0,0 +1,223 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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 + +#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 +volatile bool last_level; +volatile uint16_t level_count = 0; +volatile uint16_t result = 0; +volatile uint16_t buf_index = 0; + +uint16_t pulsein_program[] = { + 0x4001, // 1: in pins, 1 +}; + +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]), + 1000000, + NULL, 0, + NULL, 0, + pin, 1, + 0,0, + NULL, 0, + NULL, 0, + 1, 0, + 1 << self->pin, false, true, + false, 8, false, // TX, unused + 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_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); + + // 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 + 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); +} + +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; + } + 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->state_machine.pio, self->state_machine.state_machine, false); +} + +void common_hal_pulseio_pulsein_interrupt() { + + pulseio_pulsein_obj_t* self = save_self; + 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 < 4000 && result > 10) { + self->buffer[buf_index] = result; + buf_index++; + self->len++; + } + } + } +// 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); + } +} +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,0x2020); + } else { + pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0); + } + // 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, !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(225); + } + + // Reconfigure the pin for PIO + gpio_set_function(self->pin, GPIO_FUNC_PIO0); + pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true); +} + +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 we are empty reset buffer pointer and counters + if (self->len == 0 ) { + self->start = 0; + buf_index = 0; + level_count = 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..c83a86fca0 --- /dev/null +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.h @@ -0,0 +1,50 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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 "common-hal/rp2pio/StateMachine.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; + rp2pio_statemachine_obj_t state_machine; +} 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..e284cd46a2 --- /dev/null +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.c @@ -0,0 +1,95 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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) { + mp_raise_NotImplementedError(translate("Unsupported operation")); + + 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..4f1bb9fa7a --- /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) 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 + * 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 672cd4d3cf..56a7ab668d 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -222,6 +222,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; @@ -303,6 +304,7 @@ bool rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self, self->init_len = init_len; sm_config_set_fifo_join(&c, join); + self->sm_config = c; pio_sm_init(self->pio, self->state_machine, program_offset, &c); common_hal_rp2pio_statemachine_run(self, init, init_len); diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.h b/ports/raspberrypi/common-hal/rp2pio/StateMachine.h index f40db61ea3..15bc2a346c 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.h +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.h @@ -51,6 +51,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 3388280278..bcfe3efd1e 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -37,7 +37,7 @@ CIRCUITPY_FREQUENCYIO = 0 CIRCUITPY_I2CPERIPHERAL = 0 CIRCUITPY_NVM = 1 # Use PIO interally -CIRCUITPY_PULSEIO = 0 +CIRCUITPY_PULSEIO = 1 CIRCUITPY_WATCHDOG = 1 # Audio via PWM