From 3972bc19c7c788d5c587156aba39ea659decc420 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 6 Dec 2016 18:39:18 -0800 Subject: [PATCH] atmel-samd: Basic capacitive touch button support. Currently only works on a single channel and is only enabled for boards with SPI flash. Only really designed for hardware testing at this point. --- atmel-samd/Makefile | 14 +- atmel-samd/QTouch/touch_api_ptc.h | 2033 +++++++++++++++++ atmel-samd/asf_conf/conf_clocks.h | 8 +- atmel-samd/boards/cplay_m0_flash/touch.h | 613 +++++ atmel-samd/boards/feather_m0_flash/touch.h | 613 +++++ atmel-samd/boards/metro_m0_flash/touch.h | 613 +++++ atmel-samd/common-hal/microcontroller/types.h | 8 +- atmel-samd/common-hal/nativeio/TouchIn.c | 274 +++ atmel-samd/common-hal/nativeio/TouchInStub.c | 46 + atmel-samd/common-hal/nativeio/types.h | 13 + atmel-samd/main.c | 10 + atmel-samd/samd21_pins.c | 126 +- esp8266/Makefile | 1 + esp8266/common-hal/nativeio/TouchIn.c | 46 + esp8266/common-hal/nativeio/types.h | 4 + shared-bindings/nativeio/TouchIn.c | 141 ++ shared-bindings/nativeio/TouchIn.h | 39 + shared-bindings/nativeio/__init__.c | 2 + 18 files changed, 4539 insertions(+), 65 deletions(-) create mode 100644 atmel-samd/QTouch/touch_api_ptc.h create mode 100644 atmel-samd/boards/cplay_m0_flash/touch.h create mode 100644 atmel-samd/boards/feather_m0_flash/touch.h create mode 100644 atmel-samd/boards/metro_m0_flash/touch.h create mode 100644 atmel-samd/common-hal/nativeio/TouchIn.c create mode 100644 atmel-samd/common-hal/nativeio/TouchInStub.c create mode 100644 esp8266/common-hal/nativeio/TouchIn.c create mode 100644 shared-bindings/nativeio/TouchIn.c create mode 100644 shared-bindings/nativeio/TouchIn.h diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 5a1226e1a8..0ae240d672 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -141,8 +141,8 @@ endif LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) LIBM_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-file-name=libm.a) -LDFLAGS = -Lasf/thirdparty/CMSIS/Lib/GCC/ -L $(dir $(LIBGCC_FILE_NAME)) -L $(dir $(LIBM_FILE_NAME)) -nostdlib -T $(LD_FILE) -Map=$@.map --cref --gc-sections -LIBS = -larm_cortexM0l_math -lm -lgcc -lc +LDFLAGS = -L asf/thirdparty/CMSIS/Lib/GCC/ -L QTouch/ -L $(dir $(LIBGCC_FILE_NAME)) -L $(dir $(LIBM_FILE_NAME)) -nostdlib -T $(LD_FILE) -Map=$@.map --cref --gc-sections +LIBS = -larm_cortexM0l_math -lsamd21_qtouch_gcc -lm -lgcc -lc SRC_ASF = $(addprefix asf/sam0/,\ @@ -224,6 +224,16 @@ SRC_BINDINGS = \ SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \ $(addprefix common-hal/, $(SRC_BINDINGS)) +# Handle touch support on its own since it only fits in the image when external +# flash is used to store the file system. +SRC_BINDINGS_EXPANDED += shared-bindings/nativeio/TouchIn.c + +ifeq ($(FLASH_IMPL),internal_flash.c) +SRC_BINDINGS_EXPANDED += common-hal/nativeio/TouchInStub.c +else +SRC_BINDINGS_EXPANDED += common-hal/nativeio/TouchIn.c +endif + SRC_SHARED_MODULE = \ bitbangio/__init__.c \ bitbangio/I2C.c \ diff --git a/atmel-samd/QTouch/touch_api_ptc.h b/atmel-samd/QTouch/touch_api_ptc.h new file mode 100644 index 0000000000..95b6767d54 --- /dev/null +++ b/atmel-samd/QTouch/touch_api_ptc.h @@ -0,0 +1,2033 @@ +/* This source file is part of the ATMEL QTouch Library Release 5.0.8 */ + +/***************************************************************************** + * + * \file + * + * \brief This file contains the SAMD QTouch Library API for + * Capacitive Touch acquisition using the PTC module. + * + * + * - Userguide: QTouch Library Peripheral Touch Controller User Guide. + * - Support email: www.atmel.com/design-support/ + * + * + * Copyright (c) 2013-2015 Atmel Corporation. All rights reserved. + * + * \asf_license_start + * + * 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 micro-controller 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 + * + ******************************************************************************/ + +#ifndef TOUCH_API_PTC_H +#define TOUCH_API_PTC_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/*---------------------------------------------------------------------------- + * compiler information + * + * + *----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- + * include files + * + * + *----------------------------------------------------------------------------*/ + +#include "touch.h" + +/*---------------------------------------------------------------------------- + * macros + * + * + *----------------------------------------------------------------------------*/ +#define MIN_NOISE_MEAS_SIGNAL_STABILITY_VAL 1u +#define MAX_NOISE_MEAS_SIGNAL_STABILITY_VAL 1000u +#define MIN_FREQ_AUTO_TUNE_SIG_STABILITY_VAL 1u +#define MAX_FREQ_AUTO_TUNE_SIG_STABILITY_VAL 1000u +#define MIN_NOISE_MEAS_BUFFER_CNT_VAL 3u +#define MAX_NOISE_MEAS_BUFFER_CNT_VAL 10u +#define MIN_NOISE_LIMIT_VAL 1u +#define MAX_NOISE_LIMIT_VAL 255u +#define MIN_FREQ_AUTO_TUNE_CNT 1u +#define MAX_FREQ_AUTO_TUNE_CNT 255u +#define MIN_LOCKOUT_COUNT 1u +#define MAX_LOCKOUT_COUNT 255u + +#define EN_NOISE_MEAS 1u +#define DIS_NOISE_MEAS 0u +#define EN_FREQ_AUTO_TUNE 1u +#define DIS_FREQ_AUTO_TUNE 0u + +#define MAX_LOCKOUT_VAL 2u +#define NO_RECAL_RE_EN 1u + +/*Default AUTO TUNE define*/ +#define DEF_AUTO_TUNE_NONE 0 + +/*Default AUTO_OS define*/ +#define DEF_AUTO_OS_DISABLE 0 + +/*Default AKS feature define*/ +#define DEF_AKS_DISABLE 0 + +#define DEF_FREQ_AUTO_TUNE_DISABLE 0 + +#define DEF_NOISE_MEAS_DISABLE 0 + +#define DEF_SENS_LOCKOUT_NO 2 + +#define DEF_MOIS_TOLERANCE_DISABLE 0 +/*---------------------------------------------------------------------------- + * manifest constants + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Touch Library Acquisition Status bit-fields. + */ +/* ! @{ */ + +#define TOUCH_NO_ACTIVITY (0x0000u) /* !< No Touch activity. */ +#define TOUCH_IN_DETECT (0x0001u) /* !< Atleast one Touch channel + * is in detect. */ +#define TOUCH_STATUS_CHANGE (0x0002u) /* !< Change in Touch status of + * at-least one Touch channel. */ +#define TOUCH_ROTOR_SLIDER_POS_CHANGE (0x0004u) /* !< Change in Rotor or Slider + * position of at-least one rotor + * or slider. */ +#define TOUCH_CHANNEL_REF_CHANGE (0x0008u) /* !< Change in Reference value + * of at-least one Touch channel. + **/ +#define TOUCH_BURST_AGAIN (0x0100u) /* !< Indicates that reburst is + * required to resolve Filtering + * or Calibration state. */ +#define TOUCH_RESOLVE_CAL (0x0200u) /* !< Indicates that reburst is + * needed to resolve + * Calibration. */ +#define TOUCH_RESOLVE_FILTERIN (0x0400u) /* !< Indicates that reburst is + * needed to resolve Filtering. + **/ +#define TOUCH_RESOLVE_DI (0x0800u) /* !< Indicates that reburst is + * needed to resolve Detect + * Integration. */ +#define TOUCH_RESOLVE_POS_RECAL (0x1000u) /* !< Indicates that reburst is + * needed to resolve + * Recalibration. */ + +#define TOUCH_CC_CALIB_ERROR (0x2000u) /* !< Indicates that CC + * calibration + * failed on at least + *one + * channel. */ + +#define TOUCH_AUTO_OS_IN_PROGRESS (0x4000u) /* !< Indicates that + * Auto-oversamples in progress + * to get stable channel + *signal. */ +/* ! @} */ + + +/*! \name Touch Library Sensor post-processing mode options. + */ +/* ! @{ */ +#define TOUCH_LIBRARY_DRIVEN (0u) + +#define TOUCH_APPLN_DRIVEN (1u) + +/* ! @} */ + +/* ! \name SENSOR STATE CONFIGURATIONS */ +/* !@{ */ + +/** + * \def GET_SENSOR_STATE(SENSOR_NUMBER) + * \brief To get the sensor state that it is in detect or not + * \param SENSOR_NUMBER for which the state to be detected + * \return Returns either 0 or 1 + * If the bit value is 0, it is not in detect + * If the bit value is 1, it is in detect + */ +/* Self capacitance method */ + #define GET_SELFCAP_SENSOR_STATE(SENSOR_NUMBER) (p_selfcap_measure_data-> \ + p_sensor_states[(SENSOR_NUMBER / \ + 8)] & (1 << (SENSOR_NUMBER % 8))) >> (SENSOR_NUMBER % 8) +/* Mutual capacitance method */ + #define GET_MUTLCAP_SENSOR_STATE(SENSOR_NUMBER) (p_mutlcap_measure_data-> \ + p_sensor_states[(SENSOR_NUMBER / \ + 8)] & (1 << (SENSOR_NUMBER % 8))) >> (SENSOR_NUMBER % 8) + +/** + * \def GET_MUTLCAP_SENSOR_NOISE_STATUS(SENSOR_NUMBER) + * \brief To get the noise status of a particular sensor - it is noisy or not + * \param SENSOR_NUMBER for which the noise status needs to be fetched + * \return Returns either 0 or 1 + * If the bit value is 0, it is not noisy + * If the bit value is 1, it is noisy + */ + +/* Mutual capacitance method */ +#define GET_MUTLCAP_SENSOR_NOISE_STATUS(SENSOR_NUMBER) (p_mutlcap_measure_data-> \ + p_sensor_noise_status[(SENSOR_NUMBER / \ + 8)] & (1 << (SENSOR_NUMBER % 8))) >> (SENSOR_NUMBER % 8) + +/** + * \def GET_SELFCAP_SENSOR_NOISE_STATUS(SENSOR_NUMBER) + * \brief To get the noise status of a particular sensor - it is noisy or not + * \param SENSOR_NUMBER for which the noise status needs to be fetched + * \return Returns either 0 or 1 + * If the bit value is 0, it is not noisy + * If the bit value is 1, it is noisy + */ + +/* Mutual capacitance method */ +#define GET_SELFCAP_SENSOR_NOISE_STATUS(SENSOR_NUMBER) (p_selfcap_measure_data-> \ + p_sensor_noise_status[(SENSOR_NUMBER / \ + 8)] & (1 << (SENSOR_NUMBER % 8))) >> (SENSOR_NUMBER % 8) + +/** + * \def GET_MUTLCAP_SENSOR_MOIS_STATUS(SNSR_NUM) + * \brief To get the mutual capacitance moisture status of a particular sensor + * \param SNSR_NUM for which the moisture status needs to be fetched + * \return Returns either 0 or 1 + * If the bit value is 0, it is not moisture affected + * If the bit value is 1, it is moisture affected + */ + +#define GET_MUTLCAP_SENSOR_MOIS_STATUS(SNSR_NUM) ((p_mutlcap_measure_data-> \ + p_sensor_mois_status[(SNSR_NUM)/8] & (1<<((SNSR_NUM)%8))) \ + >>(SNSR_NUM %8)) + +/** + * \def GET_SELFCAP_SENSOR_MOIS_STATUS(SNSR_NUM) + * \brief To get the self capacitance moisture status of a particular sensor + * \param SNSR_NUM for which the moisture status needs to be fetched + * \return Returns either 0 or 1 + * If the bit value is 0, it is not moisture affected + * If the bit value is 1, it is moisture affected + */ + +#define GET_SELFCAP_SENSOR_MOIS_STATUS(SNSR_NUM) ((p_selfcap_measure_data-> \ + p_sensor_mois_status[(SNSR_NUM)/8] & (1<<((SNSR_NUM)%8)))>>\ + (SNSR_NUM %8)) + +/** + * \def GET_ROTOR_SLIDER_POSITION(ROTOR_SLIDER_NUMBER) + * \brief To get the rotor angle or slider position. + * These values are valid only when the sensor state for + * corresponding rotor or slider shows in detect. + * \param ROTOR_SLIDER_NUMBER for which the position to be known + * \return Returns rotor angle or sensor position + */ +/* !@} */ +/* Self capacitance method */ + #define GET_SELFCAP_ROTOR_SLIDER_POSITION(ROTOR_SLIDER_NUMBER) \ + p_selfcap_measure_data->p_rotor_slider_values[ \ + ROTOR_SLIDER_NUMBER] +/* Mutual capacitance method */ + #define GET_MUTLCAP_ROTOR_SLIDER_POSITION(ROTOR_SLIDER_NUMBER) \ + p_mutlcap_measure_data->p_rotor_slider_values[ \ + ROTOR_SLIDER_NUMBER] + +/** + * Calculate X line bit position. + */ +#define X(n) (uint32_t)(1u << n) + +/** + * Calculate Y line bit position. + */ +#define Y(n) (uint32_t)(1u << n) + +/** + * Max position hysteresis allowed + */ +#define TOUCH_MAX_POS_HYST (7u) + +/*---------------------------------------------------------------------------- + * Mutual cap macros + * + * + *----------------------------------------------------------------------------*/ +#if DEF_TOUCH_MUTLCAP == 1 +#if (DEF_MUTLCAP_NUM_SENSORS == 0u) +#error 'Number of Sensors cannot be 0.' +#endif + +#if (DEF_MUTLCAP_NUM_SENSORS > DEF_MUTLCAP_NUM_CHANNELS) + #error 'Number of Sensors cannot exceed number of channels' +#endif + +/** + * Size of Touch Sensor. + */ +#define PRIV_MUTLCAP_SIZE_SENSOR (8u) + +/** + * Size of Touch Channel. + */ +#define PRIV_MUTLCAP_SIZE_CHANNEL (13u) + +/** + * Size of Touch Rotor Slider. + */ +#define PRIV_MUTLCAP_SIZE_RS (13u) + +/** + * Size of Touch Pad bytes. + */ +#define PRIV_MUTLCAP_PAD_BYTE_SIZE (78u) + +#define PRIV_CHANNELS_64_NO (64u) +#define PRIV_BYTES_IN_UINT64 (8u) + +/** + * Touch Data block size. + */ +#define PRIV_MUTLCAP_DATA_BLK_SIZE ((PRIV_MUTLCAP_SIZE_SENSOR * \ + DEF_MUTLCAP_NUM_SENSORS) + \ + (PRIV_MUTLCAP_SIZE_CHANNEL * DEF_MUTLCAP_NUM_CHANNELS) + \ + (PRIV_MUTLCAP_SIZE_RS * DEF_MUTLCAP_NUM_ROTORS_SLIDERS) + \ + ((DEF_MUTLCAP_NUM_CHANNELS + 7u) / 8u) + \ + ((DEF_MUTLCAP_NUM_CHANNELS + 7u) / 8u) + \ + ((DEF_MUTLCAP_NOISE_MEAS_ENABLE * DEF_MUTLCAP_NUM_CHANNELS + 7u) / 8u) + \ + ((DEF_MUTLCAP_NOISE_MEAS_ENABLE * DEF_MUTLCAP_NUM_CHANNELS + 7u) / 8u) + \ + (DEF_MUTLCAP_NOISE_MEAS_ENABLE * 2 * \ + DEF_MUTLCAP_NUM_CHANNELS) + \ + (DEF_MUTLCAP_NOISE_MEAS_ENABLE * 2 * DEF_MUTLCAP_NUM_CHANNELS * \ + DEF_MUTLCAP_NOISE_MEAS_BUFFER_CNT) + \ + (DEF_MUTLCAP_FREQ_AUTO_TUNE_ENABLE * 2 * \ + DEF_MUTLCAP_NUM_CHANNELS) + \ + (PRIV_MUTLCAP_PAD_BYTE_SIZE) + \ + (((((DEF_MUTLCAP_NUM_CHANNELS - \ + 1u) / \ + PRIV_CHANNELS_64_NO) + \ + 1u) * PRIV_BYTES_IN_UINT64) * 2u) +\ + (DEF_MUTLCAP_MOIS_TOLERANCE_ENABLE * DEF_MUTLCAP_NUM_SENSORS *2u) +\ + (DEF_MUTLCAP_MOIS_TOLERANCE_ENABLE*(DEF_MUTLCAP_NUM_MOIS_GROUPS*8))+\ + ((DEF_MUTLCAP_MOIS_TOLERANCE_ENABLE * DEF_MUTLCAP_NUM_SENSORS+7)/8u)) + +/** + * Initialize rotor slider table. + */ +#if (DEF_MUTLCAP_NUM_ROTORS_SLIDERS != 0u) +#define PRIV_MUTLCAP_RS_TABLE_INIT touch_mutlcap_rs_table_init +#else +#define PRIV_MUTLCAP_RS_TABLE_INIT NULL +#endif /* MUTUAL_NUM_ROTORS_SLIDERS*/ + +#endif /* DEF_TOUCH_MUTLCAP. */ + +/*---------------------------------------------------------------------------- + * Self cap macros + * + * + *----------------------------------------------------------------------------*/ +#if DEF_TOUCH_SELFCAP == 1 +#if (DEF_SELFCAP_NUM_SENSORS == 0u) +#error 'Number of Sensors cannot be 0.' +#endif + +#if (DEF_SELFCAP_NUM_SENSORS > DEF_SELFCAP_NUM_CHANNELS) + #error 'Number of Sensors cannot exceed number of channels' +#endif + +/** + * Size of Touch Sensor. + */ +#define PRIV_SELFCAP_SIZE_SENSOR (8u) + +/** + * Size of Touch Channel. + */ +#define PRIV_SELFCAP_SIZE_CHANNEL (13u) + +/** + * Size of Touch Rotor Slider. + */ +#define PRIV_SELFCAP_SIZE_RS (13u) + +/** + * Size of Touch Pad bytes. + */ +#define PRIV_SELFCAP_PAD_BYTE_SIZE (78u) + +#define PRIV_BYTES_IN_UINT64 (8u) + +/** + * Touch Data block size. + */ +#define PRIV_SELFCAP_DATA_BLK_SIZE ((PRIV_SELFCAP_SIZE_SENSOR * \ + DEF_SELFCAP_NUM_CHANNELS) + \ + (PRIV_SELFCAP_SIZE_CHANNEL * DEF_SELFCAP_NUM_CHANNELS) + \ + (PRIV_SELFCAP_SIZE_RS * DEF_SELFCAP_NUM_ROTORS_SLIDERS) + \ + ((DEF_SELFCAP_NUM_CHANNELS + 7u) / 8u) + \ + ((DEF_SELFCAP_NUM_CHANNELS + 7u) / 8u) + \ + ((DEF_SELFCAP_NOISE_MEAS_ENABLE * DEF_SELFCAP_NUM_CHANNELS + 7u) / 8u) + \ + ((DEF_SELFCAP_NOISE_MEAS_ENABLE * DEF_SELFCAP_NUM_CHANNELS + 7u) / 8u) + \ + (DEF_SELFCAP_NOISE_MEAS_ENABLE * 2 * \ + DEF_SELFCAP_NUM_CHANNELS) + \ + (DEF_SELFCAP_NOISE_MEAS_ENABLE * 2 * DEF_SELFCAP_NUM_CHANNELS * \ + DEF_SELFCAP_NOISE_MEAS_BUFFER_CNT) + \ + (DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE * 2 * \ + DEF_SELFCAP_NUM_CHANNELS) + \ + (PRIV_SELFCAP_PAD_BYTE_SIZE) + \ + (PRIV_BYTES_IN_UINT64 * 2u) +\ + (DEF_SELFCAP_MOIS_TOLERANCE_ENABLE * DEF_SELFCAP_NUM_CHANNELS *2u) +\ + (DEF_SELFCAP_MOIS_TOLERANCE_ENABLE *(DEF_SELFCAP_NUM_MOIS_GROUPS*8))+\ + ((DEF_SELFCAP_MOIS_TOLERANCE_ENABLE * DEF_SELFCAP_NUM_CHANNELS+7)/8u)) + +/** + * Initialize rotor slider table. + */ +#if (DEF_SELFCAP_NUM_ROTORS_SLIDERS != 0u) +#define PRIV_SELFCAP_RS_TABLE_INIT touch_selfcap_rs_table_init +#else +#define PRIV_SELFCAP_RS_TABLE_INIT NULL +#endif /* SELF_NUM_ROTORS_SLIDERS*/ +#endif /* DEF_TOUCH_SELFCAP. */ + + +#define LUMP_1(n) ((uint32_t)((uint32_t)1u << n)) +#define LUMP_2(a,b) ((uint32_t)((uint32_t)1 << b) | (LUMP_1(a))) +#define LUMP_3(a,b,c) ((uint32_t)((uint32_t)1 << c) | (LUMP_2(a,b))) +#define LUMP_4(a,b,c,d) ((uint32_t)((uint32_t)1 << d) | (LUMP_3(a,b,c))) +#define LUMP_5(a,b,c,d,e) ((uint32_t)((uint32_t)1 << e) | (LUMP_4(a,b,c,d))) +#define LUMP_6(a,b,c,d,e,f) ((uint32_t)((uint32_t)1 << f) | (LUMP_5(a,b,c,d,e))) +#define LUMP_7(a,b,c,d,e,f,g) ((uint32_t)((uint32_t)1 << g) | (LUMP_6(a,b,c,d,e,f))) +#define LUMP_8(a,b,c,d,e,f,g,h) ((uint32_t)((uint32_t)1 << h) | (LUMP_7(a,b,c,d,e,f,g))) +#define LUMP_9(a,b,c,d,e,f,g,h,i) ((uint32_t)((uint32_t)1 << i) | (LUMP_8(a,b,c,d,e,f,g,h))) +#define LUMP_10(a,b,c,d,e,f,g,h,i,j) ((uint32_t)((uint32_t)1 << j) | (LUMP_9(a,b,c,d,e,f,g,h,i))) +#define LUMP_11(a,b,c,d,e,f,g,h,i,j,k) ((uint32_t)((uint32_t)1 << k) | (LUMP_10(a,b,c,d,e,f,g,h,i,j))) +#define LUMP_12(a,b,c,d,e,f,g,h,i,j,k,l) ((uint32_t)((uint32_t)1 << l) | (LUMP_11(a,b,c,d,e,f,g,h,i,j,k))) +#define LUMP_13(a,b,c,d,e,f,g,h,i,j,k,l,m) ((uint32_t)((uint32_t)1 << m) | (LUMP_12(a,b,c,d,e,f,g,h,i,j,k,l))) +#define LUMP_14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) ((uint32_t)((uint32_t)1 << n) | (LUMP_13(a,b,c,d,e,f,g,h,i,j,k,l,m))) +#define LUMP_15(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) ((uint32_t)((uint32_t)1 << o) | (LUMP_14(a,b,c,d,e,f,g,h,i,j,k,l,m,n))) +#define LUMP_16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) ((uint32_t)((uint32_t)1 << p) | (LUMP_15(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o))) +#define LUMP_17(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) ((uint32_t)((uint32_t)1 << q) | (LUMP_16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p))) +#define LUMP_18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) ((uint32_t)((uint32_t)1 << r) | (LUMP_17(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q))) +#define LUMP_19(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s) ((uint32_t)((uint32_t)1 << s) | (LUMP_18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r))) +#define LUMP_20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) ((uint32_t)((uint32_t)1 << t) | (LUMP_19(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s))) +#define LUMP_21(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u) ((uint32_t)((uint32_t)1 << u) | (LUMP_20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t))) +#define LUMP_22(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v) ((uint32_t)((uint32_t)1 << v) | (LUMP_21(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u))) +#define LUMP_23(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w) ((uint32_t)((uint32_t)1 << w) | (LUMP_22(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v))) +#define LUMP_24(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x) ((uint32_t)((uint32_t)1 << x) | (LUMP_23(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w))) +#define LUMP_25(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y) ((uint32_t)((uint32_t)1 << y) | (LUMP_24(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x))) +#define LUMP_26(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) ((uint32_t)((uint32_t)1 << z) | (LUMP_25(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y))) +#define LUMP_27(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa) ((uint32_t)((uint32_t)1 << aa) | (LUMP_26(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z))) +#define LUMP_28(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab) ((uint32_t)((uint32_t)1 << ab) | (LUMP_27(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa))) +#define LUMP_29(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac) ((uint32_t)((uint32_t)1 << ac) | (LUMP_28(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab))) +#define LUMP_30(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad) ((uint32_t)((uint32_t)1 << ad) | (LUMP_29(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac))) +#define LUMP_31(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae) ((uint32_t)((uint32_t)1 << ae) | (LUMP_30(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,ab,ac,ad))) +#define LUMP_32(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af) ((uint32_t)((uint32_t)1 << af) | (LUMP_31(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,ab,ac,ad,ae))) + +#define _NUM_ARGS2(X,X16,X15,X14,X13,X12,X11,X10,X9,X8,X7,X6,X5,X4,X3,X2,X1,N,...) N +#define NUM_ARGS(...) _NUM_ARGS2(0, __VA_ARGS__ ,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0) +/*Two levels are required to Create the macro assignment*/ +#define _LUMP_OF_L1(N, ...) LUMP_ ## N(__VA_ARGS__) +#define _LUMP_OF_L2(N, ...) _LUMP_OF_L1(N, __VA_ARGS__) +#define LUMP_X(...) _LUMP_OF_L2(NUM_ARGS(__VA_ARGS__), __VA_ARGS__) +#define LUMP_Y(...) _LUMP_OF_L2(NUM_ARGS(__VA_ARGS__), __VA_ARGS__) + +#ifndef DEF_MUTL_AUTO_TUNE_VALUE +#define DEF_MUTL_AUTO_TUNE_VALUE DEF_AUTO_TUNE_NONE +#endif +#ifndef DEF_SELF_AUTO_TUNE_VALUE +#define DEF_SELF_AUTO_TUNE_VALUE DEF_AUTO_TUNE_NONE +#endif + +#ifndef DEF_MUTLCAP_AUTO_OS_ENABLE +#define DEF_MUTLCAP_AUTO_OS_ENABLE DEF_AUTO_OS_DISABLE +#endif + +#ifndef DEF_SELFCAP_AUTO_OS_ENABLE +#define DEF_SELFCAP_AUTO_OS_ENABLE DEF_AUTO_OS_DISABLE +#endif + +#ifndef DEF_MUTLCAP_FREQ_AUTO_TUNE_ENABLE +#define DEF_MUTLCAP_FREQ_AUTO_TUNE_ENABLE DEF_FREQ_AUTO_TUNE_DISABLE +#endif + +#ifndef DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE +#define DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE DEF_FREQ_AUTO_TUNE_DISABLE +#endif + +#ifndef DEF_MUTLCAP_NOISE_MEAS_ENABLE +#define DEF_MUTLCAP_NOISE_MEAS_ENABLE DEF_NOISE_MEAS_DISABLE +#endif + +#ifndef DEF_SELFCAP_NOISE_MEAS_ENABLE +#define DEF_SELFCAP_NOISE_MEAS_ENABLE DEF_NOISE_MEAS_DISABLE +#endif + +#ifndef DEF_MUTLCAP_LOCKOUT_SEL +#define DEF_MUTLCAP_LOCKOUT_SEL DEF_SENS_LOCKOUT_NO +#endif + +#ifndef DEF_SELFCAP_LOCKOUT_SEL +#define DEF_SELFCAP_LOCKOUT_SEL DEF_SENS_LOCKOUT_NO +#endif + +#ifndef DEF_MUTLCAP_MOIS_TOLERANCE_ENABLE +#define DEF_MUTLCAP_MOIS_TOLERANCE_ENABLE DEF_MOIS_TOLERANCE_DISABLE +#endif + +#ifndef DEF_SELFCAP_MOIS_TOLERANCE_ENABLE +#define DEF_SELFCAP_MOIS_TOLERANCE_ENABLE DEF_MOIS_TOLERANCE_DISABLE +#endif + +#ifndef DEF_MUTLCAP_AKS_ENABLE +#define DEF_MUTLCAP_AKS_ENABLE DEF_AKS_DISABLE +#endif + +#ifndef DEF_SELFCAP_AKS_ENABLE +#define DEF_SELFCAP_AKS_ENABLE DEF_AKS_DISABLE +#endif + +#if (DEF_MUTL_AUTO_TUNE_VALUE == DEF_AUTO_TUNE_NONE) +#define DEF_MUTL_AUTO_TUNE_FUNC NULL +#else +#define DEF_MUTL_AUTO_TUNE_FUNC mutual_auto_tuning +#endif + +#if (DEF_SELF_AUTO_TUNE_VALUE == DEF_AUTO_TUNE_NONE) +#define DEF_SELF_AUTO_TUNE_FUNC NULL +#else +#define DEF_SELF_AUTO_TUNE_FUNC self_auto_tuning +#endif + +#if ((DEF_MUTLCAP_AUTO_OS_ENABLE != DEF_AUTO_OS_DISABLE) || (DEF_SELFCAP_AUTO_OS_ENABLE != DEF_AUTO_OS_DISABLE)) +#define DEF_AUTO_OS_FUNC auto_os_resolve +#else +#define DEF_AUTO_OS_FUNC NULL +#endif + +#if ((DEF_MUTLCAP_AKS_ENABLE != DEF_AKS_DISABLE) || (DEF_SELFCAP_AKS_ENABLE != DEF_AKS_DISABLE)) +#define DEF_AKS_FUNC enable_aks +#else +#define DEF_AKS_FUNC NULL +#endif +/** +* Initialize the Frequency Auto tune routine +*/ +#if ((DEF_MUTLCAP_FREQ_AUTO_TUNE_ENABLE != DEF_FREQ_AUTO_TUNE_DISABLE) || (DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE != DEF_FREQ_AUTO_TUNE_DISABLE)) +#define PRIV_FREQ_AUTO_TUNE_CHK chk_frequency_auto_tune +#else +#define PRIV_FREQ_AUTO_TUNE_CHK NULL +#endif /*Frequency Auto tune Enable*/ + +/** +* Initialize the Noise Measurement routine +*/ +#if ((DEF_MUTLCAP_NOISE_MEAS_ENABLE != DEF_NOISE_MEAS_DISABLE) || (DEF_SELFCAP_NOISE_MEAS_ENABLE != DEF_NOISE_MEAS_DISABLE)) +#define PRIV_NM_TABLE_INIT touch_noise_mit_table_init +#else +#define PRIV_NM_TABLE_INIT NULL +#endif /*Noise Measurement Enable*/ + + +#if (((DEF_MUTLCAP_LOCKOUT_SEL != DEF_SENS_LOCKOUT_NO) && (DEF_MUTLCAP_NOISE_MEAS_ENABLE != DEF_NOISE_MEAS_DISABLE))\ + || ((DEF_SELFCAP_LOCKOUT_SEL != DEF_SENS_LOCKOUT_NO) && (DEF_SELFCAP_NOISE_MEAS_ENABLE != DEF_NOISE_MEAS_DISABLE))) +#define DEF_LOCKOUT_FUNC lk_chk +#else +#define DEF_LOCKOUT_FUNC NULL +#endif /*Lockout Enable*/ + +/** +* Initialize the moisture tolerance routine +*/ +#if ((DEF_MUTLCAP_MOIS_TOLERANCE_ENABLE != DEF_MOIS_TOLERANCE_DISABLE) || (DEF_SELFCAP_MOIS_TOLERANCE_ENABLE != DEF_MOIS_TOLERANCE_DISABLE)) +#define PRIV_MOIS_TOLERANCE_CHK chk_moisture_tolerance +#else +#define PRIV_MOIS_TOLERANCE_CHK NULL +#endif /*Moisture Tolerance Enable*/ + +/* Moisture Tolerance */ + +/** + * \def GET_MUTLCAP_MOIS_GRP_SUM_DELTA(GRP_ID) + * \brief To get the mutual capacitance MOISTURE GROUP SUM DELTA + * \param GRP_ID for which the moisture group sum delta needs to be fetched + * \return Returns sum delta + */ + +#define GET_MUTLCAP_MOIS_GRP_SUM_DELTA(GRP_ID) (mois_mutl_grp_delta_arr[(GRP_ID)-1]) + + +/** + * \def GET_MUTLCAP_MOIS_GRP_ADJ_DELTA(GRP_ID) + * \brief To get the mutual capacitance MOISTURE GROUP ADJACENT DELTA + * \param GRP_ID for which the moisture group adjacent delta needs to be fetched + * \return Returns adjacent delta + */ + +#define GET_MUTLCAP_MOIS_GRP_ADJ_DELTA(GRP_ID) (mois_mutl_grp_adj_delta_arr[(GRP_ID)-1]) + +/** + * \def GET_MOIS_MUT_GLOB_LOCK_STATE + * \brief To get the mutual capacitance global moisture lockout status + * \param none + * \return Returns mutual capacitance global moisture lockout status + */ + +#define GET_MOIS_MUT_GLOB_LOCK_STATE (mois_lock_global_mutl) + + +/** + * \def GET_SELFCAP_MOIS_GRP_SUM_DELTA(GRP_ID) + * \brief To get the self capacitance MOISTURE GROUP SUM DELTA + * \param GRP_ID for which the moisture group sum delta needs to be fetched + * \return Returns sum delta + */ + +#define GET_SELFCAP_MOIS_GRP_SUM_DELTA(GRP_ID) (mois_self_grp_delta_arr[(GRP_ID)-1]) + + +/** + * \def GET_SELFCAP_MOIS_GRP_ADJ_DELTA(GRP_ID) + * \brief To get the self capacitance MOISTURE GROUP ADJACENT DELTA + * \param GRP_ID for which the moisture group adjacent delta needs to be fetched + * \return Returns adjacent delta + */ + +#define GET_SELFCAP_MOIS_GRP_ADJ_DELTA(GRP_ID) (mois_self_grp_adj_delta_arr[(GRP_ID)-1]) + + +/** + * \def GET_MOIS_SELF_GLOB_LOCK_STATE + * \brief To get the self capacitance global moisture lockout status + * \param none + * \return Returns self capacitance global moisture lockout status + */ + +#define GET_MOIS_SELF_GLOB_LOCK_STATE (mois_lock_global_self) +/*---------------------------------------------------------------------------- + * type definitions + * + * + *----------------------------------------------------------------------------*/ + +/* ! An unsigned 8-bit number setting a sensor detection threshold. */ +typedef uint8_t threshold_t; + +/* ! Sensor number type. */ +typedef uint8_t sensor_id_t; + +/* ! Current time type. */ +typedef uint16_t touch_current_time_t; + +/* ! Touch sensor delta value type. */ +typedef int16_t touch_delta_t; + +/* ! Status of Touch measurement. */ +typedef uint16_t touch_acq_status_t; + +/* ! Touch library GAIN setting */ +typedef enum tag_gain_t { + GAIN_1, + GAIN_2, + GAIN_4, + GAIN_8, + GAIN_16, + GAIN_32 +} +gain_t; +/* ! Touch library FILTER LEVEL setting */ +typedef enum tag_filter_level_t { + FILTER_LEVEL_1, + FILTER_LEVEL_2, + FILTER_LEVEL_4, + FILTER_LEVEL_8, + FILTER_LEVEL_16, + FILTER_LEVEL_32, + FILTER_LEVEL_64 +} +filter_level_t; +/* ! Touch library AUTO OS setting */ +typedef enum tag_auto_os_t { + AUTO_OS_DISABLE, + AUTO_OS_2, + AUTO_OS_4, + AUTO_OS_8, + AUTO_OS_16, + AUTO_OS_32, + AUTO_OS_64, + AUTO_OS_128 +} +auto_os_t; + +/* ! Touch Library error codes. */ +typedef enum tag_touch_ret_t { + /* ! Successful completion of operation. */ + TOUCH_SUCCESS, + /* ! Touch Library is busy with pending previous Touch measurement. */ + TOUCH_ACQ_INCOMPLETE, + /* ! Invalid input parameter. */ + TOUCH_INVALID_INPUT_PARAM, + /* ! Operation not allowed in the current Touch Library state. */ + TOUCH_INVALID_LIB_STATE, + /* ! Invalid self cap config input parameter. */ + TOUCH_INVALID_SELFCAP_CONFIG_PARAM, + /* ! Invalid mutual cap config input parameter. */ + TOUCH_INVALID_MUTLCAP_CONFIG_PARAM, + /* ! Invalid Recalibration threshold input value. */ + TOUCH_INVALID_RECAL_THRESHOLD, + + /* ! Channel number parameter exceeded total number of channels + * configured. */ + TOUCH_INVALID_CHANNEL_NUM, + /* ! Invalid sensor type. Sensor type can NOT be SENSOR_TYPE_UNASSIGNED. + **/ + TOUCH_INVALID_SENSOR_TYPE, + /* ! Invalid Sensor number parameter. */ + TOUCH_INVALID_SENSOR_ID, + /* ! Number of Rotor/Sliders set as 0, */ + /* ! when trying to configure a rotor/slider. */ + TOUCH_INVALID_RS_NUM +} +touch_ret_t; + +/* ! Touch Application error codes. */ +typedef enum tag_touch_app_err_t { + TOUCH_INIT_CONFIG_ERR, + TOUCH_SENSOR_CONFIG_ERR, + TOUCH_INIT_CALIB_ERR, + TOUCH_MEASURE_INCOMPLETE, + TOUCH_MEASURE_CC_CAL_FAILED +} +touch_app_err_t; + +/* ! Sensor start and end channel type of a Sensor. Channel number starts with + * value 0. */ +typedef enum tag_channel_t { + CHANNEL_0, CHANNEL_1, CHANNEL_2, CHANNEL_3, CHANNEL_4, CHANNEL_5, + CHANNEL_6, CHANNEL_7, + CHANNEL_8, CHANNEL_9, CHANNEL_10, CHANNEL_11, CHANNEL_12, CHANNEL_13, + CHANNEL_14, CHANNEL_15, + CHANNEL_16, CHANNEL_17, CHANNEL_18, CHANNEL_19, CHANNEL_20, CHANNEL_21, + CHANNEL_22, CHANNEL_23, + CHANNEL_24, CHANNEL_25, CHANNEL_26, CHANNEL_27, CHANNEL_28, CHANNEL_29, + CHANNEL_30, CHANNEL_31, + CHANNEL_32, CHANNEL_33, CHANNEL_34, CHANNEL_35, CHANNEL_36, CHANNEL_37, + CHANNEL_38, CHANNEL_39, + CHANNEL_40, CHANNEL_41, CHANNEL_42, CHANNEL_43, CHANNEL_44, CHANNEL_45, + CHANNEL_46, CHANNEL_47, + CHANNEL_48, CHANNEL_49, CHANNEL_50, CHANNEL_51, CHANNEL_52, CHANNEL_53, + CHANNEL_54, CHANNEL_55, + CHANNEL_56, CHANNEL_57, CHANNEL_58, CHANNEL_59, CHANNEL_60, CHANNEL_61, + CHANNEL_62, CHANNEL_63, + CHANNEL_64, CHANNEL_65, CHANNEL_66, CHANNEL_67, CHANNEL_68, CHANNEL_69, + CHANNEL_70, CHANNEL_71, + CHANNEL_72, CHANNEL_73, CHANNEL_74, CHANNEL_75, CHANNEL_76, CHANNEL_77, + CHANNEL_78, CHANNEL_79, + CHANNEL_80, CHANNEL_81, CHANNEL_82, CHANNEL_83, CHANNEL_84, CHANNEL_85, + CHANNEL_86, CHANNEL_87, + CHANNEL_88, CHANNEL_89, CHANNEL_90, CHANNEL_91, CHANNEL_92, CHANNEL_93, + CHANNEL_94, CHANNEL_95, + CHANNEL_96, CHANNEL_97, CHANNEL_98, CHANNEL_99, CHANNEL_100, + CHANNEL_101, CHANNEL_102, CHANNEL_103, + CHANNEL_104, CHANNEL_105, CHANNEL_106, CHANNEL_107, CHANNEL_108, + CHANNEL_109, CHANNEL_110, CHANNEL_111, + CHANNEL_112, CHANNEL_113, CHANNEL_114, CHANNEL_115, CHANNEL_116, + CHANNEL_117, CHANNEL_118, CHANNEL_119, + CHANNEL_120, CHANNEL_121, CHANNEL_122, CHANNEL_123, CHANNEL_124, + CHANNEL_125, CHANNEL_126, CHANNEL_127, + CHANNEL_128, CHANNEL_129, CHANNEL_130, CHANNEL_131, CHANNEL_132, + CHANNEL_133, CHANNEL_134, CHANNEL_135, + CHANNEL_136, CHANNEL_137, CHANNEL_138, CHANNEL_139, CHANNEL_140, + CHANNEL_141, CHANNEL_142, CHANNEL_143, + CHANNEL_144, CHANNEL_145, CHANNEL_146, CHANNEL_147, CHANNEL_148, + CHANNEL_149, CHANNEL_150, CHANNEL_151, + CHANNEL_152, CHANNEL_153, CHANNEL_154, CHANNEL_155, CHANNEL_156, + CHANNEL_157, CHANNEL_158, CHANNEL_159, + CHANNEL_160, CHANNEL_161, CHANNEL_162, CHANNEL_163, CHANNEL_164, + CHANNEL_165, CHANNEL_166, CHANNEL_167, + CHANNEL_168, CHANNEL_169, CHANNEL_170, CHANNEL_171, CHANNEL_172, + CHANNEL_173, CHANNEL_174, CHANNEL_175, + CHANNEL_176, CHANNEL_177, CHANNEL_178, CHANNEL_179, CHANNEL_180, + CHANNEL_181, CHANNEL_182, CHANNEL_183, + CHANNEL_184, CHANNEL_185, CHANNEL_186, CHANNEL_187, CHANNEL_188, + CHANNEL_189, CHANNEL_190, CHANNEL_191, + CHANNEL_192, CHANNEL_193, CHANNEL_194, CHANNEL_195, CHANNEL_196, + CHANNEL_197, CHANNEL_198, CHANNEL_199, + CHANNEL_200, CHANNEL_201, CHANNEL_202, CHANNEL_203, CHANNEL_204, + CHANNEL_205, CHANNEL_206, CHANNEL_207, + CHANNEL_208, CHANNEL_209, CHANNEL_210, CHANNEL_211, CHANNEL_212, + CHANNEL_213, CHANNEL_214, CHANNEL_215, + CHANNEL_216, CHANNEL_217, CHANNEL_218, CHANNEL_219, CHANNEL_220, + CHANNEL_221, CHANNEL_222, CHANNEL_223, + CHANNEL_224, CHANNEL_225, CHANNEL_226, CHANNEL_227, CHANNEL_228, + CHANNEL_229, CHANNEL_230, CHANNEL_231, + CHANNEL_232, CHANNEL_233, CHANNEL_234, CHANNEL_235, CHANNEL_236, + CHANNEL_237, CHANNEL_238, CHANNEL_239, + CHANNEL_240, CHANNEL_241, CHANNEL_242, CHANNEL_243, CHANNEL_244, + CHANNEL_245, CHANNEL_246, CHANNEL_247, + CHANNEL_248, CHANNEL_249, CHANNEL_250, CHANNEL_251, CHANNEL_252, + CHANNEL_253, CHANNEL_254, CHANNEL_255 +} +channel_t; + +/* ! Touch library state. */ +typedef enum tag_touch_lib_state_t { + TOUCH_STATE_NULL, + TOUCH_STATE_INIT, + TOUCH_STATE_READY, + TOUCH_STATE_CALIBRATE, + TOUCH_STATE_BUSY +} +touch_lib_state_t; + +/* ! Sensor types available. */ +typedef enum tag_sensor_type_t { + SENSOR_TYPE_UNASSIGNED, + SENSOR_TYPE_KEY, + SENSOR_TYPE_ROTOR, + SENSOR_TYPE_SLIDER, + SENSOR_TYPE_LUMP, + MAX_SENSOR_TYPE +} +sensor_type_t; + +/* Options for the sense type. */ +typedef enum tag_touch_acq_t +{ + TOUCH_MUTUAL, + TOUCH_SELF, + MAX_TOUCH_ACQ +} touch_acq_t; + +/* ! Touch library acquisition mode. */ +typedef enum tag_touch_acq_mode_t { + /* ! When Raw acquisition mode is used, the measure_complete_callback */ + /* ! function is called immediately once a fresh value of Signals are */ + /* ! available. In this mode, the Touch Library does not do any */ + /* ! processing on the Signals. So, the References, Sensor states or */ + /* ! Rotor/Slider position values are not updated in this mode. */ + RAW_ACQ_MODE, + /* ! When Nomal acquisition mode is used, the measure_complete_callback + **/ + + /* ! function is called only after the Touch Library completes + * processing */ + /* ! of the Signal values obtained. The References, Sensor states and */ + /* ! Rotor/Slider position values are updated in this mode. */ + NORMAL_ACQ_MODE +} +touch_acq_mode_t; + +/* ! Touch library PTC prescaler clock and sense resistor auto tuning setting. + **/ +typedef enum tag_auto_tune_type_t { + /*! Auto tuning disabled. */ + AUTO_TUNE_NONE = 0, + /* Auto tune PTC prescaler and sense resistor for best noise performance. */ + AUTO_TUNE_PRSC = 1, + /* Auto tune PTC prescaler and sense resistor for least power consumption. */ + AUTO_TUNE_RSEL = 2, + +} +auto_tune_type_t; + +/** + * PTC frequency mode setting. + * Example: if Generic clock input to PTC = 4MHz, then: + * FREQ_MODE_NONE No Frequency hopping + * FREQ_MODE_HOP Frequency hopping is enabled. + * FREQ_MODE_SPREAD Spread spectrum mode with median filter + *disabled. + * FREQ_MODE_SPREAD_MEDIAN Spread spectrum mode with median filter enabled. + * + */ +typedef enum tag_freq_mode_sel_t { + FREQ_MODE_NONE, + FREQ_MODE_HOP, + FREQ_MODE_SPREAD, + FREQ_MODE_SPREAD_MEDIAN +} +freq_mode_sel_t; + +/** + * PTC clock prescale setting. Refer touch_configure_ptc_clock() API in + *touch.c + * Example: if Generic clock input to PTC = 4MHz, then: + * PRSC_DIV_SEL_1 sets PTC Clock to 4MHz + * PRSC_DIV_SEL_2 sets PTC Clock to 2MHz + * PRSC_DIV_SEL_4 sets PTC Clock to 1MHz + * PRSC_DIV_SEL_8 sets PTC Clock to 500KHz + * + */ +typedef enum tag_prsc_div_sel_t { + PRSC_DIV_SEL_1, + PRSC_DIV_SEL_2, + PRSC_DIV_SEL_4, + PRSC_DIV_SEL_8 +} +prsc_div_sel_t; + +/** + * PTC series resistor setting. For Mutual cap mode, this series + * resistor is switched internally on the Y-pin. For Self cap mode, + * the series resistor is switched internally on the Sensor pin. + * + * Example: + * RSEL_VAL_0 sets internal series resistor to 0ohms. + * RSEL_VAL_20 sets internal series resistor to 20Kohms. + * RSEL_VAL_50 sets internal series resistor to 50Kohms. + * RSEL_VAL_100 sets internal series resistor to 100Kohms. + */ +typedef enum tag_rsel_val_t { + RSEL_VAL_0, + RSEL_VAL_20, + RSEL_VAL_50, + RSEL_VAL_100 +} +rsel_val_t; + +/** + * PTC acquisition frequency delay setting. + * + * The PTC acquisition frequency is dependent on the Generic clock + * input to PTC and PTC clock prescaler setting. This delay setting + * inserts "n" PTC clock cycles between consecutive measurements on + * a given sensor, thereby changing the PTC acquisition frequency. + * FREQ_HOP_SEL_1 setting inserts 1 PTC clock cycle between consecutive + * measurements. FREQ_HOP_SEL_14 setting inserts 14 PTC clock cycles. + * Hence, higher delay setting will increase the total time taken for + * capacitance measurement on a given sensor as compared to a lower + * delay setting. + * + * A desired setting can be used to avoid noise around the same frequency + * as the acquisition frequency. + * + */ +typedef enum tag_freq_hop_sel_t { + FREQ_HOP_SEL_1, + FREQ_HOP_SEL_2, + FREQ_HOP_SEL_3, + FREQ_HOP_SEL_4, + FREQ_HOP_SEL_5, + FREQ_HOP_SEL_6, + FREQ_HOP_SEL_7, + FREQ_HOP_SEL_8, + FREQ_HOP_SEL_9, + FREQ_HOP_SEL_10, + FREQ_HOP_SEL_11, + FREQ_HOP_SEL_12, + FREQ_HOP_SEL_13, + FREQ_HOP_SEL_14, + FREQ_HOP_SEL_15, + FREQ_HOP_SEL_16 +} +freq_hop_sel_t; + +/** + * Which AKS group, if any, a sensor is in. + * NO_AKS_GROUP = sensor is not in an AKS group, and cannot be suppressed. + * AKS_GROUP_x = sensor is in AKS group x. + */ +typedef enum tag_aks_group_t { + NO_AKS_GROUP, + AKS_GROUP_1, + AKS_GROUP_2, + AKS_GROUP_3, + AKS_GROUP_4, + AKS_GROUP_5, + AKS_GROUP_6, + AKS_GROUP_7, + MAX_AKS_GROUP +} +aks_group_t; + +/** + * A sensor detection hysteresis value. This is expressed as a percentage of + * the sensor detection threshold. + * HYST_x = hysteresis value is x% of detection threshold value (rounded down). + * NB: a minimum value of 2 is used. + * + * Example: if detection threshold = 20, then: + * HYST_50 = 10 (50% of 20) + * HYST_25 = 5 (25% of 20) + * HYST_12_5 = 2 (12.5% of 20) + * HYST_6_25 = 2 (6.25% of 20 = 1, but value is hard-limited to 2) + */ +typedef enum tag_hysteresis_t { + HYST_50, + HYST_25, + HYST_12_5, + HYST_6_25, + MAX_HYST +} +hysteresis_t; + +/** + * A sensor recalibration threshold. This is expressed as a percentage of the + * sensor detection threshold. + * RECAL_x = recalibration threshold is x% of detection threshold value + * (rounded down). + * NB: a minimum value of 4 is used. + * + * Example: if detection threshold = 40, then: + * RECAL_100 = 40 (100% of 40) + * RECAL_50 = 20 (50% of 40) + * RECAL_25 = 10 (25% of 40) + * RECAL_12_5 = 5 (12.5% of 40) + * RECAL_6_25 = 4 (6.25% of 40 = 2, but value is hard-limited to 4) + */ +typedef enum tag_recal_threshold_t { + RECAL_100, + RECAL_50, + RECAL_25, + RECAL_12_5, + RECAL_6_25, + MAX_RECAL +} +recal_threshold_t; + +/** + * For rotors and sliders, the resolution of the reported angle or position. + * RES_x_BIT = rotor/slider reports x-bit values. + * + * Example: if slider resolution is RES_7_BIT, then reported positions are in + * the range 0..127. + */ +typedef enum tag_resolution_t { + RES_1_BIT, + RES_2_BIT, + RES_3_BIT, + RES_4_BIT, + RES_5_BIT, + RES_6_BIT, + RES_7_BIT, + RES_8_BIT, + MAX_RES +} +resolution_t; + +/** + * For sensor lockout selection during noise condition + * Single key lockout, Global key lockout, no lockout + **/ +typedef enum tag_sensor_lockout_t { + SINGLE_SENSOR_LOCKOUT, + GLOBAL_SENSOR_LOCKOUT, + NO_LOCKOUT +} +nm_sensor_lockout_t; + +/** + * For choosing the state of the PTC pins when bursting is not happening on those pins. + * PULLHIGH_WHEN_NOT_MEASURED,GND_WHEN_NOT_MEASURED + **/ +typedef enum tag_ptc_gpio_state_t { + PULLHIGH_WHEN_NOT_MEASURED, + GND_WHEN_NOT_MEASURED + } +ptc_gpio_state_t; + +/**Structure for storing moisture multi touch group info + * + */ +typedef struct tag_snsr_mois_t{ +/** moisture group member */ +uint8_t mois_grp; +/** multi touch group member */ +uint8_t multch_grp; + +}snsr_mois_t; + +/** Moisture Groups + * + */ +typedef enum tag_moisture_grp{ + MOIS_DISABLED=0, + MOIS_GROUP_0, + MOIS_GROUP_1, + MOIS_GROUP_2, + MOIS_GROUP_3, + MOIS_GROUP_4, + MOIS_GROUP_5, + MOIS_GROUP_6, + MOIS_GROUP_7, + MOIS_GROUPN +}moisture_grp_t; + +/* Multi touch Groups Enumeration for + * moisture Group + */ + +typedef enum tag_mltch_grp{ + MLTCH_NONE = (0 << 0u), + MLTCH_GROUP_0 = (1 << 0u), + MLTCH_GROUP_1 = (1 << 1u), + MLTCH_GROUP_2 = (1 << 2u), + MLTCH_GROUP_3 = (1 << 3u), + MLTCH_GROUP_4 = (1 << 4u), + MLTCH_GROUP_5 = (1 << 5u), + MLTCH_GROUP_6 = (1 << 6u), + MLTCH_GROUP_7 = (1 << 7u), + MLTCH_GROUP_Max = MLTCH_GROUP_7 +}mltch_grp_t; + +/* Touch mode configuration + * Interrupt / Polled mode + */ + +typedef enum tag_tch_mode +{ + TCH_MODE_POLLED = 0, + TCH_MODE_ISR = 1, + TCH_MODE_NONE = 2, +} tch_mode_t; + +/** Typedef for moisture sensor threshold */ +typedef int32_t mois_snsr_threshold_t; + +/** Typedef for moisture system threshold */ +typedef int32_t mois_system_threshold_t; + +/* ! Touch Library Timing info. */ +typedef struct tag_touch_time_t { + /* ! Touch Measurement period in milliseconds. This variable determines + * how */ + /* ! often a new touch measurement must be done. */ + /* ! Default value: Every 25 milliseconds. */ + uint16_t measurement_period_ms; + /* ! Current time, set by timer ISR. */ + volatile uint16_t current_time_ms; + /* ! Flag set by timer ISR when it's time to measure touch. */ + volatile uint8_t time_to_measure_touch; + +} +touch_time_t; + +/* ! Sensor structure for storing sensor related information. */ +typedef struct tag_sensor_t { + /* ! sensor state (calibrating, on, ...). */ + uint8_t state; + /* ! general purpose ctr: used for calibration, drifting, etc. */ + uint8_t general_counter; + /* ! Detect Integration ctr. */ + uint8_t ndil_counter; + + /* ! bits 7..6: sensor type: */ + /* ! {00: key,01: rotor,10: slider,11: reserved} */ + /* ! bits 5..3: AKS group (0..7): 0 = no AKS group */ + /* ! bit 2 : positive re-cal flag */ + /* ! bits 1..0: hysteresis */ + uint8_t type_aks_pos_hyst; + + /* ! sensor detection threshold. */ + uint8_t threshold; + + /* ! sensor from channel */ + /* ! for keys: from channel = to channel */ + /* ! rotors : Top channel */ + /* ! sliders : Left most channel */ + /* ! NB:only need to_channel for keys in rotors/sliders build */ + uint8_t from_channel; + /* ! FOR KEY's: this is unused, */ + /* ! rotors: Bottom left channel */ + /* ! sliders: Middle channel */ + uint8_t to_channel; + /* ! index into array of rotor/slider values. */ + uint8_t index; +} +sensor_t; + +/* ! Global sensor configuration info. */ +typedef struct tag_touch_global_param_t { + /* ! Sensor detect integration (DI) limit. */ + uint8_t di; + /* ! Sensor away from touch drifet rate */ + uint8_t atch_drift_rate; + /* ! Sensor towards touch drift rate */ + uint8_t tch_drift_rate; + /* ! Sensor maximum on duration. */ + uint8_t max_on_duration; + /* ! Sensor drift hold time. */ + uint8_t drift_hold_time; + /* ! Sensor Positive recalibration delay. */ + uint8_t atch_recal_delay; + /* ! Sensor calibration dummy burst count. */ + uint8_t cal_seq_1_count; + /* ! Sensor calibration settling burst count. */ + uint8_t cal_seq_2_count; + /* ! Sensor recalibration threshold. */ + recal_threshold_t recal_threshold; + /* ! Touch Post processing mode */ + uint16_t touch_postprocess_mode; + /* auto oversamples Stability limit */ + uint16_t auto_os_sig_stability_limit; + /* auto tune Stability limit */ + uint16_t auto_tune_sig_stability_limit; + /*frequency tune in/out limit*/ + uint8_t auto_freq_tune_in_cnt; + /*noise sig stability limit*/ + uint16_t nm_sig_stability_limit; + /*noise threshold limit*/ + uint8_t nm_noise_limit; + /*global lockout configuration*/ + nm_sensor_lockout_t nm_enable_sensor_lock_out; + /*lockout count down cycles */ + uint8_t nm_lockout_countdown; +#if defined(_SAMC21_) || defined(_SAMC20_) || defined(_SAML22_) + /*charge_share_delay parameter for the PTC*/ + uint8_t charge_share_delay; + #endif +} +touch_global_param_t; + +/* ! Touch Filter Callback data type. */ +typedef struct tag_touch_filter_data_t { + /* ! Length of the measured signal values list. */ + uint16_t num_channel_signals; + /* ! Pointer to measured signal values for each channel. */ + uint16_t *p_channel_signals; +} +touch_filter_data_t; + +/* ! Touch Measured data type. */ +typedef volatile struct tag_touch_measure_data_t { + /* ! Flag set by touch_xxcap_measure_complete_callback() function when + * a fresh Touch */ + /* ! status is available. */ + volatile uint8_t measurement_done_touch; + /* ! Status of Touch measurement. */ + touch_acq_status_t acq_status; + /* ! Length of the measured signal values list. */ + uint16_t num_channel_signals; + /* ! Pointer to measured signal values for each channel. */ + uint16_t *p_channel_signals; + /* ! Length of the measured reference values list. */ + uint16_t num_channel_references; + /* ! Pointer to reference values for each channel. */ + uint16_t *p_channel_references; + /* ! Number of sensor state bytes. */ + uint8_t num_sensor_states; + /* ! Pointer to Touch Status of each sensor. */ + uint8_t *p_sensor_states; + /* ! Length of the Rotor and Slider position values list. */ + uint8_t num_rotor_slider_values; + /* ! Pointer to Rotor and Slider position values. */ + uint8_t *p_rotor_slider_values; + /* ! Length of the sensors data list. */ + uint16_t num_sensors; + /* ! Calibrated compensation capacitance values. */ + uint16_t *p_cc_calibration_vals; + /* ! Pointer to Sensor data. */ + sensor_t *p_sensors; + /* !Pointer to noise status */ + uint8_t *p_sensor_noise_status; + /*the noise level or value of each channel*/ + uint16_t *p_nm_ch_noise_val; + /*!Pointer to Moisture Status */ + uint8_t *p_sensor_mois_status; + /*!Pointer to Auto-Oversamples Status */ + uint8_t *p_auto_os_status; + /* flag indicates cc calibration process */ + uint8_t cc_calib_status_flag; + + +} +touch_measure_data_t; + +/* ! Touch sensor configuration type. */ +typedef struct tag_touch_selfcap_param_t { + /* ! Sensor detection threshold. */ + aks_group_t aks_group; + /* ! Sensor detection threshold. */ + threshold_t detect_threshold; + /* ! Sensor detection hysteresis. */ + hysteresis_t detect_hysteresis; + /* ! Sensor position resolution. This is valid only for a Rotor or + * Slider. */ + resolution_t position_resolution; +} +touch_selfcap_param_t; + +/* ! Touch sensor configuration type. */ +typedef struct tag_touch_mutlcap_param_t { + /* ! Sensor detection threshold. */ + aks_group_t aks_group; + /* ! Sensor detection threshold. */ + threshold_t detect_threshold; + /* ! Sensor detection hysteresis. */ + hysteresis_t detect_hysteresis; + + /* ! Sensor position resolution. This is valid only for a Rotor or + * Slider. */ + resolution_t position_resolution; + + /* ! Sensor position hysteresis. This is valid only for a Rotor or + * Slider. */ + /* ! Range of position_hysteresis value is from 0 to 7. */ + uint8_t position_hysteresis; +} +touch_mutlcap_param_t; +/* ! Mutual capacitance sensor acquisition parameter type. */ + typedef struct tag_touch_mutlcap_acq_param_t + { + /* !pointer to gain per node */ + gain_t *p_mutlcap_gain_per_node; + /* ! setup acquisition frequency mode */ + freq_mode_sel_t touch_mutlcap_freq_mode; + /* ! PTC clock prescaler value */ + prsc_div_sel_t *mutlcap_ptc_prsc; + /* ! PTC series resistor value */ + rsel_val_t *mutlcap_resistor_value; + /* ! PTC clock prescaler value during CC cal */ + prsc_div_sel_t *mutlcap_ptc_prsc_cc_cal; + /* ! PTC series resistor value during CC cal */ + rsel_val_t *mutlcap_resistor_value_cc_cal; + /* !pointer to acquisition frequency settings */ + freq_hop_sel_t *p_mutlcap_hop_freqs; + /* ! filter level */ + filter_level_t *p_mutlcap_filter_level; + /* ! auto oversampling */ + auto_os_t *p_mutlcap_auto_os; +} +touch_mutlcap_acq_param_t; + +/* ! Self capacitance sensor acquisition parameter type. */ + typedef struct tag_touch_selfcap_acq_param_t + { + /* !pointer to gain per node */ + gain_t *p_selfcap_gain_per_node; + /* ! enable/disable noise counter-measures */ + freq_mode_sel_t touch_selfcap_freq_mode; + /* ! PTC clock prescaler value */ + prsc_div_sel_t *selfcap_ptc_prsc; + /* ! PTC sense resistor value */ + rsel_val_t *selfcap_resistor_value; + /* ! PTC clock prescaler value during CC cal */ + prsc_div_sel_t *selfcap_ptc_prsc_cc_cal; + /* ! PTC sense resistor value during CC cal */ + rsel_val_t *selfcap_resistor_value_cc_cal; + /* !pointer to hop frequency options */ + freq_hop_sel_t *p_selfcap_hop_freqs; + /* ! filter level */ + filter_level_t *p_selfcap_filter_level; + /* ! auto oversampling */ + auto_os_t *p_selfcap_auto_os; +} +touch_selfcap_acq_param_t; + + //! QTouch capacitance sensor acquisition parameter type. + typedef struct tag_touch_acq_param_t + { + + //!pointer to gain per node + gain_t *p_gain_per_node; + + //! enable/disable noise counter-measures + freq_mode_sel_t touch_freq_mode; + + //! PTC clock prescaler value + prsc_div_sel_t *ptc_prsc; + + //! PTC sense resistor value + rsel_val_t *resistor_value; + + //! PTC clock prescaler value during CC cal + prsc_div_sel_t *ptc_prsc_cc_cal; + + //! PTC sense resistor value during CC cal + rsel_val_t *resistor_value_cc_cal; + + //!pointer to hop frequency options + freq_hop_sel_t *p_hop_freqs; + + //! filter level + filter_level_t *p_filter_level; + + //! auto oversampling + auto_os_t *p_auto_os; + + } + touch_acq_param_t; + +typedef struct tag_tlib_init_fn_ptr_t +{ + void (*auto_tune_init) (void *, uint8_t,uint16_t); + uint32_t (*auto_os_init) (void *, uint16_t); + void (*lk_chk) (void *, uint16_t, uint16_t); + void (*enable_aks)(void); +} tlib_init_fn_ptr; + + +/* ! Self Capacitance configuration input. */ +typedef struct tag_touch_selfcap_config_t { + /* ! Number of channels using self cap method. */ + uint16_t num_channels; + /* ! Number of sensors using self cap method. */ + uint16_t num_sensors; + /* ! Number of rotors/sliders using self cap method. */ + uint8_t num_rotors_and_sliders; + /* !global sensor configuration info. */ + touch_global_param_t global_param; + /* !sensor acquisition param info. */ + touch_selfcap_acq_param_t touch_selfcap_acq_param; + /* ! Pointer to data block buffer. */ + uint8_t *p_data_blk; + /* ! size of data block buffer */ + uint16_t buffer_size; + /* !pointer to xy nodes */ + uint32_t *p_selfcap_y_nodes; + /* !enable or disable quick re-burst response time feature */ + uint8_t self_quick_reburst_enable; + /* !callback to process sensor signal values before post-processing */ + void (*filter_callback)(touch_filter_data_t *p_filter_data); + /* !frequency hop : auto tunuing enable* / */ + uint8_t enable_freq_auto_tune; + /* !Noise measurement enable */ + uint8_t enable_noise_measurement; + /* !Memory Allocation Buffer */ + uint8_t nm_buffer_cnt; + /* !Self moisture tolerance enable flag */ + uint8_t self_mois_tlrnce_enable; /* % moisture tolerance% */ + /* !Self cap moisture number of groups */ + uint8_t self_mois_groups; + /* Moisture Quick Re burst Enable */ + uint8_t self_mois_quick_reburst_enable; + /* Self_cap PTC GPIO state*/ + ptc_gpio_state_t self_ptc_gpio_state; + /*Feature list pointers*/ + tlib_init_fn_ptr tlib_feature_list; +} +touch_selfcap_config_t; +/* ! Mutual Capacitance configuration input. */ +typedef struct tag_touch_mutlcap_config_t { + /* ! Number of channels using mutual cap method. */ + uint16_t num_channels; + /* ! Number of sensors using mutual cap method. */ + uint16_t num_sensors; + /* ! Number of rotors/sliders using mutual cap method. */ + uint8_t num_rotors_and_sliders; + /* !global sensor configuration info. */ + touch_global_param_t global_param; + //!sensor acquisition param info. + touch_mutlcap_acq_param_t touch_mutlcap_acq_param; + /* ! Pointer to data block buffer. */ + uint8_t *p_data_blk; + /* ! size of data block buffer */ + uint16_t buffer_size; + /* !pointer to xy nodes */ + uint32_t *p_mutlcap_xy_nodes; + /* !enable or disable quick reburst response time feature */ + uint8_t mutl_quick_reburst_enable; + /* !callback to process sensor signal values before post-processing */ + void (*filter_callback)(touch_filter_data_t *p_filter_data); + //!frequency hop : auto tunuing enable*/ + uint8_t enable_freq_auto_tune; + //!Noise measurement enable + uint8_t enable_noise_measurement; + //!Memory Allocation Buffer + uint8_t nm_buffer_cnt; + //! mutual cap moisture tolerance enable flag + uint8_t mutl_mois_tlrnce_enable; + //! mutual cap moisture number of groups + uint8_t mutl_mois_groups; + /* Moisture Quick Re burst Enable */ + uint8_t mutl_mois_quick_reburst_enable; + /* Mutual_cap PTC GPIO state*/ + ptc_gpio_state_t mutl_ptc_gpio_state; + /*Feature list pointers*/ + tlib_init_fn_ptr tlib_feature_list; +} +touch_mutlcap_config_t; + +/* ! Touch Input Configuration type. */ +typedef struct tag_touch_config_t { + /* ! Mutual cap configuration pointer. */ + touch_mutlcap_config_t *p_mutlcap_config; + /* ! Self cap configuration pointer. */ + touch_selfcap_config_t *p_selfcap_config; + /* ! PTC ISR priority level */ + uint8_t ptc_isr_lvl; +} +touch_config_t; + +/* ! Touch library information type. */ +typedef volatile struct tag_touch_info_t { + /* ! Touch Library state specific to method. */ + touch_lib_state_t tlib_state; + + /* ! Number of channels in use, irrespective of the corresponding Sensor + * being disabled or enabled. */ + uint16_t num_channels_in_use; + + /* ! Number of sensors in use, irrespective of the Sensor being disabled + * or enabled. */ + uint16_t num_sensors_in_use; + + /* ! Number of rotor sliders in use, irrespective of the Rotor/Slider + * being disabled or enabled. */ + uint8_t num_rotors_sliders_in_use; + /* ! Max possible number of channels per rotor or slider. */ + uint8_t max_channels_per_rotor_slider; +} +touch_info_t; + +/* ! Touch library version information type. */ +typedef struct tag_touch_libver_info_t { + /* ! Chip ID */ + uint32_t chip_id; + /* ! Product ID */ + uint16_t product_id; + /* ! Touch Library version + Bits[12:15] Reserved + Bits[8:11] TLIB_MAJOR_VERSION + Bits[4:7] TLIB_MINOR_VERSION + Bits[0:3] TLIB_PATCH_VERSION */ + uint16_t fw_version; +} +touch_libver_info_t; + +/*---------------------------------------------------------------------------- + * Structure Declarations + * + * + *----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- + * global variables + * + * + *----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +* extern variables +* ----------------------------------------------------------------------------*/ +/* ! Touch Library Timing info. */ +extern touch_time_t touch_time; + +/* ! QTouch Library acquisition status */ +//extern touch_acq_status_t touch_acq_status; + +/** + CC calibration signal limit variables, Range 0-1024 +*/ +extern uint16_t cc_cal_max_signal_limit; +extern uint16_t cc_cal_min_signal_limit; + +/* ! Self capacitance method measured data pointer. */ +extern touch_measure_data_t *p_selfcap_measure_data; + +/* ! Mutual capacitance method measured data pointer. */ +extern touch_measure_data_t *p_mutlcap_measure_data; + +extern void (* volatile touch_suspend_app_cb) (void); + +/* ! Wake up touch status Lib to Application */ +extern uint8_t wake_up_touch; + +/* ! Low power mode status from Library to Application*/ +extern uint8_t low_power_mode; + +extern int32_t mois_mutl_grp_delta_arr[] ; +extern int32_t mois_mutl_grp_adj_delta_arr[] ; +extern uint8_t mois_lock_global_mutl; + +extern int32_t mois_self_grp_delta_arr[] ; +extern int32_t mois_self_grp_adj_delta_arr[] ; +extern uint8_t mois_lock_global_self; +/*---------------------------------------------------------------------------- +* static variables +* ----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- + * prototypes + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Touch Helper API. + */ +/* ! @{ */ + +/*! \brief This API is used to initialize and configure the Touch Library. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_sensors_init(void); + +/*! \brief This API is used to perform a Single Capacitance measurement + * using the Touch Library. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_sensors_measure(void); +/*! \brief This API is used to update Timing info for Touch Library. + * Typical usage of this API is inside a periodic timer ISR. + * \return touch_ret_t: Touch Library Error status. + */ +void touch_sensors_update_time(void); + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * Touch API + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Touch API. + */ +/* ! @{ */ + +/** + * touch_ret_t touch_mutlcap_sensors_init( touch_config_t *p_touch_config); + */ +#define touch_mutlcap_sensors_init( y ) \ + touch_mutlcap_sensors_init_with_rs_table((y), \ + PRIV_MUTLCAP_RS_TABLE_INIT, PRIV_NM_TABLE_INIT,PRIV_FREQ_AUTO_TUNE_CHK,PRIV_MOIS_TOLERANCE_CHK); + +/** + * touch_ret_t touch_selfcap_sensors_init( touch_config_t *p_touch_config); + */ +#define touch_selfcap_sensors_init( y ) \ + touch_selfcap_sensors_init_with_rs_table((y), \ + PRIV_SELFCAP_RS_TABLE_INIT, PRIV_NM_TABLE_INIT,PRIV_FREQ_AUTO_TUNE_CHK,PRIV_MOIS_TOLERANCE_CHK); + +/*! \brief This API is used to initialize the Touch Library with Mutual cap + * method + * pin, register and sensor configuration provided by the user. + * + * \param p_touch_config: Pointer to Touch configuration structure. + * \return touch_ret_t: Touch Library Error status. + */ + touch_ret_t touch_mutlcap_sensors_init_with_rs_table (touch_config_t *p_touch_config, + void (*rs_table_init)(void), void (*nm_table_init)(void), void (*frequency_auto_tune_en)(touch_acq_t ), void (*mois_tole_en)(void *)); + +/*! \brief This API is used to initialize the Touch Library with Self cap method + * pin, register and sensor configuration provided by the user. + * + * \param p_touch_config: Pointer to Touch configuration structure. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_selfcap_sensors_init_with_rs_table( touch_config_t *p_touch_config, + void (*rs_table_init)(void), void (*nm_table_init)(void), void (*frequency_auto_tune_en)(touch_acq_t ), void (*mois_tole_en)(void *)); +/*! \brief This API can be used to configure a sensor of type key, rotor or slider. + * + * \param sensor_type: can be of type key, rotor or slider. + * \param from_channel: the first channel in the slider sensor. + * \param to_channel: the last channel in the slider sensor. + * \param aks_group: which AKS group (if any) the sensor is in. + * \param detect_threshold: the sensor detection threshold. + * \param detect_hysteresis: the sensor detection hysteresis value. + * \param position_resolution: the resolution of the reported position value. + * \param p_sensor_id: The sensor id value of the configured sensor is updated + * by the Touch Library. + * \note Range of position_hysteresis value is from 0 to 7. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_sensor_config( + sensor_type_t sensor_type, + channel_t from_channel, + channel_t to_channel, + aks_group_t aks_group, + threshold_t detect_threshold, + hysteresis_t detect_hysteresis, + resolution_t position_resolution, + uint8_t position_hysteresis, + sensor_id_t *p_sensor_id); + +touch_ret_t touch_selfcap_sensor_config( + sensor_type_t sensor_type, + channel_t from_channel, + channel_t to_channel, + aks_group_t aks_group, + threshold_t detect_threshold, + hysteresis_t detect_hysteresis, + resolution_t position_resolution, + sensor_id_t *p_sensor_id); + +/*! \brief This API is used to calibrate the sensors for the first time before + * starting a Touch measurement. This API can also be used to force + * calibration of sensors when any of the Touch sensor parameters are + * changed during runtime. + * + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_sensors_calibrate(auto_tune_type_t ); + +touch_ret_t touch_selfcap_sensors_calibrate(auto_tune_type_t ); + +/*! \brief This API can be used to start a Touch measurement. + * + * \param current_time_ms: Current time in millisecond. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_selfcap_sensors_measure( + touch_current_time_t current_time_ms, + touch_acq_mode_t selfcap_acq_mode, + void (*measure_complete_callback) (void)); + +touch_ret_t touch_mutlcap_sensors_measure( + touch_current_time_t current_time_ms, + touch_acq_mode_t mutlcap_acq_mode, + void (*measure_complete_callback) (void)); + +/*! \brief This API is used to start a event triggered based mutualcap low power sensor + * measure + * + * \param sensor_id: Low power sensor id + * \return touch_ret_t: Touch Library Error status. + */ + +touch_ret_t touch_mutual_lowpower_sensor_enable_event_measure(sensor_id_t sensor_id); + + +/*! \brief This API is used to start a event triggered based selfcap low power sensor + * measure + * + * \param sensor_id: Low power sensor id + * \return touch_ret_t: Touch Library Error status. + */ + +touch_ret_t touch_self_lowpower_sensor_enable_event_measure(sensor_id_t sensor_id); + +/*! \brief This API can be used retrieve the delta value corresponding to + * a given sensor. + * + * \param sensor_id: The sensor id for which delta value is being seeked. + * \param p_delta: Pointer to the delta variable to be updated by the Touch + * Library. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_sensor_get_delta( + sensor_id_t sensor_id, + touch_delta_t *p_delta); + +touch_ret_t touch_selfcap_sensor_get_delta( + sensor_id_t sensor_id, + touch_delta_t *p_delta); + +/*! \brief This API can be used to set the sensor configuration parameters. + * \param p_sensor_id: The sensor id for which configuration parameter + * information is being set. + * \param p_touch_sensor_param: The touch sensor parameter structure that will + * be used by the Touch Library to update. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_sensor_update_config( sensor_id_t sensor_id, + touch_mutlcap_param_t *p_touch_sensor_param ); + +touch_ret_t touch_selfcap_sensor_update_config(sensor_id_t sensor_id, + touch_selfcap_param_t *p_touch_sensor_param ); + +/*! \brief This API can be used to read back the sensor configuration + * parameters. + * \param p_sensor_id: The sensor id for which configuration parameter + * information is being set. + * \param p_touch_sensor_param: The touch sensor parameter structure that will + * be updated by the Touch Library. + * \return touch_ret_t: Touch Library Error status. + */ + +touch_ret_t touch_mutlcap_sensor_get_config(sensor_id_t sensor_id, + touch_mutlcap_param_t *p_touch_sensor_param ); + +touch_ret_t touch_selfcap_sensor_get_config(sensor_id_t sensor_id, + touch_selfcap_param_t *p_touch_sensor_param ); + +/*! \brief This API can be used to set the sensor configuration parameters. + * \param p_sensor_id: The sensor id for which configuration parameter + * information is being set. + * \param p_touch_sensor_param: The touch sensor parameter structure that will + * be used by the Touch Library to update. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_sensor_update_acq_config( + touch_mutlcap_acq_param_t *p_touch_mutlcap_acq_param ); + +touch_ret_t touch_selfcap_sensor_update_acq_config( + touch_selfcap_acq_param_t *p_touch_selfcap_acq_param ); + +/*! \brief This API can be used to read back the sensor configuration parameters. + * \param p_sensor_id: The sensor id for which configuration parameter + * information is being set. + * \param p_touch_sensor_param: The touch sensor parameter structure that will + * be updated by the Touch Library. + * \return touch_ret_t: Touch Library Error status. + */ + +touch_ret_t touch_mutlcap_sensor_get_acq_config( + touch_mutlcap_acq_param_t *p_touch_mutlcap_acq_param ); + +touch_ret_t touch_selfcap_sensor_get_acq_config( + touch_selfcap_acq_param_t *p_touch_selfcap_acq_param ); + +/*! \brief This API can be used to update the global parameter + * + * \param p_global_param: The pointer to global sensor configuration. + * + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_update_global_param( + touch_global_param_t *p_global_param ); + +touch_ret_t touch_selfcap_update_global_param( + touch_global_param_t *p_global_param ); + +/*! \brief This API can be used to read back the global parameter + * + * \param p_global_param: The pointer to global sensor configuration. + * + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_get_global_param( touch_global_param_t *p_global_param ); + +touch_ret_t touch_selfcap_get_global_param(touch_global_param_t *p_global_param ); + +/*! \brief This API can be used to disable any sensor + * + * \param sensor_id: Sensor number which needs to be disabled + * + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_sensor_disable(sensor_id_t sensor_id); + +touch_ret_t touch_selfcap_sensor_disable(sensor_id_t sensor_id); +/*! \brief This API can be used to reenable a disabled sensor + * + * \param p_global_param: Sensor number which needs to be reenabled + * \param no_calib if set then the calibration for the sensor is not forced. + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_sensor_reenable(sensor_id_t sensor_id, uint8_t no_calib); + +touch_ret_t touch_selfcap_sensor_reenable(sensor_id_t sensor_id, uint8_t no_calib); + +/*---------------------------------------------------------------------------- +* Moisture Tolerance API +*-----------------------------------------------------------------------------*/ + + + /*! \brief This API can be used to Configures self cap sensor in the moisture group and multi touch group + * + * \param snsr_id: The sensor id for which configuration parameter + * information is being set + * \param mois_grpid: Moisture group id + * \param mltch_grpid: Multi touch group id + * \return touch_ret: Return the touch status + */ + +touch_ret_t touch_selfcap_cnfg_mois_mltchgrp(sensor_id_t snsr_id,moisture_grp_t mois_grpid,mltch_grp_t mltch_grpid); + + /*! \brief This API can be used to Configures mutual cap sensor in the moisture group and multi touch group + * + * \param snsr_id: The sensor id for which configuration parameter + * information is being set + * \param mois_grpid: Moisture group id + * \param mltch_grpid: Multi touch group id + * \return touch_ret: Return the touch status + */ + +touch_ret_t touch_mutlcap_cnfg_mois_mltchgrp(sensor_id_t snsr_id,moisture_grp_t mois_grpid,mltch_grp_t mltch_grpid); + + + /*! \brief This API can be used to Configures self cap moisture group sensor moisuture lock and system + * moisture lock threshold + * \param mois_grpid: Moisture group id + * + * \param snsr_threshold: sensor moisuture lock threshold + * \param system_threshold: system moisuture lock threshold + * \return touch_ret: Return the touch status + */ + +touch_ret_t touch_selfcap_cnfg_mois_threshold(moisture_grp_t mois_grpid,mois_snsr_threshold_t snsr_threshold,mois_system_threshold_t system_threshold); + + + /*! \brief This API can be used to Configures mutual cap moisture group sensor moisuture lock and system + * moisture lock threshold + * \param mois_grpid: Moisture group id + * + * \param snsr_threshold: sensor moisuture lock threshold + * \param system_threshold: system moisuture lock threshold + * \return touch_ret: Return the touch status + */ + +touch_ret_t touch_mutlcap_cnfg_mois_threshold(moisture_grp_t mois_grpid,mois_snsr_threshold_t snsr_threshold,mois_system_threshold_t system_threshold); + + /*! \brief This API can be used to enable mutual cap moisture tolerance feature + * + * \param void: + * + * \return touch_ret: Return the touch status + */ + +touch_ret_t touch_mutlcap_mois_tolrnce_enable(void); + + /*! \brief This API can be used to disable mutual cap moisture tolerance feature + * + * \param void: + * + * \return touch_ret: Return the touch status + */ + +touch_ret_t touch_mutlcap_mois_tolrnce_disable(void); + + /*! \brief This API can be used to enable mutual cap moisture tolerance quick reburst feature + * + * \param void: + * + * \return touch_ret: Return the touch status + */ +touch_ret_t touch_mutlcap_mois_tolrnce_quick_reburst_enable(void); + + + /*! \brief This API can be used to disable mutual cap moisture tolerance quick reburst feature + * + * \param void: + * + * \return touch_ret: Return the touch status + */ +touch_ret_t touch_mutlcap_mois_tolrnce_quick_reburst_disable(void); + + /*! \brief This API can be used to enable self cap moisture tolerance feature + * + * \param void: + * + * \return touch_ret: Return the touch status + */ + + +touch_ret_t touch_selfcap_mois_tolrnce_enable(void); + +/*! \brief This API can be used to disable self cap moisture tolerance feature + * + * \param void: + * + * \return touch_ret: Return the touch status + */ + +touch_ret_t touch_selfcap_mois_tolrnce_disable(void); + + /*! \brief This API can be used to enable self cap moisture tolerance quick re-burst feature + * + * \param void: + * + * \return touch_ret: Return the touch status + */ +touch_ret_t touch_selfcap_mois_tolrnce_quick_reburst_enable(void); + +/*! \brief This API can be used to disable self cap moisture tolerance quick re-burst feature + * + * \param void: + * + * \return touch_ret: Return the touch status + */ + +touch_ret_t touch_selfcap_mois_tolrnce_quick_reburst_disable(void); + +/*! \brief This API can be used to get the Touch Library configuration + * + * \param p_touch_info: Pointer to the Touch info data structure that will be + * updated by the Touch Library. + * + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_get_libinfo( touch_info_t *p_touch_info ); + +touch_ret_t touch_selfcap_get_libinfo(touch_info_t *p_touch_info ); + +/*! \brief This API is to suspend the PTC controller to shutdown through the library + * To suspend attach the touch_supend_cb function before calling the suspend call. + * + * For more details check the user manual + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_suspend_ptc(void); +/*! \brief This API is to resume the PTC controller through the library + * To suspend attach the touch_supend_cb function before calling the suspend call. + * + * For more details check the user manual + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_resume_ptc(void); +/*! \brief De-init function for the Mutual cap nodes. + * + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_mutlcap_sensors_deinit(void); +/*! \brief De-init function for the Self cap nodes. + * + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_selfcap_sensors_deinit(void); + + +/*! \brief This API can be used to get the Touch Library configuration + * + * \param p_touch_info: Pointer to the Touch Library Version info data structure that will be + * updated by the Touch Library. + * + * \return touch_ret_t: Touch Library Error status. + */ +touch_ret_t touch_library_get_version_info( touch_libver_info_t *p_touch_libver_info_t ); + +/* ! @} */ + +/*---------------------------------------------------------------------------- +* Extern Objects +* ----------------------------------------------------------------------------*/ + +/*! \name Touch Library internal use functions. + */ +/* ! @{ */ + +/*! \brief This is an extern function of the Touch Library. + * This function is NOT to be used by the user. + */ +void touch_mutlcap_rs_table_init(void); +void touch_selfcap_rs_table_init(void); + +void mutual_auto_tuning(void *, uint8_t, uint16_t); +void self_auto_tuning(void *, uint8_t, uint16_t); + +uint32_t auto_os_resolve(void *, uint16_t ); +void chk_frequency_auto_tune(touch_acq_t u32_acq_type); +void touch_noise_mit_table_init(void); +void lk_chk (void * , uint16_t ,uint16_t); +void chk_moisture_tolerance(void *); +void enable_aks(void); +/* ! @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* TOUCH_API_PTC_H */ diff --git a/atmel-samd/asf_conf/conf_clocks.h b/atmel-samd/asf_conf/conf_clocks.h index 2d1b1ea4eb..372608f749 100644 --- a/atmel-samd/asf_conf/conf_clocks.h +++ b/atmel-samd/asf_conf/conf_clocks.h @@ -140,11 +140,11 @@ # define CONF_CLOCK_GCLK_0_OUTPUT_ENABLE false /* Configure GCLK generator 1 */ -# define CONF_CLOCK_GCLK_1_ENABLE false -# define CONF_CLOCK_GCLK_1_RUN_IN_STANDBY false -# define CONF_CLOCK_GCLK_1_CLOCK_SOURCE SYSTEM_CLOCK_SOURCE_XOSC32K +# define CONF_CLOCK_GCLK_1_ENABLE true +# define CONF_CLOCK_GCLK_1_RUN_IN_STANDBY true +# define CONF_CLOCK_GCLK_1_CLOCK_SOURCE SYSTEM_CLOCK_SOURCE_OSC8M # define CONF_CLOCK_GCLK_1_PRESCALER 1 -# define CONF_CLOCK_GCLK_1_OUTPUT_ENABLE true +# define CONF_CLOCK_GCLK_1_OUTPUT_ENABLE false /* Configure GCLK generator 2 (RTC) */ # define CONF_CLOCK_GCLK_2_ENABLE false diff --git a/atmel-samd/boards/cplay_m0_flash/touch.h b/atmel-samd/boards/cplay_m0_flash/touch.h new file mode 100644 index 0000000000..416ca1f427 --- /dev/null +++ b/atmel-samd/boards/cplay_m0_flash/touch.h @@ -0,0 +1,613 @@ +/* This source file is part of the ATMEL QTouch Library 5.0.8 */ + +/***************************************************************************** + * + * \file + * + * \brief This file contains the SAMD QTouch Library pin, register and + * sensors configuration options for Capacitive Touch acquisition using + * the PTC module. + * + * + * - Userguide: QTouch Library Peripheral Touch Controller User Guide. + * - Support email: www.atmel.com/design-support/ + * + * + * Copyright (c) 2013-2015 Atmel Corporation. All rights reserved. + * + * \asf_license_start + * + * 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 + * + ******************************************************************************/ + +#ifndef TOUCH_CONFIG_SAMD_H +#define TOUCH_CONFIG_SAMD_H + +/*---------------------------------------------------------------------------- + * Self Cap method enable/disable. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Enable/Disable Self/Mutual Capacitance method. + */ +#define DEF_TOUCH_SELFCAP (1) +#define DEF_TOUCH_MUTLCAP (0) + +/*---------------------------------------------------------------------------- + * PTC module clock and interrupt level configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * PTC Module clock configuration. + * Before using the QTouch library API, the PTC module clock generator source + * should be configured appropriately. The PTC module clock can be generated + * using any of the eight generic clock generators (GCLK0-GCLK7). The + *associated + * generic clock multiplexer should be configured such that the PTC module clock + * is set to 4MHz. Refer touch_configure_ptc_clock API in touch.c for more + * information. + */ + +/** + * PTC Module interrupt level. + * The Nested Vectored Interrupt Controller (NVIC) in the SAMD supports + * four different priority levels. The priority level of the PTC end of + * conversion ISR, used within QTouch library can be chosen based on application + * requirements in order to accommodate time critical operations. + * Range: 0u (Highest priority) to 3u (Lowest prioirity) + * For more details refer to the Cortex-M0 Technical Reference Manual. + */ +#define DEF_TOUCH_PTC_ISR_LVL (2u) + +/*---------------------------------------------------------------------------- + * Self Cap method pin configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap touch channel selected. + * The capacitance measurement is done sequentially in the order in which + * the touch channel (nodes) are specified below. Touch channel numbering + * follows the order in which Y lines are specified. Capacitance measurement + * is done sequentially in the order in which touch channel nodes are specified. + */ +#define DEF_SELFCAP_LINES Y(2),Y(3),Y(4),Y(5),Y(8),Y(9),Y(14),Y(15) + +/*---------------------------------------------------------------------------- + * Self Cap method channel and sensor configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap number of channels. + * Specify the number of Self Cap touch channels to be used by the Touch + *Library. + * A key is formed used one touch channel. A rotor or slider can be formed + * using 3 touch channels. + * Range: 1u to 16u. + */ +#define DEF_SELFCAP_NUM_CHANNELS (8) //Total number of channels + +/** + * Self Cap number of Sensors. + * Specify the number of Self Cap touch sensors to be used by the Touch Library. + * A sensor is either a key, rotor or slider. + * Example configuration: If the configuration has 6 keys (a key is formed + * using one Sensor Pin), one rotor (a Self Cap rotor is formed using 3 Sensor + * Pins) and one slider (a Self Cap slider is formed using 3 Sensor Pins), then + * the number of sensors is 6 key + 1 rotor + 1 slider = 8 sensors. + * Range: 1u to 16u. + */ +#define DEF_SELFCAP_NUM_SENSORS (8) //Total number of sensors + +/** + * Self Cap number of Rotors and Sliders. + * Specify the total number of Self Cap Rotors and Sliders to be used by + * the Touch Library. The number of Rotors and Sliders mentioned here is part + * of + * the Total number of sensors specified in the QT_NUM_SENSORS macro. When + * no rotors or slider are required, specify a value of 0u. + * Range: 0u to 8u. + */ +#define DEF_SELFCAP_NUM_ROTORS_SLIDERS (0) //Number of rotor sliders + +/*---------------------------------------------------------------------------- + * Self Cap method aquisition parameters. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap filter level setting. + * The filter level setting controls the number of samples taken + * to resolve each acquisition. A higher filter level setting provides + * improved signal to noise ratio under noisy conditions, while + * increasing the total time for measurement resulting in increased + * power consumption and response time. Refer filter_level_t in + *touch_api_ptc.h + * Range: FILTER_LEVEL_1 (one sample) to FILTER_LEVEL_64 ( 64 samples). + */ +#define DEF_SELFCAP_FILTER_LEVEL_PER_NODE FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16 + + /* + * SelfCap Auto OS function can be enabled/disabled + * 1u = Self cap Auto OS functionality is enabled + * 0u = Self Auto OS functionality is disabled + */ + #define DEF_SELFCAP_AUTO_OS_ENABLE 0u + +/** + * Self Cap auto oversample setting. + * Auto oversample controls the automatic oversampling of sensor channels when + * unstable signals are detected with the default setting of ?Filter level?. + * Enabling Auto oversample results in 'Filter level' x 'Auto Oversample' number + * of samples taken on the corresponding sensor channel when an unstable signal + * is observed. In a case where ?Filter level? is set to FILTER_LEVEL_4 and + * ?Auto Oversample? is set to AUTO_OS_4, 4 oversamples are taken with stable + * signal values and 16 oversamples are taken when unstable signal is detected. + * Refer auto_os_t in touch_api_ptc.h + * Range: 0 to 7 + 0 --> AUTO_OS_DISABLE, 1 --> AUTO_OS_2, + 2 --> AUTO_OS_4, 3 --> AUTO_OS_8, + 4 --> AUTO_OS_16, 5 --> AUTO_OS_32, + 6 --> AUTO_OS_64, 7 --> AUTO_OS_128 + */ +#define DEF_SELFCAP_AUTO_OS_PER_NODE AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * The signal stability limit level is set to auto trigger oversamples on noise presence. + * It is recommended to keep this setting close to the lowest sensor detect threshold of + * the system and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_AUTO_OS_SIGNAL_STABILITY_LIMIT 20 +/** + * Self Cap gain per touch channel. + * Gain is applied on a per-channel basis to allow a scaling-up of the touch + * sensitivity on contact. + * Note: delta on touch contact, not the resting signal which is measured on + * each sensor. + * Refer gain_t in touch_api_ptc.h + * Range:GAIN_1 (no scaling) to GAIN_32 (scale-up by 32) + */ +#define DEF_SELFCAP_GAIN_PER_NODE GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1 + +/*---------------------------------------------------------------------------- + * Tuning for Noise performance, touch response time and Power consumption. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Refer QTouch Library Peripheral Touch Controller User Guide for detailed + * information on tuning for Noise performance, touch response time and Power + * consumption + */ + +/** + * For best noise performance, set - + * - DEF_SELFCAP_FREQ_MODE to FREQ_MODE_HOP + * - DEF_SELFCAP_SENSE_RESISTOR to RSEL_VAL_100 + * - use AUTO_TUNE_PRSC input to touch_xxcap_sensors_calibrate() API in touch.c + * Based on the type of noise, FREQ_MODE_SPREAD or FREQ_MODE_SPREAD_MEDIAN can + * also be used. + * + * For best power consumption, set - + * - DEF_SELFCAP_FREQ_MODE to FREQ_MODE_NONE + * - DEF_SELFCAP_CLK_PRESCALE to PRSC_DIV_SEL_1 + * - use AUTO_TUNE_RSEL input to touch_xxcap_sensors_calibrate() API in touch.c + * + */ + +/** + * Self Cap acquisition frequency mode. + * + * FREQ_MODE_HOP: + * When frequency mode hopping option is selected, the PTC runs a + * frequency hopping cycle with subsequent measurements done using + * the three PTC acquisition frequency delay settings as specified in + * DEF_SELFCAP_HOP_FREQS. + * + * FREQ_MODE_SPREAD: + * When frequency mode spread spectrum option is selected, the PTC + * runs with spread spectrum enabled for jittered delay based + * acquisition. + * + * FREQ_MODE_SPREAD_MEDIAN: + * When frequency mode spread spectrum median option is selected, + * the PTC runs with spread spectrum enabled. In this case, an + * additional software median filter is applied to the measured + * signal values. + * + * FREQ_MODE_NONE: + * When frequency mode none option is selected, the PTC runs at + * constant speed. This mode is suited for best power consumption. + */ +#define DEF_SELFCAP_FREQ_MODE FREQ_MODE_NONE + +/** + * PTC acquisition frequency delay setting. + * + * Specify three frequency hop delay settings. + * + * The PTC acquisition frequency is dependent on the Generic clock + * input to PTC and PTC clock pre-scaler setting. This delay setting + * inserts "n" PTC clock cycles between consecutive measurements on + * a given sensor, thereby changing the PTC acquisition frequency. + * FREQ_HOP_SEL_1 setting inserts 0 PTC clock cycle between consecutive + * measurements. FREQ_HOP_SEL_16 setting inserts 15 PTC clock cycles. + * Hence, higher delay setting will increase the total time taken for + * capacitance measurement on a given sensor as compared to a lower + * delay setting. + * + * A desired setting can be used to avoid noise around the same frequency + * as the acquisition frequency. + * Range: FREQ_HOP_SEL_1 to FREQ_HOP_SEL_16 + */ +#define DEF_SELFCAP_HOP_FREQS FREQ_HOP_SEL_1,FREQ_HOP_SEL_2,FREQ_HOP_SEL_3 + +/** + * Self cap PTC clock pre-scalar. + * PTC clock prescale setting. Refer touch_configure_ptc_clock() API in + *touch.c + * Example: if Generic clock input to PTC = 4MHz, then: + * PRSC_DIV_SEL_1 sets PTC Clock to 4MHz + * PRSC_DIV_SEL_2 sets PTC Clock to 2MHz + * PRSC_DIV_SEL_4 sets PTC Clock to 1MHz + * PRSC_DIV_SEL_8 sets PTC Clock to 500KHz + */ +#define DEF_SELFCAP_CLK_PRESCALE_PER_NODE PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1 + +/** + * PTC series resistor setting. For Mutual cap mode, this series + * resistor is switched internally on the Y-pin. For Self cap mode, + * the series resistor is switched internally on the Sensor pin. + * + * Example: + * RSEL_VAL_0 sets internal series resistor to 0ohms. + * RSEL_VAL_20 sets internal series resistor to 20Kohms. + * RSEL_VAL_50 sets internal series resistor to 50Kohms. + * RSEL_VAL_100 sets internal series resistor to 100Kohms. + */ +#define DEF_SELFCAP_SENSE_RESISTOR_PER_NODE + +/** + This is the default auto tune mode selection. + This is used in the AUTO calibration routine, + The allowed values are 0 to 2 + 0 --> DEF_AUTO_TUNE_NONE if Auto tune is not required + 1 --> DEF_AUTO_TUNE_PRSC if the Pre-scaler needs to be tuned + 2 --> DEF_AUTO_TUNE_RSEL if the Series resistor needs to be tuned + 3 --> DEF_AUTO_TUNE_CSD this is available only in C2x devices, +DEF_AUTO_TUNE_CSD tunes internal Charge sharing delay for proper charge transfer +*/ +#define DEF_SELF_AUTO_TUNE_VALUE 2 +/*---------------------------------------------------------------------------- + * Self Cap method sensor global parameters. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Self Cap Sensor measurement interval. + * Speicify period in milliseconds. Example, DEF_TOUCH_MEASUREMENT_PERIOD_MS + *50u + * will perform measurement on touch sensors every 50msec. + */ +#define DEF_TOUCH_MEASUREMENT_PERIOD_MS 20u + + +/** + * Self Cap Sensor detect integration (DI) limit. + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_DI 4u + + +/** + * Self Cap Sensor towards touch drift rate. + * Units: 200ms + * Default value: 20 = 4 seconds. + * Range: 1u to 127u. + */ +#define DEF_SELFCAP_TCH_DRIFT_RATE 20u + + +/** + * Self Cap Sensor away from touch drift rate. + * Units: 200ms + * Default value: 5u = 1 second. + * Range: 1u to 127u. + */ +#define DEF_SELFCAP_ATCH_DRIFT_RATE 5u + + +/** + * Self Cap Sensor maximum ON time duration. + * Units: 200ms (Example: a value 5u indicated Max ON duration of 1 second.) + * Default value: 0 (No maximum ON time limit). + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_MAX_ON_DURATION 0u + + +/** + * Self Cap Sensor drift hold time. + * Units: 200ms + * Default value: 20 (hold off drifting for 4 seconds after leaving detect). + * Range: 1u to 255u. + */ +#define DEF_SELFCAP_DRIFT_HOLD_TIME 20u + + +/** + * Self Cap Sensor away from touch recalibration delay. + * Default value: 10. + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_ATCH_RECAL_DELAY 10u + + +/** Self Cap Sensor away from touch recalibration threshold. + * Default: RECAL_50 (recalibration threshold = 50% of detection threshold). + * Range: refer recal_threshold_t enum in touch_api_ptc.h. + */ +#define DEF_SELFCAP_ATCH_RECAL_THRESHOLD 0u + + +/** Self Cap Sensor post-processing mode. + * Default: TOUCH_LIBRARY_DRIVEN. + * Range: TOUCH_LIBRARY_DRIVEN or TOUCH_APPLN_DRIVEN refer in touch_api_ptc.h. + */ +#define DEF_SELFCAP_TOUCH_POSTPROCESS_MODE TOUCH_APPLN_DRIVEN + +/* ! @} */ + +/*---------------------------------------------------------------------------- +* Self Cap method moisture detection feature parameters. +* +* +*----------------------------------------------------------------------------*/ +/*! \name Mutual Cap method moisture feature parameters. +* Refer the Touch Library User guide for more information on these parameters. +*/ +/* ! @{ */ + +/** + * Enable or disable moisture detection feature. + */ +#define DEF_SELFCAP_MOIS_TOLERANCE_ENABLE (0u) + +/** + * Enable or disable quick re-burst feature within a given moisture group. + * When enabled, if within a given moisture group, when any sensor is touched, + * repeated measurements are done only that sensor to resolve detect integration or de-bounce. + * When disabled, if within a given moisture group, when any sensor is touched, repeated measurements are done + * on all sensors within the moisture group to resolve detect integration or de-bounce. + * It is recommended to enable this feature for best touch response time. + */ +#define DEF_SELFCAP_MOIS_QUICK_REBURST_ENABLE (1u) + +/** + * Self cap number of moisture groups + */ +#define DEF_SELFCAP_NUM_MOIS_GROUPS (0u) + + +/**State of the Self Cap PTC pins. + * Default: GND_WHEN_NOT_MEASURED (PTC pins are pulled to GND when they are not bursted). + * Range: ptc_gpio_state_t enum in touch_api_ptc.h. + */ +#define DEF_SELFCAP_PTC_GPIO_STATE (PULLHIGH_WHEN_NOT_MEASURED) + +/* ! @} */ + +/* + * AKS grouping function can be enabled/disabled + * 1u = AKS grouping functionality is enabled + * 0u = AKS grouping functionality is disabled + */ +#define DEF_SELFCAP_AKS_ENABLE (0u) + +/* + * Charge share delay indicates the number of additional charge cycles that are inserted within a capacitance measurement cycle + * to ensure full charging of the touch sensor. + * The CSD value is dependent on the sensor capacitance along with the series resistor on the Y line. + * When manual tuning is done, the CSD value for the sensor with largest combination of capacitance along with series resistance + * should be considered. + * allowed Range 0-255 + */ +#define DEF_SELF_CAP_CSD_VALUE 0 + +/*---------------------------------------------------------------------------- + * Self Cap method noise measurement & lockout. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Noise measurement enable/disable + * If configured as 1, noise measurement will be enabled + * If configured as 0, noise measurement will be disabled + */ +#define DEF_SELFCAP_NOISE_MEAS_ENABLE (0u) + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * Any noise level over and above the noise signal stability limit contributes to the Noise limit. + * It is recommended to keep this setting close to the lowest sensor detect threshold of + * the system and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_NOISE_MEAS_SIGNAL_STABILITY_LIMIT 10u + +/** + * The noise limit specifies the limit to the total noise accumulated over the noise buffer count. + * If the accumulated noise exceeds the noise limit, then lockout is triggered. + * Range: 1 to 255 + */ +#define DEF_SELFCAP_NOISE_LIMIT 12u + +/** + * Noise buffer count + * Selection of buffer count for noise calculation. + * Defines the buffer limit for internal noise measurement + * Range: 3 to 10 (select value N + 1, here N nmber of samples) + * if N = 4 then set DEF_NM_BUFFER_CNT 5u ->> (N + 1). + * Default : 5u + */ +#define DEF_SELFCAP_NOISE_MEAS_BUFFER_CNT (5u) + +/** + * Mutual cap method : noisy Sensor lockout settings. + * 0u: single sensor lockout. + * 1u: Global sensor lockout. + * 2u : No lockout + * Range : 0 to 2 + */ +#define DEF_SELFCAP_LOCKOUT_SEL 0 + +/** + * Mutual cap Lockout count down + * If the sensor signal is moves from noisy to a good condition and stays there + * for a count value higher than the max count defined by user, sensor is + *declared as stable + * Range: 1 to 255 + */ +#define DEF_SELFCAP_LOCKOUT_CNTDOWN 10 + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * Self Cap method Frequency auto tune. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Freq auto tune enable/disable ( applicable only to freq_hop mode) + * If configured as 1, Freq auto tune will be enabled + * If configured as 0, Freq auto tune will be disabled + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE (0u) + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * A signal stability limit level is set to auto tune acquisition frequency on noise presence. + * It is recommended to keep this setting close to the lowest sensor detect threshold of the system + * and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_SIGNAL_STABILITY_LIMIT 10u + +/** + * Frequency Auto tune-in count + * If the channel signal is noisy for a count value higher than the max count + *defined by user, + * system will trigger auto tune + * Range: 1 to 255 + * Note : this appies only for FREQ_MODE_HOP + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_IN_CNT 12 + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * Self Cap Callback functions. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Callback functions. + */ +/* ! @{ */ + +/** + * Self Cap Filter callback function. + * A filter callback (when not NULL) is called by the Touch Library each time + * a new set of Signal values are available. + * An Example filter callback function prototype. + * void touch_filter_callback( touch_filter_data_t *p_filter_data ); + */ +#define DEF_SELFCAP_FILTER_CALLBACK (NULL) + +/** + * Touch Application Error Handler Enable or Disable; while(1) is used to trap errors when it is enabled + * If configured as 1, Touch Application Error Handler will be enabled + * If configured as 0, Touch Application Error Handler will be disabled + * Default: Touch Application Error Handler will be disabled + */ +#define DEF_TOUCH_APP_ERR_HANDLER 0 + + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * QDebug debug communication parameters. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name QDebug debug communication parameters. + */ +/* ! @{ */ + +#define DEF_TOUCH_QDEBUG_ENABLE_SELFCAP + +#define DEF_TOUCH_QDEBUG_ENABLE 0u + + +/* ! @} */ + +#endif /* TOUCH_CONFIG_SAMD_H */ diff --git a/atmel-samd/boards/feather_m0_flash/touch.h b/atmel-samd/boards/feather_m0_flash/touch.h new file mode 100644 index 0000000000..a3fd1184ff --- /dev/null +++ b/atmel-samd/boards/feather_m0_flash/touch.h @@ -0,0 +1,613 @@ +/* This source file is part of the ATMEL QTouch Library 5.0.8 */ + +/***************************************************************************** + * + * \file + * + * \brief This file contains the SAMD QTouch Library pin, register and + * sensors configuration options for Capacitive Touch acquisition using + * the PTC module. + * + * + * - Userguide: QTouch Library Peripheral Touch Controller User Guide. + * - Support email: www.atmel.com/design-support/ + * + * + * Copyright (c) 2013-2015 Atmel Corporation. All rights reserved. + * + * \asf_license_start + * + * 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 + * + ******************************************************************************/ + +#ifndef TOUCH_CONFIG_SAMD_H +#define TOUCH_CONFIG_SAMD_H + +/*---------------------------------------------------------------------------- + * Self Cap method enable/disable. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Enable/Disable Self/Mutual Capacitance method. + */ +#define DEF_TOUCH_SELFCAP (1) +#define DEF_TOUCH_MUTLCAP (0) + +/*---------------------------------------------------------------------------- + * PTC module clock and interrupt level configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * PTC Module clock configuration. + * Before using the QTouch library API, the PTC module clock generator source + * should be configured appropriately. The PTC module clock can be generated + * using any of the eight generic clock generators (GCLK0-GCLK7). The + *associated + * generic clock multiplexer should be configured such that the PTC module clock + * is set to 4MHz. Refer touch_configure_ptc_clock API in touch.c for more + * information. + */ + +/** + * PTC Module interrupt level. + * The Nested Vectored Interrupt Controller (NVIC) in the SAMD supports + * four different priority levels. The priority level of the PTC end of + * conversion ISR, used within QTouch library can be chosen based on application + * requirements in order to accommodate time critical operations. + * Range: 0u (Highest priority) to 3u (Lowest prioirity) + * For more details refer to the Cortex-M0 Technical Reference Manual. + */ +#define DEF_TOUCH_PTC_ISR_LVL (2u) + +/*---------------------------------------------------------------------------- + * Self Cap method pin configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap touch channel selected. + * The capacitance measurement is done sequentially in the order in which + * the touch channel (nodes) are specified below. Touch channel numbering + * follows the order in which Y lines are specified. Capacitance measurement + * is done sequentially in the order in which touch channel nodes are specified. + */ +#define DEF_SELFCAP_LINES Y(0),Y(2),Y(3),Y(8),Y(14),Y(15) + +/*---------------------------------------------------------------------------- + * Self Cap method channel and sensor configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap number of channels. + * Specify the number of Self Cap touch channels to be used by the Touch + *Library. + * A key is formed used one touch channel. A rotor or slider can be formed + * using 3 touch channels. + * Range: 1u to 16u. + */ +#define DEF_SELFCAP_NUM_CHANNELS (6) //Total number of channels + +/** + * Self Cap number of Sensors. + * Specify the number of Self Cap touch sensors to be used by the Touch Library. + * A sensor is either a key, rotor or slider. + * Example configuration: If the configuration has 6 keys (a key is formed + * using one Sensor Pin), one rotor (a Self Cap rotor is formed using 3 Sensor + * Pins) and one slider (a Self Cap slider is formed using 3 Sensor Pins), then + * the number of sensors is 6 key + 1 rotor + 1 slider = 8 sensors. + * Range: 1u to 16u. + */ +#define DEF_SELFCAP_NUM_SENSORS (6) //Total number of sensors + +/** + * Self Cap number of Rotors and Sliders. + * Specify the total number of Self Cap Rotors and Sliders to be used by + * the Touch Library. The number of Rotors and Sliders mentioned here is part + * of + * the Total number of sensors specified in the QT_NUM_SENSORS macro. When + * no rotors or slider are required, specify a value of 0u. + * Range: 0u to 8u. + */ +#define DEF_SELFCAP_NUM_ROTORS_SLIDERS (0) //Number of rotor sliders + +/*---------------------------------------------------------------------------- + * Self Cap method aquisition parameters. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap filter level setting. + * The filter level setting controls the number of samples taken + * to resolve each acquisition. A higher filter level setting provides + * improved signal to noise ratio under noisy conditions, while + * increasing the total time for measurement resulting in increased + * power consumption and response time. Refer filter_level_t in + *touch_api_ptc.h + * Range: FILTER_LEVEL_1 (one sample) to FILTER_LEVEL_64 ( 64 samples). + */ +#define DEF_SELFCAP_FILTER_LEVEL_PER_NODE FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16 + + /* + * SelfCap Auto OS function can be enabled/disabled + * 1u = Self cap Auto OS functionality is enabled + * 0u = Self Auto OS functionality is disabled + */ + #define DEF_SELFCAP_AUTO_OS_ENABLE 0u + +/** + * Self Cap auto oversample setting. + * Auto oversample controls the automatic oversampling of sensor channels when + * unstable signals are detected with the default setting of ?Filter level?. + * Enabling Auto oversample results in 'Filter level' x 'Auto Oversample' number + * of samples taken on the corresponding sensor channel when an unstable signal + * is observed. In a case where ?Filter level? is set to FILTER_LEVEL_4 and + * ?Auto Oversample? is set to AUTO_OS_4, 4 oversamples are taken with stable + * signal values and 16 oversamples are taken when unstable signal is detected. + * Refer auto_os_t in touch_api_ptc.h + * Range: 0 to 7 + 0 --> AUTO_OS_DISABLE, 1 --> AUTO_OS_2, + 2 --> AUTO_OS_4, 3 --> AUTO_OS_8, + 4 --> AUTO_OS_16, 5 --> AUTO_OS_32, + 6 --> AUTO_OS_64, 7 --> AUTO_OS_128 + */ +#define DEF_SELFCAP_AUTO_OS_PER_NODE AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * The signal stability limit level is set to auto trigger oversamples on noise presence. + * It is recommended to keep this setting close to the lowest sensor detect threshold of + * the system and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_AUTO_OS_SIGNAL_STABILITY_LIMIT 20 +/** + * Self Cap gain per touch channel. + * Gain is applied on a per-channel basis to allow a scaling-up of the touch + * sensitivity on contact. + * Note: delta on touch contact, not the resting signal which is measured on + * each sensor. + * Refer gain_t in touch_api_ptc.h + * Range:GAIN_1 (no scaling) to GAIN_32 (scale-up by 32) + */ +#define DEF_SELFCAP_GAIN_PER_NODE GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1 + +/*---------------------------------------------------------------------------- + * Tuning for Noise performance, touch response time and Power consumption. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Refer QTouch Library Peripheral Touch Controller User Guide for detailed + * information on tuning for Noise performance, touch response time and Power + * consumption + */ + +/** + * For best noise performance, set - + * - DEF_SELFCAP_FREQ_MODE to FREQ_MODE_HOP + * - DEF_SELFCAP_SENSE_RESISTOR to RSEL_VAL_100 + * - use AUTO_TUNE_PRSC input to touch_xxcap_sensors_calibrate() API in touch.c + * Based on the type of noise, FREQ_MODE_SPREAD or FREQ_MODE_SPREAD_MEDIAN can + * also be used. + * + * For best power consumption, set - + * - DEF_SELFCAP_FREQ_MODE to FREQ_MODE_NONE + * - DEF_SELFCAP_CLK_PRESCALE to PRSC_DIV_SEL_1 + * - use AUTO_TUNE_RSEL input to touch_xxcap_sensors_calibrate() API in touch.c + * + */ + +/** + * Self Cap acquisition frequency mode. + * + * FREQ_MODE_HOP: + * When frequency mode hopping option is selected, the PTC runs a + * frequency hopping cycle with subsequent measurements done using + * the three PTC acquisition frequency delay settings as specified in + * DEF_SELFCAP_HOP_FREQS. + * + * FREQ_MODE_SPREAD: + * When frequency mode spread spectrum option is selected, the PTC + * runs with spread spectrum enabled for jittered delay based + * acquisition. + * + * FREQ_MODE_SPREAD_MEDIAN: + * When frequency mode spread spectrum median option is selected, + * the PTC runs with spread spectrum enabled. In this case, an + * additional software median filter is applied to the measured + * signal values. + * + * FREQ_MODE_NONE: + * When frequency mode none option is selected, the PTC runs at + * constant speed. This mode is suited for best power consumption. + */ +#define DEF_SELFCAP_FREQ_MODE FREQ_MODE_NONE + +/** + * PTC acquisition frequency delay setting. + * + * Specify three frequency hop delay settings. + * + * The PTC acquisition frequency is dependent on the Generic clock + * input to PTC and PTC clock pre-scaler setting. This delay setting + * inserts "n" PTC clock cycles between consecutive measurements on + * a given sensor, thereby changing the PTC acquisition frequency. + * FREQ_HOP_SEL_1 setting inserts 0 PTC clock cycle between consecutive + * measurements. FREQ_HOP_SEL_16 setting inserts 15 PTC clock cycles. + * Hence, higher delay setting will increase the total time taken for + * capacitance measurement on a given sensor as compared to a lower + * delay setting. + * + * A desired setting can be used to avoid noise around the same frequency + * as the acquisition frequency. + * Range: FREQ_HOP_SEL_1 to FREQ_HOP_SEL_16 + */ +#define DEF_SELFCAP_HOP_FREQS FREQ_HOP_SEL_1,FREQ_HOP_SEL_2,FREQ_HOP_SEL_3 + +/** + * Self cap PTC clock pre-scalar. + * PTC clock prescale setting. Refer touch_configure_ptc_clock() API in + *touch.c + * Example: if Generic clock input to PTC = 4MHz, then: + * PRSC_DIV_SEL_1 sets PTC Clock to 4MHz + * PRSC_DIV_SEL_2 sets PTC Clock to 2MHz + * PRSC_DIV_SEL_4 sets PTC Clock to 1MHz + * PRSC_DIV_SEL_8 sets PTC Clock to 500KHz + */ +#define DEF_SELFCAP_CLK_PRESCALE_PER_NODE PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1 + +/** + * PTC series resistor setting. For Mutual cap mode, this series + * resistor is switched internally on the Y-pin. For Self cap mode, + * the series resistor is switched internally on the Sensor pin. + * + * Example: + * RSEL_VAL_0 sets internal series resistor to 0ohms. + * RSEL_VAL_20 sets internal series resistor to 20Kohms. + * RSEL_VAL_50 sets internal series resistor to 50Kohms. + * RSEL_VAL_100 sets internal series resistor to 100Kohms. + */ +#define DEF_SELFCAP_SENSE_RESISTOR_PER_NODE RSEL_VAL_0,RSEL_VAL_0,RSEL_VAL_0,RSEL_VAL_0,RSEL_VAL_0,RSEL_VAL_0 + +/** + This is the default auto tune mode selection. + This is used in the AUTO calibration routine, + The allowed values are 0 to 2 + 0 --> DEF_AUTO_TUNE_NONE if Auto tune is not required + 1 --> DEF_AUTO_TUNE_PRSC if the Pre-scaler needs to be tuned + 2 --> DEF_AUTO_TUNE_RSEL if the Series resistor needs to be tuned + 3 --> DEF_AUTO_TUNE_CSD this is available only in C2x devices, +DEF_AUTO_TUNE_CSD tunes internal Charge sharing delay for proper charge transfer +*/ +#define DEF_SELF_AUTO_TUNE_VALUE 2 +/*---------------------------------------------------------------------------- + * Self Cap method sensor global parameters. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Self Cap Sensor measurement interval. + * Speicify period in milliseconds. Example, DEF_TOUCH_MEASUREMENT_PERIOD_MS + *50u + * will perform measurement on touch sensors every 50msec. + */ +#define DEF_TOUCH_MEASUREMENT_PERIOD_MS 20u + + +/** + * Self Cap Sensor detect integration (DI) limit. + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_DI 4u + + +/** + * Self Cap Sensor towards touch drift rate. + * Units: 200ms + * Default value: 20 = 4 seconds. + * Range: 1u to 127u. + */ +#define DEF_SELFCAP_TCH_DRIFT_RATE 20u + + +/** + * Self Cap Sensor away from touch drift rate. + * Units: 200ms + * Default value: 5u = 1 second. + * Range: 1u to 127u. + */ +#define DEF_SELFCAP_ATCH_DRIFT_RATE 5u + + +/** + * Self Cap Sensor maximum ON time duration. + * Units: 200ms (Example: a value 5u indicated Max ON duration of 1 second.) + * Default value: 0 (No maximum ON time limit). + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_MAX_ON_DURATION 0u + + +/** + * Self Cap Sensor drift hold time. + * Units: 200ms + * Default value: 20 (hold off drifting for 4 seconds after leaving detect). + * Range: 1u to 255u. + */ +#define DEF_SELFCAP_DRIFT_HOLD_TIME 20u + + +/** + * Self Cap Sensor away from touch recalibration delay. + * Default value: 10. + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_ATCH_RECAL_DELAY 10u + + +/** Self Cap Sensor away from touch recalibration threshold. + * Default: RECAL_50 (recalibration threshold = 50% of detection threshold). + * Range: refer recal_threshold_t enum in touch_api_ptc.h. + */ +#define DEF_SELFCAP_ATCH_RECAL_THRESHOLD 0u + + +/** Self Cap Sensor post-processing mode. + * Default: TOUCH_LIBRARY_DRIVEN. + * Range: TOUCH_LIBRARY_DRIVEN or TOUCH_APPLN_DRIVEN refer in touch_api_ptc.h. + */ +#define DEF_SELFCAP_TOUCH_POSTPROCESS_MODE TOUCH_APPLN_DRIVEN + +/* ! @} */ + +/*---------------------------------------------------------------------------- +* Self Cap method moisture detection feature parameters. +* +* +*----------------------------------------------------------------------------*/ +/*! \name Mutual Cap method moisture feature parameters. +* Refer the Touch Library User guide for more information on these parameters. +*/ +/* ! @{ */ + +/** + * Enable or disable moisture detection feature. + */ +#define DEF_SELFCAP_MOIS_TOLERANCE_ENABLE (0u) + +/** + * Enable or disable quick re-burst feature within a given moisture group. + * When enabled, if within a given moisture group, when any sensor is touched, + * repeated measurements are done only that sensor to resolve detect integration or de-bounce. + * When disabled, if within a given moisture group, when any sensor is touched, repeated measurements are done + * on all sensors within the moisture group to resolve detect integration or de-bounce. + * It is recommended to enable this feature for best touch response time. + */ +#define DEF_SELFCAP_MOIS_QUICK_REBURST_ENABLE (1u) + +/** + * Self cap number of moisture groups + */ +#define DEF_SELFCAP_NUM_MOIS_GROUPS (0u) + + +/**State of the Self Cap PTC pins. + * Default: GND_WHEN_NOT_MEASURED (PTC pins are pulled to GND when they are not bursted). + * Range: ptc_gpio_state_t enum in touch_api_ptc.h. + */ +#define DEF_SELFCAP_PTC_GPIO_STATE (PULLHIGH_WHEN_NOT_MEASURED) + +/* ! @} */ + +/* + * AKS grouping function can be enabled/disabled + * 1u = AKS grouping functionality is enabled + * 0u = AKS grouping functionality is disabled + */ +#define DEF_SELFCAP_AKS_ENABLE (0u) + +/* + * Charge share delay indicates the number of additional charge cycles that are inserted within a capacitance measurement cycle + * to ensure full charging of the touch sensor. + * The CSD value is dependent on the sensor capacitance along with the series resistor on the Y line. + * When manual tuning is done, the CSD value for the sensor with largest combination of capacitance along with series resistance + * should be considered. + * allowed Range 0-255 + */ +#define DEF_SELF_CAP_CSD_VALUE 0 + +/*---------------------------------------------------------------------------- + * Self Cap method noise measurement & lockout. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Noise measurement enable/disable + * If configured as 1, noise measurement will be enabled + * If configured as 0, noise measurement will be disabled + */ +#define DEF_SELFCAP_NOISE_MEAS_ENABLE (0u) + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * Any noise level over and above the noise signal stability limit contributes to the Noise limit. + * It is recommended to keep this setting close to the lowest sensor detect threshold of + * the system and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_NOISE_MEAS_SIGNAL_STABILITY_LIMIT 10u + +/** + * The noise limit specifies the limit to the total noise accumulated over the noise buffer count. + * If the accumulated noise exceeds the noise limit, then lockout is triggered. + * Range: 1 to 255 + */ +#define DEF_SELFCAP_NOISE_LIMIT 12u + +/** + * Noise buffer count + * Selection of buffer count for noise calculation. + * Defines the buffer limit for internal noise measurement + * Range: 3 to 10 (select value N + 1, here N nmber of samples) + * if N = 4 then set DEF_NM_BUFFER_CNT 5u ->> (N + 1). + * Default : 5u + */ +#define DEF_SELFCAP_NOISE_MEAS_BUFFER_CNT (5u) + +/** + * Mutual cap method : noisy Sensor lockout settings. + * 0u: single sensor lockout. + * 1u: Global sensor lockout. + * 2u : No lockout + * Range : 0 to 2 + */ +#define DEF_SELFCAP_LOCKOUT_SEL 0 + +/** + * Mutual cap Lockout count down + * If the sensor signal is moves from noisy to a good condition and stays there + * for a count value higher than the max count defined by user, sensor is + *declared as stable + * Range: 1 to 255 + */ +#define DEF_SELFCAP_LOCKOUT_CNTDOWN 10 + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * Self Cap method Frequency auto tune. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Freq auto tune enable/disable ( applicable only to freq_hop mode) + * If configured as 1, Freq auto tune will be enabled + * If configured as 0, Freq auto tune will be disabled + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE (0u) + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * A signal stability limit level is set to auto tune acquisition frequency on noise presence. + * It is recommended to keep this setting close to the lowest sensor detect threshold of the system + * and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_SIGNAL_STABILITY_LIMIT 10u + +/** + * Frequency Auto tune-in count + * If the channel signal is noisy for a count value higher than the max count + *defined by user, + * system will trigger auto tune + * Range: 1 to 255 + * Note : this appies only for FREQ_MODE_HOP + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_IN_CNT 12 + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * Self Cap Callback functions. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Callback functions. + */ +/* ! @{ */ + +/** + * Self Cap Filter callback function. + * A filter callback (when not NULL) is called by the Touch Library each time + * a new set of Signal values are available. + * An Example filter callback function prototype. + * void touch_filter_callback( touch_filter_data_t *p_filter_data ); + */ +#define DEF_SELFCAP_FILTER_CALLBACK (NULL) + +/** + * Touch Application Error Handler Enable or Disable; while(1) is used to trap errors when it is enabled + * If configured as 1, Touch Application Error Handler will be enabled + * If configured as 0, Touch Application Error Handler will be disabled + * Default: Touch Application Error Handler will be disabled + */ +#define DEF_TOUCH_APP_ERR_HANDLER 0 + + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * QDebug debug communication parameters. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name QDebug debug communication parameters. + */ +/* ! @{ */ + +#define DEF_TOUCH_QDEBUG_ENABLE_SELFCAP + +#define DEF_TOUCH_QDEBUG_ENABLE 0u + + +/* ! @} */ + +#endif /* TOUCH_CONFIG_SAMD_H */ diff --git a/atmel-samd/boards/metro_m0_flash/touch.h b/atmel-samd/boards/metro_m0_flash/touch.h new file mode 100644 index 0000000000..5d194ea66b --- /dev/null +++ b/atmel-samd/boards/metro_m0_flash/touch.h @@ -0,0 +1,613 @@ +/* This source file is part of the ATMEL QTouch Library 5.0.8 */ + +/***************************************************************************** + * + * \file + * + * \brief This file contains the SAMD QTouch Library pin, register and + * sensors configuration options for Capacitive Touch acquisition using + * the PTC module. + * + * + * - Userguide: QTouch Library Peripheral Touch Controller User Guide. + * - Support email: www.atmel.com/design-support/ + * + * + * Copyright (c) 2013-2015 Atmel Corporation. All rights reserved. + * + * \asf_license_start + * + * 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 + * + ******************************************************************************/ + +#ifndef TOUCH_CONFIG_SAMD_H +#define TOUCH_CONFIG_SAMD_H + +/*---------------------------------------------------------------------------- + * Self Cap method enable/disable. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Enable/Disable Self/Mutual Capacitance method. + */ +#define DEF_TOUCH_SELFCAP (1) +#define DEF_TOUCH_MUTLCAP (0) + +/*---------------------------------------------------------------------------- + * PTC module clock and interrupt level configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * PTC Module clock configuration. + * Before using the QTouch library API, the PTC module clock generator source + * should be configured appropriately. The PTC module clock can be generated + * using any of the eight generic clock generators (GCLK0-GCLK7). The + *associated + * generic clock multiplexer should be configured such that the PTC module clock + * is set to 4MHz. Refer touch_configure_ptc_clock API in touch.c for more + * information. + */ + +/** + * PTC Module interrupt level. + * The Nested Vectored Interrupt Controller (NVIC) in the SAMD supports + * four different priority levels. The priority level of the PTC end of + * conversion ISR, used within QTouch library can be chosen based on application + * requirements in order to accommodate time critical operations. + * Range: 0u (Highest priority) to 3u (Lowest prioirity) + * For more details refer to the Cortex-M0 Technical Reference Manual. + */ +#define DEF_TOUCH_PTC_ISR_LVL (2u) + +/*---------------------------------------------------------------------------- + * Self Cap method pin configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap touch channel selected. + * The capacitance measurement is done sequentially in the order in which + * the touch channel (nodes) are specified below. Touch channel numbering + * follows the order in which Y lines are specified. Capacitance measurement + * is done sequentially in the order in which touch channel nodes are specified. + */ +#define DEF_SELFCAP_LINES Y(0),Y(2),Y(3),Y(4),Y(5),Y(8),Y(14),Y(15) + +/*---------------------------------------------------------------------------- + * Self Cap method channel and sensor configuration. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap number of channels. + * Specify the number of Self Cap touch channels to be used by the Touch + *Library. + * A key is formed used one touch channel. A rotor or slider can be formed + * using 3 touch channels. + * Range: 1u to 16u. + */ +#define DEF_SELFCAP_NUM_CHANNELS (8) //Total number of channels + +/** + * Self Cap number of Sensors. + * Specify the number of Self Cap touch sensors to be used by the Touch Library. + * A sensor is either a key, rotor or slider. + * Example configuration: If the configuration has 6 keys (a key is formed + * using one Sensor Pin), one rotor (a Self Cap rotor is formed using 3 Sensor + * Pins) and one slider (a Self Cap slider is formed using 3 Sensor Pins), then + * the number of sensors is 6 key + 1 rotor + 1 slider = 8 sensors. + * Range: 1u to 16u. + */ +#define DEF_SELFCAP_NUM_SENSORS (8) //Total number of sensors + +/** + * Self Cap number of Rotors and Sliders. + * Specify the total number of Self Cap Rotors and Sliders to be used by + * the Touch Library. The number of Rotors and Sliders mentioned here is part + * of + * the Total number of sensors specified in the QT_NUM_SENSORS macro. When + * no rotors or slider are required, specify a value of 0u. + * Range: 0u to 8u. + */ +#define DEF_SELFCAP_NUM_ROTORS_SLIDERS (0) //Number of rotor sliders + +/*---------------------------------------------------------------------------- + * Self Cap method aquisition parameters. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Self Cap filter level setting. + * The filter level setting controls the number of samples taken + * to resolve each acquisition. A higher filter level setting provides + * improved signal to noise ratio under noisy conditions, while + * increasing the total time for measurement resulting in increased + * power consumption and response time. Refer filter_level_t in + *touch_api_ptc.h + * Range: FILTER_LEVEL_1 (one sample) to FILTER_LEVEL_64 ( 64 samples). + */ +#define DEF_SELFCAP_FILTER_LEVEL_PER_NODE FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16,FILTER_LEVEL_16 + + /* + * SelfCap Auto OS function can be enabled/disabled + * 1u = Self cap Auto OS functionality is enabled + * 0u = Self Auto OS functionality is disabled + */ + #define DEF_SELFCAP_AUTO_OS_ENABLE 0u + +/** + * Self Cap auto oversample setting. + * Auto oversample controls the automatic oversampling of sensor channels when + * unstable signals are detected with the default setting of ?Filter level?. + * Enabling Auto oversample results in 'Filter level' x 'Auto Oversample' number + * of samples taken on the corresponding sensor channel when an unstable signal + * is observed. In a case where ?Filter level? is set to FILTER_LEVEL_4 and + * ?Auto Oversample? is set to AUTO_OS_4, 4 oversamples are taken with stable + * signal values and 16 oversamples are taken when unstable signal is detected. + * Refer auto_os_t in touch_api_ptc.h + * Range: 0 to 7 + 0 --> AUTO_OS_DISABLE, 1 --> AUTO_OS_2, + 2 --> AUTO_OS_4, 3 --> AUTO_OS_8, + 4 --> AUTO_OS_16, 5 --> AUTO_OS_32, + 6 --> AUTO_OS_64, 7 --> AUTO_OS_128 + */ +#define DEF_SELFCAP_AUTO_OS_PER_NODE AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE,AUTO_OS_DISABLE + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * The signal stability limit level is set to auto trigger oversamples on noise presence. + * It is recommended to keep this setting close to the lowest sensor detect threshold of + * the system and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_AUTO_OS_SIGNAL_STABILITY_LIMIT 20 +/** + * Self Cap gain per touch channel. + * Gain is applied on a per-channel basis to allow a scaling-up of the touch + * sensitivity on contact. + * Note: delta on touch contact, not the resting signal which is measured on + * each sensor. + * Refer gain_t in touch_api_ptc.h + * Range:GAIN_1 (no scaling) to GAIN_32 (scale-up by 32) + */ +#define DEF_SELFCAP_GAIN_PER_NODE GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1,GAIN_1 + +/*---------------------------------------------------------------------------- + * Tuning for Noise performance, touch response time and Power consumption. + * + * + *----------------------------------------------------------------------------*/ + +/** + * Refer QTouch Library Peripheral Touch Controller User Guide for detailed + * information on tuning for Noise performance, touch response time and Power + * consumption + */ + +/** + * For best noise performance, set - + * - DEF_SELFCAP_FREQ_MODE to FREQ_MODE_HOP + * - DEF_SELFCAP_SENSE_RESISTOR to RSEL_VAL_100 + * - use AUTO_TUNE_PRSC input to touch_xxcap_sensors_calibrate() API in touch.c + * Based on the type of noise, FREQ_MODE_SPREAD or FREQ_MODE_SPREAD_MEDIAN can + * also be used. + * + * For best power consumption, set - + * - DEF_SELFCAP_FREQ_MODE to FREQ_MODE_NONE + * - DEF_SELFCAP_CLK_PRESCALE to PRSC_DIV_SEL_1 + * - use AUTO_TUNE_RSEL input to touch_xxcap_sensors_calibrate() API in touch.c + * + */ + +/** + * Self Cap acquisition frequency mode. + * + * FREQ_MODE_HOP: + * When frequency mode hopping option is selected, the PTC runs a + * frequency hopping cycle with subsequent measurements done using + * the three PTC acquisition frequency delay settings as specified in + * DEF_SELFCAP_HOP_FREQS. + * + * FREQ_MODE_SPREAD: + * When frequency mode spread spectrum option is selected, the PTC + * runs with spread spectrum enabled for jittered delay based + * acquisition. + * + * FREQ_MODE_SPREAD_MEDIAN: + * When frequency mode spread spectrum median option is selected, + * the PTC runs with spread spectrum enabled. In this case, an + * additional software median filter is applied to the measured + * signal values. + * + * FREQ_MODE_NONE: + * When frequency mode none option is selected, the PTC runs at + * constant speed. This mode is suited for best power consumption. + */ +#define DEF_SELFCAP_FREQ_MODE FREQ_MODE_NONE + +/** + * PTC acquisition frequency delay setting. + * + * Specify three frequency hop delay settings. + * + * The PTC acquisition frequency is dependent on the Generic clock + * input to PTC and PTC clock pre-scaler setting. This delay setting + * inserts "n" PTC clock cycles between consecutive measurements on + * a given sensor, thereby changing the PTC acquisition frequency. + * FREQ_HOP_SEL_1 setting inserts 0 PTC clock cycle between consecutive + * measurements. FREQ_HOP_SEL_16 setting inserts 15 PTC clock cycles. + * Hence, higher delay setting will increase the total time taken for + * capacitance measurement on a given sensor as compared to a lower + * delay setting. + * + * A desired setting can be used to avoid noise around the same frequency + * as the acquisition frequency. + * Range: FREQ_HOP_SEL_1 to FREQ_HOP_SEL_16 + */ +#define DEF_SELFCAP_HOP_FREQS FREQ_HOP_SEL_1,FREQ_HOP_SEL_2,FREQ_HOP_SEL_3 + +/** + * Self cap PTC clock pre-scalar. + * PTC clock prescale setting. Refer touch_configure_ptc_clock() API in + *touch.c + * Example: if Generic clock input to PTC = 4MHz, then: + * PRSC_DIV_SEL_1 sets PTC Clock to 4MHz + * PRSC_DIV_SEL_2 sets PTC Clock to 2MHz + * PRSC_DIV_SEL_4 sets PTC Clock to 1MHz + * PRSC_DIV_SEL_8 sets PTC Clock to 500KHz + */ +#define DEF_SELFCAP_CLK_PRESCALE_PER_NODE PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1,PRSC_DIV_SEL_1 + +/** + * PTC series resistor setting. For Mutual cap mode, this series + * resistor is switched internally on the Y-pin. For Self cap mode, + * the series resistor is switched internally on the Sensor pin. + * + * Example: + * RSEL_VAL_0 sets internal series resistor to 0ohms. + * RSEL_VAL_20 sets internal series resistor to 20Kohms. + * RSEL_VAL_50 sets internal series resistor to 50Kohms. + * RSEL_VAL_100 sets internal series resistor to 100Kohms. + */ +#define DEF_SELFCAP_SENSE_RESISTOR_PER_NODE + +/** + This is the default auto tune mode selection. + This is used in the AUTO calibration routine, + The allowed values are 0 to 2 + 0 --> DEF_AUTO_TUNE_NONE if Auto tune is not required + 1 --> DEF_AUTO_TUNE_PRSC if the Pre-scaler needs to be tuned + 2 --> DEF_AUTO_TUNE_RSEL if the Series resistor needs to be tuned + 3 --> DEF_AUTO_TUNE_CSD this is available only in C2x devices, +DEF_AUTO_TUNE_CSD tunes internal Charge sharing delay for proper charge transfer +*/ +#define DEF_SELF_AUTO_TUNE_VALUE 2 +/*---------------------------------------------------------------------------- + * Self Cap method sensor global parameters. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Self Cap Sensor measurement interval. + * Speicify period in milliseconds. Example, DEF_TOUCH_MEASUREMENT_PERIOD_MS + *50u + * will perform measurement on touch sensors every 50msec. + */ +#define DEF_TOUCH_MEASUREMENT_PERIOD_MS 20u + + +/** + * Self Cap Sensor detect integration (DI) limit. + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_DI 4u + + +/** + * Self Cap Sensor towards touch drift rate. + * Units: 200ms + * Default value: 20 = 4 seconds. + * Range: 1u to 127u. + */ +#define DEF_SELFCAP_TCH_DRIFT_RATE 20u + + +/** + * Self Cap Sensor away from touch drift rate. + * Units: 200ms + * Default value: 5u = 1 second. + * Range: 1u to 127u. + */ +#define DEF_SELFCAP_ATCH_DRIFT_RATE 5u + + +/** + * Self Cap Sensor maximum ON time duration. + * Units: 200ms (Example: a value 5u indicated Max ON duration of 1 second.) + * Default value: 0 (No maximum ON time limit). + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_MAX_ON_DURATION 0u + + +/** + * Self Cap Sensor drift hold time. + * Units: 200ms + * Default value: 20 (hold off drifting for 4 seconds after leaving detect). + * Range: 1u to 255u. + */ +#define DEF_SELFCAP_DRIFT_HOLD_TIME 20u + + +/** + * Self Cap Sensor away from touch recalibration delay. + * Default value: 10. + * Range: 0u to 255u. + */ +#define DEF_SELFCAP_ATCH_RECAL_DELAY 10u + + +/** Self Cap Sensor away from touch recalibration threshold. + * Default: RECAL_50 (recalibration threshold = 50% of detection threshold). + * Range: refer recal_threshold_t enum in touch_api_ptc.h. + */ +#define DEF_SELFCAP_ATCH_RECAL_THRESHOLD 0u + + +/** Self Cap Sensor post-processing mode. + * Default: TOUCH_LIBRARY_DRIVEN. + * Range: TOUCH_LIBRARY_DRIVEN or TOUCH_APPLN_DRIVEN refer in touch_api_ptc.h. + */ +#define DEF_SELFCAP_TOUCH_POSTPROCESS_MODE TOUCH_APPLN_DRIVEN + +/* ! @} */ + +/*---------------------------------------------------------------------------- +* Self Cap method moisture detection feature parameters. +* +* +*----------------------------------------------------------------------------*/ +/*! \name Mutual Cap method moisture feature parameters. +* Refer the Touch Library User guide for more information on these parameters. +*/ +/* ! @{ */ + +/** + * Enable or disable moisture detection feature. + */ +#define DEF_SELFCAP_MOIS_TOLERANCE_ENABLE (0u) + +/** + * Enable or disable quick re-burst feature within a given moisture group. + * When enabled, if within a given moisture group, when any sensor is touched, + * repeated measurements are done only that sensor to resolve detect integration or de-bounce. + * When disabled, if within a given moisture group, when any sensor is touched, repeated measurements are done + * on all sensors within the moisture group to resolve detect integration or de-bounce. + * It is recommended to enable this feature for best touch response time. + */ +#define DEF_SELFCAP_MOIS_QUICK_REBURST_ENABLE (1u) + +/** + * Self cap number of moisture groups + */ +#define DEF_SELFCAP_NUM_MOIS_GROUPS (0u) + + +/**State of the Self Cap PTC pins. + * Default: GND_WHEN_NOT_MEASURED (PTC pins are pulled to GND when they are not bursted). + * Range: ptc_gpio_state_t enum in touch_api_ptc.h. + */ +#define DEF_SELFCAP_PTC_GPIO_STATE (PULLHIGH_WHEN_NOT_MEASURED) + +/* ! @} */ + +/* + * AKS grouping function can be enabled/disabled + * 1u = AKS grouping functionality is enabled + * 0u = AKS grouping functionality is disabled + */ +#define DEF_SELFCAP_AKS_ENABLE (0u) + +/* + * Charge share delay indicates the number of additional charge cycles that are inserted within a capacitance measurement cycle + * to ensure full charging of the touch sensor. + * The CSD value is dependent on the sensor capacitance along with the series resistor on the Y line. + * When manual tuning is done, the CSD value for the sensor with largest combination of capacitance along with series resistance + * should be considered. + * allowed Range 0-255 + */ +#define DEF_SELF_CAP_CSD_VALUE 0 + +/*---------------------------------------------------------------------------- + * Self Cap method noise measurement & lockout. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Noise measurement enable/disable + * If configured as 1, noise measurement will be enabled + * If configured as 0, noise measurement will be disabled + */ +#define DEF_SELFCAP_NOISE_MEAS_ENABLE (0u) + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * Any noise level over and above the noise signal stability limit contributes to the Noise limit. + * It is recommended to keep this setting close to the lowest sensor detect threshold of + * the system and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_NOISE_MEAS_SIGNAL_STABILITY_LIMIT 10u + +/** + * The noise limit specifies the limit to the total noise accumulated over the noise buffer count. + * If the accumulated noise exceeds the noise limit, then lockout is triggered. + * Range: 1 to 255 + */ +#define DEF_SELFCAP_NOISE_LIMIT 12u + +/** + * Noise buffer count + * Selection of buffer count for noise calculation. + * Defines the buffer limit for internal noise measurement + * Range: 3 to 10 (select value N + 1, here N nmber of samples) + * if N = 4 then set DEF_NM_BUFFER_CNT 5u ->> (N + 1). + * Default : 5u + */ +#define DEF_SELFCAP_NOISE_MEAS_BUFFER_CNT (5u) + +/** + * Mutual cap method : noisy Sensor lockout settings. + * 0u: single sensor lockout. + * 1u: Global sensor lockout. + * 2u : No lockout + * Range : 0 to 2 + */ +#define DEF_SELFCAP_LOCKOUT_SEL 0 + +/** + * Mutual cap Lockout count down + * If the sensor signal is moves from noisy to a good condition and stays there + * for a count value higher than the max count defined by user, sensor is + *declared as stable + * Range: 1 to 255 + */ +#define DEF_SELFCAP_LOCKOUT_CNTDOWN 10 + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * Self Cap method Frequency auto tune. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Global acquisition parameters. + * Refer the Touch Library User guide for more information on these parameters. + */ +/* ! @{ */ + +/** + * Freq auto tune enable/disable ( applicable only to freq_hop mode) + * If configured as 1, Freq auto tune will be enabled + * If configured as 0, Freq auto tune will be disabled + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE (0u) + +/** + * Stability limit is the variance in sensor signal value under noisy environment. + * A signal stability limit level is set to auto tune acquisition frequency on noise presence. + * It is recommended to keep this setting close to the lowest sensor detect threshold of the system + * and tune it further based on the noise. + * Range: 1 to 1000 + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_SIGNAL_STABILITY_LIMIT 10u + +/** + * Frequency Auto tune-in count + * If the channel signal is noisy for a count value higher than the max count + *defined by user, + * system will trigger auto tune + * Range: 1 to 255 + * Note : this appies only for FREQ_MODE_HOP + */ +#define DEF_SELFCAP_FREQ_AUTO_TUNE_IN_CNT 12 + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * Self Cap Callback functions. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name Self Cap Callback functions. + */ +/* ! @{ */ + +/** + * Self Cap Filter callback function. + * A filter callback (when not NULL) is called by the Touch Library each time + * a new set of Signal values are available. + * An Example filter callback function prototype. + * void touch_filter_callback( touch_filter_data_t *p_filter_data ); + */ +#define DEF_SELFCAP_FILTER_CALLBACK (NULL) + +/** + * Touch Application Error Handler Enable or Disable; while(1) is used to trap errors when it is enabled + * If configured as 1, Touch Application Error Handler will be enabled + * If configured as 0, Touch Application Error Handler will be disabled + * Default: Touch Application Error Handler will be disabled + */ +#define DEF_TOUCH_APP_ERR_HANDLER 0 + + +/* ! @} */ + +/*---------------------------------------------------------------------------- + * QDebug debug communication parameters. + * + * + *----------------------------------------------------------------------------*/ + +/*! \name QDebug debug communication parameters. + */ +/* ! @{ */ + +#define DEF_TOUCH_QDEBUG_ENABLE_SELFCAP + +#define DEF_TOUCH_QDEBUG_ENABLE 0u + + +/* ! @} */ + +#endif /* TOUCH_CONFIG_SAMD_H */ diff --git a/atmel-samd/common-hal/microcontroller/types.h b/atmel-samd/common-hal/microcontroller/types.h index b76700c930..96aea6a073 100644 --- a/atmel-samd/common-hal/microcontroller/types.h +++ b/atmel-samd/common-hal/microcontroller/types.h @@ -61,9 +61,11 @@ typedef struct { uint8_t pin; bool has_adc:1; enum adc_positive_input adc_input:7; - pin_timer_t primary_timer; // Mux E - pin_timer_t secondary_timer; // Mux F - pin_sercom_t sercom[NUM_SERCOMS_PER_PIN]; // Mux C and D + bool has_touch:1; + uint8_t touch_y_line:7; // 0 - 15. Assumed to be Y channel. + pin_timer_t primary_timer; + pin_timer_t secondary_timer; + pin_sercom_t sercom[NUM_SERCOMS_PER_PIN]; } mcu_pin_obj_t; #endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_TYPES_H__ diff --git a/atmel-samd/common-hal/nativeio/TouchIn.c b/atmel-samd/common-hal/nativeio/TouchIn.c new file mode 100644 index 0000000000..1e7158e22c --- /dev/null +++ b/atmel-samd/common-hal/nativeio/TouchIn.c @@ -0,0 +1,274 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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 "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.h" +#include "py/mphal.h" +#include "shared-bindings/nativeio/TouchIn.h" + +#include "tick.h" + +#include "QTouch/touch_api_ptc.h" + +#define DEF_SELFCAP_CAL_SEQ1_COUNT 8 +#define DEF_SELFCAP_CAL_SEQ2_COUNT 4 +#define DEF_SELFCAP_CC_CAL_CLK_PRESCALE_PER_NODE DEF_SELFCAP_CLK_PRESCALE_PER_NODE +#define DEF_SELFCAP_CC_CAL_SENSE_RESISTOR_PER_NODE DEF_SELFCAP_SENSE_RESISTOR_PER_NODE +#define DEF_SELFCAP_QUICK_REBURST_ENABLE 1u + +/** + * Mutual Cap sensors measured data pointer. + * Note: This pointer is initialized by the QTouch library once the + * touch_mutlcap_sensors_init API is called. */ +touch_measure_data_t *p_mutlcap_measure_data = NULL; + +/** + * Self Cap sensors measured data pointer. + * Note: This pointer is initialized by the QTouch library once the + * touch_selfcap_sensors_init API is called. */ +touch_measure_data_t *p_selfcap_measure_data = NULL; + + +/** + * Self Cap sensors data block provided as input to Touch library. + */ +static uint8_t selfcap_data_blk[PRIV_SELFCAP_DATA_BLK_SIZE]; + +/** + * Self Cap sensors Pins Info. + */ + +uint32_t selfcap_y_nodes[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_LINES}; + +filter_level_t selfcap_filter_level_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_FILTER_LEVEL_PER_NODE}; + +uint8_t selfcap_auto_os_per_node[DEF_SELFCAP_NUM_CHANNELS]= {DEF_SELFCAP_AUTO_OS_PER_NODE}; + +gain_t selfcap_gain_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_GAIN_PER_NODE}; + +rsel_val_t selfcap_resistor_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_SENSE_RESISTOR_PER_NODE}; + +prsc_div_sel_t selfcap_prsc_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_CLK_PRESCALE_PER_NODE}; + +prsc_div_sel_t selfcap_boot_prsc_per_node[DEF_SELFCAP_NUM_CHANNELS] = {DEF_SELFCAP_CC_CAL_CLK_PRESCALE_PER_NODE}; + +rsel_val_t selfcap_boot_resistor_per_node[DEF_SELFCAP_NUM_CHANNELS] = { DEF_SELFCAP_CC_CAL_SENSE_RESISTOR_PER_NODE}; + +freq_hop_sel_t selfcap_freq_hops[3u] = {DEF_SELFCAP_HOP_FREQS}; + +/** + * Self Cap Configuration structure provided as input to Touch Library. + * + * Note: Use the touch.h configuration header file to fill in + * the elements of this structure. DO NOT modify any of the input values + * directly in this structure. + */ +static touch_selfcap_config_t selfcap_config = { + DEF_SELFCAP_NUM_CHANNELS, /* Self Cap number of channels. */ + DEF_SELFCAP_NUM_SENSORS, /* Self Cap number of sensors. */ + DEF_SELFCAP_NUM_ROTORS_SLIDERS, /* Self Cap number of rotors and + * sliders. */ + + /* Self Cap GLOBAL SENSOR CONFIGURATION INFO. */ + { + DEF_SELFCAP_DI, /* uint8_t di; Sensor detect + * integration (DI) limit. */ + + /* Interchanging Negative and Positive Drift rate, since Signal + * increases on Touch. */ + DEF_SELFCAP_ATCH_DRIFT_RATE, /* uint8_t neg_drift_rate; + * Sensor + * negative drift rate. */ + DEF_SELFCAP_TCH_DRIFT_RATE, /* uint8_t pos_drift_rate; + * Sensor + * positive drift rate. */ + DEF_SELFCAP_MAX_ON_DURATION, /* uint8_t max_on_duration; + * Sensor + * maximum on duration. */ + DEF_SELFCAP_DRIFT_HOLD_TIME, /* uint8_t drift_hold_time; + * Sensor + * drift hold time. */ + DEF_SELFCAP_ATCH_RECAL_DELAY, /* uint8_t pos_recal_delay; + * Sensor positive recalibration + * delay. */ + + DEF_SELFCAP_CAL_SEQ1_COUNT, + DEF_SELFCAP_CAL_SEQ2_COUNT, + DEF_SELFCAP_ATCH_RECAL_THRESHOLD, /* recal_threshold_t + * recal_threshold; Sensor + * recalibration threshold. */ + DEF_SELFCAP_TOUCH_POSTPROCESS_MODE, + DEF_SELFCAP_AUTO_OS_SIGNAL_STABILITY_LIMIT, + DEF_SELFCAP_FREQ_AUTO_TUNE_SIGNAL_STABILITY_LIMIT, + DEF_SELFCAP_FREQ_AUTO_TUNE_IN_CNT, + DEF_SELFCAP_NOISE_MEAS_SIGNAL_STABILITY_LIMIT, /* signal + *stability */ + DEF_SELFCAP_NOISE_LIMIT, + DEF_SELFCAP_LOCKOUT_SEL, + DEF_SELFCAP_LOCKOUT_CNTDOWN, + #if ((SAMC20) || (SAMC21) || (SAML22)) + DEF_SELF_CAP_CSD_VALUE, + #endif + }, + { + selfcap_gain_per_node, /* Self Cap channel gain setting. */ + DEF_SELFCAP_FREQ_MODE, /* Self Cap noise counter measure + * enable/disable. */ + selfcap_prsc_per_node, + selfcap_resistor_per_node, + selfcap_boot_prsc_per_node, + selfcap_boot_resistor_per_node, + selfcap_freq_hops, + selfcap_filter_level_per_node, /* Self Cap filter level setting. */ + selfcap_auto_os_per_node, /* Self Cap auto oversamples + *setting. */ + }, + selfcap_data_blk, /* Self Cap data block index. */ + PRIV_SELFCAP_DATA_BLK_SIZE, /* Self Cap data block size. */ + selfcap_y_nodes, /* Self Cap channel nodes. */ + DEF_SELFCAP_QUICK_REBURST_ENABLE, + DEF_SELFCAP_FILTER_CALLBACK, /* Self Cap filter callback + * function + * pointer. */ + DEF_SELFCAP_FREQ_AUTO_TUNE_ENABLE, + DEF_SELFCAP_NOISE_MEAS_ENABLE, + DEF_SELFCAP_NOISE_MEAS_BUFFER_CNT, + DEF_SELFCAP_MOIS_TOLERANCE_ENABLE, + DEF_SELFCAP_NUM_MOIS_GROUPS, + DEF_SELFCAP_MOIS_QUICK_REBURST_ENABLE, + DEF_SELFCAP_PTC_GPIO_STATE, + .tlib_feature_list ={ + .lk_chk = DEF_LOCKOUT_FUNC, + .auto_os_init = DEF_AUTO_OS_FUNC,//auto_os_resolve, + .auto_tune_init = DEF_SELF_AUTO_TUNE_FUNC,// self_auto_tuning + .enable_aks = DEF_AKS_FUNC, /*AKS grouping function */ + } +}; + +/** + * Touch Library input configuration structure. + */ +touch_config_t touch_config = { + NULL, + &selfcap_config, /* Pointer to Self Cap configuration + * structure. */ + DEF_TOUCH_PTC_ISR_LVL, /* PTC interrupt level. */ +}; + +bool ptc_initialized = false; +void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, + const mcu_pin_obj_t *pin) { + if (!pin->has_touch) { + // No ADC function on that pin + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %q does not have touch capabilities", pin->name)); + } + + touch_ret_t status; + if (!ptc_initialized) { + /* Setup and enable generic clock source for PTC module. */ + struct system_gclk_chan_config gclk_chan_conf; + system_gclk_chan_get_config_defaults(&gclk_chan_conf); + gclk_chan_conf.source_generator = GCLK_GENERATOR_1; + system_gclk_chan_set_config(PTC_GCLK_ID, &gclk_chan_conf); + system_gclk_chan_enable(PTC_GCLK_ID); + system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, PM_APBCMASK_PTC); + + /* Initialize touch library for Self Cap operation. */ + status = touch_selfcap_sensors_init_with_rs_table(&touch_config, + PRIV_SELFCAP_RS_TABLE_INIT, PRIV_NM_TABLE_INIT, + PRIV_FREQ_AUTO_TUNE_CHK, PRIV_MOIS_TOLERANCE_CHK); + if (status != TOUCH_SUCCESS) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch init failed (%d)", status)); + } + ptc_initialized = true; + } + // Map Y line to channel. Boards can switch the order. + int channel; + for (channel = 0; channel < DEF_SELFCAP_NUM_CHANNELS; channel++) { + if (selfcap_y_nodes[channel] == Y(pin->touch_y_line)) { + break; + } + } + if (channel == DEF_SELFCAP_NUM_CHANNELS) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q on this board does not have touch capabilities", pin->name)); + } + status = touch_selfcap_sensor_config(SENSOR_TYPE_KEY, channel, channel, + NO_AKS_GROUP, 10u, HYST_25, RES_8_BIT, &self->sensor_id); + if (status != TOUCH_SUCCESS) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch pad config failed (%d)", status)); + } + status = touch_selfcap_sensors_calibrate(AUTO_TUNE_RSEL); + if (status != TOUCH_SUCCESS) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Touch pad calibration failed (%d)", status)); + } + + self->pin = pin; +} + +void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) { + touch_selfcap_sensor_disable(self->sensor_id); +} + +volatile bool touch_read_ready = false; +volatile touch_acq_status_t touch_acq_status; + +void touch_selfcap_measure_complete_callback(void) +{ + touch_read_ready = true; + touch_acq_status = p_selfcap_measure_data->acq_status; +} + +bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { + if (p_selfcap_measure_data->acq_status & TOUCH_CC_CALIB_ERROR) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch calibration error")); + } + touch_acq_status = TOUCH_BURST_AGAIN; + uint64_t start_ticks = ticks_ms; + while((touch_acq_status & TOUCH_BURST_AGAIN) && ticks_ms - start_ticks < 1000) { + touch_read_ready = false; + touch_ret_t touch_ret = touch_selfcap_sensors_measure( + ticks_ms, + NORMAL_ACQ_MODE, + touch_selfcap_measure_complete_callback); + + if (touch_ret != TOUCH_SUCCESS) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch measure failed")); + } + + while(!touch_read_ready && ticks_ms - start_ticks < 1000) { + // wait + } + } + + if (touch_acq_status & TOUCH_BURST_AGAIN) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Touch read failed")); + } + + return (p_selfcap_measure_data->p_sensor_states[self->sensor_id / 8] & (1 << (self->sensor_id % 8))) == 1; +} diff --git a/atmel-samd/common-hal/nativeio/TouchInStub.c b/atmel-samd/common-hal/nativeio/TouchInStub.c new file mode 100644 index 0000000000..491e936773 --- /dev/null +++ b/atmel-samd/common-hal/nativeio/TouchInStub.c @@ -0,0 +1,46 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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 "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.h" +#include "py/mphal.h" +#include "shared-bindings/nativeio/TouchIn.h" + +void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, + const mcu_pin_obj_t *pin) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, + "No room in flash for capacitive touch hardware support.")); +} + +void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) { +} + +bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { + return false; +} diff --git a/atmel-samd/common-hal/nativeio/types.h b/atmel-samd/common-hal/nativeio/types.h index 0a34fe2095..365345a2e6 100644 --- a/atmel-samd/common-hal/nativeio/types.h +++ b/atmel-samd/common-hal/nativeio/types.h @@ -44,6 +44,10 @@ #include "asf/sam0/drivers/sercom/spi/spi.h" #include "asf/sam0/drivers/tc/tc.h" #include "asf/sam0/drivers/tcc/tcc.h" +// Only support TouchIn when external SPI flash is used. +#ifdef SPI_FLASH_SECTOR_SIZE +#include "QTouch/touch_api_ptc.h" +#endif #include "py/obj.h" @@ -90,4 +94,13 @@ typedef struct { struct tcc_module tcc_instance; } nativeio_pwmout_obj_t; +typedef struct { + mp_obj_base_t base; + // Only support TouchIn when external SPI flash is used. + #ifdef SPI_FLASH_SECTOR_SIZE + const mcu_pin_obj_t * pin; + sensor_id_t sensor_id; + #endif +} nativeio_touchin_obj_t; + #endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__ diff --git a/atmel-samd/main.c b/atmel-samd/main.c index 64f36903dc..aa94ab3e59 100644 --- a/atmel-samd/main.c +++ b/atmel-samd/main.c @@ -22,6 +22,10 @@ #include "asf/sam0/drivers/system/system.h" #include +#ifdef SPI_FLASH_SECTOR_SIZE +#include "QTouch/touch_api_ptc.h" +#endif + #include "autoreset.h" #include "mpconfigboard.h" #include "rgb_led_status.h" @@ -155,6 +159,7 @@ void reset_mp(void) { MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt); } +extern bool ptc_initialized; void reset_samd21(void) { // Reset all SERCOMs except the one being used by the SPI flash. Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS; @@ -172,6 +177,11 @@ void reset_samd21(void) { sercom_instances[i]->SPI.CTRLA.bit.SWRST = 1; } +#ifdef SPI_FLASH_SECTOR_SIZE + touch_selfcap_sensors_deinit(); + ptc_initialized = false; +#endif + struct system_pinmux_config config; system_pinmux_get_config_defaults(&config); config.powersave = true; diff --git a/atmel-samd/samd21_pins.c b/atmel-samd/samd21_pins.c index 00cf2049d4..bdfc0ae83c 100644 --- a/atmel-samd/samd21_pins.c +++ b/atmel-samd/samd21_pins.c @@ -28,15 +28,29 @@ #define NO_TIMER TCC(0, 0, 0) +#define TOUCH(y_line) \ + .has_touch = true, \ + .touch_y_line = y_line, + +#define NO_TOUCH \ + .has_touch = false, + +#define ADC_INPUT(input) \ + .has_adc = true, \ + .adc_input = input, + +#define NO_ADC \ + .has_adc = false, + // This macro is used to simplify pin definition in boards//pins.c -#define PIN(p_name, p_has_adc, p_adc_input, p_primary_timer, \ - p_secondary_timer, p_primary_sercom, p_secondary_sercom) \ +#define PIN(p_name, p_adc, 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), \ - .has_adc = p_has_adc, \ - .adc_input = p_adc_input, \ + p_adc \ + p_touch \ .primary_timer = p_primary_timer, \ .secondary_timer = p_secondary_timer, \ .sercom = {p_primary_sercom, p_secondary_sercom}, \ @@ -53,161 +67,161 @@ void reset_pin(uint8_t pin) { // Pins in datasheet order. #ifdef PIN_PA00 -PIN(PA00, false, NO_ADC_INPUT, \ +PIN(PA00, NO_ADC, NO_TOUCH, TCC(TCC2, 0, 0), NO_TIMER, SERCOM(SERCOM1, 0), NO_SERCOM); #endif #ifdef PIN_PA01 -PIN(PA01, false, NO_ADC_INPUT, +PIN(PA01, NO_ADC, NO_TOUCH, TCC(TCC2, 1, 1), NO_TIMER, SERCOM(SERCOM1, 1), NO_SERCOM); #endif #ifdef PIN_PA02 -PIN(PA02, true, ADC_POSITIVE_INPUT_PIN0, +PIN(PA02, ADC_INPUT(ADC_POSITIVE_INPUT_PIN0), TOUCH(0), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PA03 -PIN(PA03, true, ADC_POSITIVE_INPUT_PIN1, +PIN(PA03, ADC_INPUT(ADC_POSITIVE_INPUT_PIN1), TOUCH(1), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB04 -PIN(PB04, true, ADC_POSITIVE_INPUT_PIN12, +PIN(PB04, ADC_INPUT(ADC_POSITIVE_INPUT_PIN12), TOUCH(10), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB05 -PIN(PB05, true, ADC_POSITIVE_INPUT_PIN13, +PIN(PB05, ADC_INPUT(ADC_POSITIVE_INPUT_PIN13), TOUCH(11), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB06 -PIN(PB06, true, ADC_POSITIVE_INPUT_PIN14, +PIN(PB06, ADC_INPUT(ADC_POSITIVE_INPUT_PIN14), TOUCH(12), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB07 -PIN(PB07, true, ADC_POSITIVE_INPUT_PIN15, +PIN(PB07, TOUCH(ADC_POSITIVE_INPUT_PIN15), TOUCH(13), NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PB08 -PIN(PB08, true, ADC_POSITIVE_INPUT_PIN2, +PIN(PB08, ADC_INPUT(ADC_POSITIVE_INPUT_PIN2), TOUCH(14), TC(TC4, 0, 0), NO_TIMER, NO_SERCOM, SERCOM(SERCOM4, 0)); #endif #ifdef PIN_PB09 -PIN(PB09, true, ADC_POSITIVE_INPUT_PIN3, +PIN(PB09, 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, true, ADC_POSITIVE_INPUT_PIN4, +PIN(PA04, 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, true, ADC_POSITIVE_INPUT_PIN5, +PIN(PA05, 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, true, ADC_POSITIVE_INPUT_PIN6, +PIN(PA06, ADC_INPUT(ADC_POSITIVE_INPUT_PIN6), TOUCH(4), TCC(TCC1, 0, 0), NO_TIMER, SERCOM(SERCOM0, 2), NO_SERCOM); #endif #ifdef PIN_PA07 -PIN(PA07, true, ADC_POSITIVE_INPUT_PIN7, +PIN(PA07, ADC_INPUT(ADC_POSITIVE_INPUT_PIN7), TOUCH(5), TCC(TCC1, 1, 1), NO_TIMER, SERCOM(SERCOM0, 3), NO_SERCOM); #endif #ifdef PIN_PA08 -PIN(PA08, true, ADC_POSITIVE_INPUT_PIN17, +PIN(PA08, ADC_INPUT(ADC_POSITIVE_INPUT_PIN17), NO_TOUCH, TCC(TCC0, 0, 0), TCC(TCC1, 2, 2), SERCOM(SERCOM0, 0), SERCOM(SERCOM2, 0)); #endif #ifdef PIN_PA09 -PIN(PA09, true, ADC_POSITIVE_INPUT_PIN17, +PIN(PA09, 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, true, ADC_POSITIVE_INPUT_PIN18, +PIN(PA10, 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, true, ADC_POSITIVE_INPUT_PIN19, +PIN(PA11, 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, false, NO_ADC_INPUT, +PIN(PB10, NO_ADC, NO_TOUCH, TC(TC5, 0, 0), TCC(TCC0, 0, 4), NO_SERCOM, SERCOM(SERCOM4, 2)); #endif #ifdef PIN_PB11 -PIN(PB11, false, NO_ADC_INPUT, +PIN(PB11, NO_ADC, NO_TOUCH, TC(TC5, 1, 1), TCC(TCC0, 1, 5), NO_SERCOM, SERCOM(SERCOM4, 3)); #endif #ifdef PIN_PB12 -PIN(PB12, false, NO_ADC_INPUT, +PIN(PB12, NO_ADC, NO_TOUCH, TC(TC4, 0, 0), TCC(TCC0, 2, 6), SERCOM(SERCOM4, 0), NO_SERCOM); #endif #ifdef PIN_PB13 -PIN(PB13, false, NO_ADC_INPUT, +PIN(PB13, NO_ADC, NO_TOUCH, TC(TC4, 1, 1), TCC(TCC0, 3, 7), SERCOM(SERCOM4, 1), NO_SERCOM); #endif #ifdef PIN_PB14 -PIN(PB14, false, NO_ADC_INPUT, +PIN(PB14, NO_ADC, NO_TOUCH, TC(TC5, 0, 0), NO_TIMER, SERCOM(SERCOM4, 2), @@ -216,28 +230,28 @@ PIN(PB14, false, NO_ADC_INPUT, // Second page. #ifdef PIN_PB15 -PIN(PB15, false, NO_ADC_INPUT, +PIN(PB15, NO_ADC, NO_TOUCH, TC(TC5, 1, 1), NO_TIMER, SERCOM(SERCOM4, 3), NO_SERCOM); #endif #ifdef PIN_PA12 -PIN(PA12, false, NO_ADC_INPUT, +PIN(PA12, NO_ADC, NO_TOUCH, TCC(TCC2, 0, 0), TCC(TCC0, 2, 6), SERCOM(SERCOM2, 0), SERCOM(SERCOM4, 0)); #endif #ifdef PIN_PA13 -PIN(PA13, false, NO_ADC_INPUT, +PIN(PA13, NO_ADC, NO_TOUCH, TCC(TCC2, 1, 1), TCC(TCC0, 3, 7), SERCOM(SERCOM2, 1), SERCOM(SERCOM4, 1)); #endif #ifdef PIN_PA14 -PIN(PA14, false, NO_ADC_INPUT, +PIN(PA14, NO_ADC, NO_TOUCH, TC(TC3, 0, 0), TCC(TCC0, 0, 4), SERCOM(SERCOM2, 2), @@ -249,7 +263,7 @@ PIN(PA14, false, NO_ADC_INPUT, ); #endif #ifdef PIN_PA15 -PIN(PA15, false, NO_ADC_INPUT, +PIN(PA15, NO_ADC, NO_TOUCH, TC(TC3, 1, 1), TCC(TCC0, 1, 5), SERCOM(SERCOM2, 3), @@ -261,35 +275,35 @@ PIN(PA15, false, NO_ADC_INPUT, ); #endif #ifdef PIN_PA16 -PIN(PA16, false, NO_ADC_INPUT, +PIN(PA16, NO_ADC, NO_TOUCH, TCC(TCC2, 0, 0), TCC(TCC0, 2, 6), SERCOM(SERCOM1, 0), SERCOM(SERCOM3, 0)); #endif #ifdef PIN_PA17 -PIN(PA17, false, NO_ADC_INPUT, +PIN(PA17, NO_ADC, NO_TOUCH, TCC(TCC2, 1, 1), TCC(TCC0, 3, 7), SERCOM(SERCOM1, 1), SERCOM(SERCOM3, 1)); #endif #ifdef PIN_PA18 -PIN(PA18, false, NO_ADC_INPUT, +PIN(PA18, NO_ADC, NO_TOUCH, TC(TC3, 0, 0), TCC(TCC0, 2, 2), SERCOM(SERCOM1, 2), SERCOM(SERCOM3, 2)); #endif #ifdef PIN_PA19 -PIN(PA19, false, NO_ADC_INPUT, +PIN(PA19, NO_ADC, NO_TOUCH, TC(TC3, 1, 1), TCC(TCC0, 3, 3), SERCOM(SERCOM1, 3), SERCOM(SERCOM3, 3)); #endif #ifdef PIN_PB16 -PIN(PB16, false, NO_ADC_INPUT, +PIN(PB16, NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC6_INSTANCE_ TC(TC6, 0, 0), #else @@ -300,7 +314,7 @@ PIN(PB16, false, NO_ADC_INPUT, NO_SERCOM); #endif #ifdef PIN_PB17 -PIN(PB17, false, NO_ADC_INPUT, +PIN(PB17, NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC6_INSTANCE_ TC(TC6, 0, 0), #else @@ -311,7 +325,7 @@ PIN(PB17, false, NO_ADC_INPUT, NO_SERCOM); #endif #ifdef PIN_PA20 -PIN(PA20, false, NO_ADC_INPUT, +PIN(PA20, NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 0, 0), #else @@ -322,7 +336,7 @@ PIN(PA20, false, NO_ADC_INPUT, SERCOM(SERCOM3, 2)); #endif #ifdef PIN_PA21 -PIN(PA21, false, NO_ADC_INPUT, +PIN(PA21, NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 1, 1), #else @@ -333,7 +347,7 @@ PIN(PA21, false, NO_ADC_INPUT, SERCOM(SERCOM3, 3)); #endif #ifdef PIN_PA22 -PIN(PA22, false, NO_ADC_INPUT, +PIN(PA22, NO_ADC, NO_TOUCH, TC(TC4, 0, 0), TCC(TCC0, 0, 4), SERCOM(SERCOM3, 0), @@ -345,7 +359,7 @@ PIN(PA22, false, NO_ADC_INPUT, ); #endif #ifdef PIN_PA23 -PIN(PA23, false, NO_ADC_INPUT, +PIN(PA23, NO_ADC, NO_TOUCH, TC(TC4, 1, 1), TCC(TCC0, 1, 5), SERCOM(SERCOM3, 1), @@ -357,7 +371,7 @@ PIN(PA23, false, NO_ADC_INPUT, ); #endif #ifdef PIN_PA24 -PIN(PA24, false, NO_ADC_INPUT, +PIN(PA24, NO_ADC, NO_TOUCH, TC(TC5, 0, 0), TCC(TCC0, 2, 2), SERCOM(SERCOM3, 2), @@ -369,7 +383,7 @@ PIN(PA24, false, NO_ADC_INPUT, ); #endif #ifdef PIN_PA25 -PIN(PA25, false, NO_ADC_INPUT, +PIN(PA25, NO_ADC, NO_TOUCH, TC(TC5, 1, 1), TCC(TCC1, 3, 3), SERCOM(SERCOM3, 3), @@ -381,7 +395,7 @@ PIN(PA25, false, NO_ADC_INPUT, ); #endif #ifdef PIN_PB22 -PIN(PB22, false, NO_ADC_INPUT, +PIN(PB22, NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 1, 1), #else @@ -392,7 +406,7 @@ PIN(PB22, false, NO_ADC_INPUT, SERCOM(SERCOM5, 2)); #endif #ifdef PIN_PB23 -PIN(PB23, false, NO_ADC_INPUT, +PIN(PB23, NO_ADC, NO_TOUCH, #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 1, 1), #else @@ -403,49 +417,49 @@ PIN(PB23, false, NO_ADC_INPUT, SERCOM(SERCOM5, 3)); #endif #ifdef PIN_PA27 -PIN(PA27, false, NO_ADC_INPUT, +PIN(PA27, NO_ADC, NO_TOUCH, NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PA28 -PIN(PA28, false, NO_ADC_INPUT, +PIN(PA28, NO_ADC, NO_TOUCH, NO_TIMER, NO_TIMER, NO_SERCOM, NO_SERCOM); #endif #ifdef PIN_PA30 -PIN(PA30, false, NO_ADC_INPUT, +PIN(PA30, NO_ADC, NO_TOUCH, TCC(TCC1, 0, 0), NO_TIMER, NO_SERCOM, SERCOM(SERCOM1, 2)); #endif #ifdef PIN_PA31 -PIN(PA31, false, NO_ADC_INPUT, +PIN(PA31, NO_ADC, NO_TOUCH, TCC(TCC1, 1, 1), NO_TIMER, NO_SERCOM, SERCOM(SERCOM1, 3)); #endif #ifdef PIN_PB30 -PIN(PB30, false, NO_ADC_INPUT, +PIN(PB30, NO_ADC, NO_TOUCH, TCC(TCC0, 0, 0), TCC(TCC1, 2, 2), NO_SERCOM, SERCOM(SERCOM5, 0)); #endif #ifdef PIN_PB31 -PIN(PB31, false, NO_ADC_INPUT, +PIN(PB31, NO_ADC, NO_TOUCH, TCC(TCC0, 1, 1), TCC(TCC1, 3, 3), NO_SERCOM, SERCOM(SERCOM5, 1)); #endif #ifdef PIN_PB00 -PIN(PB00, true, ADC_POSITIVE_INPUT_PIN8, +PIN(PB00, ADC_INPUT(ADC_POSITIVE_INPUT_PIN8), TOUCH(6), #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 0, 0), #else @@ -456,7 +470,7 @@ PIN(PB00, true, ADC_POSITIVE_INPUT_PIN8, SERCOM(SERCOM5, 2)); #endif #ifdef PIN_PB01 -PIN(PB01, true, ADC_POSITIVE_INPUT_PIN9, +PIN(PB01, ADC_INPUT(ADC_POSITIVE_INPUT_PIN9), TOUCH(7), #ifdef _SAMD21_TC7_INSTANCE_ TC(TC7, 1, 1), #else @@ -467,7 +481,7 @@ PIN(PB01, true, ADC_POSITIVE_INPUT_PIN9, SERCOM(SERCOM5, 3)); #endif #ifdef PIN_PB02 -PIN(PB02, true, ADC_POSITIVE_INPUT_PIN10, +PIN(PB02, ADC_INPUT(ADC_POSITIVE_INPUT_PIN10), TOUCH(8), #ifdef _SAMD21_TC6_INSTANCE_ TC(TC6, 0, 0), #else @@ -478,7 +492,7 @@ PIN(PB02, true, ADC_POSITIVE_INPUT_PIN10, SERCOM(SERCOM5, 0)); #endif #ifdef PIN_PB03 -PIN(PB03, true, ADC_POSITIVE_INPUT_PIN11, +PIN(PB03, ADC_INPUT(ADC_POSITIVE_INPUT_PIN11), TOUCH(9), #ifdef _SAMD21_TC6_INSTANCE_ TC(TC6, 1, 1), #else diff --git a/esp8266/Makefile b/esp8266/Makefile index 3c950fbac0..d1583d61f9 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -116,6 +116,7 @@ SRC_COMMON_HAL = \ nativeio/I2C.c \ nativeio/PWMOut.c \ nativeio/SPI.c \ + nativeio/TouchIn.c \ neopixel_write/__init__.c \ time/__init__.c \ board/__init__.c diff --git a/esp8266/common-hal/nativeio/TouchIn.c b/esp8266/common-hal/nativeio/TouchIn.c new file mode 100644 index 0000000000..1501a79362 --- /dev/null +++ b/esp8266/common-hal/nativeio/TouchIn.c @@ -0,0 +1,46 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 + * 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 "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.h" +#include "py/mphal.h" +#include "shared-bindings/nativeio/TouchIn.h" + +void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, + const mcu_pin_obj_t *pin) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, + "No hardware support for capacitive touch.")); +} + +void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) { +} + +bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { + return false; +} diff --git a/esp8266/common-hal/nativeio/types.h b/esp8266/common-hal/nativeio/types.h index 7c263faeb8..c0b5f6289c 100644 --- a/esp8266/common-hal/nativeio/types.h +++ b/esp8266/common-hal/nativeio/types.h @@ -67,4 +67,8 @@ typedef struct { const mcu_pin_obj_t* pin; } nativeio_pwmout_obj_t; +typedef struct { + mp_obj_base_t base; +} nativeio_touchin_obj_t; + #endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__ diff --git a/shared-bindings/nativeio/TouchIn.c b/shared-bindings/nativeio/TouchIn.c new file mode 100644 index 0000000000..478fec6cd9 --- /dev/null +++ b/shared-bindings/nativeio/TouchIn.c @@ -0,0 +1,141 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * 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 "py/binary.h" +#include "py/mphal.h" +#include "py/nlr.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/nativeio/TouchIn.h" + +//| .. currentmodule:: nativeio +//| +//| :class:`TouchIn` -- Read the state of a capacitive touch sensor +//| ============================================ +//| +//| Usage:: +//| +//| import nativeio +//| from board import * +//| +//| with nativeio.TouchIn(A1) as touch: +//| if touch.value: +//| print("touched!") +//| + +//| .. class:: TouchIn(pin) +//| +//| Use the TouchIn on the given pin. +//| +//| :param ~microcontroller.Pin pin: the pin to read from +//| +STATIC mp_obj_t nativeio_touchin_make_new(const mp_obj_type_t *type, + mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { + // check number of arguments + mp_arg_check_num(n_args, n_kw, 1, 1, false); + + // 1st argument is the pin + mp_obj_t pin_obj = args[0]; + assert_pin(pin_obj, false); + + nativeio_touchin_obj_t *self = m_new_obj(nativeio_touchin_obj_t); + self->base.type = &nativeio_touchin_type; + const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); + common_hal_nativeio_touchin_construct(self, pin); + + return (mp_obj_t) self; +} + +//| .. method:: deinit() +//| +//| Deinitialises the TouchIn and releases any hardware resources for reuse. +//| +STATIC mp_obj_t nativeio_touchin_deinit(mp_obj_t self_in) { + nativeio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_nativeio_touchin_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_deinit_obj, nativeio_touchin_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +STATIC mp_obj_t nativeio_touchin_obj___enter__(mp_obj_t self_in) { + return self_in; +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin___enter___obj, nativeio_touchin_obj___enter__); + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. +//| +STATIC mp_obj_t nativeio_touchin_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_nativeio_touchin_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_touchin___exit___obj, 4, 4, nativeio_touchin_obj___exit__); + +//| .. attribute:: value +//| +//| Whether the touch pad is being touched or not. +//| +//| :return: True when touched, False otherwise. +//| :rtype: bool +//| +STATIC mp_obj_t nativeio_touchin_obj_get_value(mp_obj_t self_in) { + nativeio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(common_hal_nativeio_touchin_get_value(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_get_value_obj, nativeio_touchin_obj_get_value); + +mp_obj_property_t nativeio_touchin_value_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&nativeio_touchin_get_value_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC const mp_rom_map_elem_t nativeio_touchin_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_touchin___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_touchin___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_touchin_deinit_obj) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_touchin_value_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(nativeio_touchin_locals_dict, nativeio_touchin_locals_dict_table); + +const mp_obj_type_t nativeio_touchin_type = { + { &mp_type_type }, + .name = MP_QSTR_TouchIn, + .make_new = nativeio_touchin_make_new, + .locals_dict = (mp_obj_t)&nativeio_touchin_locals_dict, +}; diff --git a/shared-bindings/nativeio/TouchIn.h b/shared-bindings/nativeio/TouchIn.h new file mode 100644 index 0000000000..b22f655e57 --- /dev/null +++ b/shared-bindings/nativeio/TouchIn.h @@ -0,0 +1,39 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * 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_TOUCHIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ + +#include "common-hal/microcontroller/types.h" +#include "common-hal/nativeio/types.h" + +extern const mp_obj_type_t nativeio_touchin_type; + +void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, const mcu_pin_obj_t *pin); +void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self); +bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ diff --git a/shared-bindings/nativeio/__init__.c b/shared-bindings/nativeio/__init__.c index 46187a34a2..0cf4ec829b 100644 --- a/shared-bindings/nativeio/__init__.c +++ b/shared-bindings/nativeio/__init__.c @@ -41,6 +41,7 @@ #include "shared-bindings/nativeio/I2C.h" #include "shared-bindings/nativeio/PWMOut.h" #include "shared-bindings/nativeio/SPI.h" +#include "shared-bindings/nativeio/TouchIn.h" #include "common-hal/nativeio/types.h" #include "shared-bindings/nativeio/__init__.h" @@ -114,6 +115,7 @@ STATIC const mp_rom_map_elem_t nativeio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&nativeio_i2c_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) }, + { MP_ROM_QSTR(MP_QSTR_TouchIn), MP_ROM_PTR(&nativeio_touchin_type) }, }; STATIC MP_DEFINE_CONST_DICT(nativeio_module_globals, nativeio_module_globals_table);