Microchip® Advanced Software Framework

touch.c File Reference

UC3-L0 Xplained demo touch handler.

Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.

#include "compiler.h"
#include "board.h"
#include "gpio.h"
#include "sysclk.h"
#include "scif_uc3l.h"
#include "tc.h"
#include "pwma.h"
#include "print_funcs.h"
#include "touch_config_at32uc3l.h"
#include "touch_api_at32uc3l.h"
#include "uc3-l0_xplained_demo.h"
#include "QDebug_at32uc3l.h"




static void handle_touch_error (touch_ret_t touch_error)
 handles errors during touch acquisition in the touch library. More...
static void init_timer (void)
 Initializes the touch measurement timer. More...
void init_touch (void)
 Initializes the touch library ready for acquisition of touch data. More...
 Touch measurement timer interrupt. More...
static void touch_at_status_change_callback (touch_at_status *at_status)
 Callback for autonomous touch status changes. More...
void touch_handler (void)
static void touch_qm_measurement_complete_callback (touch_measure_data_t *measured_data)
 QMatrix measurement complete callback. More...


static volatile bool autonomous_qtouch_in_touch = false
 Touch detection flag for autonomous touch. More...
static volatile uint16_t current_time_ms = 0
 Current time in ms. More...
uint16_t measurement_period_ms = TOUCH_MEASUREMENT_PERIOD_MS
 Time measurement period in ms. More...
touch_measure_data_tp_qm_measure_data = NULL
 Pointer to the measured data. More...
bool p_qm_measurement_done = false
 Touch measurement complete flag. More...
static led_pwm_channel_t rgb_led = PWM_CHANNEL_RED
 Currently selected RGB LED to manipulate. More...
static volatile bool time_to_measure_touch = false
 Touch measurement flag. More...
uint8_t qm_burst_length [QM_NUM_CHANNELS]
 The below data is required for the QTouch library. More...
static uint8_t qm_data_blk [PRIV_QM_DATA_BLK_SIZE]
 QMatrix Data block provided as input to the QTouch library. More...
static touch_qm_config_t qm_config
 QMatrix configuration input. More...
static touch_at_config_t at_config
 Autonomous Touch configuration input. More...
static touch_general_config_t touch_common_config
 General touch configuration values. More...
static touch_config_t touch_config
 Main touch library configuration options. More...
static touch_qm_dma_t qm_dma
 Touch DMA configuration options. More...


Referenced by init_timer(), and ISR().


Referenced by init_timer(), and ISR().


Referenced by init_timer().


void handle_touch_error ( touch_ret_t  touch_error)

handles errors during touch acquisition in the touch library.

Write debug messages to the debug USART to indicate the problem that occurred.


Referenced by init_touch(), and touch_handler().

static void init_timer ( void  )

Initializes the touch measurement timer.

We need a timer that triggers approx. every millisecond. The touch library needs this as time-base.

Software trigger effect on TIOB.

External event effect on TIOB.

RC compare effect on TIOB.

RB compare effect on TIOB.

Software trigger effect on TIOA.

External event effect on TIOA.

RC compare effect on TIOA.

RA compare effect on TIOA.

Waveform selection

External event trigger enable.

External event selection.

External event edge selection.

Counter disable when RC compare.

Counter clock stopped with RC compare.

Burst signal selection.

Clock inversion selection.

Internal source clock 3 (fPBA / 8).

References tc_waveform_opt_t::channel, Disable_global_interrupt, Enable_global_interrupt, tc_interrupt_t::etrgs, INTC_register_interrupt(), sysclk_get_pba_hz(), TC_CLOCK_SOURCE_TC3, tc_configure_interrupts(), TC_EVT_EFFECT_NOOP, tc_init_waveform(), tc_irq(), TC_SEL_NO_EDGE, tc_start(), TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, tc_write_rc(), TOUCH_MEASUREMENT_TC, TOUCH_MEASUREMENT_TC_CHANNEL, and TOUCH_MEASUREMENT_TC_IRQ.

Referenced by init_touch().

ISR ( tc_irq  ,

Touch measurement timer interrupt.

Initiates periodically touch measurements. Triggers every 1ms and will set the flag time_to_measure_touch every TOUCH_MEASUREMENT_PERIOD_MS.

References current_time_ms, measurement_period_ms, tc_read_sr(), time_to_measure_touch, TOUCH_MEASUREMENT_TC, and TOUCH_MEASUREMENT_TC_CHANNEL.

static void touch_at_status_change_callback ( touch_at_status at_status)

Callback for autonomous touch status changes.

at_statusPointer to autonomous touch status data.

References autonomous_qtouch_in_touch, IN_TOUCH, PWM_CHANNEL_BLUE, PWM_CHANNEL_GREEN, PWM_CHANNEL_RED, rgb_led, and tag_touch_at_status_t::status_change.

Referenced by init_touch().

static void touch_qm_measurement_complete_callback ( touch_measure_data_t measured_data)

QMatrix measurement complete callback.

This function will be called from the touch library each time a measurement is completed.

measured_dataPointer to the measured data.

References p_qm_measurement_done.

Referenced by touch_handler().

touch_at_config_t at_config
Initial value:
= {
.reg = {
.chlen = AT_CHLEN,
.selen = AT_SELEN,
.dishift = AT_DISHIFT,
.dilen = AT_DILEN,
.at_param = {
.filter = AT_FILTER,
.outsens = AT_OUTSENS,
.sense = AT_SENSE,
.pthr = AT_PTHR,
.pdrift = AT_PDRIFT,
.ndrift = AT_NDRIFT,
.pin = {
#define AT_CAT_CLK_DIV
Autonomous QTouch burst timing clock Divider.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:651
Autonomous QTouch External synchronization to reduce 50 or 60 Hz mains interference.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:714
Autonomous QTouch Clock dependency note: The Example application uses a PBA clock of 48MHz...
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:627
#define AT_OUTSENS
Autonomous Touch Out-of-Touch Sensitivity.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:744
#define AT_PDRIFT
Autonomous Touch Positive Drift Compensation.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:782
#define AT_SENSE
Autonomous Touch Sensitivity.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:762
Autonomous QTouch Maximum Count.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:694
#define AT_SELEN
Autonomous QTouch Settle Length.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:669
Autonomous QTouch Spread Spectrum Sensor Drive.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:703
#define AT_DILEN
Autonomous QTouch Discharge Length.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:678
#define AT_PTHR
Autonomous Touch Positive Recalibration Threshold.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:771
#define AT_DISHIFT
Autonomous QTouch Discharge Shift.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:686
#define AT_CHLEN
Autonomous QTouch Charge Length For Autonomous QTouch sensor, specifies how many burst prescaler cloc...
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:660
#define AT_NDRIFT
Autonomous Touch Negative Drift Compensation.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:793
#define AT_FILTER
Autonomous Touch Filter Setting.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_at/touch_config_at32uc3l.h:725

Autonomous Touch configuration input.

volatile bool autonomous_qtouch_in_touch = false

Touch detection flag for autonomous touch.

This flag will be set in the touch library callback function for the autonomous touch when a touch change was detected. This must be volatile since the callback will be called from an ISR.

volatile uint16_t current_time_ms = 0

Current time in ms.

This is required for the touch library. The time is updated in the timer ISR so this must be volatile.

Referenced by ISR(), and touch_handler().

uint16_t measurement_period_ms = TOUCH_MEASUREMENT_PERIOD_MS

Time measurement period in ms.

Touch Measurement period in milliseconds.

Needs to be global since it is required for the QDebug module.

touch_measure_data_t* p_qm_measure_data = NULL

Pointer to the measured data.

QMatrix measured data pointer.

Needs to be global since it is required for the QDebug module.

bool p_qm_measurement_done = false

Touch measurement complete flag.

This flag is set by the touch library callback function once a touch measurement is completed.

Referenced by touch_handler(), and touch_qm_measurement_complete_callback().

uint8_t qm_burst_length[QM_NUM_CHANNELS]
Initial value:
= {

The below data is required for the QTouch library.

Do not alter these here since they are usually configured via the touch_config_at32uc3l.hQMatrix burst length.

Specify the number of QMatrix burst pulses for each channel. In order to disable bursting on a individual channel, a value of 1u has to be specified.

QMatrix Burst length setting recommendation. When the burst length value corresponding to (X0,Y1),(X0,Y2)...(Xn,Yn) are different, 1k ohm resistive drive MUST be used on all the enabled Y lines by setting the corresponding bits in the CSARES register.

When the burst length value corresponding to (X0,Y1),(X0,Y2)...(Xn,Yn) are same or set to 0x01(disabled), enabling the 1k ohm resistive drive on Y lines is optional.

A value of 0u will be same as specifying a burst length value of 255u pulses. Range: 1u to 255u.
Once the Touch Library has been initialized for QMatrix method using the touch_qm_sensors_init API, a new qm_burst_length[x] value of a Touch channel MUST be updated only using the touch_qm_channel_update_burstlen API. It is recommended to have qm_burst_length array as global variable as the Touch Library updates this array when the touch_qm_channel_update_burstlen API is called.
touch_qm_config_t qm_config

QMatrix configuration input.

uint8_t qm_data_blk[PRIV_QM_DATA_BLK_SIZE]

QMatrix Data block provided as input to the QTouch library.

touch_qm_dma_t qm_dma
Initial value:
= {
.dma_ch1 = QM_DMA_CHANNEL_0,
.dma_ch2 = QM_DMA_CHANNEL_1,
#define QM_DMA_CHANNEL_0
QMatrix DMA Channel 0.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qm/touch_config_at32uc3l.h:456
#define QM_DMA_CHANNEL_1
QMatrix DMA Channel 1.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qm/touch_config_at32uc3l.h:467

Touch DMA configuration options.

Referenced by main().

Currently selected RGB LED to manipulate.

This enum indicates the currently selected RGB, whose brightness will be altered by a touch of the board's slider control.

Referenced by touch_at_status_change_callback(), and touch_handler().

volatile bool time_to_measure_touch = false

Touch measurement flag.

Indicates when a new touch measurement should be done. This is set in the timer ISR so this must be volatile.

touch_general_config_t touch_common_config
Initial value:
= {
.csares = TOUCH_CSARES,
.csbres = TOUCH_CSBRES,
Touch Maximum Deviation.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qm/touch_config_at32uc3l.h:1291
Touch Resistive Drive Enable for CSB lines.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qm/touch_config_at32uc3l.h:1313
Touch Sync Pin option.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qm/touch_config_at32uc3l.h:1275
Touch Resistive Drive Enable for CSA lines.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qm/touch_config_at32uc3l.h:1302

General touch configuration values.

touch_config_t touch_config
Initial value:
= {
.p_qm_config = &qm_config,
.p_at_config = &at_config,
.p_qta_config = NULL,
.p_qtb_config = NULL,
.p_general_config = &touch_common_config,
#define NULL
Definition: lwip-1.4.1/src/include/lwip/def.h:47
static touch_general_config_t touch_common_config
General touch configuration values.
Definition: touch.c:243
static touch_at_config_t at_config
Autonomous Touch configuration input.
Definition: touch.c:218
static touch_qm_config_t qm_config
QMatrix configuration input.
Definition: touch.c:160

Main touch library configuration options.