| #define HAS_KINETIS_I2C0 | #define HAS_KINETIS_I2C0 | ||||
| #define HAS_KINETIS_LLWU_16CH | #define HAS_KINETIS_LLWU_16CH | ||||
| #define HAS_KINETIS_ADC0 | #define HAS_KINETIS_ADC0 | ||||
| #define HAS_KINETIS_TSI | |||||
| // Teensy 3.1 | // Teensy 3.1 | ||||
| #elif defined(__MK20DX256__) | #elif defined(__MK20DX256__) | ||||
| #define HAS_KINETIS_LLWU_16CH | #define HAS_KINETIS_LLWU_16CH | ||||
| #define HAS_KINETIS_ADC0 | #define HAS_KINETIS_ADC0 | ||||
| #define HAS_KINETIS_ADC1 | #define HAS_KINETIS_ADC1 | ||||
| #define HAS_KINETIS_TSI | |||||
| // Teensy-LC | // Teensy-LC | ||||
| #elif defined(__MKL26Z64__) | #elif defined(__MKL26Z64__) | ||||
| #define HAS_KINETIS_I2C1_STOPF | #define HAS_KINETIS_I2C1_STOPF | ||||
| #define HAS_KINETIS_LLWU_16CH | #define HAS_KINETIS_LLWU_16CH | ||||
| #define HAS_KINETIS_ADC0 | #define HAS_KINETIS_ADC0 | ||||
| #define HAS_KINETIS_TSI_LITE | |||||
| #elif defined(__MK64FX512__) | #elif defined(__MK64FX512__) | ||||
| #define HAS_KINETIS_MPU | #define HAS_KINETIS_MPU | ||||
| #define HAS_KINETIS_ADC0 | #define HAS_KINETIS_ADC0 | ||||
| #define HAS_KINETIS_ADC1 | #define HAS_KINETIS_ADC1 | ||||
| #define HAS_KINETIS_TSI_LITE | |||||
| // Touch sense input (TSI) | // Touch sense input (TSI) | ||||
| #if defined(KINETISK) | |||||
| #if defined(HAS_KINETIS_TSI) | |||||
| #define TSI0_GENCS (*(volatile uint32_t *)0x40045000) // General Control and Status Register | #define TSI0_GENCS (*(volatile uint32_t *)0x40045000) // General Control and Status Register | ||||
| #define TSI_GENCS_LPCLKS ((uint32_t)0x10000000) // | #define TSI_GENCS_LPCLKS ((uint32_t)0x10000000) // | ||||
| #define TSI_GENCS_LPSCNITV(n) (((n) & 15) << 24) // | #define TSI_GENCS_LPSCNITV(n) (((n) & 15) << 24) // | ||||
| #define TSI0_CNTR13 (*(volatile uint32_t *)0x40045118) // Counter Register | #define TSI0_CNTR13 (*(volatile uint32_t *)0x40045118) // Counter Register | ||||
| #define TSI0_CNTR15 (*(volatile uint32_t *)0x4004511C) // Counter Register | #define TSI0_CNTR15 (*(volatile uint32_t *)0x4004511C) // Counter Register | ||||
| #define TSI0_THRESHOLD (*(volatile uint32_t *)0x40045120) // Low Power Channel Threshold Register | #define TSI0_THRESHOLD (*(volatile uint32_t *)0x40045120) // Low Power Channel Threshold Register | ||||
| #elif defined(KINETISL) | |||||
| #elif defined(HAS_KINETIS_TSI_LITE) | |||||
| #define TSI0_GENCS (*(volatile uint32_t *)0x40045000) // General Control and Status | #define TSI0_GENCS (*(volatile uint32_t *)0x40045000) // General Control and Status | ||||
| #define TSI_GENCS_OUTRGF ((uint32_t)0x80000000) // Out of Range Flag | #define TSI_GENCS_OUTRGF ((uint32_t)0x80000000) // Out of Range Flag | ||||
| #define TSI_GENCS_ESOR ((uint32_t)0x10000000) // End-of-scan or Out-of-Range Interrupt Selection | #define TSI_GENCS_ESOR ((uint32_t)0x10000000) // End-of-scan or Out-of-Range Interrupt Selection |
| #include "core_pins.h" | #include "core_pins.h" | ||||
| //#include "HardwareSerial.h" | //#include "HardwareSerial.h" | ||||
| #if defined(HAS_KINETIS_TSI) || defined(HAS_KINETIS_TSI_LITE) | |||||
| #if defined(__MK20DX128__) || defined(__MK20DX256__) | #if defined(__MK20DX128__) || defined(__MK20DX256__) | ||||
| // These settings give approx 0.02 pF sensitivity and 1200 pF range | // These settings give approx 0.02 pF sensitivity and 1200 pF range | ||||
| // Lower current, higher number of scans, and higher prescaler | // Lower current, higher number of scans, and higher prescaler | ||||
| }; | }; | ||||
| #elif defined(__MK66FX1M0__) | #elif defined(__MK66FX1M0__) | ||||
| #define CURRENT 2 | |||||
| #define NSCAN 9 | #define NSCAN 9 | ||||
| #define PRESCALE 2 | #define PRESCALE 2 | ||||
| static const uint8_t pin2tsi[] = { | static const uint8_t pin2tsi[] = { | ||||
| int touchRead(uint8_t pin) | int touchRead(uint8_t pin) | ||||
| { | { | ||||
| #if defined(__MK64FX512__) | |||||
| return 0; // no Touch sensing :( | |||||
| #else | |||||
| uint32_t ch; | uint32_t ch; | ||||
| if (pin >= NUM_DIGITAL_PINS) return 0; | if (pin >= NUM_DIGITAL_PINS) return 0; | ||||
| *portConfigRegister(pin) = PORT_PCR_MUX(0); | *portConfigRegister(pin) = PORT_PCR_MUX(0); | ||||
| SIM_SCGC5 |= SIM_SCGC5_TSI; | SIM_SCGC5 |= SIM_SCGC5_TSI; | ||||
| #if defined(KINETISK) | |||||
| #if defined(HAS_KINETIS_TSI) | |||||
| TSI0_GENCS = 0; | TSI0_GENCS = 0; | ||||
| TSI0_PEN = (1 << ch); | TSI0_PEN = (1 << ch); | ||||
| TSI0_SCANC = TSI_SCANC_REFCHRG(3) | TSI_SCANC_EXTCHRG(CURRENT); | TSI0_SCANC = TSI_SCANC_REFCHRG(3) | TSI_SCANC_EXTCHRG(CURRENT); | ||||
| while (TSI0_GENCS & TSI_GENCS_SCNIP) ; // wait | while (TSI0_GENCS & TSI_GENCS_SCNIP) ; // wait | ||||
| delayMicroseconds(1); | delayMicroseconds(1); | ||||
| return *((volatile uint16_t *)(&TSI0_CNTR1) + ch); | return *((volatile uint16_t *)(&TSI0_CNTR1) + ch); | ||||
| #elif defined(KINETISL) | |||||
| #elif defined(HAS_KINETIS_TSI_LITE) | |||||
| TSI0_GENCS = TSI_GENCS_REFCHRG(4) | TSI_GENCS_EXTCHRG(3) | TSI_GENCS_PS(PRESCALE) | TSI0_GENCS = TSI_GENCS_REFCHRG(4) | TSI_GENCS_EXTCHRG(3) | TSI_GENCS_PS(PRESCALE) | ||||
| | TSI_GENCS_NSCN(NSCAN) | TSI_GENCS_TSIEN | TSI_GENCS_EOSF; | | TSI_GENCS_NSCN(NSCAN) | TSI_GENCS_TSIEN | TSI_GENCS_EOSF; | ||||
| TSI0_DATA = TSI_DATA_TSICH(ch) | TSI_DATA_SWTS; | TSI0_DATA = TSI_DATA_TSICH(ch) | TSI_DATA_SWTS; | ||||
| delayMicroseconds(1); | delayMicroseconds(1); | ||||
| return TSI0_DATA & 0xFFFF; | return TSI0_DATA & 0xFFFF; | ||||
| #endif | #endif | ||||
| #endif | |||||
| } | } | ||||
| #else | |||||
| int touchRead(uint8_t pin) | |||||
| { | |||||
| return 0; // no Touch sensing :( | |||||
| } | |||||
| #endif | |||||