The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 111:4336505e4b1c 1 /**
Kojto 111:4336505e4b1c 2 * \file
Kojto 111:4336505e4b1c 3 *
Kojto 111:4336505e4b1c 4 * \brief SAM TC - Timer Counter Driver
Kojto 111:4336505e4b1c 5 *
Kojto 111:4336505e4b1c 6 * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved.
Kojto 111:4336505e4b1c 7 *
Kojto 111:4336505e4b1c 8 * \asf_license_start
Kojto 111:4336505e4b1c 9 *
Kojto 111:4336505e4b1c 10 * \page License
Kojto 111:4336505e4b1c 11 *
Kojto 111:4336505e4b1c 12 * Redistribution and use in source and binary forms, with or without
Kojto 111:4336505e4b1c 13 * modification, are permitted provided that the following conditions are met:
Kojto 111:4336505e4b1c 14 *
Kojto 111:4336505e4b1c 15 * 1. Redistributions of source code must retain the above copyright notice,
Kojto 111:4336505e4b1c 16 * this list of conditions and the following disclaimer.
Kojto 111:4336505e4b1c 17 *
Kojto 111:4336505e4b1c 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Kojto 111:4336505e4b1c 19 * this list of conditions and the following disclaimer in the documentation
Kojto 111:4336505e4b1c 20 * and/or other materials provided with the distribution.
Kojto 111:4336505e4b1c 21 *
Kojto 111:4336505e4b1c 22 * 3. The name of Atmel may not be used to endorse or promote products derived
Kojto 111:4336505e4b1c 23 * from this software without specific prior written permission.
Kojto 111:4336505e4b1c 24 *
Kojto 111:4336505e4b1c 25 * 4. This software may only be redistributed and used in connection with an
Kojto 111:4336505e4b1c 26 * Atmel microcontroller product.
Kojto 111:4336505e4b1c 27 *
Kojto 111:4336505e4b1c 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
Kojto 111:4336505e4b1c 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Kojto 111:4336505e4b1c 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
Kojto 111:4336505e4b1c 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
Kojto 111:4336505e4b1c 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Kojto 111:4336505e4b1c 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Kojto 111:4336505e4b1c 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Kojto 111:4336505e4b1c 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Kojto 111:4336505e4b1c 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
Kojto 111:4336505e4b1c 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Kojto 111:4336505e4b1c 38 * POSSIBILITY OF SUCH DAMAGE.
Kojto 111:4336505e4b1c 39 *
Kojto 111:4336505e4b1c 40 * \asf_license_stop
Kojto 111:4336505e4b1c 41 *
Kojto 111:4336505e4b1c 42 */
Kojto 111:4336505e4b1c 43 /*
Kojto 111:4336505e4b1c 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
Kojto 111:4336505e4b1c 45 */
Kojto 111:4336505e4b1c 46
Kojto 111:4336505e4b1c 47 #ifndef TC_H_INCLUDED
Kojto 111:4336505e4b1c 48 #define TC_H_INCLUDED
Kojto 111:4336505e4b1c 49
Kojto 111:4336505e4b1c 50 /**
Kojto 111:4336505e4b1c 51 * \defgroup asfdoc_sam0_tc_group SAM Timer/Counter Driver (TC)
Kojto 111:4336505e4b1c 52 *
Kojto 111:4336505e4b1c 53 * This driver for Atmel&reg; | SMART SAM devices provides an interface for the configuration
Kojto 111:4336505e4b1c 54 * and management of the timer modules within the device, for waveform
Kojto 111:4336505e4b1c 55 * generation and timing operations. The following driver API modes are covered
Kojto 111:4336505e4b1c 56 * by this manual:
Kojto 111:4336505e4b1c 57 *
Kojto 111:4336505e4b1c 58 * - Polled APIs
Kojto 111:4336505e4b1c 59 * \if TC_CALLBACK_MODE
Kojto 111:4336505e4b1c 60 * - Callback APIs
Kojto 111:4336505e4b1c 61 * \endif
Kojto 111:4336505e4b1c 62 *
Kojto 111:4336505e4b1c 63 *
Kojto 111:4336505e4b1c 64 * The following peripherals are used by this module:
Kojto 111:4336505e4b1c 65 * - TC (Timer/Counter)
Kojto 111:4336505e4b1c 66 *
Kojto 111:4336505e4b1c 67 * The following devices can use this module:
Kojto 111:4336505e4b1c 68 * - Atmel | SMART SAM D20/D21
Kojto 111:4336505e4b1c 69 * - Atmel | SMART SAM R21
Kojto 111:4336505e4b1c 70 * - Atmel | SMART SAM D10/D11
Kojto 111:4336505e4b1c 71 * - Atmel | SMART SAM L21
Kojto 111:4336505e4b1c 72 * - Atmel | SMART SAM DAx
Kojto 111:4336505e4b1c 73 * - Atmel | SMART SAM C20/C21
Kojto 111:4336505e4b1c 74 *
Kojto 111:4336505e4b1c 75 * The outline of this documentation is as follows:
Kojto 111:4336505e4b1c 76 * - \ref asfdoc_sam0_tc_prerequisites
Kojto 111:4336505e4b1c 77 * - \ref asfdoc_sam0_tc_module_overview
Kojto 111:4336505e4b1c 78 * - \ref asfdoc_sam0_tc_special_considerations
Kojto 111:4336505e4b1c 79 * - \ref asfdoc_sam0_tc_extra_info
Kojto 111:4336505e4b1c 80 * - \ref asfdoc_sam0_tc_examples
Kojto 111:4336505e4b1c 81 * - \ref asfdoc_sam0_tc_api_overview
Kojto 111:4336505e4b1c 82 *
Kojto 111:4336505e4b1c 83 *
Kojto 111:4336505e4b1c 84 * \section asfdoc_sam0_tc_prerequisites Prerequisites
Kojto 111:4336505e4b1c 85 *
Kojto 111:4336505e4b1c 86 * There are no prerequisites for this module.
Kojto 111:4336505e4b1c 87 *
Kojto 111:4336505e4b1c 88 *
Kojto 111:4336505e4b1c 89 * \section asfdoc_sam0_tc_module_overview Module Overview
Kojto 111:4336505e4b1c 90 *
Kojto 111:4336505e4b1c 91 * The Timer/Counter (TC) module provides a set of timing and counting related
Kojto 111:4336505e4b1c 92 * functionality, such as the generation of periodic waveforms, the capturing
Kojto 111:4336505e4b1c 93 * of a periodic waveform's frequency/duty cycle, and software timekeeping for
Kojto 111:4336505e4b1c 94 * periodic operations. TC modules can be configured to use an 8-, 16-, or
Kojto 111:4336505e4b1c 95 * 32-bit counter size.
Kojto 111:4336505e4b1c 96 *
Kojto 111:4336505e4b1c 97 * This TC module for the SAM is capable of the following functions:
Kojto 111:4336505e4b1c 98 *
Kojto 111:4336505e4b1c 99 * - Generation of PWM signals
Kojto 111:4336505e4b1c 100 * - Generation of timestamps for events
Kojto 111:4336505e4b1c 101 * - General time counting
Kojto 111:4336505e4b1c 102 * - Waveform period capture
Kojto 111:4336505e4b1c 103 * - Waveform frequency capture
Kojto 111:4336505e4b1c 104 *
Kojto 111:4336505e4b1c 105 * \ref asfdoc_sam0_tc_block_diagram "The diagram below" shows the overview
Kojto 111:4336505e4b1c 106 * of the TC module design.
Kojto 111:4336505e4b1c 107 *
Kojto 111:4336505e4b1c 108 * \anchor asfdoc_sam0_tc_block_diagram
Kojto 111:4336505e4b1c 109 * \image html overview.svg "Basic Overview of the TC Module"
Kojto 111:4336505e4b1c 110 *
Kojto 111:4336505e4b1c 111 *
Kojto 111:4336505e4b1c 112 * \subsection asfdoc_sam0_tc_features Driver Feature Macro Definition
Kojto 111:4336505e4b1c 113 * <table>
Kojto 111:4336505e4b1c 114 * <tr>
Kojto 111:4336505e4b1c 115 * <th>Driver Feature Macro</th>
Kojto 111:4336505e4b1c 116 * <th>Supported devices</th>
Kojto 111:4336505e4b1c 117 * </tr>
Kojto 111:4336505e4b1c 118 * <tr>
Kojto 111:4336505e4b1c 119 * <td>FEATURE_TC_DOUBLE_BUFFERED</td>
Kojto 111:4336505e4b1c 120 * <td>SAML21/C20/C21</td>
Kojto 111:4336505e4b1c 121 * </tr>
Kojto 111:4336505e4b1c 122 * <tr>
Kojto 111:4336505e4b1c 123 * <td>FEATURE_TC_SYNCBUSY_SCHEME_VERSION_2</td>
Kojto 111:4336505e4b1c 124 * <td>SAML21/C20/C21</td>
Kojto 111:4336505e4b1c 125 * </tr>
Kojto 111:4336505e4b1c 126 * <tr>
Kojto 111:4336505e4b1c 127 * <td>FEATURE_TC_STAMP_PW_CAPTURE</td>
Kojto 111:4336505e4b1c 128 * <td>SAML21/C20/C21</td>
Kojto 111:4336505e4b1c 129 * </tr>
Kojto 111:4336505e4b1c 130 * <tr>
Kojto 111:4336505e4b1c 131 * <td>FEATURE_TC_READ_SYNC</td>
Kojto 111:4336505e4b1c 132 * <td>SAML21/C20/C21</td>
Kojto 111:4336505e4b1c 133 * </tr>
Kojto 111:4336505e4b1c 134 * <tr>
Kojto 111:4336505e4b1c 135 * <td>FEATURE_TC_IO_CAPTURE</td>
Kojto 111:4336505e4b1c 136 * <td>SAML21/C20/C21</td>
Kojto 111:4336505e4b1c 137 * </tr>
Kojto 111:4336505e4b1c 138 * <tr>
Kojto 111:4336505e4b1c 139 * <td>FEATURE_TC_GENERATE_DMA_TRIGGER</td>
Kojto 111:4336505e4b1c 140 * <td>SAML21</td>
Kojto 111:4336505e4b1c 141 * </tr>
Kojto 111:4336505e4b1c 142 * </table>
Kojto 111:4336505e4b1c 143 * \note The specific features are only available in the driver when the
Kojto 111:4336505e4b1c 144 * selected device supports those features.
Kojto 111:4336505e4b1c 145 *
Kojto 111:4336505e4b1c 146 * \subsection asfdoc_sam0_tc_module_overview_func_desc Functional Description
Kojto 111:4336505e4b1c 147 * Independent of the configured counter size, each TC module can be set up
Kojto 111:4336505e4b1c 148 * in one of two different modes; capture and compare.
Kojto 111:4336505e4b1c 149 *
Kojto 111:4336505e4b1c 150 * In capture mode, the counter value is stored when a configurable event
Kojto 111:4336505e4b1c 151 * occurs. This mode can be used to generate timestamps used in event capture,
Kojto 111:4336505e4b1c 152 * or it can be used for the measurement of a periodic input signal's
Kojto 111:4336505e4b1c 153 * frequency/duty cycle.
Kojto 111:4336505e4b1c 154 *
Kojto 111:4336505e4b1c 155 * In compare mode, the counter value is compared against one or more of the
Kojto 111:4336505e4b1c 156 * configured channel compare values. When the counter value coincides with a
Kojto 111:4336505e4b1c 157 * compare value an action can be taken automatically by the module, such as
Kojto 111:4336505e4b1c 158 * generating an output event or toggling a pin when used for frequency or PWM
Kojto 111:4336505e4b1c 159 * signal generation.
Kojto 111:4336505e4b1c 160 *
Kojto 111:4336505e4b1c 161 * \note The connection of events between modules requires the use of the
Kojto 111:4336505e4b1c 162 * \ref asfdoc_sam0_events_group "SAM Event System Driver (EVENTS)"
Kojto 111:4336505e4b1c 163 * to route output event of one module to the input event of another.
Kojto 111:4336505e4b1c 164 * For more information on event routing, refer to the event driver
Kojto 111:4336505e4b1c 165 * documentation.
Kojto 111:4336505e4b1c 166 *
Kojto 111:4336505e4b1c 167 * \subsection asfdoc_sam0_tc_module_overview_tc_size Timer/Counter Size
Kojto 111:4336505e4b1c 168 * Each timer module can be configured in one of three different counter
Kojto 111:4336505e4b1c 169 * sizes; 8-, 16-, and 32-bit. The size of the counter determines the maximum
Kojto 111:4336505e4b1c 170 * value it can count to before an overflow occurs and the count is reset back
Kojto 111:4336505e4b1c 171 * to zero. \ref asfdoc_sam0_tc_count_size_vs_top "The table below" shows the
Kojto 111:4336505e4b1c 172 * maximum values for each of the possible counter sizes.
Kojto 111:4336505e4b1c 173 *
Kojto 111:4336505e4b1c 174 * \anchor asfdoc_sam0_tc_count_size_vs_top
Kojto 111:4336505e4b1c 175 * <table>
Kojto 111:4336505e4b1c 176 * <caption>Timer Counter Sizes and Their Maximum Count Values</caption>
Kojto 111:4336505e4b1c 177 * <tr>
Kojto 111:4336505e4b1c 178 * <th>Counter size</th>
Kojto 111:4336505e4b1c 179 * <th>Max. (hexadecimal)</th>
Kojto 111:4336505e4b1c 180 * <th>Max. (decimal)</th>
Kojto 111:4336505e4b1c 181 * </tr>
Kojto 111:4336505e4b1c 182 * <tr>
Kojto 111:4336505e4b1c 183 * <td>8-bit</td>
Kojto 111:4336505e4b1c 184 * <td>0xFF</td>
Kojto 111:4336505e4b1c 185 * <td>255</td>
Kojto 111:4336505e4b1c 186 * </tr>
Kojto 111:4336505e4b1c 187 * <tr>
Kojto 111:4336505e4b1c 188 * <td>16-bit</td>
Kojto 111:4336505e4b1c 189 * <td>0xFFFF</td>
Kojto 111:4336505e4b1c 190 * <td>65,535</td>
Kojto 111:4336505e4b1c 191 * </tr>
Kojto 111:4336505e4b1c 192 * <tr>
Kojto 111:4336505e4b1c 193 * <td>32-bit</td>
Kojto 111:4336505e4b1c 194 * <td>0xFFFFFFFF</td>
Kojto 111:4336505e4b1c 195 * <td>4,294,967,295</td>
Kojto 111:4336505e4b1c 196 * </tr>
Kojto 111:4336505e4b1c 197 * </table>
Kojto 111:4336505e4b1c 198 *
Kojto 111:4336505e4b1c 199 * When using the counter in 16- or 32-bit count mode, Compare Capture
Kojto 111:4336505e4b1c 200 * register 0 (CC0) is used to store the period value when running in PWM
Kojto 111:4336505e4b1c 201 * generation match mode.
Kojto 111:4336505e4b1c 202 *
Kojto 111:4336505e4b1c 203 * When using 32-bit counter size, two 16-bit counters are chained together
Kojto 111:4336505e4b1c 204 * in a cascade formation. Except in SAM D10/D11, Even numbered TC modules
Kojto 111:4336505e4b1c 205 * (e.g. TC0, TC2) can be configured as 32-bit counters. The odd numbered
Kojto 111:4336505e4b1c 206 * counters will act as slaves to the even numbered masters, and will not
Kojto 111:4336505e4b1c 207 * be reconfigurable until the master timer is disabled. The pairing of timer
Kojto 111:4336505e4b1c 208 * modules for 32-bit mode is shown in \ref asfdoc_sam0_tc_module_ms_pairs
Kojto 111:4336505e4b1c 209 * "the table below".
Kojto 111:4336505e4b1c 210 *
Kojto 111:4336505e4b1c 211 * \anchor asfdoc_sam0_tc_module_ms_pairs
Kojto 111:4336505e4b1c 212 * <table>
Kojto 111:4336505e4b1c 213 * <caption>TC Master and Slave Module Pairings</caption>
Kojto 111:4336505e4b1c 214 * <tr>
Kojto 111:4336505e4b1c 215 * <th>Master TC Module</th>
Kojto 111:4336505e4b1c 216 * <th>Slave TC Module</th>
Kojto 111:4336505e4b1c 217 * </tr>
Kojto 111:4336505e4b1c 218 * <tr>
Kojto 111:4336505e4b1c 219 * <td>TC0</td>
Kojto 111:4336505e4b1c 220 * <td>TC1</td>
Kojto 111:4336505e4b1c 221 * </tr>
Kojto 111:4336505e4b1c 222 * <tr>
Kojto 111:4336505e4b1c 223 * <td>TC2</td>
Kojto 111:4336505e4b1c 224 * <td>TC3</td>
Kojto 111:4336505e4b1c 225 * </tr>
Kojto 111:4336505e4b1c 226 * <tr>
Kojto 111:4336505e4b1c 227 * <td>...</td>
Kojto 111:4336505e4b1c 228 * <td>...</td>
Kojto 111:4336505e4b1c 229 * </tr>
Kojto 111:4336505e4b1c 230 * <tr>
Kojto 111:4336505e4b1c 231 * <td>TCn-1</td>
Kojto 111:4336505e4b1c 232 * <td>TCn</td>
Kojto 111:4336505e4b1c 233 * </tr>
Kojto 111:4336505e4b1c 234 * </table>
Kojto 111:4336505e4b1c 235 *
Kojto 111:4336505e4b1c 236 * In SAMD10/D11, odd numbered TC modules (e.g. TC1) can be configured as 32-bit
Kojto 111:4336505e4b1c 237 * counters. The even numbered(e.g. TC2) counters will act as slaves to the odd
Kojto 111:4336505e4b1c 238 * numbered masters.
Kojto 111:4336505e4b1c 239 *
Kojto 111:4336505e4b1c 240 * \subsection asfdoc_sam0_tc_module_overview_clock Clock Settings
Kojto 111:4336505e4b1c 241 *
Kojto 111:4336505e4b1c 242 * \subsubsection asfdoc_sam0_tc_module_overview_clock_selection Clock Selection
Kojto 111:4336505e4b1c 243 * Each TC peripheral is clocked asynchronously to the system clock by a GCLK
Kojto 111:4336505e4b1c 244 * (Generic Clock) channel. The GCLK channel connects to any of the GCLK
Kojto 111:4336505e4b1c 245 * generators. The GCLK generators are configured to use one of the available
Kojto 111:4336505e4b1c 246 * clock sources on the system such as internal oscillator, external crystals,
Kojto 111:4336505e4b1c 247 * etc. see the \ref asfdoc_sam0_system_clock_group "Generic Clock driver"
Kojto 111:4336505e4b1c 248 *for
Kojto 111:4336505e4b1c 249 * more information.
Kojto 111:4336505e4b1c 250 *
Kojto 111:4336505e4b1c 251 * \subsubsection asfdoc_sam0_tc_module_overview_clock_prescaler Prescaler
Kojto 111:4336505e4b1c 252 * Each TC module in the SAM has its own individual clock prescaler, which
Kojto 111:4336505e4b1c 253 * can be used to divide the input clock frequency used in the counter. This
Kojto 111:4336505e4b1c 254 * prescaler only scales the clock used to provide clock pulses for the counter
Kojto 111:4336505e4b1c 255 * to count, and does not affect the digital register interface portion of
Kojto 111:4336505e4b1c 256 * the module, thus the timer registers will synchronize to the raw GCLK
Kojto 111:4336505e4b1c 257 * frequency input to the module.
Kojto 111:4336505e4b1c 258 *
Kojto 111:4336505e4b1c 259 * As a result of this, when selecting a GCLK frequency and timer prescaler
Kojto 111:4336505e4b1c 260 * value the user application should consider both the timer resolution
Kojto 111:4336505e4b1c 261 * required and the synchronization frequency, to avoid lengthy
Kojto 111:4336505e4b1c 262 * synchronization times of the module if a very slow GCLK frequency is fed
Kojto 111:4336505e4b1c 263 * into the TC module. It is preferable to use a higher module GCLK frequency
Kojto 111:4336505e4b1c 264 * as the input to the timer, and prescale this down as much as possible to
Kojto 111:4336505e4b1c 265 * obtain a suitable counter frequency in latency-sensitive applications.
Kojto 111:4336505e4b1c 266 *
Kojto 111:4336505e4b1c 267 * \subsubsection asfdoc_sam0_tc_module_overview_clock_reloading Reloading
Kojto 111:4336505e4b1c 268 * Timer modules also contain a configurable reload action, used when a
Kojto 111:4336505e4b1c 269 * re-trigger event occurs. Examples of a re-trigger event are the counter
Kojto 111:4336505e4b1c 270 * reaching the maximum value when counting up, or when an event from the event
Kojto 111:4336505e4b1c 271 * system tells the counter to re-trigger. The reload action determines if the
Kojto 111:4336505e4b1c 272 * prescaler should be reset, and when this should happen. The counter will
Kojto 111:4336505e4b1c 273 * always be reloaded with the value it is set to start counting from. The user
Kojto 111:4336505e4b1c 274 * can choose between three different reload actions, described in
Kojto 111:4336505e4b1c 275 * \ref asfdoc_sam0_tc_module_reload_act "the table below".
Kojto 111:4336505e4b1c 276 *
Kojto 111:4336505e4b1c 277 * \anchor asfdoc_sam0_tc_module_reload_act
Kojto 111:4336505e4b1c 278 * <table>
Kojto 111:4336505e4b1c 279 * <caption>TC Module Reload Actions</caption>
Kojto 111:4336505e4b1c 280 * <tr>
Kojto 111:4336505e4b1c 281 * <th>Reload action</th>
Kojto 111:4336505e4b1c 282 * <th>Description</th>
Kojto 111:4336505e4b1c 283 * </tr>
Kojto 111:4336505e4b1c 284 * <tr>
Kojto 111:4336505e4b1c 285 * <td>\ref TC_RELOAD_ACTION_GCLK </td>
Kojto 111:4336505e4b1c 286 * <td>Reload TC counter value on next GCLK cycle. Leave prescaler
Kojto 111:4336505e4b1c 287 * as-is.</td>
Kojto 111:4336505e4b1c 288 * </tr>
Kojto 111:4336505e4b1c 289 * <tr>
Kojto 111:4336505e4b1c 290 * <td>\ref TC_RELOAD_ACTION_PRESC </td>
Kojto 111:4336505e4b1c 291 * <td>Reloads TC counter value on next prescaler clock. Leave prescaler
Kojto 111:4336505e4b1c 292 * as-is.</td>
Kojto 111:4336505e4b1c 293 * </tr>
Kojto 111:4336505e4b1c 294 * <tr>
Kojto 111:4336505e4b1c 295 * <td> \ref TC_RELOAD_ACTION_RESYNC </td>
Kojto 111:4336505e4b1c 296 * <td>Reload TC counter value on next GCLK cycle. Clear prescaler to
Kojto 111:4336505e4b1c 297 * zero.</td>
Kojto 111:4336505e4b1c 298 * </tr>
Kojto 111:4336505e4b1c 299 * </table>
Kojto 111:4336505e4b1c 300 *
Kojto 111:4336505e4b1c 301 * The reload action to use will depend on the specific application being
Kojto 111:4336505e4b1c 302 * implemented. One example is when an external trigger for a reload occurs; if
Kojto 111:4336505e4b1c 303 * the TC uses the prescaler, the counter in the prescaler should not have a
Kojto 111:4336505e4b1c 304 * value between zero and the division factor. The TC counter and the counter
Kojto 111:4336505e4b1c 305 * in the prescaler should both start at zero. When the counter is set to
Kojto 111:4336505e4b1c 306 * re-trigger when it reaches the maximum value on the other hand, this is not the
Kojto 111:4336505e4b1c 307 * right option to use. In such a case it would be better if the prescaler is
Kojto 111:4336505e4b1c 308 * left unaltered when the re-trigger happens, letting the counter reset on the
Kojto 111:4336505e4b1c 309 * next GCLK cycle.
Kojto 111:4336505e4b1c 310 *
Kojto 111:4336505e4b1c 311 * \subsection asfdoc_sam0_tc_module_overview_compare_match Compare Match Operations
Kojto 111:4336505e4b1c 312 * In compare match operation, Compare/Capture registers are used in comparison
Kojto 111:4336505e4b1c 313 * with the counter value. When the timer's count value matches the value of a
Kojto 111:4336505e4b1c 314 * compare channel, a user defined action can be taken.
Kojto 111:4336505e4b1c 315 *
Kojto 111:4336505e4b1c 316 * \subsubsection asfdoc_sam0_tc_module_overview_compare_match_timer Basic Timer
Kojto 111:4336505e4b1c 317 *
Kojto 111:4336505e4b1c 318 * A Basic Timer is a simple application where compare match operations is used
Kojto 111:4336505e4b1c 319 * to determine when a specific period has elapsed. In Basic Timer operations,
Kojto 111:4336505e4b1c 320 * one or more values in the module's Compare/Capture registers are used to
Kojto 111:4336505e4b1c 321 * specify the time (as a number of prescaled GCLK cycles) when an action should
Kojto 111:4336505e4b1c 322 * be taken by the microcontroller. This can be an Interrupt Service Routine
Kojto 111:4336505e4b1c 323 * (ISR), event generator via the event system, or a software flag that is
Kojto 111:4336505e4b1c 324 * polled via the user application.
Kojto 111:4336505e4b1c 325 *
Kojto 111:4336505e4b1c 326 * \subsubsection asfdoc_sam0_tc_module_overview_compare_match_wg Waveform Generation
Kojto 111:4336505e4b1c 327 *
Kojto 111:4336505e4b1c 328 * Waveform generation enables the TC module to generate square waves, or if
Kojto 111:4336505e4b1c 329 * combined with an external passive low-pass filter; analog waveforms.
Kojto 111:4336505e4b1c 330 *
Kojto 111:4336505e4b1c 331 * \subsubsection asfdoc_sam0_tc_module_overview_compare_match_wg_pwm Waveform Generation - PWM
Kojto 111:4336505e4b1c 332 *
Kojto 111:4336505e4b1c 333 * Pulse width modulation is a form of waveform generation and a signalling
Kojto 111:4336505e4b1c 334 * technique that can be useful in many situations. When PWM mode is used,
Kojto 111:4336505e4b1c 335 * a digital pulse train with a configurable frequency and duty cycle can be
Kojto 111:4336505e4b1c 336 * generated by the TC module and output to a GPIO pin of the device.
Kojto 111:4336505e4b1c 337 *
Kojto 111:4336505e4b1c 338 * Often PWM is used to communicate a control or information parameter to an
Kojto 111:4336505e4b1c 339 * external circuit or component. Differing impedances of the source generator
Kojto 111:4336505e4b1c 340 * and sink receiver circuits is less of an issue when using PWM compared to
Kojto 111:4336505e4b1c 341 * using an analog voltage value, as noise will not generally affect the
Kojto 111:4336505e4b1c 342 * signal's integrity to a meaningful extent.
Kojto 111:4336505e4b1c 343 *
Kojto 111:4336505e4b1c 344 * \ref asfdoc_sam0_tc_module_pwm_normal_diag "The figure below" illustrates
Kojto 111:4336505e4b1c 345 * operations and different states of the counter and its output when running
Kojto 111:4336505e4b1c 346 * the counter in PWM normal mode. As can be seen, the TOP value is unchanged
Kojto 111:4336505e4b1c 347 * and is set to MAX. The compare match value is changed at several points to
Kojto 111:4336505e4b1c 348 * illustrate the resulting waveform output changes. The PWM output is set to
Kojto 111:4336505e4b1c 349 * normal (i.e. non-inverted) output mode.
Kojto 111:4336505e4b1c 350 *
Kojto 111:4336505e4b1c 351 * \anchor asfdoc_sam0_tc_module_pwm_normal_diag
Kojto 111:4336505e4b1c 352 * \image html pwm_normal_ex.svg "Example of PWM in Normal Mode, and Different Counter Operations"
Kojto 111:4336505e4b1c 353 *
Kojto 111:4336505e4b1c 354 *
Kojto 111:4336505e4b1c 355 * In \ref asfdoc_sam0_tc_module_pwm_match_diag "the figure below", the
Kojto 111:4336505e4b1c 356 * counter is set to generate PWM in Match mode. The PWM output is inverted via
Kojto 111:4336505e4b1c 357 * the appropriate configuration option in the TC driver configuration
Kojto 111:4336505e4b1c 358 * structure. In this example, the counter value is changed once, but the
Kojto 111:4336505e4b1c 359 * compare match value is kept unchanged. As can be seen, it is possible to
Kojto 111:4336505e4b1c 360 * change the TOP value when running in PWM match mode.
Kojto 111:4336505e4b1c 361 *
Kojto 111:4336505e4b1c 362 * \anchor asfdoc_sam0_tc_module_pwm_match_diag
Kojto 111:4336505e4b1c 363 * \image html pwm_match_ex.svg "Example of PWM in Match Mode, and Different Counter Operations"
Kojto 111:4336505e4b1c 364 *
Kojto 111:4336505e4b1c 365 * \subsubsection asfdoc_sam0_tc_module_overview_compare_match_wg_freq Waveform Generation - Frequency
Kojto 111:4336505e4b1c 366 *
Kojto 111:4336505e4b1c 367 * Frequency Generation mode is in many ways identical to PWM
Kojto 111:4336505e4b1c 368 * generation. However, in Frequency Generation a toggle only occurs
Kojto 111:4336505e4b1c 369 * on the output when a match on a capture channels occurs. When the
Kojto 111:4336505e4b1c 370 * match is made, the timer value is reset, resulting in a variable
Kojto 111:4336505e4b1c 371 * frequency square wave with a fixed 50% duty cycle.
Kojto 111:4336505e4b1c 372 *
Kojto 111:4336505e4b1c 373 * \subsubsection asfdoc_sam0_tc_module_overview_compare_match_capt Capture Operations
Kojto 111:4336505e4b1c 374 *
Kojto 111:4336505e4b1c 375 * In capture operations, any event from the event system or a pin change can
Kojto 111:4336505e4b1c 376 * trigger a capture of the counter value. This captured counter value can be
Kojto 111:4336505e4b1c 377 * used as a timestamp for the event, or it can be used in frequency and pulse
Kojto 111:4336505e4b1c 378 * width capture.
Kojto 111:4336505e4b1c 379 *
Kojto 111:4336505e4b1c 380 * \subsubsection asfdoc_sam0_tc_module_overview_compare_match_capt_event_capture Capture Operations - Event
Kojto 111:4336505e4b1c 381 *
Kojto 111:4336505e4b1c 382 * Event capture is a simple use of the capture functionality,
Kojto 111:4336505e4b1c 383 * designed to create timestamps for specific events. When the TC
Kojto 111:4336505e4b1c 384 * module's input capture pin is externally toggled, the current timer
Kojto 111:4336505e4b1c 385 * count value is copied into a buffered register which can then be
Kojto 111:4336505e4b1c 386 * read out by the user application.
Kojto 111:4336505e4b1c 387 *
Kojto 111:4336505e4b1c 388 * Note that when performing any capture operation, there is a risk that the
Kojto 111:4336505e4b1c 389 * counter reaches its top value (MAX) when counting up, or the bottom value
Kojto 111:4336505e4b1c 390 * (zero) when counting down, before the capture event occurs. This can distort
Kojto 111:4336505e4b1c 391 * the result, making event timestamps to appear shorter than reality; the
Kojto 111:4336505e4b1c 392 * user application should check for timer overflow when reading a capture
Kojto 111:4336505e4b1c 393 * result in order to detect this situation and perform an appropriate
Kojto 111:4336505e4b1c 394 * adjustment.
Kojto 111:4336505e4b1c 395 *
Kojto 111:4336505e4b1c 396 * Before checking for a new capture, \ref TC_STATUS_COUNT_OVERFLOW
Kojto 111:4336505e4b1c 397 * should be checked. The response to an overflow error is left to the user
Kojto 111:4336505e4b1c 398 * application, however it may be necessary to clear both the capture overflow
Kojto 111:4336505e4b1c 399 * flag and the capture flag upon each capture reading.
Kojto 111:4336505e4b1c 400 *
Kojto 111:4336505e4b1c 401 * \subsubsection asfdoc_sam0_tc_module_overview_compare_match_capt_pwc Capture Operations - Pulse Width
Kojto 111:4336505e4b1c 402 *
Kojto 111:4336505e4b1c 403 * Pulse Width Capture mode makes it possible to measure the pulse width and
Kojto 111:4336505e4b1c 404 * period of PWM signals. This mode uses two capture channels of the counter.
Kojto 111:4336505e4b1c 405 * This means that the counter module used for Pulse Width Capture can not be
Kojto 111:4336505e4b1c 406 * used for any other purpose. There are two modes for pulse width capture;
Kojto 111:4336505e4b1c 407 * Pulse Width Period (PWP) and Period Pulse Width (PPW). In PWP mode, capture
Kojto 111:4336505e4b1c 408 * channel 0 is used for storing the pulse width and capture channel 1 stores
Kojto 111:4336505e4b1c 409 * the observed period. While in PPW mode, the roles of the two capture channels
Kojto 111:4336505e4b1c 410 * is reversed.
Kojto 111:4336505e4b1c 411 *
Kojto 111:4336505e4b1c 412 * As in the above example it is necessary to poll on interrupt flags to see
Kojto 111:4336505e4b1c 413 * if a new capture has happened and check that a capture overflow error has
Kojto 111:4336505e4b1c 414 * not occurred.
Kojto 111:4336505e4b1c 415 *
Kojto 111:4336505e4b1c 416 * \subsection asfdoc_sam0_tc_module_overview_oneshot One-shot Mode
Kojto 111:4336505e4b1c 417 *
Kojto 111:4336505e4b1c 418 * TC modules can be configured into a one-shot mode. When configured in this
Kojto 111:4336505e4b1c 419 * manner, starting the timer will cause it to count until the next overflow
Kojto 111:4336505e4b1c 420 * or underflow condition before automatically halting, waiting to be manually
Kojto 111:4336505e4b1c 421 * triggered by the user application software or an event signal from the event
Kojto 111:4336505e4b1c 422 * system.
Kojto 111:4336505e4b1c 423 *
Kojto 111:4336505e4b1c 424 * \subsubsection asfdoc_sam0_tc_module_overview_inversion Wave Generation Output Inversion
Kojto 111:4336505e4b1c 425 *
Kojto 111:4336505e4b1c 426 * The output of the wave generation can be inverted by hardware if desired,
Kojto 111:4336505e4b1c 427 * resulting in the logically inverted value being output to the configured
Kojto 111:4336505e4b1c 428 * device GPIO pin.
Kojto 111:4336505e4b1c 429 *
Kojto 111:4336505e4b1c 430 *
Kojto 111:4336505e4b1c 431 * \section asfdoc_sam0_tc_special_considerations Special Considerations
Kojto 111:4336505e4b1c 432 *
Kojto 111:4336505e4b1c 433 * The number of capture compare registers in each TC module is dependent on
Kojto 111:4336505e4b1c 434 * the specific SAM device being used, and in some cases the counter size.
Kojto 111:4336505e4b1c 435 *
Kojto 111:4336505e4b1c 436 * The maximum amount of capture compare registers available in any SAM
Kojto 111:4336505e4b1c 437 * device is two when running in 32-bit mode and four in 8- and 16-bit modes.
Kojto 111:4336505e4b1c 438 *
Kojto 111:4336505e4b1c 439 *
Kojto 111:4336505e4b1c 440 * \section asfdoc_sam0_tc_extra_info Extra Information
Kojto 111:4336505e4b1c 441 *
Kojto 111:4336505e4b1c 442 * For extra information, see \ref asfdoc_sam0_tc_extra. This includes:
Kojto 111:4336505e4b1c 443 * - \ref asfdoc_sam0_tc_extra_acronyms
Kojto 111:4336505e4b1c 444 * - \ref asfdoc_sam0_tc_extra_dependencies
Kojto 111:4336505e4b1c 445 * - \ref asfdoc_sam0_tc_extra_errata
Kojto 111:4336505e4b1c 446 * - \ref asfdoc_sam0_tc_extra_history
Kojto 111:4336505e4b1c 447 *
Kojto 111:4336505e4b1c 448 *
Kojto 111:4336505e4b1c 449 * \section asfdoc_sam0_tc_examples Examples
Kojto 111:4336505e4b1c 450 *
Kojto 111:4336505e4b1c 451 * For a list of examples related to this driver, see
Kojto 111:4336505e4b1c 452 * \ref asfdoc_sam0_tc_exqsg.
Kojto 111:4336505e4b1c 453 *
Kojto 111:4336505e4b1c 454 * \section asfdoc_sam0_tc_api_overview API Overview
Kojto 111:4336505e4b1c 455 * @{
Kojto 111:4336505e4b1c 456 */
Kojto 111:4336505e4b1c 457
Kojto 111:4336505e4b1c 458 #include <compiler.h>
Kojto 111:4336505e4b1c 459 #include <clock.h>
Kojto 111:4336505e4b1c 460 #include <gclk.h>
Kojto 111:4336505e4b1c 461 #include <pinmux.h>
Kojto 111:4336505e4b1c 462
Kojto 111:4336505e4b1c 463 /**
Kojto 111:4336505e4b1c 464 * Define port features set according to different device family
Kojto 111:4336505e4b1c 465 * @{
Kojto 111:4336505e4b1c 466 */
Kojto 111:4336505e4b1c 467 #if (SAML21) || (SAMC20) || (SAMC21) || defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 468 /** TC double buffered. */
Kojto 111:4336505e4b1c 469 # define FEATURE_TC_DOUBLE_BUFFERED
Kojto 111:4336505e4b1c 470 /** SYNCBUSY scheme version 2. */
Kojto 111:4336505e4b1c 471 # define FEATURE_TC_SYNCBUSY_SCHEME_VERSION_2
Kojto 111:4336505e4b1c 472 /** TC time stamp capture and pulse width capture. */
Kojto 111:4336505e4b1c 473 # define FEATURE_TC_STAMP_PW_CAPTURE
Kojto 111:4336505e4b1c 474 /** Read synchronization of COUNT. */
Kojto 111:4336505e4b1c 475 # define FEATURE_TC_READ_SYNC
Kojto 111:4336505e4b1c 476 /** IO pin edge capture. */
Kojto 111:4336505e4b1c 477 # define FEATURE_TC_IO_CAPTURE
Kojto 111:4336505e4b1c 478 /** Generate DMA triggers. */
Kojto 111:4336505e4b1c 479 # define FEATURE_TC_GENERATE_DMA_TRIGGER
Kojto 111:4336505e4b1c 480 #endif
Kojto 111:4336505e4b1c 481 /*@}*/
Kojto 111:4336505e4b1c 482
Kojto 111:4336505e4b1c 483 #if !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 484 #if SAMD20 || SAML21 || SAMC20 || SAMC21
Kojto 111:4336505e4b1c 485 # define TC_INSTANCE_OFFSET 0
Kojto 111:4336505e4b1c 486 #endif
Kojto 111:4336505e4b1c 487 #if SAMD21 || SAMR21 || SAMDA1
Kojto 111:4336505e4b1c 488 # define TC_INSTANCE_OFFSET 3
Kojto 111:4336505e4b1c 489 #endif
Kojto 111:4336505e4b1c 490 #if SAMD10 || SAMD11
Kojto 111:4336505e4b1c 491 # define TC_INSTANCE_OFFSET 1
Kojto 111:4336505e4b1c 492 #endif
Kojto 111:4336505e4b1c 493
Kojto 111:4336505e4b1c 494 #if SAMD20
Kojto 111:4336505e4b1c 495 # define NUMBER_OF_COMPARE_CAPTURE_CHANNELS TC0_CC8_NUM
Kojto 111:4336505e4b1c 496 #elif SAML21 || SAMC20 || SAMC21
Kojto 111:4336505e4b1c 497 # define NUMBER_OF_COMPARE_CAPTURE_CHANNELS TC0_CC_NUM
Kojto 111:4336505e4b1c 498 #elif SAMD10 || SAMD11
Kojto 111:4336505e4b1c 499 # define NUMBER_OF_COMPARE_CAPTURE_CHANNELS TC1_CC8_NUM
Kojto 111:4336505e4b1c 500 #else
Kojto 111:4336505e4b1c 501 # define NUMBER_OF_COMPARE_CAPTURE_CHANNELS TC3_CC8_NUM
Kojto 111:4336505e4b1c 502 /* Same number for 8-, 16- and 32-bit TC and all TC instances */
Kojto 111:4336505e4b1c 503 #endif
Kojto 111:4336505e4b1c 504
Kojto 111:4336505e4b1c 505 /** TC Instance MAX ID Number. */
Kojto 111:4336505e4b1c 506 #if SAMD20E || SAMD21G || SAMD21E || SAMR21
Kojto 111:4336505e4b1c 507 #define TC_INST_MAX_ID 5
Kojto 111:4336505e4b1c 508 #elif SAML21 || SAMC20 || SAMC21
Kojto 111:4336505e4b1c 509 #define TC_INST_MAX_ID 4
Kojto 111:4336505e4b1c 510 #elif SAMD10 || SAMD11
Kojto 111:4336505e4b1c 511 #define TC_INST_MAX_ID 2
Kojto 111:4336505e4b1c 512 #else
Kojto 111:4336505e4b1c 513 #define TC_INST_MAX_ID 7
Kojto 111:4336505e4b1c 514 #endif
Kojto 111:4336505e4b1c 515
Kojto 111:4336505e4b1c 516 #endif
Kojto 111:4336505e4b1c 517
Kojto 111:4336505e4b1c 518 #if TC_ASYNC == true
Kojto 111:4336505e4b1c 519 # include <system_interrupt.h>
Kojto 111:4336505e4b1c 520 #endif
Kojto 111:4336505e4b1c 521
Kojto 111:4336505e4b1c 522 #ifdef __cplusplus
Kojto 111:4336505e4b1c 523 extern "C" {
Kojto 111:4336505e4b1c 524 #endif
Kojto 111:4336505e4b1c 525
Kojto 111:4336505e4b1c 526 #if TC_ASYNC == true
Kojto 111:4336505e4b1c 527 /** Enum for the possible callback types for the TC module. */
Kojto 111:4336505e4b1c 528 enum tc_callback {
Kojto 111:4336505e4b1c 529 /** Callback for TC overflow. */
Kojto 111:4336505e4b1c 530 TC_CALLBACK_OVERFLOW,
Kojto 111:4336505e4b1c 531 /** Callback for capture overflow error. */
Kojto 111:4336505e4b1c 532 TC_CALLBACK_ERROR,
Kojto 111:4336505e4b1c 533 /** Callback for capture compare channel 0. */
Kojto 111:4336505e4b1c 534 TC_CALLBACK_CC_CHANNEL0,
Kojto 111:4336505e4b1c 535 /** Callback for capture compare channel 1. */
Kojto 111:4336505e4b1c 536 TC_CALLBACK_CC_CHANNEL1,
Kojto 111:4336505e4b1c 537 # if !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 538 /** Number of available callbacks. */
Kojto 111:4336505e4b1c 539 TC_CALLBACK_N,
Kojto 111:4336505e4b1c 540 # endif
Kojto 111:4336505e4b1c 541 };
Kojto 111:4336505e4b1c 542 #endif
Kojto 111:4336505e4b1c 543
Kojto 111:4336505e4b1c 544 /**
Kojto 111:4336505e4b1c 545 * \name Module Status Flags
Kojto 111:4336505e4b1c 546 *
Kojto 111:4336505e4b1c 547 * TC status flags, returned by \ref tc_get_status() and cleared by
Kojto 111:4336505e4b1c 548 * \ref tc_clear_status().
Kojto 111:4336505e4b1c 549 *
Kojto 111:4336505e4b1c 550 * @{
Kojto 111:4336505e4b1c 551 */
Kojto 111:4336505e4b1c 552
Kojto 111:4336505e4b1c 553 /** Timer channel 0 has matched against its compare value, or has captured a
Kojto 111:4336505e4b1c 554 * new value.
Kojto 111:4336505e4b1c 555 */
Kojto 111:4336505e4b1c 556 #define TC_STATUS_CHANNEL_0_MATCH (1UL << 0)
Kojto 111:4336505e4b1c 557
Kojto 111:4336505e4b1c 558 /** Timer channel 1 has matched against its compare value, or has captured a
Kojto 111:4336505e4b1c 559 * new value.
Kojto 111:4336505e4b1c 560 */
Kojto 111:4336505e4b1c 561 #define TC_STATUS_CHANNEL_1_MATCH (1UL << 1)
Kojto 111:4336505e4b1c 562
Kojto 111:4336505e4b1c 563 /** Timer register synchronization has completed, and the synchronized count
Kojto 111:4336505e4b1c 564 * value may be read.
Kojto 111:4336505e4b1c 565 */
Kojto 111:4336505e4b1c 566 #define TC_STATUS_SYNC_READY (1UL << 2)
Kojto 111:4336505e4b1c 567
Kojto 111:4336505e4b1c 568 /** A new value was captured before the previous value was read, resulting in
Kojto 111:4336505e4b1c 569 * lost data.
Kojto 111:4336505e4b1c 570 */
Kojto 111:4336505e4b1c 571 #define TC_STATUS_CAPTURE_OVERFLOW (1UL << 3)
Kojto 111:4336505e4b1c 572
Kojto 111:4336505e4b1c 573 /** The timer count value has overflowed from its maximum value to its minimum
Kojto 111:4336505e4b1c 574 * when counting upward, or from its minimum value to its maximum when
Kojto 111:4336505e4b1c 575 * counting downward.
Kojto 111:4336505e4b1c 576 */
Kojto 111:4336505e4b1c 577 #define TC_STATUS_COUNT_OVERFLOW (1UL << 4)
Kojto 111:4336505e4b1c 578
Kojto 111:4336505e4b1c 579 #ifdef FEATURE_TC_DOUBLE_BUFFERED
Kojto 111:4336505e4b1c 580 /** Channel 0 compare or capture buffer valid. */
Kojto 111:4336505e4b1c 581 #define TC_STATUS_CHN0_BUFFER_VALID (1UL << 5)
Kojto 111:4336505e4b1c 582 /** Channel 1 compare or capture buffer valid. */
Kojto 111:4336505e4b1c 583 #define TC_STATUS_CHN1_BUFFER_VALID (1UL << 6)
Kojto 111:4336505e4b1c 584 /** Period buffer valid. */
Kojto 111:4336505e4b1c 585 #define TC_STATUS_PERIOD_BUFFER_VALID (1UL << 7)
Kojto 111:4336505e4b1c 586 #endif
Kojto 111:4336505e4b1c 587 /** @} */
Kojto 111:4336505e4b1c 588
Kojto 111:4336505e4b1c 589 /**
Kojto 111:4336505e4b1c 590 * \brief Index of the compare capture channels.
Kojto 111:4336505e4b1c 591 *
Kojto 111:4336505e4b1c 592 * This enum is used to specify which capture/compare channel to do
Kojto 111:4336505e4b1c 593 * operations on.
Kojto 111:4336505e4b1c 594 */
Kojto 111:4336505e4b1c 595 enum tc_compare_capture_channel {
Kojto 111:4336505e4b1c 596 /** Index of compare capture channel 0. */
Kojto 111:4336505e4b1c 597 TC_COMPARE_CAPTURE_CHANNEL_0,
Kojto 111:4336505e4b1c 598 /** Index of compare capture channel 1. */
Kojto 111:4336505e4b1c 599 TC_COMPARE_CAPTURE_CHANNEL_1,
Kojto 111:4336505e4b1c 600 };
Kojto 111:4336505e4b1c 601
Kojto 111:4336505e4b1c 602 /** TC wave generation mode. */
Kojto 111:4336505e4b1c 603 #if SAML21 || SAMC20 || SAMC21
Kojto 111:4336505e4b1c 604 #define TC_WAVE_GENERATION_NORMAL_FREQ_MODE TC_WAVE_WAVEGEN_NFRQ
Kojto 111:4336505e4b1c 605 #define TC_WAVE_GENERATION_MATCH_FREQ_MODE TC_WAVE_WAVEGEN_MFRQ
Kojto 111:4336505e4b1c 606 #define TC_WAVE_GENERATION_NORMAL_PWM_MODE TC_WAVE_WAVEGEN_NPWM
Kojto 111:4336505e4b1c 607 #define TC_WAVE_GENERATION_MATCH_PWM_MODE TC_WAVE_WAVEGEN_MPWM
Kojto 111:4336505e4b1c 608 #else
Kojto 111:4336505e4b1c 609 #define TC_WAVE_GENERATION_NORMAL_FREQ_MODE TC_CTRLA_WAVEGEN_NFRQ
Kojto 111:4336505e4b1c 610 #define TC_WAVE_GENERATION_MATCH_FREQ_MODE TC_CTRLA_WAVEGEN_MFRQ
Kojto 111:4336505e4b1c 611 #define TC_WAVE_GENERATION_NORMAL_PWM_MODE TC_CTRLA_WAVEGEN_NPWM
Kojto 111:4336505e4b1c 612 #define TC_WAVE_GENERATION_MATCH_PWM_MODE TC_CTRLA_WAVEGEN_MPWM
Kojto 111:4336505e4b1c 613 #endif
Kojto 111:4336505e4b1c 614
Kojto 111:4336505e4b1c 615 /**
Kojto 111:4336505e4b1c 616 * \brief TC wave generation mode enum.
Kojto 111:4336505e4b1c 617 *
Kojto 111:4336505e4b1c 618 * This enum is used to select which mode to run the wave
Kojto 111:4336505e4b1c 619 * generation in.
Kojto 111:4336505e4b1c 620 *
Kojto 111:4336505e4b1c 621 */
Kojto 111:4336505e4b1c 622 enum tc_wave_generation {
Kojto 111:4336505e4b1c 623 /** Top is maximum, except in 8-bit counter size where it is the PER
Kojto 111:4336505e4b1c 624 * register.
Kojto 111:4336505e4b1c 625 */
Kojto 111:4336505e4b1c 626 TC_WAVE_GENERATION_NORMAL_FREQ = TC_WAVE_GENERATION_NORMAL_FREQ_MODE,
Kojto 111:4336505e4b1c 627
Kojto 111:4336505e4b1c 628 /** Top is CC0, except in 8-bit counter size where it is the PER
Kojto 111:4336505e4b1c 629 * register.
Kojto 111:4336505e4b1c 630 */
Kojto 111:4336505e4b1c 631 TC_WAVE_GENERATION_MATCH_FREQ = TC_WAVE_GENERATION_MATCH_FREQ_MODE,
Kojto 111:4336505e4b1c 632
Kojto 111:4336505e4b1c 633 /** Top is maximum, except in 8-bit counter size where it is the PER
Kojto 111:4336505e4b1c 634 * register.
Kojto 111:4336505e4b1c 635 */
Kojto 111:4336505e4b1c 636 TC_WAVE_GENERATION_NORMAL_PWM = TC_WAVE_GENERATION_NORMAL_PWM_MODE,
Kojto 111:4336505e4b1c 637
Kojto 111:4336505e4b1c 638 /** Top is CC0, except in 8-bit counter size where it is the PER
Kojto 111:4336505e4b1c 639 * register.
Kojto 111:4336505e4b1c 640 */
Kojto 111:4336505e4b1c 641 TC_WAVE_GENERATION_MATCH_PWM = TC_WAVE_GENERATION_MATCH_PWM_MODE,
Kojto 111:4336505e4b1c 642 };
Kojto 111:4336505e4b1c 643
Kojto 111:4336505e4b1c 644 /**
Kojto 111:4336505e4b1c 645 * \brief Specifies if the counter is 8-, 16-, or 32-bit.
Kojto 111:4336505e4b1c 646 *
Kojto 111:4336505e4b1c 647 * This enum specifies the maximum value it is possible to count to.
Kojto 111:4336505e4b1c 648 */
Kojto 111:4336505e4b1c 649 enum tc_counter_size {
Kojto 111:4336505e4b1c 650 /** The counter's maximum value is 0xFF, the period register is
Kojto 111:4336505e4b1c 651 * available to be used as top value.
Kojto 111:4336505e4b1c 652 */
Kojto 111:4336505e4b1c 653 TC_COUNTER_SIZE_8BIT = TC_CTRLA_MODE_COUNT8,
Kojto 111:4336505e4b1c 654
Kojto 111:4336505e4b1c 655 /** The counter's maximum value is 0xFFFF. There is no separate
Kojto 111:4336505e4b1c 656 * period register, to modify top one of the capture compare
Kojto 111:4336505e4b1c 657 * registers has to be used. This limits the amount of
Kojto 111:4336505e4b1c 658 * available channels.
Kojto 111:4336505e4b1c 659 */
Kojto 111:4336505e4b1c 660 TC_COUNTER_SIZE_16BIT = TC_CTRLA_MODE_COUNT16,
Kojto 111:4336505e4b1c 661
Kojto 111:4336505e4b1c 662 /** The counter's maximum value is 0xFFFFFFFF. There is no separate
Kojto 111:4336505e4b1c 663 * period register, to modify top one of the capture compare
Kojto 111:4336505e4b1c 664 * registers has to be used. This limits the amount of
Kojto 111:4336505e4b1c 665 * available channels.
Kojto 111:4336505e4b1c 666 */
Kojto 111:4336505e4b1c 667 TC_COUNTER_SIZE_32BIT = TC_CTRLA_MODE_COUNT32,
Kojto 111:4336505e4b1c 668 };
Kojto 111:4336505e4b1c 669
Kojto 111:4336505e4b1c 670 /**
Kojto 111:4336505e4b1c 671 * \brief TC Counter reload action enum.
Kojto 111:4336505e4b1c 672 *
Kojto 111:4336505e4b1c 673 * This enum specify how the counter and prescaler should reload.
Kojto 111:4336505e4b1c 674 */
Kojto 111:4336505e4b1c 675 enum tc_reload_action {
Kojto 111:4336505e4b1c 676 /** The counter is reloaded/reset on the next GCLK and starts
Kojto 111:4336505e4b1c 677 * counting on the prescaler clock.
Kojto 111:4336505e4b1c 678 */
Kojto 111:4336505e4b1c 679 TC_RELOAD_ACTION_GCLK = TC_CTRLA_PRESCSYNC_GCLK,
Kojto 111:4336505e4b1c 680
Kojto 111:4336505e4b1c 681 /** The counter is reloaded/reset on the next prescaler clock.
Kojto 111:4336505e4b1c 682 */
Kojto 111:4336505e4b1c 683 TC_RELOAD_ACTION_PRESC = TC_CTRLA_PRESCSYNC_PRESC,
Kojto 111:4336505e4b1c 684
Kojto 111:4336505e4b1c 685 /** The counter is reloaded/reset on the next GCLK, and the
Kojto 111:4336505e4b1c 686 * prescaler is restarted as well.
Kojto 111:4336505e4b1c 687 */
Kojto 111:4336505e4b1c 688 TC_RELOAD_ACTION_RESYNC = TC_CTRLA_PRESCSYNC_RESYNC,
Kojto 111:4336505e4b1c 689 };
Kojto 111:4336505e4b1c 690
Kojto 111:4336505e4b1c 691 /**
Kojto 111:4336505e4b1c 692 * \brief TC clock prescaler values.
Kojto 111:4336505e4b1c 693 *
Kojto 111:4336505e4b1c 694 * This enum is used to choose the clock prescaler
Kojto 111:4336505e4b1c 695 * configuration. The prescaler divides the clock frequency of the TC
Kojto 111:4336505e4b1c 696 * module to make the counter count slower.
Kojto 111:4336505e4b1c 697 */
Kojto 111:4336505e4b1c 698 enum tc_clock_prescaler {
Kojto 111:4336505e4b1c 699 /** Divide clock by 1. */
Kojto 111:4336505e4b1c 700 TC_CLOCK_PRESCALER_DIV1 = TC_CTRLA_PRESCALER(0),
Kojto 111:4336505e4b1c 701 /** Divide clock by 2. */
Kojto 111:4336505e4b1c 702 TC_CLOCK_PRESCALER_DIV2 = TC_CTRLA_PRESCALER(1),
Kojto 111:4336505e4b1c 703 /** Divide clock by 4. */
Kojto 111:4336505e4b1c 704 TC_CLOCK_PRESCALER_DIV4 = TC_CTRLA_PRESCALER(2),
Kojto 111:4336505e4b1c 705 /** Divide clock by 8. */
Kojto 111:4336505e4b1c 706 TC_CLOCK_PRESCALER_DIV8 = TC_CTRLA_PRESCALER(3),
Kojto 111:4336505e4b1c 707 /** Divide clock by 16. */
Kojto 111:4336505e4b1c 708 TC_CLOCK_PRESCALER_DIV16 = TC_CTRLA_PRESCALER(4),
Kojto 111:4336505e4b1c 709 /** Divide clock by 64. */
Kojto 111:4336505e4b1c 710 TC_CLOCK_PRESCALER_DIV64 = TC_CTRLA_PRESCALER(5),
Kojto 111:4336505e4b1c 711 /** Divide clock by 256. */
Kojto 111:4336505e4b1c 712 TC_CLOCK_PRESCALER_DIV256 = TC_CTRLA_PRESCALER(6),
Kojto 111:4336505e4b1c 713 /** Divide clock by 1024. */
Kojto 111:4336505e4b1c 714 TC_CLOCK_PRESCALER_DIV1024 = TC_CTRLA_PRESCALER(7),
Kojto 111:4336505e4b1c 715 };
Kojto 111:4336505e4b1c 716
Kojto 111:4336505e4b1c 717 /**
Kojto 111:4336505e4b1c 718 * \brief TC module count direction.
Kojto 111:4336505e4b1c 719 *
Kojto 111:4336505e4b1c 720 * Timer/Counter count direction.
Kojto 111:4336505e4b1c 721 */
Kojto 111:4336505e4b1c 722 enum tc_count_direction {
Kojto 111:4336505e4b1c 723 /** Timer should count upward from zero to MAX. */
Kojto 111:4336505e4b1c 724 TC_COUNT_DIRECTION_UP,
Kojto 111:4336505e4b1c 725
Kojto 111:4336505e4b1c 726 /** Timer should count downward to zero from MAX. */
Kojto 111:4336505e4b1c 727 TC_COUNT_DIRECTION_DOWN,
Kojto 111:4336505e4b1c 728 };
Kojto 111:4336505e4b1c 729
Kojto 111:4336505e4b1c 730 /** Waveform inversion mode. */
Kojto 111:4336505e4b1c 731 #if SAML21 || SAMC20 || SAMC21
Kojto 111:4336505e4b1c 732 #define TC_WAVEFORM_INVERT_CC0_MODE TC_DRVCTRL_INVEN(1)
Kojto 111:4336505e4b1c 733 #define TC_WAVEFORM_INVERT_CC1_MODE TC_DRVCTRL_INVEN(2)
Kojto 111:4336505e4b1c 734 #else
Kojto 111:4336505e4b1c 735 #define TC_WAVEFORM_INVERT_CC0_MODE TC_CTRLC_INVEN(1)
Kojto 111:4336505e4b1c 736 #define TC_WAVEFORM_INVERT_CC1_MODE TC_CTRLC_INVEN(2)
Kojto 111:4336505e4b1c 737 #endif
Kojto 111:4336505e4b1c 738
Kojto 111:4336505e4b1c 739 /**
Kojto 111:4336505e4b1c 740 * \brief Waveform inversion mode.
Kojto 111:4336505e4b1c 741 *
Kojto 111:4336505e4b1c 742 * Output waveform inversion mode.
Kojto 111:4336505e4b1c 743 */
Kojto 111:4336505e4b1c 744 enum tc_waveform_invert_output {
Kojto 111:4336505e4b1c 745 /** No inversion of the waveform output. */
Kojto 111:4336505e4b1c 746 TC_WAVEFORM_INVERT_OUTPUT_NONE = 0,
Kojto 111:4336505e4b1c 747 /** Invert output from compare channel 0. */
Kojto 111:4336505e4b1c 748 TC_WAVEFORM_INVERT_OUTPUT_CHANNEL_0 = TC_WAVEFORM_INVERT_CC0_MODE,
Kojto 111:4336505e4b1c 749 /** Invert output from compare channel 1. */
Kojto 111:4336505e4b1c 750 TC_WAVEFORM_INVERT_OUTPUT_CHANNEL_1 = TC_WAVEFORM_INVERT_CC1_MODE,
Kojto 111:4336505e4b1c 751 };
Kojto 111:4336505e4b1c 752
Kojto 111:4336505e4b1c 753 /**
Kojto 111:4336505e4b1c 754 * \brief Action to perform when the TC module is triggered by an event.
Kojto 111:4336505e4b1c 755 *
Kojto 111:4336505e4b1c 756 * Event action to perform when the module is triggered by an event.
Kojto 111:4336505e4b1c 757 */
Kojto 111:4336505e4b1c 758 enum tc_event_action {
Kojto 111:4336505e4b1c 759 /** No event action. */
Kojto 111:4336505e4b1c 760 TC_EVENT_ACTION_OFF = TC_EVCTRL_EVACT_OFF,
Kojto 111:4336505e4b1c 761 /** Re-trigger on event. */
Kojto 111:4336505e4b1c 762 TC_EVENT_ACTION_RETRIGGER = TC_EVCTRL_EVACT_RETRIGGER,
Kojto 111:4336505e4b1c 763 /** Increment counter on event. */
Kojto 111:4336505e4b1c 764 TC_EVENT_ACTION_INCREMENT_COUNTER = TC_EVCTRL_EVACT_COUNT,
Kojto 111:4336505e4b1c 765 /** Start counter on event. */
Kojto 111:4336505e4b1c 766 TC_EVENT_ACTION_START = TC_EVCTRL_EVACT_START,
Kojto 111:4336505e4b1c 767
Kojto 111:4336505e4b1c 768 /** Store period in capture register 0, pulse width in capture
Kojto 111:4336505e4b1c 769 * register 1.
Kojto 111:4336505e4b1c 770 */
Kojto 111:4336505e4b1c 771 TC_EVENT_ACTION_PPW = TC_EVCTRL_EVACT_PPW,
Kojto 111:4336505e4b1c 772
Kojto 111:4336505e4b1c 773 /** Store pulse width in capture register 0, period in capture
Kojto 111:4336505e4b1c 774 * register 1.
Kojto 111:4336505e4b1c 775 */
Kojto 111:4336505e4b1c 776 TC_EVENT_ACTION_PWP = TC_EVCTRL_EVACT_PWP,
Kojto 111:4336505e4b1c 777 #ifdef FEATURE_TC_STAMP_PW_CAPTURE
Kojto 111:4336505e4b1c 778 /** Time stamp capture. */
Kojto 111:4336505e4b1c 779 TC_EVENT_ACTION_STAMP = TC_EVCTRL_EVACT_STAMP,
Kojto 111:4336505e4b1c 780 /** Pulse width capture. */
Kojto 111:4336505e4b1c 781 TC_EVENT_ACTION_PW = TC_EVCTRL_EVACT_PW,
Kojto 111:4336505e4b1c 782 #endif
Kojto 111:4336505e4b1c 783 };
Kojto 111:4336505e4b1c 784
Kojto 111:4336505e4b1c 785 /**
Kojto 111:4336505e4b1c 786 * \brief TC event enable/disable structure.
Kojto 111:4336505e4b1c 787 *
Kojto 111:4336505e4b1c 788 * Event flags for the \ref tc_enable_events() and \ref tc_disable_events().
Kojto 111:4336505e4b1c 789 */
Kojto 111:4336505e4b1c 790 struct tc_events {
Kojto 111:4336505e4b1c 791 /** Generate an output event on a compare channel match. */
Kojto 111:4336505e4b1c 792 bool generate_event_on_compare_channel
Kojto 111:4336505e4b1c 793 [NUMBER_OF_COMPARE_CAPTURE_CHANNELS];
Kojto 111:4336505e4b1c 794 /** Generate an output event on counter overflow. */
Kojto 111:4336505e4b1c 795 bool generate_event_on_overflow;
Kojto 111:4336505e4b1c 796 /** Perform the configured event action when an incoming event is signalled. */
Kojto 111:4336505e4b1c 797 bool on_event_perform_action;
Kojto 111:4336505e4b1c 798 /** Specifies if the input event source is inverted, when used in PWP or
Kojto 111:4336505e4b1c 799 * PPW event action modes.
Kojto 111:4336505e4b1c 800 */
Kojto 111:4336505e4b1c 801 bool invert_event_input;
Kojto 111:4336505e4b1c 802 /** Specifies which event to trigger if an event is triggered. */
Kojto 111:4336505e4b1c 803 enum tc_event_action event_action;
Kojto 111:4336505e4b1c 804 };
Kojto 111:4336505e4b1c 805
Kojto 111:4336505e4b1c 806 /**
Kojto 111:4336505e4b1c 807 * \brief Configuration struct for TC module in 8-bit size counter mode.
Kojto 111:4336505e4b1c 808 */
Kojto 111:4336505e4b1c 809 struct tc_8bit_config {
Kojto 111:4336505e4b1c 810 /** Initial timer count value. */
Kojto 111:4336505e4b1c 811 uint8_t value;
Kojto 111:4336505e4b1c 812 /** Where to count to or from depending on the direction on the counter. */
Kojto 111:4336505e4b1c 813 uint8_t period;
Kojto 111:4336505e4b1c 814 /** Value to be used for compare match on each channel. */
Kojto 111:4336505e4b1c 815 uint8_t compare_capture_channel[NUMBER_OF_COMPARE_CAPTURE_CHANNELS];
Kojto 111:4336505e4b1c 816 };
Kojto 111:4336505e4b1c 817
Kojto 111:4336505e4b1c 818 /**
Kojto 111:4336505e4b1c 819 * \brief Configuration struct for TC module in 16-bit size counter mode.
Kojto 111:4336505e4b1c 820 */
Kojto 111:4336505e4b1c 821 struct tc_16bit_config {
Kojto 111:4336505e4b1c 822 /** Initial timer count value. */
Kojto 111:4336505e4b1c 823 uint16_t value;
Kojto 111:4336505e4b1c 824 /** Value to be used for compare match on each channel. */
Kojto 111:4336505e4b1c 825 uint16_t compare_capture_channel[NUMBER_OF_COMPARE_CAPTURE_CHANNELS];
Kojto 111:4336505e4b1c 826 };
Kojto 111:4336505e4b1c 827
Kojto 111:4336505e4b1c 828 /**
Kojto 111:4336505e4b1c 829 * \brief Configuration struct for TC module in 32-bit size counter mode.
Kojto 111:4336505e4b1c 830 */
Kojto 111:4336505e4b1c 831 struct tc_32bit_config {
Kojto 111:4336505e4b1c 832 /** Initial timer count value. */
Kojto 111:4336505e4b1c 833 uint32_t value;
Kojto 111:4336505e4b1c 834 /** Value to be used for compare match on each channel. */
Kojto 111:4336505e4b1c 835 uint32_t compare_capture_channel[NUMBER_OF_COMPARE_CAPTURE_CHANNELS];
Kojto 111:4336505e4b1c 836 };
Kojto 111:4336505e4b1c 837
Kojto 111:4336505e4b1c 838 /**
Kojto 111:4336505e4b1c 839 * \brief Configuration struct for TC module in 32-bit size counter mode.
Kojto 111:4336505e4b1c 840 */
Kojto 111:4336505e4b1c 841 struct tc_pwm_channel {
Kojto 111:4336505e4b1c 842 /** When \c true, PWM output for the given channel is enabled. */
Kojto 111:4336505e4b1c 843 bool enabled;
Kojto 111:4336505e4b1c 844 /** Specifies pin output for each channel. */
Kojto 111:4336505e4b1c 845 uint32_t pin_out;
Kojto 111:4336505e4b1c 846 /** Specifies MUX setting for each output channel pin. */
Kojto 111:4336505e4b1c 847 uint32_t pin_mux;
Kojto 111:4336505e4b1c 848 };
Kojto 111:4336505e4b1c 849
Kojto 111:4336505e4b1c 850 /**
Kojto 111:4336505e4b1c 851 * \brief TC configuration structure.
Kojto 111:4336505e4b1c 852 *
Kojto 111:4336505e4b1c 853 * Configuration struct for a TC instance. This structure should be
Kojto 111:4336505e4b1c 854 * initialized by the \ref tc_get_config_defaults function before being
Kojto 111:4336505e4b1c 855 * modified by the user application.
Kojto 111:4336505e4b1c 856 */
Kojto 111:4336505e4b1c 857 struct tc_config {
Kojto 111:4336505e4b1c 858 /** GCLK generator used to clock the peripheral. */
Kojto 111:4336505e4b1c 859 enum gclk_generator clock_source;
Kojto 111:4336505e4b1c 860
Kojto 111:4336505e4b1c 861 /** When \c true the module is enabled during standby. */
Kojto 111:4336505e4b1c 862 bool run_in_standby;
Kojto 111:4336505e4b1c 863 #if (SAML21) || (SAMC20) || (SAMC21)
Kojto 111:4336505e4b1c 864 /** Run on demand. */
Kojto 111:4336505e4b1c 865 bool on_demand;
Kojto 111:4336505e4b1c 866 #endif
Kojto 111:4336505e4b1c 867 /** Specifies either 8-, 16-, or 32-bit counter size. */
Kojto 111:4336505e4b1c 868 enum tc_counter_size counter_size;
Kojto 111:4336505e4b1c 869 /** Specifies the prescaler value for GCLK_TC. */
Kojto 111:4336505e4b1c 870 enum tc_clock_prescaler clock_prescaler;
Kojto 111:4336505e4b1c 871 /** Specifies which waveform generation mode to use. */
Kojto 111:4336505e4b1c 872 enum tc_wave_generation wave_generation;
Kojto 111:4336505e4b1c 873
Kojto 111:4336505e4b1c 874 /** Specifies the reload or reset time of the counter and prescaler
Kojto 111:4336505e4b1c 875 * resynchronization on a re-trigger event for the TC.
Kojto 111:4336505e4b1c 876 */
Kojto 111:4336505e4b1c 877 enum tc_reload_action reload_action;
Kojto 111:4336505e4b1c 878
Kojto 111:4336505e4b1c 879 /** Specifies which channel(s) to invert the waveform on.
Kojto 111:4336505e4b1c 880 For SAML21/C20/C21, it's also used to invert IO input pin. */
Kojto 111:4336505e4b1c 881 uint8_t waveform_invert_output;
Kojto 111:4336505e4b1c 882
Kojto 111:4336505e4b1c 883 /** Specifies which channel(s) to enable channel capture
Kojto 111:4336505e4b1c 884 * operation on.
Kojto 111:4336505e4b1c 885 */
Kojto 111:4336505e4b1c 886 bool enable_capture_on_channel[NUMBER_OF_COMPARE_CAPTURE_CHANNELS];
Kojto 111:4336505e4b1c 887 #ifdef FEATURE_TC_IO_CAPTURE
Kojto 111:4336505e4b1c 888 /** Specifies which channel(s) to enable I/O capture
Kojto 111:4336505e4b1c 889 * operation on.
Kojto 111:4336505e4b1c 890 */
Kojto 111:4336505e4b1c 891 bool enable_capture_on_IO[NUMBER_OF_COMPARE_CAPTURE_CHANNELS];
Kojto 111:4336505e4b1c 892 #endif
Kojto 111:4336505e4b1c 893
Kojto 111:4336505e4b1c 894 /** When \c true, one-shot will stop the TC on next hardware or software
Kojto 111:4336505e4b1c 895 * re-trigger event or overflow/underflow.
Kojto 111:4336505e4b1c 896 */
Kojto 111:4336505e4b1c 897 bool oneshot;
Kojto 111:4336505e4b1c 898
Kojto 111:4336505e4b1c 899 /** Specifies the direction for the TC to count. */
Kojto 111:4336505e4b1c 900 enum tc_count_direction count_direction;
Kojto 111:4336505e4b1c 901
Kojto 111:4336505e4b1c 902 /** Specifies the PWM channel for TC. */
Kojto 111:4336505e4b1c 903 struct tc_pwm_channel pwm_channel[NUMBER_OF_COMPARE_CAPTURE_CHANNELS];
Kojto 111:4336505e4b1c 904
Kojto 111:4336505e4b1c 905 /** Access the different counter size settings though this configuration member. */
Kojto 111:4336505e4b1c 906 union {
Kojto 111:4336505e4b1c 907 /** Struct for 8-bit specific timer configuration. */
Kojto 111:4336505e4b1c 908 struct tc_8bit_config counter_8_bit;
Kojto 111:4336505e4b1c 909 /** Struct for 16-bit specific timer configuration. */
Kojto 111:4336505e4b1c 910 struct tc_16bit_config counter_16_bit;
Kojto 111:4336505e4b1c 911 /** Struct for 32-bit specific timer configuration. */
Kojto 111:4336505e4b1c 912 struct tc_32bit_config counter_32_bit;
Kojto 111:4336505e4b1c 913 };
Kojto 111:4336505e4b1c 914
Kojto 111:4336505e4b1c 915 #ifdef FEATURE_TC_DOUBLE_BUFFERED
Kojto 111:4336505e4b1c 916 /** Set to \c true to enable double buffering write. When enabled any write
Kojto 111:4336505e4b1c 917 * through \ref tc_set_top_value(), \ref tc_set_compare_value() and
Kojto 111:4336505e4b1c 918 * will direct to the buffer register as buffered
Kojto 111:4336505e4b1c 919 * value, and the buffered value will be committed to effective register
Kojto 111:4336505e4b1c 920 * on UPDATE condition, if update is not locked.
Kojto 111:4336505e4b1c 921 */
Kojto 111:4336505e4b1c 922 bool double_buffering_enabled;
Kojto 111:4336505e4b1c 923 #endif
Kojto 111:4336505e4b1c 924 };
Kojto 111:4336505e4b1c 925
Kojto 111:4336505e4b1c 926 #if TC_ASYNC == true
Kojto 111:4336505e4b1c 927 /* Forward Declaration for the device instance. */
Kojto 111:4336505e4b1c 928 struct tc_module;
Kojto 111:4336505e4b1c 929
Kojto 111:4336505e4b1c 930 /* Type of the callback functions. */
Kojto 111:4336505e4b1c 931 typedef void (*tc_callback_t)(struct tc_module *const module);
Kojto 111:4336505e4b1c 932 #endif
Kojto 111:4336505e4b1c 933
Kojto 111:4336505e4b1c 934 /**
Kojto 111:4336505e4b1c 935 * \brief TC software device instance structure.
Kojto 111:4336505e4b1c 936 *
Kojto 111:4336505e4b1c 937 * TC software instance structure, used to retain software state information
Kojto 111:4336505e4b1c 938 * of an associated hardware module instance.
Kojto 111:4336505e4b1c 939 *
Kojto 111:4336505e4b1c 940 * \note The fields of this structure should not be altered by the user
Kojto 111:4336505e4b1c 941 * application; they are reserved for module-internal use only.
Kojto 111:4336505e4b1c 942 */
Kojto 111:4336505e4b1c 943 struct tc_module {
Kojto 111:4336505e4b1c 944 #if !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 945 /** Hardware module pointer of the associated Timer/Counter peripheral. */
Kojto 111:4336505e4b1c 946 Tc *hw;
Kojto 111:4336505e4b1c 947
Kojto 111:4336505e4b1c 948 /** Size of the initialized Timer/Counter module configuration. */
Kojto 111:4336505e4b1c 949 enum tc_counter_size counter_size;
Kojto 111:4336505e4b1c 950 # if TC_ASYNC == true
Kojto 111:4336505e4b1c 951 /** Array of callbacks. */
Kojto 111:4336505e4b1c 952 tc_callback_t callback[TC_CALLBACK_N];
Kojto 111:4336505e4b1c 953 /** Bit mask for callbacks registered. */
Kojto 111:4336505e4b1c 954 uint8_t register_callback_mask;
Kojto 111:4336505e4b1c 955 /** Bit mask for callbacks enabled. */
Kojto 111:4336505e4b1c 956 uint8_t enable_callback_mask;
Kojto 111:4336505e4b1c 957 # endif
Kojto 111:4336505e4b1c 958 #ifdef FEATURE_TC_DOUBLE_BUFFERED
Kojto 111:4336505e4b1c 959 /** Set to \c true to enable double buffering write. */
Kojto 111:4336505e4b1c 960 bool double_buffering_enabled;
Kojto 111:4336505e4b1c 961 #endif
Kojto 111:4336505e4b1c 962 #endif
Kojto 111:4336505e4b1c 963 };
Kojto 111:4336505e4b1c 964
Kojto 111:4336505e4b1c 965 #if !defined(__DOXYGEN__)
Kojto 111:4336505e4b1c 966 uint8_t _tc_get_inst_index(
Kojto 111:4336505e4b1c 967 Tc *const hw);
Kojto 111:4336505e4b1c 968 #endif
Kojto 111:4336505e4b1c 969
Kojto 111:4336505e4b1c 970 /**
Kojto 111:4336505e4b1c 971 * \name Driver Initialization and Configuration
Kojto 111:4336505e4b1c 972 * @{
Kojto 111:4336505e4b1c 973 */
Kojto 111:4336505e4b1c 974
Kojto 111:4336505e4b1c 975 /**
Kojto 111:4336505e4b1c 976 * \brief Determines if the hardware module(s) are currently synchronizing to
Kojto 111:4336505e4b1c 977 *the bus.
Kojto 111:4336505e4b1c 978 *
Kojto 111:4336505e4b1c 979 * Checks to see if the underlying hardware peripheral module(s) are currently
Kojto 111:4336505e4b1c 980 * synchronizing across multiple clock domains to the hardware bus. This
Kojto 111:4336505e4b1c 981 * function can be used to delay further operations on a module until such time
Kojto 111:4336505e4b1c 982 * that it is ready, to prevent blocking delays for synchronization in the
Kojto 111:4336505e4b1c 983 * user application.
Kojto 111:4336505e4b1c 984 *
Kojto 111:4336505e4b1c 985 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 986 *
Kojto 111:4336505e4b1c 987 * \return Synchronization status of the underlying hardware module(s).
Kojto 111:4336505e4b1c 988 *
Kojto 111:4336505e4b1c 989 * \retval false If the module has completed synchronization
Kojto 111:4336505e4b1c 990 * \retval true If the module synchronization is ongoing
Kojto 111:4336505e4b1c 991 */
Kojto 111:4336505e4b1c 992 static inline bool tc_is_syncing(
Kojto 111:4336505e4b1c 993 const struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 994 {
Kojto 111:4336505e4b1c 995 /* Sanity check arguments */
Kojto 111:4336505e4b1c 996 Assert(module_inst);
Kojto 111:4336505e4b1c 997 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 998
Kojto 111:4336505e4b1c 999 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1000 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1001
Kojto 111:4336505e4b1c 1002 #if (SAML21) || (SAMC20) || (SAMC21)
Kojto 111:4336505e4b1c 1003 return (tc_module->SYNCBUSY.reg);
Kojto 111:4336505e4b1c 1004 #else
Kojto 111:4336505e4b1c 1005 return (tc_module->STATUS.reg & TC_STATUS_SYNCBUSY);
Kojto 111:4336505e4b1c 1006 #endif
Kojto 111:4336505e4b1c 1007 }
Kojto 111:4336505e4b1c 1008
Kojto 111:4336505e4b1c 1009 /**
Kojto 111:4336505e4b1c 1010 * \brief Initializes config with predefined default values.
Kojto 111:4336505e4b1c 1011 *
Kojto 111:4336505e4b1c 1012 * This function will initialize a given TC configuration structure to
Kojto 111:4336505e4b1c 1013 * a set of known default values. This function should be called on
Kojto 111:4336505e4b1c 1014 * any new instance of the configuration structures before being
Kojto 111:4336505e4b1c 1015 * modified by the user application.
Kojto 111:4336505e4b1c 1016 *
Kojto 111:4336505e4b1c 1017 * The default configuration is as follows:
Kojto 111:4336505e4b1c 1018 * \li GCLK generator 0 (GCLK main) clock source
Kojto 111:4336505e4b1c 1019 * \li 16-bit counter size on the counter
Kojto 111:4336505e4b1c 1020 * \li No prescaler
Kojto 111:4336505e4b1c 1021 * \li Normal frequency wave generation
Kojto 111:4336505e4b1c 1022 * \li GCLK reload action
Kojto 111:4336505e4b1c 1023 * \li Don't run in standby
Kojto 111:4336505e4b1c 1024 * \li Don't run on demand for SAML21/C20/C21
Kojto 111:4336505e4b1c 1025 * \li No inversion of waveform output
Kojto 111:4336505e4b1c 1026 * \li No capture enabled
Kojto 111:4336505e4b1c 1027 * \li No I/O capture enabled for SAML21/C20/C21
Kojto 111:4336505e4b1c 1028 * \li No event input enabled
Kojto 111:4336505e4b1c 1029 * \li Count upward
Kojto 111:4336505e4b1c 1030 * \li Don't perform one-shot operations
Kojto 111:4336505e4b1c 1031 * \li No event action
Kojto 111:4336505e4b1c 1032 * \li No channel 0 PWM output
Kojto 111:4336505e4b1c 1033 * \li No channel 1 PWM output
Kojto 111:4336505e4b1c 1034 * \li Counter starts on 0
Kojto 111:4336505e4b1c 1035 * \li Capture compare channel 0 set to 0
Kojto 111:4336505e4b1c 1036 * \li Capture compare channel 1 set to 0
Kojto 111:4336505e4b1c 1037 * \li No PWM pin output enabled
Kojto 111:4336505e4b1c 1038 * \li Pin and MUX configuration not set
Kojto 111:4336505e4b1c 1039 * \li Double buffer disabled (if have this feature)
Kojto 111:4336505e4b1c 1040 *
Kojto 111:4336505e4b1c 1041 * \param[out] config Pointer to a TC module configuration structure to set
Kojto 111:4336505e4b1c 1042 */
Kojto 111:4336505e4b1c 1043 static inline void tc_get_config_defaults(
Kojto 111:4336505e4b1c 1044 struct tc_config *const config)
Kojto 111:4336505e4b1c 1045 {
Kojto 111:4336505e4b1c 1046 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1047 Assert(config);
Kojto 111:4336505e4b1c 1048
Kojto 111:4336505e4b1c 1049 /* Write default config to config struct */
Kojto 111:4336505e4b1c 1050 config->clock_source = GCLK_GENERATOR_0;
Kojto 111:4336505e4b1c 1051 config->counter_size = TC_COUNTER_SIZE_16BIT;
Kojto 111:4336505e4b1c 1052 config->clock_prescaler = TC_CLOCK_PRESCALER_DIV1;
Kojto 111:4336505e4b1c 1053 config->wave_generation = TC_WAVE_GENERATION_NORMAL_FREQ;
Kojto 111:4336505e4b1c 1054 config->reload_action = TC_RELOAD_ACTION_GCLK;
Kojto 111:4336505e4b1c 1055 config->run_in_standby = false;
Kojto 111:4336505e4b1c 1056 #if (SAML21) || (SAMC20) || (SAMC21)
Kojto 111:4336505e4b1c 1057 config->on_demand = false;
Kojto 111:4336505e4b1c 1058 #endif
Kojto 111:4336505e4b1c 1059 config->waveform_invert_output = TC_WAVEFORM_INVERT_OUTPUT_NONE;
Kojto 111:4336505e4b1c 1060 config->enable_capture_on_channel[TC_COMPARE_CAPTURE_CHANNEL_0] = false;
Kojto 111:4336505e4b1c 1061 config->enable_capture_on_channel[TC_COMPARE_CAPTURE_CHANNEL_1] = false;
Kojto 111:4336505e4b1c 1062 #ifdef FEATURE_TC_IO_CAPTURE
Kojto 111:4336505e4b1c 1063 config->enable_capture_on_IO[TC_COMPARE_CAPTURE_CHANNEL_0] = false;
Kojto 111:4336505e4b1c 1064 config->enable_capture_on_IO[TC_COMPARE_CAPTURE_CHANNEL_1] = false;
Kojto 111:4336505e4b1c 1065 #endif
Kojto 111:4336505e4b1c 1066
Kojto 111:4336505e4b1c 1067 config->count_direction = TC_COUNT_DIRECTION_UP;
Kojto 111:4336505e4b1c 1068 config->oneshot = false;
Kojto 111:4336505e4b1c 1069
Kojto 111:4336505e4b1c 1070 config->pwm_channel[TC_COMPARE_CAPTURE_CHANNEL_0].enabled = false;
Kojto 111:4336505e4b1c 1071 config->pwm_channel[TC_COMPARE_CAPTURE_CHANNEL_0].pin_out = 0;
Kojto 111:4336505e4b1c 1072 config->pwm_channel[TC_COMPARE_CAPTURE_CHANNEL_0].pin_mux = 0;
Kojto 111:4336505e4b1c 1073
Kojto 111:4336505e4b1c 1074 config->pwm_channel[TC_COMPARE_CAPTURE_CHANNEL_1].enabled = false;
Kojto 111:4336505e4b1c 1075 config->pwm_channel[TC_COMPARE_CAPTURE_CHANNEL_1].pin_out = 0;
Kojto 111:4336505e4b1c 1076 config->pwm_channel[TC_COMPARE_CAPTURE_CHANNEL_1].pin_mux = 0;
Kojto 111:4336505e4b1c 1077
Kojto 111:4336505e4b1c 1078 config->counter_16_bit.value = 0x0000;
Kojto 111:4336505e4b1c 1079 config->counter_16_bit.compare_capture_channel\
Kojto 111:4336505e4b1c 1080 [TC_COMPARE_CAPTURE_CHANNEL_0] = 0x0000;
Kojto 111:4336505e4b1c 1081 config->counter_16_bit.compare_capture_channel\
Kojto 111:4336505e4b1c 1082 [TC_COMPARE_CAPTURE_CHANNEL_1] = 0x0000;
Kojto 111:4336505e4b1c 1083 #ifdef FEATURE_TC_DOUBLE_BUFFERED
Kojto 111:4336505e4b1c 1084 config->double_buffering_enabled = false;
Kojto 111:4336505e4b1c 1085 #endif
Kojto 111:4336505e4b1c 1086
Kojto 111:4336505e4b1c 1087 }
Kojto 111:4336505e4b1c 1088
Kojto 111:4336505e4b1c 1089 enum status_code tc_init(
Kojto 111:4336505e4b1c 1090 struct tc_module *const module_inst,
Kojto 111:4336505e4b1c 1091 Tc *const hw,
Kojto 111:4336505e4b1c 1092 const struct tc_config *const config);
Kojto 111:4336505e4b1c 1093
Kojto 111:4336505e4b1c 1094 /** @} */
Kojto 111:4336505e4b1c 1095
Kojto 111:4336505e4b1c 1096 /**
Kojto 111:4336505e4b1c 1097 * \name Event Management
Kojto 111:4336505e4b1c 1098 * @{
Kojto 111:4336505e4b1c 1099 */
Kojto 111:4336505e4b1c 1100
Kojto 111:4336505e4b1c 1101 /**
Kojto 111:4336505e4b1c 1102 * \brief Enables a TC module event input or output.
Kojto 111:4336505e4b1c 1103 *
Kojto 111:4336505e4b1c 1104 * Enables one or more input or output events to or from the TC module.
Kojto 111:4336505e4b1c 1105 * See \ref tc_events for a list of events this module supports.
Kojto 111:4336505e4b1c 1106 *
Kojto 111:4336505e4b1c 1107 * \note Events cannot be altered while the module is enabled.
Kojto 111:4336505e4b1c 1108 *
Kojto 111:4336505e4b1c 1109 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1110 * \param[in] events Struct containing flags of events to enable
Kojto 111:4336505e4b1c 1111 */
Kojto 111:4336505e4b1c 1112 static inline void tc_enable_events(
Kojto 111:4336505e4b1c 1113 struct tc_module *const module_inst,
Kojto 111:4336505e4b1c 1114 struct tc_events *const events)
Kojto 111:4336505e4b1c 1115 {
Kojto 111:4336505e4b1c 1116 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1117 Assert(module_inst);
Kojto 111:4336505e4b1c 1118 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1119 Assert(events);
Kojto 111:4336505e4b1c 1120
Kojto 111:4336505e4b1c 1121 Tc *const tc_module = module_inst->hw;
Kojto 111:4336505e4b1c 1122
Kojto 111:4336505e4b1c 1123 uint32_t event_mask = 0;
Kojto 111:4336505e4b1c 1124
Kojto 111:4336505e4b1c 1125 if (events->invert_event_input == true) {
Kojto 111:4336505e4b1c 1126 event_mask |= TC_EVCTRL_TCINV;
Kojto 111:4336505e4b1c 1127 }
Kojto 111:4336505e4b1c 1128
Kojto 111:4336505e4b1c 1129 if (events->on_event_perform_action == true) {
Kojto 111:4336505e4b1c 1130 event_mask |= TC_EVCTRL_TCEI;
Kojto 111:4336505e4b1c 1131 }
Kojto 111:4336505e4b1c 1132
Kojto 111:4336505e4b1c 1133 if (events->generate_event_on_overflow == true) {
Kojto 111:4336505e4b1c 1134 event_mask |= TC_EVCTRL_OVFEO;
Kojto 111:4336505e4b1c 1135 }
Kojto 111:4336505e4b1c 1136
Kojto 111:4336505e4b1c 1137 for (uint8_t i = 0; i < NUMBER_OF_COMPARE_CAPTURE_CHANNELS; i++) {
Kojto 111:4336505e4b1c 1138 if (events->generate_event_on_compare_channel[i] == true) {
Kojto 111:4336505e4b1c 1139 event_mask |= (TC_EVCTRL_MCEO(1) << i);
Kojto 111:4336505e4b1c 1140 }
Kojto 111:4336505e4b1c 1141 }
Kojto 111:4336505e4b1c 1142
Kojto 111:4336505e4b1c 1143 tc_module->COUNT8.EVCTRL.reg |= event_mask | events->event_action;
Kojto 111:4336505e4b1c 1144 }
Kojto 111:4336505e4b1c 1145
Kojto 111:4336505e4b1c 1146 /**
Kojto 111:4336505e4b1c 1147 * \brief Disables a TC module event input or output.
Kojto 111:4336505e4b1c 1148 *
Kojto 111:4336505e4b1c 1149 * Disables one or more input or output events to or from the TC module.
Kojto 111:4336505e4b1c 1150 * See \ref tc_events for a list of events this module supports.
Kojto 111:4336505e4b1c 1151 *
Kojto 111:4336505e4b1c 1152 * \note Events cannot be altered while the module is enabled.
Kojto 111:4336505e4b1c 1153 *
Kojto 111:4336505e4b1c 1154 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1155 * \param[in] events Struct containing flags of events to disable
Kojto 111:4336505e4b1c 1156 */
Kojto 111:4336505e4b1c 1157 static inline void tc_disable_events(
Kojto 111:4336505e4b1c 1158 struct tc_module *const module_inst,
Kojto 111:4336505e4b1c 1159 struct tc_events *const events)
Kojto 111:4336505e4b1c 1160 {
Kojto 111:4336505e4b1c 1161 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1162 Assert(module_inst);
Kojto 111:4336505e4b1c 1163 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1164 Assert(events);
Kojto 111:4336505e4b1c 1165
Kojto 111:4336505e4b1c 1166 Tc *const tc_module = module_inst->hw;
Kojto 111:4336505e4b1c 1167
Kojto 111:4336505e4b1c 1168 uint32_t event_mask = 0;
Kojto 111:4336505e4b1c 1169
Kojto 111:4336505e4b1c 1170 if (events->invert_event_input == true) {
Kojto 111:4336505e4b1c 1171 event_mask |= TC_EVCTRL_TCINV;
Kojto 111:4336505e4b1c 1172 }
Kojto 111:4336505e4b1c 1173
Kojto 111:4336505e4b1c 1174 if (events->on_event_perform_action == true) {
Kojto 111:4336505e4b1c 1175 event_mask |= TC_EVCTRL_TCEI;
Kojto 111:4336505e4b1c 1176 }
Kojto 111:4336505e4b1c 1177
Kojto 111:4336505e4b1c 1178 if (events->generate_event_on_overflow == true) {
Kojto 111:4336505e4b1c 1179 event_mask |= TC_EVCTRL_OVFEO;
Kojto 111:4336505e4b1c 1180 }
Kojto 111:4336505e4b1c 1181
Kojto 111:4336505e4b1c 1182 for (uint8_t i = 0; i < NUMBER_OF_COMPARE_CAPTURE_CHANNELS; i++) {
Kojto 111:4336505e4b1c 1183 if (events->generate_event_on_compare_channel[i] == true) {
Kojto 111:4336505e4b1c 1184 event_mask |= (TC_EVCTRL_MCEO(1) << i);
Kojto 111:4336505e4b1c 1185 }
Kojto 111:4336505e4b1c 1186 }
Kojto 111:4336505e4b1c 1187
Kojto 111:4336505e4b1c 1188 tc_module->COUNT8.EVCTRL.reg &= ~event_mask;
Kojto 111:4336505e4b1c 1189 }
Kojto 111:4336505e4b1c 1190
Kojto 111:4336505e4b1c 1191 /** @} */
Kojto 111:4336505e4b1c 1192
Kojto 111:4336505e4b1c 1193 /**
Kojto 111:4336505e4b1c 1194 * \name Enable/Disable/Reset
Kojto 111:4336505e4b1c 1195 * @{
Kojto 111:4336505e4b1c 1196 */
Kojto 111:4336505e4b1c 1197
Kojto 111:4336505e4b1c 1198 enum status_code tc_reset(
Kojto 111:4336505e4b1c 1199 const struct tc_module *const module_inst);
Kojto 111:4336505e4b1c 1200
Kojto 111:4336505e4b1c 1201 /**
Kojto 111:4336505e4b1c 1202 * \brief Enable the TC module.
Kojto 111:4336505e4b1c 1203 *
Kojto 111:4336505e4b1c 1204 * Enables a TC module that has been previously initialized. The counter will
Kojto 111:4336505e4b1c 1205 * start when the counter is enabled.
Kojto 111:4336505e4b1c 1206 *
Kojto 111:4336505e4b1c 1207 * \note When the counter is configured to re-trigger on an event, the counter
Kojto 111:4336505e4b1c 1208 * will not start until the start function is used.
Kojto 111:4336505e4b1c 1209 *
Kojto 111:4336505e4b1c 1210 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1211 */
Kojto 111:4336505e4b1c 1212 static inline void tc_enable(
Kojto 111:4336505e4b1c 1213 const struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 1214 {
Kojto 111:4336505e4b1c 1215 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1216 Assert(module_inst);
Kojto 111:4336505e4b1c 1217 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1218
Kojto 111:4336505e4b1c 1219 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1220 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1221
Kojto 111:4336505e4b1c 1222 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1223 /* Wait for sync */
Kojto 111:4336505e4b1c 1224 }
Kojto 111:4336505e4b1c 1225
Kojto 111:4336505e4b1c 1226 /* Enable TC module */
Kojto 111:4336505e4b1c 1227 tc_module->CTRLA.reg |= TC_CTRLA_ENABLE;
Kojto 111:4336505e4b1c 1228 }
Kojto 111:4336505e4b1c 1229
Kojto 111:4336505e4b1c 1230 /**
Kojto 111:4336505e4b1c 1231 * \brief Disables the TC module.
Kojto 111:4336505e4b1c 1232 *
Kojto 111:4336505e4b1c 1233 * Disables a TC module and stops the counter.
Kojto 111:4336505e4b1c 1234 *
Kojto 111:4336505e4b1c 1235 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1236 */
Kojto 111:4336505e4b1c 1237 static inline void tc_disable(
Kojto 111:4336505e4b1c 1238 const struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 1239 {
Kojto 111:4336505e4b1c 1240 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1241 Assert(module_inst);
Kojto 111:4336505e4b1c 1242 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1243
Kojto 111:4336505e4b1c 1244 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1245 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1246
Kojto 111:4336505e4b1c 1247 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1248 /* Wait for sync */
Kojto 111:4336505e4b1c 1249 }
Kojto 111:4336505e4b1c 1250
Kojto 111:4336505e4b1c 1251 /* Disable TC module */
Kojto 111:4336505e4b1c 1252 tc_module->CTRLA.reg &= ~TC_CTRLA_ENABLE;
Kojto 111:4336505e4b1c 1253 }
Kojto 111:4336505e4b1c 1254
Kojto 111:4336505e4b1c 1255 /** @} */
Kojto 111:4336505e4b1c 1256
Kojto 111:4336505e4b1c 1257 /**
Kojto 111:4336505e4b1c 1258 * \name Get/Set Count Value
Kojto 111:4336505e4b1c 1259 * @{
Kojto 111:4336505e4b1c 1260 */
Kojto 111:4336505e4b1c 1261
Kojto 111:4336505e4b1c 1262 uint32_t tc_get_count_value(
Kojto 111:4336505e4b1c 1263 const struct tc_module *const module_inst);
Kojto 111:4336505e4b1c 1264
Kojto 111:4336505e4b1c 1265 enum status_code tc_set_count_value(
Kojto 111:4336505e4b1c 1266 const struct tc_module *const module_inst,
Kojto 111:4336505e4b1c 1267 const uint32_t count);
Kojto 111:4336505e4b1c 1268
Kojto 111:4336505e4b1c 1269 /** @} */
Kojto 111:4336505e4b1c 1270
Kojto 111:4336505e4b1c 1271 /**
Kojto 111:4336505e4b1c 1272 * \name Start/Stop Counter
Kojto 111:4336505e4b1c 1273 * @{
Kojto 111:4336505e4b1c 1274 */
Kojto 111:4336505e4b1c 1275
Kojto 111:4336505e4b1c 1276 /**
Kojto 111:4336505e4b1c 1277 * \brief Stops the counter.
Kojto 111:4336505e4b1c 1278 *
Kojto 111:4336505e4b1c 1279 * This function will stop the counter. When the counter is stopped
Kojto 111:4336505e4b1c 1280 * the value in the count value is set to 0 if the counter was
Kojto 111:4336505e4b1c 1281 * counting up, or maximum if the counter was counting
Kojto 111:4336505e4b1c 1282 * down when stopped.
Kojto 111:4336505e4b1c 1283 *
Kojto 111:4336505e4b1c 1284 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1285 */
Kojto 111:4336505e4b1c 1286 static inline void tc_stop_counter(
Kojto 111:4336505e4b1c 1287 const struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 1288 {
Kojto 111:4336505e4b1c 1289 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1290 Assert(module_inst);
Kojto 111:4336505e4b1c 1291 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1292
Kojto 111:4336505e4b1c 1293 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1294 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1295
Kojto 111:4336505e4b1c 1296 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1297 /* Wait for sync */
Kojto 111:4336505e4b1c 1298 }
Kojto 111:4336505e4b1c 1299
Kojto 111:4336505e4b1c 1300 /* Write command to execute */
Kojto 111:4336505e4b1c 1301 tc_module->CTRLBSET.reg = TC_CTRLBSET_CMD(TC_CTRLBSET_CMD_STOP_Val);
Kojto 111:4336505e4b1c 1302 }
Kojto 111:4336505e4b1c 1303
Kojto 111:4336505e4b1c 1304 /**
Kojto 111:4336505e4b1c 1305 * \brief Starts the counter.
Kojto 111:4336505e4b1c 1306 *
Kojto 111:4336505e4b1c 1307 * Starts or restarts an initialized TC module's counter.
Kojto 111:4336505e4b1c 1308 *
Kojto 111:4336505e4b1c 1309 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1310 */
Kojto 111:4336505e4b1c 1311 static inline void tc_start_counter(
Kojto 111:4336505e4b1c 1312 const struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 1313 {
Kojto 111:4336505e4b1c 1314 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1315 Assert(module_inst);
Kojto 111:4336505e4b1c 1316 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1317
Kojto 111:4336505e4b1c 1318 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1319 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1320
Kojto 111:4336505e4b1c 1321 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1322 /* Wait for sync */
Kojto 111:4336505e4b1c 1323 }
Kojto 111:4336505e4b1c 1324
Kojto 111:4336505e4b1c 1325 /* Make certain that there are no conflicting commands in the register */
Kojto 111:4336505e4b1c 1326 tc_module->CTRLBCLR.reg = TC_CTRLBCLR_CMD_NONE;
Kojto 111:4336505e4b1c 1327
Kojto 111:4336505e4b1c 1328 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1329 /* Wait for sync */
Kojto 111:4336505e4b1c 1330 }
Kojto 111:4336505e4b1c 1331
Kojto 111:4336505e4b1c 1332 /* Write command to execute */
Kojto 111:4336505e4b1c 1333 tc_module->CTRLBSET.reg = TC_CTRLBSET_CMD(TC_CTRLBSET_CMD_RETRIGGER_Val);
Kojto 111:4336505e4b1c 1334 }
Kojto 111:4336505e4b1c 1335
Kojto 111:4336505e4b1c 1336 /** @} */
Kojto 111:4336505e4b1c 1337
Kojto 111:4336505e4b1c 1338 #ifdef FEATURE_TC_DOUBLE_BUFFERED
Kojto 111:4336505e4b1c 1339 /**
Kojto 111:4336505e4b1c 1340 * \name Double Buffering
Kojto 111:4336505e4b1c 1341 * @{
Kojto 111:4336505e4b1c 1342 */
Kojto 111:4336505e4b1c 1343
Kojto 111:4336505e4b1c 1344 /**
Kojto 111:4336505e4b1c 1345 * \brief Update double buffer.
Kojto 111:4336505e4b1c 1346 *
Kojto 111:4336505e4b1c 1347 * Update double buffer.
Kojto 111:4336505e4b1c 1348 *
Kojto 111:4336505e4b1c 1349 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1350 */
Kojto 111:4336505e4b1c 1351 static inline void tc_update_double_buffer(
Kojto 111:4336505e4b1c 1352 const struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 1353 {
Kojto 111:4336505e4b1c 1354 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1355 Assert(module_inst);
Kojto 111:4336505e4b1c 1356 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1357
Kojto 111:4336505e4b1c 1358 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1359 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1360
Kojto 111:4336505e4b1c 1361 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1362 /* Wait for sync */
Kojto 111:4336505e4b1c 1363 }
Kojto 111:4336505e4b1c 1364
Kojto 111:4336505e4b1c 1365 /* Make certain that there are no conflicting commands in the register */
Kojto 111:4336505e4b1c 1366 tc_module->CTRLBCLR.reg = TC_CTRLBCLR_CMD_NONE;
Kojto 111:4336505e4b1c 1367
Kojto 111:4336505e4b1c 1368 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1369 /* Wait for sync */
Kojto 111:4336505e4b1c 1370 }
Kojto 111:4336505e4b1c 1371
Kojto 111:4336505e4b1c 1372 /* Write command to execute */
Kojto 111:4336505e4b1c 1373 tc_module->CTRLBSET.reg = TC_CTRLBSET_CMD(TC_CTRLBSET_CMD_UPDATE_Val);
Kojto 111:4336505e4b1c 1374 }
Kojto 111:4336505e4b1c 1375 /** @} */
Kojto 111:4336505e4b1c 1376 #endif
Kojto 111:4336505e4b1c 1377
Kojto 111:4336505e4b1c 1378 #ifdef FEATURE_TC_READ_SYNC
Kojto 111:4336505e4b1c 1379 /**
Kojto 111:4336505e4b1c 1380 * \name Count Read Synchronization
Kojto 111:4336505e4b1c 1381 * @{
Kojto 111:4336505e4b1c 1382 */
Kojto 111:4336505e4b1c 1383
Kojto 111:4336505e4b1c 1384 /**
Kojto 111:4336505e4b1c 1385 * \brief Read synchronization of COUNT.
Kojto 111:4336505e4b1c 1386 *
Kojto 111:4336505e4b1c 1387 * Read synchronization of COUNT.
Kojto 111:4336505e4b1c 1388 *
Kojto 111:4336505e4b1c 1389 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1390 */
Kojto 111:4336505e4b1c 1391 static inline void tc_sync_read_count(
Kojto 111:4336505e4b1c 1392 const struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 1393 {
Kojto 111:4336505e4b1c 1394 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1395 Assert(module_inst);
Kojto 111:4336505e4b1c 1396 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1397
Kojto 111:4336505e4b1c 1398 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1399 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1400
Kojto 111:4336505e4b1c 1401 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1402 /* Wait for sync */
Kojto 111:4336505e4b1c 1403 }
Kojto 111:4336505e4b1c 1404
Kojto 111:4336505e4b1c 1405 /* Make certain that there are no conflicting commands in the register */
Kojto 111:4336505e4b1c 1406 tc_module->CTRLBCLR.reg = TC_CTRLBCLR_CMD_NONE;
Kojto 111:4336505e4b1c 1407
Kojto 111:4336505e4b1c 1408 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1409 /* Wait for sync */
Kojto 111:4336505e4b1c 1410 }
Kojto 111:4336505e4b1c 1411
Kojto 111:4336505e4b1c 1412 /* Write command to execute */
Kojto 111:4336505e4b1c 1413 tc_module->CTRLBSET.reg = TC_CTRLBSET_CMD(TC_CTRLBSET_CMD_READSYNC_Val);
Kojto 111:4336505e4b1c 1414 }
Kojto 111:4336505e4b1c 1415 /** @} */
Kojto 111:4336505e4b1c 1416 #endif
Kojto 111:4336505e4b1c 1417
Kojto 111:4336505e4b1c 1418 #ifdef FEATURE_TC_GENERATE_DMA_TRIGGER
Kojto 111:4336505e4b1c 1419 /**
Kojto 111:4336505e4b1c 1420 * \name Generate TC DMA Triggers command
Kojto 111:4336505e4b1c 1421 * @{
Kojto 111:4336505e4b1c 1422 */
Kojto 111:4336505e4b1c 1423
Kojto 111:4336505e4b1c 1424 /**
Kojto 111:4336505e4b1c 1425 * \brief TC DMA Trigger.
Kojto 111:4336505e4b1c 1426 *
Kojto 111:4336505e4b1c 1427 * TC DMA trigger command.
Kojto 111:4336505e4b1c 1428 *
Kojto 111:4336505e4b1c 1429 * \param[in] module_inst Pointer to the software module instance struct
Kojto 111:4336505e4b1c 1430 */
Kojto 111:4336505e4b1c 1431 static inline void tc_dma_trigger_command(
Kojto 111:4336505e4b1c 1432 const struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 1433 {
Kojto 111:4336505e4b1c 1434 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1435 Assert(module_inst);
Kojto 111:4336505e4b1c 1436 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1437
Kojto 111:4336505e4b1c 1438 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1439 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1440
Kojto 111:4336505e4b1c 1441 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1442 /* Wait for sync */
Kojto 111:4336505e4b1c 1443 }
Kojto 111:4336505e4b1c 1444
Kojto 111:4336505e4b1c 1445 /* Make certain that there are no conflicting commands in the register */
Kojto 111:4336505e4b1c 1446 tc_module->CTRLBCLR.reg = TC_CTRLBCLR_CMD_NONE;
Kojto 111:4336505e4b1c 1447
Kojto 111:4336505e4b1c 1448 while (tc_is_syncing(module_inst)) {
Kojto 111:4336505e4b1c 1449 /* Wait for sync */
Kojto 111:4336505e4b1c 1450 }
Kojto 111:4336505e4b1c 1451
Kojto 111:4336505e4b1c 1452 #if SAML21
Kojto 111:4336505e4b1c 1453 /* Write command to execute */
Kojto 111:4336505e4b1c 1454 tc_module->CTRLBSET.reg = TC_CTRLBSET_CMD(TC_CTRLBSET_CMD_DMATRG_Val);
Kojto 111:4336505e4b1c 1455 #endif
Kojto 111:4336505e4b1c 1456 #if (SAMC20) || (SAMC21)
Kojto 111:4336505e4b1c 1457 /* Write command to execute */
Kojto 111:4336505e4b1c 1458 tc_module->CTRLBSET.reg = TC_CTRLBSET_CMD(TC_CTRLBSET_CMD_DMAOS_Val);
Kojto 111:4336505e4b1c 1459 #endif
Kojto 111:4336505e4b1c 1460 }
Kojto 111:4336505e4b1c 1461 /** @} */
Kojto 111:4336505e4b1c 1462 #endif
Kojto 111:4336505e4b1c 1463
Kojto 111:4336505e4b1c 1464 /**
Kojto 111:4336505e4b1c 1465 * \name Get Capture Set Compare
Kojto 111:4336505e4b1c 1466 * @{
Kojto 111:4336505e4b1c 1467 */
Kojto 111:4336505e4b1c 1468
Kojto 111:4336505e4b1c 1469 uint32_t tc_get_capture_value(
Kojto 111:4336505e4b1c 1470 const struct tc_module *const module_inst,
Kojto 111:4336505e4b1c 1471 const enum tc_compare_capture_channel channel_index);
Kojto 111:4336505e4b1c 1472
Kojto 111:4336505e4b1c 1473 enum status_code tc_set_compare_value(
Kojto 111:4336505e4b1c 1474 const struct tc_module *const module_inst,
Kojto 111:4336505e4b1c 1475 const enum tc_compare_capture_channel channel_index,
Kojto 111:4336505e4b1c 1476 const uint32_t compare_value);
Kojto 111:4336505e4b1c 1477
Kojto 111:4336505e4b1c 1478 /** @} */
Kojto 111:4336505e4b1c 1479
Kojto 111:4336505e4b1c 1480 /**
Kojto 111:4336505e4b1c 1481 * \name Set Top Value
Kojto 111:4336505e4b1c 1482 * @{
Kojto 111:4336505e4b1c 1483 */
Kojto 111:4336505e4b1c 1484
Kojto 111:4336505e4b1c 1485 enum status_code tc_set_top_value(
Kojto 111:4336505e4b1c 1486 const struct tc_module *const module_inst,
Kojto 111:4336505e4b1c 1487 const uint32_t top_value);
Kojto 111:4336505e4b1c 1488
Kojto 111:4336505e4b1c 1489 /** @} */
Kojto 111:4336505e4b1c 1490
Kojto 111:4336505e4b1c 1491 /**
Kojto 111:4336505e4b1c 1492 * \name Status Management
Kojto 111:4336505e4b1c 1493 * @{
Kojto 111:4336505e4b1c 1494 */
Kojto 111:4336505e4b1c 1495
Kojto 111:4336505e4b1c 1496 /**
Kojto 111:4336505e4b1c 1497 * \brief Retrieves the current module status.
Kojto 111:4336505e4b1c 1498 *
Kojto 111:4336505e4b1c 1499 * Retrieves the status of the module, giving overall state information.
Kojto 111:4336505e4b1c 1500 *
Kojto 111:4336505e4b1c 1501 * \param[in] module_inst Pointer to the TC software instance struct
Kojto 111:4336505e4b1c 1502 *
Kojto 111:4336505e4b1c 1503 * \return Bitmask of \c TC_STATUS_* flags.
Kojto 111:4336505e4b1c 1504 *
Kojto 111:4336505e4b1c 1505 * \retval TC_STATUS_CHANNEL_0_MATCH Timer channel 0 compare/capture match
Kojto 111:4336505e4b1c 1506 * \retval TC_STATUS_CHANNEL_1_MATCH Timer channel 1 compare/capture match
Kojto 111:4336505e4b1c 1507 * \retval TC_STATUS_SYNC_READY Timer read synchronization has completed
Kojto 111:4336505e4b1c 1508 * \retval TC_STATUS_CAPTURE_OVERFLOW Timer capture data has overflowed
Kojto 111:4336505e4b1c 1509 * \retval TC_STATUS_COUNT_OVERFLOW Timer count value has overflowed
Kojto 111:4336505e4b1c 1510 * \retval TC_STATUS_CHN0_BUFFER_VALID Timer count channel 0 compare/capture buffer valid
Kojto 111:4336505e4b1c 1511 * \retval TC_STATUS_CHN1_BUFFER_VALID Timer count channel 1 compare/capture buffer valid
Kojto 111:4336505e4b1c 1512 * \retval TC_STATUS_PERIOD_BUFFER_VALID Timer count period buffer valid
Kojto 111:4336505e4b1c 1513 */
Kojto 111:4336505e4b1c 1514 static inline uint32_t tc_get_status(
Kojto 111:4336505e4b1c 1515 struct tc_module *const module_inst)
Kojto 111:4336505e4b1c 1516 {
Kojto 111:4336505e4b1c 1517 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1518 Assert(module_inst);
Kojto 111:4336505e4b1c 1519 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1520
Kojto 111:4336505e4b1c 1521 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1522 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1523
Kojto 111:4336505e4b1c 1524 uint32_t int_flags = tc_module->INTFLAG.reg;
Kojto 111:4336505e4b1c 1525
Kojto 111:4336505e4b1c 1526 uint32_t status_flags = 0;
Kojto 111:4336505e4b1c 1527
Kojto 111:4336505e4b1c 1528 /* Check for TC channel 0 match */
Kojto 111:4336505e4b1c 1529 if (int_flags & TC_INTFLAG_MC(1)) {
Kojto 111:4336505e4b1c 1530 status_flags |= TC_STATUS_CHANNEL_0_MATCH;
Kojto 111:4336505e4b1c 1531 }
Kojto 111:4336505e4b1c 1532
Kojto 111:4336505e4b1c 1533 /* Check for TC channel 1 match */
Kojto 111:4336505e4b1c 1534 if (int_flags & TC_INTFLAG_MC(2)) {
Kojto 111:4336505e4b1c 1535 status_flags |= TC_STATUS_CHANNEL_1_MATCH;
Kojto 111:4336505e4b1c 1536 }
Kojto 111:4336505e4b1c 1537
Kojto 111:4336505e4b1c 1538 #if !defined(FEATURE_TC_SYNCBUSY_SCHEME_VERSION_2)
Kojto 111:4336505e4b1c 1539 /* Check for TC read synchronization ready */
Kojto 111:4336505e4b1c 1540 if (int_flags & TC_INTFLAG_SYNCRDY) {
Kojto 111:4336505e4b1c 1541 status_flags |= TC_STATUS_SYNC_READY;
Kojto 111:4336505e4b1c 1542 }
Kojto 111:4336505e4b1c 1543 #endif
Kojto 111:4336505e4b1c 1544
Kojto 111:4336505e4b1c 1545 /* Check for TC capture overflow */
Kojto 111:4336505e4b1c 1546 if (int_flags & TC_INTFLAG_ERR) {
Kojto 111:4336505e4b1c 1547 status_flags |= TC_STATUS_CAPTURE_OVERFLOW;
Kojto 111:4336505e4b1c 1548 }
Kojto 111:4336505e4b1c 1549
Kojto 111:4336505e4b1c 1550 /* Check for TC count overflow */
Kojto 111:4336505e4b1c 1551 if (int_flags & TC_INTFLAG_OVF) {
Kojto 111:4336505e4b1c 1552 status_flags |= TC_STATUS_COUNT_OVERFLOW;
Kojto 111:4336505e4b1c 1553 }
Kojto 111:4336505e4b1c 1554 #ifdef FEATURE_TC_DOUBLE_BUFFERED
Kojto 111:4336505e4b1c 1555 uint8_t double_buffer_valid_status = tc_module->STATUS.reg;
Kojto 111:4336505e4b1c 1556
Kojto 111:4336505e4b1c 1557 /* Check channel 0 compare or capture buffer valid */
Kojto 111:4336505e4b1c 1558 if (double_buffer_valid_status & TC_STATUS_CCBUFV0) {
Kojto 111:4336505e4b1c 1559 status_flags |= TC_STATUS_CHN0_BUFFER_VALID;
Kojto 111:4336505e4b1c 1560 }
Kojto 111:4336505e4b1c 1561 /* Check channel 0 compare or capture buffer valid */
Kojto 111:4336505e4b1c 1562 if (double_buffer_valid_status & TC_STATUS_CCBUFV1) {
Kojto 111:4336505e4b1c 1563 status_flags |= TC_STATUS_CHN1_BUFFER_VALID;
Kojto 111:4336505e4b1c 1564 }
Kojto 111:4336505e4b1c 1565 /* Check period buffer valid */
Kojto 111:4336505e4b1c 1566 if (double_buffer_valid_status & TC_STATUS_PERBUFV) {
Kojto 111:4336505e4b1c 1567 status_flags |= TC_STATUS_PERIOD_BUFFER_VALID;
Kojto 111:4336505e4b1c 1568 }
Kojto 111:4336505e4b1c 1569 #endif
Kojto 111:4336505e4b1c 1570
Kojto 111:4336505e4b1c 1571 return status_flags;
Kojto 111:4336505e4b1c 1572 }
Kojto 111:4336505e4b1c 1573
Kojto 111:4336505e4b1c 1574 /**
Kojto 111:4336505e4b1c 1575 * \brief Clears a module status flag.
Kojto 111:4336505e4b1c 1576 *
Kojto 111:4336505e4b1c 1577 * Clears the given status flag of the module.
Kojto 111:4336505e4b1c 1578 *
Kojto 111:4336505e4b1c 1579 * \param[in] module_inst Pointer to the TC software instance struct
Kojto 111:4336505e4b1c 1580 * \param[in] status_flags Bitmask of \c TC_STATUS_* flags to clear
Kojto 111:4336505e4b1c 1581 */
Kojto 111:4336505e4b1c 1582 static inline void tc_clear_status(
Kojto 111:4336505e4b1c 1583 struct tc_module *const module_inst,
Kojto 111:4336505e4b1c 1584 const uint32_t status_flags)
Kojto 111:4336505e4b1c 1585 {
Kojto 111:4336505e4b1c 1586 /* Sanity check arguments */
Kojto 111:4336505e4b1c 1587 Assert(module_inst);
Kojto 111:4336505e4b1c 1588 Assert(module_inst->hw);
Kojto 111:4336505e4b1c 1589
Kojto 111:4336505e4b1c 1590 /* Get a pointer to the module's hardware instance */
Kojto 111:4336505e4b1c 1591 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
Kojto 111:4336505e4b1c 1592
Kojto 111:4336505e4b1c 1593 uint32_t int_flags = 0;
Kojto 111:4336505e4b1c 1594
Kojto 111:4336505e4b1c 1595 /* Check for TC channel 0 match */
Kojto 111:4336505e4b1c 1596 if (status_flags & TC_STATUS_CHANNEL_0_MATCH) {
Kojto 111:4336505e4b1c 1597 int_flags |= TC_INTFLAG_MC(1);
Kojto 111:4336505e4b1c 1598 }
Kojto 111:4336505e4b1c 1599
Kojto 111:4336505e4b1c 1600 /* Check for TC channel 1 match */
Kojto 111:4336505e4b1c 1601 if (status_flags & TC_STATUS_CHANNEL_1_MATCH) {
Kojto 111:4336505e4b1c 1602 int_flags |= TC_INTFLAG_MC(2);
Kojto 111:4336505e4b1c 1603 }
Kojto 111:4336505e4b1c 1604
Kojto 111:4336505e4b1c 1605 #if !defined(FEATURE_TC_SYNCBUSY_SCHEME_VERSION_2)
Kojto 111:4336505e4b1c 1606 /* Check for TC read synchronization ready */
Kojto 111:4336505e4b1c 1607 if (status_flags & TC_STATUS_SYNC_READY) {
Kojto 111:4336505e4b1c 1608 int_flags |= TC_INTFLAG_SYNCRDY;
Kojto 111:4336505e4b1c 1609 }
Kojto 111:4336505e4b1c 1610 #endif
Kojto 111:4336505e4b1c 1611
Kojto 111:4336505e4b1c 1612 /* Check for TC capture overflow */
Kojto 111:4336505e4b1c 1613 if (status_flags & TC_STATUS_CAPTURE_OVERFLOW) {
Kojto 111:4336505e4b1c 1614 int_flags |= TC_INTFLAG_ERR;
Kojto 111:4336505e4b1c 1615 }
Kojto 111:4336505e4b1c 1616
Kojto 111:4336505e4b1c 1617 /* Check for TC count overflow */
Kojto 111:4336505e4b1c 1618 if (status_flags & TC_STATUS_COUNT_OVERFLOW) {
Kojto 111:4336505e4b1c 1619 int_flags |= TC_INTFLAG_OVF;
Kojto 111:4336505e4b1c 1620 }
Kojto 111:4336505e4b1c 1621
Kojto 111:4336505e4b1c 1622 /* Clear interrupt flag */
Kojto 111:4336505e4b1c 1623 tc_module->INTFLAG.reg = int_flags;
Kojto 111:4336505e4b1c 1624 }
Kojto 111:4336505e4b1c 1625
Kojto 111:4336505e4b1c 1626 /** @} */
Kojto 111:4336505e4b1c 1627
Kojto 111:4336505e4b1c 1628 /** @} */
Kojto 111:4336505e4b1c 1629
Kojto 111:4336505e4b1c 1630 #ifdef __cplusplus
Kojto 111:4336505e4b1c 1631 }
Kojto 111:4336505e4b1c 1632 #endif
Kojto 111:4336505e4b1c 1633
Kojto 111:4336505e4b1c 1634 /**
Kojto 111:4336505e4b1c 1635 * \page asfdoc_sam0_tc_extra Extra Information for TC Driver
Kojto 111:4336505e4b1c 1636 *
Kojto 111:4336505e4b1c 1637 * \section asfdoc_sam0_tc_extra_acronyms Acronyms
Kojto 111:4336505e4b1c 1638 * The table below presents the acronyms used in this module:
Kojto 111:4336505e4b1c 1639 *
Kojto 111:4336505e4b1c 1640 * <table>
Kojto 111:4336505e4b1c 1641 * <tr>
Kojto 111:4336505e4b1c 1642 * <th>Acronym</th>
Kojto 111:4336505e4b1c 1643 * <th>Description</th>
Kojto 111:4336505e4b1c 1644 * </tr>
Kojto 111:4336505e4b1c 1645 * <tr>
Kojto 111:4336505e4b1c 1646 * <td>DMA</td>
Kojto 111:4336505e4b1c 1647 * <td>Direct Memory Access</td>
Kojto 111:4336505e4b1c 1648 * </tr>
Kojto 111:4336505e4b1c 1649 * <tr>
Kojto 111:4336505e4b1c 1650 * <td>TC</td>
Kojto 111:4336505e4b1c 1651 * <td>Timer Counter</td>
Kojto 111:4336505e4b1c 1652 * </tr>
Kojto 111:4336505e4b1c 1653 * <tr>
Kojto 111:4336505e4b1c 1654 * <td>PWM</td>
Kojto 111:4336505e4b1c 1655 * <td>Pulse Width Modulation</td>
Kojto 111:4336505e4b1c 1656 * </tr>
Kojto 111:4336505e4b1c 1657 * <tr>
Kojto 111:4336505e4b1c 1658 * <td>PWP</td>
Kojto 111:4336505e4b1c 1659 * <td>Pulse Width Period</td>
Kojto 111:4336505e4b1c 1660 * </tr>
Kojto 111:4336505e4b1c 1661 * <tr>
Kojto 111:4336505e4b1c 1662 * <td>PPW</td>
Kojto 111:4336505e4b1c 1663 * <td>Period Pulse Width</td>
Kojto 111:4336505e4b1c 1664 * </tr>
Kojto 111:4336505e4b1c 1665 * </table>
Kojto 111:4336505e4b1c 1666 *
Kojto 111:4336505e4b1c 1667 *
Kojto 111:4336505e4b1c 1668 * \section asfdoc_sam0_tc_extra_dependencies Dependencies
Kojto 111:4336505e4b1c 1669 * This driver has the following dependencies:
Kojto 111:4336505e4b1c 1670 *
Kojto 111:4336505e4b1c 1671 * - \ref asfdoc_sam0_system_pinmux_group "System Pin Multiplexer Driver"
Kojto 111:4336505e4b1c 1672 *
Kojto 111:4336505e4b1c 1673 *
Kojto 111:4336505e4b1c 1674 * \section asfdoc_sam0_tc_extra_errata Errata
Kojto 111:4336505e4b1c 1675 * There are no errata related to this driver.
Kojto 111:4336505e4b1c 1676 *
Kojto 111:4336505e4b1c 1677 *
Kojto 111:4336505e4b1c 1678 * \section asfdoc_sam0_tc_extra_history Module History
Kojto 111:4336505e4b1c 1679 * An overview of the module history is presented in the table below, with
Kojto 111:4336505e4b1c 1680 * details on the enhancements and fixes made to the module since its first
Kojto 111:4336505e4b1c 1681 * release. The current version of this corresponds to the newest version in
Kojto 111:4336505e4b1c 1682 * the table.
Kojto 111:4336505e4b1c 1683 *
Kojto 111:4336505e4b1c 1684 * <table>
Kojto 111:4336505e4b1c 1685 * <tr>
Kojto 111:4336505e4b1c 1686 * <th>Changelog</th>
Kojto 111:4336505e4b1c 1687 * </tr>
Kojto 111:4336505e4b1c 1688 * <tr>
Kojto 111:4336505e4b1c 1689 * <td>Added support for SAMD21 and do some modifications as below:
Kojto 111:4336505e4b1c 1690 * \li Clean up in the configuration structure, the counter size
Kojto 111:4336505e4b1c 1691 * setting specific registers is accessed through the counter_8_bit,
Kojto 111:4336505e4b1c 1692 * counter_16_bit and counter_32_bit structures
Kojto 111:4336505e4b1c 1693 * \li All event related settings moved into the tc_event structure </td>
Kojto 111:4336505e4b1c 1694 * </tr>
Kojto 111:4336505e4b1c 1695 * <tr>
Kojto 111:4336505e4b1c 1696 * <td>Added automatic digital clock interface enable for the slave TC
Kojto 111:4336505e4b1c 1697 * module when a timer is initialized in 32-bit mode</td>
Kojto 111:4336505e4b1c 1698 * </tr>
Kojto 111:4336505e4b1c 1699 * <tr>
Kojto 111:4336505e4b1c 1700 * <td>Initial Release</td>
Kojto 111:4336505e4b1c 1701 * </tr>
Kojto 111:4336505e4b1c 1702 * </table>
Kojto 111:4336505e4b1c 1703 */
Kojto 111:4336505e4b1c 1704
Kojto 111:4336505e4b1c 1705 /**
Kojto 111:4336505e4b1c 1706 * \page asfdoc_sam0_tc_exqsg Examples for TC Driver
Kojto 111:4336505e4b1c 1707 *
Kojto 111:4336505e4b1c 1708 * This is a list of the available Quick Start guides (QSGs) and example
Kojto 111:4336505e4b1c 1709 * applications for \ref asfdoc_sam0_tc_group. QSGs are simple examples with
Kojto 111:4336505e4b1c 1710 * step-by-step instructions to configure and use this driver in a selection of
Kojto 111:4336505e4b1c 1711 * use cases. Note that QSGs can be compiled as a standalone application or be
Kojto 111:4336505e4b1c 1712 * added to the user application.
Kojto 111:4336505e4b1c 1713 *
Kojto 111:4336505e4b1c 1714 * - \subpage asfdoc_sam0_tc_basic_use_case
Kojto 111:4336505e4b1c 1715 * - \subpage asfdoc_sam0_tc_macth_freq_use_case
Kojto 111:4336505e4b1c 1716 * \if TC_CALLBACK_MODE
Kojto 111:4336505e4b1c 1717 * - \subpage asfdoc_sam0_tc_timer_use_case
Kojto 111:4336505e4b1c 1718 * - \subpage asfdoc_sam0_tc_callback_use_case
Kojto 111:4336505e4b1c 1719 * \endif
Kojto 111:4336505e4b1c 1720 * - \subpage asfdoc_sam0_tc_dma_use_case
Kojto 111:4336505e4b1c 1721 *
Kojto 111:4336505e4b1c 1722 * \page asfdoc_sam0_tc_document_revision_history Document Revision History
Kojto 111:4336505e4b1c 1723 *
Kojto 111:4336505e4b1c 1724 * <table>
Kojto 111:4336505e4b1c 1725 * <tr>
Kojto 111:4336505e4b1c 1726 * <th>Doc. Rev.</td>
Kojto 111:4336505e4b1c 1727 * <th>Date</td>
Kojto 111:4336505e4b1c 1728 * <th>Comments</td>
Kojto 111:4336505e4b1c 1729 * </tr>
Kojto 111:4336505e4b1c 1730 * <tr>
Kojto 111:4336505e4b1c 1731 * <td>F</td>
Kojto 111:4336505e4b1c 1732 * <td>12/2014</td>
Kojto 111:4336505e4b1c 1733 * <td>Added support for SAMC21.</td>
Kojto 111:4336505e4b1c 1734 * </tr>
Kojto 111:4336505e4b1c 1735 * <tr>
Kojto 111:4336505e4b1c 1736 * <td>E</td>
Kojto 111:4336505e4b1c 1737 * <td>04/2015</td>
Kojto 111:4336505e4b1c 1738 * <td>Added support for SAML21 and SAMDAx.</td>
Kojto 111:4336505e4b1c 1739 * </tr>
Kojto 111:4336505e4b1c 1740 * <tr>
Kojto 111:4336505e4b1c 1741 * <td>D</td>
Kojto 111:4336505e4b1c 1742 * <td>12/2014</td>
Kojto 111:4336505e4b1c 1743 * <td>Added timer use case.
Kojto 111:4336505e4b1c 1744 * Added support for SAMR21 and SAMD10/D11.</td>
Kojto 111:4336505e4b1c 1745 * </tr>
Kojto 111:4336505e4b1c 1746 * <tr>
Kojto 111:4336505e4b1c 1747 * <td>C</td>
Kojto 111:4336505e4b1c 1748 * <td>01/2014</td>
Kojto 111:4336505e4b1c 1749 * <td>Added support for SAMD21.</td>
Kojto 111:4336505e4b1c 1750 * </tr>
Kojto 111:4336505e4b1c 1751 * <tr>
Kojto 111:4336505e4b1c 1752 * <td>B</td>
Kojto 111:4336505e4b1c 1753 * <td>06/2013</td>
Kojto 111:4336505e4b1c 1754 * <td>Corrected documentation typos.</td>
Kojto 111:4336505e4b1c 1755 * </tr>
Kojto 111:4336505e4b1c 1756 * <tr>
Kojto 111:4336505e4b1c 1757 * <td>A</td>
Kojto 111:4336505e4b1c 1758 * <td>06/2013</td>
Kojto 111:4336505e4b1c 1759 * <td>Initial release</td>
Kojto 111:4336505e4b1c 1760 * </tr>
Kojto 111:4336505e4b1c 1761 * </table>
Kojto 111:4336505e4b1c 1762 */
Kojto 111:4336505e4b1c 1763
Kojto 111:4336505e4b1c 1764 #endif /* TC_H_INCLUDED */