2017-03-13 16:55:11 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2017-03-13 16:55:11 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2018-05-25 21:39:16 -04:00
|
|
|
* Copyright (c) 2017-2018 Scott Shawcroft for Adafruit Industries
|
2017-03-13 16:55:11 -04: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.
|
|
|
|
*/
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "common-hal/pulseio/PulseIn.h"
|
2017-03-13 16:55:11 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-02-14 19:59:04 -05:00
|
|
|
#include "atmel_start_pins.h"
|
2018-02-09 03:22:29 -05:00
|
|
|
#include "hal/include/hal_gpio.h"
|
2017-03-13 16:55:11 -04:00
|
|
|
|
2018-06-05 15:38:31 -04:00
|
|
|
#include "background.h"
|
2019-04-04 16:04:11 -04:00
|
|
|
#include "eic_handler.h"
|
2017-03-13 16:55:11 -04:00
|
|
|
#include "mpconfigport.h"
|
2020-03-13 20:21:15 -04:00
|
|
|
#include "timer_handler.h"
|
2017-03-13 16:55:11 -04:00
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/runtime.h"
|
2018-06-15 19:16:21 -04:00
|
|
|
#include "samd/external_interrupts.h"
|
|
|
|
#include "samd/pins.h"
|
2020-03-13 20:21:15 -04:00
|
|
|
#include "samd/timers.h"
|
2017-03-13 16:55:11 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/pulseio/PulseIn.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2017-03-13 16:55:11 -04:00
|
|
|
|
2020-03-13 20:21:15 -04:00
|
|
|
// This timer is shared amongst all PulseIn objects as a higher resolution clock.
|
|
|
|
static uint8_t refcount = 0;
|
|
|
|
static uint8_t pulsein_tc_index = 0xff;
|
|
|
|
|
2020-04-02 20:35:52 -04:00
|
|
|
volatile static uint32_t overflow_count = 0;
|
2020-03-13 20:21:15 -04:00
|
|
|
|
|
|
|
void pulsein_timer_interrupt_handler(uint8_t index) {
|
|
|
|
if (index != pulsein_tc_index) return;
|
|
|
|
overflow_count++;
|
|
|
|
Tc* tc = tc_insts[index];
|
|
|
|
if (!tc->COUNT16.INTFLAG.bit.OVF) return;
|
|
|
|
|
|
|
|
// Clear the interrupt bit.
|
|
|
|
tc->COUNT16.INTFLAG.reg = TC_INTFLAG_OVF;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
static void pulsein_set_config(pulseio_pulsein_obj_t* self, bool first_edge) {
|
2018-05-29 21:21:19 -04:00
|
|
|
uint32_t sense_setting;
|
2017-03-13 16:55:11 -04:00
|
|
|
if (!first_edge) {
|
2018-05-29 21:21:19 -04:00
|
|
|
sense_setting = EIC_CONFIG_SENSE0_BOTH_Val;
|
|
|
|
configure_eic_channel(self->channel, sense_setting);
|
|
|
|
return;
|
2017-03-13 16:55:11 -04:00
|
|
|
} else if (self->idle_state) {
|
2018-05-29 21:21:19 -04:00
|
|
|
sense_setting = EIC_CONFIG_SENSE0_FALL_Val;
|
2017-03-13 16:55:11 -04:00
|
|
|
} else {
|
2018-05-29 21:21:19 -04:00
|
|
|
sense_setting = EIC_CONFIG_SENSE0_RISE_Val;
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
2019-04-04 16:04:11 -04:00
|
|
|
set_eic_handler(self->channel, EIC_HANDLER_PULSEIN);
|
|
|
|
turn_on_eic_channel(self->channel, sense_setting);
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
|
2018-05-29 21:21:19 -04:00
|
|
|
void pulsein_interrupt_handler(uint8_t channel) {
|
2017-03-13 16:55:11 -04:00
|
|
|
// Grab the current time first.
|
2020-03-13 20:21:15 -04:00
|
|
|
uint32_t current_overflow = overflow_count;
|
|
|
|
Tc* tc = tc_insts[pulsein_tc_index];
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2020-04-03 21:07:56 -04:00
|
|
|
tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_READSYNC;
|
|
|
|
while (tc->COUNT16.SYNCBUSY.bit.COUNT == 1 ||
|
|
|
|
tc->COUNT16.CTRLBSET.bit.CMD == TC_CTRLBSET_CMD_READSYNC_Val) {}
|
|
|
|
#endif
|
2020-03-13 20:21:15 -04:00
|
|
|
uint32_t current_count = tc->COUNT16.COUNT.reg;
|
2018-06-05 15:38:31 -04:00
|
|
|
|
2018-05-29 21:21:19 -04:00
|
|
|
pulseio_pulsein_obj_t* self = get_eic_channel_data(channel);
|
2018-06-05 15:38:31 -04:00
|
|
|
if (!background_tasks_ok() || self->errored_too_fast) {
|
|
|
|
self->errored_too_fast = true;
|
|
|
|
common_hal_pulseio_pulsein_pause(self);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-14 19:59:04 -05:00
|
|
|
if (self->first_edge) {
|
|
|
|
self->first_edge = false;
|
|
|
|
pulsein_set_config(self, false);
|
|
|
|
} else {
|
2020-04-02 20:35:52 -04:00
|
|
|
// Sometimes we beat the overflow interrupt so just fudge overflow in
|
|
|
|
// that case.
|
|
|
|
if (current_count < self->last_count && current_overflow == self->last_overflow) {
|
|
|
|
current_overflow += 1;
|
|
|
|
}
|
2020-03-13 20:21:15 -04:00
|
|
|
uint32_t total_diff = current_count + 0xffff * (current_overflow - self->last_overflow) - self->last_count;
|
2020-04-08 13:49:46 -04:00
|
|
|
// The SAMD21 clock is 48MHz. We prescale it to 3MHz so // 3 here.
|
2020-03-13 20:21:15 -04:00
|
|
|
#ifdef SAMD21
|
|
|
|
total_diff /= 3;
|
|
|
|
#endif
|
|
|
|
// Cap duration at 16 bits.
|
2018-02-14 19:59:04 -05:00
|
|
|
uint16_t duration = 0xffff;
|
|
|
|
if (total_diff < duration) {
|
|
|
|
duration = total_diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t i = (self->start + self->len) % self->maxlen;
|
|
|
|
self->buffer[i] = duration;
|
|
|
|
if (self->len < self->maxlen) {
|
|
|
|
self->len++;
|
|
|
|
} else {
|
|
|
|
self->start++;
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 20:21:15 -04:00
|
|
|
self->last_overflow = current_overflow;
|
|
|
|
self->last_count = current_count;
|
2018-02-14 19:59:04 -05:00
|
|
|
}
|
2017-03-13 16:55:11 -04:00
|
|
|
|
2020-04-03 21:07:56 -04:00
|
|
|
void pulsein_reset() {
|
2020-07-04 22:15:38 -04:00
|
|
|
#ifdef SAMD21
|
|
|
|
rtc_end_pulsein();
|
|
|
|
#endif
|
2020-04-03 21:07:56 -04:00
|
|
|
refcount = 0;
|
|
|
|
pulsein_tc_index = 0xff;
|
|
|
|
overflow_count = 0;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
|
2017-03-13 16:55:11 -04:00
|
|
|
const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) {
|
|
|
|
if (!pin->has_extint) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_RuntimeError(translate("No hardware support on pin"));
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
2018-05-29 21:21:19 -04:00
|
|
|
if (eic_get_enable() && !eic_channel_free(pin->extint_channel)) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_RuntimeError(translate("EXTINT channel already in use"));
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 03:22:29 -05:00
|
|
|
self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false);
|
2017-03-13 16:55:11 -04:00
|
|
|
if (self->buffer == NULL) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t));
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
self->channel = pin->extint_channel;
|
2018-07-31 17:01:01 -04:00
|
|
|
self->pin = pin->number;
|
2017-03-13 16:55:11 -04:00
|
|
|
self->maxlen = maxlen;
|
|
|
|
self->idle_state = idle_state;
|
|
|
|
self->start = 0;
|
|
|
|
self->len = 0;
|
|
|
|
self->first_edge = true;
|
2018-06-08 17:57:20 -04:00
|
|
|
self->errored_too_fast = false;
|
2017-03-13 16:55:11 -04:00
|
|
|
|
2020-03-13 20:21:15 -04:00
|
|
|
if (refcount == 0) {
|
|
|
|
// Find a spare timer.
|
|
|
|
Tc *tc = NULL;
|
|
|
|
int8_t index = TC_INST_NUM - 1;
|
|
|
|
for (; index >= 0; index--) {
|
|
|
|
if (tc_insts[index]->COUNT16.CTRLA.bit.ENABLE == 0) {
|
|
|
|
tc = tc_insts[index];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tc == NULL) {
|
|
|
|
mp_raise_RuntimeError(translate("All timers in use"));
|
|
|
|
}
|
|
|
|
|
|
|
|
pulsein_tc_index = index;
|
|
|
|
|
|
|
|
set_timer_handler(true, index, TC_HANDLER_PULSEIN);
|
|
|
|
#ifdef SAMD21
|
2020-04-08 13:49:46 -04:00
|
|
|
// We use GCLK0 for SAMD21 which is 48MHz. We prescale it to 3MHz.
|
2020-03-13 20:21:15 -04:00
|
|
|
turn_on_clocks(true, index, 0);
|
|
|
|
#endif
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2020-04-08 13:49:46 -04:00
|
|
|
// We use GCLK5 for SAMD51 because it runs at 2MHz and we can use it for a 1MHz clock,
|
2020-03-13 20:21:15 -04:00
|
|
|
// 1us per tick.
|
|
|
|
turn_on_clocks(true, index, 5);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SAMD21
|
|
|
|
tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 |
|
|
|
|
TC_CTRLA_PRESCALER_DIV16 |
|
|
|
|
TC_CTRLA_WAVEGEN_NFRQ;
|
|
|
|
#endif
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2020-03-13 20:21:15 -04:00
|
|
|
tc_reset(tc);
|
|
|
|
tc_set_enable(tc, false);
|
|
|
|
tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 | TC_CTRLA_PRESCALER_DIV2;
|
|
|
|
tc->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_NFRQ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tc_set_enable(tc, true);
|
2020-04-02 20:35:52 -04:00
|
|
|
|
|
|
|
// Clear our interrupt in case it was set earlier
|
|
|
|
tc->COUNT16.INTFLAG.reg = TC_INTFLAG_OVF;
|
|
|
|
tc->COUNT16.INTENSET.reg = TC_INTENSET_OVF;
|
|
|
|
tc_enable_interrupts(pulsein_tc_index);
|
2020-03-13 20:21:15 -04:00
|
|
|
tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_RETRIGGER;
|
|
|
|
|
|
|
|
overflow_count = 0;
|
|
|
|
}
|
|
|
|
refcount++;
|
2020-04-02 20:35:52 -04:00
|
|
|
self->last_overflow = overflow_count;
|
|
|
|
self->last_count = 0;
|
2020-03-13 20:21:15 -04:00
|
|
|
|
2018-05-29 21:21:19 -04:00
|
|
|
set_eic_channel_data(pin->extint_channel, (void*) self);
|
2017-03-13 16:55:11 -04:00
|
|
|
|
2018-02-14 19:59:04 -05:00
|
|
|
// Check to see if the EIC is enabled and start it up if its not.'
|
2018-02-15 19:04:39 -05:00
|
|
|
if (eic_get_enable() == 0) {
|
2018-05-29 21:21:19 -04:00
|
|
|
turn_on_external_interrupt_controller();
|
2018-02-14 19:59:04 -05:00
|
|
|
}
|
|
|
|
|
2018-07-31 17:01:01 -04:00
|
|
|
gpio_set_pin_function(pin->number, GPIO_PIN_FUNCTION_A);
|
2018-02-14 19:59:04 -05:00
|
|
|
|
2018-05-29 21:21:19 -04:00
|
|
|
turn_on_cpu_interrupt(self->channel);
|
2018-02-14 19:59:04 -05:00
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
claim_pin(pin);
|
|
|
|
|
2018-02-14 19:59:04 -05:00
|
|
|
// Set config will enable the EIC.
|
2017-03-13 16:55:11 -04:00
|
|
|
pulsein_set_config(self, true);
|
2020-07-04 22:15:38 -04:00
|
|
|
#ifdef SAMD21
|
|
|
|
rtc_start_pulsein();
|
|
|
|
#endif
|
|
|
|
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
|
2017-10-02 20:49:40 -04:00
|
|
|
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
|
|
|
|
return self->pin == NO_PIN;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
|
2017-10-02 20:49:40 -04:00
|
|
|
if (common_hal_pulseio_pulsein_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-04 22:15:38 -04:00
|
|
|
#ifdef SAMD21
|
|
|
|
rtc_end_pulsein();
|
|
|
|
#endif
|
2019-04-04 16:04:11 -04:00
|
|
|
set_eic_handler(self->channel, EIC_HANDLER_NO_INTERRUPT);
|
2018-05-29 21:21:19 -04:00
|
|
|
turn_off_eic_channel(self->channel);
|
2018-08-31 17:46:03 -04:00
|
|
|
reset_pin_number(self->pin);
|
2020-03-13 20:21:15 -04:00
|
|
|
|
|
|
|
refcount--;
|
|
|
|
if (refcount == 0) {
|
|
|
|
tc_reset(tc_insts[pulsein_tc_index]);
|
|
|
|
pulsein_tc_index = 0xff;
|
|
|
|
}
|
2017-10-02 20:49:40 -04:00
|
|
|
self->pin = NO_PIN;
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
|
2018-02-14 19:59:04 -05:00
|
|
|
uint32_t mask = 1 << self->channel;
|
|
|
|
EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos;
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
|
2017-03-13 16:55:11 -04:00
|
|
|
uint16_t trigger_duration) {
|
2018-02-15 18:12:58 -05:00
|
|
|
// Make sure we're paused.
|
|
|
|
common_hal_pulseio_pulsein_pause(self);
|
|
|
|
|
2018-06-05 15:38:31 -04:00
|
|
|
// Reset erroring
|
|
|
|
self->errored_too_fast = false;
|
|
|
|
|
2017-03-13 16:55:11 -04:00
|
|
|
// Send the trigger pulse.
|
|
|
|
if (trigger_duration > 0) {
|
2018-02-09 03:22:29 -05:00
|
|
|
gpio_set_pin_pull_mode(self->pin, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_direction(self->pin, GPIO_DIRECTION_OUT);
|
|
|
|
gpio_set_pin_level(self->pin, !self->idle_state);
|
2018-04-16 08:11:14 -04:00
|
|
|
common_hal_mcu_delay_us((uint32_t)trigger_duration);
|
2018-02-09 03:22:29 -05:00
|
|
|
gpio_set_pin_level(self->pin, self->idle_state);
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reconfigure the pin and make sure its set to detect the first edge.
|
|
|
|
self->first_edge = true;
|
2020-03-13 20:21:15 -04:00
|
|
|
self->last_overflow = 0;
|
|
|
|
self->last_count = 0;
|
2018-02-14 19:59:04 -05:00
|
|
|
gpio_set_pin_function(self->pin, GPIO_PIN_FUNCTION_A);
|
|
|
|
uint32_t mask = 1 << self->channel;
|
|
|
|
// Clear previous interrupt state and re-enable it.
|
|
|
|
EIC->INTFLAG.reg = mask << EIC_INTFLAG_EXTINT_Pos;
|
|
|
|
EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos;
|
|
|
|
|
2017-03-13 16:55:11 -04:00
|
|
|
pulsein_set_config(self, true);
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
|
2017-03-13 16:55:11 -04:00
|
|
|
common_hal_mcu_disable_interrupts();
|
|
|
|
self->start = 0;
|
|
|
|
self->len = 0;
|
|
|
|
common_hal_mcu_enable_interrupts();
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
|
2017-03-13 16:55:11 -04:00
|
|
|
if (self->len == 0) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_IndexError(translate("pop from an empty PulseIn"));
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
common_hal_mcu_disable_interrupts();
|
|
|
|
uint16_t value = self->buffer[self->start];
|
|
|
|
self->start = (self->start + 1) % self->maxlen;
|
|
|
|
self->len--;
|
|
|
|
common_hal_mcu_enable_interrupts();
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
|
2017-03-13 16:55:11 -04:00
|
|
|
return self->maxlen;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
|
2017-03-13 16:55:11 -04:00
|
|
|
return self->len;
|
|
|
|
}
|
|
|
|
|
2018-06-05 15:38:31 -04:00
|
|
|
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) {
|
|
|
|
uint32_t mask = 1 << self->channel;
|
|
|
|
return (EIC->INTENSET.reg & (mask << EIC_INTENSET_EXTINT_Pos)) == 0;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self,
|
2017-03-13 16:55:11 -04:00
|
|
|
int16_t index) {
|
|
|
|
common_hal_mcu_disable_interrupts();
|
|
|
|
if (index < 0) {
|
|
|
|
index += self->len;
|
|
|
|
}
|
|
|
|
if (index < 0 || index >= self->len) {
|
|
|
|
common_hal_mcu_enable_interrupts();
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_IndexError(translate("index out of range"));
|
2017-03-13 16:55:11 -04:00
|
|
|
}
|
|
|
|
uint16_t value = self->buffer[(self->start + index) % self->maxlen];
|
|
|
|
common_hal_mcu_enable_interrupts();
|
|
|
|
return value;
|
|
|
|
}
|