2021-03-09 00:23:51 -05:00
|
|
|
/*
|
2021-02-24 17:58:29 -05:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2021-03-04 14:51:46 -05:00
|
|
|
* Copyright (c) 2021 Dave Putz for Adafruit Industries
|
2021-02-24 17:58:29 -05:00
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
#include "shared-bindings/pulseio/PulseIn.h"
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
#include "bindings/rp2pio/StateMachine.h"
|
|
|
|
#include "common-hal/pulseio/PulseIn.h"
|
|
|
|
|
|
|
|
#define NO_PIN 0xff
|
2021-04-26 19:52:15 -04:00
|
|
|
#define MAX_PULSE 65535
|
2022-05-18 13:34:25 -04:00
|
|
|
#define MIN_PULSE 0
|
2021-03-04 14:51:46 -05:00
|
|
|
|
2022-05-03 00:00:58 -04:00
|
|
|
static const uint16_t pulsein_program[] = {
|
2021-03-12 17:06:57 -05:00
|
|
|
0x4001, // 1: in pins, 1
|
2021-02-24 17:58:29 -05:00
|
|
|
};
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
|
|
|
|
const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) {
|
2021-02-24 17:58:29 -05:00
|
|
|
|
2023-08-11 00:30:34 -04:00
|
|
|
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t));
|
2021-02-24 17:58:29 -05:00
|
|
|
if (self->buffer == NULL) {
|
2022-05-13 15:33:43 -04:00
|
|
|
m_malloc_fail(maxlen * sizeof(uint16_t));
|
2021-02-24 17:58:29 -05:00
|
|
|
}
|
|
|
|
self->pin = pin->number;
|
|
|
|
self->maxlen = maxlen;
|
|
|
|
self->idle_state = idle_state;
|
|
|
|
self->start = 0;
|
|
|
|
self->len = 0;
|
|
|
|
|
2022-05-03 00:00:58 -04:00
|
|
|
common_hal_rp2pio_statemachine_construct(&self->state_machine,
|
|
|
|
pulsein_program, MP_ARRAY_SIZE(pulsein_program),
|
|
|
|
1000000, // frequency
|
|
|
|
NULL, 0, // init, init_len
|
2023-07-27 12:39:40 -04:00
|
|
|
NULL, 0, // may_exec
|
2022-05-03 00:00:58 -04:00
|
|
|
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.
|
2023-07-27 12:58:43 -04:00
|
|
|
0, -1, // wrap settings
|
|
|
|
PIO_ANY_OFFSET);
|
2021-07-16 16:59:08 -04:00
|
|
|
|
2022-05-18 13:34:25 -04:00
|
|
|
common_hal_pulseio_pulsein_pause(self);
|
2021-02-24 17:58:29 -05:00
|
|
|
|
2023-08-14 00:47:22 -04:00
|
|
|
common_hal_rp2pio_statemachine_set_interrupt_handler(&(self->state_machine), &common_hal_pulseio_pulsein_interrupt, self, PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS);
|
2021-02-24 17:58:29 -05:00
|
|
|
|
2022-05-18 13:34:25 -04:00
|
|
|
common_hal_pulseio_pulsein_resume(self, 0);
|
2021-02-24 17:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) {
|
2021-02-24 17:58:29 -05:00
|
|
|
return self->pin == NO_PIN;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
|
2021-02-24 17:58:29 -05:00
|
|
|
if (common_hal_pulseio_pulsein_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-26 15:14:55 -05:00
|
|
|
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
|
2021-04-25 12:58:22 -04:00
|
|
|
common_hal_rp2pio_statemachine_deinit(&self->state_machine);
|
2021-02-24 17:58:29 -05:00
|
|
|
m_free(self->buffer);
|
2021-04-25 12:58:22 -04:00
|
|
|
reset_pin_number(self->pin);
|
2021-02-24 17:58:29 -05:00
|
|
|
self->pin = NO_PIN;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) {
|
2021-06-04 13:10:41 -04:00
|
|
|
pio_sm_restart(self->state_machine.pio, self->state_machine.state_machine);
|
2021-02-26 15:14:55 -05:00
|
|
|
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
|
2023-08-14 00:47:22 -04:00
|
|
|
pio_sm_clear_fifos(self->state_machine.pio, self->state_machine.state_machine);
|
2021-07-16 16:59:08 -04:00
|
|
|
self->last_level = self->idle_state;
|
|
|
|
self->level_count = 0;
|
2022-05-18 13:34:25 -04:00
|
|
|
self->paused = true;
|
2021-02-24 17:58:29 -05:00
|
|
|
}
|
2021-11-10 09:42:21 -05:00
|
|
|
void common_hal_pulseio_pulsein_interrupt(void *self_in) {
|
|
|
|
pulseio_pulsein_obj_t *self = self_in;
|
2021-02-24 17:58:29 -05:00
|
|
|
|
2021-03-04 14:51:46 -05:00
|
|
|
uint32_t rxfifo = 0;
|
|
|
|
|
|
|
|
rxfifo = pio_sm_get_blocking(self->state_machine.pio, self->state_machine.state_machine);
|
|
|
|
// translate from fifo to buffer
|
2021-07-16 16:59:08 -04:00
|
|
|
if ((rxfifo == 0 && self->last_level == false) || (rxfifo == 0xffffffff && self->last_level == true)) {
|
2021-07-16 18:18:06 -04:00
|
|
|
self->level_count = self->level_count + 32;
|
2021-07-16 16:59:08 -04:00
|
|
|
} else {
|
2021-07-16 18:18:06 -04:00
|
|
|
for (uint i = 0; i < 32; i++) {
|
|
|
|
bool level = (rxfifo & (1 << i)) >> i;
|
|
|
|
if (level == self->last_level) {
|
|
|
|
self->level_count++;
|
|
|
|
} else {
|
2021-07-18 13:00:37 -04:00
|
|
|
uint32_t result = self->level_count;
|
2021-07-16 18:18:06 -04:00
|
|
|
self->last_level = level;
|
2022-05-18 13:34:25 -04:00
|
|
|
self->level_count = 1;
|
2021-07-16 18:18:06 -04:00
|
|
|
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
|
|
|
|
if (result > MAX_PULSE) {
|
|
|
|
result = MAX_PULSE;
|
2021-04-30 16:31:08 -04:00
|
|
|
}
|
2021-07-16 18:18:06 -04:00
|
|
|
// return pulses that are not too short
|
|
|
|
if (result > MIN_PULSE) {
|
2022-05-06 20:57:10 -04:00
|
|
|
size_t buf_index = (self->start + self->len) % self->maxlen;
|
|
|
|
self->buffer[buf_index] = (uint16_t)result;
|
2021-07-16 18:18:06 -04:00
|
|
|
if (self->len < self->maxlen) {
|
|
|
|
self->len++;
|
2022-05-06 20:57:10 -04:00
|
|
|
} else {
|
|
|
|
self->start = (self->start + 1) % self->maxlen;
|
2021-07-16 18:18:06 -04:00
|
|
|
}
|
2021-04-29 13:38:13 -04:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 17:58:29 -05:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
|
|
|
|
uint16_t trigger_duration) {
|
2022-05-18 13:34:25 -04:00
|
|
|
|
|
|
|
common_hal_pulseio_pulsein_pause(self);
|
2021-02-24 17:58:29 -05:00
|
|
|
// Send the trigger pulse.
|
|
|
|
if (trigger_duration > 0) {
|
2023-08-14 00:47:22 -04:00
|
|
|
gpio_set_function(self->pin, GPIO_FUNC_SIO);
|
|
|
|
gpio_set_dir(self->pin, true);
|
2021-02-26 15:14:55 -05:00
|
|
|
gpio_put(self->pin, !self->idle_state);
|
2021-02-24 17:58:29 -05:00
|
|
|
common_hal_mcu_delay_us((uint32_t)trigger_duration);
|
2023-08-14 00:47:22 -04:00
|
|
|
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
|
2021-03-12 17:06:57 -05:00
|
|
|
}
|
|
|
|
|
2021-06-04 13:10:41 -04:00
|
|
|
// exec a wait for the selected pin to change state
|
|
|
|
if (self->idle_state == true) {
|
2023-08-14 00:47:22 -04:00
|
|
|
pio_sm_exec(self->state_machine.pio, self->state_machine.state_machine, 0x2020);
|
2021-06-04 13:10:41 -04:00
|
|
|
} else {
|
2023-08-14 00:47:22 -04:00
|
|
|
pio_sm_exec(self->state_machine.pio, self->state_machine.state_machine, 0x20a0);
|
2021-06-04 13:10:41 -04:00
|
|
|
}
|
2021-02-26 15:14:55 -05:00
|
|
|
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
|
2022-05-18 13:34:25 -04:00
|
|
|
self->paused = false;
|
2021-02-24 17:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) {
|
2021-02-24 17:58:29 -05:00
|
|
|
self->len = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
|
2021-02-24 17:58:29 -05:00
|
|
|
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--;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) {
|
2021-02-24 17:58:29 -05:00
|
|
|
return self->maxlen;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
|
2021-02-24 17:58:29 -05:00
|
|
|
return self->len;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
|
2022-05-18 13:34:25 -04:00
|
|
|
return self->paused;
|
2021-02-24 17:58:29 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self,
|
|
|
|
int16_t index) {
|
2021-02-24 17:58:29 -05:00
|
|
|
if (index < 0) {
|
|
|
|
index += self->len;
|
|
|
|
}
|
|
|
|
if (index < 0 || index >= self->len) {
|
2022-05-20 10:10:55 -04:00
|
|
|
mp_arg_validate_index_range(index, 0, self->len, MP_QSTR_index);
|
2021-02-24 17:58:29 -05:00
|
|
|
}
|
|
|
|
uint16_t value = self->buffer[(self->start + index) % self->maxlen];
|
|
|
|
return value;
|
|
|
|
}
|