From e2a4c76a3745c512eb85ca5555ec35e5f7d52769 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sun, 18 Aug 2019 08:44:10 -0400 Subject: make nrf touchio be generic: now available for SAMD51 too --- .../boards/capablerobot_usbhub/mpconfigboard.mk | 2 - ports/atmel-samd/boards/cp32-m4/mpconfigboard.mk | 2 - .../boards/datalore_ip_m4/mpconfigboard.mk | 2 - .../boards/feather_m4_express/mpconfigboard.mk | 2 - .../grandcentral_m4_express/mpconfigboard.mk | 2 - .../boards/itsybitsy_m4_express/mpconfigboard.mk | 2 - .../boards/kicksat-sprite/mpconfigboard.mk | 2 - .../boards/metro_m4_airlift_lite/mpconfigboard.mk | 2 - .../boards/metro_m4_express/mpconfigboard.mk | 2 - .../atmel-samd/boards/mini_sam_m4/mpconfigboard.mk | 2 - ports/atmel-samd/boards/pybadge/mpconfigboard.mk | 2 - .../boards/pybadge_airlift/mpconfigboard.mk | 2 - ports/atmel-samd/boards/pygamer/mpconfigboard.mk | 2 - .../boards/pygamer_advance/mpconfigboard.mk | 2 - ports/atmel-samd/boards/pyportal/mpconfigboard.mk | 3 - .../boards/pyportal_titano/mpconfigboard.mk | 3 - .../boards/robohatmm1_m4/mpconfigboard.mk | 3 - ports/atmel-samd/boards/sam32/mpconfigboard.mk | 3 - .../boards/trellis_m4_express/mpconfigboard.mk | 2 - ports/atmel-samd/common-hal/touchio/TouchIn.c | 7 +- ports/atmel-samd/common-hal/touchio/TouchIn.h | 5 + ports/atmel-samd/mpconfigport.mk | 2 + ports/atmel-samd/supervisor/port.c | 6 +- ports/nrf/common-hal/touchio/TouchIn.c | 116 --------------------- ports/nrf/common-hal/touchio/TouchIn.h | 43 -------- ports/nrf/common-hal/touchio/__init__.c | 1 - py/circuitpy_defns.mk | 35 ++++--- py/circuitpy_mpconfig.mk | 12 ++- shared-bindings/touchio/TouchIn.h | 5 + shared-module/touchio/TouchIn.c | 110 +++++++++++++++++++ shared-module/touchio/TouchIn.h | 44 ++++++++ shared-module/touchio/__init__.c | 1 + 32 files changed, 208 insertions(+), 221 deletions(-) delete mode 100644 ports/nrf/common-hal/touchio/TouchIn.c delete mode 100644 ports/nrf/common-hal/touchio/TouchIn.h delete mode 100644 ports/nrf/common-hal/touchio/__init__.c create mode 100644 shared-module/touchio/TouchIn.c create mode 100644 shared-module/touchio/TouchIn.h create mode 100644 shared-module/touchio/__init__.c diff --git a/ports/atmel-samd/boards/capablerobot_usbhub/mpconfigboard.mk b/ports/atmel-samd/boards/capablerobot_usbhub/mpconfigboard.mk index 97c8eb863..f390a8681 100644 --- a/ports/atmel-samd/boards/capablerobot_usbhub/mpconfigboard.mk +++ b/ports/atmel-samd/boards/capablerobot_usbhub/mpconfigboard.mk @@ -14,5 +14,3 @@ LONGINT_IMPL = MPZ # No I2S on SAMD51G CIRCUITPY_AUDIOBUSIO = 0 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/cp32-m4/mpconfigboard.mk b/ports/atmel-samd/boards/cp32-m4/mpconfigboard.mk index 72a191044..86a7719b8 100644 --- a/ports/atmel-samd/boards/cp32-m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/cp32-m4/mpconfigboard.mk @@ -14,5 +14,3 @@ EXTERNAL_FLASH_DEVICES = "W25Q128JV_PM" CIRCUITPY_PS2IO = 1 # No I2S on SAMD51G. CIRCUITPY_AUDIOBUSIO = 0 -# No touch on SAMD51 yet. -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/datalore_ip_m4/mpconfigboard.mk b/ports/atmel-samd/boards/datalore_ip_m4/mpconfigboard.mk index 794920fe3..aa5f47d40 100644 --- a/ports/atmel-samd/boards/datalore_ip_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/datalore_ip_m4/mpconfigboard.mk @@ -15,5 +15,3 @@ LONGINT_IMPL = MPZ CIRCUITPY_NETWORK = 1 MICROPY_PY_WIZNET5K = 5500 CIRCUITPY_PS2IO = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk index 8169345f5..176ebac82 100644 --- a/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m4_express/mpconfigboard.mk @@ -15,5 +15,3 @@ LONGINT_IMPL = MPZ CIRCUITPY_NETWORK = 1 MICROPY_PY_WIZNET5K = 5500 CIRCUITPY_PS2IO = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/grandcentral_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/grandcentral_m4_express/mpconfigboard.mk index 13631a736..423761c3f 100644 --- a/ports/atmel-samd/boards/grandcentral_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/grandcentral_m4_express/mpconfigboard.mk @@ -13,5 +13,3 @@ EXTERNAL_FLASH_DEVICES = "W25Q64JV_IQ, GD25Q64C" LONGINT_IMPL = MPZ CIRCUITPY_PS2IO = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk index 31bb38e20..f53983ef7 100644 --- a/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.mk @@ -15,5 +15,3 @@ LONGINT_IMPL = MPZ CIRCUITPY_PS2IO = 1 # No I2S on SAMD51G CIRCUITPY_AUDIOBUSIO = 0 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk b/ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk index 5871cca2a..706e551c5 100644 --- a/ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk +++ b/ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk @@ -11,8 +11,6 @@ QSPI_FLASH_FILESYSTEM = 0 INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = MPZ -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 # Not needed. CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_DISPLAYIO = 0 diff --git a/ports/atmel-samd/boards/metro_m4_airlift_lite/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m4_airlift_lite/mpconfigboard.mk index d7d0c1d6e..98d85ba82 100644 --- a/ports/atmel-samd/boards/metro_m4_airlift_lite/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m4_airlift_lite/mpconfigboard.mk @@ -15,5 +15,3 @@ LONGINT_IMPL = MPZ CIRCUITPY_NETWORK = 1 MICROPY_PY_WIZNET5K = 5500 CIRCUITPY_PS2IO = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk index 829bf749f..68b6f6406 100644 --- a/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m4_express/mpconfigboard.mk @@ -15,5 +15,3 @@ LONGINT_IMPL = MPZ CIRCUITPY_NETWORK = 1 MICROPY_PY_WIZNET5K = 5500 CIRCUITPY_PS2IO = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk b/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk index a8f47dced..b70b9bbb0 100644 --- a/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/mini_sam_m4/mpconfigboard.mk @@ -15,5 +15,3 @@ LONGINT_IMPL = MPZ CIRCUITPY_PS2IO = 1 # No I2S on SAMD51G CIRCUITPY_AUDIOBUSIO = 0 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/pybadge/mpconfigboard.mk b/ports/atmel-samd/boards/pybadge/mpconfigboard.mk index b8d10387b..7e2cb6439 100644 --- a/ports/atmel-samd/boards/pybadge/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pybadge/mpconfigboard.mk @@ -17,7 +17,5 @@ CIRCUITPY_DISPLAYIO = 1 CIRCUITPY_GAMEPAD = 1 CIRCUITPY_GAMEPADSHIFT = 1 CIRCUITPY_STAGE = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pybadge diff --git a/ports/atmel-samd/boards/pybadge_airlift/mpconfigboard.mk b/ports/atmel-samd/boards/pybadge_airlift/mpconfigboard.mk index 12683e995..7e77b0fea 100644 --- a/ports/atmel-samd/boards/pybadge_airlift/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pybadge_airlift/mpconfigboard.mk @@ -17,7 +17,5 @@ CIRCUITPY_DISPLAYIO = 1 CIRCUITPY_GAMEPAD = 1 CIRCUITPY_GAMEPADSHIFT = 1 CIRCUITPY_STAGE = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pybadge diff --git a/ports/atmel-samd/boards/pygamer/mpconfigboard.mk b/ports/atmel-samd/boards/pygamer/mpconfigboard.mk index 69fb14358..0fce468d9 100644 --- a/ports/atmel-samd/boards/pygamer/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pygamer/mpconfigboard.mk @@ -17,7 +17,5 @@ CIRCUITPY_DISPLAYIO = 1 CIRCUITPY_GAMEPAD = 1 CIRCUITPY_GAMEPADSHIFT = 1 CIRCUITPY_STAGE = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pygamer diff --git a/ports/atmel-samd/boards/pygamer_advance/mpconfigboard.mk b/ports/atmel-samd/boards/pygamer_advance/mpconfigboard.mk index 1bb2c553d..8711738d4 100644 --- a/ports/atmel-samd/boards/pygamer_advance/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pygamer_advance/mpconfigboard.mk @@ -17,7 +17,5 @@ CIRCUITPY_DISPLAYIO = 1 CIRCUITPY_GAMEPAD = 1 CIRCUITPY_GAMEPADSHIFT = 1 CIRCUITPY_STAGE = 1 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pybadge diff --git a/ports/atmel-samd/boards/pyportal/mpconfigboard.mk b/ports/atmel-samd/boards/pyportal/mpconfigboard.mk index 996b17b3b..f763c74fa 100644 --- a/ports/atmel-samd/boards/pyportal/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pyportal/mpconfigboard.mk @@ -11,6 +11,3 @@ QSPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICE_COUNT = 2 EXTERNAL_FLASH_DEVICES = "W25Q64JV_IQ, GD25Q64C" LONGINT_IMPL = MPZ - -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/pyportal_titano/mpconfigboard.mk b/ports/atmel-samd/boards/pyportal_titano/mpconfigboard.mk index 0ca438311..58b363779 100644 --- a/ports/atmel-samd/boards/pyportal_titano/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pyportal_titano/mpconfigboard.mk @@ -11,6 +11,3 @@ QSPI_FLASH_FILESYSTEM = 1 EXTERNAL_FLASH_DEVICE_COUNT = 2 EXTERNAL_FLASH_DEVICES = "W25Q64JV_IQ, GD25Q64C" LONGINT_IMPL = MPZ - -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/robohatmm1_m4/mpconfigboard.mk b/ports/atmel-samd/boards/robohatmm1_m4/mpconfigboard.mk index afd15ad0a..f391bf625 100644 --- a/ports/atmel-samd/boards/robohatmm1_m4/mpconfigboard.mk +++ b/ports/atmel-samd/boards/robohatmm1_m4/mpconfigboard.mk @@ -17,8 +17,6 @@ LONGINT_IMPL = MPZ CIRCUITPY_PS2IO = 1 # No I2S on SAMD51G CIRCUITPY_AUDIOBUSIO = 0 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 # Make room for more stuff CIRCUITPY_DISPLAYIO = 0 CIRCUITPY_FREQUENCYIO = 0 @@ -28,4 +26,3 @@ FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel #FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_INA219 #FROZEN_MPY_DIRS += $(TOP)/frozen/RoboticsMasters_CircuitPython_MPU9250 - diff --git a/ports/atmel-samd/boards/sam32/mpconfigboard.mk b/ports/atmel-samd/boards/sam32/mpconfigboard.mk index ca1e11f01..12884a9cc 100644 --- a/ports/atmel-samd/boards/sam32/mpconfigboard.mk +++ b/ports/atmel-samd/boards/sam32/mpconfigboard.mk @@ -9,6 +9,3 @@ CHIP_FAMILY = samd51 INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = MPZ - -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/boards/trellis_m4_express/mpconfigboard.mk b/ports/atmel-samd/boards/trellis_m4_express/mpconfigboard.mk index 6d108e649..f1fb49901 100644 --- a/ports/atmel-samd/boards/trellis_m4_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/trellis_m4_express/mpconfigboard.mk @@ -15,5 +15,3 @@ LONGINT_IMPL = MPZ CIRCUITPY_PS2IO = 1 # No I2S on SAMD51G CIRCUITPY_AUDIOBUSIO = 0 -# No touch on SAMD51 yet -CIRCUITPY_TOUCHIO = 0 diff --git a/ports/atmel-samd/common-hal/touchio/TouchIn.c b/ports/atmel-samd/common-hal/touchio/TouchIn.c index 9fb3a2ea1..0658c1d05 100644 --- a/ports/atmel-samd/common-hal/touchio/TouchIn.c +++ b/ports/atmel-samd/common-hal/touchio/TouchIn.c @@ -34,9 +34,10 @@ #include "shared-bindings/touchio/TouchIn.h" #include "supervisor/shared/translate.h" +// Native touchio only exists for SAMD21 #ifdef SAMD21 + #include "hpl/pm/hpl_pm_base.h" -#endif #include "samd/clocks.h" #include "samd/pins.h" @@ -67,7 +68,6 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, claim_pin(pin); // Turn on the PTC if its not in use. We won't turn it off until reset. - #ifdef SAMD21 if ((( Ptc *) PTC)->CTRLA.bit.ENABLE == 0) { // We run the PTC at 8mhz so divide the 48mhz clock by 6. uint8_t gclk = find_free_gclk(6); @@ -96,7 +96,6 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, // but for touches using fruit or other objects, the difference is much less. self->threshold = get_raw_reading(self) + 100; - #endif } bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) { @@ -141,3 +140,5 @@ uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) { void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold) { self->threshold = new_threshold; } + +#endif // SAMD21 diff --git a/ports/atmel-samd/common-hal/touchio/TouchIn.h b/ports/atmel-samd/common-hal/touchio/TouchIn.h index ffe8a6c01..85550b8a3 100644 --- a/ports/atmel-samd/common-hal/touchio/TouchIn.h +++ b/ports/atmel-samd/common-hal/touchio/TouchIn.h @@ -27,6 +27,9 @@ #ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_TOUCHIO_TOUCHIN_H #define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_TOUCHIO_TOUCHIN_H +// Native touchio only exists for SAMD21 +#ifdef SAMD21 + #include "common-hal/microcontroller/Pin.h" #include "samd21_ptc_component.h" @@ -42,4 +45,6 @@ typedef struct { void touchin_reset(void); +#endif // SAMD21 + #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_TOUCHIO_TOUCHIN_H diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index 4ddd097f0..af33edb0e 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -18,11 +18,13 @@ endif ifeq ($(CHIP_FAMILY),samd21) # frequencyio not yet verified as working on SAMD21. CIRCUITPY_FREQUENCYIO = 0 +CIRCUITPY_TOUCHIO_USE_NATIVE = 1 endif # Put samd51-only choices here. ifeq ($(CHIP_FAMILY),samd51) CIRCUITPY_SAMD = 1 +CIRCUITPY_TOUCHIO_USE_NATIVE = 0 endif INTERNAL_LIBM = 1 diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 37360deb0..512fd8eb8 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -54,7 +54,11 @@ #include "common-hal/pulseio/PWMOut.h" #include "common-hal/ps2io/Ps2.h" #include "common-hal/rtc/RTC.h" + +#if CIRCUITPY_TOUCHIO_USE_NATIVE #include "common-hal/touchio/TouchIn.h" +#endif + #include "samd/cache.h" #include "samd/clocks.h" #include "samd/events.h" @@ -211,7 +215,7 @@ void reset_port(void) { //pdmin_reset(); #endif -#if CIRCUITPY_TOUCHIO +#if CIRCUITPY_TOUCHIO && CIRCUITPY_TOUCHIO_USE_NATIVE touchin_reset(); #endif eic_reset(); diff --git a/ports/nrf/common-hal/touchio/TouchIn.c b/ports/nrf/common-hal/touchio/TouchIn.c deleted file mode 100644 index ba50b17b0..000000000 --- a/ports/nrf/common-hal/touchio/TouchIn.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries - * Copyright (c) 2018 Nick Moore 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 - -#include "py/nlr.h" -#include "py/mperrno.h" -#include "py/runtime.h" -#include "py/binary.h" -#include "py/mphal.h" -#include "shared-bindings/touchio/TouchIn.h" -#include "supervisor/shared/translate.h" - -#include "nrf.h" - -// This is a capacitive touch sensing routine using a single digital -// pin. The pin should be connected to the sensing pad, and to ground -// via a 1Mohm or thereabout drain resistor. When a reading is taken, -// the pin's capacitance is charged by setting it to a digital output -// 'high' for a few microseconds, and then it is changed to a high -// impedance input. We measure how long it takes to discharge through -// the resistor (around 50us), using a busy-waiting loop, and average -// over N_SAMPLES cycles to reduce the effects of noise. - -#define N_SAMPLES 10 -#define TIMEOUT_TICKS 10000 - -static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { - - uint16_t ticks = 0; - - for (uint16_t i = 0; i < N_SAMPLES; i++) { - // set pad to digital output high for 10us to charge it - - nrf_gpio_cfg_output(self->pin->number); - nrf_gpio_pin_set(self->pin->number); - mp_hal_delay_us(10); - - // set pad back to an input and take some samples - - nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL); - - while(nrf_gpio_pin_read(self->pin->number)) { - if (ticks >= TIMEOUT_TICKS) return TIMEOUT_TICKS; - ticks++; - } - } - return ticks; -} - -void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, - const mcu_pin_obj_t *pin) { - self->pin = pin; - claim_pin(pin); - - self->threshold = get_raw_reading(self) * 1.05 + 100; -} - -bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) { - return self->pin == NULL; -} - -void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) { - if (common_hal_touchio_touchin_deinited(self)) { - return; - } - - reset_pin_number(self->pin->number); - self->pin = NULL; -} - -void touchin_reset() { -} - -bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) { - uint16_t reading = get_raw_reading(self); - return reading > self->threshold; -} - -uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) { - return get_raw_reading(self); -} - -uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) { - return self->threshold; -} - -void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, - uint16_t new_threshold) { - self->threshold = new_threshold; -} diff --git a/ports/nrf/common-hal/touchio/TouchIn.h b/ports/nrf/common-hal/touchio/TouchIn.h deleted file mode 100644 index 121580eac..000000000 --- a/ports/nrf/common-hal/touchio/TouchIn.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * Copyright (c) 2018 Nick Moore 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_NRF_COMMON_HAL_TOUCHIO_TOUCHIN_H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_TOUCHIO_TOUCHIN_H - -#include "common-hal/microcontroller/Pin.h" - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t *pin; - uint16_t threshold; -} touchio_touchin_obj_t; - -void touchin_reset(void); - -#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_TOUCHIO_TOUCHIN_H diff --git a/ports/nrf/common-hal/touchio/__init__.c b/ports/nrf/common-hal/touchio/__init__.c deleted file mode 100644 index d2290447c..000000000 --- a/ports/nrf/common-hal/touchio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No touchio module functions. diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index d4733e338..95939de2b 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -217,9 +217,8 @@ ifeq ($(CIRCUITPY_PEW),1) SRC_PATTERNS += _pew/% endif -# All possible sources are listed here, and are filtered by SRC_PATTERNS. -SRC_COMMON_HAL = \ -$(filter $(SRC_PATTERNS), \ +# All possible sources are listed here, and are filtered by SRC_PATTERNS in SRC_COMMON_HAL +SRC_COMMON_HAL_ALL = \ analogio/AnalogIn.c \ analogio/AnalogOut.c \ analogio/__init__.c \ @@ -272,10 +271,9 @@ $(filter $(SRC_PATTERNS), \ rtc/__init__.c \ supervisor/Runtime.c \ supervisor/__init__.c \ - time/__init__.c \ - touchio/TouchIn.c \ - touchio/__init__.c \ -) + time/__init__.c + +SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL)) # These don't have corresponding files in each port but are still located in # shared-bindings to make it clear what the contents of the modules are. @@ -298,9 +296,7 @@ SRC_BINDINGS_ENUMS += \ help.c \ util.c -# All possible sources are listed here, and are filtered by SRC_PATTERNS. -SRC_SHARED_MODULE = \ -$(filter $(SRC_PATTERNS), \ +SRC_SHARED_MODULE_ALL = \ _pixelbuf/PixelBuf.c \ _pixelbuf/__init__.c \ _stage/Layer.c \ @@ -349,8 +345,23 @@ $(filter $(SRC_PATTERNS), \ uheap/__init__.c \ ustack/__init__.c \ _pew/__init__.c \ - _pew/PewPew.c \ -) + _pew/PewPew.c + +# All possible sources are listed here, and are filtered by SRC_PATTERNS. +SRC_SHARED_MODULE = $(filter $(SRC_PATTERNS), $(SRC_SHARED_MODULE_ALL)) + +# Use the native touchio if requested. This flag is set conditionally in, say, mpconfigport.h. +# The presence of common-hal/touchio/* # does not imply it's available for all chips in a port, +# so there is an explicit flag. For example, SAMD21 touchio is native, but SAMD51 is not. +ifeq ($(CIRCUITPY_TOUCHIO_USE_NATIVE),1) +SRC_COMMON_HAL_ALL += \ + touchio/TouchIn.c \ + touchio/__init__.c +else +SRC_SHARED_MODULE_ALL += \ + touchio/TouchIn.c \ + touchio/__init__.c +endif ifeq ($(INTERNAL_LIBM),1) SRC_LIBM = \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index a408cd7ac..7469fbf3f 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -45,10 +45,10 @@ endif # Use for for early stage or highly restricted ports ifndef CIRCUITPY_DEFAULT_BUILD ifeq ($(CIRCUITPY_MINIMAL_BUILD),1) - CIRCUITPY_FULL_BUILD = 0 - CIRCUITPY_DEFAULT_BUILD = 0 + CIRCUITPY_FULL_BUILD = 0 + CIRCUITPY_DEFAULT_BUILD = 0 else - CIRCUITPY_DEFAULT_BUILD = 1 + CIRCUITPY_DEFAULT_BUILD = 1 endif endif @@ -235,6 +235,12 @@ CIRCUITPY_TIME = $(CIRCUITPY_DEFAULT_BUILD) endif CFLAGS += -DCIRCUITPY_TIME=$(CIRCUITPY_TIME) +# touchio might be native or generic. See circuitpy_defns.mk. +ifndef CIRCUITPY_TOUCHIO_USE_NATIVE +CIRCUITPY_TOUCHIO_USE_NATIVE = 0 +endif +CFLAGS += -DCIRCUITPY_TOUCHIO_USE_NATIVE=$(CIRCUITPY_TOUCHIO_USE_NATIVE) + ifndef CIRCUITPY_TOUCHIO CIRCUITPY_TOUCHIO = $(CIRCUITPY_DEFAULT_BUILD) endif diff --git a/shared-bindings/touchio/TouchIn.h b/shared-bindings/touchio/TouchIn.h index 2e5c51607..e04e79c0b 100644 --- a/shared-bindings/touchio/TouchIn.h +++ b/shared-bindings/touchio/TouchIn.h @@ -28,7 +28,12 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H #include "common-hal/microcontroller/Pin.h" + +#if CIRCUITPY_TOUCHIO_USE_NATIVE #include "common-hal/touchio/TouchIn.h" +#else +#include "shared-module/touchio/TouchIn.h" +#endif extern const mp_obj_type_t touchio_touchin_type; diff --git a/shared-module/touchio/TouchIn.c b/shared-module/touchio/TouchIn.c new file mode 100644 index 000000000..056556c90 --- /dev/null +++ b/shared-module/touchio/TouchIn.c @@ -0,0 +1,110 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2018 Nick Moore 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 + +#include "py/runtime.h" +#include "py/mphal.h" +#include "shared-bindings/touchio/TouchIn.h" + +// This is a capacitive touch sensing routine using a single digital +// pin. The pin should be connected to the sensing pad, and to ground +// via a 1Mohm or thereabout drain resistor. When a reading is taken, +// the pin's capacitance is charged by setting it to a digital output +// 'high' for a few microseconds, and then it is changed to a high +// impedance input. We measure how long it takes to discharge through +// the resistor (around 50us), using a busy-waiting loop, and average +// over N_SAMPLES cycles to reduce the effects of noise. + +#define N_SAMPLES 10 +#define TIMEOUT_TICKS 10000 + +static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { + + uint16_t ticks = 0; + + for (uint16_t i = 0; i < N_SAMPLES; i++) { + // set pad to digital output high for 10us to charge it + + common_hal_digitalio_digitalinout_switch_to_output(self->digitalinout, true, DRIVE_MODE_PUSH_PULL); + mp_hal_delay_us(10); + + // set pad back to an input and take some samples + + common_hal_digitalio_digitalinout_switch_to_input(self->digitalinout, PULL_NONE); + + while(common_hal_digitalio_digitalinout_get_value(self->digitalinout)) { + if (ticks >= TIMEOUT_TICKS) return TIMEOUT_TICKS; + ticks++; + } + } + return ticks; +} + +void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin) { + self->digitalinout = m_new_obj(digitalio_digitalinout_obj_t); + self->digitalinout->base.type = &digitalio_digitalinout_type; + + common_hal_digitalio_digitalinout_construct(self->digitalinout, pin); + + self->threshold = get_raw_reading(self) * 1.05 + 100; +} + +bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) { + return self->digitalinout == MP_OBJ_NULL; +} + +void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) { + if (common_hal_touchio_touchin_deinited(self)) { + return; + } + + common_hal_digitalio_digitalinout_deinit(self->digitalinout); + self->digitalinout = MP_OBJ_NULL; +} + +void touchin_reset() { +} + +bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) { + uint16_t reading = get_raw_reading(self); + return reading > self->threshold; +} + +uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) { + return get_raw_reading(self); +} + +uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) { + return self->threshold; +} + +void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, + uint16_t new_threshold) { + self->threshold = new_threshold; +} diff --git a/shared-module/touchio/TouchIn.h b/shared-module/touchio/TouchIn.h new file mode 100644 index 000000000..8ff6fda8f --- /dev/null +++ b/shared-module/touchio/TouchIn.h @@ -0,0 +1,44 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * Copyright (c) 2018 Nick Moore 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_MODULE_TOUCHIO_TOUCHIN_H +#define MICROPY_INCLUDED_SHARED_MODULE_TOUCHIO_TOUCHIN_H + +#include "common-hal/microcontroller/Pin.h" +#include "shared-bindings/digitalio/DigitalInOut.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + digitalio_digitalinout_obj_t *digitalinout; + uint16_t threshold; +} touchio_touchin_obj_t; + +void touchin_reset(void); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_TOUCHIO_TOUCHIN_H diff --git a/shared-module/touchio/__init__.c b/shared-module/touchio/__init__.c new file mode 100644 index 000000000..d2290447c --- /dev/null +++ b/shared-module/touchio/__init__.c @@ -0,0 +1 @@ +// No touchio module functions. -- cgit v1.2.3