From 7cb54864aa7c651fc65350708afcddfab2a15fcc Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 13 Mar 2017 21:55:11 +0100 Subject: Add PulseIn support which can be used to measure a series of pulse widths. This is useful for infrared input and DHT sensors. --- atmel-samd/Makefile | 3 + atmel-samd/asf_conf/conf_extint.h | 51 ++++++ atmel-samd/common-hal/microcontroller/types.h | 2 + atmel-samd/common-hal/nativeio/PulseIn.c | 220 ++++++++++++++++++++++++++ atmel-samd/common-hal/nativeio/PulseIn.h | 32 ++++ atmel-samd/common-hal/nativeio/types.h | 25 ++- atmel-samd/samd21_pins.c | 116 +++++++------- atmel-samd/tick.c | 2 +- atmel-samd/tick.h | 2 + 9 files changed, 392 insertions(+), 61 deletions(-) create mode 100644 atmel-samd/asf_conf/conf_extint.h create mode 100644 atmel-samd/common-hal/nativeio/PulseIn.c create mode 100644 atmel-samd/common-hal/nativeio/PulseIn.h (limited to 'atmel-samd') diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 5db6734f0..93704eb08 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -150,6 +150,8 @@ LIBS = -larm_cortexM0l_math -lsamd21_qtouch_gcc -lm -lgcc -lc SRC_ASF = $(addprefix asf/sam0/,\ drivers/adc/adc_sam_d_r/adc.c \ drivers/dac/dac_sam_d_c/dac.c \ + drivers/extint/extint_callback.c \ + drivers/extint/extint_sam_d_r/extint.c \ drivers/nvm/nvm.c \ drivers/port/port.c \ drivers/sercom/i2c/i2c_sam0/i2c_master.c \ @@ -224,6 +226,7 @@ SRC_BINDINGS = \ nativeio/AnalogOut.c \ nativeio/DigitalInOut.c \ nativeio/I2C.c \ + nativeio/PulseIn.c \ nativeio/PulseOut.c \ nativeio/PWMOut.c \ nativeio/SPI.c \ diff --git a/atmel-samd/asf_conf/conf_extint.h b/atmel-samd/asf_conf/conf_extint.h new file mode 100644 index 000000000..abcbdb84d --- /dev/null +++ b/atmel-samd/asf_conf/conf_extint.h @@ -0,0 +1,51 @@ +/** + * \file + * + * \brief SAM D21 External Interrupt Driver Configuration Header + * + * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved. + * + * \asf_license_start + * + * \page License + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The name of Atmel may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 4. This software may only be redistributed and used in connection with an + * Atmel microcontroller product. + * + * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE + * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * \asf_license_stop + * + */ +/* + * Support and FAQ: visit Atmel Support + */ +#ifndef CONF_EXTINT_H_INCLUDED +#define CONF_EXTINT_H_INCLUDED + +# define EXTINT_CLOCK_SOURCE GCLK_GENERATOR_0 + +#endif diff --git a/atmel-samd/common-hal/microcontroller/types.h b/atmel-samd/common-hal/microcontroller/types.h index 96aea6a07..d8fdc4c2e 100644 --- a/atmel-samd/common-hal/microcontroller/types.h +++ b/atmel-samd/common-hal/microcontroller/types.h @@ -59,6 +59,8 @@ typedef struct { mp_obj_base_t base; qstr name; uint8_t pin; + bool has_extint:1; + uint8_t extint_channel:7; bool has_adc:1; enum adc_positive_input adc_input:7; bool has_touch:1; diff --git a/atmel-samd/common-hal/nativeio/PulseIn.c b/atmel-samd/common-hal/nativeio/PulseIn.c new file mode 100644 index 000000000..f5df2e8cd --- /dev/null +++ b/atmel-samd/common-hal/nativeio/PulseIn.c @@ -0,0 +1,220 @@ +/* + * This file is part of the Micro Python 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. + */ + +#include "common-hal/nativeio/PulseIn.h" + +#include + +#include "asf/common2/services/delay/delay.h" +#include "asf/sam0/drivers/extint/extint.h" +#include "asf/sam0/drivers/extint/extint_callback.h" + +#include "mpconfigport.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "samd21_pins.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/nativeio/PulseIn.h" + +#include "tick.h" + +static nativeio_pulsein_obj_t *active_pulseins[EIC_NUMBER_OF_INTERRUPTS]; +static uint64_t last_ms[EIC_NUMBER_OF_INTERRUPTS]; +static uint16_t last_us[EIC_NUMBER_OF_INTERRUPTS]; + +void pulsein_reset(void) { + for (int i = 0; i < EIC_NUMBER_OF_INTERRUPTS; i++) { + active_pulseins[i] = NULL; + last_ms[i] = 0; + last_us[i] = 0; + } +} + +static void pulsein_set_config(nativeio_pulsein_obj_t* self, bool first_edge) { + struct extint_chan_conf config; + extint_chan_get_config_defaults(&config); + config.gpio_pin = self->pin; + config.gpio_pin_pull = EXTINT_PULL_NONE; + config.filter_input_signal = true; + + if (!first_edge) { + config.detection_criteria = EXTINT_DETECT_BOTH; + } else if (self->idle_state) { + config.detection_criteria = EXTINT_DETECT_FALLING; + } else { + config.detection_criteria = EXTINT_DETECT_RISING; + } + extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); + extint_chan_set_config(self->channel, &config); + // Clear any interrupts that may have triggered without notifying the CPU. + EIC->INTFLAG.reg |= (1UL << self->channel); + extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); +} + +static void pulsein_callback(void) { + // Grab the current time first. + uint16_t current_us = tc_get_count_value(&ms_timer); + uint64_t current_ms = ticks_ms; + nativeio_pulsein_obj_t* self = active_pulseins[extint_get_current_channel()]; + current_us = current_us * 1000 / self->ticks_per_ms; + if (self->first_edge) { + self->first_edge = false; + pulsein_set_config(self, false); + } else { + uint32_t ms_diff = current_ms - last_ms[self->channel]; + uint16_t us_diff = current_us - last_us[self->channel]; + if (last_us[self->channel] > current_us) { + us_diff = 1000 + current_us - last_us[self->channel]; + } + uint32_t total_diff = us_diff; + if (ms_diff > 1) { + total_diff += (ms_diff - 1) * 1000; + } + 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++; + } + } + last_ms[self->channel] = current_ms; + last_us[self->channel] = current_us; +} + +void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, + const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { + if (!pin->has_extint) { + mp_raise_RuntimeError("No hardware support on pin"); + } + // TODO(tannewt): Switch to checking actual extint peripheral state when other + // classes use extints. + if (active_pulseins[pin->extint_channel] != NULL) { + mp_raise_RuntimeError("EXTINT channel already in use"); + } + + self->buffer = (uint16_t *) gc_alloc(maxlen * sizeof(uint16_t), false); + if (self->buffer == NULL) { + mp_raise_msg_varg(&mp_type_MemoryError, "Failed to allocate RX buffer of %d bytes", maxlen * sizeof(uint16_t)); + } + self->channel = pin->extint_channel; + self->pin = pin->pin; + self->maxlen = maxlen; + self->idle_state = idle_state; + self->start = 0; + self->len = 0; + self->first_edge = true; + self->ticks_per_ms = (system_cpu_clock_get_hz() / 1000 - 1); + + active_pulseins[pin->extint_channel] = self; + + pulsein_set_config(self, true); + extint_register_callback( + pulsein_callback, + self->channel, + EXTINT_CALLBACK_TYPE_DETECT); + extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); +} + +void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) { + extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); + active_pulseins[self->channel] = NULL; + reset_pin(self->pin); +} + +void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) { + extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); +} + +void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, + uint16_t trigger_duration) { + // Send the trigger pulse. + if (trigger_duration > 0) { + struct port_config pin_conf; + port_get_config_defaults(&pin_conf); + + pin_conf.direction = PORT_PIN_DIR_OUTPUT; + pin_conf.input_pull = PORT_PIN_PULL_NONE; + port_pin_set_config(self->pin, &pin_conf); + port_pin_set_output_level(self->pin, !self->idle_state); + delay_us(trigger_duration); + port_pin_set_output_level(self->pin, self->idle_state); + } + + // Reconfigure the pin and make sure its set to detect the first edge. + last_ms[self->channel] = 0; + last_us[self->channel] = 0; + self->first_edge = true; + pulsein_set_config(self, true); +} + +void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) { + common_hal_mcu_disable_interrupts(); + self->start = 0; + self->len = 0; + common_hal_mcu_enable_interrupts(); +} + +uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) { + if (self->len == 0) { + mp_raise_IndexError("pop from an empty PulseIn"); + } + 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; +} + +uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) { + return self->maxlen; +} + +uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) { + return self->len; +} + +uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, + 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(); + mp_raise_IndexError("index out of range"); + } + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; + common_hal_mcu_enable_interrupts(); + return value; +} diff --git a/atmel-samd/common-hal/nativeio/PulseIn.h b/atmel-samd/common-hal/nativeio/PulseIn.h new file mode 100644 index 000000000..9d6169414 --- /dev/null +++ b/atmel-samd/common-hal/nativeio/PulseIn.h @@ -0,0 +1,32 @@ +/* + * 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_NATIVEIO_PULSEIN_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__ + +void pulsein_reset(void); + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__ diff --git a/atmel-samd/common-hal/nativeio/types.h b/atmel-samd/common-hal/nativeio/types.h index ab999cfc6..5b26e86f6 100644 --- a/atmel-samd/common-hal/nativeio/types.h +++ b/atmel-samd/common-hal/nativeio/types.h @@ -88,6 +88,25 @@ typedef struct { uint32_t current_baudrate; } nativeio_spi_obj_t; +typedef struct { + mp_obj_base_t base; + uint8_t channel; + uint8_t pin; + uint16_t* buffer; + uint16_t maxlen; + bool idle_state; + volatile uint16_t start; + volatile uint16_t len; + volatile bool first_edge; + uint16_t ticks_per_ms; +} nativeio_pulsein_obj_t; + +typedef struct { + mp_obj_base_t base; + __IO PORT_PINCFG_Type *pincfg; + uint8_t pin; +} nativeio_pulseout_obj_t; + typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; @@ -99,12 +118,6 @@ typedef struct { }; } nativeio_pwmout_obj_t; -typedef struct { - mp_obj_base_t base; - __IO PORT_PINCFG_Type *pincfg; - uint8_t pin; -} nativeio_pulseout_obj_t; - typedef struct { mp_obj_base_t base; // Only support TouchIn when external SPI flash is used. diff --git a/atmel-samd/samd21_pins.c b/atmel-samd/samd21_pins.c index 754509b96..6be3197e4 100644 --- a/atmel-samd/samd21_pins.c +++ b/atmel-samd/samd21_pins.c @@ -35,6 +35,13 @@ #define NO_TOUCH \ .has_touch = false, +#define EXTINT_CHANNEL(channel) \ + .has_extint = true, \ + .extint_channel = channel, + +#define NO_EXTINT \ + .has_extint = false, + #define ADC_INPUT(input) \ .has_adc = true, \ .adc_input = input, @@ -43,12 +50,13 @@ .has_adc = false, // This macro is used to simplify pin definition in boards//pins.c -#define PIN(p_name, p_adc, p_touch, p_primary_timer, p_secondary_timer, \ - p_primary_sercom, p_secondary_sercom) \ +#define PIN(p_name, p_adc, p_extint, p_touch, p_primary_timer, \ + p_secondary_timer, p_primary_sercom, p_secondary_sercom) \ const mcu_pin_obj_t pin_## p_name = { \ { &mcu_pin_type }, \ .name = MP_QSTR_ ## p_name, \ .pin = (PIN_## p_name), \ + p_extint \ p_adc \ p_touch \ .primary_timer = p_primary_timer, \ @@ -72,161 +80,161 @@ void reset_pin(uint8_t pin) { // NOTE(tannewt): TC wave out 0 is commented out because the first channel is // used to vary the 16 bit timer's frequency. #ifdef PIN_PA00 -PIN(PA00, NO_ADC, NO_TOUCH, +PIN(PA00, EXTINT_CHANNEL(0), NO_ADC, NO_TOUCH, TCC(TCC2, 0, 0), NO_TIMER, NO_SERCOM, SERCOM(SERCOM1, 0)); #endif #ifdef PIN_PA01 -PIN(PA01, NO_ADC, NO_TOUCH, +PIN(PA01, EXTINT_CHANNEL(1), NO_ADC, NO_TOUCH, TCC(TCC2, 1, 1), NO_TIMER, NO_SERCOM, SERCOM(SERCOM1, 1)); #endif #ifdef PIN_PA02 -PIN(PA02, ADC_INPUT(ADC_POSITIVE_INPUT_PIN0), TOUCH(0), +PIN(PA02, EXTINT_CHANNEL(2), ADC_INPUT(ADC_POSITIVE_INPUT_PIN0), TOUCH(0), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PA03 -PIN(PA03, ADC_INPUT(ADC_POSITIVE_INPUT_PIN1), TOUCH(1), +PIN(PA03, EXTINT_CHANNEL(3), ADC_INPUT(ADC_POSITIVE_INPUT_PIN1), TOUCH(1), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB04 -PIN(PB04, ADC_INPUT(ADC_POSITIVE_INPUT_PIN12), TOUCH(10), +PIN(PB04, EXTINT_CHANNEL(4), ADC_INPUT(ADC_POSITIVE_INPUT_PIN12), TOUCH(10), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB05 -PIN(PB05, ADC_INPUT(ADC_POSITIVE_INPUT_PIN13), TOUCH(11), +PIN(PB05, EXTINT_CHANNEL(5), ADC_INPUT(ADC_POSITIVE_INPUT_PIN13), TOUCH(11), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB06 -PIN(PB06, ADC_INPUT(ADC_POSITIVE_INPUT_PIN14), TOUCH(12), +PIN(PB06, EXTINT_CHANNEL(6), ADC_INPUT(ADC_POSITIVE_INPUT_PIN14), TOUCH(12), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB07 -PIN(PB07, TOUCH(ADC_POSITIVE_INPUT_PIN15), TOUCH(13), +PIN(PB07, EXTINT_CHANNEL(7), TOUCH(ADC_POSITIVE_INPUT_PIN15), TOUCH(13), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB08 -PIN(PB08, ADC_INPUT(ADC_POSITIVE_INPUT_PIN2), TOUCH(14), +PIN(PB08, EXTINT_CHANNEL(8), ADC_INPUT(ADC_POSITIVE_INPUT_PIN2), TOUCH(14), NO_TIMER, // TC(TC4, 0, 0), NO_TIMER, NO_SERCOM, SERCOM(SERCOM4, 0)); #endif #ifdef PIN_PB09 -PIN(PB09, ADC_INPUT(ADC_POSITIVE_INPUT_PIN3), TOUCH(15), +PIN(PB09, EXTINT_CHANNEL(9), ADC_INPUT(ADC_POSITIVE_INPUT_PIN3), TOUCH(15), TC(TC4, 1, 1), NO_TIMER, NO_SERCOM, SERCOM(SERCOM4, 1)); #endif #ifdef PIN_PA04 -PIN(PA04, ADC_INPUT(ADC_POSITIVE_INPUT_PIN4), TOUCH(2), +PIN(PA04, EXTINT_CHANNEL(4), ADC_INPUT(ADC_POSITIVE_INPUT_PIN4), TOUCH(2), TCC(TCC0, 0, 0), NO_TIMER, NO_SERCOM, SERCOM(SERCOM0, 0)); #endif #ifdef PIN_PA05 -PIN(PA05, ADC_INPUT(ADC_POSITIVE_INPUT_PIN5), TOUCH(3), +PIN(PA05, EXTINT_CHANNEL(5), ADC_INPUT(ADC_POSITIVE_INPUT_PIN5), TOUCH(3), TCC(TCC0, 1, 1), NO_TIMER, NO_SERCOM, SERCOM(SERCOM0, 1)); #endif #ifdef PIN_PA06 -PIN(PA06, ADC_INPUT(ADC_POSITIVE_INPUT_PIN6), TOUCH(4), +PIN(PA06, EXTINT_CHANNEL(6), ADC_INPUT(ADC_POSITIVE_INPUT_PIN6), TOUCH(4), TCC(TCC1, 0, 0), NO_TIMER, NO_SERCOM, SERCOM(SERCOM0, 2)); #endif #ifdef PIN_PA07 -PIN(PA07, ADC_INPUT(ADC_POSITIVE_INPUT_PIN7), TOUCH(5), +PIN(PA07, EXTINT_CHANNEL(7), ADC_INPUT(ADC_POSITIVE_INPUT_PIN7), TOUCH(5), TCC(TCC1, 1, 1), NO_TIMER, NO_SERCOM, SERCOM(SERCOM0, 3)); #endif #ifdef PIN_PA08 -PIN(PA08, ADC_INPUT(ADC_POSITIVE_INPUT_PIN16), NO_TOUCH, +PIN(PA08, NO_EXTINT, ADC_INPUT(ADC_POSITIVE_INPUT_PIN16), NO_TOUCH, TCC(TCC0, 0, 0), TCC(TCC1, 2, 2), SERCOM(SERCOM0, 0), SERCOM(SERCOM2, 0)); #endif #ifdef PIN_PA09 -PIN(PA09, ADC_INPUT(ADC_POSITIVE_INPUT_PIN17), NO_TOUCH, +PIN(PA09, EXTINT_CHANNEL(9), ADC_INPUT(ADC_POSITIVE_INPUT_PIN17), NO_TOUCH, TCC(TCC0, 1, 1), TCC(TCC1, 3, 3), SERCOM(SERCOM0, 1), SERCOM(SERCOM2, 1)); #endif #ifdef PIN_PA10 -PIN(PA10, ADC_INPUT(ADC_POSITIVE_INPUT_PIN18), NO_TOUCH, +PIN(PA10, EXTINT_CHANNEL(10), ADC_INPUT(ADC_POSITIVE_INPUT_PIN18), NO_TOUCH, TCC(TCC1, 0, 0), TCC(TCC0, 2, 2), SERCOM(SERCOM0, 2), SERCOM(SERCOM2, 2)); #endif #ifdef PIN_PA11 -PIN(PA11, ADC_INPUT(ADC_POSITIVE_INPUT_PIN19), NO_TOUCH, +PIN(PA11, EXTINT_CHANNEL(11), ADC_INPUT(ADC_POSITIVE_INPUT_PIN19), NO_TOUCH, TCC(TCC1, 1, 1), TCC(TCC0, 3, 3), SERCOM(SERCOM0, 3), SERCOM(SERCOM2, 3)); #endif #ifdef PIN_PB10 -PIN(PB10, NO_ADC, NO_TOUCH, +PIN(PB10, EXTINT_CHANNEL(10), NO_ADC, NO_TOUCH, NO_TIMER, // TC(TC5, 0, 0), TCC(TCC0, 0, 4), NO_SERCOM, SERCOM(SERCOM4, 2)); #endif #ifdef PIN_PB11 -PIN(PB11, NO_ADC, NO_TOUCH, +PIN(PB11, EXTINT_CHANNEL(11), NO_ADC, NO_TOUCH, TC(TC5, 1, 1), TCC(TCC0, 1, 5), NO_SERCOM, SERCOM(SERCOM4, 3)); #endif #ifdef PIN_PB12 -PIN(PB12, NO_ADC, NO_TOUCH, +PIN(PB12, EXTINT_CHANNEL(12), NO_ADC, NO_TOUCH, NO_TIMER, // TC(TC4, 0, 0), TCC(TCC0, 2, 6), SERCOM(SERCOM4, 0), NO_SERCOM); #endif #ifdef PIN_PB13 -PIN(PB13, NO_ADC, NO_TOUCH, +PIN(PB13, EXTINT_CHANNEL(13), NO_ADC, NO_TOUCH, TC(TC4, 1, 1), TCC(TCC0, 3, 7), SERCOM(SERCOM4, 1), NO_SERCOM); #endif #ifdef PIN_PB14 -PIN(PB14, NO_ADC, NO_TOUCH, +PIN(PB14, EXTINT_CHANNEL(14), NO_ADC, NO_TOUCH, NO_TIMER, // TC(TC5, 0, 0), NO_TIMER, SERCOM(SERCOM4, 2), @@ -235,28 +243,28 @@ PIN(PB14, NO_ADC, NO_TOUCH, // Second page. #ifdef PIN_PB15 -PIN(PB15, NO_ADC, NO_TOUCH, +PIN(PB15, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, TC(TC5, 1, 1), NO_TIMER, SERCOM(SERCOM4, 3), NO_SERCOM); #endif #ifdef PIN_PA12 -PIN(PA12, NO_ADC, NO_TOUCH, +PIN(PA12, EXTINT_CHANNEL(12), NO_ADC, NO_TOUCH, TCC(TCC2, 0, 0), TCC(TCC0, 2, 6), SERCOM(SERCOM2, 0), SERCOM(SERCOM4, 0)); #endif #ifdef PIN_PA13 -PIN(PA13, NO_ADC, NO_TOUCH, +PIN(PA13, EXTINT_CHANNEL(13), NO_ADC, NO_TOUCH, TCC(TCC2, 1, 1), TCC(TCC0, 3, 7), SERCOM(SERCOM2, 1), SERCOM(SERCOM4, 1)); #endif #ifdef PIN_PA14 -PIN(PA14, NO_ADC, NO_TOUCH, +PIN(PA14, EXTINT_CHANNEL(14), NO_ADC, NO_TOUCH, NO_TIMER, // TC(TC3, 0, 0), TCC(TCC0, 0, 4), SERCOM(SERCOM2, 2), @@ -268,7 +276,7 @@ PIN(PA14, NO_ADC, NO_TOUCH, ); #endif #ifdef PIN_PA15 -PIN(PA15, NO_ADC, NO_TOUCH, +PIN(PA15, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, TC(TC3, 1, 1), TCC(TCC0, 1, 5), SERCOM(SERCOM2, 3), @@ -280,35 +288,35 @@ PIN(PA15, NO_ADC, NO_TOUCH, ); #endif #ifdef PIN_PA16 -PIN(PA16, NO_ADC, NO_TOUCH, +PIN(PA16, EXTINT_CHANNEL(0), NO_ADC, NO_TOUCH, TCC(TCC2, 0, 0), TCC(TCC0, 2, 6), SERCOM(SERCOM1, 0), SERCOM(SERCOM3, 0)); #endif #ifdef PIN_PA17 -PIN(PA17, NO_ADC, NO_TOUCH, +PIN(PA17, EXTINT_CHANNEL(1), NO_ADC, NO_TOUCH, TCC(TCC2, 1, 1), TCC(TCC0, 3, 7), SERCOM(SERCOM1, 1), SERCOM(SERCOM3, 1)); #endif #ifdef PIN_PA18 -PIN(PA18, NO_ADC, NO_TOUCH, +PIN(PA18, EXTINT_CHANNEL(2), NO_ADC, NO_TOUCH, NO_TIMER, // TC(TC3, 0, 0), TCC(TCC0, 2, 2), SERCOM(SERCOM1, 2), SERCOM(SERCOM3, 2)); #endif #ifdef PIN_PA19 -PIN(PA19, NO_ADC, NO_TOUCH, +PIN(PA19, EXTINT_CHANNEL(3), NO_ADC, NO_TOUCH, TC(TC3, 1, 1), TCC(TCC0, 3, 3), SERCOM(SERCOM1, 3), SERCOM(SERCOM3, 3)); #endif #ifdef PIN_PB16 -PIN(PB16, NO_ADC, NO_TOUCH, +PIN(PB16, EXTINT_CHANNEL(0), NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC6_INSTANCE_ NO_TIMER, // TC(TC6, 0, 0), #else @@ -319,7 +327,7 @@ PIN(PB16, NO_ADC, NO_TOUCH, NO_SERCOM); #endif #ifdef PIN_PB17 -PIN(PB17, NO_ADC, NO_TOUCH, +PIN(PB17, EXTINT_CHANNEL(1), NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC6_INSTANCE_ TC(TC6, 1, 1), #else @@ -330,7 +338,7 @@ PIN(PB17, NO_ADC, NO_TOUCH, NO_SERCOM); #endif #ifdef PIN_PA20 -PIN(PA20, NO_ADC, NO_TOUCH, +PIN(PA20, EXTINT_CHANNEL(4), NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC7_INSTANCE_ NO_TIMER, // TC(TC7, 0, 0), #else @@ -341,7 +349,7 @@ PIN(PA20, NO_ADC, NO_TOUCH, SERCOM(SERCOM3, 2)); #endif #ifdef PIN_PA21 -PIN(PA21, NO_ADC, NO_TOUCH, +PIN(PA21, EXTINT_CHANNEL(5), NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 1, 1), #else @@ -352,7 +360,7 @@ PIN(PA21, NO_ADC, NO_TOUCH, SERCOM(SERCOM3, 3)); #endif #ifdef PIN_PA22 -PIN(PA22, NO_ADC, NO_TOUCH, +PIN(PA22, EXTINT_CHANNEL(6), NO_ADC, NO_TOUCH, NO_TIMER, // TC(TC4, 0, 0), TCC(TCC0, 0, 4), SERCOM(SERCOM3, 0), @@ -364,7 +372,7 @@ PIN(PA22, NO_ADC, NO_TOUCH, ); #endif #ifdef PIN_PA23 -PIN(PA23, NO_ADC, NO_TOUCH, +PIN(PA23, EXTINT_CHANNEL(7), NO_ADC, NO_TOUCH, TC(TC4, 1, 1), TCC(TCC0, 1, 5), SERCOM(SERCOM3, 1), @@ -376,7 +384,7 @@ PIN(PA23, NO_ADC, NO_TOUCH, ); #endif #ifdef PIN_PA24 -PIN(PA24, NO_ADC, NO_TOUCH, +PIN(PA24, EXTINT_CHANNEL(12), NO_ADC, NO_TOUCH, NO_TIMER, // TC(TC5, 0, 0), TCC(TCC0, 2, 2), SERCOM(SERCOM3, 2), @@ -388,7 +396,7 @@ PIN(PA24, NO_ADC, NO_TOUCH, ); #endif #ifdef PIN_PA25 -PIN(PA25, NO_ADC, NO_TOUCH, +PIN(PA25, EXTINT_CHANNEL(13), NO_ADC, NO_TOUCH, TC(TC5, 1, 1), TCC(TCC1, 3, 3), SERCOM(SERCOM3, 3), @@ -400,7 +408,7 @@ PIN(PA25, NO_ADC, NO_TOUCH, ); #endif #ifdef PIN_PB22 -PIN(PB22, NO_ADC, NO_TOUCH, +PIN(PB22, EXTINT_CHANNEL(6), NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC7_INSTANCE_ NO_TIMER, // TC(TC7, 0, 0), #else @@ -411,7 +419,7 @@ PIN(PB22, NO_ADC, NO_TOUCH, SERCOM(SERCOM5, 2)); #endif #ifdef PIN_PB23 -PIN(PB23, NO_ADC, NO_TOUCH, +PIN(PB23, EXTINT_CHANNEL(7), NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 1, 1), #else @@ -422,49 +430,49 @@ PIN(PB23, NO_ADC, NO_TOUCH, SERCOM(SERCOM5, 3)); #endif #ifdef PIN_PA27 -PIN(PA27, NO_ADC, NO_TOUCH, +PIN(PA27, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PA28 -PIN(PA28, NO_ADC, NO_TOUCH, +PIN(PA28, EXTINT_CHANNEL(8), NO_ADC, NO_TOUCH, NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PA30 -PIN(PA30, NO_ADC, NO_TOUCH, +PIN(PA30, EXTINT_CHANNEL(10), NO_ADC, NO_TOUCH, TCC(TCC1, 0, 0), NO_TIMER, NO_SERCOM, SERCOM(SERCOM1, 2)); #endif #ifdef PIN_PA31 -PIN(PA31, NO_ADC, NO_TOUCH, +PIN(PA31, EXTINT_CHANNEL(11), NO_ADC, NO_TOUCH, TCC(TCC1, 1, 1), NO_TIMER, NO_SERCOM, SERCOM(SERCOM1, 3)); #endif #ifdef PIN_PB30 -PIN(PB30, NO_ADC, NO_TOUCH, +PIN(PB30, EXTINT_CHANNEL(14), NO_ADC, NO_TOUCH, TCC(TCC0, 0, 0), TCC(TCC1, 2, 2), NO_SERCOM, SERCOM(SERCOM5, 0)); #endif #ifdef PIN_PB31 -PIN(PB31, NO_ADC, NO_TOUCH, +PIN(PB31, EXTINT_CHANNEL(15), NO_ADC, NO_TOUCH, TCC(TCC0, 1, 1), TCC(TCC1, 3, 3), NO_SERCOM, SERCOM(SERCOM5, 1)); #endif #ifdef PIN_PB00 -PIN(PB00, ADC_INPUT(ADC_POSITIVE_INPUT_PIN8), TOUCH(6), +PIN(PB00, EXTINT_CHANNEL(0), ADC_INPUT(ADC_POSITIVE_INPUT_PIN8), TOUCH(6), #ifdef _SAMD21_TC7_INSTANCE_ NO_TIMER, // TC(TC7, 0, 0), #else @@ -475,7 +483,7 @@ PIN(PB00, ADC_INPUT(ADC_POSITIVE_INPUT_PIN8), TOUCH(6), SERCOM(SERCOM5, 2)); #endif #ifdef PIN_PB01 -PIN(PB01, ADC_INPUT(ADC_POSITIVE_INPUT_PIN9), TOUCH(7), +PIN(PB01, EXTINT_CHANNEL(1), ADC_INPUT(ADC_POSITIVE_INPUT_PIN9), TOUCH(7), #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 1, 1), #else @@ -486,7 +494,7 @@ PIN(PB01, ADC_INPUT(ADC_POSITIVE_INPUT_PIN9), TOUCH(7), SERCOM(SERCOM5, 3)); #endif #ifdef PIN_PB02 -PIN(PB02, ADC_INPUT(ADC_POSITIVE_INPUT_PIN10), TOUCH(8), +PIN(PB02, EXTINT_CHANNEL(2), ADC_INPUT(ADC_POSITIVE_INPUT_PIN10), TOUCH(8), #ifdef _SAMD21_TC6_INSTANCE_ NO_TIMER, // TC(TC6, 0, 0), #else @@ -497,7 +505,7 @@ PIN(PB02, ADC_INPUT(ADC_POSITIVE_INPUT_PIN10), TOUCH(8), SERCOM(SERCOM5, 0)); #endif #ifdef PIN_PB03 -PIN(PB03, ADC_INPUT(ADC_POSITIVE_INPUT_PIN11), TOUCH(9), +PIN(PB03, EXTINT_CHANNEL(3), ADC_INPUT(ADC_POSITIVE_INPUT_PIN11), TOUCH(9), #ifdef _SAMD21_TC6_INSTANCE_ TC(TC6, 1, 1), #else diff --git a/atmel-samd/tick.c b/atmel-samd/tick.c index d779f7fdc..84893e3e2 100644 --- a/atmel-samd/tick.c +++ b/atmel-samd/tick.c @@ -7,7 +7,7 @@ // Global millisecond tick count volatile uint64_t ticks_ms = 0; -static struct tc_module ms_timer; +struct tc_module ms_timer; static void ms_tick(struct tc_module *const module_inst) { // SysTick interrupt handler called when the SysTick timer reaches zero diff --git a/atmel-samd/tick.h b/atmel-samd/tick.h index 68e099ba0..401d77652 100644 --- a/atmel-samd/tick.h +++ b/atmel-samd/tick.h @@ -30,6 +30,8 @@ extern volatile uint64_t ticks_ms; +extern struct tc_module ms_timer; + void tick_init(void); #endif // __MICROPY_INCLUDED_ATMEL_SAMD_TICK_H__ -- cgit v1.2.3