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"

Macros

#define TOUCH_MEASUREMENT_PERIOD_MS   25
 
#define TOUCH_MEASUREMENT_TC   (&AVR32_TC0)
 
#define TOUCH_MEASUREMENT_TC_CHANNEL   0
 
#define TOUCH_MEASUREMENT_TC_IRQ   AVR32_TC0_IRQ0
 
#define TOUCH_MEASUREMENT_TC_IRQ_GROUP   AVR32_TC0_IRQ_GROUP
 

Functions

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...
 
 ISR (tc_irq, TOUCH_MEASUREMENT_TC_IRQ_GROUP, 1)
 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...
 

Variables

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...
 

#define TOUCH_MEASUREMENT_PERIOD_MS   25
#define TOUCH_MEASUREMENT_TC   (&AVR32_TC0)

Referenced by init_timer(), and ISR().

#define TOUCH_MEASUREMENT_TC_CHANNEL   0

Referenced by init_timer(), and ISR().

#define TOUCH_MEASUREMENT_TC_IRQ   AVR32_TC0_IRQ0

Referenced by init_timer().

#define TOUCH_MEASUREMENT_TC_IRQ_GROUP   AVR32_TC0_IRQ_GROUP

void handle_touch_error ( touch_ret_t  touch_error)
static

handles errors during touch acquisition in the touch library.

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

References print_dbg(), print_dbg_hex(), TOUCH_ACQ_INCOMPLETE, TOUCH_INVALID_AT_CONFIG_PARAM, TOUCH_INVALID_INPUT_PARAM, TOUCH_INVALID_LIB_STATE, and TOUCH_INVALID_QM_CONFIG_PARAM.

Referenced by init_touch(), and touch_handler().

static void init_timer ( void  )
static

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_TC_IRQ_GROUP  ,
 
)

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)
static

Callback for autonomous touch status changes.

Parameters
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)
static

QMatrix measurement complete callback.

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

Parameters
measured_dataPointer to the measured data.

References p_qm_measurement_done.

Referenced by touch_handler().

touch_at_config_t at_config
static
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 = {
.atsp = AT_SP_SELECTED,
},
}
#define AT_CHLEN
Autonomous QTouch Charge Length For Autonomous QTouch sensor, specifies how many burst prescaler cloc...
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:560
#define AT_PTHR
Autonomous Touch Positive Recalibration Threshold.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:665
#define AT_PDRIFT
Autonomous Touch Positive Drift Compensation.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:676
#define AT_DISHIFT
Autonomous QTouch Discharge Shift.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:586
#define AT_SELEN
Autonomous QTouch Settle Length.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:569
#define AT_SENSE
Autonomous Touch Sensitivity.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:657
#define AT_FILTER
Autonomous Touch Filter Setting.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:620
#define AT_ENABLE_EXTERNAL_SYNC
Autonomous QTouch External synchronization to reduce 50 or 60 Hz mains interference.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:610
#define AT_CAT_CLK_DIV
Autonomous QTouch burst timing clock Divider.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:551
#define AT_ENABLE_SPREAD_SPECTRUM
Autonomous QTouch Spread Spectrum Sensor Drive.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:601
#define AT_SP_SELECTED
Autonomous QTouch Clock dependency note: The Example application uses a PBA clock of 48MHz...
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:534
#define AT_NDRIFT
Autonomous Touch Negative Drift Compensation.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:687
#define AT_OUTSENS
Autonomous Touch Out-of-Touch Sensitivity.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:639
#define AT_MAX_ACQ_COUNT
Autonomous QTouch Maximum Count.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:594
#define AT_DILEN
Autonomous QTouch Discharge Length.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:578

Autonomous Touch configuration input.

volatile bool autonomous_qtouch_in_touch = false
static

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.

Referenced by touch_at_status_change_callback(), touch_at_status_change_interrupt_callback(), and Transmit_State().

volatile uint16_t current_time_ms = 0
static

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.

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:
= {
245,
140,
140,
255,
}

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.

Warning
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.

Note
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
static

QMatrix configuration input.

uint8_t qm_data_blk[PRIV_QM_DATA_BLK_SIZE]
static

QMatrix Data block provided as input to the QTouch library.

touch_qm_dma_t qm_dma
static
Initial value:
= {
.dma_ch1 = QM_DMA_CHANNEL_0,
.dma_ch2 = QM_DMA_CHANNEL_1,
}
#define QM_DMA_CHANNEL_1
QMatrix DMA Channel 1.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:414
#define QM_DMA_CHANNEL_0
QMatrix DMA Channel 0.
Definition: avr32/applications/uc3-l0_xplained-demo/touch_config_at32uc3l.h:403

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
static

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
static
Initial value:
= {
.sync_pin = TOUCH_SYNC_PIN_OPTION,
.csares = TOUCH_CSARES,
.csbres = TOUCH_CSBRES,
}
#define TOUCH_SPREAD_SPECTRUM_MAX_DEV
Touch Maximum Deviation.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qt/touch_config_at32uc3l.h:1291
#define TOUCH_CSARES
Touch Resistive Drive Enable for CSA lines.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qt/touch_config_at32uc3l.h:1302
#define TOUCH_CSBRES
Touch Resistive Drive Enable for CSB lines.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qt/touch_config_at32uc3l.h:1313
#define TOUCH_SYNC_PIN_OPTION
Touch Sync Pin option.
Definition: thirdparty/qtouch/devspecific/uc3/uc3l0/examples/example_qt/touch_config_at32uc3l.h:1275

General touch configuration values.

touch_config_t touch_config
static
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.0/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.