|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "common-hal/nativeio/PulseIn.h" |
| 28 | + |
| 29 | +#include <stdint.h> |
| 30 | + |
| 31 | +#include "asf/common2/services/delay/delay.h" |
| 32 | +#include "asf/sam0/drivers/extint/extint.h" |
| 33 | +#include "asf/sam0/drivers/extint/extint_callback.h" |
| 34 | + |
| 35 | +#include "mpconfigport.h" |
| 36 | +#include "py/gc.h" |
| 37 | +#include "py/runtime.h" |
| 38 | +#include "samd21_pins.h" |
| 39 | +#include "shared-bindings/microcontroller/__init__.h" |
| 40 | +#include "shared-bindings/nativeio/PulseIn.h" |
| 41 | + |
| 42 | +#include "tick.h" |
| 43 | + |
| 44 | +static nativeio_pulsein_obj_t *active_pulseins[EIC_NUMBER_OF_INTERRUPTS]; |
| 45 | +static uint64_t last_ms[EIC_NUMBER_OF_INTERRUPTS]; |
| 46 | +static uint16_t last_us[EIC_NUMBER_OF_INTERRUPTS]; |
| 47 | + |
| 48 | +void pulsein_reset(void) { |
| 49 | + for (int i = 0; i < EIC_NUMBER_OF_INTERRUPTS; i++) { |
| 50 | + active_pulseins[i] = NULL; |
| 51 | + last_ms[i] = 0; |
| 52 | + last_us[i] = 0; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +static void pulsein_set_config(nativeio_pulsein_obj_t* self, bool first_edge) { |
| 57 | + struct extint_chan_conf config; |
| 58 | + extint_chan_get_config_defaults(&config); |
| 59 | + config.gpio_pin = self->pin; |
| 60 | + config.gpio_pin_pull = EXTINT_PULL_NONE; |
| 61 | + config.filter_input_signal = true; |
| 62 | + |
| 63 | + if (!first_edge) { |
| 64 | + config.detection_criteria = EXTINT_DETECT_BOTH; |
| 65 | + } else if (self->idle_state) { |
| 66 | + config.detection_criteria = EXTINT_DETECT_FALLING; |
| 67 | + } else { |
| 68 | + config.detection_criteria = EXTINT_DETECT_RISING; |
| 69 | + } |
| 70 | + extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); |
| 71 | + extint_chan_set_config(self->channel, &config); |
| 72 | + // Clear any interrupts that may have triggered without notifying the CPU. |
| 73 | + EIC->INTFLAG.reg |= (1UL << self->channel); |
| 74 | + extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); |
| 75 | +} |
| 76 | + |
| 77 | +static void pulsein_callback(void) { |
| 78 | + // Grab the current time first. |
| 79 | + uint16_t current_us = tc_get_count_value(&ms_timer); |
| 80 | + uint64_t current_ms = ticks_ms; |
| 81 | + nativeio_pulsein_obj_t* self = active_pulseins[extint_get_current_channel()]; |
| 82 | + current_us = current_us * 1000 / self->ticks_per_ms; |
| 83 | + if (self->first_edge) { |
| 84 | + self->first_edge = false; |
| 85 | + pulsein_set_config(self, false); |
| 86 | + } else { |
| 87 | + uint32_t ms_diff = current_ms - last_ms[self->channel]; |
| 88 | + uint16_t us_diff = current_us - last_us[self->channel]; |
| 89 | + if (last_us[self->channel] > current_us) { |
| 90 | + us_diff = 1000 + current_us - last_us[self->channel]; |
| 91 | + } |
| 92 | + uint32_t total_diff = us_diff; |
| 93 | + if (ms_diff > 1) { |
| 94 | + total_diff += (ms_diff - 1) * 1000; |
| 95 | + } |
| 96 | + uint16_t duration = 0xffff; |
| 97 | + if (total_diff < duration) { |
| 98 | + duration = total_diff; |
| 99 | + } |
| 100 | + |
| 101 | + uint16_t i = (self->start + self->len) % self->maxlen; |
| 102 | + self->buffer[i] = duration; |
| 103 | + if (self->len < self->maxlen) { |
| 104 | + self->len++; |
| 105 | + } else { |
| 106 | + self->start++; |
| 107 | + } |
| 108 | + } |
| 109 | + last_ms[self->channel] = current_ms; |
| 110 | + last_us[self->channel] = current_us; |
| 111 | +} |
| 112 | + |
| 113 | +void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, |
| 114 | + const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { |
| 115 | + if (!pin->has_extint) { |
| 116 | + mp_raise_RuntimeError("No hardware support on pin"); |
| 117 | + } |
| 118 | + // TODO(tannewt): Switch to checking actual extint peripheral state when other |
| 119 | + // classes use extints. |
| 120 | + if (active_pulseins[pin->extint_channel] != NULL) { |
| 121 | + mp_raise_RuntimeError("EXTINT channel already in use"); |
| 122 | + } |
| 123 | + |
| 124 | + self->buffer = (uint16_t *) gc_alloc(maxlen * sizeof(uint16_t), false); |
| 125 | + if (self->buffer == NULL) { |
| 126 | + mp_raise_msg_varg(&mp_type_MemoryError, "Failed to allocate RX buffer of %d bytes", maxlen * sizeof(uint16_t)); |
| 127 | + } |
| 128 | + self->channel = pin->extint_channel; |
| 129 | + self->pin = pin->pin; |
| 130 | + self->maxlen = maxlen; |
| 131 | + self->idle_state = idle_state; |
| 132 | + self->start = 0; |
| 133 | + self->len = 0; |
| 134 | + self->first_edge = true; |
| 135 | + self->ticks_per_ms = (system_cpu_clock_get_hz() / 1000 - 1); |
| 136 | + |
| 137 | + active_pulseins[pin->extint_channel] = self; |
| 138 | + |
| 139 | + pulsein_set_config(self, true); |
| 140 | + extint_register_callback( |
| 141 | + pulsein_callback, |
| 142 | + self->channel, |
| 143 | + EXTINT_CALLBACK_TYPE_DETECT); |
| 144 | + extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); |
| 145 | +} |
| 146 | + |
| 147 | +void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) { |
| 148 | + extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); |
| 149 | + active_pulseins[self->channel] = NULL; |
| 150 | + reset_pin(self->pin); |
| 151 | +} |
| 152 | + |
| 153 | +void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) { |
| 154 | + extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); |
| 155 | +} |
| 156 | + |
| 157 | +void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, |
| 158 | + uint16_t trigger_duration) { |
| 159 | + // Send the trigger pulse. |
| 160 | + if (trigger_duration > 0) { |
| 161 | + struct port_config pin_conf; |
| 162 | + port_get_config_defaults(&pin_conf); |
| 163 | + |
| 164 | + pin_conf.direction = PORT_PIN_DIR_OUTPUT; |
| 165 | + pin_conf.input_pull = PORT_PIN_PULL_NONE; |
| 166 | + port_pin_set_config(self->pin, &pin_conf); |
| 167 | + port_pin_set_output_level(self->pin, !self->idle_state); |
| 168 | + delay_us(trigger_duration); |
| 169 | + port_pin_set_output_level(self->pin, self->idle_state); |
| 170 | + } |
| 171 | + |
| 172 | + // Reconfigure the pin and make sure its set to detect the first edge. |
| 173 | + last_ms[self->channel] = 0; |
| 174 | + last_us[self->channel] = 0; |
| 175 | + self->first_edge = true; |
| 176 | + pulsein_set_config(self, true); |
| 177 | +} |
| 178 | + |
| 179 | +void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) { |
| 180 | + common_hal_mcu_disable_interrupts(); |
| 181 | + self->start = 0; |
| 182 | + self->len = 0; |
| 183 | + common_hal_mcu_enable_interrupts(); |
| 184 | +} |
| 185 | + |
| 186 | +uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) { |
| 187 | + if (self->len == 0) { |
| 188 | + mp_raise_IndexError("pop from an empty PulseIn"); |
| 189 | + } |
| 190 | + common_hal_mcu_disable_interrupts(); |
| 191 | + uint16_t value = self->buffer[self->start]; |
| 192 | + self->start = (self->start + 1) % self->maxlen; |
| 193 | + self->len--; |
| 194 | + common_hal_mcu_enable_interrupts(); |
| 195 | + |
| 196 | + return value; |
| 197 | +} |
| 198 | + |
| 199 | +uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) { |
| 200 | + return self->maxlen; |
| 201 | +} |
| 202 | + |
| 203 | +uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) { |
| 204 | + return self->len; |
| 205 | +} |
| 206 | + |
| 207 | +uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, |
| 208 | + int16_t index) { |
| 209 | + common_hal_mcu_disable_interrupts(); |
| 210 | + if (index < 0) { |
| 211 | + index += self->len; |
| 212 | + } |
| 213 | + if (index < 0 || index >= self->len) { |
| 214 | + common_hal_mcu_enable_interrupts(); |
| 215 | + mp_raise_IndexError("index out of range"); |
| 216 | + } |
| 217 | + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; |
| 218 | + common_hal_mcu_enable_interrupts(); |
| 219 | + return value; |
| 220 | +} |
0 commit comments