Skip to content

Commit

Permalink
fixing of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MX682X committed Oct 10, 2023
1 parent b1d165b commit a1c42da
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 134 deletions.
26 changes: 13 additions & 13 deletions megaavr/libraries/PTC/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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`:
Expand Down Expand Up @@ -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:
Expand All @@ -104,32 +104,32 @@ 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.

### 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 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.
4 changes: 2 additions & 2 deletions megaavr/libraries/PTC/examples/mutualcap/mutualcap.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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));


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

#include <ptc.h>
/*
* 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
Expand All @@ -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));

Expand Down
6 changes: 3 additions & 3 deletions megaavr/libraries/PTC/examples/selfcap/selfcap.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);

Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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));

Expand Down
Loading

0 comments on commit a1c42da

Please sign in to comment.