1*1b2596b5SMatthias Ringwald /** 2*1b2596b5SMatthias Ringwald * \file 3*1b2596b5SMatthias Ringwald * 4*1b2596b5SMatthias Ringwald * \brief SAM Timer Counter (TC) driver. 5*1b2596b5SMatthias Ringwald * 6*1b2596b5SMatthias Ringwald * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved. 7*1b2596b5SMatthias Ringwald * 8*1b2596b5SMatthias Ringwald * \asf_license_start 9*1b2596b5SMatthias Ringwald * 10*1b2596b5SMatthias Ringwald * \page License 11*1b2596b5SMatthias Ringwald * 12*1b2596b5SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 13*1b2596b5SMatthias Ringwald * modification, are permitted provided that the following conditions are met: 14*1b2596b5SMatthias Ringwald * 15*1b2596b5SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright notice, 16*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer. 17*1b2596b5SMatthias Ringwald * 18*1b2596b5SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright notice, 19*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer in the documentation 20*1b2596b5SMatthias Ringwald * and/or other materials provided with the distribution. 21*1b2596b5SMatthias Ringwald * 22*1b2596b5SMatthias Ringwald * 3. The name of Atmel may not be used to endorse or promote products derived 23*1b2596b5SMatthias Ringwald * from this software without specific prior written permission. 24*1b2596b5SMatthias Ringwald * 25*1b2596b5SMatthias Ringwald * 4. This software may only be redistributed and used in connection with an 26*1b2596b5SMatthias Ringwald * Atmel microcontroller product. 27*1b2596b5SMatthias Ringwald * 28*1b2596b5SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 29*1b2596b5SMatthias Ringwald * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 30*1b2596b5SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 31*1b2596b5SMatthias Ringwald * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 32*1b2596b5SMatthias Ringwald * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33*1b2596b5SMatthias Ringwald * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34*1b2596b5SMatthias Ringwald * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35*1b2596b5SMatthias Ringwald * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 36*1b2596b5SMatthias Ringwald * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 37*1b2596b5SMatthias Ringwald * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38*1b2596b5SMatthias Ringwald * POSSIBILITY OF SUCH DAMAGE. 39*1b2596b5SMatthias Ringwald * 40*1b2596b5SMatthias Ringwald * \asf_license_stop 41*1b2596b5SMatthias Ringwald * 42*1b2596b5SMatthias Ringwald */ 43*1b2596b5SMatthias Ringwald /* 44*1b2596b5SMatthias Ringwald * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> 45*1b2596b5SMatthias Ringwald */ 46*1b2596b5SMatthias Ringwald 47*1b2596b5SMatthias Ringwald #ifndef TC_H_INCLUDED 48*1b2596b5SMatthias Ringwald #define TC_H_INCLUDED 49*1b2596b5SMatthias Ringwald 50*1b2596b5SMatthias Ringwald /** 51*1b2596b5SMatthias Ringwald * \defgroup asfdoc_sam_drivers_tc_group SAM3/4S/4L/4E/4N/4CM/4C/G Timer Counter (TC) Driver 52*1b2596b5SMatthias Ringwald * 53*1b2596b5SMatthias Ringwald * This driver for Atmel® | SMART ARM®-based microcontrollers 54*1b2596b5SMatthias Ringwald * provides an interface for the configuration and management of the 55*1b2596b5SMatthias Ringwald * device's Timer Counter functionality. 56*1b2596b5SMatthias Ringwald * 57*1b2596b5SMatthias Ringwald * The Timer Counter (TC) includes several identical 16-bit or 32-bit 58*1b2596b5SMatthias Ringwald * Timer Counter channels. Each channel can be independently programmed 59*1b2596b5SMatthias Ringwald * to perform a wide range of functions that includes frequency 60*1b2596b5SMatthias Ringwald * measurement, event counting, interval measurement, pulse generation, 61*1b2596b5SMatthias Ringwald * delay timing, and pulse width modulation. 62*1b2596b5SMatthias Ringwald * 63*1b2596b5SMatthias Ringwald * Devices from the following series can use this module: 64*1b2596b5SMatthias Ringwald * - Atmel | SMART SAM3 65*1b2596b5SMatthias Ringwald * - Atmel | SMART SAM4S 66*1b2596b5SMatthias Ringwald * - Atmel | SMART SAM4L 67*1b2596b5SMatthias Ringwald * - Atmel | SMART SAM4E 68*1b2596b5SMatthias Ringwald * - Atmel | SMART SAM4N 69*1b2596b5SMatthias Ringwald * - Atmel | SMART SAM4CM 70*1b2596b5SMatthias Ringwald * - Atmel | SMART SAM4C 71*1b2596b5SMatthias Ringwald * - Atmel | SMART SAMG 72*1b2596b5SMatthias Ringwald * 73*1b2596b5SMatthias Ringwald * The outline of this documentation is as follows: 74*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_prerequisites 75*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_module_overview 76*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_special_considerations 77*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_extra_info 78*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_examples 79*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_api_overview 80*1b2596b5SMatthias Ringwald * 81*1b2596b5SMatthias Ringwald * 82*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_prerequisites Prerequisites 83*1b2596b5SMatthias Ringwald * 84*1b2596b5SMatthias Ringwald * There are no prerequisites for this module. 85*1b2596b5SMatthias Ringwald * 86*1b2596b5SMatthias Ringwald * 87*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_module_overview Module Overview 88*1b2596b5SMatthias Ringwald * The Timer Counter (TC) includes several identical 16-bit or 32-bit Timer 89*1b2596b5SMatthias Ringwald * Counter channels. The number of TC channels is device specific, refer 90*1b2596b5SMatthias Ringwald * to the device-specific datasheet for more information. 91*1b2596b5SMatthias Ringwald * 92*1b2596b5SMatthias Ringwald * Each channel can be independently programmed to perform a wide range of 93*1b2596b5SMatthias Ringwald * functions that includes frequency measurement, event counting, interval measurement, 94*1b2596b5SMatthias Ringwald * pulse generation, delay timing, and pulse width modulation. 95*1b2596b5SMatthias Ringwald * 96*1b2596b5SMatthias Ringwald * Each channel has three external clock inputs, five internal clock inputs, 97*1b2596b5SMatthias Ringwald * and two multi-purpose input/output signals which can be configured by the user. 98*1b2596b5SMatthias Ringwald * Each channel drives an internal interrupt signal which can be programmed to 99*1b2596b5SMatthias Ringwald * generate processor interrupts. 100*1b2596b5SMatthias Ringwald * 101*1b2596b5SMatthias Ringwald * The Timer Counter (TC) embeds a quadrature decoder logic connected in front of 102*1b2596b5SMatthias Ringwald * the timers. When enabled, the quadrature decoder performs the input line 103*1b2596b5SMatthias Ringwald * filtering, decoding of quadrature signals and connects to the timers/counters 104*1b2596b5SMatthias Ringwald * in order to read the position and speed of the motor. 105*1b2596b5SMatthias Ringwald * 106*1b2596b5SMatthias Ringwald * 107*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_special_considerations Special Considerations 108*1b2596b5SMatthias Ringwald * \subsection asfdoc_sam_drivers_tc_special_considerations_clock External Clock 109*1b2596b5SMatthias Ringwald * In all cases, if an external clock is used, the duration of each of its levels 110*1b2596b5SMatthias Ringwald * must be longer than the master clock (MCLK) period. The external clock frequency 111*1b2596b5SMatthias Ringwald * must be at least 2.5 times lower than the master clock. 112*1b2596b5SMatthias Ringwald * 113*1b2596b5SMatthias Ringwald * \subsection asfdoc_sam_drivers_tc_special_considerations_trigger External Trigger 114*1b2596b5SMatthias Ringwald * If an external trigger is used, the duration of its pulses must be longer than 115*1b2596b5SMatthias Ringwald * the master clock (MCLK) period in order to be detected. 116*1b2596b5SMatthias Ringwald * 117*1b2596b5SMatthias Ringwald * 118*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_extra_info Extra Information 119*1b2596b5SMatthias Ringwald * 120*1b2596b5SMatthias Ringwald * For extra information, see \ref asfdoc_sam_drivers_tc_extra. This includes: 121*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_extra_acronyms 122*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_extra_dependencies 123*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_extra_errata 124*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_extra_history 125*1b2596b5SMatthias Ringwald * 126*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_examples Examples 127*1b2596b5SMatthias Ringwald * 128*1b2596b5SMatthias Ringwald * For a list of examples related to this driver, see 129*1b2596b5SMatthias Ringwald * \ref asfdoc_sam_drivers_tc_exqsg. 130*1b2596b5SMatthias Ringwald * 131*1b2596b5SMatthias Ringwald * 132*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_api_overview API Overview 133*1b2596b5SMatthias Ringwald * @{ 134*1b2596b5SMatthias Ringwald */ 135*1b2596b5SMatthias Ringwald 136*1b2596b5SMatthias Ringwald #include <compiler.h> 137*1b2596b5SMatthias Ringwald 138*1b2596b5SMatthias Ringwald /// @cond 139*1b2596b5SMatthias Ringwald /**INDENT-OFF**/ 140*1b2596b5SMatthias Ringwald #ifdef __cplusplus 141*1b2596b5SMatthias Ringwald extern "C" { 142*1b2596b5SMatthias Ringwald #endif 143*1b2596b5SMatthias Ringwald /**INDENT-ON**/ 144*1b2596b5SMatthias Ringwald /// @endcond 145*1b2596b5SMatthias Ringwald 146*1b2596b5SMatthias Ringwald void tc_init(Tc *p_tc, uint32_t ul_Channel, uint32_t ul_Mode); 147*1b2596b5SMatthias Ringwald void tc_sync_trigger(Tc *p_tc); 148*1b2596b5SMatthias Ringwald void tc_set_block_mode(Tc *p_tc, uint32_t ul_blockmode); 149*1b2596b5SMatthias Ringwald 150*1b2596b5SMatthias Ringwald #if (!SAM3U) || defined(__DOXYGEN__) 151*1b2596b5SMatthias Ringwald uint32_t tc_init_2bit_gray(Tc *p_tc, uint32_t ul_channel, 152*1b2596b5SMatthias Ringwald uint32_t ul_steppermode); 153*1b2596b5SMatthias Ringwald #endif /* (!SAM3U) || defined(__DOXYGEN__) */ 154*1b2596b5SMatthias Ringwald 155*1b2596b5SMatthias Ringwald void tc_start(Tc *p_tc, uint32_t ul_channel); 156*1b2596b5SMatthias Ringwald void tc_stop(Tc *p_tc, uint32_t ul_channel); 157*1b2596b5SMatthias Ringwald 158*1b2596b5SMatthias Ringwald uint32_t tc_read_cv(Tc *p_tc, uint32_t ul_channel); 159*1b2596b5SMatthias Ringwald uint32_t tc_read_ra(Tc *p_tc, uint32_t ul_channel); 160*1b2596b5SMatthias Ringwald uint32_t tc_read_rb(Tc *p_tc, uint32_t ul_channel); 161*1b2596b5SMatthias Ringwald uint32_t tc_read_rc(Tc *p_tc, uint32_t ul_channel); 162*1b2596b5SMatthias Ringwald 163*1b2596b5SMatthias Ringwald void tc_write_ra(Tc *p_tc, uint32_t ul_channel, 164*1b2596b5SMatthias Ringwald uint32_t ul_value); 165*1b2596b5SMatthias Ringwald void tc_write_rb(Tc *p_tc, uint32_t ul_channel, 166*1b2596b5SMatthias Ringwald uint32_t ul_value); 167*1b2596b5SMatthias Ringwald void tc_write_rc(Tc *p_tc, uint32_t ul_channel, 168*1b2596b5SMatthias Ringwald uint32_t ul_value); 169*1b2596b5SMatthias Ringwald 170*1b2596b5SMatthias Ringwald uint32_t tc_find_mck_divisor(uint32_t ul_freq, uint32_t ul_mck, 171*1b2596b5SMatthias Ringwald uint32_t *p_uldiv, uint32_t *ul_tcclks, uint32_t ul_boardmck); 172*1b2596b5SMatthias Ringwald void tc_enable_interrupt(Tc *p_tc, uint32_t ul_channel, 173*1b2596b5SMatthias Ringwald uint32_t ul_sources); 174*1b2596b5SMatthias Ringwald void tc_disable_interrupt(Tc *p_tc, uint32_t ul_channel, 175*1b2596b5SMatthias Ringwald uint32_t ul_sources); 176*1b2596b5SMatthias Ringwald uint32_t tc_get_interrupt_mask(Tc *p_tc, uint32_t ul_channel); 177*1b2596b5SMatthias Ringwald uint32_t tc_get_status(Tc *p_tc, uint32_t ul_channel); 178*1b2596b5SMatthias Ringwald #if (!SAM4L && !SAMG) || defined(__DOXYGEN__) 179*1b2596b5SMatthias Ringwald void tc_enable_qdec_interrupt(Tc *p_tc, uint32_t ul_sources); 180*1b2596b5SMatthias Ringwald void tc_disable_qdec_interrupt(Tc *p_tc, uint32_t ul_sources); 181*1b2596b5SMatthias Ringwald uint32_t tc_get_qdec_interrupt_mask(Tc *p_tc); 182*1b2596b5SMatthias Ringwald uint32_t tc_get_qdec_interrupt_status(Tc *p_tc); 183*1b2596b5SMatthias Ringwald #endif /* (!SAM4L && !SAMG) || defined(__DOXYGEN__) */ 184*1b2596b5SMatthias Ringwald 185*1b2596b5SMatthias Ringwald #if (!SAM3U) 186*1b2596b5SMatthias Ringwald void tc_set_writeprotect(Tc *p_tc, uint32_t ul_enable); 187*1b2596b5SMatthias Ringwald #endif /* (!SAM3U) */ 188*1b2596b5SMatthias Ringwald 189*1b2596b5SMatthias Ringwald #if SAM4L || defined(__DOXYGEN__) 190*1b2596b5SMatthias Ringwald uint32_t tc_get_feature(Tc *p_tc); 191*1b2596b5SMatthias Ringwald uint32_t tc_get_version(Tc *p_tc); 192*1b2596b5SMatthias Ringwald #endif /* SAM4L || defined(__DOXYGEN__) */ 193*1b2596b5SMatthias Ringwald 194*1b2596b5SMatthias Ringwald /// @cond 195*1b2596b5SMatthias Ringwald /**INDENT-OFF**/ 196*1b2596b5SMatthias Ringwald #ifdef __cplusplus 197*1b2596b5SMatthias Ringwald } 198*1b2596b5SMatthias Ringwald #endif 199*1b2596b5SMatthias Ringwald /**INDENT-ON**/ 200*1b2596b5SMatthias Ringwald /// @endcond 201*1b2596b5SMatthias Ringwald 202*1b2596b5SMatthias Ringwald /** @} */ 203*1b2596b5SMatthias Ringwald 204*1b2596b5SMatthias Ringwald /** 205*1b2596b5SMatthias Ringwald * \page asfdoc_sam_drivers_tc_extra Extra Information for Timer Counter Driver 206*1b2596b5SMatthias Ringwald * 207*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_extra_acronyms Acronyms 208*1b2596b5SMatthias Ringwald * Below is a table listing the acronyms used in this module, along with their 209*1b2596b5SMatthias Ringwald * intended meanings. 210*1b2596b5SMatthias Ringwald * 211*1b2596b5SMatthias Ringwald * <table> 212*1b2596b5SMatthias Ringwald * <tr> 213*1b2596b5SMatthias Ringwald * <th>Acronym</th> 214*1b2596b5SMatthias Ringwald * <th>Definition</th> 215*1b2596b5SMatthias Ringwald * </tr> 216*1b2596b5SMatthias Ringwald * <tr> 217*1b2596b5SMatthias Ringwald * <td>MCK</td> 218*1b2596b5SMatthias Ringwald * <td>Master Clock</td> 219*1b2596b5SMatthias Ringwald * </tr> 220*1b2596b5SMatthias Ringwald * <tr> 221*1b2596b5SMatthias Ringwald * <td>PBA</td> 222*1b2596b5SMatthias Ringwald * <td>Peripheral Bus A clock</td> 223*1b2596b5SMatthias Ringwald * </tr> 224*1b2596b5SMatthias Ringwald * <tr> 225*1b2596b5SMatthias Ringwald * <td>PHA</td> 226*1b2596b5SMatthias Ringwald * <td>Quadrature Decoder input signal Phase A</td> 227*1b2596b5SMatthias Ringwald * </tr> 228*1b2596b5SMatthias Ringwald * <tr> 229*1b2596b5SMatthias Ringwald * <td>PHB</td> 230*1b2596b5SMatthias Ringwald * <td>Quadrature Decoder input signal Phase B</td> 231*1b2596b5SMatthias Ringwald * </tr> 232*1b2596b5SMatthias Ringwald * <tr> 233*1b2596b5SMatthias Ringwald * <td>QDEC</td> 234*1b2596b5SMatthias Ringwald * <td>Quadrature Decoder</td> 235*1b2596b5SMatthias Ringwald * </tr> 236*1b2596b5SMatthias Ringwald * <tr> 237*1b2596b5SMatthias Ringwald * <td>QSG</td> 238*1b2596b5SMatthias Ringwald * <td>Quick Start Guide</td> 239*1b2596b5SMatthias Ringwald * </tr> 240*1b2596b5SMatthias Ringwald * <tr> 241*1b2596b5SMatthias Ringwald * <td>RA</td> 242*1b2596b5SMatthias Ringwald * <td>Register A</td> 243*1b2596b5SMatthias Ringwald * </tr> 244*1b2596b5SMatthias Ringwald * <tr> 245*1b2596b5SMatthias Ringwald * <td>RB</td> 246*1b2596b5SMatthias Ringwald * <td>Register B</td> 247*1b2596b5SMatthias Ringwald * </tr> 248*1b2596b5SMatthias Ringwald * <tr> 249*1b2596b5SMatthias Ringwald * <td>RC</td> 250*1b2596b5SMatthias Ringwald * <td>Register C</td> 251*1b2596b5SMatthias Ringwald * </tr> 252*1b2596b5SMatthias Ringwald * <tr> 253*1b2596b5SMatthias Ringwald * <td>TIOB</td> 254*1b2596b5SMatthias Ringwald * <td>Timer Input Output B</td> 255*1b2596b5SMatthias Ringwald * </tr> 256*1b2596b5SMatthias Ringwald * </table> 257*1b2596b5SMatthias Ringwald * 258*1b2596b5SMatthias Ringwald * 259*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_extra_dependencies Dependencies 260*1b2596b5SMatthias Ringwald * This driver has the following dependencies: 261*1b2596b5SMatthias Ringwald * 262*1b2596b5SMatthias Ringwald * - \ref clk_group "System Clock Management (sysclock)" 263*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_gpio_group "General Purpose I/O (GPIO) driver" 264*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_pmc_group "Power Manager Controller (PMC) driver" 265*1b2596b5SMatthias Ringwald * 266*1b2596b5SMatthias Ringwald * 267*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_extra_errata Errata 268*1b2596b5SMatthias Ringwald * There are no errata related to this driver. 269*1b2596b5SMatthias Ringwald * 270*1b2596b5SMatthias Ringwald * 271*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_extra_history Module History 272*1b2596b5SMatthias Ringwald * An overview of the module history is presented in the table below, with 273*1b2596b5SMatthias Ringwald * details on the enhancements and fixes made to the module since its first 274*1b2596b5SMatthias Ringwald * release. The current version of this corresponds to the newest version in 275*1b2596b5SMatthias Ringwald * the table. 276*1b2596b5SMatthias Ringwald * 277*1b2596b5SMatthias Ringwald * <table> 278*1b2596b5SMatthias Ringwald * <tr> 279*1b2596b5SMatthias Ringwald * <th>Changelog</th> 280*1b2596b5SMatthias Ringwald * </tr> 281*1b2596b5SMatthias Ringwald * <tr> 282*1b2596b5SMatthias Ringwald * <td>Initial document release</td> 283*1b2596b5SMatthias Ringwald * </tr> 284*1b2596b5SMatthias Ringwald * </table> 285*1b2596b5SMatthias Ringwald */ 286*1b2596b5SMatthias Ringwald 287*1b2596b5SMatthias Ringwald /** 288*1b2596b5SMatthias Ringwald * \page asfdoc_sam_drivers_tc_exqsg Examples for Timer Counter 289*1b2596b5SMatthias Ringwald * 290*1b2596b5SMatthias Ringwald * This is a list of the available Quick Start Guides (QSGs) and example 291*1b2596b5SMatthias Ringwald * applications for \ref asfdoc_sam_drivers_tc_group. QSGs are simple examples with 292*1b2596b5SMatthias Ringwald * step-by-step instructions to configure and use this driver in a selection of 293*1b2596b5SMatthias Ringwald * use cases. Note that a QSG can be compiled as a standalone application or be 294*1b2596b5SMatthias Ringwald * added to the user application. 295*1b2596b5SMatthias Ringwald * 296*1b2596b5SMatthias Ringwald * - \subpage asfdoc_sam_drivers_tc_qsg 297*1b2596b5SMatthias Ringwald * - \subpage asfdoc_sam_drivers_tc_example 298*1b2596b5SMatthias Ringwald * 299*1b2596b5SMatthias Ringwald * \page asfdoc_sam_drivers_tc_document_revision_history Document Revision History 300*1b2596b5SMatthias Ringwald * 301*1b2596b5SMatthias Ringwald * <table> 302*1b2596b5SMatthias Ringwald * <tr> 303*1b2596b5SMatthias Ringwald * <th>Doc. Rev.</td> 304*1b2596b5SMatthias Ringwald * <th>Date</td> 305*1b2596b5SMatthias Ringwald * <th>Comments</td> 306*1b2596b5SMatthias Ringwald * </tr> 307*1b2596b5SMatthias Ringwald * <tr> 308*1b2596b5SMatthias Ringwald * <td>42301B</td> 309*1b2596b5SMatthias Ringwald * <td>07/2015</td> 310*1b2596b5SMatthias Ringwald * <td>Updated title of application note and added list of supported devices</td> 311*1b2596b5SMatthias Ringwald * </tr> 312*1b2596b5SMatthias Ringwald * <tr> 313*1b2596b5SMatthias Ringwald * <td>42301A</td> 314*1b2596b5SMatthias Ringwald * <td>05/2014</td> 315*1b2596b5SMatthias Ringwald * <td>Initial document release</td> 316*1b2596b5SMatthias Ringwald * </tr> 317*1b2596b5SMatthias Ringwald * </table> 318*1b2596b5SMatthias Ringwald * 319*1b2596b5SMatthias Ringwald */ 320*1b2596b5SMatthias Ringwald 321*1b2596b5SMatthias Ringwald /** 322*1b2596b5SMatthias Ringwald * \page asfdoc_sam_drivers_tc_qsg Quick Start Guide for the TC driver 323*1b2596b5SMatthias Ringwald * 324*1b2596b5SMatthias Ringwald * This is the quick start guide for the \ref asfdoc_sam_drivers_tc_group, with 325*1b2596b5SMatthias Ringwald * step-by-step instructions on how to configure and use the driver for 326*1b2596b5SMatthias Ringwald * a specific use case. The code examples can be copied into the main 327*1b2596b5SMatthias Ringwald * application loop or any other function that will need to control the 328*1b2596b5SMatthias Ringwald * AST module. 329*1b2596b5SMatthias Ringwald * 330*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_qsg_use_cases Use Cases 331*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_qsg_basic_capture 332*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_tc_qsg_basic_waveform 333*1b2596b5SMatthias Ringwald * 334*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_qsg_basic_capture TC Capture Mode Basic Usage 335*1b2596b5SMatthias Ringwald * 336*1b2596b5SMatthias Ringwald * This use case will demonstrate how to initialize the TC module to 337*1b2596b5SMatthias Ringwald * operate in capture mode using interrupts. Note, that the macros 338*1b2596b5SMatthias Ringwald * used to select the TC channel are device specific. Refer to the 339*1b2596b5SMatthias Ringwald * appropriate device-specific datasheet for more information. 340*1b2596b5SMatthias Ringwald * 341*1b2596b5SMatthias Ringwald * 342*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_qsg_basic_capture_setup Setup Steps 343*1b2596b5SMatthias Ringwald * 344*1b2596b5SMatthias Ringwald * \subsection asfdoc_sam_tc_qsg_basic_capture_prereq Prerequisites 345*1b2596b5SMatthias Ringwald * 346*1b2596b5SMatthias Ringwald * This module requires the following services: 347*1b2596b5SMatthias Ringwald * - \ref clk_group "System Clock Management (sysclock)" 348*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_gpio_group "General Purpose I/O (GPIO) driver" 349*1b2596b5SMatthias Ringwald * 350*1b2596b5SMatthias Ringwald * \subsection asfdoc_sam_drivers_tc_qsg_basic_capture_setup_code Setup Code 351*1b2596b5SMatthias Ringwald * 352*1b2596b5SMatthias Ringwald * Add these macros to the top of your main application C-file: 353*1b2596b5SMatthias Ringwald * \snippet conf_board.h tc_define_peripheral 354*1b2596b5SMatthias Ringwald * \snippet conf_board.h tc_define_ch2 355*1b2596b5SMatthias Ringwald * \snippet conf_board.h tc_define_irq_handler 356*1b2596b5SMatthias Ringwald * 357*1b2596b5SMatthias Ringwald * Add this macro and functions to your main application C-file: 358*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_selection 359*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_init 360*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_irq_handler_start 361*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_irq_handler_end 362*1b2596b5SMatthias Ringwald * 363*1b2596b5SMatthias Ringwald * \subsection asfdoc_sam_drivers_tc_qsg_basic_capture_setup_workflow Workflow 364*1b2596b5SMatthias Ringwald * 365*1b2596b5SMatthias Ringwald * -# Enable the TC module's capture pin: 366*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_gpio 367*1b2596b5SMatthias Ringwald * -# Initialize the capture channel to the following: 368*1b2596b5SMatthias Ringwald * - Load RA on the rising edge of TIOA 369*1b2596b5SMatthias Ringwald * - Load RB on the falling edge of TIOA 370*1b2596b5SMatthias Ringwald * - Set the external trigger to TIOA 371*1b2596b5SMatthias Ringwald * - Set the external trigger to falling edge 372*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_init_call 373*1b2596b5SMatthias Ringwald * -# Enable the TC interrupt using NVIC: 374*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_init_irq 375*1b2596b5SMatthias Ringwald * -# Enable the capture channel interrupt: 376*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_init_module_irq 377*1b2596b5SMatthias Ringwald * -# In the \ref TC_Handler_null "TC_Handler()" function, the load. 378*1b2596b5SMatthias Ringwald * RB interrupt can be checked by: 379*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_irq_handler_status 380*1b2596b5SMatthias Ringwald * \code } \endcode 381*1b2596b5SMatthias Ringwald * -# In the \ref TC_Handler_null "TC_Handler()" function, the RA value. 382*1b2596b5SMatthias Ringwald * can be read by: 383*1b2596b5SMatthias Ringwald * \code uint32_t gs_ul_captured_ra; \endcode 384*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_irq_handler_read_ra 385*1b2596b5SMatthias Ringwald * -# In the \ref TC_Handler_null "TC_Handler()" function, the RB value. 386*1b2596b5SMatthias Ringwald * can be read by: 387*1b2596b5SMatthias Ringwald * \code uint32_t gs_ul_captured_rb; \endcode 388*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_capture_irq_handler_read_rb 389*1b2596b5SMatthias Ringwald * 390*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_qsg_basic_waveform TC Waveform Mode Basic Usage 391*1b2596b5SMatthias Ringwald * 392*1b2596b5SMatthias Ringwald * This use case will demonstrate how to initialize the TC module to 393*1b2596b5SMatthias Ringwald * operate in waveform mode. Note, that the macros used to select 394*1b2596b5SMatthias Ringwald * the TC channel are device specific. Refer to the appropriate 395*1b2596b5SMatthias Ringwald * device-specific datasheet for more information. 396*1b2596b5SMatthias Ringwald * 397*1b2596b5SMatthias Ringwald * 398*1b2596b5SMatthias Ringwald * \section asfdoc_sam_drivers_tc_qsg_basic_waveform_setup Setup Steps 399*1b2596b5SMatthias Ringwald * 400*1b2596b5SMatthias Ringwald * \subsection asfdoc_sam_tc_qsg_basic_waveform_prereq Prerequisites 401*1b2596b5SMatthias Ringwald * 402*1b2596b5SMatthias Ringwald * This module requires the following services: 403*1b2596b5SMatthias Ringwald * - \ref clk_group "System Clock Management (sysclock)" 404*1b2596b5SMatthias Ringwald * - \ref asfdoc_sam_drivers_gpio_group "General Purpose I/O (GPIO) driver" 405*1b2596b5SMatthias Ringwald * 406*1b2596b5SMatthias Ringwald * \subsection asfdoc_sam_drivers_tc_qsg_basic_waveform_setup_code Setup Code 407*1b2596b5SMatthias Ringwald * 408*1b2596b5SMatthias Ringwald * Add these macros to the top of your main application C-file: 409*1b2596b5SMatthias Ringwald * \snippet conf_board.h tc_define_peripheral 410*1b2596b5SMatthias Ringwald * \snippet conf_board.h tc_define_ch1 411*1b2596b5SMatthias Ringwald * 412*1b2596b5SMatthias Ringwald * Add these macros and function to your main application C-file: 413*1b2596b5SMatthias Ringwald * \code #define TC_WAVEFORM_TIMER_SELECTION TC_CMR_TCCLKS_TIMER_CLOCK4 \endcode 414*1b2596b5SMatthias Ringwald * \code #define TC_WAVEFORM_DIVISOR 128 \endcode 415*1b2596b5SMatthias Ringwald * \code #define TC_WAVEFORM_FREQUENCY 178 \endcode 416*1b2596b5SMatthias Ringwald * \code #define TC_WAVEFORM_DUTY_CYCLE 30 \endcode 417*1b2596b5SMatthias Ringwald * \code 418*1b2596b5SMatthias Ringwald * static void tc_waveform_initialize(void) 419*1b2596b5SMatthias Ringwald * { 420*1b2596b5SMatthias Ringwald * uint32_t ra, rc; 421*1b2596b5SMatthias Ringwald * 422*1b2596b5SMatthias Ringwald * // Configure the PMC to enable the TC module. 423*1b2596b5SMatthias Ringwald * sysclk_enable_peripheral_clock(ID_TC_WAVEFORM); 424*1b2596b5SMatthias Ringwald * 425*1b2596b5SMatthias Ringwald * // Init TC to waveform mode. 426*1b2596b5SMatthias Ringwald * tc_init(TC, TC_CHANNEL_WAVEFORM, 427*1b2596b5SMatthias Ringwald * TC_WAVEFORM_TIMER_SELECTION // Waveform Clock Selection 428*1b2596b5SMatthias Ringwald * | TC_CMR_WAVE // Waveform mode is enabled 429*1b2596b5SMatthias Ringwald * | TC_CMR_ACPA_SET // RA Compare Effect: set 430*1b2596b5SMatthias Ringwald * | TC_CMR_ACPC_CLEAR // RC Compare Effect: clear 431*1b2596b5SMatthias Ringwald * | TC_CMR_CPCTRG // UP mode with automatic trigger on RC Compare 432*1b2596b5SMatthias Ringwald * ); 433*1b2596b5SMatthias Ringwald * 434*1b2596b5SMatthias Ringwald * // Configure waveform frequency and duty cycle. 435*1b2596b5SMatthias Ringwald * rc = (sysclk_get_peripheral_bus_hz(TC) / 436*1b2596b5SMatthias Ringwald * TC_WAVEFORM_DIVISOR / 437*1b2596b5SMatthias Ringwald * TC_WAVEFORM_FREQUENCY; 438*1b2596b5SMatthias Ringwald * tc_write_rc(TC, TC_CHANNEL_WAVEFORM, rc); 439*1b2596b5SMatthias Ringwald * ra = (100 - TC_WAVEFORM_FREQUENCY_DUTY_CYCLE * rc / 100; 440*1b2596b5SMatthias Ringwald * tc_write_ra(TC, TC_CHANNEL_WAVEFORM, ra); 441*1b2596b5SMatthias Ringwald * 442*1b2596b5SMatthias Ringwald * // Enable TC TC_CHANNEL_WAVEFORM. 443*1b2596b5SMatthias Ringwald * tc_start(TC, TC_CHANNEL_WAVEFORM); 444*1b2596b5SMatthias Ringwald * } \endcode 445*1b2596b5SMatthias Ringwald * 446*1b2596b5SMatthias Ringwald * \subsection asfdoc_sam_drivers_tc_qsg_basic_waveform_setup_workflow Workflow 447*1b2596b5SMatthias Ringwald * 448*1b2596b5SMatthias Ringwald * -# Enable the TC module's waveform pin: 449*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_waveform_gpio 450*1b2596b5SMatthias Ringwald * -# Initialize the waveform channel to the following: 451*1b2596b5SMatthias Ringwald * - Output frequency of 178Hz, with a duty-cycle of 30% 452*1b2596b5SMatthias Ringwald * - Use TC_CMR_TCCLKS_TIMER_CLOCK4, with a divisor of 128 453*1b2596b5SMatthias Ringwald * \snippet tc_capture_waveform_example.c tc_waveform_init_call 454*1b2596b5SMatthias Ringwald */ 455*1b2596b5SMatthias Ringwald #endif /* TC_H_INCLUDED */ 456