atmel avr1632: asf - atmel qtouch with xmega usb hid
TRANSCRIPT
APPLICATION NOTE
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID
8-bit Atmel Microcontrollers
Features
• Single-Chip solution • Atmel® QTouch® and USB HID on Atmel AVR® ATxmega256A3BU
• Using QTouch Library 4.4
• Using Atmel Studio 6
• Simple PC interface with Read/Write functions
• Remote wake up and USB bus powered support
Introduction
This application note describes how to use the QTouch library with the ATxmega256A3BU microcontroller using QTouch button available on XMEGA-A3BU Xplained board. QTouch sensor status is acquired and reported over the USB HID class to a host PC. It provides a single chip solution for sensing QTouch through USB interface.
USB interfaceUSB interface
42013A−AVR−07/12
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
2
Table of Contents
1. Glossary ............................................................................................... 3
2. Overview .............................................................................................. 3
3. Quick start ............................................................................................ 4 3.2 User interface .................................................................................................... 5
4. Building a USB device HID Generic with QTouch................................ 5 4.1 Importing and configuring USB module ............................................................. 5 4.2 QTouch Library Integration ............................................................................... 6
5. Project options ..................................................................................... 7 5.1 Compiler and assembler options ....................................................................... 7 5.2 Linker options .................................................................................................... 9
6. Example firmware ................................................................................ 9 6.1 QTouch interface file – touch.c.......................................................................... 9
6.1.1 QTouch initialization............................................................................ 9 6.1.1.1 config_sensor .................................................................... 9 6.1.1.2 qt_init_sensing ................................................................ 10 6.1.1.3 qt_set_parameters .......................................................... 10 6.1.1.4 init_timer_isr .................................................................... 10
6.1.2 Timer interrupt callback ..................................................................... 10 6.1.3 Check key press ............................................................................... 10
6.2 Example behavior ........................................................................................... 11 6.2.1 ui_process ........................................................................................ 11
7. Host application ................................................................................. 12
8. Recommended reading ...................................................................... 13
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
3
1. Glossary ASF Atmel Software Framework
HID Human Interface Device
UDC USB Device Controller
UDD USB Device Descriptor
UDI USB Device Interface
USB Universal Serial Bus
Atmel Studio 6 Integrated Development Environment (IDE) for Atmel AVR applications
Atmel QTouch Library Software library for touch applications on Atmel AT91SAM and AVR microcontrollers
XMEGA-A3BU Xplained Evaluation Kit for the Atmel ATxmega256A3BU
2. Overview This demo application acquires the change in QTouch key status (ON/OFF), generates the report, and sends it to a host (PC) on request. This application is built over the generic USB HID application which allows simple data exchange between host and the device. The host peridiocally requests for change in QTouch key status. The device will send changed status if available, otherwise it will send a NAK (No Acknowledge) to tell the host that there is no change in the key status. The data packet sent from the device to the host is called report IN.
Figure 2-1. Application overview.
The report sent to the host contains a set of bytes which can be used by the user application to transfer data depending on the requirement. Because this application transfers the status of one button at a time, one bit in a byte is sufficient to transfer the button status. The least significant byte (LSB) of the report byte has the value of the button status. This can be modified depending on the requirement.
UDD
UDC
UDI – HID
USB + QTouch Application
UDI – HID Generic
USB Device stack from the ASF
QTouch Library
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
4
Table 2-1. Push buttons and QTouch button mapping on XMEGA-A3BU Xplained.
Button Pin mapping
Push button SW0 PE5
Push button SW1 PF1
Push button SW2 PF2
QTouch button QTB0 PF6 -> SNS, PF7 -> SNSK
3. Quick start USB device generic HID example with QTouch for XMEGA-A3BU Xplained is available in Atmel Studio 6.
Atmel Studio 6 allows the creation of a New Example Project. In the examples list, select a USB QTouch Device HID Generic Example.
The project does not require any modification and only needs to be built, loaded, and run.
Figure 3-1. New example from Atmel Studio.
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
5
3.2 User interface The user interface consists of switches and LEDs. The resources used from XMEGA-A3BU Xplained board for this example are:
• Two user LEDs – LED2 and LED3
• One QTouch button QTB0
• Three push buttons SW0, SW1 and SW2
• LED0 is ON when USB line is in IDLE mode, and OFF in SUSPEND mode
• LED1 blinks when USB Host has checked and enabled HID generic interface
Figure 3-2. XMEGA – A3BU Xplained.
4. Building a USB device HID Generic with QTouch The USB device HID Generic modules provide a USB HID Generic interface which can be used to build a USB application. These modules are available in Atmel Studio 6 and can be imported in an Atmel Studio 6 Project. To this, QTouch library should be added and configured.
4.1 Importing and configuring USB module The USB device HID Generic modules can be added from the ASF wizard in Atmel Studio 6. Once these modules are added, it is necessary to configure them as per the application needs.
The configuration details are illustrated in Atmel AVR4905: ASF – USB Device HID Generic Application: http://www.atmel.com/Images/doc8499.pdf [4].
Push buttons
User LEDs
LED0
QTouch button
LED1
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
6
4.2 QTouch Library Integration To build the application, the appropriate QTouch header files, assembler files and library files (which are device and compiler specific) need to be integrated with other source files. To select the correct device and the application-specific library files, refer to the Library_Selection_Guide.xls file in the QTouch library [2] available for download on Atmel website. The following should be options should be set for selecting the QTouch library for ATxmega256A3BU.
Table 4-1. Options to be set for selecting QTouch Library for ATxmega256A3BU.
Option Value
Technology QTouch
MCU Family Atmel AVR XMEGA®
MCU Type 8-bit
MCU ATxmega256A3BU
Ports available for QTouch A, B, C, D, E, F
Maximum number of channels 4
Maximum number of rotors 0
Toolchain GCC
By selecting these parameters, the appropriate library files to be included can be seen.
• libavrxmega6g1-4qt-k-0rs.a – GCC library file for the respective device
• qt_asm_xmega.S – assembler file
• touch_qt_config.h
• touch_api.h
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
7
Figure 4-1. Library Selection Guide.
5. Project options
5.1 Compiler and assembler options For the QTouch application, certain options should be set in the compiler and assembler. In GCC, open Project Options -> Toolchain -> AVR/GNU C Compiler -> Symbols. Add the following to Defined symbols one by one in Atmel Studio 6 (Figure 5-1).
_QTOUCH_ SNS1=F SNSK1=F _SNS1_SNSK1_SAME_PORT_ QT_DELAY_CYCLES=1 QT_NUM_CHANNELS=4 QTOUCH_STUDIO_MASKS=1 NUMBER_OF_PORTS=1
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
8
Figure 5-1. GCC compiler options.
Add Symbols
The above options have to be set in the assembler as well, as shown in Figure 5-2.
Figure 5-2. GCC assembler options.
Add here
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
9
5.2 Linker options In GCC, open Project Options -> Toolchain -> AVR/GNU C Linker -> Libraries. Add the following library (Figure 5-3)
avrxmega6g1-4qt-k-0rs
Also specify the path where the library is located.
Figure 5-3. Linker options.
Add library
Add library path
6. Example firmware This document explains only the QTouch interface file and example behavior. Description about USB Device HID Generic is available in Atmel AVR4905: ASF – USB Device HID Generic Application: http://www.atmel.com/Images/doc8499.pdf.
6.1 QTouch interface file – touch.c
6.1.1 QTouch initialization The touch_init function is responsible for calling the initialization routines related to QTouch sensing.
config_sensor(); qt_init_sensing(); qt_set_parameters(); init_timer_isr();
Each of the functions are described in the following sections.
6.1.1.1 config_sensor The config_sensor function is responsible for configuring the sensors as keys and assigning the channel numbers.
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
10
qt_enable_key(CHANNEL_0, NO_AKS_GROUP, 10, HYST_6_25);
channel_t – channel number to be configured for a key
aks_group_t – Adjacent Key Suppression® (AKS®) group associated with the sensor being configured as “key”
threshold_t – Detect threshold for the sensor
hysteresis_t – Detection hysteresis for the sensor
Refer to the Atmel AVR QTouch Library User Guide: http://www.atmel.com/dyn/resources/prod_documents/doc8207.pdf for more information on this function.
6.1.1.2 qt_init_sensing The qt_init_sensing function is used to initialize the QTouch sensing for all enabled channels. All required sensors should be configured before calling this function.
#define qt_init_sensing( ) qt_init_sensing_with_burst( BURST_FUNC_NAME ,CALCULATE_MASKS)
6.1.1.3 qt_set_parameters The qt_set_parameters function is used to initialize the global configuration settings in the variable qt_config_data of the QTouch acquisition method libraries. In other words, this will fill the default threshold values in the configuration data structure.
6.1.1.4 init_timer_isr The example uses TC driver from ASF. It enables the timer, sets the timer period, clock source, interrupt level and interrupt callback functions. The timer period defines the time interval between consecutive QTouch measurements.
6.1.2 Timer interrupt callback Sets the flag time_to_measure_touch and updates the current time that is used by the QTouch Library.
6.1.3 Check key press The check_touch_key_pressed function in the QTouch interface file (touch.c) from example is responsible for measuring the QTouch sensing and reporting the key status.
uint16_t status_flag;
uint16_t burst_flag;
#ifdef _DEBUG_INTERFACE_
/* Process commands from PC */
QDebug_ProcessCommands();
#endif
if (time_to_measure_touch)
{
time_to_measure_touch = false;
do {
status_flag = qt_measure_sensors( current_time_ms_touch );
burst_flag = status_flag & QTLIB_BURST_AGAIN;
#ifdef _DEBUG_INTERFACE_
QDebug_SendData(status_flag);
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
11
#endif
/* Time-critical host application code should be placed here since
* a full burst cycle may delay critical task in the main application
*/
}while (burst_flag) ;
#ifdef _DEBUG_INTERFACE_
/* Process commands from PC */
QDebug_ProcessCommands();
#endif
}
if(GET_SENSOR_STATE(0))
return 0;
else
return 1;
6.2 Example behavior
6.2.1 ui_process The main.c and ui.c implement the user interface for Atmel QTouch demonstration with Atmel XMEGA USB HID application.
The implementation is comprised of three steps: 1. Start the USB Device
udc_start();
udc_attach(); // Must be called when the USB cable is plugged
// Cable plugged is detected via VBUS events
2. Wait for the HID Generic interface to be enabled via a callback UDI_HID_GENERIC_ENABLE_EXT() // Authorize the Generic HID events
3. Scan the buttons and send the status b_btn_state = (!check_touch_key_pressed()) ? true : false;
if (b_btn_state != btnq_last_state) {
ui_hid_report[0]=b_btn_state;
udi_hid_generic_send_report_in(ui_hid_report);
btnq_last_state = b_btn_state;
}
Note: To simplify the USB implementation, the SOF event is used to scan switches every 1ms.
More description on how Generic HID example works is available in the application note Atmel AVR4905: ASF – USB Device HID Generic Application: http://www.atmel.com/Images/doc8499.pdf.
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
12
7. Host application Atmel PC tool allows communicating with the generic HID device. The PC tool is available with Application Note AVR153: USB PC Drivers Based on Generic HID Class: http://www.atmel.com/dyn/resources/prod_documents/doc7645.pdf AVR153 – USB PC Drivers Based on Generic HID Class as AVR153.zip [6]. Whenever there is a key press and release, it updates in the display of PC Tool. The key press or release is displayed as shown in Figure 7-1.
Use the LED2 ON/OFF and LED3 ON/OFF buttons to turn ON and OFF the LED2 and 3. Push and release the buttons SW0, SW1, SW2 and QTouch button QTB0 to see the status displayed on the PC application window.
Information on integrating the Atmel USB HID DLL functions and simple code examples that demonstrate different types of implementation are available in the document “USB PC Drivers Based on Generic HID Class” [6].
Figure 7-1. Ket status.
Note: Use the PID 0x2402 in the graphic tool interface.
Caution: The button ‘Firmware Upgrade’ only disconnects the USB device.
Atmel AVR1632: ASF – Atmel QTouch with XMEGA USB HID [APPLICATION NOTE] 42013A−AVR−07/12
13
8. Recommended reading [1] Atmel XMEGA AU Manual: http://atmel.com/dyn/resources/prod_documents/doc8331.pdf [2] Atmel QTouch Library 4.4: http://www.atmel.com/tools/QTOUCHSTUDIO4_3_1.aspx [3] Atmel AVR QTouch Library User
Guide: http://www.atmel.com/dyn/resources/prod_documents/doc8207.pdf [4] Atmel AVR4905: ASF – USB Device HID Generic Application: http://www.atmel.com/Images/doc8499.pdf [5] ASF Documentation: http://asf.atmel.com/docs/latest/ [6] AVR153: USB PC Drivers Based on Generic HID
Class: http://www.atmel.com/dyn/resources/prod_documents/doc7645.pdf [7] AVR1923: XMEGA-A3BU Xplained Hardware User
Guide: http://atmel.com/dyn/resources/prod_documents/doc8394.pdf [8] AVR1934: XMEGA A3BU Xplained Software User
Guide: http://atmel.com/dyn/resources/prod_documents/doc8413.pdf [9] AVR1935: XMEGA A3BU Xplained Getting Started
Guide: http://atmel.com/dyn/resources/prod_documents/doc8324.pdf
Atmel Corporation 1600 Technology Drive San Jose, CA 95110 USA Tel: (+1)(408) 441-0311 Fax: (+1)(408) 487-2600 www.atmel.com
Atmel Asia Limited Unit 01-5 & 16, 19F BEA Tower, Millennium City 5 418 Kwun Tong Road Kwun Tong, Kowloon HONG KONG Tel: (+852) 2245-6100 Fax: (+852) 2722-1369
Atmel Munich GmbHBusiness Campus Parkring 4 D-85748 Garching b. Munich GERMANY Tel: (+49) 89-31970-0 Fax: (+49) 89-3194621
Atmel Japan G.K.16F Shin-Osaki Kangyo Bldg. 1-6-4 Osaki, Shinagawa-ku Tokyo 141-0032 JAPAN Tel: (+81)(3) 6417-0300 Fax: (+81)(3) 6417-0370
© 2012 Atmel Corporation. All rights reserved. / Rev.: 42013A−AVR−07/12
Atmel®, Atmel logo and combinations thereof, AVR®, Adjacent Key Suppression®, AKS®, Enabling Unlimited Possibilities®, QTouch®, XMEGA®, and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.
Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.