mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

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

Committer:
mbed_official
Date:
Fri Jul 17 09:15:10 2015 +0100
Revision:
592:a274ee790e56
Parent:
579:53297373a894
Synchronized with git revision e7144f83a8d75df80c4877936b6ffe552b0be9e6

Full URL: https://github.com/mbedmicro/mbed/commit/e7144f83a8d75df80c4877936b6ffe552b0be9e6/

More API implementation for SAMR21

Who changed what in which revision?

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