From 7cb54864aa7c651fc65350708afcddfab2a15fcc Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 13 Mar 2017 21:55:11 +0100 Subject: [PATCH] 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 + esp8266/Makefile | 1 + esp8266/common-hal/nativeio/PulseIn.c | 69 +++++ esp8266/common-hal/nativeio/PulseOut.c | 1 - esp8266/common-hal/nativeio/types.h | 4 + shared-bindings/nativeio/PulseIn.c | 285 ++++++++++++++++++ shared-bindings/nativeio/PulseIn.h | 46 +++ shared-bindings/nativeio/PulseOut.c | 2 +- shared-bindings/nativeio/__init__.c | 5 +- 17 files changed, 802 insertions(+), 64 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 create mode 100644 esp8266/common-hal/nativeio/PulseIn.c create mode 100644 shared-bindings/nativeio/PulseIn.c create mode 100644 shared-bindings/nativeio/PulseIn.h diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 5db6734f0313..93704eb0867c 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 000000000000..abcbdb84dd07 --- /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 96aea6a0730e..d8fdc4c2ee4b 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 000000000000..f5df2e8cdbbd --- /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 000000000000..9d6169414b16 --- /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 ab999cfc6cf0..5b26e86f6261 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 754509b96981..6be3197e4670 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 d779f7fdc3f2..84893e3e225a 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 68e099ba06d0..401d77652558 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__ diff --git a/esp8266/Makefile b/esp8266/Makefile index 8b522a0aa6fe..ec2475439001 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -114,6 +114,7 @@ SRC_COMMON_HAL = \ nativeio/AnalogOut.c \ nativeio/DigitalInOut.c \ nativeio/I2C.c \ + nativeio/PulseIn.c \ nativeio/PulseOut.c \ nativeio/PWMOut.c \ nativeio/SPI.c \ diff --git a/esp8266/common-hal/nativeio/PulseIn.c b/esp8266/common-hal/nativeio/PulseIn.c new file mode 100644 index 000000000000..c4bd5a77c022 --- /dev/null +++ b/esp8266/common-hal/nativeio/PulseIn.c @@ -0,0 +1,69 @@ +/* + * 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 + +#include "mpconfigport.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/nativeio/PulseIn.h" + +void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, + const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { + mp_raise_NotImplementedError(""); +} + +void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) { + +} + +void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) { +} + +void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, + uint16_t trigger_duration) { +} + +void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) { +} + +uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) { + return 0; +} + +uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) { + return 0; +} + +uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) { + return 0; +} + +uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, + int16_t index) { + return 0; +} diff --git a/esp8266/common-hal/nativeio/PulseOut.c b/esp8266/common-hal/nativeio/PulseOut.c index 5961d286e905..d5d1e6366151 100644 --- a/esp8266/common-hal/nativeio/PulseOut.c +++ b/esp8266/common-hal/nativeio/PulseOut.c @@ -32,7 +32,6 @@ void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, const nativeio_pwmout_obj_t* carrier) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut.")); } diff --git a/esp8266/common-hal/nativeio/types.h b/esp8266/common-hal/nativeio/types.h index 6a205e12f571..bc8a3bdc187d 100644 --- a/esp8266/common-hal/nativeio/types.h +++ b/esp8266/common-hal/nativeio/types.h @@ -61,6 +61,10 @@ typedef struct { bool locked; } nativeio_spi_obj_t; +typedef struct { + mp_obj_base_t base; +} nativeio_pulsein_obj_t; + typedef struct { mp_obj_base_t base; } nativeio_pulseout_obj_t; diff --git a/shared-bindings/nativeio/PulseIn.c b/shared-bindings/nativeio/PulseIn.c new file mode 100644 index 000000000000..78e0c84e4d58 --- /dev/null +++ b/shared-bindings/nativeio/PulseIn.c @@ -0,0 +1,285 @@ +/* + * 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 + +#include "lib/utils/context_manager_helpers.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/runtime0.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/nativeio/PulseIn.h" + +//| .. currentmodule:: nativeio +//| +//| :class:`PulseIn` -- Read a series of pulse durations +//| ======================================================== +//| +//| PulseIn is used to measure a series of active and idle pulses. This is +//| commonly used in infrared receivers and low cost temperature sensors (DHT). +//| The pulsed signal consists of timed active and idle periods. Unlike PWM, +//| there is no set duration for active and idle pairs. +//| +//| .. class:: PulseIn(pin, maxlen=2, \*, idle_state=False) +//| +//| Create a PulseIn object associated with the given pin. The object acts as +//| a read-only sequence of pulse lengths with a given max length. When it is +//| active, new pulse lengths are added to the end of the list. When there is +//| no more room (len() == `maxlen`) the oldest pulse length is removed to +//| make room. +//| +//| :param ~microcontroller.Pin pin: Pin to read pulses from. +//| :param int maxlen: Maximum number of pulse durations to store at once +//| :param bool idle_state: Idle state of the pin. At start and after `resume` +//| the first recorded pulse will the opposite state from idle. +//| +//| Read a short series of pulses:: +//| +//| import nativeio +//| import board +//| +//| with nativeio.PulseIn(board.D7) as pulses: +//| # Wait for an active pulse +//| while len(pulses) == 0: +//| pass +//| # Pause while we do something with the pulses +//| pulses.pause() +//| +//| # Print the pulses. pulses[0] is an active pulse unless the length +//| # reached max length and idle pulses are recorded. +//| print(pulses) +//| +//| # Clear the rest +//| pulse_in.clear() +//| +//| # Resume with an 80 microsecond active pulse +//| pulse_in.resume(80) +//| +STATIC mp_obj_t nativeio_pulsein_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); + enum { ARG_pin, ARG_maxlen, ARG_idle_state }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_maxlen, MP_ARG_INT, {.u_int = 2} }, + { MP_QSTR_idle_state, MP_ARG_BOOL, {.u_bool = false} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + assert_pin(args[ARG_pin].u_obj, false); + const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); + assert_pin_free(pin); + + nativeio_pulsein_obj_t *self = m_new_obj(nativeio_pulsein_obj_t); + self->base.type = &nativeio_pulsein_type; + + common_hal_nativeio_pulsein_construct(self, pin, args[ARG_maxlen].u_int, + args[ARG_idle_state].u_bool); + + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit() +//| +//| Deinitialises the PulseIn and releases any hardware resources for reuse. +//| +STATIC mp_obj_t nativeio_pulsein_deinit(mp_obj_t self_in) { + nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_nativeio_pulsein_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_deinit_obj, nativeio_pulsein_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. +//| +STATIC mp_obj_t nativeio_pulsein_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_nativeio_pulsein_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulsein___exit___obj, 4, 4, nativeio_pulsein_obj___exit__); + +//| .. method:: pause() +//| +//| Pause pulse capture +//| +STATIC mp_obj_t nativeio_pulsein_obj_pause(mp_obj_t self_in) { + nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + + common_hal_nativeio_pulsein_pause(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_pause_obj, nativeio_pulsein_obj_pause); + +//| .. method:: resume(trigger_duration=0) +//| +//| Resumes pulse capture after an optional trigger pulse. +//| +//| .. warning:: Using trigger pulse with a device that drives both high and +//| low signals risks a short. Make sure your device is open drain (only +//| drives low) when using a trigger pulse. You most likely added a +//| "pull-up" resistor to your circuit to do this. +//| +//| :param int trigger_duration: trigger pulse duration in microseconds +//| +STATIC mp_obj_t nativeio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_obj) { + nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint16_t trigger_duration = 0; + if (MP_OBJ_IS_SMALL_INT(duration_obj)) { + trigger_duration = MP_OBJ_SMALL_INT_VALUE(duration_obj); + } else if (duration_obj != mp_const_none) { + mp_raise_TypeError("trigger_duration must be int"); + } + + common_hal_nativeio_pulsein_resume(self, trigger_duration); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pulsein_resume_obj, nativeio_pulsein_obj_resume); + +//| .. method:: clear() +//| +//| Clears all captured pulses +//| +STATIC mp_obj_t nativeio_pulsein_obj_clear(mp_obj_t self_in) { + nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + + common_hal_nativeio_pulsein_clear(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_clear_obj, nativeio_pulsein_obj_clear); + +//| .. method:: popleft() +//| +//| Removes and returns the oldest read pulse. +//| +STATIC mp_obj_t nativeio_pulsein_obj_popleft(mp_obj_t self_in) { + nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_popleft(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_popleft_obj, nativeio_pulsein_obj_popleft); + +//| .. attribute:: maxlen +//| +//| Returns the maximum length of the PulseIn. When len() is equal to maxlen, +//| it is unclear which pulses are active and which are idle. +//| +STATIC mp_obj_t nativeio_pulsein_obj_get_maxlen(mp_obj_t self_in) { + nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_get_maxlen(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_get_maxlen_obj, nativeio_pulsein_obj_get_maxlen); + +const mp_obj_property_t nativeio_pulsein_maxlen_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&nativeio_pulsein_get_maxlen_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. method:: __len__() +//| +//| Returns the current pulse length +//| +//| This allows you to:: +//| +//| pulses = nativeio.PulseIn(pin) +//| print(len(pulses)) +//| +STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { + nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint16_t len = common_hal_nativeio_pulsein_get_len(self); + switch (op) { + case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); + case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len); + default: return MP_OBJ_NULL; // op not supported + } +} + +//| .. method:: __get__(index) +//| +//| Returns the value at the given index or values in slice. +//| +//| This allows you to:: +//| +//| pulses = nativeio.PulseIn(pin) +//| print(pulses[0]) +//| +STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) { + if (value == mp_const_none) { + // delete item + mp_raise_AttributeError("Cannot delete values"); + } else { + nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { + mp_raise_NotImplementedError("Slices not supported"); + } else { + uint16_t index = 0; + if (MP_OBJ_IS_SMALL_INT(index_obj)) { + index = MP_OBJ_SMALL_INT_VALUE(index_obj); + } else { + mp_raise_TypeError("index must be int"); + } + if (value == MP_OBJ_SENTINEL) { + // load + return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_get_item(self, index)); + } else { + mp_raise_AttributeError("Read-only"); + } + } + } + return mp_const_none; +} + +STATIC const mp_rom_map_elem_t nativeio_pulsein_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pulsein_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pulsein___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&nativeio_pulsein_pause_obj) }, + { MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&nativeio_pulsein_resume_obj) }, + { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&nativeio_pulsein_clear_obj) }, + { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&nativeio_pulsein_popleft_obj) }, + { MP_ROM_QSTR(MP_QSTR_maxlen), MP_ROM_PTR(&nativeio_pulsein_maxlen_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(nativeio_pulsein_locals_dict, nativeio_pulsein_locals_dict_table); + +const mp_obj_type_t nativeio_pulsein_type = { + { &mp_type_type }, + .name = MP_QSTR_PulseIn, + .make_new = nativeio_pulsein_make_new, + .subscr = pulsein_subscr, + .unary_op = pulsein_unary_op, + .locals_dict = (mp_obj_dict_t*)&nativeio_pulsein_locals_dict, +}; diff --git a/shared-bindings/nativeio/PulseIn.h b/shared-bindings/nativeio/PulseIn.h new file mode 100644 index 000000000000..cdb5e95c500e --- /dev/null +++ b/shared-bindings/nativeio/PulseIn.h @@ -0,0 +1,46 @@ +/* + * 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. + */ + +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ + +#include "common-hal/microcontroller/types.h" +#include "common-hal/nativeio/types.h" + +extern const mp_obj_type_t nativeio_pulsein_type; + +extern void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, + const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state); +extern void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self); +extern void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self); +extern void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, uint16_t trigger_duration); +extern void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self); +extern uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self); +extern uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self); +extern uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self); +extern uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, int16_t index); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ diff --git a/shared-bindings/nativeio/PulseOut.c b/shared-bindings/nativeio/PulseOut.c index 013e0851add1..79f189293f9e 100644 --- a/shared-bindings/nativeio/PulseOut.c +++ b/shared-bindings/nativeio/PulseOut.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Damien P. George + * 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 diff --git a/shared-bindings/nativeio/__init__.c b/shared-bindings/nativeio/__init__.c index af3b5e84b871..7395ce31131e 100644 --- a/shared-bindings/nativeio/__init__.c +++ b/shared-bindings/nativeio/__init__.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Scott Shawcroft + * Copyright (c) 2016 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 @@ -39,6 +39,7 @@ #include "shared-bindings/nativeio/AnalogOut.h" #include "shared-bindings/nativeio/DigitalInOut.h" #include "shared-bindings/nativeio/I2C.h" +#include "shared-bindings/nativeio/PulseIn.h" #include "shared-bindings/nativeio/PulseOut.h" #include "shared-bindings/nativeio/PWMOut.h" #include "shared-bindings/nativeio/SPI.h" @@ -75,6 +76,7 @@ //| AnalogOut //| DigitalInOut //| I2C +//| PulseIn //| PulseOut //| PWMOut //| SPI @@ -118,6 +120,7 @@ STATIC const mp_rom_map_elem_t nativeio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&nativeio_analogout_type) }, { MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&nativeio_digitalinout_type) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&nativeio_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_PulseIn), MP_ROM_PTR(&nativeio_pulsein_type) }, { MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&nativeio_pulseout_type) }, { MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&nativeio_pwmout_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&nativeio_spi_type) },