Microchip® Advanced Software Framework

main.c File Reference

Autonomous QTouch Example with CAT module using SleepWalking feature in AT32UC3L series.

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

#include <stdbool.h>
#include <stdint.h>
#include "compiler.h"
#include "board.h"
#include "conf_example.h"
#include "ast.h"
#include "gpio.h"
#include "pm_uc3l.h"
#include "scif_uc3l.h"
#include "intc.h"
#include "sysclk.h"
#include "sleepmgr.h"
#include "status_codes.h"
#include "touch_api_at32uc3l.h"
#include "QDebugSettings.h"
#include "QDebug_at32uc3l.h"

Macros

APPLICATION SPECIFIC CONFIGURATION
#define STATUS_LED   LED1_GPIO
 LED1_GPIO is used as status LED. More...
 
#define ERROR_LED   LED0_GPIO
 LED0_GPIO is used as error status LED. More...
 
#define POWER_SUPPLY_MODE_1_8V   false
 Enable when operated in 1.8V VDDIN. More...
 
#define AST_CLOCK_SOURCE   AST_OSC_32KHZ
 32KHz external oscillator is used as AST source More...
 
#define AST_PRESCALER   AST_PSEL_32KHZ_1HZ
 Prescaler is set to AST_PSEL_32KHZ_1HZ to run at 1Hz. More...
 
#define AST_TRIGGER_PRESCALER   10
 Periodic interrupt prescaler is set to 10 for trigger in every 62.5ms. More...
 
#define NB_GPIO_USED_PINS   10
 
#define QDEBUG_USART   QDEBUG_SPI_USART
 

Functions

int main (void)
 Main Application Routine. More...
 
static void power_save_measures_init (void)
 Initialize Asynchronous Timer to trigger CAT module at regular intervals. More...
 
static status_code_t touch_api_init (void)
 Initialize the touch library and touch sensors. More...
 
static void touch_at_status_change_interrupt_callback (touch_at_status *p_at_status)
 Autonomous QTouch Group status change callback function example prototype. More...
 

Variables

static touch_at_config_t at_config
 Autonomous QTouch Configuration structure provided as input to Touch Library. More...
 
volatile int8_t autonomous_qtouch_in_touch = -1
 Flag set by touch_at_status_change_interrupt_callback() function when a fresh touch status is available. More...
 
static touch_general_config_t general_config
 General configuration options common to QMatrix, QTouch and Autonomous Touch provided as input to Touch library. More...
 
unsigned int gpio_used_pins [NB_GPIO_USED_PINS]
 Array to hold the used GPIO pins. More...
 
uint16_t measurement_period_ms
 Dummy variable for QDebug protocol for compatibility with QMatrix and QTouch. More...
 
touch_config_t touch_config
 Touch Library input configuration structure. More...
 

#define AST_CLOCK_SOURCE   AST_OSC_32KHZ

32KHz external oscillator is used as AST source

#define AST_PRESCALER   AST_PSEL_32KHZ_1HZ

Prescaler is set to AST_PSEL_32KHZ_1HZ to run at 1Hz.

#define AST_TRIGGER_PRESCALER   10

Periodic interrupt prescaler is set to 10 for trigger in every 62.5ms.

#define ERROR_LED   LED0_GPIO

LED0_GPIO is used as error status LED.

Referenced by main().

#define NB_GPIO_USED_PINS   10
#define POWER_SUPPLY_MODE_1_8V   false

Enable when operated in 1.8V VDDIN.

#define QDEBUG_USART   QDEBUG_SPI_USART

Referenced by main().

#define STATUS_LED   LED1_GPIO

LED1_GPIO is used as status LED.

Referenced by main(), and touch_at_status_change_interrupt_callback().

int main ( void  )

Main Application Routine.

  • Initialize the system clocks
  • Initialize the sleep manager
  • Initialize the power save measures
  • Initialize the touch library and sensors
  • Initialize the AST to trigger CAT at regular intervals
  • Go to STATIC sleep mode and wake up on a touch status change

References ast_clear_all_status_flags(), ERROR_LED, gpio_clr_gpio_pin(), gpio_set_gpio_pin(), gpio_tgl_gpio_pin(), pm_asyn_wake_up_disable(), pm_asyn_wake_up_enable(), power_save_measures_init(), QDebug_Init(), QDebug_ProcessCommands(), QDebug_SendData(), QDEBUG_USART, sleepmgr_enter_sleep(), sleepmgr_init(), sleepmgr_lock_mode(), SLEEPMGR_STATIC, STATUS_LED, STATUS_OK, sysclk_disable_pba_module(), sysclk_enable_peripheral_clock(), SYSCLK_GPIO, sysclk_init(), and touch_api_init().

void power_save_measures_init ( void  )
static

Initialize Asynchronous Timer to trigger CAT module at regular intervals.

Asynchronous Timer Initialization.

Initialize the power saving measures to lower power consumption

  • Start the 32KHz Oscillator
  • Initializes the AST module with periodic trigger events
Return values
STATUS_OKConfiguration OK
ERR_BUSYError in configuring the AST module Low Power Configuration Initializes the power saving measures to reduce power consumption
  • Enable pull ups on GPIO pins
  • Disable the clocks to unwanted modules
  • Disable internal voltage regulator when in 1.8V supply mode

References gpio_configure_group(), GPIO_DIR_INPUT, GPIO_PULL_UP, gpio_used_pins, sysclk_disable_cpu_module(), and SYSCLK_OCD.

Referenced by main().

static status_code_t touch_api_init ( )
static

Initialize the touch library and touch sensors.

Touch Library & Sensors Intialization.

  • Register the CAT interrupt with priority level 3
  • Initialize the touch library
  • Intilialize the Autonomous touch sensor
Return values
STATUS_OKConfiguration success
ERR_INVALID_ARGError in configuration parameters

References cpu_irq_disable, cpu_irq_enable, ERR_INVALID_ARG, irq_initialize_vectors, irq_register_handler, STATUS_OK, SYSCLK_CAT, sysclk_enable_pba_module(), touch_at_sensor_enable(), touch_at_sensor_init(), touch_at_status_change_interrupt_callback(), and TOUCH_SUCCESS.

Referenced by main().

void touch_at_status_change_interrupt_callback ( touch_at_status p_at_status)
static

Autonomous QTouch Group status change callback function example prototype.

Autonomous QTouch status change interrupt callback function.

This callback function is called by the Touch library in the CAT Autonomous QTouch status change Interrupt context, each time there is a status change in the Autonomous Touch sensor.

Parameters
p_at_status,:Autonomous QTouch status. p_at_status->status_change: Autonomous QTouch status change. p_at_status->base_count: Autonomous QTouch base count value. p_at_status->current_count: Autonomous QTouch current count value.
Note
1. CAUTION - This callback function is called in the CAT Autonomous QTouch Status change INTERRUPT SERVICE ROUTINE by the Touch Library.
  1. The Autonomous QTouch Status change callback is called both for an IN_TOUCH status change and an OUT_OF_TOUCH status change.

References autonomous_qtouch_in_touch, gpio_clr_gpio_pin(), gpio_set_gpio_pin(), IN_TOUCH, tag_touch_at_status_t::status_change, STATUS_LED, sysclk_enable_pba_module(), and SYSCLK_GPIO.

Referenced by touch_api_init().

touch_at_config_t at_config
static
Initial value:
= {
{
},
{
{
}
}
}
#define AT_DISHIFT
Autonomous QTouch Discharge Shift.
Definition: touch_config_at32uc3l.h:686
#define AT_PTHR
Autonomous Touch Positive Recalibration Threshold.
Definition: touch_config_at32uc3l.h:771
#define AT_DILEN
Autonomous QTouch Discharge Length.
Definition: touch_config_at32uc3l.h:678
#define AT_ENABLE_EXTERNAL_SYNC
Autonomous QTouch External synchronization to reduce 50 or 60 Hz mains interference.
Definition: touch_config_at32uc3l.h:714
#define AT_NDRIFT
Autonomous Touch Negative Drift Compensation.
Definition: touch_config_at32uc3l.h:793
#define AT_PDRIFT
Autonomous Touch Positive Drift Compensation.
Definition: touch_config_at32uc3l.h:782
#define AT_OUTSENS
Autonomous Touch Out-of-Touch Sensitivity.
Definition: touch_config_at32uc3l.h:744
#define AT_CAT_CLK_DIV
Prescaler to set CAT module Autonomous Touch burst frequency.
Definition: touch_config_at32uc3l.h:651
#define AT_CHLEN
Autonomous QTouch Charge Length For Autonomous QTouch sensor, specifies how many burst prescaler cloc...
Definition: touch_config_at32uc3l.h:660
#define AT_MAX_ACQ_COUNT
Autonomous QTouch Maximum Count.
Definition: touch_config_at32uc3l.h:694
#define AT_SENSE
Autonomous Touch Sensitivity.
Definition: touch_config_at32uc3l.h:762
#define AT_ENABLE_SPREAD_SPECTRUM
Autonomous QTouch Spread Spectrum Sensor Drive.
Definition: touch_config_at32uc3l.h:703
#define AT_SP_SELECTED
CS pin Pair for Autonomous touch.
Definition: touch_config_at32uc3l.h:627
#define AT_SELEN
Autonomous QTouch Settle Length.
Definition: touch_config_at32uc3l.h:669
#define AT_FILTER
Autonomous Touch Filter Setting.
Definition: touch_config_at32uc3l.h:725

Autonomous QTouch Configuration structure provided as input to Touch Library.

Note: Use the touch_config_at32uc3l.h configuration header file to fill in the elements of this structure. DO NOT modify any of the input values directly in this structure.

volatile int8_t autonomous_qtouch_in_touch = -1

Flag set by touch_at_status_change_interrupt_callback() function when a fresh touch status is available.

Referenced by touch_at_status_change_interrupt_callback(), and Transmit_State().

touch_general_config_t general_config
static
Initial value:
= {
}
#define TOUCH_CSBRES
Touch Resistive Drive Enable for CSB lines.
Definition: touch_config_at32uc3l.h:1380
#define TOUCH_SPREAD_SPECTRUM_MAX_DEV
Touch Maximum Deviation.
Definition: touch_config_at32uc3l.h:1356
#define TOUCH_SYNC_PIN_OPTION
Touch Sync Pin option.
Definition: touch_config_at32uc3l.h:1339
#define TOUCH_CSARES
Touch Resistive Drive Enable for CSA lines.
Definition: touch_config_at32uc3l.h:1368

General configuration options common to QMatrix, QTouch and Autonomous Touch provided as input to Touch library.

Note: Use the touch_config_at32uc3l.h configuration header file to fill in the elements of this structure. DO NOT modify any of the input values directly in this structure.

unsigned int gpio_used_pins[NB_GPIO_USED_PINS]
Initial value:
= {
AVR32_SCIF_XIN32_0_PIN,
AVR32_SCIF_XOUT32_0_PIN
}
#define QDEBUG_SPI_MISO_PIN
Definition: QDebugSettings.h:88
#define EXAMPLE_CAT_SNSK_PIN
Definition: conf_example.h:42
#define QDEBUG_SPI_MOSI_PIN
Definition: QDebugSettings.h:90
#define QDEBUG_SPI_SCK_PIN
Definition: QDebugSettings.h:86
#define STATUS_LED
LED1_GPIO is used as status LED.
Definition: main.c:130
#define QDEBUG_SPI_NSS_PIN
Definition: QDebugSettings.h:92
#define EXAMPLE_CAT_SNS_PIN
Definition: conf_example.h:41
#define ERROR_LED
LED0_GPIO is used as error status LED.
Definition: main.c:132

Array to hold the used GPIO pins.

Referenced by power_save_measures_init().

uint16_t measurement_period_ms

Dummy variable for QDebug protocol for compatibility with QMatrix and QTouch.

Referenced by Set_Measurement_Period(), and Transmit_Global_Config().

touch_config_t touch_config
Initial value:
= {
NULL,
NULL,
NULL,
}
static touch_at_config_t at_config
Autonomous QTouch Configuration structure provided as input to Touch Library.
Definition: main.c:212
static touch_general_config_t general_config
General configuration options common to QMatrix, QTouch and Autonomous Touch provided as input to Tou...
Definition: main.c:250

Touch Library input configuration structure.