From b1d165b87dc4f43df594df2e94f5aaff35c25246 Mon Sep 17 00:00:00 2001 From: MX682X Date: Tue, 10 Oct 2023 20:32:36 +0200 Subject: [PATCH 1/3] Adding PTC! (Beta) --- megaavr/libraries/PTC/README.md | 135 +++ .../PTC/examples/mutualcap/mutualcap.ino | 60 + .../self_and_mutual_mix.ino | 60 + .../PTC/examples/selfcap/selfcap.ino | 62 + .../selfcap_other_nodes_shield.ino | 56 + .../selfcap_shield_lowpower.ino | 122 ++ .../selfcap_with_dedicated_shield.ino | 61 + .../temperature_mutual/temperature_mutual.ino | 70 ++ megaavr/libraries/PTC/keywords.txt | 91 ++ megaavr/libraries/PTC/library.properties | 10 + megaavr/libraries/PTC/src/ptc.c | 1014 +++++++++++++++++ megaavr/libraries/PTC/src/ptc.h | 235 ++++ megaavr/libraries/PTC/src/ptc_io.h | 449 ++++++++ megaavr/libraries/PTC/src/ptc_types.h | 237 ++++ 14 files changed, 2662 insertions(+) create mode 100644 megaavr/libraries/PTC/README.md create mode 100644 megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino create mode 100644 megaavr/libraries/PTC/examples/self_and_mutual_mix/self_and_mutual_mix.ino create mode 100644 megaavr/libraries/PTC/examples/selfcap/selfcap.ino create mode 100644 megaavr/libraries/PTC/examples/selfcap_other_nodes_shield/selfcap_other_nodes_shield.ino create mode 100644 megaavr/libraries/PTC/examples/selfcap_shield_lowpower/selfcap_shield_lowpower.ino create mode 100644 megaavr/libraries/PTC/examples/selfcap_with_dedicated_shield/selfcap_with_dedicated_shield.ino create mode 100644 megaavr/libraries/PTC/examples/temperature_mutual/temperature_mutual.ino create mode 100644 megaavr/libraries/PTC/keywords.txt create mode 100644 megaavr/libraries/PTC/library.properties create mode 100644 megaavr/libraries/PTC/src/ptc.c create mode 100644 megaavr/libraries/PTC/src/ptc.h create mode 100644 megaavr/libraries/PTC/src/ptc_io.h create mode 100644 megaavr/libraries/PTC/src/ptc_types.h diff --git a/megaavr/libraries/PTC/README.md b/megaavr/libraries/PTC/README.md new file mode 100644 index 00000000..09259635 --- /dev/null +++ b/megaavr/libraries/PTC/README.md @@ -0,0 +1,135 @@ +# ptc_touch +An Arduino compatible C library to support the PTC in the AtTiny 1-Series + +## Features +As this is library can be considered an Alpha version, things might change in the future, so nothing is set in stone yet. +* Ready to use with minimal knowledge of the peripheral, but can still be well configured (options are similar to the "official" library) +* Supports Mutual-cap and Self-cap (+Shield) sensing technology +* Supports event-based triggering of conversions, as well as window-compared wake-up from Stand-by sleep +* Can be suspended to allow the polled use of ADC0 (ADC0 ISRs are used by the library) +* Reduced memory footprint due to CPU specific optimizations compared to the "official" implementation (needs about 3.5k Flash) + + +## Installation + +Not needed, comes with the core. + +For the non-Arduino people: all you need is inside the src folder. + + +## How to use this library + + +#### Step 1: Memory Initialization +Allocate memory for every single sensing node. The easiest way is to have an array with `cap_sensor_t` elements, e.g. `cap_sensor_t nodes[3];`. Even though it allows you to access the data stored in this structures directly, it is not allowed to change the contents directly. Always use the provided setter functions for this. + +#### Step 2: Node registration +Initialize the node to be either a mutual-cap node with `uint8_t ptc_add_mutualcap_node(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc_ch_bm_t xCh);` or to a self-cap node with `uint8_t ptc_add_selfcap_node(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc_ch_bm_t xCh);`. When everything went without problems, the functions will return the enum `PTC_LIB_SUCCESS`. While mutual-cap requires yCh and xCh not to be 0 (otherwise `PTC_LIB_BAD_ARGUMENT` will be returned), the self-cap requires only the yCh not to be 0. If the xCh is zero, the node will be a normal self-cap node, otherwise it will be of the self-cap with shield type. This becomes relevant when you change between the types, but that will be explained later. This function will also disable the pullup and digital input function of the specified pins. The order, in which this function is called determines the conversion order. The nodes will be also numbered for easier identification. Up to 255 nodes are supported for devices with 16 or less PTC pins. 65k for bigger devices. The nodes are enabled by default, so, in case an added node should be disabled, you can call `uint8_t ptc_disable_node(cap_sensor_t* node);`, and for `re-enabling uint8_t ptc_enable_node(cap_sensor_t* node);`. +Here are some examples: +- `ptc_add_mutualcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA7));` + - PA4 will be sensing, PA7 will be driving +- `ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA4), 0);` + - No Driving pin, only sensing on PA4 +- `ptc_add_selfcap_node(&nodes[2], (PIN_TO_PTC(PIN_PA4) | PIN_TO_PTC(PIN_PA5)), PIN_TO_PTC(PIN_PA7));` + - useful for wakeups, all "buttons" work as one, no matter on which you press, plus driven shield pin. + + +#### Step 3: Acquisition and Processing +This library requires a regular call to the function `void ptc_process(uint16_t currTime)`. This function handles all background functionality of the library and calls the callback `extern void ptc_event_callback(const uint8_t eventType, cap_sensor_t* node)` when appropriate. First, the function checks if an acquisition was completed (all nodes of the selected type converted). If that's the case, it proceeds to handle the gathered data to handle the respective state machine of each node whose conversion was completed. The more nodes you have, the more time it might take. +The exact workings of this function will exceed the scope of this document. +This function takes an argument, `uint16_t currTime`, to decide when to start the next acquisition. This, compared to having a, as an example, `millis()` inside the library, offers the user significant flexibility on choosing the time source (e.g. TCB.CNT). The Period can be set by `void ptc_set_acqusition_period(uint16_t period)`. Whenever the currTime minus the currTime when the last acquisition trigger happened is greater or equal the period, a new acquisition is started. +However, if there was a successful call to `uint8_t ptc_suspend(void)`, only the timestamp is updated, but no acquisition is started. This also applies when the library was put into low-power mode, except that conversions can still be triggered by events. +Even though this library can handle three different node types, only one type can be acquired at a time. By default, the type of the first node in the list is used. But the user can use the function `ptc_set_next_conversion_type(uint8_t type)` to change this behavior. + +#### Step 4: Callback +In order to provide an efficient way of notification, the library provides a couple of extern callbacks, listed below with the type of events that are passed to those functions. In a previous version, all callback-events would have called `void ptc_event_callback(const ptc_cb_event_t eventType, cap_sensor_t* node) {`, however, I realized that a single function would end up too complex. +All (sub-)callbacks are provided with the `weak` and `alias(ptc_event_callback)` attributes, meaning that if you do not provide a definition in your files, the callbacks will be redirected to the standard callback function, but never to both. The standard callback has also a default version that literally does nothing, meaning that your sketch will compile, even if you don't define any of the callbacks (not recommended though). + + +There are following callbacks: +- `ptc_event_cb_touch` With following Events: + - `PTC_CB_EVENT_TOUCH_DETECT` (0x11): Whenever a node changes to the pressed state + - `PTC_CB_EVENT_TOUCH_RELEASE` (0x12): Whenever a node changes to the un-pressed state +- `ptc_event_cb_wake` Only when low-power is enabled, called from ptc_process: + - `PTC_CB_EVENT_WAKE_TOUCH` (0x15): When the Window-Comparator detected a touch + - `PTC_CB_EVENT_WAKE_NO_TOUCH` (0x16): opposite of above +- `ptc_event_cb_conversion`: + - `PTC_CB_EVENT_CONV_CMPL` (0x20): Anytime a node has finished it's conversion and the state-machine was updated + - `PTC_CB_EVENT_CONV_MUTUAL_CMPL` (0x21). Useful to select the next type of conversions, if you have a mix of types + - `PTC_CB_EVENT_CONV_SELF_CMPL` (0x24). You can AND the values with NODE_MUTUAL_bm, NODE_SELFCAP_bm, + - `PTC_CB_EVENT_CONV_SHIELD_CMPL` (0x28). NODE_SELFCAP_SHIELD_bm. + - `PTC_CB_EVENT_CONV_TYPE_CMPL_MSK`(0x0D): Can be used with bitwise AND to differentiate between node and type conversions. +- `ptc_event_cb_calibration`: + - `PTC_CB_EVENT_CONV_CALIB` (0x40): When ever a calibration was finished. + - `PTC_CB_EVENT_ERR_CALIB_MSK` (0x07): Can be used with bitwise AND to to differentiate between success and Error. + - `PTC_CB_EVENT_ERR_CALIB_LOW` (0x41): If the Capacitor compensation ended up too low. + - `PTC_CB_EVENT_ERR_CALIB_HIGH` (0x42): As above, but too high. + - `PTC_CB_EVENT_ERR_CALIB_TO` (0x44): If the calibration timed out. +- `ptc_event_cb_error': + - Currently unused, but might be in the future. +The hexa-decimal values are there for orientation and might change in the future. + + +### Low Power operation +This library also provides a so-called low power mode. This mode changes the operation of the library and the ADC. It will be set up to wait for Events, however it is up to the user to set up the Event-Routing network. The low power mode also sets up the window-comparator to detect touch events. It should be also noted, that it is not possible to use `ptc_suspend` while the library is in low-power mode. the low-power mode has to be disabled first before being able to suspend the library. + +In order to initialize the low-power mode, just call `uint8_t ptc_lp_init(cap_sensor_t* node)` (to disable: `uint8_t ptc_lp_disable(void)`) with the node you want to have automatically converted. The node must be enabled and must have been calibrated beforehand to work correctly, meaning it had to have a couple finished conversions. + +To check, if the window-comparator was triggered outside of the WAKE events, you can use the function `uint8_t ptc_lp_was_waken(void)` that returns either `PTC_LIB_WAS_WAKEN` or `PTC_LIB_ASLEEP`, at least while the low-power mode is active or use the callback. + +### Pins and how to use the ptc_add_* functions + +The ptc_add_* functions require a bitmap (ptc_ch_bm_t yCh; ptc_ch_bm_t xCh) of pins, that will be connected to the PTC internals upon the beginning of a conversion. The bitposition equals to the PTC pin numbering, that you can find in the PTC column in the datasheet. It is different from the usual numbering, for example PA4 on the 1614 is X0/Y0, resulting in a bitmap of 0x01. + +However, to make it easier, a compile-time look-up-table with a macro to access it is provided, `PIN_TO_PTC(PIN_Pnx)`. If the pin is valid, the corresponding bit-map is returned, otherwise null. But this requires the megaTinyCore from Spence Konde, as the PIN_Pnx naming is defined there. + +The reason, why a bit-map is used, is to provide a way to connect the electrodes of multiple pins to act as one big electrode. In order to create a node with multiple pins connected, a simple OR operation is enough. + +The ptc_ch_bm_t is a typedef that depends on the pincount of the device and ranges from uint8_t to uint16_t (and up to uint64_t on the DAs). Up to 8 PTC-Pins, uint8_t, up to 16 PTC pins, uint16_t, and so on. + +### PTC Operation +![PTC_InnerWorkings](https://github.com/MX682X/ptc_touch/assets/58419867/823c487c-0633-4031-b381-45e7a32867fb) +(Source: [Microchip's PTC Subsystem Firmware User's Guide](https://web.archive.org/web/20221225142000/https://www.mouser.com/pdfdocs/SAMA5D2_PTC_Firmware_UG.pdf)) +This schematic was made for a different chip, but it is likely to look similar on the AVRs. + +Most of the following is a hypothesis based on the publically available documentation and observation. +The PTC is using a charge transfer between a variable and a fixed capacitance to measure a difference between those two. The variable capacitance in this case is the sensor electrode and the fixed one is the internal Cc. The neat thing about having a dedicated hardware for this, is that Cc is not really fixed, compared to the Sample-and-Hold capacitor of the normal ADC, it can be calibrated to be about the same as the electrode we want to measure (See below). + +In order to measure the capacitance of the node, the PTC performs following Steps: + 1. Charge the electrode to Vdd + 2. Wait for charge to complete + 3. Disconnect node from Vdd and instead connect it to Cc + 4. Measure voltage of Cc + 5. Connect node and Cc to GND to discharge them + +The measured voltage depends on what happens around the electrode. In an idle state, 50% of the charge of the node (and thus voltage) is transferred to Cc, however when something conductive is moved towards the electrode, the capacitance will increase slightly. With a higher capacitance, the node will have a higher charge, meaning less then 50% of the charge can be transferred to Cc until they reach an equal Voltage, which means the overall voltage will be higher, which can be measured by the ADC. + +If you have trouble understanding: Imagine two equally sized volumes connected trough a valve. You fill the first up with water, open the valve and look on the second, how high the water has risen. After marking the "idle" level, when you put a finger in the water and see the water rise. This new level is the increase in voltage due to an interference. + +### Calibration / Compensation + +Based on the documentation found online, the PTC has an internal, tunable capacitor connected after the series resistance to ground that is used to compensate the parasitic capacitance of the electrodes. Every node starts with a default compensation value. As soon as the node is enabled, the library attempts to find a compensation setting that will result in an ADC value of about 512 counts (1/2 of ADC resolution). Based on oscilloscope readings, it can also be said that the PTC tries to have a charge of 50% VCC on the electrode when being acquired. This is the also the reason, why the digital input function of the pins is disabled. + +The maximum compensation is about 30pF for Mutual-cap and about 50pF for Self-cap. It is possible to get the compensation capacitance with uint16_t ptc_get_node_cc_fempto(cap_sensor_t* node); - however this function has to do a lot of calculations and is thus a bit bloat-y. It will also return the value in fempto farrads, to avoid floats. Read more here: https://www.microchipdeveloper.com/touch:guide-to-interpret-cc-calibration-value + +Different pins have a different parasitic capacitance. I suspect this is depends on the internal circuitry and alternative functions, so it's normal to see some difference with pins next to each other. + +### Tuning of nodes + +In order to ease the use of the PTC module, the ptc_add_* functions will initialize the cap_sensor_t struct with some default values, like the CC value mentioned above. That values can be easily changed and will be applied the next time a conversion of said node starts. Here is a list: + - Analog Gain. Increases the sensitivity of the electrode by adjusting a capacitor on a integrator (I think) (1x Gain) + - Digital Gain. Defines the amount of ADC Oversampling. Will not affect the count value, as it is internally right-shifted. (16x Oversampled) + - Charge Share Delay. Affects the Sample length of the ADC. (0 extra clocks) + - Prescaler. It is possible to slow down the ADC clock by adjusting the Prescaler. (Depends on CPU clock, targeted: 1MHz +/- 25%) + - Serial Resistor. Allows to change the serial resistor between the Cc and the node. Fixed at 100k for Self-Cap. Creates RC-low-pass filter. + +If a node is not sensitive enough, you can increase the Analog Gain (if it becomes too sensitive, an increase of the thresholds might be needed). However it is better to have a bigger node to begin with because the bigger the area, the higher is the capacitance delta. + +### Global settings of the State-maschine +The state-machine, which changes the node's state between Calibration, touch, no touch, etc. uses some variables that are valid for all nodes, those are: + - `uint16_t force_recal_delta`. Each node has a threshold value that is used to calculate the delta. This Threshold value is drifting over time to adjust for environmental changes. If the threshold value drifts 512 +/- this value, a recalibration of CC is performed. Default: 150 + - `uint8_t touched_detect_nom`. Number of consecutive Measurements (Conversions) that are above the touch threshold until the node becomes "touched". Default: 3 + - `uint8_t untouched_detect_nom`. Number of consecutive measurments that are below the no-touch threshold until the node is fully untouched. Default: 3 + - `uint8_t touched_max_nom`. Number of consecutive measurements plus one in the touched state until a recalibration is forced. Can be disabled by writing 255 to it. Default: 200 + - `uint8_t drift_up_nom`. If the delta is higher then the reference, but lower then the threshold, the amount of consecutive measurements plus one, until the reference is incremented. Can be disabled with 255. Default: 20. + - `uint8_t drift_down_nom`. If the delta is below the reference, the amount of consecutive measurements plus one until the reference is decremented. Can be disabled with 255. Default: 20. diff --git a/megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino b/megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino new file mode 100644 index 00000000..5c52a1c7 --- /dev/null +++ b/megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino @@ -0,0 +1,60 @@ +#include +/* + * This example creates two different sensing nodes. + * PA4 and PA5 are on the Y-Lines, PA6 is the X-Line. + * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can + * be used for quick actions, like switching a pin or variable, + * but it is recommended to use PTC_CB_EVENT_CONV_MUTUAL_CMPL, as + * otherwise the handling of the successing nodes would be delayed. + */ +#define MySerial Serial + +cap_sensor_t nodes[2]; + +void setup() { + MySerial.begin(115200); + + // this puts the node on the list and initializes to default values + ptc_add_mutualcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA7)); + ptc_add_mutualcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PA7)); + + + // Make sure Serial works + MySerial.println("Hello World!"); +} + +void loop() { + ptc_process(millis()); // main ptc task, requires regular calls +} + +// callbacks that are called by ptc_process at different points to ease user interaction +void ptc_event_cb_touch(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_TOUCH_DETECT == eventType) { + MySerial.print("node touched:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_TOUCH_RELEASE == eventType) { + MySerial.print("node released:"); + MySerial.println(ptc_get_node_id(node)); + } +} + +void ptc_event_cb_conversion(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_CONV_TYPE_CMPL_MSK & eventType) { + // Do more complex things here + } + (void)node; // remove unused warning +} + +void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_ERR_CALIB_LOW == eventType) { + MySerial.print("Calib error, Cc too low."); + } else if (PTC_CB_EVENT_ERR_CALIB_HIGH == eventType) { + MySerial.print("Calib error, Cc too high."); + } else if (PTC_CB_EVENT_ERR_CALIB_TO == eventType) { + MySerial.print("Calib error, calculation timeout."); + } else { + MySerial.print("Calib Successful."); + } + MySerial.print(" Node: "); + MySerial.println(ptc_get_node_id(node)); +} diff --git a/megaavr/libraries/PTC/examples/self_and_mutual_mix/self_and_mutual_mix.ino b/megaavr/libraries/PTC/examples/self_and_mutual_mix/self_and_mutual_mix.ino new file mode 100644 index 00000000..97da98d1 --- /dev/null +++ b/megaavr/libraries/PTC/examples/self_and_mutual_mix/self_and_mutual_mix.ino @@ -0,0 +1,60 @@ + +#include + + +#define MySerial Serial +cap_sensor_t nodes[4]; + + +void setup() { + // put your setup code here, to run once: + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PB0)); + ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PB0)); + + ptc_add_mutualcap_node(&nodes[2], PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PB1)); + ptc_add_mutualcap_node(&nodes[3], PIN_TO_PTC(PIN_PA7), PIN_TO_PTC(PIN_PB1)); + + MySerial.begin(115200); + MySerial.println("Hello World!"); +} + +void loop() { + // put your main code here, to run repeatedly: + ptc_process(millis()); // main ptc task, requires regular calls +} + + + +// callbacks that are called by ptc_process at different points to ease user interaction +void ptc_event_cb_touch(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_TOUCH_DETECT == eventType) { + MySerial.print("node touched:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_TOUCH_RELEASE == eventType) { + MySerial.print("node released:"); + MySerial.println(ptc_get_node_id(node)); + } +} + +void ptc_event_cb_conversion(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_CONV_MUTUAL_CMPL == eventType) { + ptc_set_next_conversion_type(NODE_SELFCAP_SHIELD_bm); + } else if (PTC_CB_EVENT_CONV_SHIELD_CMPL == eventType) { + ptc_set_next_conversion_type(NODE_MUTUAL_bm); + } + (void)node; // remove unused warning +} + +void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_ERR_CALIB_LOW == eventType) { + MySerial.print("Calib error, Cc too low."); + } else if (PTC_CB_EVENT_ERR_CALIB_HIGH == eventType) { + MySerial.print("Calib error, Cc too high."); + } else if (PTC_CB_EVENT_ERR_CALIB_TO == eventType) { + MySerial.print("Calib error, calculation timeout."); + } else { + MySerial.print("Calib Successful."); + } + MySerial.print(" Node: "); + MySerial.println(ptc_get_node_id(node)); +} diff --git a/megaavr/libraries/PTC/examples/selfcap/selfcap.ino b/megaavr/libraries/PTC/examples/selfcap/selfcap.ino new file mode 100644 index 00000000..ee9e02f2 --- /dev/null +++ b/megaavr/libraries/PTC/examples/selfcap/selfcap.ino @@ -0,0 +1,62 @@ +#include +/* + * This example creates three different sensing nodes on pins + * PA4, PA5 and PA6. They get the IDs 0, 1 and 2 respectively. + * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can + * be used for quick actions, like switching a pin or variable, + * but it is recommended to use PTC_CB_EVENT_CONV_SELF_CMPL, as + * otherwise the handling of the successing nodes would be delayed. + */ +#define MySerial Serial + +cap_sensor_t nodes[3]; + +void setup() { + MySerial.begin(115200); + + // this puts the node on the list and initializes to default values + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), 0); + ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), 0); + ptc_add_selfcap_node(&nodes[2], PIN_TO_PTC(PIN_PA6), 0); + + // Make sure Serial works + MySerial.println("Hello World!"); +} + +void loop() { + ptc_process(millis()); // main ptc task, requires regular calls +} + + + +// callbacks that are called by ptc_process at different points to ease user interaction +void ptc_event_cb_touch(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_TOUCH_DETECT == eventType) { + MySerial.print("node touched:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_TOUCH_RELEASE == eventType) { + MySerial.print("node released:"); + MySerial.println(ptc_get_node_id(node)); + } +} + +void ptc_event_cb_conversion(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_CONV_TYPE_CMPL_MSK & eventType) { + // Do more complex things here + } + (void)node; // remove unused warning +} + +void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_ERR_CALIB_LOW == eventType) { + MySerial.print("Calib error, Cc too low."); + } else if (PTC_CB_EVENT_ERR_CALIB_HIGH == eventType) { + MySerial.print("Calib error, Cc too high."); + } else if (PTC_CB_EVENT_ERR_CALIB_TO == eventType) { + MySerial.print("Calib error, calculation timeout."); + } else { + MySerial.print("Calib Successful."); + } + MySerial.print(" Node: "); + MySerial.println(ptc_get_node_id(node)); +} \ No newline at end of file diff --git a/megaavr/libraries/PTC/examples/selfcap_other_nodes_shield/selfcap_other_nodes_shield.ino b/megaavr/libraries/PTC/examples/selfcap_other_nodes_shield/selfcap_other_nodes_shield.ino new file mode 100644 index 00000000..3e17275a --- /dev/null +++ b/megaavr/libraries/PTC/examples/selfcap_other_nodes_shield/selfcap_other_nodes_shield.ino @@ -0,0 +1,56 @@ +#include +/* + * This example creates three different sensing nodes on pins + * PA4, PA5 and PA6. They get the IDs 0, 1 and 2 respectively. + * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can + * be used for quick actions, like switching a pin or variable, + * but it is recommended to use PTC_CB_EVENT_CONV_SELF_CMPL, as + * otherwise the handling of the successing nodes would be delayed. + * This example demonstrates how to use the other sensor nodes + * as shield. This improves the signal-to-noise ratio. + */ +#define MySerial Serial + +cap_sensor_t nodes[3]; + +void setup() { + MySerial.begin(115200); + + // this puts the node on the list and initializes to default values + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA5) | PIN_TO_PTC(PIN_PA6)); + ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PA4) | PIN_TO_PTC(PIN_PA6)); + ptc_add_selfcap_node(&nodes[2], PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PA4) | PIN_TO_PTC(PIN_PA5)); + + + // Make sure Serial works + MySerial.println("Hello World!"); +} + +void loop() { + ptc_process(millis()); // main ptc task, requires regular calls +} + +// callback that is called by ptc_process at different points to ease user interaction +void ptc_event_callback(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_TOUCH_DETECT == eventType) { + MySerial.print("node touched:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_TOUCH_RELEASE == eventType) { + MySerial.print("node released:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_CONV_SELF_CMPL == eventType) { + // Do more complex things here + } else if (PTC_CB_EVENT_CONV_CALIB & eventType) { + if (PTC_CB_EVENT_ERR_CALIB_LOW == eventType) { + MySerial.print("Calib error, Cc too low."); + } else if (PTC_CB_EVENT_ERR_CALIB_HIGH == eventType) { + MySerial.print("Calib error, Cc too high."); + } else if (PTC_CB_EVENT_ERR_CALIB_TO == eventType) { + MySerial.print("Calib error, calculation timeout."); + } else { + MySerial.print("Calib Successful."); + } + MySerial.print(" Node: "); + MySerial.println(ptc_get_node_id(node)); + } +} diff --git a/megaavr/libraries/PTC/examples/selfcap_shield_lowpower/selfcap_shield_lowpower.ino b/megaavr/libraries/PTC/examples/selfcap_shield_lowpower/selfcap_shield_lowpower.ino new file mode 100644 index 00000000..9216a677 --- /dev/null +++ b/megaavr/libraries/PTC/examples/selfcap_shield_lowpower/selfcap_shield_lowpower.ino @@ -0,0 +1,122 @@ +#include +#include +/* + * This example creates two normal nodes and a combined node + * on the pins PA4 and PA6 with PA5 acting as shield. Had to use + * the shield, as the compensation went too high for the combined + * node. + * As it is not possible to change the node when the CPU is asleep + * a combined node is used to wait for touch events. + * The first nodes have the IDs 0 and 1, while the lp_node has + * the ID 2. + * As millis() is not incremented while the CPU is asleep, it will + * take the Chip 10 seconds after a wake event before it goes back + * to sleep. + * ptc_process will also fire either PTC_CB_EVENT_WAKE_TOUCH or + * PTC_CB_EVENT_WAKE_NO_TOUCH after a conversion on the lp-node + * has been finished. + */ +#define MySerial Serial + +cap_sensor_t nodes[2]; +cap_sensor_t lp_node; // as an example, could also be part of the array + +void setup() { + MySerial.begin(115200); + + // this puts the node on the list and initializes to default values + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA5)); + ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PA5)); + + // as each node requires some calibration and drift, it is recommended + // to keep it "in the loop" during normal operation. + // However, it is possible to disable it until it is needed. + // This is also a so called "lumped" node. This allows to check all nodes at the same time. + // Useful for low-power monitoring for user-input + ptc_add_selfcap_node(&lp_node, PIN_TO_PTC(PIN_PA4) | PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PA5)); + + + + // In low-power mode, the ADC is triggered based on positive Event fanks + // Due to the big variety of possible Event sources, it is up to the + // User to set up the Routing of the Event System. + // This example will use the RTC PIT as Event generator, as it allows us + // to use the low-power oscillator. + + // Enable PIT for the EVSYS with 32kHz / 4096 giving us about 8Hz + RTC.PITCTRLA = RTC_PITEN_bm; + EVSYS.ASYNCCH3 = EVSYS_ASYNCCH3_PIT_DIV4096_gc; + EVSYS.ASYNCUSER1 = EVSYS_ASYNCUSER1_ASYNCCH3_gc; + + // For Debugging of EVSYS (to make sure there is a clock) + //EVSYS.ASYNCUSER8 = EVSYS_ASYNCUSER8_ASYNCCH3_gc; + //PORTMUX.CTRLA = PORTMUX_EVOUT0_bm; + //pinMode(PIN_PA2, OUTPUT); + + + // Stand-by sleep + SLPCTRL.CTRLA = SLEEP_MODE_STANDBY | SLPCTRL_SEN_bm; + + // Make sure Serial works + MySerial.println("Hello World!"); +} + +uint32_t nextSleep = 10000; + +void loop() { + uint32_t ms = millis(); + ptc_process(ms); // main ptc task, requires regular calls + if (ms > nextSleep) { + nextSleep = ms + 10000; + MySerial.println("Sleepy..."); + MySerial.flush(); // empty the Serial TX buffer + + ptc_lp_init(&lp_node); // Will set STARTEI bit to start conversions + while (ptc_lp_was_waken() != PTC_LIB_WAS_WAKEN) { + sleep_cpu(); // Make sure the ADC has woken the CPU, otherwise, go back to sleep + } // The library does not filter the wake-up events. The user must make sure + // there are no detected touches before going back to sleep. + MySerial.println("I'm Awake!"); + ptc_lp_disable(); + } +} + +void ptc_event_cb_touch(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_TOUCH_DETECT == eventType) { + MySerial.print("node touched:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_TOUCH_RELEASE == eventType) { + MySerial.print("node released:"); + MySerial.println(ptc_get_node_id(node)); + } +} + +void ptc_event_cb_conversion(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_CONV_TYPE_CMPL_MSK == eventType) { + // Do more complex things here + } + (void)node; // remove unused warning +} + +void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_ERR_CALIB_LOW == eventType) { + MySerial.print("Calib error, Cc too low."); + } else if (PTC_CB_EVENT_ERR_CALIB_HIGH == eventType) { + MySerial.print("Calib error, Cc too high."); + } else if (PTC_CB_EVENT_ERR_CALIB_TO == eventType) { + MySerial.print("Calib error, calculation timeout."); + } else { + MySerial.print("Calib Successful."); + } + MySerial.print(" Node: "); + MySerial.println(ptc_get_node_id(node)); +} + +void ptc_event_cb_wake(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_WAKE_TOUCH == eventType) { + // True if the node was touched when a wakeup occured + } else if (PTC_CB_EVENT_WAKE_NO_TOUCH == eventType) { + // True if the node was no touch when a wakeup occured + } + (void)node; // remove unused warning +} diff --git a/megaavr/libraries/PTC/examples/selfcap_with_dedicated_shield/selfcap_with_dedicated_shield.ino b/megaavr/libraries/PTC/examples/selfcap_with_dedicated_shield/selfcap_with_dedicated_shield.ino new file mode 100644 index 00000000..09f92ce7 --- /dev/null +++ b/megaavr/libraries/PTC/examples/selfcap_with_dedicated_shield/selfcap_with_dedicated_shield.ino @@ -0,0 +1,61 @@ +#include +/* + * This example creates three different sensing nodes on pins + * PA4 and PA5. PA6 acts as a dedicated shield pin. They get the IDs 0 and 1 respectively. + * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can + * be used for quick actions, like switching a pin or variable, + * but it is recommended to use PTC_CB_EVENT_CONV_SELF_CMPL, as + * otherwise the handling of the successing nodes would be delayed. + * This example demonstrates how to use the other sensor nodes + * as shield. This improves the signal-to-noise ratio. + */ +#define MySerial Serial + +cap_sensor_t nodes[2]; + +void setup() { + MySerial.begin(115200); + + // this puts the node on the list and initializes to default values + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA6)); + ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PA6)); + + // Make sure Serial works + MySerial.println("Hello World!"); +} + +void loop() { + ptc_process(millis()); // main ptc task, requires regular calls +} + +// callback that is called by ptc_process at different points to ease user interaction +void ptc_event_cb_touch(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_TOUCH_DETECT == eventType) { + MySerial.print("node touched:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_TOUCH_RELEASE == eventType) { + MySerial.print("node released:"); + MySerial.println(ptc_get_node_id(node)); + } +} + +void ptc_event_cb_conversion(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_CONV_TYPE_CMPL_MSK == eventType) { + // Do more complex things here + } + (void)node; // remove unused warning +} + +void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_ERR_CALIB_LOW == eventType) { + MySerial.print("Calib error, Cc too low."); + } else if (PTC_CB_EVENT_ERR_CALIB_HIGH == eventType) { + MySerial.print("Calib error, Cc too high."); + } else if (PTC_CB_EVENT_ERR_CALIB_TO == eventType) { + MySerial.print("Calib error, calculation timeout."); + } else { + MySerial.print("Calib Successful."); + } + MySerial.print(" Node: "); + MySerial.println(ptc_get_node_id(node)); +} diff --git a/megaavr/libraries/PTC/examples/temperature_mutual/temperature_mutual.ino b/megaavr/libraries/PTC/examples/temperature_mutual/temperature_mutual.ino new file mode 100644 index 00000000..9da047ff --- /dev/null +++ b/megaavr/libraries/PTC/examples/temperature_mutual/temperature_mutual.ino @@ -0,0 +1,70 @@ +#include +/* + * This example creates two different sensing nodes. + * PA4 and PA5 are on the Y-Lines, PA6 is the X-Line. + * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can + * be used for quick actions, like switching a pin or variable, + * but it is recommended to use PTC_CB_EVENT_CONV_MUTUAL_CMPL, as + * otherwise the handling of the successing nodes would be delayed. + */ +#define MySerial Serial + +cap_sensor_t nodes[3]; + +void setup() { + MySerial.begin(115200); + + // this puts the node on the list and initializes to default values + ptc_add_mutualcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA7)); + ptc_add_mutualcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PA7)); + ptc_add_mutualcap_node(&nodes[2], PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PA7)); + + + // Make sure Serial works + MySerial.println("Hello World!"); +} + +void loop() { + ptc_process(millis()); // main ptc task, requires regular calls +} + +// callback that is called by ptc_process at different points to ease user interaction +void ptc_event_callback(const ptc_cb_event_t eventType, cap_sensor_t* node) { + if (PTC_CB_EVENT_TOUCH_DETECT == eventType) { + MySerial.print("node touched:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_TOUCH_RELEASE == eventType) { + MySerial.print("node released:"); + MySerial.println(ptc_get_node_id(node)); + } else if (PTC_CB_EVENT_CONV_MUTUAL_CMPL == eventType) { + while (ptc_suspend() != PTC_LIB_SUCCESS) {} + ptc_init_ADC0(); + analogReference(INTERNAL1V1); + static int32_t last_temp; + int8_t sigrow_offset = SIGROW.TEMPSENSE1; // Read signed value from signature row + uint8_t sigrow_gain = SIGROW.TEMPSENSE0; // Read unsigned value from signature row + int32_t tempRaw = analogRead(ADC_TEMPERATURE) - sigrow_offset; + tempRaw *= sigrow_gain; + tempRaw += 0x80; + tempRaw >>= 8; + tempRaw -= 273; // From Kelvin to Celsius + if (last_temp != tempRaw) { + last_temp = tempRaw; + MySerial.print("IC Temp: "); + MySerial.println(tempRaw); + } + ptc_resume(); // Enables the acquisition + } else if (PTC_CB_EVENT_CONV_CALIB & eventType) { + if (PTC_CB_EVENT_ERR_CALIB_LOW == eventType) { + MySerial.print("Calib error, Cc too low."); + } else if (PTC_CB_EVENT_ERR_CALIB_HIGH == eventType) { + MySerial.print("Calib error, Cc too high."); + } else if (PTC_CB_EVENT_ERR_CALIB_TO == eventType) { + MySerial.print("Calib error, calculation timeout."); + } else { + MySerial.print("Calib Successful."); + } + MySerial.print(" Node: "); + MySerial.println(ptc_get_node_id(node)); + } +} diff --git a/megaavr/libraries/PTC/keywords.txt b/megaavr/libraries/PTC/keywords.txt new file mode 100644 index 00000000..78c0124b --- /dev/null +++ b/megaavr/libraries/PTC/keywords.txt @@ -0,0 +1,91 @@ +####################################### +# Syntax Coloring Map For the ptc library +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### +ptc_ch_bm_t KEYWORD1 +ptc_node_state_t KEYWORD1 +ptc_sm_t KEYWORD1 +ptc_ret_t KEYWORD1 +ptc_lib_t KEYWORD1 +cap_sensor_t KEYWORD1 +ptc_lib_sm_set_t KEYWORD1 +ptc_cb_event_t KEYWORD1 +PTC_t KEYWORD1 + + + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +ptc_process KEYWORD2 +ptc_event_callback KEYWORD2 +ptc_event_cb_touch KEYWORD2 +ptc_event_cb_conversion KEYWORD2 +ptc_event_cb_calibration KEYWORD2 +ptc_event_cb_wake KEYWORD2 +ptc_event_cb_error KEYWORD2 +ptc_enable_node KEYWORD2 +ptc_disable_node KEYWORD2 +ptc_set_next_conversion_type KEYWORD2 +ptc_node_set_thresholds KEYWORD2 +ptc_node_set_resistor KEYWORD2 +ptc_node_set_prescaler KEYWORD2 +ptc_node_set_gain KEYWORD2 +ptc_add_selfcap_node KEYWORD2 +ptc_add_mutualcap_node KEYWORD2 +ptc_suspend KEYWORD2 +ptc_resume KEYWORD2 +ptc_get_node_cc_fempto KEYWORD2 +ptc_get_node_xCh_bm KEYWORD2 +ptc_get_node_yCh_bm KEYWORD2 +ptc_get_node_sensor_value KEYWORD2 +ptc_get_node_touched KEYWORD2 +ptc_get_node_sm KEYWORD2 +ptc_get_node_delta KEYWORD2 +ptc_get_node_state KEYWORD2 +ptc_node_request_recal KEYWORD2 +ptc_init_ADC0 KEYWORD2 +ptc_lp_init KEYWORD2 +ptc_lp_disable KEYWORD2 +ptc_lp_was_waken KEYWORD2 +ptc_get_sm_settings KEYWORD2 +ptc_event_cb_touch KEYWORD2 +ptc_event_cb_wake KEYWORD2 +ptc_event_cb_conversion KEYWORD2 +ptc_event_cb_calibration KEYWORD2 +ptc_event_cb_error KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +####################################### +PTC_CB_EVENT_TOUCH LITERAL1 +PTC_CB_EVENT_TOUCH_DETECT LITERAL1 +PTC_CB_EVENT_TOUCH_RELEASE LITERAL1 +PTC_CB_EVENT_TOUCH_WAKE LITERAL1 +PTC_CB_EVENT_WAKE_TOUCH LITERAL1 +PTC_CB_EVENT_WAKE_NO_TOUCH LITERAL1 +PTC_CB_EVENT_CONV_CMPL LITERAL1 +PTC_CB_EVENT_CONV_MUTUAL_CMPL LITERAL1 +PTC_CB_EVENT_CONV_SELF_CMPL LITERAL1 +PTC_CB_EVENT_CONV_SHIELD_CMPL LITERAL1 +PTC_CB_EVENT_CONV_TYPE_CMPL_MSK LITERAL1 +PTC_CB_EVENT_CONV_CALIB LITERAL1 +PTC_CB_EVENT_ERR_CALIB_LOW LITERAL1 +PTC_CB_EVENT_ERR_CALIB_HIGH LITERAL1 +PTC_CB_EVENT_ERR_CALIB_TO LITERAL1 +PTC_CB_EVENT_ERR_CALIB_MSK LITERAL1 +NODE_TYPE_NOCONV_bm LITERAL1 +NODE_MUTUAL_bm LITERAL1 +NODE_SELFCAP_bm LITERAL1 +NODE_SHIELD_bm LITERAL1 +NODE_SELFCAP_SHIELD_bm LITERAL1 +NODE_TYPE_bm LITERAL1 + diff --git a/megaavr/libraries/PTC/library.properties b/megaavr/libraries/PTC/library.properties new file mode 100644 index 00000000..e954c009 --- /dev/null +++ b/megaavr/libraries/PTC/library.properties @@ -0,0 +1,10 @@ +name=PTC +version=1.0.0 +author=MX682X +maintainer=MX682X +sentence=This library (experimental) allows a straightforward use of the Peripheral Touch Controller (PTC) +paragraph=The PTC contains hardware to assist in touch detection. This library helps taking leverage of this peripheral
Supports only the 1-Series AtTiny chips.
Tested with 1614 so far
feedback appreciated! +category=Sensors +includes=ptc.h +url=https://github.com/MX682X/ptc_touch +architectures=megaavr \ No newline at end of file diff --git a/megaavr/libraries/PTC/src/ptc.c b/megaavr/libraries/PTC/src/ptc.c new file mode 100644 index 00000000..74164f1a --- /dev/null +++ b/megaavr/libraries/PTC/src/ptc.c @@ -0,0 +1,1014 @@ +/* + * Refer to ptc_touch.h file for copyright, changelog, usage and license information + */ + + +#include "ptc.h" + + + +// The following functions are used internally only. +// get the pointer of the last valid node struct can be "firstNode" +cap_sensor_t* ptc_get_last_node(void); + + +uint8_t ptc_append_node(cap_sensor_t* pNewNode); + +// Prepares the ADC/PTC registers for the next conversion batch, starts conversion with firstNode +void ptc_init_conversion(uint8_t nodeType); + +// Strts the conversion of the node that is passed as argument +void ptc_start_conversion (cap_sensor_t* node); + +// State-Machine Handler, interprets the measured values and decides if key is "pressed" +void ptc_process_node_sm (cap_sensor_t* node); + +// Helper function +void ptc_process_measurement (cap_sensor_t* node); + +// Handles (initial) calibration +uint8_t ptc_process_calibrate (cap_sensor_t* node); + +// Handles adjustment of the reference value when a button is not pressed +void ptc_process_adjust (); + +void ptc_set_registers(cap_sensor_t* node); + + +cap_sensor_t *firstNode = NULL; +cap_sensor_t *lowPowerNode = NULL; + +volatile cap_sensor_t *currConvNode = NULL; + +ptc_node_type_t currConvType = 0; // does not "remember" if type was low-power +ptc_node_type_t nextConvType = 0; + +uint16_t acqPeriod = 20; // Period in ms until a new acquision is started +uint16_t lastAcqTime = 0; // millis value of last Acqusition (16-bit) + +ptc_freq_t freq_select = 0; /* FREQ_SEL_0 to FREQ_SEL_15, FREQ_SEL_SPREAD -> CTRLD */ + +volatile ptc_lib_t ptc_lib_state = PTC_LIB_IDLE; + + +/* + * Global settings for the state-machine applied on every node. + * Pointer to this struct can be retrieved by ptc_get_sm_settings() + * + */ +ptc_lib_sm_set_t ptc_sm_settings = { + .force_recal_delta = 150, + .touched_detect_nom = 3, + .untouched_detect_nom = 3, + .touched_max_nom = 200, + .drift_up_nom = 15, + .drift_down_nom = 15 +}; + +ptc_lib_sm_set_t* ptc_get_sm_settings() { + return &ptc_sm_settings; +} + +// Analog gain Values, extracted with a debugger + +const uint8_t ptc_a_gain_lut[] = { + 0x3F, 0x1C, 0x0B, + 0x05, 0x03, 0x01, +}; + + + + + +//#if (NUM_TOTAL_PINS > 32) +//uint16_t ptc_touch_stack = 0; +//#else +//uint8_t ptc_touch_stack = 0; +//#endif + +// Workarounds to make the code work with DAs... Argh. +#if defined (__PTC_DA__) + #ifndef ADC_SAMPNUM_ACC1_gc + #define ADC_SAMPNUM_ACC1_gc ADC_SAMPNUM_NONE_gc + #endif + #ifndef ADC_REFSEL_VDDREF_gc + #define ADC_REFSEL_VDDREF_gc 0x00 + #endif +#endif + +#if defined (__PTC_Tiny__) + #define PTC_DEFAULT_SC_CC 0x0567 + #define PTC_DEFAULT_MC_CC 0x0234 +#elif defined (__PTC_DA__) + #define PTC_DEFAULT_SC_CC 0x00F0 + #define PTC_DEFAULT_MC_CC 0x00A0 +#endif +/* + * Different Functions that change the behaviour of the supplied node + */ +__attribute__ ((weak)) +void ptc_event_callback(const ptc_cb_event_t eventType, cap_sensor_t* node) { + (void)eventType; + (void)node; +} +__attribute__ ((weak, alias("ptc_event_callback"))) void ptc_event_cb_touch(const ptc_cb_event_t eventType, cap_sensor_t* node); +__attribute__ ((weak, alias("ptc_event_callback"))) void ptc_event_cb_wake(const ptc_cb_event_t eventType, cap_sensor_t* node); +__attribute__ ((weak, alias("ptc_event_callback"))) void ptc_event_cb_conversion(const ptc_cb_event_t eventType, cap_sensor_t* node); +__attribute__ ((weak, alias("ptc_event_callback"))) void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node); +__attribute__ ((weak, alias("ptc_event_callback"))) void ptc_event_cb_error(const ptc_cb_event_t eventType, cap_sensor_t* node); + + + +// Set the threshold for touch detection and away from touch for a node +uint8_t ptc_node_set_thresholds (cap_sensor_t* node, int16_t th_in, int16_t th_out) { + if (NULL == node) + return PTC_LIB_BAD_POINTER; + + node->touch_in_th = th_in; + node->touch_out_th = th_out; + return PTC_LIB_SUCCESS; +} + + +// Change Resistor Setting. Note: Only has an effect on mutual sensors +uint8_t ptc_node_set_resistor(cap_sensor_t* node, uint8_t res) { + PTC_CHECK_FOR_BAD_POINTER(node); + + if (res > RSEL_MAX) + return PTC_LIB_BAD_ARGUMENT; + + + if (node->type & NODE_MUTUAL_bm) { + uint8_t presc = node->hw_rsel_presc & 0x0F; + presc |= ((res & 0x0F) << 4); + node->hw_rsel_presc = presc; + return PTC_LIB_SUCCESS; + } + + return PTC_LIB_BAD_ARGUMENT; +} + + +// Change preschaler. Recomended ADC frequency: < 1.5MHz, but max 3 factors below +uint8_t ptc_node_set_prescaler(cap_sensor_t* node, uint8_t presc) { + PTC_CHECK_FOR_BAD_POINTER(node); + + if ((presc > (PTC_PRESC_DEFAULT + 2)) || (presc < PTC_PRESC_DEFAULT)) + return PTC_LIB_BAD_ARGUMENT; + + uint8_t res = node->hw_rsel_presc & 0xF0; + res |= (presc & 0x0F); + node->hw_rsel_presc = res; + return PTC_LIB_SUCCESS; +} + + +uint8_t ptc_node_set_gain(cap_sensor_t* node, uint8_t aGain, uint8_t dGain) { + PTC_CHECK_FOR_BAD_POINTER(node); + if (aGain > 0x05) { + if (__builtin_constant_p(aGain)) + badArg("Analog Gain too high. Max Analog Gain Value is 0x05 (equals 32x)"); + return PTC_LIB_BAD_ARGUMENT; + } + + if (dGain > 0x06) { + if (__builtin_constant_p(dGain)) + badArg("Digital Gain too high. Max Digital Gain Value is 0x06 (equals 64x)"); + return PTC_LIB_BAD_ARGUMENT; + } + node->hw_a_d_gain = NODE_GAIN(aGain, dGain); + return PTC_LIB_SUCCESS; +} + + + +/* + * Two functions to suspend and resume of the normal PTC operation, however, + * this functions will only work when no acqusition is in progress. Neither + * when the library is in low-power mode. This is due to the fact that the + * low-power mode relies on the registers being set once, at the low-power + * initialization. Thus, the user has to disable LP mode, then suspend, resume + * and then re-init low-power mode. + */ +uint8_t ptc_suspend(void) { + if (PTC_LIB_IDLE == ptc_lib_state) { // allow disabling only outside conversions + ptc_lib_state = PTC_LIB_SUSPENDED; + #if defined (__PTC_Tiny__) + PTC.CTRLP = 0x00; + #elif defined (__PTC_DA__) + PTC.CTRLA = 0x00; + #endif + return PTC_LIB_SUCCESS; + } + return PTC_LIB_WRONG_STATE; +} + +void ptc_resume(void) { + if (PTC_LIB_SUSPENDED == ptc_lib_state) { + ptc_lib_state = PTC_LIB_IDLE; + } +} + + +// own implementation of mTC's initADC0(), as the one that comes with the core does not overwrite +// sample number (CTRLB) +void ptc_init_ADC0(void) { + PTC_t* pPTC; + _fastPtr_d(pPTC, &PTC); + #if defined (__PTC_Tiny__) + #if F_CPU > 24000000 + pPTC->CTRLC = ADC_PRESC_DIV32_gc | ADC_REFSEL_VDDREF_gc | ADC_SAMPCAP_bm; + #elif F_CPU >= 12000000 + pPTC->CTRLC = ADC_PRESC_DIV16_gc | ADC_REFSEL_VDDREF_gc | ADC_SAMPCAP_bm; + #elif F_CPU >= 6000000 + pPTC->CTRLC = ADC_PRESC_DIV8_gc | ADC_REFSEL_VDDREF_gc | ADC_SAMPCAP_bm; + #elif F_CPU >= 3000000 + pPTC->CTRLC = ADC_PRESC_DIV4_gc | ADC_REFSEL_VDDREF_gc | ADC_SAMPCAP_bm; + #else + pPTC->CTRLC = ADC_PRESC_DIV2_gc | ADC_REFSEL_VDDREF_gc | ADC_SAMPCAP_bm; + #endif + #if (F_CPU == 6000000 || F_CPU == 12000000 || F_CPU == 24000000 || F_CPU ==25000000) + pPTC->SAMPCTRL = (7); + #elif (F_CPU == 5000000 || F_CPU == 10000000 || F_CPU == 20000000) + pPTC->SAMPCTRL = (13); + #else + pPTC->SAMPCTRL = (10); + #endif + pPTC->CTRLD = ADC_INITDLY_DLY16_gc; + pPTC->CTRLB = ADC_SAMPNUM_ACC1_gc; + pPTC->CTRLA = ADC_ENABLE_bm; + #elif defined (__PTC_DA__) + + #endif +} + + +// a helper function to reduce copy-pasting between self and mutual initialisations +void ptc_add_node_common(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc_ch_bm_t xCh); + +// if xCh > 0, with shield, otherwise usual selfcap +uint8_t ptc_add_selfcap_node_asserted(cap_sensor_t* node, const ptc_ch_bm_t yCh, const ptc_ch_bm_t xCh) { + PTC_CHECK_POINTER(node, PTC_LIB_BAD_POINTER); // check not in h file as gcc doesn't know about the memory address + + if (ptc_append_node(node) != PTC_LIB_SUCCESS) + return PTC_LIB_BAD_POINTER; + + ptc_add_node_common(node, yCh, xCh); + + if (xCh > 0) node->type = NODE_SELFCAP_SHIELD_bm; + else node->type = NODE_SELFCAP_bm; + + + node->touch_in_th = 20; + node->touch_out_th = 10; + node->hw_compCaps = PTC_DEFAULT_SC_CC; /* value from official library */ + node->hw_rsel_presc = NODE_RSEL_PRSC(RSEL_VAL_0, PTC_PRESC_DEFAULT); + + return PTC_LIB_SUCCESS; +} + + +uint8_t ptc_add_mutualcap_node_asserted(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc_ch_bm_t xCh) { + PTC_CHECK_POINTER(node, PTC_LIB_BAD_POINTER); // check not in h file as gcc doesn't know about the memory address + + if (ptc_append_node(node) != PTC_LIB_SUCCESS) + return PTC_LIB_BAD_POINTER; + + ptc_add_node_common(node, yCh, xCh); + + node->type = NODE_MUTUAL_bm; + + node->touch_in_th = 10; + node->touch_out_th = 5; + node->hw_compCaps = PTC_DEFAULT_MC_CC; /* value from official library */ + node->hw_rsel_presc = NODE_RSEL_PRSC(RSEL_VAL_100, PTC_PRESC_DEFAULT); + + return PTC_LIB_SUCCESS; +} + +void ptc_add_node_common(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc_ch_bm_t xCh) { + + node->stateMachine = PTC_SM_NOINIT_CAL; + node->hw_xCh_bm = xCh; + node->hw_yCh_bm = yCh; + node->hw_a_d_gain = NODE_GAIN(ADC_SAMPNUM_ACC1_gc, ADC_SAMPNUM_ACC16_gc); + + #if defined (__PTC_Tiny__) + uint8_t* src = (uint8_t*)&PTC.PIN_OVR; // Get "addresses" of relevant elements + uint8_t* orY = (uint8_t*)&yCh; // This allows to split the uint64_t to an access of one byte at a time. + uint8_t* orX = (uint8_t*)&xCh; // on AtTinies, the compiler unrolls the loop to two iterations + uint8_t bit_pos = 0; // counts from 0 to "highest PTC pin number" + for (uint8_t i = 0; i < sizeof(ptc_ch_bm_t); i++) { + // this equals PTC.PIN_OVR |= (xCh | yCh);, but more friendly for CPU-registers on DAs with uint64_t + uint8_t temp_bm = *(orY++) | *(orX++); + *(src) |= temp_bm; + src++; + + for (uint8_t j = 0; j < 8; j++) { + if (temp_bm & 0x01) { + if (bit_pos < sizeof(ptc_ch_to_pin)) { // avoid accesses outside of the array, which can be smaller then the bit-map + uint8_t offset = ptc_ch_to_pin[bit_pos]; + if (offset != 0) { + uint16_t basePointer = (uint16_t)&PORTA; + uint8_t* portSettings = (uint8_t*)(basePointer + offset); + (*portSettings) = PORT_ISC_INPUT_DISABLE_gc; + } + } + } + bit_pos++; + temp_bm >>= 1; + } + } + #elif defined (__PTC_DA__) +// Not started yet + #endif +} + + + +uint8_t ptc_enable_node(cap_sensor_t* node) { + PTC_CHECK_FOR_BAD_POINTER(node); + + node->state.disabled = 0; + return PTC_LIB_SUCCESS; +} + +// Will finish conversion, but not start a new one +uint8_t ptc_disable_node(cap_sensor_t* node) { + PTC_CHECK_FOR_BAD_POINTER(node); + + node->state.disabled = 1; + return PTC_LIB_SUCCESS; +} + + + +// Make the ADC wake up the CPU after the next conversion to allow a drift +uint8_t ptc_lp_force_drift(void) { + if (NULL == lowPowerNode) + return PTC_LIB_WRONG_STATE; + + if (PTC.INTCTRL == ADC_WCMP_bm) { // only do something when we are waiting for a value over the threshold + PTC.INTCTRL = (ADC_RESRDY_bm | ADC_WCMP_bm); + } + + return PTC_LIB_SUCCESS; +} + + + + +// select node type to convert next (Selfcap, Shield, Mutual) +// when not specified, the type of the "firstNode" is always used +void ptc_set_next_conversion_type(ptc_node_type_t type) { + nextConvType = type; +} + +// ev_ch: bitmask of required channels to be used for ADC0/PTC, e.g. channel 2 and 5 -> 0x24 +// this would allow to periodically start ADC0/PIT by a timer without sleep +uint8_t ptc_lp_init(cap_sensor_t* node) { + PTC_CHECK_FOR_BAD_POINTER(node); + + if (NULL != lowPowerNode) + return PTC_LIB_WRONG_STATE; + + node->state.low_power = 1; + node->stateMachine = PTC_SM_LOW_POWER; + + lowPowerNode = node; + + ptc_init_conversion(node->type); + + return PTC_LIB_SUCCESS; +} + +uint8_t ptc_lp_disable(void) { + if (NULL == lowPowerNode) + return PTC_LIB_WRONG_STATE; + + PTC.INTCTRL = 0; + PTC.EVCTRL = 0; + + lowPowerNode->state.low_power = 0; + lowPowerNode->state.win_comp = 0; + + return PTC_LIB_SUCCESS; +} + +// use this in a conversion complete callback to see if the window comparator was triggered +uint8_t ptc_lp_was_waken(void) { + if (NULL == lowPowerNode) + return PTC_LIB_WRONG_STATE; + + if (1 == lowPowerNode->state.win_comp) + return PTC_LIB_WAS_WAKEN; + else + return PTC_LIB_ASLEEP; + + return PTC_LIB_ERROR; +} + + + +/* not recommended to use, as the calculation is bloated */ +uint16_t ptc_get_node_cc_fempto (cap_sensor_t* node) { + if (NULL == node) + return 0; + + uint16_t retVal = 0; + uint16_t comp = node->hw_compCaps; + for (uint8_t i = 0; i < 3; i++) { + retVal /= 10; /* "skips" last addition */ + uint8_t temp = comp & 0x0F; + retVal += (temp * 675); + comp >>= 4; /* select next field */ + } + retVal += (comp & 0x03) * 6750; + comp >>= 2; + retVal += (comp & 0x03) * 6200; + return retVal; //max 51000 +} + + +// pass the time, e.g. return value of millis to the function instead of hving a +// millis in here. improves portability +void ptc_process (uint16_t currTime) { + if ((PTC_LIB_CONV_WCMP | PTC_LIB_CONV_LP) & ptc_lib_state) { + if (NULL != lowPowerNode) { + if (PTC_LIB_CONV_WCMP == ptc_lib_state) { + ptc_event_cb_wake(PTC_CB_EVENT_WAKE_TOUCH, lowPowerNode); + } else { + ptc_event_cb_wake(PTC_CB_EVENT_WAKE_NO_TOUCH, lowPowerNode); + } + ptc_process_measurement(lowPowerNode); + if (NULL == lowPowerNode) { // Due to callbacks, low Power node may have been set to NULL by a disable + currConvType = NODE_TYPE_NOCONV_bm; + ptc_lib_state = PTC_LIB_IDLE; + } else { + PTC.INTCTRL = ADC_WCMP_bm; + ptc_lib_state = PTC_LIB_EVENT; + } + } + } else if (PTC_LIB_CONV_COMPL == ptc_lib_state) { + ptc_lib_state = PTC_LIB_IDLE; + for (cap_sensor_t* node = firstNode; node != NULL; node = node->nextNode) { + ptc_process_measurement(node); + } + ptc_event_cb_conversion((PTC_CB_EVENT_CONV_CMPL | currConvType), (cap_sensor_t *)currConvNode); + currConvType = NODE_TYPE_NOCONV_bm; + } + + if ((currTime - lastAcqTime) >= acqPeriod) { + lastAcqTime += acqPeriod; + if (PTC_LIB_IDLE == ptc_lib_state) { + uint8_t convType; + if (nextConvType == 0) { + convType = firstNode->type; + } else { + convType = nextConvType; + } + ptc_init_conversion(convType); + } + } +} + + + +/* + * Internal functions only below. Not part of the API + */ +void ptc_process_measurement (cap_sensor_t* node) { + if (node->state.error) + return; + + if (node->state.data_ready != 0) { + node->state.data_ready = 0; + ptc_process_node_sm(node); + ptc_event_cb_conversion(PTC_CB_EVENT_CONV_CMPL, node); + } +} + + +void ptc_process_node_sm (cap_sensor_t* node) { + uint16_t nodeData = node->sensorData; + uint8_t nodeSM = node->stateMachine; + uint8_t lastChange = node->lastStateChange; + uint16_t reference = node->reference; + int16_t nodeDelta = nodeData - reference; + + if (nodeSM == PTC_SM_NOINIT_CAL) { /* Beginning here */ + uint8_t retVal = ptc_process_calibrate(node); + if (PTC_LIB_CALIB_DONE == retVal) { + node->reference = nodeData; + if (node->state.low_power) { + nodeSM = PTC_SM_LOW_POWER; + } else { + nodeSM = PTC_SM_NO_TOUCH; + } + ptc_event_cb_calibration(PTC_CB_EVENT_CONV_CALIB, node); + } else if (PTC_LIB_SUCCESS != retVal || lastChange > 10) { // if we stay here for over 10 conversions, something didn't went right + node->state.error = 1; + if (PTC_LIB_CALIB_TOO_LOW == retVal) { + ptc_event_cb_calibration(PTC_CB_EVENT_ERR_CALIB_LOW, node); + } else if (PTC_LIB_CALIB_TOO_HIGH == retVal) { + ptc_event_cb_calibration(PTC_CB_EVENT_ERR_CALIB_HIGH, node); + } else { + ptc_event_cb_calibration(PTC_CB_EVENT_ERR_CALIB_TO, node); + } + return; + } else if (PTC_LIB_SUCCESS == retVal) { + // calibration didn't finish (yet), but didn't threw an error + } + + } else if (nodeSM & PTC_SM_RECAL_FLT) { /* if the delta was really low, we might have to recalibrate the caps */ + if (nodeDelta > -150) { + nodeSM = PTC_SM_NO_TOUCH; + } else if (lastChange > 3) { + nodeSM = PTC_SM_NOINIT_CAL; + } + + + } else if (nodeSM & PTC_SM_NT_LOW_FLT) { /* no touch, delta below -1, threshold drift */ + if (nodeDelta > 0) { + nodeSM = PTC_SM_NO_TOUCH; + } else if (nodeDelta < -150) { + nodeSM = PTC_SM_RECAL_FLT; + } else if (lastChange > ptc_sm_settings.drift_down_nom) { /* By using '>' the drift can be disabled with integer overflow */ + node->reference--; + nodeSM = PTC_SM_NO_TOUCH; + } + + + } else if (nodeSM & PTC_SM_NO_TOUCH) { /* default State, no touch */ + if (nodeDelta < -150) { /* if the touch value is way too small, something chnaged with the lines, recal */ + nodeSM = PTC_SM_RECAL_FLT; + } else if (nodeDelta >= node->touch_in_th) { + nodeSM = PTC_SM_TOUCH_IN_FLT; + } else if (reference <= (512 - ptc_sm_settings.force_recal_delta)) { + nodeSM = PTC_SM_NOINIT_CAL; + } else if (reference >= (512 + ptc_sm_settings.force_recal_delta)) { + nodeSM = PTC_SM_NOINIT_CAL; + } else if (nodeDelta < -1) { + nodeSM = PTC_SM_NT_LOW_FLT; + } else if (nodeDelta > 1) { + nodeSM = PTC_SM_NT_HIGH_FLT; + } + + + } else if (nodeSM & PTC_SM_NT_HIGH_FLT) { + if (nodeDelta < 0) { + nodeSM = PTC_SM_NO_TOUCH; + } else if (nodeDelta >= node->touch_in_th) { + nodeSM = PTC_SM_TOUCH_IN_FLT; + } else if (lastChange > ptc_sm_settings.drift_up_nom) { + node->reference++; + nodeSM = PTC_SM_NO_TOUCH; + } + + + } else if (nodeSM & PTC_SM_TOUCH_IN_FLT) { + if (lastChange >= ptc_sm_settings.touched_detect_nom) { + nodeSM = PTC_SM_TOUCH_DETECT; + ptc_event_cb_touch(PTC_CB_EVENT_TOUCH_DETECT, node); + } else if (nodeDelta < node->touch_in_th) { + nodeSM = PTC_SM_NO_TOUCH; + } + + + } else if (nodeSM & PTC_SM_TOUCH_OUT_FLT) { + if (lastChange >= ptc_sm_settings.untouched_detect_nom) { + nodeSM = PTC_SM_NO_TOUCH; + ptc_event_cb_touch(PTC_CB_EVENT_TOUCH_RELEASE, node); + } else if (nodeDelta > node->touch_in_th) { + nodeSM = PTC_SM_TOUCH_DETECT; + } + + + } else if (nodeSM & PTC_SM_TOUCH_DETECT) { + if (lastChange > ptc_sm_settings.touched_max_nom) { + nodeSM = PTC_SM_NOINIT_CAL; + } else if (nodeDelta < node->touch_out_th) { + nodeSM = PTC_SM_TOUCH_OUT_FLT; + } + + + } else if (nodeSM & PTC_SM_LOW_POWER) { + if (0 == node->state.low_power) { + if (nodeDelta > node->touch_in_th) { + nodeSM = PTC_SM_TOUCH_DETECT; + } else { + nodeSM = PTC_SM_NO_TOUCH; + } + lowPowerNode = NULL; + } + if (nodeDelta < -1) { + node->reference--; + PTC.WINHT--; + } else if (nodeDelta > 1) { + node->reference++; + PTC.WINHT++; + } + + + } + + + if (node->stateMachine != nodeSM) { + node->lastStateChange = 0; + node->stateMachine = nodeSM; + } else { + node->lastStateChange = lastChange + 1; + } +} + + +uint8_t ptc_process_calibrate (cap_sensor_t* node) { + uint16_t rawData = node->sensorData; + + #if defined(__PTC_Tiny__) + uint16_t compensation = node->hw_compCaps; + uint8_t cc_accurate = compensation & 0x0F; + uint8_t cc_fine = ((uint8_t) compensation >> 4) & 0x0F; + uint8_t cc_coarse = (uint8_t)(compensation >> 8) & 0x0F; + uint8_t cc_add_rough = (uint8_t)(compensation >> 8) & 0xC0; + uint8_t cc_rough = (uint8_t)(compensation >> 12) & 0x03; + + //uint8_t err = 0; + + int8_t dirOvf; + int8_t dir; + if (rawData > 0x0200) { + rawData = rawData - 0x1FF; + if (rawData < 10) { + return PTC_LIB_CALIB_DONE; + } + + if (node->type & NODE_MUTUAL_bm){ + dir = -1; + dirOvf = -6; + rawData /= 2; + } else { + dir = 1; + dirOvf = 6; + if (rawData > 0x0100) { + if (cc_add_rough <= 0xC0) { + cc_add_rough = (cc_add_rough + 0x40); + rawData -= 0xF0; + } + } + } + } else { + rawData = 0x1FF - rawData; + if (rawData < 10) { + return PTC_LIB_CALIB_DONE; + } + + if (node->type & NODE_MUTUAL_bm){ + dir = 1; + dirOvf = 6; + rawData /= 2; + } else { + dir = -1; + dirOvf = -6; + if (rawData > 0x0100) { + if (cc_add_rough >= 0x40) { + cc_add_rough = (cc_add_rough - 0x40); + rawData -= 0xF0; + } + } + } + } + + while (rawData > 0x0001) { + while (rawData > 0x00CF) { + cc_rough += dir; // this algorithm takes advantage of integer underflow + if (cc_rough > 0x03) { // by checking against >0x03, we can also check for 0xFF aka -1 + cc_rough -= dir; // thus saving some flash. +/-1 can be made in one insn + break; // by using sub reg with 0xFF or 0x01 in a reg respectively + } + rawData -= 0xCF; + } + + while (rawData > 0x0015) { + cc_coarse += dir; + if (cc_coarse > 0x0F) + break; + rawData -= 0x15; + } + if (cc_coarse > 0x0F) { + cc_rough += dir; + if (cc_rough > 0x03) { + cc_rough -= dir; + cc_coarse -= dir; + } else { + cc_coarse -= dirOvf; + } + } + + + while (rawData > 0x0001) { + cc_fine += dir; + if (cc_fine > 0x0F) + break; + rawData -= 0x02; + } + + if (cc_fine > 0x0F) { + cc_coarse += dir; + if (cc_coarse > 0x0F) { + cc_rough += dir; + if (cc_rough > 0x03) { + cc_rough -= dir; + cc_coarse -= dir; + cc_fine -= dir; + if (dir < 0) + return PTC_LIB_CALIB_TOO_LOW; + else + return PTC_LIB_CALIB_TOO_HIGH; + } else { + cc_coarse -= dirOvf; + } + } else { + cc_fine -= dirOvf; + } + } /* if (cc_fine > 0x0F) */ + } /* (rawData > 0x0001) */ + + + cc_fine <<= 4; + cc_rough <<= 4; + cc_rough |= cc_add_rough; + cc_rough |= cc_coarse; + cc_fine |= cc_accurate; + node->hw_compCaps = (uint16_t)((cc_rough << 8) | cc_fine); + return PTC_LIB_SUCCESS; + + #elif defined (__PTC_DA__) + int8_t dir; + if (rawData > 0x0200) { + rawData = rawData - 0x1FF; + if (rawData < 5) { + return PTC_LIB_CALIB_DONE; + } + + if (node->type & NODE_MUTUAL_bm) { + dir = -1; + rawData /= 2; + } else { + dir = 1; + } + } else { + rawData = 0x1FF - rawData; + if (rawData < 5) { + return PTC_LIB_CALIB_DONE; + } + + if (node->type & NODE_MUTUAL_bm) { + dir = 1; + rawData /= 2; + } else { + dir = -1; + } + } + node->hw_compCaps += (int16_t)(dir * rawData); + #endif +} + + +void ptc_init_conversion(uint8_t nodeType) { + PTC_t *pPTC; + _fastPtr_d(pPTC,&PTC); + + if (ptc_lib_state != PTC_LIB_IDLE) + return; + +#if defined (__PTC_Tiny__) + pPTC->CTRLP = 0x00; + if (nodeType == NODE_MUTUAL_bm) { /* NODE_MUTUAL */ + pPTC->CTRLP = 0xC0 | 0x20; + pPTC->SHIELD = 0x00; + } else if (nodeType == NODE_SELFCAP_bm){ /* NODE_SELFCAP */ + pPTC->CTRLP = 0x28; + pPTC->SHIELD = 0x00; + } else if (nodeType == NODE_SELFCAP_SHIELD_bm) { + pPTC->CTRLP = 0x28; + pPTC->SHIELD = 0x86; + } else { + return; + } + + pPTC->CTRLA = 0x00; + + uint8_t freq = freq_select; + if (freq > 0x0F) /* FREQ_SELECT_15 */ + freq = ADC_ASDV_bm; + pPTC->CTRLD = freq; + pPTC->INTFLAGS = ADC_RESRDY_bm | ADC_WCMP_bm; // clear ISR flags, if there were unhandled + + currConvType = nodeType; + if (NULL == lowPowerNode) { + pPTC->INTCTRL = ADC_RESRDY_bm; + pPTC->CTRLE = ADC_WINCM_NONE_gc; + ptc_lib_state = PTC_LIB_CONV_PROG; + ptc_start_conversion(firstNode); + } else { + pPTC->INTCTRL = ADC_WCMP_bm; // Wakeup only above of window + pPTC->CTRLE = ADC_WINCM_ABOVE_gc; + pPTC->WINHT = (lowPowerNode->reference + lowPowerNode->touch_in_th) << (lowPowerNode->hw_a_d_gain & 0x0F); + ptc_lib_state = PTC_LIB_EVENT; + ptc_start_conversion(lowPowerNode); + } + +#elif defined (__PTC_DA__) +// The following code is a placeholder, DA not really started yet + if (nodeType == NODE_MUTUAL_bm) { + pPTC->CTRL_SC = 0x00; + //PTC.CTRL_BOOST = 0x00; + pPTC->CTRLA = 0x00; + } else if (nodeType == NODE_SELFCAP_bm || nodeType == NODE_SELFCAP_SHIELD_bm) { + pPTC->CTRL_SC = 0x01; + //PTC.CTRL_BOOST = 0x00; + pPTC->CTRLA = 0x00; + //} else if (nodeType == 0x82) { // unused + // PTC.CTRL_SC = 0x01; + // PTC.CTRL_BOOST = 0x09; + // PTC.CTRLA = 0x00; + } else { + return; + } + + + uint8_t freq = freq_select; + if (nodeType == NODE_MUTUAL_bm) { + pPTC->SAMPDLY = 0x00; + if (freq < 0x10) /* freq is 0 - 0x0F, 0x10 is auto variation */ + freq |= ADC_INITDLY_DLY16_gc; + else + freq = ADC_INITDLY_DLY16_gc | ADC_SAMPDLY_DLY15_gc; + pPTC->CTRLD = freq; + } else { + pPTC->CTRLD = ADC_INITDLY_DLY16_gc; + if (freq > 0x0F) + freq = 0x0F; + pPTC->SAMPDLY = freq; + } + + pPTC->INTFLAGS = ADC_RESRDY_bm | ADC_WCMP_bm; // clear ISR flags, if there were unhandled + + currConvType = nodeType; + if (NULL != lowPowerNode) { + pPTC->INTCTRL = ADC_WCMP_bm; // Wakeup only above of window + pPTC->CTRLE = ADC_WINCM_ABOVE_gc; + pPTC->WINHT = (lowPowerNode->reference + lowPowerNode->touch_in_th) << (lowPowerNode->hw_a_d_gain & 0x0F); + ptc_lib_state = PTC_LIB_EVENT; + ptc_start_conversion(lowPowerNode); + } else { + pPTC->INTCTRL = ADC_RESRDY_bm; + pPTC->CTRLE = ADC_WINCM_NONE_gc; + ptc_lib_state = PTC_LIB_CONV_PROG; + ptc_start_conversion(firstNode); + } +#endif +} + +void ptc_start_conversion (cap_sensor_t* node) { + while (1) { + if (NULL == node) { + PTC.INTCTRL = 0; // disable ISR for ADC reuse + ptc_lib_state = PTC_LIB_CONV_COMPL; + return; + } + if ((0 == node->state.disabled) && (node->type == currConvType)) { + break; + } else { + node = node->nextNode; + } + } + + currConvNode = node; + + ptc_set_registers(node); +} + +void ptc_set_registers(cap_sensor_t* node) { + PTC_t *pPTC; + _fastPtr_d(node,node); // Sometimes it takes the compiler a bit more of convincing... + _fastPtr_d(pPTC,&PTC); + + if (NULL == node) + return; + + uint8_t analogGain = 0x3F; + if ((node->state.disabled == 0) && (node->stateMachine != PTC_SM_NOINIT_CAL)) { + uint8_t lut_index = node->hw_a_d_gain / 16; // A little workaround as >> 4 is kinda broken sometimes. + analogGain = ptc_a_gain_lut[lut_index]; + } + + uint8_t chargeDelay = node->hw_csd; + if (chargeDelay < 0x1B) { + chargeDelay += 4; + } else { + chargeDelay = 0x1F; + } + + #if defined (__PTC_Tiny__) + pPTC->YBM = node->hw_yCh_bm; + pPTC->XBM = node->hw_xCh_bm; + pPTC->COMP = node->hw_compCaps; + pPTC->AGAIN = analogGain; + pPTC->SAMPCTRL = chargeDelay; + pPTC->CTRLB = node->hw_a_d_gain & 0x0F; + pPTC->RES = node->hw_rsel_presc / 16; + pPTC->CTRLC = (node->hw_rsel_presc & 0x0F) | ADC_REFSEL_VDDREF_gc; + pPTC->CTRLP |= 0x03; + pPTC->CTRLA = ADC_RUNSTBY_bm | ADC_ENABLE_bm; /* 0x81 */ + + if (0 == node->state.low_power) + pPTC->COMMAND = 0x01; // Normal opertion: Manual Start + else + pPTC->EVCTRL = 0x01; // Low Power: Start by positive Flank on Event + #elif defined (__PTC_DA__) + + #endif +} + +void ptc_eoc(void) { + PTC_t *pPTC; + volatile cap_sensor_t *pCurrentNode; // volatile nedded to pass type check + _fastPtr_d(pPTC,&PTC); + _fastPtr_d(pCurrentNode,currConvNode); + + if (NULL == pCurrentNode) + return; + + pPTC->CTRLA = 0x00; + uint8_t flags = pPTC->INTFLAGS; // save the flags before they get cleared by RES read + uint16_t rawVal = pPTC->RES; // clears ISR flags + uint8_t oversampling = pCurrentNode->hw_a_d_gain & 0x0F; + pCurrentNode->sensorData = rawVal >> oversampling; + + //currConvNode->sensorData = pPTC->RES_TRUE; + pCurrentNode->state.data_ready = 1; + + if (pCurrentNode->state.low_power) { + if (flags & ADC_WCMP_bm) { + pCurrentNode->state.win_comp = 1; + ptc_lib_state = PTC_LIB_CONV_WCMP; + } else { + pCurrentNode->state.win_comp = 0; + ptc_lib_state = PTC_LIB_CONV_LP; + } + + } else { + ptc_start_conversion(pCurrentNode->nextNode); + } +} + + + +// returns the last node in the list, or NULL if list empty. +cap_sensor_t* ptc_get_last_node (void) { + if (firstNode == NULL) + return NULL; + + cap_sensor_t *node = firstNode; + while (1) { + cap_sensor_t *nextNode = node->nextNode; + if (nextNode == NULL) + return node; + else + node = nextNode; + } +} + + +// puts the node to the back of the single-linked list. +// returns NULL if list was empty, otherwise the previous last node +uint8_t ptc_append_node(cap_sensor_t* pNewNode) { + cap_sensor_t* lastNode = ptc_get_last_node(); + if (lastNode == NULL) { + firstNode = pNewNode; + pNewNode->id = 0; + } else if (lastNode != pNewNode) { // Make sure we don't create an endless loop on accident + lastNode->nextNode = pNewNode; + pNewNode->id = lastNode->id + 1; + } else { + return PTC_LIB_ERROR; + } + return PTC_LIB_SUCCESS; +} + + +#if defined(__PTC_Tiny__) +ISR(ADC0_RESRDY_vect) { + ptc_eoc(); +} + +ISR(ADC0_WCOMP_vect) { + ptc_eoc(); +} +#elif defined(__PTC_DA__) +ISR(PTC_PTC_vect) { + ptc_eoc(); +} +#endif diff --git a/megaavr/libraries/PTC/src/ptc.h b/megaavr/libraries/PTC/src/ptc.h new file mode 100644 index 00000000..0e64a9f5 --- /dev/null +++ b/megaavr/libraries/PTC/src/ptc.h @@ -0,0 +1,235 @@ +/* + ptc library to use the PTC module in AVR devices + Copyright (c) 2023, MX682X + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + This license applies to all files that are part of this library + (ptc.c, ptc.h, ptc.h, ptc.h) +*/ + + + +#pragma once +#ifndef PTC_TOUCH_H +#define PTC_TOUCH_H + +#if defined(MEGATINYCORE) || defined(DXCORE) + #include +#else + #include + #ifndef _fastPtr_d + #define _fastPtr_d(_x_, _y_) _x_ = _y_; + #endif +#endif + +#include "ptc_io.h" +#include "ptc_types.h" + + + +#ifndef NULL + #define NULL (void*)0 +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + + +#define NODE_GAIN(_a_, _d_) (uint8_t)(((_a_) << 4) | ((_d_) & 0x0F)) +#define NODE_RSEL_PRSC(_r_, _p_) (uint8_t)(((_r_) << 4) | ((_p_) & 0x0F)) +#define NUM_TO_BM(__n__) (0x01 << __n__) + + + +// Paranoid Pointer validation +// all attinies have a RAM address where the higher byte starts with 0x38. +// Devices with smaller memories start at 0x3E or 0x3F, but both have the same bits set as 0x38. +// if it's Flash, it will be 0x80, as signed it means negative. +// if it's anything else (I/O, EEPROM, etc.), p will be <= 14 +// thus making a signed check will make pretty sure the pointer points to a RAM address +// Disadvantage: GCC can not validate this at compiletime as it does not know the variable's +// address at compile-time. +#define PTC_CHECK_POINTER(__p__, __ret__) \ + if (((int8_t)((uint16_t)__p__ >> 8)) < 0x38) { \ + return __ret__; \ + } + +#define PTC_CHECK_FOR_BAD_POINTER(__p__) \ + if (NULL == __p__) { \ + if (__builtin_constant_p(__p__)) { \ + badArg("Null pointer detected"); \ + } \ + return PTC_LIB_BAD_POINTER; \ + } + + + + +//extern void ptc_conversion_complete(uint8_t type); +//extern void ptc_error_callback(uint8_t source, cap_sensor_t* node); +extern void ptc_event_callback(const ptc_cb_event_t eventType, cap_sensor_t* node); + +extern void ptc_event_cb_touch(const ptc_cb_event_t eventType, cap_sensor_t* node); +extern void ptc_event_cb_wake(const ptc_cb_event_t eventType, cap_sensor_t* node); +extern void ptc_event_cb_conversion(const ptc_cb_event_t eventType, cap_sensor_t* node); +extern void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node); +extern void ptc_event_cb_error(const ptc_cb_event_t eventType, cap_sensor_t* node); + + + + + +// Enables the node. Can be called while an acquisition is in progress. +uint8_t ptc_enable_node(cap_sensor_t* node); + +// Disables a node. If the conversion is started, it will be finished +uint8_t ptc_disable_node(cap_sensor_t* node); + +// Can be used outside an acqusition process to select ADC/SELFCAP/MUTUAL/SHIELD +void ptc_set_next_conversion_type(ptc_node_type_t type); + +// Main task handle for PTC. Handles State-Machine, drift, and calibration +void ptc_process(uint16_t currTime); + + +// Set the threshold for touch detection and away from touch for a node +uint8_t ptc_node_set_thresholds (cap_sensor_t* node, int16_t th_in, int16_t th_out); + + +// Change Resistor Setting. Note: Only has an effect on mutual sensors +uint8_t ptc_node_set_resistor(cap_sensor_t* node, uint8_t res); + +// Change preschaler. +uint8_t ptc_node_set_prescaler(cap_sensor_t* node, uint8_t presc); + +uint8_t ptc_node_set_gain(cap_sensor_t* node, uint8_t aGain, uint8_t dGain); + +// this two functions assume that yCh and xCh have validated values. Don't call these directly +uint8_t ptc_add_selfcap_node_asserted(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc_ch_bm_t xCh); +uint8_t ptc_add_mutualcap_node_asserted(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc_ch_bm_t xCh); + +inline uint8_t ptc_add_selfcap_node(cap_sensor_t* node, const ptc_ch_bm_t yCh, const ptc_ch_bm_t xCh) { + if(__builtin_constant_p(yCh)) { + if (yCh == 0) badArg("yCh bitmap mustn't be 0 (Pin_Pxn is not a PTC pin)"); + if (yCh & xCh) badArg("pin bitmap overlap detected"); + } else if ((yCh == 0) || ((yCh & xCh) != 0)) { + return PTC_LIB_BAD_ARGUMENT; // We need at least one pin to connect to + } + return ptc_add_selfcap_node_asserted(node, yCh, xCh); +}; + +inline uint8_t ptc_add_mutualcap_node(cap_sensor_t* node, const ptc_ch_bm_t yCh, const ptc_ch_bm_t xCh) { + if(__builtin_constant_p(yCh) && __builtin_constant_p(xCh)) { + if (yCh == 0) badArg("yCh bitmap mustn't be 0 (Pin_Pxn is not a PTC pin)"); + if (xCh == 0) badArg("xCh bitmap mustn't be 0 (Pin_Pxn is not a PTC pin)"); + if (yCh & xCh) badArg("pin overlap detected"); + } else if ((yCh == 0 || xCh == 0) || ((yCh & xCh) != 0)) { + return PTC_LIB_BAD_ARGUMENT; // mutual requires at least one pin on y-Channel and x-Channel + } + return ptc_add_mutualcap_node_asserted(node, yCh, xCh); +}; + + + +uint8_t ptc_suspend(void); +void ptc_resume(void); + +// If you want to know the compensation capacitance in fempto Farrad +uint16_t ptc_get_node_cc_fempto(cap_sensor_t* node); + +ptc_lib_sm_set_t* ptc_get_sm_settings(); + +// X and Y channel bitmasks +inline ptc_ch_bm_t ptc_get_node_xCh_bm(cap_sensor_t* node) { + if (node == NULL) return 0x00; + return node->hw_xCh_bm; +} + +inline ptc_ch_bm_t ptc_get_node_yCh_bm(cap_sensor_t* node) { + if (node == NULL) return 0x00; + return node->hw_yCh_bm; +} + +// the measured PTC value. 512 is 0. 0x00 means BAD_POINTER +inline uint16_t ptc_get_node_sensor_value(cap_sensor_t* node) { + if (node == NULL) return 0x00; + return node->sensorData; +} + +// returns true, if node is a valid pointer and node is touched, otherwise false. +// No other return value so there can be easy checks - not null or null +inline uint8_t ptc_get_node_touched(cap_sensor_t* node) { + if (node == NULL) return 0x00; + + if (node->stateMachine & (PTC_SM_TOUCH_DETECT | PTC_SM_TOUCH_OUT_FLT)) + return 0x01; + return 0x00; +} + + +inline uint8_t ptc_get_node_sm(cap_sensor_t* node) { + PTC_CHECK_FOR_BAD_POINTER(node); + return node->stateMachine; +} + + +inline int16_t ptc_get_node_delta(cap_sensor_t* node) { + if (node == NULL) + return 0x8000; //-32k - impossible value for normal operation + + return (node->sensorData - node->reference); +} + +inline uint8_t ptc_get_node_state(cap_sensor_t* node) { + if (node == NULL) + return 0xFF; + + return (node->stateMachine); +} + +inline ptc_id_t ptc_get_node_id(cap_sensor_t* node) { + if (node == NULL) + return 0xFF; + + return (node->id); +} + + +inline uint8_t ptc_node_request_recal(cap_sensor_t* node) { + PTC_CHECK_FOR_BAD_POINTER(node); + + node->stateMachine = PTC_SM_NOINIT_CAL; + node->lastStateChange = 0; + return PTC_LIB_SUCCESS; +} + + +void ptc_init_ADC0(void); +uint8_t ptc_lp_init(cap_sensor_t* node); +uint8_t ptc_lp_disable(void); +uint8_t ptc_lp_was_waken(void); + +// Called by the interrupt routine. Saves result and selects next node +extern void ptc_eoc(void); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/megaavr/libraries/PTC/src/ptc_io.h b/megaavr/libraries/PTC/src/ptc_io.h new file mode 100644 index 00000000..a658009a --- /dev/null +++ b/megaavr/libraries/PTC/src/ptc_io.h @@ -0,0 +1,449 @@ +/* + * Refer to ptc.h file for copyright, changelog, usage and license information + */ + +#pragma once +#ifndef PTC_TOUCH_IO_H +#define PTC_TOUCH_IO_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ptc_types.h" + +#if defined (__PTC_Tiny__) + +typedef struct PTC_struct { + register8_t CTRLA; /* Control A */ + register8_t CTRLB; /* Control B */ + register8_t CTRLC; /* Control C */ + register8_t CTRLD; /* Control D */ + register8_t CTRLE; /* Control E */ + register8_t SAMPCTRL; /* Sample Control */ + register8_t MUXPOS; /* Positive mux input */ + register8_t reserved_0; + register8_t COMMAND; /* Command */ + register8_t EVCTRL; /* Event Control */ + register8_t INTCTRL; /* Interrupt Control */ + register8_t INTFLAGS; /* Interrupt Flags */ + register8_t DBGCTRL; /* Debug Control */ + register8_t TEMP; /* Temporary Data */ + _WORDREGISTER(reserved_1); + _WORDREGISTER(RES); /* ADC Accumulator Result */ + _WORDREGISTER(WINLT); /* Window comparator low threshold */ + _WORDREGISTER(WINHT); /* Window comparator high threshold */ + register8_t CALIB; /* Calibration */ + register8_t reserved_2; /* +0x17 */ + /* Any register Name below this point is based on speculation */ + register8_t CTRLP; /* +0x18 OR with 0x03 to enable, written 0x28 for Selfcap */ + register8_t RSEL; /* +0x19 */ + _WORDREGISTER(COMP); /* +0x1A */ + register8_t AGAIN; /* +0x1C */ + register8_t reserved_3; /* +0x1D */ + register8_t SHIELD; /* +0x1E if enabled, written 0x86, otherwise 0x00 */ + register8_t reserved_4; /* +0x1F */ + _WORDREGISTER(RES_PTC); /* +0x20 Some Result, written by PTC. Seems to be RES, but left-shifted, or result from PTC accumulated by ADC */ + _WORDREGISTER(PIN_OVR); /* +0x22 all X and Y pins OR'd together at init. Pin Function Overwrite Probably*/ + _WORDREGISTER(reserved_5); /* +0x24 */ + _WORDREGISTER(XBM); /* +0x26 amount of writeable bits depends on chip-die family */ + _WORDREGISTER(reserved_6); /* +0x28 */ + _WORDREGISTER(YBM); /* +0x2A e.g. 0x3FFF (15 pins) for 1614 with only 6 PTC pins */ + _WORDREGISTER(reserved_7);; /* +0x2C */ +} PTC_t; + +#define PTC (*(PTC_t *) 0x0600) /* Analog to Digital Converter */ + +#elif defined (__PTC_DA__) + +typedef struct PTC_struct { + /* Any register Name below this point is based on speculation */ + register8_t CTRLA; /* 0xC0 [0x81] Control A (RUNSTDBY, ENABLE)*/ + register8_t CTRL_SC; /* 0xC1 [0x07] 0x01: Selfcap_EN, */ + register8_t CTRLC; /* 0xC2 [0x87] Control C (Prescaler | 0x80 (SampCap?))*/ + register8_t CTRLD; /* 0xC3 [0x6F] Control D (Max: 0x6F) Is always OR'd with 0x20 or limited to 0x2F */ + register8_t SAMPCTRL; /* 0xC4 [0x7F] SAMPCTRL (Max: 0x7F) */ + register8_t SAMPDLY; /* 0xC5 [0x7F] 0~15 Inserted ADC clocks */ + register8_t reserved_0; /* 0xC6 [0xFF] unused */ + register8_t CTRLB; /* 0xC7 [0x07] SAMPNUM */ + register8_t CTRLE; /* 0xC8 [0x07] WINCM probably */ + register8_t RSEL; /* 0xC9 [0x77] Resistor Setting */ + register8_t CC_EN; /* 0xCA [0x01] Enable Compensation Caps? */ + register8_t CTRL_BOOST; /* 0xCB [0xFF] set to 0x09 if NODE_SELFCAP_SHIELD_2L */ + _DWORDREGISTER(reserved_1); /* 0xCC [0x00] unwritable */ + register8_t COMMAND; /* 0xD0 [0x01] Command */ + register8_t EVCTRL; /* 0xD1 [0x01] Event Control */ + register8_t INTCTRL; /* 0xD2 [0x07] Interrupt Control */ + register8_t INTFLAGS; /* 0xD3 [0x07] Interrupt Flags */ + register8_t reserved_2; /* 0xD4 [0x02] unknown */ + register8_t DEBUGCTRL; /* 0xD5 [0x01] DBGRUN */ + _WORDREGISTER(reserved_3); /* 0xD6 [0x00] unwritable */ + _WORDREGISTER(reserved_4); /* 0xD8 [0x00] some other result */ + _WORDREGISTER(RES); /* 0xDA [0x00] ADC Result register */ + _WORDREGISTER(WINLT); /* 0xDC [0xFF] Window comparator low threshold */ + _WORDREGISTER(WINHT); /* 0xDD [0xFF] Window comparator high threshold */ + register8_t CTRL_SHIELD; /* 0xE0 [0x00] Strobing Register, changes between Shield and Mutual ?*/ + register8_t SOME_RES; /* 0xE1 [0x81] some unknown result value */ + _WORDREGISTER(reserved_5); /* 0xE2 [0x00] unwritable */ + _WORDREGISTER(COMP); /* 0xE4 [0x3FF] Compensation */ + register8_t AGAIN; /* 0xE6 [0x1F] Analog Gain */ + register8_t reserved_6; /* 0xE7 [0x00] unwritable */ + register8_t XBM[8]; /* 0xE8 X Channel bitmask */ + register8_t YBM[8]; /* 0xF0 Y Channel bitmask */ + register8_t XBM_4P[8]; /* 0xF8 X Channel bm when using Boost mode, unused */ +} PTC_t; + +#define PTC (*(PTC_t *) 0x07C0) /* Analog to Digital Converter */ + +#else + #warning "Neither __PTC_Tiny__ nor __PTC_DA__ defined" +#endif + + + + +#define RSEL_MAX RSEL_VAL_200 +#define PRSC_MAX ADC_PRESC_DIV256_gc + + +#if F_CPU >= 12000000 // 16 MHz / 16 = 1.0 MHz, 20 MHz / 16 = 1.25 MHz + #define PTC_PRESC_DEFAULT ADC_PRESC_DIV16_gc +#elif F_CPU >= 6000000 // 8 MHz / 8 = 1.0 MHz, 10 MHz / 8 = 1.25 MHz + #define PTC_PRESC_DEFAULT ADC_PRESC_DIV8_gc +#elif F_CPU >= 3000000 // 4 MHz / 4 = 1.0 MHz, 5 MHz / 4 = 1.25 MHz + #define PTC_PRESC_DEFAULT ADC_PRESC_DIV4_gc +#else // 1 MHz / 2 = 500 kHz - the lowest setting + #define PTC_PRESC_DEFAULT ADC_PRESC_DIV2_gc +#endif + + + +#define PIN_TO_PTC(__pin__) (((__pin__) < NUM_TOTAL_PINS ) ? digital_pin_to_ptc_bm[__pin__] : 0x00) +const ptc_ch_bm_t digital_pin_to_ptc_bm [] = { +#if (__PTC_Pincount__ == 6) + 0x01 << 0, //PA4 + 0x01 << 1, //PA5 + 0x01 << 2, //PA6 + 0x01 << 3, //PA7 + 0x00, //PB3 + 0x00, //PB2 + 0x01 << 4, //PB1 + 0x01 << 5, //PB0 + 0x00, //PA1 + 0x00, //PA2 + 0x00, //PA3 + 0x00 //PA0 +#elif (__PTC_Pincount__ == 12) + 0x01 << 0, // 0 PA4 + 0x01 << 1, // 1 PA5 + 0x01 << 2, // 2 PA6 + 0x01 << 3, // 3 PA7 + 0x01 << 12, // 4 PB5 + 0x01 << 13, // 5 PB4 + 0x00, // 6 PB3 + 0x00, // 7 PB2 + 0x01 << 4, // 8 PB1 + // Right side, bottom to top + 0x01 << 5, // 9 PB0 + 0x01 << 6, // 10 PC0 + 0x01 << 7, // 11 PC1 + 0x01 << 8, // 12 PC2 + 0x01 << 9, // 13 PC3 + 0x00, // 14 PA1 + 0x00, // 15 PA2 + 0x00, // 16 PA3 + 0x00 // 17 PA0 +#elif (__PTC_Pincount__ == 14) + 0x01 << 0, // 0 PA4 + 0x01 << 1, // 1 PA5 + 0x01 << 2, // 2 PA6 + 0x01 << 3, // 3 PA7 + 0x00, // 4 PB7 + 0x00, // 5 PB6 + 0x01 << 12, // 6 PB5 + 0x01 << 13, // 7 PB4 + 0x00, // 8 PB3 + 0x00, // 9 PB2 + 0x01 << 4, // 10 PB1 + // Right side, bottom to top + 0x01 << 5, // 11 PB0 + 0x01 << 6, // 12 PC0 + 0x01 << 7, // 13 PC1 + 0x01 << 8, // 14 PC2 + 0x01 << 9, // 15 PC3 + 0x01 << 10, // 16 PC4 + 0x01 << 11, // 17 PC5 + 0x00, // 18 PA1 + 0x00, // 19 PA2 + 0x00, // 20 PA3 + 0x00 // 21 PA0 +#elif (__PTC_Pincount__ == 17) // DA (ToDo) + 0x01ULL << 0, // 0 PA0 + 0x01ULL << 1, // 1 PA1 + 0x01ULL << 2, // 2 PA2/SDA + 0x01ULL << 3, // 3 PA3/SCL + 0x01ULL << 4, // 4 PA4/MOSI + 0x01ULL << 5, // 5 PA5/MISO + 0x01ULL << 6, // 6 PA6/SCK + 0x01ULL << 7, // 7 PA7/SS/CLKOUT + 0x00ULL, // 8 PC0/USART1_Tx + 0x00ULL, // 9 PC1/USART1_Rx + 0x00ULL, // 10 PC2 + 0x00ULL, // 11 PC3 + 0x01ULL << 16, // 12 PD0/AIN0 + 0x01ULL << 17, // 13 PD1/AIN1 + 0x01ULL << 18, // 14 PD2/AIN2 + 0x01ULL << 19, // 15 PD3/AIN3 + 0x01ULL << 20, // 16 PD4/AIN4 + 0x01ULL << 21, // 17 PD5/AIN5 + 0x01ULL << 22, // 18 PD6/AIN6 + 0x01ULL << 23, // 19 PD7/AIN7/AREF + 0x01ULL << 32, // 20 PF0/USART2_Tx/TOSC1 + 0x01ULL << 33, // 21 PF1/USART2_Rx/TOSC2 +#elif (__PTC_Pincount__ == 21) + 0x01ULL << 0, // 0 PA0 + 0x01ULL << 1, // 1 PA1 + 0x01ULL << 2, // 2 PA2/SDA + 0x01ULL << 3, // 3 PA3/SCL + 0x01ULL << 4, // 4 PA4/MOSI + 0x01ULL << 5, // 5 PA5/MISO + 0x01ULL << 6, // 6 PA6/SCK + 0x01ULL << 7, // 7 PA7/SS/CLKOUT + 0x00ULL, // 8 PC0/USART1_Tx + 0x00ULL, // 9 PC1/USART1_Rx + 0x00ULL, // 10 PC2 + 0x00ULL, // 11 PC3 + 0x01ULL << 16, // 12 PD0/AIN0 + 0x01ULL << 17, // 13 PD1/AIN1 + 0x01ULL << 18, // 14 PD2/AIN2 + 0x01ULL << 19, // 15 PD3/AIN3 + 0x01ULL << 20, // 16 PD4/AIN4 + 0x01ULL << 21, // 17 PD5/AIN5 + 0x01ULL << 22, // 18 PD6/AIN6 + 0x01ULL << 23, // 19 PD7/AIN7/AREF + 0x01ULL << 32, // 20 PF0/USART2_Tx/TOSC1 + 0x01ULL << 33, // 21 PF1/USART2_Rx/TOSC2 + 0x01ULL << 34, // 22 PF2/AIN12 + 0x01ULL << 35, // 23 PF3/AIN13 + 0x01ULL << 36, // 24 PF4/AIN14/TCB0 PWM + 0x01ULL << 37, // 25 PF5/AIN15/TCB1 PWM +#elif (__PTC_Pincount__ == 31) + 0x01ULL << 0, // 0 PA0 + 0x01ULL << 1, // 1 PA1 + 0x01ULL << 2, // 2 PA2/SDA + 0x01ULL << 3, // 3 PA3/SCL + 0x01ULL << 4, // 4 PA4/MOSI + 0x01ULL << 5, // 5 PA5/MISO + 0x01ULL << 6, // 6 PA6/SCK + 0x01ULL << 7, // 7 PA7/SS/CLKOUT/LED_BUILTIN + 0x01ULL << 8, // 8 PB0/USART3_Tx + 0x01ULL << 9, // 9 PB1/USART3_Rx + 0x01ULL << 10, // 10 PB2 + 0x01ULL << 11, // 11 PB3 + 0x01ULL << 12, // 12 PB4/(TCB2 PWM) + 0x01ULL << 13, // 13 PB5 + 0x00ULL, // 14 PC0/USART1_Tx + 0x00ULL, // 15 PC1/USART1_Rx + 0x00ULL, // 16 PC2 + 0x00ULL, // 17 PC3 + 0x00ULL, // 18 PC4 + 0x00ULL, // 19 PC5 + 0x00ULL, // 20 PC6 + 0x00ULL, // 21 PC7 + 0x01ULL << 16, // 22 PD0/AIN0 + 0x01ULL << 17, // 23 PD1/AIN1 + 0x01ULL << 18, // 24 PD2/AIN2 + 0x01ULL << 19, // 25 PD3/AIN3 + 0x01ULL << 20, // 26 PD4/AIN4 + 0x01ULL << 21, // 27 PD5/AIN5 + 0x01ULL << 22, // 28 PD6/AIN6 + 0x01ULL << 23, // 29 PD7/AIN7/AREF + 0x01ULL << 24, // 30 PE0/AIN8 + 0x01ULL << 25, // 31 PE1/AIN9 + 0x01ULL << 26, // 32 PE2/AIN10 + 0x01ULL << 27, // 33 PE3/AIN11 + 0x01ULL << 32, // 34 PF0/USART2_Tx/TOSC1 + 0x01ULL << 33, // 35 PF1/USART2_Rx/TOSC2 + 0x01ULL << 34, // 36 PF2/AIN12 + 0x01ULL << 35, // 37 PF3/AIN13 + 0x01ULL << 36, // 38 PF4/AIN14 + 0x01ULL << 37, // 39 PF5/AIN15 +#elif (__PTC_Pincount__ == 47) + 0x01ULL << 0, // 0 PA0 + 0x01ULL << 1, // 1 PA1 + 0x01ULL << 2, // 2 PA2/SDA + 0x01ULL << 3, // 3 PA3/SCL + 0x01ULL << 4, // 4 PA4/MOSI + 0x01ULL << 5, // 5 PA5/MISO + 0x01ULL << 6, // 6 PA6/SCK + 0x01ULL << 7, // 7 PA7/SS/CLKOUT/LED_BUILTIN + 0x01ULL << 8, // 8 PB0/USART3_Tx + 0x01ULL << 9, // 9 PB1/USART3_Rx + 0x01ULL << 10, // 10 PB2 + 0x01ULL << 11, // 11 PB3 + 0x01ULL << 12, // 12 PB4/(TCB2 PWM) + 0x01ULL << 13, // 13 PB5 + 0x01ULL << 14, // 14 PB6 + 0x01ULL << 15, // 15 PB7 + 0x00ULL, // 16 PC0/USART1_Tx + 0x00ULL, // 17 PC1/USART1_Rx + 0x00ULL, // 18 PC2 + 0x00ULL, // 19 PC3 + 0x00ULL, // 20 PC4 + 0x00ULL, // 21 PC5 + 0x00ULL, // 22 PC6 + 0x00ULL, // 23 PC7 + 0x01ULL << 16, // 24 PD0/AIN0 + 0x01ULL << 17, // 25 PD1/AIN1 + 0x01ULL << 18, // 26 PD2/AIN2 + 0x01ULL << 19, // 27 PD3/AIN3 + 0x01ULL << 20, // 28 PD4/AIN4 + 0x01ULL << 21, // 29 PD5/AIN5 + 0x01ULL << 22, // 30 PD6/AIN6 + 0x01ULL << 23, // 31 PD7/AIN7/AREF + 0x01ULL << 24, // 32 PE0/AIN8 + 0x01ULL << 25, // 33 PE1/AIN9 + 0x01ULL << 26, // 34 PE2/AIN10 + 0x01ULL << 27, // 35 PE3/AIN11 + 0x01ULL << 28, // 36 PE4 + 0x01ULL << 29, // 37 PE5 + 0x01ULL << 30, // 38 PE6 + 0x01ULL << 31, // 39 PE7 + 0x01ULL << 32, // 40 PF0/USART2_Tx/TOSC1 + 0x01ULL << 33, // 41 PF1/USART2_Rx/TOSC2 + 0x01ULL << 34, // 42 PF2/AIN12 + 0x01ULL << 35, // 43 PF3/AIN13 + 0x01ULL << 36, // 44 PF4/AIN14 + 0x01ULL << 37, // 45 PF5/AIN15 + 0x01ULL << 40, // 46 PG0 + 0x01ULL << 41, // 47 PG1 + 0x01ULL << 42, // 48 PG2 + 0x01ULL << 43, // 49 PG3 + 0x01ULL << 44, // 50 PG4 + 0x01ULL << 45, // 51 PG5 + 0x01ULL << 46, // 52 PG6 + 0x01ULL << 47, // 53 PG7 +#endif +}; + + +#if defined (PORTA) + #define PORTA_ISC(_pin_) ((0x20 * 0) + 0x10 + _pin_) +#else + #define PORTA_ISC(_pin_) 0x00 +#endif +#if defined (PORTB) + #define PORTB_ISC(_pin_) ((0x20 * 1) + 0x10 + _pin_) +#else + #define PORTB_ISC(_pin_) 0x00 +#endif +#if defined (PORTC) + #define PORTC_ISC(_pin_) ((0x20 * 2) + 0x10 + _pin_) +#else + #define PORTC_ISC(_pin_) 0x00 +#endif +#if defined (PORTD) + #define PORTD_ISC(_pin_) ((0x20 * 3) + 0x10 + _pin_) +#else + #define PORTD_ISC(_pin_) 0x00 +#endif +#if defined (PORTE) + #define PORTE_ISC(_pin_) ((0x20 * 4) + 0x10 + _pin_) +#else + #define PORTE_ISC(_pin_) 0x00 +#endif +#if defined (PORTF) + #define PORTF_ISC(_pin_) ((0x20 * 5) + 0x10 + _pin_) +#else + #define PORTF_ISC(_pin_) 0x00 +#endif +#if defined (PORTG) + #define PORTG_ISC(_pin_) ((0x20 * 6) + 0x10 + _pin_) +#else + #define PORTG_ISC(_pin_) 0x00 +#endif + + +// lookup-table to quickly disable input and pull-up +const uint8_t ptc_ch_to_pin [] = { +#if (__PTC_Pincount__ <= 14) + PORTA_ISC(4), + PORTA_ISC(5), + PORTA_ISC(6), + PORTA_ISC(7), + PORTB_ISC(1), /* X4 / Y4 */ + PORTA_ISC(0), +#if (__PTC_Pincount__ == 12 || __PTC_Pincount__ == 14) + PORTC_ISC(0), + PORTC_ISC(1), + PORTC_ISC(2), /* X8 / Y8 */ + PORTC_ISC(3), + PORTC_ISC(4), // 20 pin parts: writing to this location will have no effect, but likely pre-filtered by PIN_TO_PTC anyway + PORTC_ISC(5), // 20 pin parts: writing to this location will have no effect, but likely pre-filtered by PIN_TO_PTC anyway + PORTB_ISC(5), /* X12 / Y12 */ + PORTB_ISC(6), +#endif +#else + PORTA_ISC(0), + PORTA_ISC(1), + PORTA_ISC(2), + PORTA_ISC(3), + PORTA_ISC(4), + PORTA_ISC(5), + PORTA_ISC(6), + PORTA_ISC(7), // X7 + + PORTB_ISC(0), + PORTB_ISC(1), + PORTB_ISC(2), + PORTB_ISC(3), + PORTB_ISC(4), + PORTB_ISC(5), + PORTB_ISC(6), + PORTB_ISC(7), // X15 + + PORTD_ISC(0), + PORTD_ISC(1), + PORTD_ISC(2), + PORTD_ISC(3), + PORTD_ISC(4), + PORTD_ISC(5), + PORTD_ISC(6), + PORTD_ISC(7), // X23 + + PORTE_ISC(0), + PORTE_ISC(1), + PORTE_ISC(2), + PORTE_ISC(3), + PORTE_ISC(4), + PORTE_ISC(5), + PORTE_ISC(6), + PORTE_ISC(7), // X31 + + PORTF_ISC(0), + PORTF_ISC(1), + PORTF_ISC(2), + PORTF_ISC(3), + PORTF_ISC(4), + PORTF_ISC(5), // X37 + 0x00, + 0x00, + + PORTG_ISC(0), + PORTG_ISC(1), + PORTG_ISC(2), + PORTG_ISC(3), + PORTG_ISC(4), + PORTG_ISC(5), + PORTG_ISC(6), + PORTG_ISC(7), // X47 +#endif +}; + + +#ifdef __cplusplus +} +#endif + + +#endif /* PTC_TOUCH.H_H_ */ diff --git a/megaavr/libraries/PTC/src/ptc_types.h b/megaavr/libraries/PTC/src/ptc_types.h new file mode 100644 index 00000000..43f2da6b --- /dev/null +++ b/megaavr/libraries/PTC/src/ptc_types.h @@ -0,0 +1,237 @@ +/* + * Refer to ptc_touch.h file for copyright, changelog, usage and license information + */ + + +#pragma once +#ifndef PTC_TOUCH_TYPES_H +#define PTC_TOUCH_TYPES_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#if (defined(__AVR_ATtiny814__) || defined(__AVR_ATtiny1614__) || defined(__AVR_ATtiny3214__)) + typedef uint8_t ptc_id_t; + typedef uint8_t ptc_ch_bm_t; + #define __PTC_Tiny__ + #define __PTC_Pincount__ 6 +#elif (defined(__AVR_ATtiny816__) || defined(__AVR_ATtiny1616__) || defined(__AVR_ATtiny3216__)) + typedef uint8_t ptc_id_t; + typedef uint16_t ptc_ch_bm_t; + #define __PTC_Tiny__ + #define __PTC_Pincount__ 12 +#elif (defined(__AVR_ATtiny817__) || defined(__AVR_ATtiny1617__) || defined(__AVR_ATtiny3217__)) + typedef uint8_t ptc_id_t; + typedef uint16_t ptc_ch_bm_t; + #define __PTC_Tiny__ + #define __PTC_Pincount__ 14 +#elif (defined(__AVR_AVR32DA28__) || defined(__AVR_AVR64DA28__) || defined(__AVR_AVR128DA28__)) + typedef uint16_t ptc_id_t; + typedef uint64_t ptc_ch_bm_t; + #define __PTC_DA__ + #define __PTC_Pincount__ 17 +#elif (defined(__AVR_AVR32DA32__) || defined(__AVR_AVR64DA32__) || defined(__AVR_AVR128DA32__)) + typedef uint16_t ptc_id_t; + typedef uint64_t ptc_ch_bm_t; + #define __PTC_DA__ + #define __PTC_Pincount__ 21 +#elif (defined(__AVR_AVR32DA48__) || defined(__AVR_AVR64DA48__) || defined(__AVR_AVR128DA48__)) + typedef uint16_t ptc_id_t; + typedef uint64_t ptc_ch_bm_t; + #define __PTC_DA__ + #define __PTC_Pincount__ 31 +#elif (defined(__AVR_AVR32DA64__) || defined(__AVR_AVR64DA64__) || defined(__AVR_AVR128DA64__)) + typedef uint16_t ptc_id_t; + typedef uint64_t ptc_ch_bm_t; + #define __PTC_DA__ + #define __PTC_Pincount__ 47 +#else + #error "PTC not supported by this chip" +#endif + +/** + * PTC series resistor setting. For Mutual cap mode, this series + * resistor is switched internally on the Y-pin. For Self cap mode, + * thes 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_70 sets internal series resistor to 70Kohms. + * RSEL_VAL_100 sets internal series resistor to 100Kohms. + * RSEL_VAL_200 sets internal series resistor to 200Kohms. + */ +typedef enum PTC_RSEL_enum { + RSEL_VAL_0, + RSEL_VAL_20, + RSEL_VAL_50, + RSEL_VAL_70, + RSEL_VAL_100, + RSEL_VAL_200 +} PTC_RSEL_t; + +typedef enum ptc_freq_enum { + FREQ_SEL_0, + FREQ_SEL_1, + FREQ_SEL_2, + FREQ_SEL_3, + FREQ_SEL_4, + FREQ_SEL_5, + FREQ_SEL_6, + FREQ_SEL_7, + FREQ_SEL_8, + FREQ_SEL_9, + FREQ_SEL_10, + FREQ_SEL_11, + FREQ_SEL_12, + FREQ_SEL_13, + FREQ_SEL_14, + FREQ_SEL_15, + FREQ_SEL_SPREAD +} ptc_freq_t; + + + +typedef enum PTC_PRESC_enum +{ + PTC_PRESC_DIV2_gc = (0x00<<0), /* CLK_PER divided by 2 */ + PTC_PRESC_DIV4_gc = (0x01<<0), /* CLK_PER divided by 4 */ + PTC_PRESC_DIV8_gc = (0x02<<0), /* CLK_PER divided by 8 */ + PTC_PRESC_DIV16_gc = (0x03<<0), /* CLK_PER divided by 16 */ + PTC_PRESC_DIV32_gc = (0x04<<0), /* CLK_PER divided by 32 */ + PTC_PRESC_DIV64_gc = (0x05<<0), /* CLK_PER divided by 64 */ + PTC_PRESC_DIV128_gc = (0x06<<0), /* CLK_PER divided by 128 */ + PTC_PRESC_DIV256_gc = (0x07<<0) /* CLK_PER divided by 256 */ +} PTC_PRESC_t; + + + +typedef struct ptc_node_state_type { + uint8_t error:1; + uint8_t win_comp:1; + uint8_t low_power:1; + uint8_t data_ready:1; + uint8_t disabled:1; +} ptc_node_state_t; + + +typedef enum ptc_sm_enum { + PTC_SM_NOINIT_CAL = 0x00, + PTC_SM_RECAL_FLT = 0x01, + PTC_SM_NT_LOW_FLT = 0x02, + PTC_SM_NO_TOUCH = 0x04, + PTC_SM_NT_HIGH_FLT = 0x08, + PTC_SM_TOUCH_IN_FLT = 0x10, + PTC_SM_TOUCH_OUT_FLT = 0x20, + PTC_SM_TOUCH_DETECT = 0x40, + PTC_SM_LOW_POWER = 0x80 +} ptc_sm_t; + +typedef enum ptc_ret_enum { + // Normal return types + PTC_LIB_SUCCESS = 0x00, + PTC_LIB_WAS_WAKEN, + PTC_LIB_ASLEEP, + PTC_LIB_CALIB_DONE, + PTC_LIB_CALIB_TOO_LOW, + PTC_LIB_CALIB_TOO_HIGH, + PTC_LIB_CALIB_TOO_LONG, + // Error Return types + PTC_LIB_ERROR = 0x10, + PTC_LIB_BAD_POINTER, + PTC_LIB_BAD_ARGUMENT, + PTC_LIB_WRONG_STATE, + // Node Return types + PTC_NODE_WRONG_STATE = 0x30, + PTC_NODE_TOUCHED, + PTC_NODE_NOT_TOUCHED +} ptc_ret_t; + + +typedef enum ptc_node_type_enum { + NODE_TYPE_NOCONV_bm = (0x00), + NODE_MUTUAL_bm = (0x01), + NODE_RESERVED_bm = (0x02), + NODE_SELFCAP_bm = (0x04), + NODE_SHIELD_bm = (0x08), + NODE_SELFCAP_SHIELD_bm = (NODE_SHIELD_bm | NODE_SELFCAP_bm), // 0x0C + NODE_TYPE_bm = (NODE_MUTUAL_bm | NODE_SELFCAP_bm | NODE_SHIELD_bm), +} ptc_node_type_t; + + +typedef enum ptc_cb_event_enum { + PTC_CB_EVENT_TOUCH = 0x10, + PTC_CB_EVENT_TOUCH_DETECT = (PTC_CB_EVENT_TOUCH | 0x01), // 0x11 + PTC_CB_EVENT_TOUCH_RELEASE = (PTC_CB_EVENT_TOUCH | 0x02), // 0x12 + PTC_CB_EVENT_TOUCH_WAKE = (PTC_CB_EVENT_TOUCH | 0x04), // 0x14 + PTC_CB_EVENT_WAKE_TOUCH = (PTC_CB_EVENT_TOUCH_WAKE | 0x01), // 0x15 + PTC_CB_EVENT_WAKE_NO_TOUCH = (PTC_CB_EVENT_TOUCH_WAKE | 0x02), // 0x16 + PTC_CB_EVENT_CONV_CMPL = 0x20, + PTC_CB_EVENT_CONV_MUTUAL_CMPL = (PTC_CB_EVENT_CONV_CMPL | NODE_MUTUAL_bm), // 0x21 + PTC_CB_EVENT_CONV_SELF_CMPL = (PTC_CB_EVENT_CONV_CMPL | NODE_SELFCAP_bm), // 0x24 + PTC_CB_EVENT_CONV_SHIELD_CMPL = (PTC_CB_EVENT_CONV_CMPL | NODE_SELFCAP_SHIELD_bm), // 0x28 + PTC_CB_EVENT_CONV_TYPE_CMPL_MSK = (NODE_TYPE_bm), // 0x0D + PTC_CB_EVENT_CONV_CALIB = 0x40, + PTC_CB_EVENT_ERR_CALIB_LOW = (PTC_CB_EVENT_CONV_CALIB | 0x01), // 0x41 + PTC_CB_EVENT_ERR_CALIB_HIGH = (PTC_CB_EVENT_CONV_CALIB | 0x02), // 0x42 + PTC_CB_EVENT_ERR_CALIB_TO = (PTC_CB_EVENT_CONV_CALIB | 0x04), // 0x44 + PTC_CB_EVENT_ERR_CALIB_MSK = (0x04 | 0x02 | 0x01), // 0x07 +} ptc_cb_event_t; + + + + + +typedef enum ptc_lib_enum { + PTC_LIB_IDLE = 0x00, + PTC_LIB_CONV_PROG = 0x01, + PTC_LIB_CONV_COMPL = 0x02, + PTC_LIB_EVENT = 0x04, + PTC_LIB_CONV_LP = 0x08, + PTC_LIB_CONV_WCMP = 0x10, + PTC_LIB_SUSPENDED = 0x80 +} ptc_lib_t; + +typedef struct cap_sensor_type { + struct cap_sensor_type* nextNode; + ptc_node_type_t type; + ptc_id_t id; // number for easier identification in the callback + + ptc_ch_bm_t hw_xCh_bm; + ptc_ch_bm_t hw_yCh_bm; + uint16_t hw_compCaps; // [13:12] rough; [11:8] course; [7:4] fine; [3:0] accurate + uint8_t hw_rsel_presc; // [7:4] RSEL, [3:0] PRESC + uint8_t hw_a_d_gain; // [7:4] Analog Gain, [3:0] Digital Gain /* PTC_AGAIN / CTRLB.SAMPNUM */ + uint8_t hw_csd; // [4:0] Charge Share Delay /* SAMPLEN in SAMPCTRL */ + + ptc_node_state_t state; + uint16_t sensorData; // ADC data, Oversampling-corrected + uint16_t reference; // Compare Value for detection + int16_t touch_in_th; // this value is compared to the sensorData - threshold delta + int16_t touch_out_th; // this value is compared to the sensorData - threshold delta + + uint8_t stateMachine; + uint8_t lastStateChange; // stateChangeCounter +} cap_sensor_t; + + +// Abbreviation: NoM: Number of Measurements +typedef struct ptc_lib_sm_settings_type { + uint16_t force_recal_delta; // if the threshold value exceeds this compared to optimal (512), force recalibration. Defualt: 150 + uint8_t touched_detect_nom; // NoM above node threshold for the node to become touched. Default: 3 + uint8_t untouched_detect_nom; // NoM below node threshold for the node to become untouched. Default: 3 + uint8_t touched_max_nom; // NoM a touch was detected until a recal if forced. Value +1. Disabled with 0xFF. Default: 200 + uint8_t drift_up_nom; // NoM when no touch is detected, until the threshold is increased. Value +1. Disabled with 0xFF. Default: 20 + uint8_t drift_down_nom; // NoM when no touch is detected, until the threshold is decreased. Value +1. Disabled with 0xFF. Default: 20 +} ptc_lib_sm_set_t; + + + +#ifdef __cplusplus +} +#endif +#endif From a1c42da3cdb1d7529143b2b26ecad54821315658 Mon Sep 17 00:00:00 2001 From: MX682X Date: Tue, 10 Oct 2023 21:28:47 +0200 Subject: [PATCH 2/3] fixing of errors --- megaavr/libraries/PTC/README.md | 26 ++-- .../PTC/examples/mutualcap/mutualcap.ino | 4 +- .../self_and_mutual_mix.ino | 11 +- .../PTC/examples/selfcap/selfcap.ino | 6 +- .../selfcap_other_nodes_shield.ino | 4 +- .../selfcap_shield_lowpower.ino | 12 +- .../selfcap_with_dedicated_shield.ino | 4 +- .../temperature_mutual/temperature_mutual.ino | 4 +- megaavr/libraries/PTC/keywords.txt | 117 +++++++++--------- megaavr/libraries/PTC/library.properties | 2 +- megaavr/libraries/PTC/src/ptc.c | 58 ++++----- megaavr/libraries/PTC/src/ptc.h | 8 +- megaavr/libraries/PTC/src/ptc_io.h | 16 +-- megaavr/libraries/PTC/src/ptc_types.h | 2 +- 14 files changed, 140 insertions(+), 134 deletions(-) diff --git a/megaavr/libraries/PTC/README.md b/megaavr/libraries/PTC/README.md index 09259635..0b760450 100644 --- a/megaavr/libraries/PTC/README.md +++ b/megaavr/libraries/PTC/README.md @@ -16,9 +16,9 @@ Not needed, comes with the core. For the non-Arduino people: all you need is inside the src folder. - ## How to use this library +### Overview #### Step 1: Memory Initialization Allocate memory for every single sensing node. The easiest way is to have an array with `cap_sensor_t` elements, e.g. `cap_sensor_t nodes[3];`. Even though it allows you to access the data stored in this structures directly, it is not allowed to change the contents directly. Always use the provided setter functions for this. @@ -35,8 +35,8 @@ Here are some examples: #### Step 3: Acquisition and Processing -This library requires a regular call to the function `void ptc_process(uint16_t currTime)`. This function handles all background functionality of the library and calls the callback `extern void ptc_event_callback(const uint8_t eventType, cap_sensor_t* node)` when appropriate. First, the function checks if an acquisition was completed (all nodes of the selected type converted). If that's the case, it proceeds to handle the gathered data to handle the respective state machine of each node whose conversion was completed. The more nodes you have, the more time it might take. -The exact workings of this function will exceed the scope of this document. +This library requires a regular call to the function `void ptc_process(uint16_t currTime)`. This function handles all background functionality of the library and calls the callback `extern void ptc_event_callback(const uint8_t eventType, cap_sensor_t* node)` when appropriate. First, the function checks if an acquisition was completed (all nodes of the selected type converted). If that's the case, it proceeds to handle the gathered data to handle the respective state machine of each node whose conversion was completed. The more nodes you have, the more time it might take. +The exact workings of this function will exceed the scope of this document. This function takes an argument, `uint16_t currTime`, to decide when to start the next acquisition. This, compared to having a, as an example, `millis()` inside the library, offers the user significant flexibility on choosing the time source (e.g. TCB.CNT). The Period can be set by `void ptc_set_acqusition_period(uint16_t period)`. Whenever the currTime minus the currTime when the last acquisition trigger happened is greater or equal the period, a new acquisition is started. However, if there was a successful call to `uint8_t ptc_suspend(void)`, only the timestamp is updated, but no acquisition is started. This also applies when the library was put into low-power mode, except that conversions can still be triggered by events. Even though this library can handle three different node types, only one type can be acquired at a time. By default, the type of the first node in the list is used. But the user can use the function `ptc_set_next_conversion_type(uint8_t type)` to change this behavior. @@ -56,7 +56,7 @@ There are following callbacks: - `ptc_event_cb_conversion`: - `PTC_CB_EVENT_CONV_CMPL` (0x20): Anytime a node has finished it's conversion and the state-machine was updated - `PTC_CB_EVENT_CONV_MUTUAL_CMPL` (0x21). Useful to select the next type of conversions, if you have a mix of types - - `PTC_CB_EVENT_CONV_SELF_CMPL` (0x24). You can AND the values with NODE_MUTUAL_bm, NODE_SELFCAP_bm, + - `PTC_CB_EVENT_CONV_SELF_CMPL` (0x24). You can AND the values with NODE_MUTUAL_bm, NODE_SELFCAP_bm, - `PTC_CB_EVENT_CONV_SHIELD_CMPL` (0x28). NODE_SELFCAP_SHIELD_bm. - `PTC_CB_EVENT_CONV_TYPE_CMPL_MSK`(0x0D): Can be used with bitwise AND to differentiate between node and type conversions. - `ptc_event_cb_calibration`: @@ -92,7 +92,7 @@ The ptc_ch_bm_t is a typedef that depends on the pincount of the device and rang (Source: [Microchip's PTC Subsystem Firmware User's Guide](https://web.archive.org/web/20221225142000/https://www.mouser.com/pdfdocs/SAMA5D2_PTC_Firmware_UG.pdf)) This schematic was made for a different chip, but it is likely to look similar on the AVRs. -Most of the following is a hypothesis based on the publically available documentation and observation. +Most of the following is a hypothesis based on the publicly available documentation and observation. The PTC is using a charge transfer between a variable and a fixed capacitance to measure a difference between those two. The variable capacitance in this case is the sensor electrode and the fixed one is the internal Cc. The neat thing about having a dedicated hardware for this, is that Cc is not really fixed, compared to the Sample-and-Hold capacitor of the normal ADC, it can be calibrated to be about the same as the electrode we want to measure (See below). In order to measure the capacitance of the node, the PTC performs following Steps: @@ -104,24 +104,24 @@ In order to measure the capacitance of the node, the PTC performs following Step The measured voltage depends on what happens around the electrode. In an idle state, 50% of the charge of the node (and thus voltage) is transferred to Cc, however when something conductive is moved towards the electrode, the capacitance will increase slightly. With a higher capacitance, the node will have a higher charge, meaning less then 50% of the charge can be transferred to Cc until they reach an equal Voltage, which means the overall voltage will be higher, which can be measured by the ADC. -If you have trouble understanding: Imagine two equally sized volumes connected trough a valve. You fill the first up with water, open the valve and look on the second, how high the water has risen. After marking the "idle" level, when you put a finger in the water and see the water rise. This new level is the increase in voltage due to an interference. +If you have trouble understanding: Imagine two equally sized volumes connected through a valve. You fill the first up with water, open the valve and look on the second, how high the water has risen. After marking the "idle" level, when you put a finger in the water and see the water rise. This new level is the increase in voltage due to an interference. ### Calibration / Compensation Based on the documentation found online, the PTC has an internal, tunable capacitor connected after the series resistance to ground that is used to compensate the parasitic capacitance of the electrodes. Every node starts with a default compensation value. As soon as the node is enabled, the library attempts to find a compensation setting that will result in an ADC value of about 512 counts (1/2 of ADC resolution). Based on oscilloscope readings, it can also be said that the PTC tries to have a charge of 50% VCC on the electrode when being acquired. This is the also the reason, why the digital input function of the pins is disabled. -The maximum compensation is about 30pF for Mutual-cap and about 50pF for Self-cap. It is possible to get the compensation capacitance with uint16_t ptc_get_node_cc_fempto(cap_sensor_t* node); - however this function has to do a lot of calculations and is thus a bit bloat-y. It will also return the value in fempto farrads, to avoid floats. Read more here: https://www.microchipdeveloper.com/touch:guide-to-interpret-cc-calibration-value +The maximum compensation is about 30pF for Mutual-cap and about 50pF for Self-cap. It is possible to get the compensation capacitance with uint16_t ptc_get_node_cc_femto(cap_sensor_t* node); - however this function has to do a lot of calculations and is thus a bit bloat-y. It will also return the value in femto farrads, to avoid floats. Read more here: https://www.microchipdeveloper.com/touch:guide-to-interpret-cc-calibration-value Different pins have a different parasitic capacitance. I suspect this is depends on the internal circuitry and alternative functions, so it's normal to see some difference with pins next to each other. ### Tuning of nodes In order to ease the use of the PTC module, the ptc_add_* functions will initialize the cap_sensor_t struct with some default values, like the CC value mentioned above. That values can be easily changed and will be applied the next time a conversion of said node starts. Here is a list: - - Analog Gain. Increases the sensitivity of the electrode by adjusting a capacitor on a integrator (I think) (1x Gain) - - Digital Gain. Defines the amount of ADC Oversampling. Will not affect the count value, as it is internally right-shifted. (16x Oversampled) - - Charge Share Delay. Affects the Sample length of the ADC. (0 extra clocks) - - Prescaler. It is possible to slow down the ADC clock by adjusting the Prescaler. (Depends on CPU clock, targeted: 1MHz +/- 25%) - - Serial Resistor. Allows to change the serial resistor between the Cc and the node. Fixed at 100k for Self-Cap. Creates RC-low-pass filter. +- Analog Gain. Increases the sensitivity of the electrode by adjusting a capacitor on a integrator (I think) (1x Gain) +- Digital Gain. Defines the amount of ADC Oversampling. Will not affect the count value, as it is internally right-shifted. (16x Oversampled) +- Charge Share Delay. Affects the Sample length of the ADC. (0 extra clocks) +- Prescaler. It is possible to slow down the ADC clock by adjusting the Prescaler. (Depends on CPU clock, targeted: 1MHz +/- 25%) +- Serial Resistor. Allows to change the serial resistor between the Cc and the node. Fixed at 100k for Self-Cap. Creates RC-low-pass filter. If a node is not sensitive enough, you can increase the Analog Gain (if it becomes too sensitive, an increase of the thresholds might be needed). However it is better to have a bigger node to begin with because the bigger the area, the higher is the capacitance delta. @@ -129,7 +129,7 @@ If a node is not sensitive enough, you can increase the Analog Gain (if it becom The state-machine, which changes the node's state between Calibration, touch, no touch, etc. uses some variables that are valid for all nodes, those are: - `uint16_t force_recal_delta`. Each node has a threshold value that is used to calculate the delta. This Threshold value is drifting over time to adjust for environmental changes. If the threshold value drifts 512 +/- this value, a recalibration of CC is performed. Default: 150 - `uint8_t touched_detect_nom`. Number of consecutive Measurements (Conversions) that are above the touch threshold until the node becomes "touched". Default: 3 - - `uint8_t untouched_detect_nom`. Number of consecutive measurments that are below the no-touch threshold until the node is fully untouched. Default: 3 + - `uint8_t untouched_detect_nom`. Number of consecutive measurements that are below the no-touch threshold until the node is fully untouched. Default: 3 - `uint8_t touched_max_nom`. Number of consecutive measurements plus one in the touched state until a recalibration is forced. Can be disabled by writing 255 to it. Default: 200 - `uint8_t drift_up_nom`. If the delta is higher then the reference, but lower then the threshold, the amount of consecutive measurements plus one, until the reference is incremented. Can be disabled with 255. Default: 20. - `uint8_t drift_down_nom`. If the delta is below the reference, the amount of consecutive measurements plus one until the reference is decremented. Can be disabled with 255. Default: 20. diff --git a/megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino b/megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino index 5c52a1c7..a17dfa72 100644 --- a/megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino +++ b/megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino @@ -5,7 +5,7 @@ * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can * be used for quick actions, like switching a pin or variable, * but it is recommended to use PTC_CB_EVENT_CONV_MUTUAL_CMPL, as - * otherwise the handling of the successing nodes would be delayed. + * otherwise the handling of the successive nodes would be delayed. */ #define MySerial Serial @@ -15,7 +15,7 @@ void setup() { MySerial.begin(115200); // this puts the node on the list and initializes to default values - ptc_add_mutualcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA7)); + ptc_add_mutualcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA7)); ptc_add_mutualcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PA7)); diff --git a/megaavr/libraries/PTC/examples/self_and_mutual_mix/self_and_mutual_mix.ino b/megaavr/libraries/PTC/examples/self_and_mutual_mix/self_and_mutual_mix.ino index 97da98d1..052aaf86 100644 --- a/megaavr/libraries/PTC/examples/self_and_mutual_mix/self_and_mutual_mix.ino +++ b/megaavr/libraries/PTC/examples/self_and_mutual_mix/self_and_mutual_mix.ino @@ -1,5 +1,12 @@ - #include +/* + * This example creates four different sensing nodes. of two different types. + * PA4 and PA5 are the self-cap lines with PB0 acting as shield pin. + * PA6 and PA7 are the Y-Lines with PB1 acting as the X-line. + * PTC_CB_EVENT_CONV_MUTUAL_CMPL and + * PTC_CB_EVENT_CONV_SHIELD_CMPL can be used to change the type that is converted. + * This will create an interlaced conversion, but it is not mandatory to do so. + */ #define MySerial Serial @@ -10,7 +17,7 @@ void setup() { // put your setup code here, to run once: ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PB0)); ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PB0)); - + ptc_add_mutualcap_node(&nodes[2], PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PB1)); ptc_add_mutualcap_node(&nodes[3], PIN_TO_PTC(PIN_PA7), PIN_TO_PTC(PIN_PB1)); diff --git a/megaavr/libraries/PTC/examples/selfcap/selfcap.ino b/megaavr/libraries/PTC/examples/selfcap/selfcap.ino index ee9e02f2..05e3eea3 100644 --- a/megaavr/libraries/PTC/examples/selfcap/selfcap.ino +++ b/megaavr/libraries/PTC/examples/selfcap/selfcap.ino @@ -5,7 +5,7 @@ * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can * be used for quick actions, like switching a pin or variable, * but it is recommended to use PTC_CB_EVENT_CONV_SELF_CMPL, as - * otherwise the handling of the successing nodes would be delayed. + * otherwise the handling of the successive nodes would be delayed. */ #define MySerial Serial @@ -15,7 +15,7 @@ void setup() { MySerial.begin(115200); // this puts the node on the list and initializes to default values - ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), 0); + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), 0); ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), 0); ptc_add_selfcap_node(&nodes[2], PIN_TO_PTC(PIN_PA6), 0); @@ -59,4 +59,4 @@ void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node } MySerial.print(" Node: "); MySerial.println(ptc_get_node_id(node)); -} \ No newline at end of file +} diff --git a/megaavr/libraries/PTC/examples/selfcap_other_nodes_shield/selfcap_other_nodes_shield.ino b/megaavr/libraries/PTC/examples/selfcap_other_nodes_shield/selfcap_other_nodes_shield.ino index 3e17275a..de2ea758 100644 --- a/megaavr/libraries/PTC/examples/selfcap_other_nodes_shield/selfcap_other_nodes_shield.ino +++ b/megaavr/libraries/PTC/examples/selfcap_other_nodes_shield/selfcap_other_nodes_shield.ino @@ -5,7 +5,7 @@ * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can * be used for quick actions, like switching a pin or variable, * but it is recommended to use PTC_CB_EVENT_CONV_SELF_CMPL, as - * otherwise the handling of the successing nodes would be delayed. + * otherwise the handling of the successive nodes would be delayed. * This example demonstrates how to use the other sensor nodes * as shield. This improves the signal-to-noise ratio. */ @@ -17,7 +17,7 @@ void setup() { MySerial.begin(115200); // this puts the node on the list and initializes to default values - ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA5) | PIN_TO_PTC(PIN_PA6)); + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA5) | PIN_TO_PTC(PIN_PA6)); ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PA4) | PIN_TO_PTC(PIN_PA6)); ptc_add_selfcap_node(&nodes[2], PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PA4) | PIN_TO_PTC(PIN_PA5)); diff --git a/megaavr/libraries/PTC/examples/selfcap_shield_lowpower/selfcap_shield_lowpower.ino b/megaavr/libraries/PTC/examples/selfcap_shield_lowpower/selfcap_shield_lowpower.ino index 9216a677..f714209e 100644 --- a/megaavr/libraries/PTC/examples/selfcap_shield_lowpower/selfcap_shield_lowpower.ino +++ b/megaavr/libraries/PTC/examples/selfcap_shield_lowpower/selfcap_shield_lowpower.ino @@ -12,7 +12,7 @@ * As millis() is not incremented while the CPU is asleep, it will * take the Chip 10 seconds after a wake event before it goes back * to sleep. - * ptc_process will also fire either PTC_CB_EVENT_WAKE_TOUCH or + * ptc_process will also fire either PTC_CB_EVENT_WAKE_TOUCH or * PTC_CB_EVENT_WAKE_NO_TOUCH after a conversion on the lp-node * has been finished. */ @@ -25,7 +25,7 @@ void setup() { MySerial.begin(115200); // this puts the node on the list and initializes to default values - ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA5)); + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA5)); ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PA5)); // as each node requires some calibration and drift, it is recommended @@ -52,7 +52,7 @@ void setup() { //EVSYS.ASYNCUSER8 = EVSYS_ASYNCUSER8_ASYNCCH3_gc; //PORTMUX.CTRLA = PORTMUX_EVOUT0_bm; //pinMode(PIN_PA2, OUTPUT); - + // Stand-by sleep SLPCTRL.CTRLA = SLEEP_MODE_STANDBY | SLPCTRL_SEN_bm; @@ -72,7 +72,7 @@ void loop() { MySerial.flush(); // empty the Serial TX buffer ptc_lp_init(&lp_node); // Will set STARTEI bit to start conversions - while (ptc_lp_was_waken() != PTC_LIB_WAS_WAKEN) { + while (ptc_lp_was_waken() != PTC_LIB_WAS_WAKEN) { sleep_cpu(); // Make sure the ADC has woken the CPU, otherwise, go back to sleep } // The library does not filter the wake-up events. The user must make sure // there are no detected touches before going back to sleep. @@ -114,9 +114,9 @@ void ptc_event_cb_calibration(const ptc_cb_event_t eventType, cap_sensor_t* node void ptc_event_cb_wake(const ptc_cb_event_t eventType, cap_sensor_t* node) { if (PTC_CB_EVENT_WAKE_TOUCH == eventType) { - // True if the node was touched when a wakeup occured + // True if the node was touched when a wakeup occurred } else if (PTC_CB_EVENT_WAKE_NO_TOUCH == eventType) { - // True if the node was no touch when a wakeup occured + // True if the node was no touch when a wakeup occurred } (void)node; // remove unused warning } diff --git a/megaavr/libraries/PTC/examples/selfcap_with_dedicated_shield/selfcap_with_dedicated_shield.ino b/megaavr/libraries/PTC/examples/selfcap_with_dedicated_shield/selfcap_with_dedicated_shield.ino index 09f92ce7..e09f7aea 100644 --- a/megaavr/libraries/PTC/examples/selfcap_with_dedicated_shield/selfcap_with_dedicated_shield.ino +++ b/megaavr/libraries/PTC/examples/selfcap_with_dedicated_shield/selfcap_with_dedicated_shield.ino @@ -5,7 +5,7 @@ * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can * be used for quick actions, like switching a pin or variable, * but it is recommended to use PTC_CB_EVENT_CONV_SELF_CMPL, as - * otherwise the handling of the successing nodes would be delayed. + * otherwise the handling of the successive nodes would be delayed. * This example demonstrates how to use the other sensor nodes * as shield. This improves the signal-to-noise ratio. */ @@ -17,7 +17,7 @@ void setup() { MySerial.begin(115200); // this puts the node on the list and initializes to default values - ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA6)); + ptc_add_selfcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA6)); ptc_add_selfcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PA6)); // Make sure Serial works diff --git a/megaavr/libraries/PTC/examples/temperature_mutual/temperature_mutual.ino b/megaavr/libraries/PTC/examples/temperature_mutual/temperature_mutual.ino index 9da047ff..0c6a1d64 100644 --- a/megaavr/libraries/PTC/examples/temperature_mutual/temperature_mutual.ino +++ b/megaavr/libraries/PTC/examples/temperature_mutual/temperature_mutual.ino @@ -5,7 +5,7 @@ * PTC_CB_EVENT_TOUCH_DETECT and PTC_CB_EVENT_TOUCH_RELEASE can * be used for quick actions, like switching a pin or variable, * but it is recommended to use PTC_CB_EVENT_CONV_MUTUAL_CMPL, as - * otherwise the handling of the successing nodes would be delayed. + * otherwise the handling of the successive nodes would be delayed. */ #define MySerial Serial @@ -15,7 +15,7 @@ void setup() { MySerial.begin(115200); // this puts the node on the list and initializes to default values - ptc_add_mutualcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA7)); + ptc_add_mutualcap_node(&nodes[0], PIN_TO_PTC(PIN_PA4), PIN_TO_PTC(PIN_PA7)); ptc_add_mutualcap_node(&nodes[1], PIN_TO_PTC(PIN_PA5), PIN_TO_PTC(PIN_PA7)); ptc_add_mutualcap_node(&nodes[2], PIN_TO_PTC(PIN_PA6), PIN_TO_PTC(PIN_PA7)); diff --git a/megaavr/libraries/PTC/keywords.txt b/megaavr/libraries/PTC/keywords.txt index 78c0124b..ea0ca5b6 100644 --- a/megaavr/libraries/PTC/keywords.txt +++ b/megaavr/libraries/PTC/keywords.txt @@ -6,14 +6,14 @@ # Datatypes (KEYWORD1) ####################################### ptc_ch_bm_t KEYWORD1 -ptc_node_state_t KEYWORD1 -ptc_sm_t KEYWORD1 +ptc_node_state_t KEYWORD1 +ptc_sm_t KEYWORD1 ptc_ret_t KEYWORD1 ptc_lib_t KEYWORD1 -cap_sensor_t KEYWORD1 -ptc_lib_sm_set_t KEYWORD1 -ptc_cb_event_t KEYWORD1 -PTC_t KEYWORD1 +cap_sensor_t KEYWORD1 +ptc_lib_sm_set_t KEYWORD1 +ptc_cb_event_t KEYWORD1 +PTC_t KEYWORD1 @@ -21,42 +21,42 @@ PTC_t KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### ptc_process KEYWORD2 -ptc_event_callback KEYWORD2 -ptc_event_cb_touch KEYWORD2 -ptc_event_cb_conversion KEYWORD2 -ptc_event_cb_calibration KEYWORD2 -ptc_event_cb_wake KEYWORD2 -ptc_event_cb_error KEYWORD2 -ptc_enable_node KEYWORD2 -ptc_disable_node KEYWORD2 -ptc_set_next_conversion_type KEYWORD2 -ptc_node_set_thresholds KEYWORD2 -ptc_node_set_resistor KEYWORD2 -ptc_node_set_prescaler KEYWORD2 -ptc_node_set_gain KEYWORD2 -ptc_add_selfcap_node KEYWORD2 -ptc_add_mutualcap_node KEYWORD2 -ptc_suspend KEYWORD2 -ptc_resume KEYWORD2 -ptc_get_node_cc_fempto KEYWORD2 -ptc_get_node_xCh_bm KEYWORD2 -ptc_get_node_yCh_bm KEYWORD2 -ptc_get_node_sensor_value KEYWORD2 -ptc_get_node_touched KEYWORD2 +ptc_event_callback KEYWORD2 +ptc_event_cb_touch KEYWORD2 +ptc_event_cb_conversion KEYWORD2 +ptc_event_cb_calibration KEYWORD2 +ptc_event_cb_wake KEYWORD2 +ptc_event_cb_error KEYWORD2 +ptc_enable_node KEYWORD2 +ptc_disable_node KEYWORD2 +ptc_set_next_conversion_type KEYWORD2 +ptc_node_set_thresholds KEYWORD2 +ptc_node_set_resistor KEYWORD2 +ptc_node_set_prescaler KEYWORD2 +ptc_node_set_gain KEYWORD2 +ptc_add_selfcap_node KEYWORD2 +ptc_add_mutualcap_node KEYWORD2 +ptc_suspend KEYWORD2 +ptc_resume KEYWORD2 +ptc_get_node_cc_fempto KEYWORD2 +ptc_get_node_xCh_bm KEYWORD2 +ptc_get_node_yCh_bm KEYWORD2 +ptc_get_node_sensor_value KEYWORD2 +ptc_get_node_touched KEYWORD2 ptc_get_node_sm KEYWORD2 -ptc_get_node_delta KEYWORD2 -ptc_get_node_state KEYWORD2 -ptc_node_request_recal KEYWORD2 +ptc_get_node_delta KEYWORD2 +ptc_get_node_state KEYWORD2 +ptc_node_request_recal KEYWORD2 ptc_init_ADC0 KEYWORD2 ptc_lp_init KEYWORD2 -ptc_lp_disable KEYWORD2 -ptc_lp_was_waken KEYWORD2 +ptc_lp_disable KEYWORD2 +ptc_lp_was_waken KEYWORD2 ptc_get_sm_settings KEYWORD2 -ptc_event_cb_touch KEYWORD2 -ptc_event_cb_wake KEYWORD2 -ptc_event_cb_conversion KEYWORD2 -ptc_event_cb_calibration KEYWORD2 -ptc_event_cb_error KEYWORD2 +ptc_event_cb_touch KEYWORD2 +ptc_event_cb_wake KEYWORD2 +ptc_event_cb_conversion KEYWORD2 +ptc_event_cb_calibration KEYWORD2 +ptc_event_cb_error KEYWORD2 ####################################### # Instances (KEYWORD2) @@ -66,26 +66,25 @@ ptc_event_cb_error KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### -PTC_CB_EVENT_TOUCH LITERAL1 -PTC_CB_EVENT_TOUCH_DETECT LITERAL1 -PTC_CB_EVENT_TOUCH_RELEASE LITERAL1 -PTC_CB_EVENT_TOUCH_WAKE LITERAL1 -PTC_CB_EVENT_WAKE_TOUCH LITERAL1 -PTC_CB_EVENT_WAKE_NO_TOUCH LITERAL1 -PTC_CB_EVENT_CONV_CMPL LITERAL1 -PTC_CB_EVENT_CONV_MUTUAL_CMPL LITERAL1 -PTC_CB_EVENT_CONV_SELF_CMPL LITERAL1 -PTC_CB_EVENT_CONV_SHIELD_CMPL LITERAL1 -PTC_CB_EVENT_CONV_TYPE_CMPL_MSK LITERAL1 -PTC_CB_EVENT_CONV_CALIB LITERAL1 -PTC_CB_EVENT_ERR_CALIB_LOW LITERAL1 -PTC_CB_EVENT_ERR_CALIB_HIGH LITERAL1 -PTC_CB_EVENT_ERR_CALIB_TO LITERAL1 -PTC_CB_EVENT_ERR_CALIB_MSK LITERAL1 +PTC_CB_EVENT_TOUCH LITERAL1 +PTC_CB_EVENT_TOUCH_DETECT LITERAL1 +PTC_CB_EVENT_TOUCH_RELEASE LITERAL1 +PTC_CB_EVENT_TOUCH_WAKE LITERAL1 +PTC_CB_EVENT_WAKE_TOUCH LITERAL1 +PTC_CB_EVENT_WAKE_NO_TOUCH LITERAL1 +PTC_CB_EVENT_CONV_CMPL LITERAL1 +PTC_CB_EVENT_CONV_MUTUAL_CMPL LITERAL1 +PTC_CB_EVENT_CONV_SELF_CMPL LITERAL1 +PTC_CB_EVENT_CONV_SHIELD_CMPL LITERAL1 +PTC_CB_EVENT_CONV_TYPE_CMPL_MSK LITERAL1 +PTC_CB_EVENT_CONV_CALIB LITERAL1 +PTC_CB_EVENT_ERR_CALIB_LOW LITERAL1 +PTC_CB_EVENT_ERR_CALIB_HIGH LITERAL1 +PTC_CB_EVENT_ERR_CALIB_TO LITERAL1 +PTC_CB_EVENT_ERR_CALIB_MSK LITERAL1 NODE_TYPE_NOCONV_bm LITERAL1 -NODE_MUTUAL_bm LITERAL1 +NODE_MUTUAL_bm LITERAL1 NODE_SELFCAP_bm LITERAL1 -NODE_SHIELD_bm LITERAL1 -NODE_SELFCAP_SHIELD_bm LITERAL1 -NODE_TYPE_bm LITERAL1 - +NODE_SHIELD_bm LITERAL1 +NODE_SELFCAP_SHIELD_bm LITERAL1 +NODE_TYPE_bm LITERAL1 diff --git a/megaavr/libraries/PTC/library.properties b/megaavr/libraries/PTC/library.properties index e954c009..52f3c017 100644 --- a/megaavr/libraries/PTC/library.properties +++ b/megaavr/libraries/PTC/library.properties @@ -7,4 +7,4 @@ paragraph=The PTC contains hardware to assist in touch detection. This library h category=Sensors includes=ptc.h url=https://github.com/MX682X/ptc_touch -architectures=megaavr \ No newline at end of file +architectures=megaavr diff --git a/megaavr/libraries/PTC/src/ptc.c b/megaavr/libraries/PTC/src/ptc.c index 74164f1a..7ce8e945 100644 --- a/megaavr/libraries/PTC/src/ptc.c +++ b/megaavr/libraries/PTC/src/ptc.c @@ -1,5 +1,5 @@ -/* - * Refer to ptc_touch.h file for copyright, changelog, usage and license information +/* + * Refer to ptc_touch.h file for copyright, changelog, usage and license information */ @@ -8,7 +8,7 @@ // The following functions are used internally only. -// get the pointer of the last valid node struct can be "firstNode" +// get the pointer of the last valid node struct can be "firstNode" cap_sensor_t* ptc_get_last_node(void); @@ -54,7 +54,7 @@ volatile ptc_lib_t ptc_lib_state = PTC_LIB_IDLE; /* * Global settings for the state-machine applied on every node. * Pointer to this struct can be retrieved by ptc_get_sm_settings() - * + * */ ptc_lib_sm_set_t ptc_sm_settings = { .force_recal_delta = 150, @@ -72,7 +72,7 @@ ptc_lib_sm_set_t* ptc_get_sm_settings() { // Analog gain Values, extracted with a debugger const uint8_t ptc_a_gain_lut[] = { - 0x3F, 0x1C, 0x0B, + 0x3F, 0x1C, 0x0B, 0x05, 0x03, 0x01, }; @@ -89,7 +89,7 @@ const uint8_t ptc_a_gain_lut[] = { // Workarounds to make the code work with DAs... Argh. #if defined (__PTC_DA__) #ifndef ADC_SAMPNUM_ACC1_gc - #define ADC_SAMPNUM_ACC1_gc ADC_SAMPNUM_NONE_gc + #define ADC_SAMPNUM_ACC1_gc ADC_SAMPNUM_NONE_gc #endif #ifndef ADC_REFSEL_VDDREF_gc #define ADC_REFSEL_VDDREF_gc 0x00 @@ -121,7 +121,7 @@ __attribute__ ((weak, alias("ptc_event_callback"))) void ptc_event_cb_error(cons // Set the threshold for touch detection and away from touch for a node uint8_t ptc_node_set_thresholds (cap_sensor_t* node, int16_t th_in, int16_t th_out) { - if (NULL == node) + if (NULL == node) return PTC_LIB_BAD_POINTER; node->touch_in_th = th_in; @@ -133,7 +133,7 @@ uint8_t ptc_node_set_thresholds (cap_sensor_t* node, int16_t th_in, int16_t th_o // Change Resistor Setting. Note: Only has an effect on mutual sensors uint8_t ptc_node_set_resistor(cap_sensor_t* node, uint8_t res) { PTC_CHECK_FOR_BAD_POINTER(node); - + if (res > RSEL_MAX) return PTC_LIB_BAD_ARGUMENT; @@ -169,10 +169,10 @@ uint8_t ptc_node_set_gain(cap_sensor_t* node, uint8_t aGain, uint8_t dGain) { if (__builtin_constant_p(aGain)) badArg("Analog Gain too high. Max Analog Gain Value is 0x05 (equals 32x)"); return PTC_LIB_BAD_ARGUMENT; - } - + } + if (dGain > 0x06) { - if (__builtin_constant_p(dGain)) + if (__builtin_constant_p(dGain)) badArg("Digital Gain too high. Max Digital Gain Value is 0x06 (equals 64x)"); return PTC_LIB_BAD_ARGUMENT; } @@ -238,7 +238,7 @@ void ptc_init_ADC0(void) { pPTC->CTRLB = ADC_SAMPNUM_ACC1_gc; pPTC->CTRLA = ADC_ENABLE_bm; #elif defined (__PTC_DA__) - + #endif } @@ -249,10 +249,10 @@ void ptc_add_node_common(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc_ch_bm_t xCh); // if xCh > 0, with shield, otherwise usual selfcap uint8_t ptc_add_selfcap_node_asserted(cap_sensor_t* node, const ptc_ch_bm_t yCh, const ptc_ch_bm_t xCh) { PTC_CHECK_POINTER(node, PTC_LIB_BAD_POINTER); // check not in h file as gcc doesn't know about the memory address - + if (ptc_append_node(node) != PTC_LIB_SUCCESS) return PTC_LIB_BAD_POINTER; - + ptc_add_node_common(node, yCh, xCh); if (xCh > 0) node->type = NODE_SELFCAP_SHIELD_bm; @@ -281,8 +281,8 @@ uint8_t ptc_add_mutualcap_node_asserted(cap_sensor_t* node, ptc_ch_bm_t yCh, ptc node->touch_in_th = 10; node->touch_out_th = 5; node->hw_compCaps = PTC_DEFAULT_MC_CC; /* value from official library */ - node->hw_rsel_presc = NODE_RSEL_PRSC(RSEL_VAL_100, PTC_PRESC_DEFAULT); - + node->hw_rsel_presc = NODE_RSEL_PRSC(RSEL_VAL_100, PTC_PRESC_DEFAULT); + return PTC_LIB_SUCCESS; } @@ -411,14 +411,14 @@ uint8_t ptc_lp_was_waken(void) { /* not recommended to use, as the calculation is bloated */ -uint16_t ptc_get_node_cc_fempto (cap_sensor_t* node) { +uint16_t ptc_get_node_cc_femto (cap_sensor_t* node) { if (NULL == node) return 0; uint16_t retVal = 0; uint16_t comp = node->hw_compCaps; for (uint8_t i = 0; i < 3; i++) { - retVal /= 10; /* "skips" last addition */ + retVal /= 10; /* "skips" last addition */ uint8_t temp = comp & 0x0F; retVal += (temp * 675); comp >>= 4; /* select next field */ @@ -612,7 +612,7 @@ void ptc_process_node_sm (cap_sensor_t* node) { } - + if (node->stateMachine != nodeSM) { node->lastStateChange = 0; node->stateMachine = nodeSM; @@ -624,7 +624,7 @@ void ptc_process_node_sm (cap_sensor_t* node) { uint8_t ptc_process_calibrate (cap_sensor_t* node) { uint16_t rawData = node->sensorData; - + #if defined(__PTC_Tiny__) uint16_t compensation = node->hw_compCaps; uint8_t cc_accurate = compensation & 0x0F; @@ -723,7 +723,7 @@ uint8_t ptc_process_calibrate (cap_sensor_t* node) { cc_fine -= dir; if (dir < 0) return PTC_LIB_CALIB_TOO_LOW; - else + else return PTC_LIB_CALIB_TOO_HIGH; } else { cc_coarse -= dirOvf; @@ -836,8 +836,8 @@ void ptc_init_conversion(uint8_t nodeType) { } else { return; } - - + + uint8_t freq = freq_select; if (nodeType == NODE_MUTUAL_bm) { pPTC->SAMPDLY = 0x00; @@ -852,7 +852,7 @@ void ptc_init_conversion(uint8_t nodeType) { freq = 0x0F; pPTC->SAMPDLY = freq; } - + pPTC->INTFLAGS = ADC_RESRDY_bm | ADC_WCMP_bm; // clear ISR flags, if there were unhandled currConvType = nodeType; @@ -897,7 +897,7 @@ void ptc_set_registers(cap_sensor_t* node) { if (NULL == node) return; - + uint8_t analogGain = 0x3F; if ((node->state.disabled == 0) && (node->stateMachine != PTC_SM_NOINIT_CAL)) { uint8_t lut_index = node->hw_a_d_gain / 16; // A little workaround as >> 4 is kinda broken sometimes. @@ -923,9 +923,9 @@ void ptc_set_registers(cap_sensor_t* node) { pPTC->CTRLP |= 0x03; pPTC->CTRLA = ADC_RUNSTBY_bm | ADC_ENABLE_bm; /* 0x81 */ - if (0 == node->state.low_power) + if (0 == node->state.low_power) pPTC->COMMAND = 0x01; // Normal opertion: Manual Start - else + else pPTC->EVCTRL = 0x01; // Low Power: Start by positive Flank on Event #elif defined (__PTC_DA__) @@ -952,7 +952,7 @@ void ptc_eoc(void) { if (pCurrentNode->state.low_power) { if (flags & ADC_WCMP_bm) { - pCurrentNode->state.win_comp = 1; + pCurrentNode->state.win_comp = 1; ptc_lib_state = PTC_LIB_CONV_WCMP; } else { pCurrentNode->state.win_comp = 0; @@ -976,7 +976,7 @@ cap_sensor_t* ptc_get_last_node (void) { cap_sensor_t *nextNode = node->nextNode; if (nextNode == NULL) return node; - else + else node = nextNode; } } diff --git a/megaavr/libraries/PTC/src/ptc.h b/megaavr/libraries/PTC/src/ptc.h index 0e64a9f5..7ff70370 100644 --- a/megaavr/libraries/PTC/src/ptc.h +++ b/megaavr/libraries/PTC/src/ptc.h @@ -150,7 +150,7 @@ uint8_t ptc_suspend(void); void ptc_resume(void); // If you want to know the compensation capacitance in fempto Farrad -uint16_t ptc_get_node_cc_fempto(cap_sensor_t* node); +uint16_t ptc_get_node_cc_femto(cap_sensor_t* node); ptc_lib_sm_set_t* ptc_get_sm_settings(); @@ -171,12 +171,12 @@ inline uint16_t ptc_get_node_sensor_value(cap_sensor_t* node) { return node->sensorData; } -// returns true, if node is a valid pointer and node is touched, otherwise false. +// returns true, if node is a valid pointer and node is touched, otherwise false. // No other return value so there can be easy checks - not null or null inline uint8_t ptc_get_node_touched(cap_sensor_t* node) { if (node == NULL) return 0x00; - if (node->stateMachine & (PTC_SM_TOUCH_DETECT | PTC_SM_TOUCH_OUT_FLT)) + if (node->stateMachine & (PTC_SM_TOUCH_DETECT | PTC_SM_TOUCH_OUT_FLT)) return 0x01; return 0x00; } @@ -205,7 +205,7 @@ inline uint8_t ptc_get_node_state(cap_sensor_t* node) { inline ptc_id_t ptc_get_node_id(cap_sensor_t* node) { if (node == NULL) return 0xFF; - + return (node->id); } diff --git a/megaavr/libraries/PTC/src/ptc_io.h b/megaavr/libraries/PTC/src/ptc_io.h index a658009a..a80f24a4 100644 --- a/megaavr/libraries/PTC/src/ptc_io.h +++ b/megaavr/libraries/PTC/src/ptc_io.h @@ -1,5 +1,5 @@ -/* - * Refer to ptc.h file for copyright, changelog, usage and license information +/* + * Refer to ptc.h file for copyright, changelog, usage and license information */ #pragma once @@ -367,7 +367,7 @@ const ptc_ch_bm_t digital_pin_to_ptc_bm [] = { // lookup-table to quickly disable input and pull-up const uint8_t ptc_ch_to_pin [] = { #if (__PTC_Pincount__ <= 14) - PORTA_ISC(4), + PORTA_ISC(4), PORTA_ISC(5), PORTA_ISC(6), PORTA_ISC(7), @@ -392,7 +392,7 @@ const uint8_t ptc_ch_to_pin [] = { PORTA_ISC(5), PORTA_ISC(6), PORTA_ISC(7), // X7 - + PORTB_ISC(0), PORTB_ISC(1), PORTB_ISC(2), @@ -401,7 +401,7 @@ const uint8_t ptc_ch_to_pin [] = { PORTB_ISC(5), PORTB_ISC(6), PORTB_ISC(7), // X15 - + PORTD_ISC(0), PORTD_ISC(1), PORTD_ISC(2), @@ -410,7 +410,7 @@ const uint8_t ptc_ch_to_pin [] = { PORTD_ISC(5), PORTD_ISC(6), PORTD_ISC(7), // X23 - + PORTE_ISC(0), PORTE_ISC(1), PORTE_ISC(2), @@ -419,7 +419,7 @@ const uint8_t ptc_ch_to_pin [] = { PORTE_ISC(5), PORTE_ISC(6), PORTE_ISC(7), // X31 - + PORTF_ISC(0), PORTF_ISC(1), PORTF_ISC(2), @@ -428,7 +428,7 @@ const uint8_t ptc_ch_to_pin [] = { PORTF_ISC(5), // X37 0x00, 0x00, - + PORTG_ISC(0), PORTG_ISC(1), PORTG_ISC(2), diff --git a/megaavr/libraries/PTC/src/ptc_types.h b/megaavr/libraries/PTC/src/ptc_types.h index 43f2da6b..28cea3b8 100644 --- a/megaavr/libraries/PTC/src/ptc_types.h +++ b/megaavr/libraries/PTC/src/ptc_types.h @@ -200,7 +200,7 @@ typedef struct cap_sensor_type { struct cap_sensor_type* nextNode; ptc_node_type_t type; ptc_id_t id; // number for easier identification in the callback - + ptc_ch_bm_t hw_xCh_bm; ptc_ch_bm_t hw_yCh_bm; uint16_t hw_compCaps; // [13:12] rough; [11:8] course; [7:4] fine; [3:0] accurate From e26da4aff996d203e443c43f405962bb5a0efc39 Mon Sep 17 00:00:00 2001 From: MX682X Date: Tue, 10 Oct 2023 21:41:25 +0200 Subject: [PATCH 3/3] fixes V2 --- megaavr/libraries/PTC/README.md | 16 ++++++++-------- megaavr/libraries/PTC/src/ptc.c | 16 ++++++++-------- megaavr/libraries/PTC/src/ptc.h | 6 +++--- megaavr/libraries/PTC/src/ptc_types.h | 10 +++++----- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/megaavr/libraries/PTC/README.md b/megaavr/libraries/PTC/README.md index 0b760450..5d815336 100644 --- a/megaavr/libraries/PTC/README.md +++ b/megaavr/libraries/PTC/README.md @@ -1,4 +1,4 @@ -# ptc_touch +# PTC Library An Arduino compatible C library to support the PTC in the AtTiny 1-Series ## Features @@ -89,7 +89,7 @@ The ptc_ch_bm_t is a typedef that depends on the pincount of the device and rang ### PTC Operation ![PTC_InnerWorkings](https://github.com/MX682X/ptc_touch/assets/58419867/823c487c-0633-4031-b381-45e7a32867fb) -(Source: [Microchip's PTC Subsystem Firmware User's Guide](https://web.archive.org/web/20221225142000/https://www.mouser.com/pdfdocs/SAMA5D2_PTC_Firmware_UG.pdf)) +(Source: [Microchip's PTC Subsystem Firmware User's Guide]()) This schematic was made for a different chip, but it is likely to look similar on the AVRs. Most of the following is a hypothesis based on the publicly available documentation and observation. @@ -127,9 +127,9 @@ If a node is not sensitive enough, you can increase the Analog Gain (if it becom ### Global settings of the State-maschine The state-machine, which changes the node's state between Calibration, touch, no touch, etc. uses some variables that are valid for all nodes, those are: - - `uint16_t force_recal_delta`. Each node has a threshold value that is used to calculate the delta. This Threshold value is drifting over time to adjust for environmental changes. If the threshold value drifts 512 +/- this value, a recalibration of CC is performed. Default: 150 - - `uint8_t touched_detect_nom`. Number of consecutive Measurements (Conversions) that are above the touch threshold until the node becomes "touched". Default: 3 - - `uint8_t untouched_detect_nom`. Number of consecutive measurements that are below the no-touch threshold until the node is fully untouched. Default: 3 - - `uint8_t touched_max_nom`. Number of consecutive measurements plus one in the touched state until a recalibration is forced. Can be disabled by writing 255 to it. Default: 200 - - `uint8_t drift_up_nom`. If the delta is higher then the reference, but lower then the threshold, the amount of consecutive measurements plus one, until the reference is incremented. Can be disabled with 255. Default: 20. - - `uint8_t drift_down_nom`. If the delta is below the reference, the amount of consecutive measurements plus one until the reference is decremented. Can be disabled with 255. Default: 20. +- `uint16_t force_recal_delta`. Each node has a threshold value that is used to calculate the delta. This Threshold value is drifting over time to adjust for environmental changes. If the threshold value drifts 512 +/- this value, a recalibration of CC is performed. Default: 150 +- `uint8_t touched_detect_nom`. Number of consecutive Measurements (Conversions) that are above the touch threshold until the node becomes "touched". Default: 3 +- `uint8_t untouched_detect_nom`. Number of consecutive measurements that are below the no-touch threshold until the node is fully untouched. Default: 3 +- `uint8_t touched_max_nom`. Number of consecutive measurements plus one in the touched state until a recalibration is forced. Can be disabled by writing 255 to it. Default: 200 +- `uint8_t drift_up_nom`. If the delta is higher then the reference, but lower then the threshold, the amount of consecutive measurements plus one, until the reference is incremented. Can be disabled with 255. Default: 20. +- `uint8_t drift_down_nom`. If the delta is below the reference, the amount of consecutive measurements plus one until the reference is decremented. Can be disabled with 255. Default: 20. diff --git a/megaavr/libraries/PTC/src/ptc.c b/megaavr/libraries/PTC/src/ptc.c index 7ce8e945..25337796 100644 --- a/megaavr/libraries/PTC/src/ptc.c +++ b/megaavr/libraries/PTC/src/ptc.c @@ -43,8 +43,8 @@ volatile cap_sensor_t *currConvNode = NULL; ptc_node_type_t currConvType = 0; // does not "remember" if type was low-power ptc_node_type_t nextConvType = 0; -uint16_t acqPeriod = 20; // Period in ms until a new acquision is started -uint16_t lastAcqTime = 0; // millis value of last Acqusition (16-bit) +uint16_t acqPeriod = 20; // Period in ms until a new acquisition is started +uint16_t lastAcqTime = 0; // millis value of last acquisition (16-bit) ptc_freq_t freq_select = 0; /* FREQ_SEL_0 to FREQ_SEL_15, FREQ_SEL_SPREAD -> CTRLD */ @@ -149,7 +149,7 @@ uint8_t ptc_node_set_resistor(cap_sensor_t* node, uint8_t res) { } -// Change preschaler. Recomended ADC frequency: < 1.5MHz, but max 3 factors below +// Change pre-scaler. Recommended ADC frequency: < 1.5MHz, but max 3 factors below uint8_t ptc_node_set_prescaler(cap_sensor_t* node, uint8_t presc) { PTC_CHECK_FOR_BAD_POINTER(node); @@ -184,7 +184,7 @@ uint8_t ptc_node_set_gain(cap_sensor_t* node, uint8_t aGain, uint8_t dGain) { /* * Two functions to suspend and resume of the normal PTC operation, however, - * this functions will only work when no acqusition is in progress. Neither + * this functions will only work when no acquisition is in progress. Neither * when the library is in low-power mode. This is due to the fact that the * low-power mode relies on the registers being set once, at the low-power * initialization. Thus, the user has to disable LP mode, then suspend, resume @@ -539,8 +539,8 @@ void ptc_process_node_sm (cap_sensor_t* node) { } - } else if (nodeSM & PTC_SM_NO_TOUCH) { /* default State, no touch */ - if (nodeDelta < -150) { /* if the touch value is way too small, something chnaged with the lines, recal */ + } else if (nodeSM & PTC_SM_NO_TOUCH) { /* default State, no touch */ + if (nodeDelta < -150) { /* if the touch value is way too small, something changed with the lines, recalibrate */ nodeSM = PTC_SM_RECAL_FLT; } else if (nodeDelta >= node->touch_in_th) { nodeSM = PTC_SM_TOUCH_IN_FLT; @@ -924,7 +924,7 @@ void ptc_set_registers(cap_sensor_t* node) { pPTC->CTRLA = ADC_RUNSTBY_bm | ADC_ENABLE_bm; /* 0x81 */ if (0 == node->state.low_power) - pPTC->COMMAND = 0x01; // Normal opertion: Manual Start + pPTC->COMMAND = 0x01; // Normal operation: Manual Start else pPTC->EVCTRL = 0x01; // Low Power: Start by positive Flank on Event #elif defined (__PTC_DA__) @@ -934,7 +934,7 @@ void ptc_set_registers(cap_sensor_t* node) { void ptc_eoc(void) { PTC_t *pPTC; - volatile cap_sensor_t *pCurrentNode; // volatile nedded to pass type check + volatile cap_sensor_t *pCurrentNode; // volatile needed to pass type check _fastPtr_d(pPTC,&PTC); _fastPtr_d(pCurrentNode,currConvNode); diff --git a/megaavr/libraries/PTC/src/ptc.h b/megaavr/libraries/PTC/src/ptc.h index 7ff70370..c074f62d 100644 --- a/megaavr/libraries/PTC/src/ptc.h +++ b/megaavr/libraries/PTC/src/ptc.h @@ -62,7 +62,7 @@ extern "C" { // if it's Flash, it will be 0x80, as signed it means negative. // if it's anything else (I/O, EEPROM, etc.), p will be <= 14 // thus making a signed check will make pretty sure the pointer points to a RAM address -// Disadvantage: GCC can not validate this at compiletime as it does not know the variable's +// Disadvantage: GCC can not validate this at compile time as it does not know the variable's // address at compile-time. #define PTC_CHECK_POINTER(__p__, __ret__) \ if (((int8_t)((uint16_t)__p__ >> 8)) < 0x38) { \ @@ -100,7 +100,7 @@ uint8_t ptc_enable_node(cap_sensor_t* node); // Disables a node. If the conversion is started, it will be finished uint8_t ptc_disable_node(cap_sensor_t* node); -// Can be used outside an acqusition process to select ADC/SELFCAP/MUTUAL/SHIELD +// Can be used outside an acquisition process to select ADC/SELFCAP/MUTUAL/SHIELD void ptc_set_next_conversion_type(ptc_node_type_t type); // Main task handle for PTC. Handles State-Machine, drift, and calibration @@ -149,7 +149,7 @@ inline uint8_t ptc_add_mutualcap_node(cap_sensor_t* node, const ptc_ch_bm_t yCh, uint8_t ptc_suspend(void); void ptc_resume(void); -// If you want to know the compensation capacitance in fempto Farrad +// If you want to know the compensation capacitance in femto Farrad uint16_t ptc_get_node_cc_femto(cap_sensor_t* node); ptc_lib_sm_set_t* ptc_get_sm_settings(); diff --git a/megaavr/libraries/PTC/src/ptc_types.h b/megaavr/libraries/PTC/src/ptc_types.h index 28cea3b8..8dcb5797 100644 --- a/megaavr/libraries/PTC/src/ptc_types.h +++ b/megaavr/libraries/PTC/src/ptc_types.h @@ -1,5 +1,5 @@ -/* - * Refer to ptc_touch.h file for copyright, changelog, usage and license information +/* + * Refer to ptc_touch.h file for copyright, changelog, usage and license information */ @@ -55,7 +55,7 @@ extern "C" { /** * PTC series resistor setting. For Mutual cap mode, this series * resistor is switched internally on the Y-pin. For Self cap mode, - * thes series resistor is switched internally on the Sensor pin. + * the series resistor is switched internally on the Sensor pin. * * Example: * RSEL_VAL_0 sets internal series resistor to 0ohms. @@ -221,10 +221,10 @@ typedef struct cap_sensor_type { // Abbreviation: NoM: Number of Measurements typedef struct ptc_lib_sm_settings_type { - uint16_t force_recal_delta; // if the threshold value exceeds this compared to optimal (512), force recalibration. Defualt: 150 + uint16_t force_recal_delta; // if the threshold value exceeds this compared to optimal (512), force recalibration. Default: 150 uint8_t touched_detect_nom; // NoM above node threshold for the node to become touched. Default: 3 uint8_t untouched_detect_nom; // NoM below node threshold for the node to become untouched. Default: 3 - uint8_t touched_max_nom; // NoM a touch was detected until a recal if forced. Value +1. Disabled with 0xFF. Default: 200 + uint8_t touched_max_nom; // NoM a touch was detected until a recalibration is forced. Value +1. Disabled with 0xFF. Default: 200 uint8_t drift_up_nom; // NoM when no touch is detected, until the threshold is increased. Value +1. Disabled with 0xFF. Default: 20 uint8_t drift_down_nom; // NoM when no touch is detected, until the threshold is decreased. Value +1. Disabled with 0xFF. Default: 20 } ptc_lib_sm_set_t;