/** \brief Configures touch library and measurement setup * * @pre Interrupts must be enabled in order to trigger regular measurements * The touch configuration requires a fixed CPU speed to work properly in this * case we need 2 MHz. */ void touch_init(void) { #ifdef QTOUCH_STUDIO_MASKS SNS_array[0][0] = 0x40; SNS_array[0][1] = 0x0; SNS_array[1][0] = 0x0; SNS_array[1][1] = 0x0; SNSK_array[0][0] = 0x80; SNSK_array[0][1] = 0x0; SNSK_array[1][0] = 0x0; SNSK_array[1][1] = 0x0; #endif config_sensor(); qt_init_sensing(); qt_set_parameters(); init_timer_isr(); /* Set up callback function. This function is called after the library * has made capacitive measurements, but before it has processed them. * The user can use this hook to apply filter functions to the measured * signal values.(Possibly to fix sensor layout faults) */ qt_filter_callback = 0; #ifdef _DEBUG_INTERFACE_ QDebug_Init(); #endif }
/*! \brief Example application entry function. */ touch_ret_t touch_sensors_init(void) { touch_ret_t touch_ret = TOUCH_SUCCESS; /* Setup and enable generic clock source for PTC module. */ touch_configure_ptc_clock(); touch_time.measurement_period_ms = DEF_TOUCH_MEASUREMENT_PERIOD_MS; /* Initialize touch library for Mutual Cap operation. */ touch_ret = touch_mutlcap_sensors_init(&touch_config); if (touch_ret != TOUCH_SUCCESS) { while (1u) { /* Check API Error return code. */ } } #if DEF_TOUCH_QDEBUG_ENABLE == 1 QDebug_Init(); #endif /* configure the touch library sensors. */ touch_ret = touch_sensors_config(); if (touch_ret != TOUCH_SUCCESS) { while (1u) { /* Check API Error return code. */ } } touch_ret = touch_mutlcap_sensors_calibrate(); if (touch_ret != TOUCH_SUCCESS) { while (1u) { /* Check API Error return code. */ } } return (touch_ret); }
void app_touch_init(void) { #ifdef QTOUCH_STUDIO_MASKS SNS_array[0][0] = 0x50; SNS_array[0][1] = 0x00; SNS_array[1][0] = 0x00; SNS_array[1][1] = 0x00; SNSK_array[0][0] = 0xA0; SNSK_array[0][1] = 0x00; SNSK_array[1][0] = 0x00; SNSK_array[1][1] = 0x00; #endif /* Configures the sensors as keys and assigns the channel numbers. * The sensor is wired up with SNS=PF6 and SNSK=PF7 * When using "pin reconfigurability" this will result in channel 0 * because it is the first and only channel that is used. * For the standard qtouch library setup we would need to use * channel 3 since we are using the last two pins on the port. */ qt_enable_key(CHANNEL_0, NO_AKS_GROUP, 10u, HYST_6_25); qt_enable_key(CHANNEL_1, NO_AKS_GROUP, 10u, HYST_6_25); qt_init_sensing(); /* This will fill the default threshold values in the configuration * data structure. But User can change the values of these parameters. */ qt_config_data.qt_di = DEF_QT_DI; qt_config_data.qt_neg_drift_rate = DEF_QT_NEG_DRIFT_RATE; qt_config_data.qt_pos_drift_rate = DEF_QT_POS_DRIFT_RATE; qt_config_data.qt_max_on_duration = DEF_QT_MAX_ON_DURATION; qt_config_data.qt_drift_hold_time = DEF_QT_DRIFT_HOLD_TIME; qt_config_data.qt_recal_threshold = DEF_QT_RECAL_THRESHOLD; qt_config_data.qt_pos_recal_delay = DEF_QT_POS_RECAL_DELAY; /* Initialize the timer counter */ tc_enable(&TCC0); tc_write_period(&TCC0, TIMER_PERIOD); tc_write_clock_source(&TCC0, TC_CLKSEL_DIV8_gc); tc_set_cca_interrupt_level(&TCC0, PMIC_LVL_LOW); tc_set_cca_interrupt_callback(&TCC0, app_touch_tc_interrupt_callback); /* * Set up callback function. This function is called after the library * has made capacitive measurements, but before it has processed them. * The user can use this hook to apply filter functions to the measured * signal values.(Possibly to fix sensor layout faults) */ qt_filter_callback = NULL; #ifdef _DEBUG_INTERFACE_ QDebug_Init(); #endif sleepmgr_lock_mode(SLEEPMGR_IDLE); }
/*! \brief Example application entry function. */ touch_ret_t touch_sensors_init(void) { touch_ret_t touch_ret = TOUCH_SUCCESS; /* Setup and enable generic clock source for PTC module. */ touch_configure_ptc_clock(); touch_time.measurement_period_ms = DEF_TOUCH_MEASUREMENT_PERIOD_MS; /* Initialize touch library for Mutual Cap operation. */ touch_ret = touch_mutlcap_sensors_init(&touch_config); if (touch_ret != TOUCH_SUCCESS) { while (1u) { /* Check API Error return code. */ } } #if DEF_TOUCH_QDEBUG_ENABLE == 1 QDebug_Init(); #endif /* configure the touch library sensors. */ touch_ret = touch_sensors_config(); if (touch_ret != TOUCH_SUCCESS) { while (1u) { /* Check API Error return code. */ } } /* Auto Tuning setting for calibration. * * AUTO_TUNE_PRSC: When Auto tuning of pre-scaler is selected * the PTC uses the user defined internal series resistor setting * (DEF_MUTLCAP_SENSE_RESISTOR) and the pre-scaler is adjusted * to slow down the PTC operation to ensure full charge transfer. * * AUTO_TUNE_RSEL: When Auto tuning of the series resistor is * selected the PTC runs at user defined pre-scaler setting speed * (DEF_MUTLCAP_CLK_PRESCALE) and the internal series resistor is * tuned automatically to the optimum value to allow for full * charge transfer. * * AUTO_TUNE_NONE: When manual tuning option is selected (AUTO_TUNE_NONE), * the user defined values of PTC pre-scaler and series resistor is used * for PTC operation as given in DEF_MUTLCAP_CLK_PRESCALE and * DEF_MUTLCAP_SENSE_RESISTOR * */ touch_ret = touch_mutlcap_sensors_calibrate(AUTO_TUNE_RSEL); if (touch_ret != TOUCH_SUCCESS) { while (1u) { /* Check API Error return code. */ } } return (touch_ret); }
/*! \brief Example application entry function. */ touch_ret_t touch_sensors_init (void) { touch_ret_t touch_ret = TOUCH_SUCCESS; touch_qt_time.measurement_period_ms = TOUCH_MEASUREMENT_PERIOD_MS; /* Initialize touch library and sam4l cat module for QTouch Library operation. */ touch_ret = touch_qt_sensors_init (&touch_config); if (touch_ret != TOUCH_SUCCESS) { while (1u); /* Check API Error return code. */ } #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* Initialize the debug interface. */ QDebug_Init (); #endif /* configure the touch library sensors. */ touch_ret = touch_sensors_config (); if (touch_ret != TOUCH_SUCCESS) { while (1u); /* Check API Error return code. */ } /* Initialize touch sensing. */ touch_ret = touch_qt_sensors_calibrate (); if (touch_ret != TOUCH_SUCCESS) { while (1u); /* Check API Error return code. */ } // Set up CATB interrupt handler NVIC_ClearPendingIRQ(CATB_IRQn); NVIC_SetPriority(CATB_IRQn,0); NVIC_EnableIRQ(CATB_IRQn); return (touch_ret); }
void touch_init( void ) { /* Configure the Sensors as keys or Keys With Rotor/Sliders in this function */ config_sensors(); /* initialise touch sensing */ qt_init_sensing(); /* Set the parameters like recalibration threshold, Max_On_Duration etc in this function by the user */ qt_set_parameters( ); /* Address to pass address of user functions */ /* This function is called after the library has made capacitive measurements, * but before it has processed them. The user can use this hook to apply filter * functions to the measured signal values.(Possibly to fix sensor layout faults) */ #ifdef _DEBUG_INTERFACE_ #ifdef _QDEBUG_TIME_STAMPS_ qt_filter_callback = &set_timestamp1; #else qt_filter_callback = 0; #endif #else qt_filter_callback = 0; #endif #ifdef _DEBUG_INTERFACE_ /* Initialize debug protocol */ QDebug_Init(); #endif /* enable interrupts */ __enable_interrupt(); #ifdef _DEBUG_INTERFACE_ /* Process commands from PC */ QDebug_ProcessCommands(); #endif }
int main( void ) { /*status flags to indicate the re-burst for library*/ uint16_t status_flag = 0u; uint16_t burst_flag = 0u; /* initialise host app, pins, watchdog, etc */ init_system(); /* Configure the Sensors as keys or Keys With Rotor/Sliders in this function */ config_sensors(); /* initialise touch sensing */ qt_init_sensing(); /* Set the parameters like recalibration threshold, Max_On_Duration etc in this function by the user */ qt_set_parameters( ); /* configure timer ISR to fire regularly */ init_timer_isr(); #ifdef _DEBUG_INTERFACE_ timestamp1_hword = current_time_ms_touch; timestamp1_lword = (uint16_t)TIMER_COUNTER_L; timestamp1_lword |= (uint16_t)(TIMER_COUNTER_H << 8); #endif /* Address to pass address of user functions */ /* This function is called after the library has made capacitive measurements, * but before it has processed them. The user can use this hook to apply filter * functions to the measured signal values.(Possibly to fix sensor layout faults) */ qt_filter_callback = 0; #ifdef _DEBUG_INTERFACE_ QDebug_Init(); #endif /* enable interrupts */ __enable_interrupt(); #ifdef _DEBUG_INTERFACE_ /* Process commands from PC */ QDebug_ProcessCommands(); #endif /* loop forever */ for( ; ; ) { if( time_to_measure_touch ) { /* clear flag: it's time to measure touch */ time_to_measure_touch = 0u; do { #ifdef _DEBUG_INTERFACE_ timestamp2_hword = current_time_ms_touch; timestamp2_lword = (uint16_t)TIMER_COUNTER_L; timestamp2_lword |= (uint16_t)(TIMER_COUNTER_H << 8); #endif /* one time measure touch sensors */ status_flag = qt_measure_sensors( current_time_ms_touch ); #ifdef _DEBUG_INTERFACE_ timestamp3_hword = current_time_ms_touch; timestamp3_lword = (uint16_t)TIMER_COUNTER_L; timestamp3_lword |= (uint16_t)(TIMER_COUNTER_H << 8); #endif burst_flag = status_flag & QTLIB_BURST_AGAIN; #ifdef _DEBUG_INTERFACE_ /* send debug data */ QDebug_SendData(status_flag); #endif /*Time critical host application code goes here*/ }while ( burst_flag) ; } #ifdef _DEBUG_INTERFACE_ /* Process commands from PC */ QDebug_ProcessCommands(); #endif /* Time Non-critical host application code goes here */ } }
/*! \brief Example application entry function. */ int main (void) { /* BEFORE USING THE EXAMPLE PROJECTS. 1. For support queries on, - QTouch Library usage - Capacitive Touch Sensor Tuning - Capacitive Touch Schematic design - Capacitive Touch Sensor design refer to http://www.atmel.com/design-support/ 2. For more QTouch Library documentation, refer Atmel QTouch Library User Guide doc8207.pdf. For Capacitive Touch Sensor tuning guidelines, refer QTAN0062: QTouch and QMatrix Sensitivity Tuning for Keys, Sliders and Wheels. For Capacitive Touch Sensor design, refer doc10620.pdf: Touch Sensors Design Guide. http://www.atmel.com/dyn/products/app_notes.asp?family_id=697 3. The Example application uses a CPU, PBA and PBB clock of 48MHz. When using a different frequency setting, the following parameters must be changed accordingly to ensure proper QTouch operation. a. QTx_CAT_CLK_DIV. b. TOUCH_SPREAD_SPECTRUM_MAX_DEV, when Spread spectrum is enabled. c. PBA_HZ, when using qdebug/SPI_Master.c d. TARGET_PBA_FREQ_HZ and TARGET_CPU_FREQ_HZ, when using qdebug/SERIAL.c 4. STK600-QTouch Test setup pin information. The following table indicates the STK600 pin connections for the STK600-QTouch test setup. Important Note: The (csa1/csab1) and (csa2/csb2) Touch channel connections are multiplexed with the JTAG pins. So, when using the JTAG debugging mode, these Touch channel connections MUST be removed. In the Flash mode, this will not cause any issues. ---------------------------------------------- CAT CSA/CSB name - STK600 board Port-pin name ---------------------------------------------- ROTOR/WHEEL csa1 - pa1 (This pair is multiplexed with JTAG pins. csb1 - pa6 Remove Touch connections on these pins during JTAG debug mode.) csa2 - pa0 (This pair is multiplexed with JTAG pins. csb2 - pa7 Remove Touch connections on these pins during JTAG debug mode.) csa5 - pb2 csb5 - pb4 SLIDER csa9 - pd0 csb9 - pd1 csa7 - pa4 csb7 - pa5 csa8 - pc0 csb8 - pc1 KEY 1 csa15 - pe4 csb15 - pe1 KEY 2 csa16 - pe3 csb16 - pe2 ---------------------------------------------- QT600 USB Bridge 'TOUCH DATA' Header Pin name - STK600 board Port-pin name ---------------------------------------------- PA22 - 'TOUCH DATA' header pin 8 - clk - pc6 PA21 - 'TOUCH DATA' header pin 7 - miso - pc5 PA20 - 'TOUCH DATA' header pin 6 - mosi - pc4 PA14 - 'TOUCH DATA' header pin 5 - nss - pb6 5. When two or more acquisition methods are used, care must be taken such that a given port pin is not used by more than one method at the same time. The following pin configuration options available in touch_config_at32uc3l.h must be carefully chosen to avoid any overlapping. a. QMatrix Pin Configuration Options. b. Autonomous QTouch Pin Configuration Options. c. QTouch Group A Pin Configuration Options. d. QTouch Group B Pin Configuration Options. e. Touch Sync Pin option. */ touch_ret_t touch_ret = TOUCH_SUCCESS; touch_qt_dma_t qt_dma_ch; /* Initialize host clock, pins, watchdog, etc. */ init_system (); /* Disable interrupts. */ Disable_global_interrupt (); /* The INTC driver has to be used only for GNU GCC for AVR32. */ #if (defined __GNUC__) /* initialize interrupt vectors. */ INTC_init_interrupts (); /* Register the Timer interrupt handler to the interrupt controller. */ INTC_register_interrupt (&tc_irq, EXAMPLE_TC_IRQ, AVR32_INTC_INT0); /* Register the Touch Library CAT interrupt handler to the interrupt controller. Note: This interrupt registration is a MUST before using the Touch Library with the GCC compiler. For the case of IAR the registration of interrupt is automatically taken care by the compiler. The Touch Libary CAT interrupt level for the case of IAR is fixed to Interrupt level 3. */ INTC_register_interrupt (&touch_acq_done_irq, AVR32_CAT_IRQ, AVR32_INTC_INT3); #endif /* Enable interrupts. */ Enable_global_interrupt (); /* Configure timer to fire ISR regularly. */ init_timer (); /* Initialize touch library and uc3l cat module for QTouch Group A operation. */ touch_ret = touch_qt_sensors_init (TOUCH_QT_GRP_A, &touch_config); if (touch_ret != TOUCH_SUCCESS) { while (1u); /* Check API Error return code. */ } #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* Initialize the debug interface. */ QDebug_Init (); #endif /* configure the touch library sensors. */ touch_ret = config_qt_grp_a_touch_sensors (); if (touch_ret != TOUCH_SUCCESS) { while (1u); /* Check API Error return code. */ } /* Initialize touch sensing. */ touch_ret = touch_qt_sensors_calibrate (TOUCH_QT_GRP_A); if (touch_ret != TOUCH_SUCCESS) { while (1u); /* Check API Error return code. */ } /* Provide the dma channel to be used by the CAT module. For each acquisition cycle, any different dma channel from 0 to 11 can be provided. The touch library can handle a different dma channel for each call of the touch_qt_sensors_start_acquisition API. */ qt_dma_ch = QTA_DMA_CHANNEL_0; /* Loop forever */ for (;;) { /* Process touch library events. The touch_event_dispatcher API needs to be called as frequently as possible in order to have a good touch response. */ touch_event_dispatcher (); if (time_to_measure_touch == 1u) { /* Clear flag: it's time to measure touch */ time_to_measure_touch = 0u; /* Start a touch sensors measurement process. */ touch_ret = touch_qt_sensors_start_acquisition (TOUCH_QT_GRP_A, current_time_ms_touch, qt_dma_ch, NORMAL_ACQ_MODE, touch_qta_measure_complete_callback); if ((touch_ret != TOUCH_SUCCESS) && (touch_ret != TOUCH_ACQ_INCOMPLETE)) { while (1); /* Reaching this point can be due to - 1. The api has retured an error due to a invalid input parameter. 2. The api has been called during a invalid Touch Library state. */ } } /* Host application code goes here */ if (qta_measurement_done_touch == 1u) { /* Clear flag: QTouch Group A measurement complete. */ qta_measurement_done_touch = 0u; #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* QT600 two-way QDebug communication application Example. */ /* Process any commands received from QTouch Studio. */ QDebug_ProcessCommands (); /* Send out the Touch debug information data each time when Touch measurement process is completed . */ QDebug_SendData (p_qta_measure_data->acq_status); #endif } } /* Loop forever */ }
/*============================================================================ Name : main ------------------------------------------------------------------------------ Purpose : main code entry point. Input : n/a Output : n/a Notes : ============================================================================*/ int main( void ) { /* BEFORE USING THE EXAMPLE PROJECTS. 1. The Example application uses a CPU, PBA and PBB clock of 48MHz. When using a different frequency setting, the following parameters must be changed accordingly to ensure proper QMatrix operation. a. QM_GCLK_CAT_DIV. b. QM_CAT_CLK_DIV. c. TOUCH_SPREAD_SPECTRUM_MAX_DEV, when Spread spectrum is enabled. d. PBA_HZ, when using QDebug/SPI_Master.c e. TARGET_PBA_FREQ_HZ and TARGET_CPU_FREQ_HZ, when using QDebug/SERIAL.c 2. In the UC3L-Evaluation kit (Rev2), the R42 and R54 (both 470KOhm) resistors MUST be replaced to 910KOhms. 3. The QTouch library uses PDCA channels 0 and 1. (QM_DMA_CHANNEL_0, QM_DMA_CHANNEL_1). Similarly, the QDebug/SERIAL.c uses PDCA channels 2 and 3. (PDCA_CHANNEL_RX_USART, PDCA_CHANNEL_TX_USART) 4. For QMatrix operation, the Analog comparators channels are used (using the ACIFB interface) depending on the Y Lines enabled. For example, when Y lines Y2 and Y7 are enabled the Analog comparator channels 2 and 7 are used by the CAT module for QMatrix operation. The user can uses the rest of the Analog comparator channels in the main application. The QTouch Library enables the ACIFB using the Control register (if not already enabled by the main application) when the touch_qm_sensors_init API is called. 5. When two or more acquisition methods are used, care must be taken such that a given port pin is not used by more than one method at the same time. The following pin configuration options available in touch_config_at32uc3l.h must be carefully chosen to avoid any overlapping. a. QMatrix Pin Configuration Options. b. Autonomous QTouch Pin Configuration Options. c. QTouch Group A Pin Configuration Options. d. QTouch Group B Pin Configuration Options. e. Touch Sync Pin option. */ touch_ret_t touch_ret = TOUCH_SUCCESS; touch_qm_dma_t qm_dma; /* Initialize host clock, pins, watchdog, etc. */ init_system(); /* Disable interrupts. */ Disable_global_interrupt(); /* The INTC driver has to be used only for GNU GCC for AVR32. */ #if (defined __GNUC__) /* initialize interrupt vectors. */ INTC_init_interrupts(); /* Register the Timer interrupt handler to the interrupt controller. */ INTC_register_interrupt(&tc_irq, EXAMPLE_TC_IRQ, AVR32_INTC_INT1); /* Register the Touch Library CAT interrupt handler to the interrupt controller. Note: This interrupt registration is a MUST before using the Touch Library with the GCC compiler. For the case of IAR the registration of interrupt is automatically taken care by the compiler. The Touch Library CAT interrupt level for the case of IAR is fixed to Interrupt level 3. */ INTC_register_interrupt(&touch_acq_done_irq, AVR32_CAT_IRQ, AVR32_INTC_INT3); #endif /* Enable interrupts. */ Enable_global_interrupt(); /* Configure timer to fire ISR regularly. */ init_timer(); /* Initialize touch library and uc3l cat module for QMatrix operation. Note: Set up the GCLK_CAT for proper QMatrix operation. Refer init_system(). */ touch_ret = touch_qm_sensors_init( &touch_config ); if(touch_ret != TOUCH_SUCCESS) { while(1u); /* Check API Error return code. */ } #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* Initialize the debug interface. */ QDebug_Init(); #endif /* configure the touch library sensors. */ touch_ret = config_uc3lek_touch_sensors(); if(touch_ret != TOUCH_SUCCESS) { while(1u); /* Check API Error return code. */ } /* Initialize touch sensing. */ touch_ret = touch_qm_sensors_calibrate(); if(touch_ret != TOUCH_SUCCESS) { while(1u); /* Check API Error return code. */ } /* Provide the dma channels to be used by the CAT module. For each acquisition cycle, any different combination of dma channels from 0 to 11 can be provided. The touch library can also handle a different combination of dma channels for each call of the touch_qm_sensors_start_acquisition API. */ qm_dma.dma_ch1 = QM_DMA_CHANNEL_0; qm_dma.dma_ch2 = QM_DMA_CHANNEL_1; // Initialize the PWMA module demo_init_pwma(); // At the start of the demo, automatically change several times the PWMA duty // cycle (i.e. the intensity) of all LEDs. demo_automatic_ledshow_play(DEMO_INIT_NB_AUTOMATIC_CHANGES); /* Loop forever */ for( ; ; ) { /* Process touch library events. The touch_event_dispatcher API needs to be called as frequently as possible in order to have a good touch response. */ touch_event_dispatcher(); if( time_to_measure_touch == 1u ) { /* Clear flag: it's time to measure touch */ time_to_measure_touch = 0u; /* Start a touch sensors measurement process. */ touch_ret = touch_qm_sensors_start_acquisition( current_time_ms_touch, &qm_dma, NORMAL_ACQ_MODE, touch_qm_measure_complete_callback); if( (touch_ret != TOUCH_SUCCESS) && (touch_ret != TOUCH_ACQ_INCOMPLETE) ) { gpio_clr_gpio_pin(LED0_GPIO); // LED0 gpio_clr_gpio_pin(LED1_GPIO); // LED1 gpio_clr_gpio_pin(LED2_GPIO); // LED2 gpio_clr_gpio_pin(LED3_GPIO); // LED3 do{ delay_ms(50); gpio_tgl_gpio_pin(LED0_GPIO); gpio_tgl_gpio_pin(LED1_GPIO); gpio_tgl_gpio_pin(LED2_GPIO); gpio_tgl_gpio_pin(LED3_GPIO); }while(1); /* Reaching this point can be due to - 1. The api has returned an error due to a invalid input parameter. 2. The api has been called during a invalid Touch Library state. */ } } /* Host application code goes here */ /* Led demo application. */ if(qm_measurement_done_touch == 1u) { #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* UC3L_EK two-way QDebug communication application Example. */ /* Process any commands received from QTouch Studio. */ QDebug_ProcessCommands(); /* Send out the Touch debug information data each time when Touch */ /* measurement process is completed . */ QDebug_SendData(p_qm_measure_data->acq_status); #endif // New touch data measurement are available. process_qtouchlib_data(); /* Clear flag: QMatrix measurement complete. */ qm_measurement_done_touch = 0u; // Once the latest touch data measurements have been processed, clear them. // Here we clear only the measurements that are used by the application. p_qm_measure_data->p_sensor_states[0] = 0; p_qm_measure_data->acq_status = TOUCH_NO_ACTIVITY; p_qm_measure_data->p_rotor_slider_values[0] = 0; } else process_qtouchlib_data(); // Note: we cannot go deeper than the IDLE sleep mode because the QMatrix lib // uses the PDMA. SLEEP(AVR32_PM_SMODE_IDLE); } }
/** * \brief Main Application Code * - Initialize the system clocks * - Initialize the touch functions * - Initialize the timer * - if debug interface is enabled, initialize the qdebug commmands * - When touch status in detect, Set the led & send data to QTouch Studio */ int main (void) { // status flags to indicate the re-burst for library uint16_t status_flag = 0u; uint16_t burst_flag = 0u; /** * \brief Initialize the system clock * Clock settings are done in cof_clock.h * It sets the cpu to run at crystal frequency * which uses OSC0 as souce */ sysclk_init(); //! Initializes the functions necessary for QTouch qtouch_init(); //! Configure the timer ISR to fire regularly for QTouch Acquisition init_timer_isr(); //! Address to pass address of user functions /** * \brief This function is called after the library has made * capacitive measurements, but before it has processed them. * The user can use this hook to apply filter functions to the measured * signal values.(Possibly to fix sensor layout faults) */ qt_filter_callback = 0; #ifdef _DEBUG_INTERFACE_ // Initialize QDebug protocol QDebug_Init(); #endif // Enable global interrupts cpu_irq_enable(); #ifdef _DEBUG_INTERFACE_ // Process commands from PC QDebug_ProcessCommands(); #endif // loop forever for( ; ; ) { if( time_to_measure_touch ) { if( qt_measure_data.qt_touch_status.sensor_states[0]) { gpio_clr_gpio_pin(STATUS_LED); } else { gpio_set_gpio_pin(STATUS_LED); } // clear flag: it's time to measure touch time_to_measure_touch = 0u; do { // one time measure touch sensors status_flag = qt_measure_sensors( current_time_ms_touch ); burst_flag = status_flag & QTLIB_BURST_AGAIN; /*Time critical host application code goes here*/ #ifdef _DEBUG_INTERFACE_ // send debug data QDebug_SendData(status_flag); #endif } while (burst_flag) ; #ifdef _DEBUG_INTERFACE_ // Process commands from PC QDebug_ProcessCommands(); #endif } // Time Non-critical host application code goes here } //end of loop forever } // end of main
/** * \brief Initializes the touch measurement timer. * * We need a timer that triggers approx. every millisecond. * The touch library seems to need this as time-base. */ static void init_timer(void) { volatile avr32_tc_t *tc = TOUCH_MEASUREMENT_TC; const tc_waveform_opt_t waveform_options = { .channel = TOUCH_MEASUREMENT_TC_CHANNEL, //! Software trigger effect on TIOB. .bswtrg = TC_EVT_EFFECT_NOOP, //! External event effect on TIOB. .beevt = TC_EVT_EFFECT_NOOP, //! RC compare effect on TIOB. .bcpc = TC_EVT_EFFECT_NOOP, //! RB compare effect on TIOB. .bcpb = TC_EVT_EFFECT_NOOP, //! Software trigger effect on TIOA. .aswtrg = TC_EVT_EFFECT_NOOP, //! External event effect on TIOA. .aeevt = TC_EVT_EFFECT_NOOP, //! RC compare effect on TIOA: toggle. .acpc = TC_EVT_EFFECT_NOOP, //! RA compare effect on TIOA: toggle. .acpa = TC_EVT_EFFECT_NOOP, //! Waveform selection .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, //! External event trigger enable. .enetrg = 0, //! External event selection. .eevt = 0, //! External event edge selection. .eevtedg = TC_SEL_NO_EDGE, //! Counter disable when RC compare. .cpcdis = 0, //! Counter clock stopped with RC compare. .cpcstop = 0, //! Burst signal selection. .burst = 0, //! Clock inversion selection. .clki = 0, //! Internal source clock 3 (fPBA / 8). .tcclks = TC_CLOCK_SOURCE_TC3 }; const tc_interrupt_t tc_interrupt = { .etrgs = 0, .ldrbs = 0, .ldras = 0, .cpcs = 1, .cpbs = 0, .cpas = 0, .lovrs = 0, .covfs = 0, }; #if (defined __GNUC__) Disable_global_interrupt(); INTC_register_interrupt(&tc_irq, TOUCH_MEASUREMENT_TC_IRQ, AVR32_INTC_INT0); Enable_global_interrupt(); #endif /* initialize the timer/counter. */ sysclk_enable_peripheral_clock(&AVR32_TC1); tc_init_waveform(tc, &waveform_options); /* set the compare triggers. */ tc_write_rc(tc, TOUCH_MEASUREMENT_TC_CHANNEL, (sysclk_get_pba_hz() / 8) / 1000); tc_configure_interrupts(tc, TOUCH_MEASUREMENT_TC_CHANNEL, &tc_interrupt); tc_start(tc, TOUCH_MEASUREMENT_TC_CHANNEL); } /** * \brief Initializes the touch library configuration. * * Sets the correct configuration for the QTouch library. */ static void qt_set_parameters(void) { /* This will be modified by the user to different values. */ qt_config_data.qt_di = DEF_QT_DI; qt_config_data.qt_neg_drift_rate = DEF_QT_NEG_DRIFT_RATE; qt_config_data.qt_pos_drift_rate = DEF_QT_POS_DRIFT_RATE; qt_config_data.qt_max_on_duration = DEF_QT_MAX_ON_DURATION; qt_config_data.qt_drift_hold_time = DEF_QT_DRIFT_HOLD_TIME; qt_config_data.qt_recal_threshold = DEF_QT_RECAL_THRESHOLD; qt_config_data.qt_pos_recal_delay = DEF_QT_POS_RECAL_DELAY; /* * This function is called after the library has made capacitive * measurements, but before it has processed them. The user can use * this hook to apply filter functions to the measured signal * values. (Possibly to fix sensor layout faults). */ qt_filter_callback = NULL; } /** * \brief Initialize touch sensors * * The touch channels are connected on the GPIO controller 4 which is a part of * port X * * Touch Button: * GPIO * 98 SNSK1 CHANNEL1_SNS * 99 SNS1 CHANNEL1_SNSK * * Touch Slider: * 100 SNS0 CHANNEL2_SNS * 101 SNSK0 CHANNEL2_SNSK * 102 SNS1 CHANNEL3_SNS * 103 SNSK1 CHANNEL3_SNSK * 104 SNS2 CHANNEL4_SNS * 105 SNSK2 CHANNEL4_SNSK */ void touch_init(void) { /* * Reset sensing is only needed when doing re-initialization during * run-time */ // qt_reset_sensing(); qt_enable_key(CHANNEL_1, NO_AKS_GROUP, 30u, HYST_12_5); /* * Position hysteresis is not used in QTouch slider so this value will * be ignored */ qt_enable_slider(CHANNEL_2, CHANNEL_4, NO_AKS_GROUP, 20u, HYST_12_5, RES_8_BIT, 0); qt_init_sensing(); qt_set_parameters(); #ifdef _DEBUG_INTERFACE_ sysclk_enable_peripheral_clock(&AVR32_SPI0); /* Initialize debug protocol */ QDebug_Init(); /* Process commands from PC */ QDebug_ProcessCommands(); #endif init_timer(); }
/** * \brief 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 */ int main(void) { #if DEF_TOUCH_QDEBUG_ENABLE == 1 uint32_t delay_counter; #endif uint32_t i; /* Switch on the STATUS LED */ gpio_clr_gpio_pin(STATUS_LED); /* Switch off the error LED. */ gpio_set_gpio_pin(ERROR_LED); /* * Initialize the system clock. * Note: Clock settings are specified in conf_clock.h */ sysclk_init(); /* * Initialize the sleep manager. * Note: CONFIG_SLEEPMGR_ENABLE should have been defined in conf_sleepmgr.h */ sleepmgr_init(); /* Lock required sleep mode. */ sleepmgr_lock_mode(SLEEPMGR_STATIC); /* Initialize the power saving features */ power_save_measures_init(); /* Switch off the error LED. */ gpio_set_gpio_pin(ERROR_LED); #if DEF_TOUCH_QDEBUG_ENABLE == 0 /* Initialize the AST peripheral */ if (ast_init() != STATUS_OK) { /* Error initializing the AST peripheral */ while (1) { for (i = 0; i < 10000; i++) { } gpio_tgl_gpio_pin(ERROR_LED); } } #endif /* Initialize the touch library */ if (touch_api_init() != STATUS_OK) { /* Error initializing the touch sensors */ while (1) { for (i = 0; i < 10000; i++) { } gpio_tgl_gpio_pin(ERROR_LED); } } #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* Enable PBA clock for AST clock to switch its source */ sysclk_enable_peripheral_clock(QDEBUG_USART); /* * Initialize the QDebug interface. * QT600 USB Bridge two-way QDebug communication. */ QDebug_Init(); #endif /* Turn OFF the Status LED */ gpio_set_gpio_pin(STATUS_LED); /* Loop forever */ while (1) { #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* Process any commands received from QTouch Studio. */ QDebug_ProcessCommands(); /* * Send out the Touch debug information data each time when * Touch measurement process is completed. * param - 0x000A -> Enable TOUCH_STATUS_CHANGE & * TOUCH_CHANNEL_REF_CHANGE * qt_lib_flags always for Autonomous QTouch. */ QDebug_SendData(0x000A); /* Delay to avoid sending the data to QTouch Studio too frequently. */ for (delay_counter = 0u; delay_counter < 1200u; delay_counter++) { } #else /* Enable Asynchronous Wakeup for CAT module */ pm_asyn_wake_up_enable(AVR32_PM_AWEN_CATWEN_MASK); /* Disable GPIO clock after waking from sleep mode */ sysclk_disable_pba_module(SYSCLK_GPIO); /* Enter STATIC sleep mode */ sleepmgr_enter_sleep(); /* Disable Asynchronous Wakeup for CAT module. */ pm_asyn_wake_up_disable(AVR32_PM_AWEN_CATWEN_MASK); /* Clear All AST Interrupt request and clear SR */ ast_clear_all_status_flags(&AVR32_AST); /** * When woken up by Autonomous QTouch interrupt, the * touch_at_status_change_interrupt_callback() is called that * updates the autonomous_qtouch_in_touch status flag. */ /* Host application code goes here */ #endif } } /* End of main() */