SMART ARM-based Microcontrollers AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver APPLICATION NOTE Introduction ® ® This driver for Atmel | SMART ARM -based microcontrollers provides an interface for the configuration and management of the TCC module within the device, for waveform generation and timing operations. It also provides extended options for control applications. The following driver API modes are covered by this manual: • • Polled APIs Callback APIs The following peripheral is used by this module: • TCC (Timer/Counter for Control Applications) The following devices can use this module: • Atmel | SMART SAM D21 • Atmel | SMART SAM R21 • Atmel | SMART SAM D10/D11 • Atmel | SMART SAM L21/L22 • Atmel | SMART SAM DA1 • Atmel | SMART SAM C20/C21 The outline of this documentation is as follows: • Prerequisites • Module Overview • Special Considerations • Extra Information • Examples • API Overview Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 Table of Contents Introduction......................................................................................................................1 1. Software License....................................................................................................... 5 2. Prerequisites..............................................................................................................6 3. Module Overview....................................................................................................... 7 3.1. 3.2. 3.3. 3.4. 3.5. 3.6. 3.7. Functional Description..................................................................................................................7 Base Timer/Counter..................................................................................................................... 8 3.2.1. Timer/Counter Size........................................................................................................ 8 3.2.2. Timer/Counter Clock and Prescaler...............................................................................8 3.2.3. Timer/Counter Control Inputs (Events).......................................................................... 9 3.2.4. Timer/Counter Reloading.............................................................................................10 3.2.5. One-shot Mode............................................................................................................ 10 Capture Operations.................................................................................................................... 10 3.3.1. Capture Operations - Event......................................................................................... 10 3.3.2. Capture Operations - Pulse Width............................................................................... 11 Compare Match Operation......................................................................................................... 11 3.4.1. Basic Timer.................................................................................................................. 11 3.4.2. Waveform Generation.................................................................................................. 11 3.4.3. Waveform Generation - PWM...................................................................................... 11 3.4.4. Waveform Generation - Frequency..............................................................................12 Waveform Extended Controls.....................................................................................................12 3.5.1. Pattern Generation...................................................................................................... 12 3.5.2. Recoverable Faults......................................................................................................12 3.5.3. Non-Recoverable Faults.............................................................................................. 14 Double and Circular Buffering.................................................................................................... 14 Sleep Mode................................................................................................................................ 14 4. Special Considerations............................................................................................ 15 4.1. 4.2. 4.3. Driver Feature Macro Definition..................................................................................................15 Module Features.........................................................................................................................15 4.2.1. SAM TCC Feature List.................................................................................................15 4.2.2. SAM D10/D11 TCC Feature List..................................................................................15 Channels vs. Pinouts..................................................................................................................15 5. Extra Information..................................................................................................... 17 6. Examples................................................................................................................. 18 7. API Overview........................................................................................................... 19 7.1. 7.2. Variable and Type Definitions..................................................................................................... 19 7.1.1. Type tcc_callback_t..................................................................................................... 19 Structure Definitions................................................................................................................... 19 7.2.1. Struct tcc_capture_config............................................................................................ 19 7.2.2. Struct tcc_config.......................................................................................................... 19 Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 2 7.2.3. 7.3. 7.4. 7.5. Union tcc_config.__unnamed__.................................................................................. 20 7.2.4. Struct tcc_counter_config............................................................................................ 20 7.2.5. Struct tcc_events......................................................................................................... 21 7.2.6. Struct tcc_input_event_config......................................................................................21 7.2.7. Struct tcc_match_wave_config.................................................................................... 22 7.2.8. Struct tcc_module........................................................................................................ 22 7.2.9. Struct tcc_non_recoverable_fault_config.....................................................................23 7.2.10. Struct tcc_output_event_config................................................................................... 23 7.2.11. Struct tcc_pins_config..................................................................................................23 7.2.12. Struct tcc_recoverable_fault_config.............................................................................23 7.2.13. Struct tcc_wave_extension_config.............................................................................. 24 Macro Definitions........................................................................................................................24 7.3.1. Driver Feature Definition..............................................................................................24 7.3.2. Module Status Flags.................................................................................................... 24 7.3.3. Macro _TCC_CHANNEL_ENUM_LIST....................................................................... 26 7.3.4. Macro _TCC_ENUM....................................................................................................26 7.3.5. Macro _TCC_WO_ENUM_LIST.................................................................................. 26 7.3.6. Macro TCC_NUM_CHANNELS...................................................................................26 7.3.7. Macro TCC_NUM_FAULTS......................................................................................... 26 7.3.8. Macro TCC_NUM_WAVE_OUTPUTS.........................................................................27 Function Definitions....................................................................................................................27 7.4.1. Driver Initialization and Configuration.......................................................................... 27 7.4.2. Event Management......................................................................................................29 7.4.3. Enable/Disable/Reset.................................................................................................. 30 7.4.4. Set/Toggle Count Direction.......................................................................................... 31 7.4.5. Get/Set Count Value.................................................................................................... 31 7.4.6. Stop/Restart Counter................................................................................................... 32 7.4.7. Generate TCC DMA Triggers Command.....................................................................33 7.4.8. Get/Set Compare/Capture Register.............................................................................33 7.4.9. Set Top Value...............................................................................................................34 7.4.10. Set Output Pattern....................................................................................................... 35 7.4.11. Set Ramp Index........................................................................................................... 36 7.4.12. Status Management.....................................................................................................36 7.4.13. Double Buffering Management.................................................................................... 38 Enumeration Definitions............................................................................................................. 42 7.5.1. Enum tcc_callback.......................................................................................................42 7.5.2. Enum tcc_channel_function.........................................................................................43 7.5.3. Enum tcc_clock_prescaler...........................................................................................43 7.5.4. Enum tcc_count_direction........................................................................................... 43 7.5.5. Enum tcc_count_overflow_dma_trigger_mode........................................................... 44 7.5.6. Enum tcc_event0_action............................................................................................. 44 7.5.7. Enum tcc_event1_action............................................................................................. 44 7.5.8. Enum tcc_event_action............................................................................................... 45 7.5.9. Enum tcc_event_generation_selection........................................................................46 7.5.10. Enum tcc_fault_blanking..............................................................................................47 7.5.11. Enum tcc_fault_capture_action................................................................................... 47 7.5.12. Enum tcc_fault_capture_channel................................................................................ 48 7.5.13. Enum tcc_fault_halt_action..........................................................................................48 7.5.14. Enum tcc_fault_keep................................................................................................... 48 Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 3 7.5.15. Enum tcc_fault_qualification........................................................................................ 48 7.5.16. 7.5.17. 7.5.18. 7.5.19. 7.5.20. 7.5.21. 7.5.22. 7.5.23. 7.5.24. 7.5.25. 7.5.26. 7.5.27. Enum tcc_fault_restart.................................................................................................49 Enum tcc_fault_source................................................................................................ 49 Enum tcc_fault_state_output....................................................................................... 49 Enum tcc_match_capture_channel............................................................................. 49 Enum tcc_output_invertion.......................................................................................... 49 Enum tcc_output_pattern.............................................................................................50 Enum tcc_ramp............................................................................................................50 Enum tcc_ramp_index................................................................................................. 50 Enum tcc_reload_action.............................................................................................. 50 Enum tcc_wave_generation........................................................................................ 51 Enum tcc_wave_output............................................................................................... 51 Enum tcc_wave_polarity..............................................................................................52 8. Extra Information for TCC Driver............................................................................. 53 8.1. 8.2. 8.3. 8.4. Acronyms....................................................................................................................................53 Dependencies.............................................................................................................................53 Errata..........................................................................................................................................53 Module History............................................................................................................................53 9. Examples for TCC Driver.........................................................................................54 9.1. 9.2. 9.3. 9.4. 9.5. 9.6. 9.7. Quick Start Guide for TCC - Basic..............................................................................................54 9.1.1. Quick Start................................................................................................................... 55 9.1.2. Use Case..................................................................................................................... 56 Quick Start Guide for TCC - Double Buffering and Circular....................................................... 57 9.2.1. Quick Start................................................................................................................... 58 9.2.2. Use Case..................................................................................................................... 60 Quick Start Guide for TCC - Timer............................................................................................. 60 9.3.1. Quick Start................................................................................................................... 61 9.3.2. Use Case..................................................................................................................... 63 Quick Start Guide for TCC - Callback.........................................................................................63 9.4.1. Quick Start................................................................................................................... 64 9.4.2. Use Case..................................................................................................................... 66 Quick Start Guide for TCC - Non-Recoverable Fault................................................................. 67 9.5.1. Quick Start................................................................................................................... 68 9.5.2. Use Case..................................................................................................................... 75 Quick Start Guide for TCC - Recoverable Fault......................................................................... 75 9.6.1. Quick Start................................................................................................................... 77 9.6.2. Use Case..................................................................................................................... 84 Quick Start Guide for Using DMA with TCC............................................................................... 84 9.7.1. Quick Start................................................................................................................... 86 9.7.2. Use Case..................................................................................................................... 94 10. Document Revision History..................................................................................... 95 Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 4 1. Software License Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of Atmel may not be used to endorse or promote products derived from this software without specific prior written permission. 4. This software may only be redistributed and used in connection with an Atmel microcontroller product. THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 5 2. Prerequisites There are no prerequisites for this module. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 6 3. Module Overview The Timer/Counter for Control Applications (TCC) module provides a set of timing and counting related functionality, such as the generation of periodic waveforms, the capturing of a periodic waveform's frequency/duty cycle, software timekeeping for periodic operations, waveform extension control, fault detection etc. The counter size of the TCC modules can be 16- or 24-bit depending on the TCC instance. Refer SAM TCC Feature List and SAM D10/D11 TCC Feature List for details on TCC instances. The TCC module for the SAM includes the following functions: • • • • • • • Generation of PWM signals Generation of timestamps for events General time counting Waveform period capture Waveform frequency capture Additional control for generated waveform outputs Fault protection for waveform generation Figure 3-1 Overview of the TCC Module on page 7 shows the overview of the TCC Module. Figure 3-1 Overview of the TCC Module Base Counter PERB PER Prescaler "count" "clear" "load" "direction" Counter COUNT = OVF (INT/Event/DMA Req.) ERR (INT Req.) Control Logic TOP BOTTOM =0 "TCCx_EV0" "TCCx_EV1" "ev" UPDATE BV "TCCx_MCx" Event System WO[7] = 3.1. Waveform Generation "match" Pattern Generation SWAP Control Logic Dead-Time Insertion CCx Output Matrix CCBx Recoverable Faults BV "capture" Non-recoverable Faults WO[6] Compare/Capture (Unit x = {0,1,…,3}) WO[5] WO[4] WO[3] WO[2] WO[1] WO[0] MCx (INT/Event/DMA Req.) Functional Description The TCC module consists of following sections: Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 7 • • • • Base Counter Compare/Capture channels, with waveform generation Waveform extension control and fault detection Interface to the event system, DMAC, and the interrupt system The base counter can be configured to either count a prescaled generic clock or events from the event system.(TCEx, with event action configured to counting). The counter value can be used by compare/ capture channels which can be set up either in compare mode or capture mode. In capture mode, the counter value is stored when a configurable event occurs. This mode can be used to generate timestamps used in event capture, or it can be used for the measurement of a periodic input signal's frequency/duty cycle. In compare mode, the counter value is compared against one or more of the configured channels' compare values. When the counter value coincides with a compare value an action can be taken automatically by the module, such as generating an output event or toggling a pin when used for frequency or PWM signal generation. Note: The connection of events between modules requires the use of the SAM Event System Driver (EVENTS) to route output event of one module to the the input event of another. For more information on event routing, refer to the event driver documentation. In compare mode, when output signal is generated, extended waveform controls are available, to arrange the compare outputs into specific formats. The Output matrix can change the channel output routing. Pattern generation unit can overwrite the output signal line to specific state. The Fault protection feature of the TCC supports recoverable and non-recoverable faults. 3.2. Base Timer/Counter 3.2.1. Timer/Counter Size Each TCC has a counter size of either 16- or 24-bits. The size of the counter determines the maximum value it can count to before an overflow occurs. Table 3-1 Timer Counter Sizes and Their Maximum Count Values on page 8 shows the maximum values for each of the possible counter sizes. Table 3-1 Timer Counter Sizes and Their Maximum Count Values Counter size Max. (hexadecimal) Max. (decimal) 16-bit 0xFFFF 65,535 24-bit 0xFFFFFF 16,777,215 The period/top value of the counter can be set, to define counting period. This will allow the counter to overflow when the counter value reaches the period/top value. 3.2.2. Timer/Counter Clock and Prescaler TCC is clocked asynchronously to the system clock by a GCLK (Generic Clock) channel. The GCLK channel can be connected to any of the GCLK generators. The GCLK generators are configured to use one of the available clock sources in the system such as internal oscillator, external crystals, etc. See the Generic Clock driver for more information. Each TCC module in the SAM has its own individual clock prescaler, which can be used to divide the input clock frequency used by the counter. This prescaler only scales the clock used to provide clock Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 8 pulses for the counter to count, and does not affect the digital register interface portion of the module, thus the timer registers will be synchronized to the raw GCLK frequency input to the module. As a result of this, when selecting a GCLK frequency and timer prescaler value, the user application should consider both the timer resolution required and the synchronization frequency to avoid lengthy synchronization times of the module if a very slow GCLK frequency is fed into the TCC module. It is preferable to use a higher module GCLK frequency as the input to the timer, and prescale this down as much as possible to obtain a suitable counter frequency in latency-sensitive applications. 3.2.3. Timer/Counter Control Inputs (Events) The TCC can take several actions on the occurrence of an input event. The event actions are listed in Table 3-2 TCC Module Event Actions on page 9. Table 3-2 TCC Module Event Actions Event action Description Applied event TCC_EVENT_ACTION_OFF No action on the event input All TCC_EVENT_ACTION_RETRIGGER Re-trigger Counter on event All TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT Generate NonRecoverable Fault on event All TCC_EVENT_ACTION_START Counter start on event EV0 TCC_EVENT_ACTION_DIR_CONTROL Counter direction control EV0 TCC_EVENT_ACTION_DECREMENT Counter decrement on event EV0 TCC_EVENT_ACTION_PERIOD_PULSE_WIDTH_CAPTURE Capture pulse period and pulse width EV0 TCC_EVENT_ACTION_PULSE_WIDTH_PERIOD_CAPTURE Capture pulse width and pulse period EV0 TCC_EVENT_ACTION_STOP Counter stop on event EV1 TCC_EVENT_ACTION_COUNT_EVENT Counter count on event EV1 TCC_EVENT_ACTION_INCREMENT Counter increment on event EV1 TCC_EVENT_ACTION_COUNT_DURING_ACTIVE Counter count during active state of asynchronous event EV1 Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 9 3.2.4. Timer/Counter Reloading The TCC also has a configurable reload action, used when a re-trigger event occurs. Examples of a retrigger event could be the counter reaching the maximum value when counting up, or when an event from the event system makes the counter to re-trigger. The reload action determines if the prescaler should be reset, and on which clock. The counter will always be reloaded with the value it is set to start counting. The user can choose between three different reload actions, described in Table 3-3 TCC Module Reload Actions on page 10. Table 3-3 TCC Module Reload Actions Reload action Description TCC_RELOAD_ACTION_GCLK Reload TCC counter value on next GCLK cycle. Leave prescaler as-is. TCC_RELOAD_ACTION_PRESC Reloads TCC counter value on next prescaler clock. Leave prescaler as-is. TCC_RELOAD_ACTION_RESYNC Reload TCC counter value on next GCLK cycle. Clear prescaler to zero. The reload action to use will depend on the specific application being implemented. One example is when an external trigger for a reload occurs; if the TCC uses the prescaler, the counter in the prescaler should not have a value between zero and the division factor. The counter in the TCC module and the counter in the prescaler should both start at zero. If the counter is set to re-trigger when it reaches the maximum value, this is not the right option to use. In such a case it would be better if the prescaler is left unaltered when the re-trigger happens, letting the counter reset on the next GCLK cycle. 3.2.5. One-shot Mode The TCC module can be configured in one-shot mode. When configured in this manner, starting the timer will cause it to count until the next overflow or underflow condition before automatically halting, waiting to be manually triggered by the user application software or an event from the event system. 3.3. Capture Operations In capture operations, any event from the event system or a pin change can trigger a capture of the counter value. This captured counter value can be used as timestamps for the events, or it can be used in frequency and pulse width capture. 3.3.1. Capture Operations - Event Event capture is a simple use of the capture functionality, designed to create timestamps for specific events. When the input event appears, the current counter value is copied into the corresponding compare/capture register, which can then be read by the user application. Note that when performing any capture operation, there is a risk that the counter reaches its top value (MAX) when counting up, or the bottom value (zero) when counting down, before the capture event occurs. This can distort the result, making event timestamps to appear shorter than they really are. In this case, the user application should check for timer overflow when reading a capture result in order to detect this situation and perform an appropriate adjustment. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 10 Before checking for a new capture, TCC_STATUS_COUNT_OVERFLOW should be checked. The response to an overflow error is left to the user application, however, it may be necessary to clear both the overflow flag and the capture flag upon each capture reading. 3.3.2. Capture Operations - Pulse Width Pulse Width Capture mode makes it possible to measure the pulse width and period of PWM signals. This mode uses two capture channels of the counter. There are two modes for pulse width capture; Pulse Width Period (PWP) and Period Pulse Width (PPW). In PWP mode, capture channel 0 is used for storing the pulse width and capture channel 1 stores the observed period. While in PPW mode, the roles of the two capture channels are reversed. As in the above example it is necessary to poll on interrupt flags to see if a new capture has happened and check that a capture overflow error has not occurred. Refer to Timer/Counter Control Inputs (Events) to set up the input event to perform pulse width capture. 3.4. Compare Match Operation In compare match operation, Compare/Capture registers are compared with the counter value. When the timer's count value matches the value of a compare channel, a user defined action can be taken. 3.4.1. Basic Timer A Basic Timer is a simple application where compare match operation is used to determine when a specific period has elapsed. In Basic Timer operations, one or more values in the module's Compare/ Capture registers are used to specify the time (in terms of the number of prescaled GCLK cycles, or input events) at which an action should be taken by the microcontroller. This can be an Interrupt Service Routine (ISR), event generation via the event system, or a software flag that is polled from the user application. 3.4.2. Waveform Generation Waveform generation enables the TCC module to generate square waves, or, if combined with an external passive low-pass filter, analog waveforms. 3.4.3. Waveform Generation - PWM Pulse width modulation is a form of waveform generation and a signalling technique that can be useful in many applications. When PWM mode is used, a digital pulse train with a configurable frequency and duty cycle can be generated by the TCC module and output to a GPIO pin of the device. Often PWM is used to communicate a control or information parameter to an external circuit or component. Differing impedances of the source generator and sink receiver circuits is less of an issue when using PWM compared to using an analog voltage value, as noise will not generally affect the signal's integrity to a meaningful extent. Figure 3-2 Example Of PWM In Single-Slope Mode, and Different Counter Operations on page 12 illustrates operations and different states of the counter and its output when using the timer in Normal PWM mode (Single Slope). As can be seen, the TOP/PERIOD value is unchanged and is set to MAX. The compare match value is changed at several points to illustrate the resulting waveform output changes. The PWM output is set to normal (i.e. non-inverted) output mode. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 11 Figure 3-2 Example Of PWM In Single-Slope Mode, and Different Counter Operations Reload counter to zero TOP/Period = Max (PER) Counter value (COUNT) Compare/Match value (CCx) Time Compare value has been changed (CCx) Match PWM output Several PWM modes are supported by the TCC module, refer to datasheet for the details on PWM waveform generation. 3.4.4. Waveform Generation - Frequency Normal Frequency Generation is in many ways identical to PWM generation. However, only in Frequency Generation, a toggle occurs on the output when a match on a compare channels occurs. When the Match Frequency Generation is used, the timer value is reset on match condition, resulting in a variable frequency square wave with a fixed 50% duty cycle. 3.5. Waveform Extended Controls 3.5.1. Pattern Generation Pattern insertion allows the TCC module to change the actual pin output level without modifying the compare/match settings. Table 3-4 TCC Module Output Pattern Generation 3.5.2. Pattern Description TCC_OUTPUT_PATTERN_DISABLE Pattern disabled, generate output as is TCC_OUTPUT_PATTERN_0 Generate pattern 0 on output (keep the output LOW) TCC_OUTPUT_PATTERN_1 Generate pattern 1 on output (keep the output HIGH) Recoverable Faults The recoverable faults can trigger one or several of following fault actions: Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 12 1. *Halt* action: The recoverable faults can halt the TCC timer/counter, so that the final output wave is kept at a defined state. When the fault state is removed it is possible to recover the counter and waveform generation. The halt action is defined as: Table 3-5 TCC Module Recoverable Fault Halt Actions Action Description TCC_FAULT_HALT_ACTION_DISABLE Halt action is disabled TCC_FAULT_HALT_ACTION_HW_HALT The timer/counter is halted as long as the corresponding fault is present TCC_FAULT_HALT_ACTION_SW_HALT The timer/counter is halted until the corresponding fault is removed and fault state cleared by software TCC_FAULT_HALT_ACTION_NON_RECOVERABLE Force all the TCC output pins to a pre-defined level, as what Non-Recoverable Fault do 2. 3. 4. *Restart* action: When enabled, the recoverable faults can restart the TCC timer/counter. *Keep* action: When enabled, the recoverable faults can keep the corresponding channel output to zero when the fault condition is present. *Capture* action: When the recoverable fault occurs, the capture action can time stamps the corresponding fault. The following capture mode is supported: Table 3-6 TCC Module Recoverable Fault Capture Actions Action Description TCC_FAULT_CAPTURE_DISABLE Capture action is disabled TCC_FAULT_CAPTURE_EACH Equivalent to standard capture operation, on each fault occurrence the time stamp is captured TCC_FAULT_CAPTURE_MINIMUM Get the minimum time stamped value in all time stamps TCC_FAULT_CAPTURE_MAXIMUM Get the maximum time stamped value in all time stamps TCC_FAULT_CAPTURE_SMALLER Time stamp the fault input if the value is smaller than last one TCC_FAULT_CAPTURE_BIGGER Time stamp the fault input if the value is bigger than last one TCC_FAULT_CAPTURE_CHANGE Time stamp the fault input if the time stamps changes its increment direction In TCC module, only the first two compare channels (CC0 and CC1) can work with recoverable fault inputs. The corresponding event inputs (TCCx MC0 and TCCx MC1) are then used as fault inputs respectively. The faults are called Fault A and Fault B. The recoverable fault can be filtered or effected by corresponding channel output. On fault condition there are many other settings that can be chosen. Refer to data sheet for more details about the recoverable fault operations. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 13 3.5.3. Non-Recoverable Faults The non-recoverable faults force all the TCC output pins to a pre-defined level (can be forced to 0 or 1). The input control signal of non-recoverable fault is from timer/counter event (TCCx EV0 and TCCx EV1). To enable non-recoverable fault, corresponding TCEx event action must be set to non-recoverable fault action (TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT). Refer to Timer/Counter Control Inputs (Events) to see the available event input action. 3.6. Double and Circular Buffering The pattern, period, and the compare channels registers are double buffered. For these options there are effective registers (PATT, PER, and CCx) and buffer registers (PATTB, PERB, and CCx). When writing to the buffer registers, the values are buffered and will be committed to effective registers on UPDATE condition. Usually the buffered value is cleared after it is committed, but there is also an option to circular the register buffers. The period (PER) and four lowest compare channels register (CCx, x is 0 ~ 3) support this function. When circular buffer is used, on UPDATE the previous period or compare values are copied back into the corresponding period buffer and compare buffers. This way, the register value and its buffer register value is actually switched on UPDATE condition, and will be switched back on next UPDATE condition. For input capture, the buffer register (CCBx) and the corresponding capture channel register (CCx) act like a FIFO. When regular register (CCx) is empty or read, any content in the buffer register is passed to regular one. In TCC module driver, when the double buffering write is enabled, any write through tcc_set_top_value(), tcc_set_compare_value(), and tcc_set_pattern() will be done to the corresponding buffer register. Then the value in the buffer register will be transferred to the regular register on the next UPDATE condition or by a force UPDATE using tcc_force_double_buffer_update(). 3.7. Sleep Mode TCC modules can be configured to operate in any sleep mode, with its "run in standby" function enabled. It can wake up the device using interrupts or perform internal actions with the help of the Event System. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 14 4. Special Considerations 4.1. Driver Feature Macro Definition Table 4-1 TCC Module Specific Features on page 15 shows some specific features of the TCC Module. Table 4-1 TCC Module Specific Features Driver Feature Macro Supported devices FEATURE_TCC_GENERATE_DMA_TRIGGER SAM L21/L22 Note: The specific features are only available in the driver when the selected device supports those features. 4.2. Module Features The features of TCC, such as timer/counter size, number of compare capture channels, and number of outputs, are dependent on the TCC module instance being used. 4.2.1. SAM TCC Feature List For SAM D21/R21/L21/L22/DA1/C21, the TCC features are: Table 4-2 TCC module features for SAM D21/R21/L21/L22/DA1/C21 4.2.2. TCC# Match/ Capture channels Wave outputs Counter size [bits] Fault Dithering Output matrix Dead-Time SWAP Pattern insertion 0 4 8 24 Y Y Y 1 2 4 24 Y Y 2 2 2 16 Y Y Y Y Y SAM D10/D11 TCC Feature List For SAM D10/D11, the TCC features are: Table 4-3 TCC Module Features For SAM D10/D11 4.3. TCC# Match/ Capture channels Wave outputs Counter Fault Dithering Output size [bits] matrix Dead-Time SWAP Pattern insertion 0 8 24 Y 4 Y Y Y Y Y Channels vs. Pinouts As the TCC module may have more waveform output pins than the number of compare/capture channels, the free pins (with number higher than number of channels) will reuse the waveform generated by Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 15 channels subsequently. E.g., if the number of channels is four and the number of wave output pins is eight, channel 0 output will be available on out pin 0 and 4, channel 1 output on wave out pin 1 and 5, and so on. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 16 5. Extra Information For extra information, see Extra Information for TCC Driver. This includes: • Acronyms • Dependencies • Errata • Module History Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 17 6. Examples For a list of examples related to this driver, see Examples for TCC Driver. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 18 7. API Overview 7.1. Variable and Type Definitions 7.1.1. Type tcc_callback_t typedef void(* tcc_callback_t )(struct tcc_module *const module) Type definition for the TCC callback function. 7.2. Structure Definitions 7.2.1. Struct tcc_capture_config Structure used when configuring TCC channels in capture mode. Table 7-1 Members 7.2.2. Type Name Description enum tcc_channel_function channel_function[] Channel functions selection (capture/match) Struct tcc_config Configuration struct for a TCC instance. This structure should be initialized by the tcc_get_config_defaults function before being modified by the user application. Table 7-2 Members Type Name Description union tcc_config.@1 @1 TCC match/capture configurations struct tcc_counter_config counter Structure for configuring TCC base timer/ counter bool double_buffering_enabled Set to true to enable double buffering write. When enabled any write through tcc_set_top_value(), tcc_set_compare_value() and tcc_set_pattern() will direct to the buffer register as buffered value, and the buffered value will be committed to effective register on UPDATE condition, if update is not locked. Note: The init values in tcc_config for tcc_init are always filled to effective registers, no matter if double buffering is enabled or not. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 19 7.2.3. Type Name Description struct tcc_pins_config pins Structure for configuring TCC output pins bool run_in_standby When true the module is enabled during standby struct tcc_wave_extension_config wave_ext Structure for configuring TCC waveform extension Union tcc_config.__unnamed__ TCC match/capture configurations Table 7-3 Members Type Name Description struct tcc_capture_config capture Helps to configure a TCC channel in capture mode struct tcc_match_wave_config compare For configuring a TCC channel in compare mode struct tcc_match_wave_config wave 7.2.4. Serves the same purpose as compare. Used as an alias for compare, when a TCC channel is configured for wave generation Struct tcc_counter_config Structure for configuring a TCC as a counter. Table 7-4 Members Type Name Description enum tcc_clock_prescaler clock_prescaler Specifies the prescaler value for GCLK_TCC enum gclk_generator clock_source GCLK generator used to clock the peripheral uint32_t count Value to initialize the count register enum tcc_count_direction direction Specifies the direction for the TCC to count enum tcc_count_overflow_dma_trigger_mode dma_trigger_mode Counter overflow trigger a DMA request mode bool oneshot When true, the counter will be stopped on the next hardware or software re-trigger event or overflow/ underflow Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 20 7.2.5. Type Name Description uint32_t period Period/top and period/top buffer values for counter enum tcc_reload_action reload_action Specifies the reload or reset time of the counter and prescaler resynchronization on a re-trigger event for the TCC Struct tcc_events Event flags for the tcc_enable_events() and tcc_disable_events(). Table 7-5 Members 7.2.6. Type Name Description bool generate_event_on_channel[] Generate an output event on a channel capture/match. Specify which channels will generate events bool generate_event_on_counter_event Generate an output event on counter boundary. See tcc_event_output_action. bool generate_event_on_counter_overflow Generate an output event on counter overflow/underflow bool generate_event_on_counter_retrigger Generate an output event on counter retrigger struct tcc_input_event_config input_config[] Input events configuration bool on_event_perform_channel_action[] Perform the configured event action when an incoming channel event is signalled bool on_input_event_perform_action[] Perform the configured event action when an incoming event is signalled struct tcc_output_event_config output_config Output event configuration Struct tcc_input_event_config For configuring an input event. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 21 Table 7-6 Members 7.2.7. Type Name Description enum tcc_event_action action Event action on incoming event bool invert Invert incoming event input line bool modify_action Modify event action Struct tcc_match_wave_config The structure, which helps to configure a TCC channel for compare operation and wave generation. Table 7-7 Members Type Name Description enum tcc_channel_function channel_function[] Channel functions selection (capture/match) uint32_t 7.2.8. match[] Value to be used for compare match on each channel enum tcc_wave_generation wave_generation Specifies which waveform generation mode to use enum tcc_wave_polarity wave_polarity[] Specifies polarity for match output waveform generation enum tcc_ramp wave_ramp Specifies Ramp mode for waveform generation Struct tcc_module TCC software instance structure, used to retain software state information of an associated hardware module instance. Note: The fields of this structure should not be altered by the user application; they are reserved only for module-internal use. Table 7-8 Members Type Name Description tcc_callback_t callback[] Array of callbacks bool double_buffering_enabled Set to true to write to buffered registers uint32_t enable_callback_mask Bit mask for callbacks enabled Tcc * hw Hardware module pointer of the associated Timer/Counter peripheral. uint32_t register_callback_mask Bit mask for callbacks registered Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 22 7.2.9. Struct tcc_non_recoverable_fault_config Table 7-9 Members Type Name Description uint8_t filter_value Fault filter value applied on TCEx event input line (0x0 ~ 0xF). Must be 0 when TCEx event is used as synchronous event. enum tcc_fault_state_output output 7.2.10. Output Struct tcc_output_event_config Structure used for configuring an output event. Table 7-10 Members 7.2.11. Type Name Description enum tcc_event_generation_selection generation_selection It decides which part of the counter cycle the counter event output is generated bool modify_generation_selection A switch to allow enable/disable of events, without modifying the event output configuration Struct tcc_pins_config Structure which is used when taking wave output from TCC. Table 7-11 Members 7.2.12. Type Name Description bool enable_wave_out_pin[] When true, PWM output pin for the given channel is enabled uint32_t wave_out_pin[] Specifies pin output for each channel uint32_t wave_out_pin_mux[] Specifies MUX setting for each output channel pin Struct tcc_recoverable_fault_config Table 7-12 Members Type Name Description enum tcc_fault_blanking blanking Fault Blanking Start Point for recoverable Fault uint8_t blanking_cycles Fault blanking value (0 ~ 255), disable input source for several TCC clocks after the detection of the waveform edge enum tcc_fault_capture_action capture_action enum tcc_fault_capture_channel capture_channel Channel triggered by recoverable Fault Capture action for recoverable Fault Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 23 7.2.13. Type Name Description uint8_t filter_value Fault filter value applied on MCEx event input line (0x0 ~ 0xF). Must be 0 when MCEx event is used as synchronous event. Apply to both recoverable and non-recoverable fault. enum tcc_fault_halt_action halt_action Halt action for recoverable Fault bool keep Set to true to enable keep action (keep until end of TCC cycle) bool qualification Set to true to enable input qualification (disable input when output is inactive) bool restart Set to true to enable restart action enum tcc_fault_source source Specifies if the event input generates recoverable Fault. The event system channel connected to MCEx event input must be configured as asynchronous. Struct tcc_wave_extension_config This structure is used to specify the waveform extension features for TCC. Table 7-13 Members Type Name Description bool invert[] Invert waveform final outputs lines struct tcc_non_recoverable_fault_config non_recoverable_fault[] Configuration for non-recoverable faults struct tcc_recoverable_fault_config 7.3. Macro Definitions 7.3.1. Driver Feature Definition recoverable_fault[] Configuration for recoverable faults Define port features set according to different device family. 7.3.1.1. Macro FEATURE_TCC_GENERATE_DMA_TRIGGER #define FEATURE_TCC_GENERATE_DMA_TRIGGER Generate DMA triggers 7.3.2. Module Status Flags TCC status flags, returned by tcc_get_status() and cleared by tcc_clear_status(). Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 24 7.3.2.1. Macro TCC_STATUS_CHANNEL_MATCH_CAPTURE #define TCC_STATUS_CHANNEL_MATCH_CAPTURE(ch) Timer channel ch (0 ~ 3) has matched against its compare value, or has captured a new value. 7.3.2.2. Macro TCC_STATUS_CHANNEL_OUTPUT #define TCC_STATUS_CHANNEL_OUTPUT(ch) Timer channel ch (0 ~ 3) match/compare output state. 7.3.2.3. Macro TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR #define TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(x) A Non-Recoverable Fault x (0 ~ 1) has occurred. 7.3.2.4. Macro TCC_STATUS_RECOVERABLE_FAULT_OCCUR #define TCC_STATUS_RECOVERABLE_FAULT_OCCUR(n) A Recoverable Fault n (0 ~ 1 representing A ~ B) has occured. 7.3.2.5. Macro TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT #define TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(x) The Non-Recoverable Fault x (0 ~ 1) input is present. 7.3.2.6. Macro TCC_STATUS_RECOVERABLE_FAULT_PRESENT #define TCC_STATUS_RECOVERABLE_FAULT_PRESENT(n) A Recoverable Fault n (0 ~ 1 representing A ~ B) is present. 7.3.2.7. Macro TCC_STATUS_SYNC_READY #define TCC_STATUS_SYNC_READY Timer registers synchronization has completed, and the synchronized count value may be read. 7.3.2.8. Macro TCC_STATUS_CAPTURE_OVERFLOW #define TCC_STATUS_CAPTURE_OVERFLOW A new value was captured before the previous value was read, resulting in lost data. 7.3.2.9. Macro TCC_STATUS_COUNTER_EVENT #define TCC_STATUS_COUNTER_EVENT A counter event occurred. 7.3.2.10. Macro TCC_STATUS_COUNTER_RETRIGGERED #define TCC_STATUS_COUNTER_RETRIGGERED Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 25 A counter retrigger occurred. 7.3.2.11. Macro TCC_STATUS_COUNT_OVERFLOW #define TCC_STATUS_COUNT_OVERFLOW The timer count value has overflowed from its maximum value to its minimum when counting upward, or from its minimum value to its maximum when counting downward. 7.3.2.12. Macro TCC_STATUS_RAMP_CYCLE_INDEX #define TCC_STATUS_RAMP_CYCLE_INDEX Ramp period cycle index. In ramp operation, each two period cycles are marked as cycle A and B, the index 0 represents cycle A and 1 represents cycle B. 7.3.2.13. Macro TCC_STATUS_STOPPED #define TCC_STATUS_STOPPED The counter has been stopped (due to disable, stop command, or one-shot). 7.3.3. Macro _TCC_CHANNEL_ENUM_LIST #define _TCC_CHANNEL_ENUM_LIST(type) Generates table enum list entries for all channels of a given type and channel number on TCC module. 7.3.4. Macro _TCC_ENUM #define _TCC_ENUM(n, type) Generates a table enum list entry for a given type and index (e.g. "TCC_CALLBACK_MC_CHANNEL_0,"). 7.3.5. Macro _TCC_WO_ENUM_LIST #define _TCC_WO_ENUM_LIST(type) Generates table enum list entries for all output of a given type and waveform output number on TCC module. 7.3.6. Macro TCC_NUM_CHANNELS #define TCC_NUM_CHANNELS Maximum number of channels supported by the driver (Channel index from 0 to TCC_NUM_CHANNELS 1). 7.3.7. Macro TCC_NUM_FAULTS #define TCC_NUM_FAULTS Maximum number of (recoverable) faults supported by the driver. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 26 7.3.8. Macro TCC_NUM_WAVE_OUTPUTS #define TCC_NUM_WAVE_OUTPUTS Maximum number of wave outputs lines supported by the driver (Output line index from 0 to TCC_NUM_WAVE_OUTPUTS - 1). 7.4. Function Definitions 7.4.1. Driver Initialization and Configuration 7.4.1.1. Function tcc_is_syncing() Determines if the hardware module is currently synchronizing to the bus. bool tcc_is_syncing( const struct tcc_module *const module_inst) Checks to see if the underlying hardware peripheral module is currently synchronizing across multiple clock domains to the hardware bus. This function can be used to delay further operations on a module until such time that it is ready, to prevent blocking delays for synchronization in the user application. Table 7-14 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct Returns Synchronization status of the underlying hardware module. Table 7-15 Return Values 7.4.1.2. Return value Description false If the module has completed synchronization true If the module synchronization is ongoing Function tcc_get_config_defaults() Initializes config with predefined default values. void tcc_get_config_defaults( struct tcc_config *const config, Tcc *const hw) This function will initialize a given TCC configuration structure to a set of known default values. This function should be called on any new instance of the configuration structures before being modified by the user application. The default configuration is as follows: • Don't run in standby • When setting top, compare, or pattern by API, do double buffering write • The base timer/counter configurations: Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 27 • • • • • • GCLK generator 0 clock source • No prescaler • GCLK reload action • Count upward • Don't perform one-shot operations • Counter starts on 0 • Period/top value set to maximum The match/capture configurations: • All Capture compare channel value set to 0 • No capture enabled (all channels use compare function) • Normal frequency wave generation • Waveform generation polarity set to 0 • Don't perform ramp on waveform The waveform extension configurations: • No recoverable fault is enabled, fault actions are disabled, filter is set to 0 • No non-recoverable fault state output is enabled and filter is 0 • No inversion of waveform output No channel output enabled No PWM pin output enabled Pin and MUX configuration not set Table 7-16 Parameters 7.4.1.3. Data direction Parameter name Description [out] config Pointer to a TCC module configuration structure to set [in] hw Pointer to the TCC hardware module Function tcc_init() Initializes a hardware TCC module instance. enum status_code tcc_init( struct tcc_module *const module_inst, Tcc *const hw, const struct tcc_config *const config) Enables the clock and initializes the given TCC module, based on the given configuration values. Table 7-17 Parameters Data direction Parameter name Description [in, out] module_inst Pointer to the software module instance struct [in] hw Pointer to the TCC hardware module [in] config Pointer to the TCC configuration options struct Returns Status of the initialization procedure. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 28 Table 7-18 Return Values Return value Description STATUS_OK The module was initialized successfully STATUS_BUSY The hardware module was busy when the initialization procedure was attempted STATUS_INVALID_ARG An invalid configuration option or argument was supplied STATUS_ERR_DENIED The hardware module was already enabled 7.4.2. Event Management 7.4.2.1. Function tcc_enable_events() Enables the TCC module event input or output. enum status_code tcc_enable_events( struct tcc_module *const module_inst, struct tcc_events *const events) Enables one or more input or output events to or from the TCC module. See tcc_events for a list of events this module supports. Note: Events cannot be altered while the module is enabled. Table 7-19 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] events Struct containing flags of events to enable or configure Returns Status of the events setup procedure. Table 7-20 Return Values 7.4.2.2. Return value Description STATUS_OK The module was initialized successfully STATUS_INVALID_ARG An invalid configuration option or argument was supplied Function tcc_disable_events() Disables the event input or output of a TCC instance. void tcc_disable_events( struct tcc_module *const module_inst, struct tcc_events *const events) Disables one or more input or output events for the given TCC module. See tcc_events for a list of events this module supports. Note: Events cannot be altered while the module is enabled. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 29 Table 7-21 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] events Struct containing flags of events to disable 7.4.3. Enable/Disable/Reset 7.4.3.1. Function tcc_enable() Enable the TCC module. void tcc_enable( const struct tcc_module *const module_inst) Enables a TCC module that has been previously initialized. The counter will start when the counter is enabled. Note: When the counter is configured to re-trigger on an event, the counter will not start until the next incoming event appears. Then it restarts on any following event. Table 7-22 Parameters 7.4.3.2. Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct Function tcc_disable() Disables the TCC module. void tcc_disable( const struct tcc_module *const module_inst) Disables a TCC module and stops the counter. Table 7-23 Parameters 7.4.3.3. Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct Function tcc_reset() Resets the TCC module. void tcc_reset( const struct tcc_module *const module_inst) Resets the TCC module, restoring all hardware module registers to their default values and disabling the module. The TCC module will not be accessible while the reset is being performed. Note: When resetting a 32-bit counter only the master TCC module's instance structure should be passed to the function. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 30 Table 7-24 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct 7.4.4. Set/Toggle Count Direction 7.4.4.1. Function tcc_set_count_direction() Sets the TCC module count direction. void tcc_set_count_direction( const struct tcc_module *const module_inst, enum tcc_count_direction dir) Sets the count direction of an initialized TCC module. The specified TCC module can remain running or stopped. Table 7-25 Parameters 7.4.4.2. Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] dir New timer count direction to set Function tcc_toggle_count_direction() Toggles the TCC module count direction. void tcc_toggle_count_direction( const struct tcc_module *const module_inst) Toggles the count direction of an initialized TCC module. The specified TCC module can remain running or stopped. Table 7-26 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct 7.4.5. Get/Set Count Value 7.4.5.1. Function tcc_get_count_value() Get count value of the given TCC module. uint32_t tcc_get_count_value( const struct tcc_module *const module_inst) Retrieves the current count value of a TCC module. The specified TCC module can remain running or stopped. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 31 Table 7-27 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct Returns Count value of the specified TCC module. 7.4.5.2. Function tcc_set_count_value() Sets count value for the given TCC module. enum status_code tcc_set_count_value( const struct tcc_module *const module_inst, const uint32_t count) Sets the timer count value of an initialized TCC module. The specified TCC module can remain running or stopped. Table 7-28 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] count New timer count value to set Returns Status which indicates whether the new value is set. Table 7-29 Return Values Return value Description STATUS_OK The timer count was updated successfully STATUS_ERR_INVALID_ARG An invalid timer counter size was specified 7.4.6. Stop/Restart Counter 7.4.6.1. Function tcc_stop_counter() Stops the counter. void tcc_stop_counter( const struct tcc_module *const module_inst) This function will stop the counter. When the counter is stopped the value in the count register is set to 0 if the counter was counting up, or maximum or the top value if the counter was counting down. Table 7-30 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 32 7.4.6.2. Function tcc_restart_counter() Starts the counter from beginning. void tcc_restart_counter( const struct tcc_module *const module_inst) Restarts an initialized TCC module's counter. Table 7-31 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct 7.4.7. Generate TCC DMA Triggers Command 7.4.7.1. Function tcc_dma_trigger_command() TCC DMA Trigger. void tcc_dma_trigger_command( const struct tcc_module *const module_inst) TCC DMA trigger command. Table 7-32 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct 7.4.8. Get/Set Compare/Capture Register 7.4.8.1. Function tcc_get_capture_value() Gets the TCC module capture value. uint32_t tcc_get_capture_value( const struct tcc_module *const module_inst, const enum tcc_match_capture_channel channel_index) Retrieves the capture value in the indicated TCC module capture channel. Table 7-33 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] channel_index Index of the Compare Capture channel to read Returns Capture value stored in the specified timer channel. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 33 7.4.8.2. Function tcc_set_compare_value() Sets a TCC module compare value. enum status_code tcc_set_compare_value( const struct tcc_module *const module_inst, const enum tcc_match_capture_channel channel_index, const uint32_t compare) Writes a compare value to the given TCC module compare/capture channel. If double buffering is enabled it always write to the buffer register. The value will then be updated immediately by calling tcc_force_double_buffer_update(), or be updated when the lock update bit is cleared and the UPDATE condition happen. Table 7-34 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] channel_index Index of the compare channel to write to [in] compare New compare value to set Returns Status of the compare update procedure. Table 7-35 Return Values Return value Description STATUS_OK The compare value was updated successfully STATUS_ERR_INVALID_ARG An invalid channel index was supplied or compare value exceed resolution 7.4.9. Set Top Value 7.4.9.1. Function tcc_set_top_value() Set the timer TOP/PERIOD value. enum status_code tcc_set_top_value( const struct tcc_module *const module_inst, const uint32_t top_value) This function writes the given value to the PER/PERB register. If double buffering is enabled it always write to the buffer register (PERB). The value will then be updated immediately by calling tcc_force_double_buffer_update(), or be updated when the lock update bit is cleared and the UPDATE condition happen. When using MFRQ, the top value is defined by the CC0 register value and the PER value is ignored, so tcc_set_compare_value (module,channel_0,value) must be used instead of this function to change the actual top value in that case. For all other waveforms operation the top value is defined by PER register value. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 34 Table 7-36 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] top_value New value to be loaded into the PER/PERB register Returns Status of the TOP set procedure. Table 7-37 Return Values Return value Description STATUS_OK The timer TOP value was updated successfully STATUS_ERR_INVALID_ARG An invalid channel index was supplied or top/period value exceed resolution 7.4.10. Set Output Pattern 7.4.10.1. Function tcc_set_pattern() Sets the TCC module waveform output pattern. enum status_code tcc_set_pattern( const struct tcc_module *const module_inst, const uint32_t line_index, const enum tcc_output_pattern pattern) Force waveform output line to generate specific pattern (0, 1, or as is). If double buffering is enabled it always write to the buffer register. The value will then be updated immediately by calling tcc_force_double_buffer_update(), or be updated when the lock update bit is cleared and the UPDATE condition happen. Table 7-38 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] line_index Output line index [in] pattern Output pattern to use (tcc_output_pattern) Returns Status of the pattern set procedure. Table 7-39 Return Values Return value Description STATUS_OK The PATT register is updated successfully STATUS_ERR_INVALID_ARG An invalid line index was supplied Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 35 7.4.11. Set Ramp Index 7.4.11.1. Function tcc_set_ramp_index() Sets the TCC module ramp index on next cycle. void tcc_set_ramp_index( const struct tcc_module *const module_inst, const enum tcc_ramp_index ramp_index) In RAMP2 and RAMP2A operation, we can force either cycle A or cycle B at the output, on the next clock cycle. When ramp index command is disabled, cycle A and cycle B will appear at the output, on alternate clock cycles. See tcc_ramp. Table 7-40 Parameters 7.4.12. Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] ramp_index Ramp index (tcc_ramp_index) of the next cycle Status Management 7.4.12.1. Function tcc_is_running() Checks if the timer/counter is running. bool tcc_is_running( struct tcc_module *const module_inst) Table 7-41 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct Returns Status which indicates whether the module is running. Table 7-42 Return Values Return value Description true The timer/counter is running false The timer/counter is stopped 7.4.12.2. Function tcc_get_status() Retrieves the current module status. uint32_t tcc_get_status( struct tcc_module *const module_inst) Retrieves the status of the module, giving overall state information. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 36 Table 7-43 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct Returns Bitmask of TCC_STATUS_* flags. Table 7-44 Return Values Return value Description TCC_STATUS_CHANNEL_MATCH_CAPTURE(n) Channel n match/capture has occured TCC_STATUS_CHANNEL_OUTPUT(n) Channel n match/capture output state TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(x) Non-recoverable fault x has occured TCC_STATUS_RECOVERABLE_FAULT_OCCUR(n) Recoverable fault n has occured TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(x) Non-recoverable fault x input present TCC_STATUS_RECOVERABLE_FAULT_PRESENT(n) Recoverable fault n input present TCC_STATUS_SYNC_READY None of register is syncing TCC_STATUS_CAPTURE_OVERFLOW Timer capture data has overflowed TCC_STATUS_COUNTER_EVENT Timer counter event has occurred TCC_STATUS_COUNT_OVERFLOW Timer count value has overflowed TCC_STATUS_COUNTER_RETRIGGERED Timer counter has been retriggered TCC_STATUS_STOP Timer counter has been stopped TCC_STATUS_RAMP_CYCLE_INDEX Wave ramp index for cycle 7.4.12.3. Function tcc_clear_status() Clears a module status flag. void tcc_clear_status( struct tcc_module *const module_inst, const uint32_t status_flags) Clears the given status flag of the module. Table 7-45 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct [in] status_flags Bitmask of TCC_STATUS_* flags to clear Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 37 7.4.13. Double Buffering Management 7.4.13.1. Function tcc_enable_double_buffering() Enable TCC double buffering write. void tcc_enable_double_buffering( struct tcc_module *const module_inst) When double buffering write is enabled, the following function will write values to buffered registers instead of effective ones (buffered): • PERB: through tcc_set_top_value() • CCBx(x is 0~3): through tcc_set_compare_value() • PATTB: through tcc_set_pattern() Then, on UPDATE condition the buffered registers are committed to regular ones to take effect. Table 7-46 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct 7.4.13.2. Function tcc_disable_double_buffering() Disable TCC double buffering Write. void tcc_disable_double_buffering( struct tcc_module *const module_inst) When double buffering write is disabled, following function will write values to effective registers (not buffered): • PER: through tcc_set_top_value() • CCx(x is 0~3): through tcc_set_compare_value() • PATT: through tcc_set_pattern() Note: This function does not lock double buffer update, which means on next UPDATE condition the last written buffered values will be committed to take effect. Invoke tcc_lock_double_buffer_update() before this function to disable double buffering update, if this change is not expected. Table 7-47 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct 7.4.13.3. Function tcc_lock_double_buffer_update() Lock the TCC double buffered registers updates. void tcc_lock_double_buffer_update( struct tcc_module *const module_inst) Locks the double buffered registers so they will not be updated through their buffered values on UPDATE conditions. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 38 Table 7-48 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct 7.4.13.4. Function tcc_unlock_double_buffer_update() Unlock the TCC double buffered registers updates. void tcc_unlock_double_buffer_update( struct tcc_module *const module_inst) Unlock the double buffered registers so they will be updated through their buffered values on UPDATE conditions. Table 7-49 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct 7.4.13.5. Function tcc_force_double_buffer_update() Force the TCC double buffered registers to update once. void tcc_force_double_buffer_update( struct tcc_module *const module_inst) Table 7-50 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct 7.4.13.6. Function tcc_enable_circular_buffer_top() Enable Circular option for double buffered Top/Period Values. void tcc_enable_circular_buffer_top( struct tcc_module *const module_inst) Enable circular option for the double buffered top/period values. On each UPDATE condition, the contents of PERB and PER are switched, meaning that the contents of PERB are transferred to PER and the contents of PER are transferred to PERB. Table 7-51 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct 7.4.13.7. Function tcc_disable_circular_buffer_top() Disable Circular option for double buffered Top/Period Values. void tcc_disable_circular_buffer_top( struct tcc_module *const module_inst) Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 39 Stop circularing the double buffered top/period values. Table 7-52 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct 7.4.13.8. Function tcc_set_double_buffer_top_values() Set the timer TOP/PERIOD value and buffer value. enum status_code tcc_set_double_buffer_top_values( const struct tcc_module *const module_inst, const uint32_t top_value, const uint32_t top_buffer_value) This function writes the given value to the PER and PERB register. Usually as preparation for double buffer or circulared double buffer (circular buffer). When using MFRQ, the top values are defined by the CC0 and CCB0, the PER and PERB values are ignored, so tcc_set_double_buffer_compare_values (module,channel_0,value,buffer) must be used instead of this function to change the actual top values in that case. For all other waveforms operation the top values are defined by PER and PERB registers values. Table 7-53 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] top_value New value to be loaded into the PER register [in] top_buffer_value New value to be loaded into the PERB register Returns Status of the TOP set procedure. Table 7-54 Return Values Return value Description STATUS_OK The timer TOP value was updated successfully STATUS_ERR_INVALID_ARG An invalid channel index was supplied or top/period value exceed resolution 7.4.13.9. Function tcc_enable_circular_buffer_compare() Enable circular option for double buffered compare values. enum status_code tcc_enable_circular_buffer_compare( struct tcc_module *const module_inst, enum tcc_match_capture_channel channel_index) Enable circular option for the double buffered channel compare values. On each UPDATE condition, the contents of CCBx and CCx are switched, meaning that the contents of CCBx are transferred to CCx and the contents of CCx are transferred to CCBx. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 40 Table 7-55 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct [in] channel_index Index of the compare channel to set up to Table 7-56 Return Values Return value Description STATUS_OK The module was initialized successfully STATUS_INVALID_ARG An invalid channel index is supplied 7.4.13.10. Function tcc_disable_circular_buffer_compare() Disable circular option for double buffered compare values. enum status_code tcc_disable_circular_buffer_compare( struct tcc_module *const module_inst, enum tcc_match_capture_channel channel_index) Stop circularing the double buffered compare values. Table 7-57 Parameters Data direction Parameter name Description [in] module_inst Pointer to the TCC software instance struct [in] channel_index Index of the compare channel to set up to Table 7-58 Return Values Return value Description STATUS_OK The module was initialized successfully STATUS_INVALID_ARG An invalid channel index is supplied 7.4.13.11. Function tcc_set_double_buffer_compare_values() Sets a TCC module compare value and buffer value. enum status_code tcc_set_double_buffer_compare_values( struct tcc_module *const module_inst, enum tcc_match_capture_channel channel_index, const uint32_t compare, const uint32_t compare_buffer) Writes compare value and buffer to the given TCC module compare/capture channel. Usually as preparation for double buffer or circulared double buffer (circular buffer). Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 41 Table 7-59 Parameters Data direction Parameter name Description [in] module_inst Pointer to the software module instance struct [in] channel_index Index of the compare channel to write to [in] compare New compare value to set [in] compare_buffer New compare buffer value to set Returns Status of the compare update procedure. Table 7-60 Return Values Return value Description STATUS_OK The compare value was updated successfully STATUS_ERR_INVALID_ARG An invalid channel index was supplied or compare value exceed resolution 7.5. Enumeration Definitions 7.5.1. Enum tcc_callback Enum for the possible callback types for the TCC module. Table 7-61 Members Enum value Description TCC_CALLBACK_OVERFLOW Callback for TCC overflow TCC_CALLBACK_RETRIGGER Callback for TCC Retrigger TCC_CALLBACK_COUNTER_EVENT Callback for TCC counter event TCC_CALLBACK_ERROR Callback for capture overflow error TCC_CALLBACK_FAULTA Callback for Recoverable Fault A TCC_CALLBACK_FAULTB Callback for Recoverable Fault B TCC_CALLBACK_FAULT0 Callback for Non-Recoverable Fault 0 Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 42 Enum value Description TCC_CALLBACK_FAULT1 Callback for Non-Recoverable Fault 1 TCC_CALLBACK_CHANNEL_n Channel callback type table for TCC Each TCC module may contain several callback types for channels; each channel will have its own callback type in the table, with the channel index number substituted for "n" in the channel callback type (e.g. TCC_MATCH_CAPTURE_CHANNEL_0). 7.5.2. Enum tcc_channel_function To set a timer channel either in compare or in capture mode. Table 7-62 Members 7.5.3. Enum value Description TCC_CHANNEL_FUNCTION_COMPARE TCC channel performs compare operation TCC_CHANNEL_FUNCTION_CAPTURE TCC channel performs capture operation Enum tcc_clock_prescaler This enum is used to choose the clock prescaler configuration. The prescaler divides the clock frequency of the TCC module to operate TCC at a slower clock rate. Table 7-63 Members 7.5.4. Enum value Description TCC_CLOCK_PRESCALER_DIV1 Divide clock by 1 TCC_CLOCK_PRESCALER_DIV2 Divide clock by 2 TCC_CLOCK_PRESCALER_DIV4 Divide clock by 4 TCC_CLOCK_PRESCALER_DIV8 Divide clock by 8 TCC_CLOCK_PRESCALER_DIV16 Divide clock by 16 TCC_CLOCK_PRESCALER_DIV64 Divide clock by 64 TCC_CLOCK_PRESCALER_DIV256 Divide clock by 256 TCC_CLOCK_PRESCALER_DIV1024 Divide clock by 1024 Enum tcc_count_direction Used when selecting the Timer/Counter count direction. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 43 Table 7-64 Members 7.5.5. Enum value Description TCC_COUNT_DIRECTION_UP Timer should count upward TCC_COUNT_DIRECTION_DOWN Timer should count downward Enum tcc_count_overflow_dma_trigger_mode Used when selecting the Timer/Counter overflow DMA request mode. Table 7-65 Members Enum value Description TCC_COUNT_OVERFLOW_DMA_TRIGGER_MODE_CONTINUE TCC generates a DMA request on each cycle when an update condition is detected TCC_COUNT_OVERFLOW_DMA_TRIGGER_MODE_ONE_SHOT When an update condition is detected, the TCC generates a DMA trigger on the cycle following the DMA One-Shot Command written to the Control B register 7.5.6. Enum tcc_event0_action Event action to perform when the module is triggered by event0. Table 7-66 Members Enum value Description TCC_EVENT0_ACTION_OFF No event action TCC_EVENT0_ACTION_RETRIGGER Re-trigger Counter on event TCC_EVENT0_ACTION_COUNT_EVENT Count events (increment or decrement, depending on count direction) TCC_EVENT0_ACTION_START Start counter on event TCC_EVENT0_ACTION_INCREMENT Increment counter on event TCC_EVENT0_ACTION_COUNT_DURING_ACTIVE Count during active state of asynchronous event TCC_EVENT0_ACTION_NON_RECOVERABLE_FAULT Generate Non-Recoverable Fault on event 7.5.7. Enum tcc_event1_action Event action to perform when the module is triggered by event1. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 44 Table 7-67 Members Enum value Description TCC_EVENT1_ACTION_OFF No event action TCC_EVENT1_ACTION_RETRIGGER Re-trigger Counter on event TCC_EVENT1_ACTION_DIR_CONTROL The event source must be an asynchronous event, and the input value will override the direction settings. If TCEINVx is 0 and input event is LOW: counter will count up. If TCEINVx is 0 and input event is HIGH: counter will count down. TCC_EVENT1_ACTION_STOP Stop counter on event TCC_EVENT1_ACTION_DECREMENT Decrement on event TCC_EVENT1_ACTION_PERIOD_PULSE_WIDTH_CAPTURE Store period in capture register 0, pulse width in capture register 1 TCC_EVENT1_ACTION_PULSE_WIDTH_PERIOD_CAPTURE Store pulse width in capture register 0, period in capture register 1 TCC_EVENT1_ACTION_NON_RECOVERABLE_FAULT 7.5.8. Generate Non-Recoverable Fault on event Enum tcc_event_action Event action to perform when the module is triggered by events. Table 7-68 Members Enum value Description TCC_EVENT_ACTION_OFF No event action TCC_EVENT_ACTION_STOP Stop counting, the counter will maintain its current value, waveforms are set to a defined Non-Recoverable State output (tcc_non_recoverable_state_output). TCC_EVENT_ACTION_RETRIGGER Re-trigger counter on event, may generate an event if the re-trigger event output is enabled. Note: When re-trigger event action is enabled, enabling the counter will not start until the next incoming event appears. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 45 Enum value Description TCC_EVENT_ACTION_START Start counter when previously stopped. Start counting on the event rising edge. Further events will not restart the counter; the counter keeps on counting using prescaled GCLK_TCCx, until it reaches TOP or Zero depending on the direction. TCC_EVENT_ACTION_COUNT_EVENT Count events; i.e. Increment or decrement depending on count direction. TCC_EVENT_ACTION_DIR_CONTROL The event source must be an asynchronous event, input value will overrides the direction settings (input low: counting up, input high: counting down). TCC_EVENT_ACTION_INCREMENT Increment the counter on event, irrespective of count direction TCC_EVENT_ACTION_DECREMENT Decrement the counter on event, irrespective of count direction TCC_EVENT_ACTION_COUNT_DURING_ACTIVE Count during active state of asynchronous event. In this case, depending on the count direction, the count will be incremented or decremented on each prescaled GCLK_TCCx, as long as the input event remains active. TCC_EVENT_ACTION_PERIOD_PULSE_WIDTH_CAPTURE Store period in capture register 0, pulse width in capture register 1 TCC_EVENT_ACTION_PULSE_WIDTH_PERIOD_CAPTURE Store pulse width in capture register 0, period in capture register 1 TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT 7.5.9. Generate Non-Recoverable Fault on event Enum tcc_event_generation_selection This enum is used to define the point at which the counter event is generated. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 46 Table 7-69 Members Enum value Description TCC_EVENT_GENERATION_SELECTION_START Counter Event is generated when a new counter cycle starts TCC_EVENT_GENERATION_SELECTION_END Counter Event is generated when a counter cycle ends TCC_EVENT_GENERATION_SELECTION_BETWEEN Counter Event is generated when a counter cycle ends, except for the first and last cycles TCC_EVENT_GENERATION_SELECTION_BOUNDARY Counter Event is generated when a new counter cycle starts or ends 7.5.10. Enum tcc_fault_blanking Table 7-70 Members Enum value Description TCC_FAULT_BLANKING_DISABLE No blanking TCC_FAULT_BLANKING_RISING_EDGE Blanking applied from rising edge of the output waveform TCC_FAULT_BLANKING_FALLING_EDGE Blanking applied from falling edge of the output waveform TCC_FAULT_BLANKING_BOTH_EDGE 7.5.11. Blanking applied from each toggle of the output waveform Enum tcc_fault_capture_action Table 7-71 Members Enum value Description TCC_FAULT_CAPTURE_DISABLE Capture disabled TCC_FAULT_CAPTURE_EACH Capture on Fault, each value is captured TCC_FAULT_CAPTURE_MINIMUM Capture the minimum detection, but notify on smaller ones TCC_FAULT_CAPTURE_MAXIMUM Capture the maximum detection, but notify on bigger ones TCC_FAULT_CAPTURE_SMALLER Capture if the value is smaller than last, notify event or interrupt if previous stamp is confirmed to be "local minimum" (not bigger than current stamp). TCC_FAULT_CAPTURE_BIGGER Capture if the value is bigger than last, notify event or interrupt if previous stamp is confirmed to be "local maximum" (not smaller than current stamp). TCC_FAULT_CAPTURE_CHANGE Capture if the time stamps changes its increment direction Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 47 7.5.12. Enum tcc_fault_capture_channel Table 7-72 Members 7.5.13. Enum value Description TCC_FAULT_CAPTURE_CHANNEL_0 Recoverable fault triggers channel 0 capture operation TCC_FAULT_CAPTURE_CHANNEL_1 Recoverable fault triggers channel 1 capture operation TCC_FAULT_CAPTURE_CHANNEL_2 Recoverable fault triggers channel 2 capture operation TCC_FAULT_CAPTURE_CHANNEL_3 Recoverable fault triggers channel 3 capture operation Enum tcc_fault_halt_action Table 7-73 Members Enum value Description TCC_FAULT_HALT_ACTION_DISABLE Halt action disabled. TCC_FAULT_HALT_ACTION_HW_HALT Hardware halt action, counter is halted until restart TCC_FAULT_HALT_ACTION_SW_HALT Software halt action, counter is halted until fault bit cleared TCC_FAULT_HALT_ACTION_NON_RECOVERABLE Non-Recoverable fault, force output to predefined level 7.5.14. Enum tcc_fault_keep Table 7-74 Members 7.5.15. Enum value Description TCC_FAULT_KEEP_DISABLE Disable keeping, wave output released as soon as fault is released TCC_FAULT_KEEP_TILL_END Keep wave output until end of TCC cycle Enum tcc_fault_qualification Table 7-75 Members Enum value Description TCC_FAULT_QUALIFICATION_DISABLE The input is not disabled on compare condition TCC_FAULT_QUALIFICATION_BY_OUTPUT The input is disabled when match output signal is at inactive level Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 48 7.5.16. Enum tcc_fault_restart Table 7-76 Members 7.5.17. Enum value Description TCC_FAULT_RESTART_DISABLE Restart Action disabled TCC_FAULT_RESTART_ENABLE Restart Action enabled Enum tcc_fault_source Table 7-77 Members 7.5.18. Enum value Description TCC_FAULT_SOURCE_DISABLE Fault input is disabled TCC_FAULT_SOURCE_ENABLE Match Capture Event x (x=0,1) input TCC_FAULT_SOURCE_INVERT Inverted MCEx (x=0,1) event input TCC_FAULT_SOURCE_ALTFAULT Alternate fault (A or B) state at the end of the previous period Enum tcc_fault_state_output Table 7-78 Members 7.5.19. Enum value Description TCC_FAULT_STATE_OUTPUT_OFF Non-recoverable fault output is tri-stated TCC_FAULT_STATE_OUTPUT_0 Non-recoverable fault force output 0 TCC_FAULT_STATE_OUTPUT_1 Non-recoverable fault force output 1 Enum tcc_match_capture_channel This enum is used to specify which capture/match channel to do operations on. Table 7-79 Members Enum value Description TCC_MATCH_CAPTURE_CHANNEL_n Match capture channel index table for TCC Each TCC module may contain several match capture channels; each channel will have its own index in the table, with the index number substituted for "n" in the index name (e.g. TCC_MATCH_CAPTURE_CHANNEL_0). 7.5.20. Enum tcc_output_invertion Used when enabling or disabling output inversion. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 49 Table 7-80 Members 7.5.21. Enum value Description TCC_OUTPUT_INVERTION_DISABLE Output inversion not to be enabled TCC_OUTPUT_INVERTION_ENABLE Invert the output from WO[x] Enum tcc_output_pattern Used when disabling output pattern or when selecting a specific pattern. Table 7-81 Members 7.5.22. Enum value Description TCC_OUTPUT_PATTERN_DISABLE SWAP output pattern is not used TCC_OUTPUT_PATTERN_0 Pattern 0 is applied to SWAP output TCC_OUTPUT_PATTERN_1 Pattern 1 is applied to SWAP output Enum tcc_ramp Ramp operations which are supported in single-slope PWM generation. Table 7-82 Members Enum value Description TCC_RAMP_RAMP1 Default timer/counter PWM operation TCC_RAMP_RAMP2A Uses a single channel (CC0) to control both CC0/CC1 compare outputs. In cycle A, the channel 0 output is disabled, and in cycle B, the channel 1 output is disabled. TCC_RAMP_RAMP2 7.5.23. Uses channels CC0 and CC1 to control compare outputs. In cycle A, the channel 0 output is disabled, and in cycle B, the channel 1 output is disabled. Enum tcc_ramp_index In ramp operation, each two period cycles are marked as cycle A and B, the index 0 represents cycle A and 1 represents cycle B. Table 7-83 Members 7.5.24. Enum value Description TCC_RAMP_INDEX_DEFAULT Default, cycle index toggles. TCC_RAMP_INDEX_FORCE_B Force next cycle to be cycle B (set to 1) TCC_RAMP_INDEX_FORCE_A Force next cycle to be cycle A (clear to 0) TCC_RAMP_INDEX_FORCE_KEEP Force next cycle keeping the same as current Enum tcc_reload_action This enum specify how the counter is reloaded and whether the prescaler should be restarted. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 50 Table 7-84 Members Enum value Description TCC_RELOAD_ACTION_GCLK The counter is reloaded/reset on the next GCLK and starts counting on the prescaler clock TCC_RELOAD_ACTION_PRESC The counter is reloaded/reset on the next prescaler clock TCC_RELOAD_ACTION_RESYNC The counter is reloaded/reset on the next GCLK, and the prescaler is restarted as well 7.5.25. Enum tcc_wave_generation This enum is used to specify the waveform generation mode. Table 7-85 Members Enum value Description TCC_WAVE_GENERATION_NORMAL_FREQ Normal Frequency: Top is the PER register, output toggled on each compare match TCC_WAVE_GENERATION_MATCH_FREQ Match Frequency: Top is CC0 register, output toggles on each update condition TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM Single-Slope PWM: Top is the PER register, CCx controls duty cycle (output active when count is greater than CCx) TCC_WAVE_GENERATION_DOUBLE_SLOPE_CRITICAL Double-slope (count up and down), non centre-aligned: Top is the PER register, CC[x] controls duty cycle while counting up and CC[x+N/2] controls it while counting down TCC_WAVE_GENERATION_DOUBLE_SLOPE_BOTTOM Double-slope (count up and down), interrupt/event at Bottom (Top is the PER register, output active when count is greater than CCx) 7.5.26. TCC_WAVE_GENERATION_DOUBLE_SLOPE_BOTH Double-slope (count up and down), interrupt/event at Bottom and Top: (Top is the PER register, output active when count is lower than CCx) TCC_WAVE_GENERATION_DOUBLE_SLOPE_TOP Double-slope (count up and down), interrupt/event at Top (Top is the PER register, output active when count is greater than CCx) Enum tcc_wave_output This enum is used to specify which wave output to do operations on. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 51 Table 7-86 Members Enum value Description TCC_WAVE_OUTPUT_n Waveform output index table for TCC Each TCC module may contain several wave outputs; each output will have its own index in the table, with the index number substituted for "n" in the index name (e.g. TCC_WAVE_OUTPUT_0). 7.5.27. Enum tcc_wave_polarity Specifies whether the wave output needs to be inverted or not. Table 7-87 Members Enum value Description TCC_WAVE_POLARITY_0 Wave output is not inverted TCC_WAVE_POLARITY_1 Wave output is inverted Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 52 8. Extra Information for TCC Driver 8.1. Acronyms The table below presents the acronyms used in this module: 8.2. Acronym Description DMA Direct Memory Access TCC Timer Counter for Control Applications PWM Pulse Width Modulation PWP Pulse Width Period PPW Period Pulse Width Dependencies This driver has the following dependencies: • 8.3. System Pin Multiplexer Driver Errata There are no errata related to this driver. 8.4. Module History An overview of the module history is presented in the table below, with details on the enhancements and fixes made to the module since its first release. The current version of this corresponds to the newest version in the table. Changelog Add double buffering functionality Add fault handling functionality Initial Release Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 53 9. Examples for TCC Driver This is a list of the available Quick Start guides (QSGs) and example applications for SAM Timer Counter for Control Applications (TCC) Driver. QSGs are simple examples with step-by-step instructions to configure and use this driver in a selection of use cases. Note that QSGs can be compiled as a standalone application or be added to the user application. • • • • • • • 9.1. Quick Start Guide for TCC - Basic Quick Start Guide for TCC - Double Buffering and Circular Quick Start Guide for TCC - Timer Quick Start Guide for TCC - Callback Quick Start Guide for TCC - Non-Recoverable Fault Quick Start Guide for TCC - Recoverable Fault Quick Start Guide for Using DMA with TCC Quick Start Guide for TCC - Basic The supported board list: • SAM D21/R21/L21/L22/DA1/C21 Xplained Pro In this use case, the TCC will be used to generate a PWM signal. Here the pulse width is set to one quarter of the period. When the PWM signal connects to LED, LED will light. To see the waveform, you may need an oscilloscope. The PWM output is set up as follows: Board Pin Connect to SAM D21 Xpro PB30 LED0 SAM R21 Xpro PA19 LED0 SAM L21 Xpro PB10 LED0 SAM L22 Xpro PC27 LED0 SAM DA1 Xpro PB30 LED0 SAM C21 Xpro PA15 LED0 The TCC module will be set up as follows: • GCLK generator 0 (GCLK main) clock source • Use double buffering write when set top, compare, or pattern through API • No dithering on the counter or compare • Prescaler is set to 256 • Single Slope PWM wave generation • GCLK reload action • Don't run in standby • No fault or waveform extensions • No inversion of waveform output Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 54 • • • • • • • • No capture enabled Count upward Don't perform one-shot operations No event input enabled No event action No event generation enabled Counter starts on 0 Counter top set to 0xFFFF • Capture compare channel 0 set to 0xFFFF/4 9.1.1. Quick Start 9.1.1.1. Prerequisites There are no prerequisites for this use case. 9.1.1.2. Code Add to the main application source file, before any functions: #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX Add to the main application source file, outside of any functions: struct tcc_module tcc_instance; Copy-paste the following setup code to your user application: static void configure_tcc(void) { struct tcc_config config_tcc; tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV256; config_tcc.counter.period = 0xFFFF; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = (0xFFFF / 4); config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); } tcc_enable(&tcc_instance); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 55 Add to user application initialization (typically the start of main()): configure_tcc(); 9.1.1.3. Workflow 1. Create a module software instance structure for the TCC module to store the TCC driver state while it is in use. struct tcc_module tcc_instance; 2. Note: This should never go out of scope as long as the module is in use. In most cases, this should be global. Configure the TCC module. 1. Create a TCC module configuration struct, which can be filled out to adjust the configuration of a physical TCC peripheral. struct tcc_config config_tcc; 2. Initialize the TCC configuration struct with the module's default values. tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); 3. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Alter the TCC settings to configure the counter width, wave generation mode, and the compare channel 0 value. config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV256; config_tcc.counter.period = 0xFFFF; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = (0xFFFF / 4); 4. Alter the TCC settings to configure the PWM output on a physical device pin. config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; 5. Configure the TCC module with the desired settings. tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); 6. Enable the TCC module to start the timer and begin PWM signal generation. tcc_enable(&tcc_instance); 9.1.2. Use Case 9.1.2.1. Code Copy-paste the following code to your user application: while (true) { /* Infinite loop */ } Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 56 9.1.2.2. Workflow 1. Enter an infinite loop while the PWM wave is generated via the TCC module. while (true) { /* Infinite loop */ } 9.2. Quick Start Guide for TCC - Double Buffering and Circular The supported board list: • SAM D21/R21/L21/L22/DA1/C21 Xplained Pro In this use case, the TCC will be used to generate a PWM signal. Here the pulse width alters in one quarter and three quarter of the period. When the PWM signal connects to LED, LED will light. To see the waveform, you may need an oscilloscope. The PWM output is set up as follows: Board Pin Connect to SAM D21 Xpro PB30 LED0 SAM R21 Xpro PA19 LED0 SAM L21 Xpro PB10 LED0 SAM L22 Xpro PC27 LED0 SAM DA1 Xpro PB30 LED0 SAM C21 Xpro PA15 LED0 The TCC module will be set up as follows: • GCLK generator 0 (GCLK main) clock source • Use double buffering write when set top, compare, or pattern through API • No dithering on the counter or compare • Prescaler is set to 1024 • Single Slope PWM wave generation • GCLK reload action • Don't run in standby • No fault or waveform extensions • No inversion of waveform output • No capture enabled • Count upward • Don't perform one-shot operations • No event input enabled • No event action • No event generation enabled • Counter starts on 0 • Counter top set to 8000 Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 57 • • • Capture compare channel set to 8000/4 Capture compare channel buffer set to 8000*3/4 Circular option for compare channel is enabled so that the compare values keep switching on update condition 9.2.1. Quick Start 9.2.1.1. Prerequisites There are no prerequisites for this use case. 9.2.1.2. Code Add to the main application source file, before any functions: #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX Add to the main application source file, outside of any functions: struct tcc_module tcc_instance; Copy-paste the following setup code to your user application: static void configure_tcc(void) { struct tcc_config config_tcc; tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1024; config_tcc.counter.period = 8000; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = (8000 / 4); config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); tcc_set_compare_value(&tcc_instance, (enum tcc_match_capture_channel)CONF_PWM_CHANNEL, 8000*3/4); tcc_enable_circular_buffer_compare(&tcc_instance, (enum tcc_match_capture_channel)CONF_PWM_CHANNEL); } tcc_enable(&tcc_instance); Add to user application initialization (typically the start of main()): configure_tcc(); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 58 9.2.1.3. Workflow 1. Create a module software instance structure for the TCC module to store the TCC driver state while it is in use. struct tcc_module tcc_instance; 2. Note: This should never go out of scope as long as the module is in use. In most cases, this should be global. Configure the TCC module. 1. Create a TCC module configuration struct, which can be filled out to adjust the configuration of a physical TCC peripheral. struct tcc_config config_tcc; 2. Initialize the TCC configuration struct with the module's default values. tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); 3. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Alter the TCC settings to configure the counter width, wave generation mode, and the compare channel 0 value. config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1024; config_tcc.counter.period = 8000; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = (8000 / 4); 4. Alter the TCC settings to configure the PWM output on a physical device pin. config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; 5. Configure the TCC module with the desired settings. tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); 6. Set to compare buffer value and enable circular of double buffered compare values. tcc_set_compare_value(&tcc_instance, (enum tcc_match_capture_channel)CONF_PWM_CHANNEL, 8000*3/4); tcc_enable_circular_buffer_compare(&tcc_instance, (enum tcc_match_capture_channel)CONF_PWM_CHANNEL); 7. Enable the TCC module to start the timer and begin PWM signal generation. tcc_enable(&tcc_instance); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 59 9.2.2. Use Case 9.2.2.1. Code Copy-paste the following code to your user application: while (true) { /* Infinite loop */ } 9.2.2.2. Workflow 1. Enter an infinite loop while the PWM wave is generated via the TCC module. while (true) { /* Infinite loop */ } 9.3. Quick Start Guide for TCC - Timer The supported board list: • SAM D21/R21/L21/L22/DA1/C21 Xplained Pro • SAM D11 Xplained Pro In this use case, the TCC will be used as a timer, to generate overflow and compare match callbacks. In the callbacks the on-board LED is toggled. The TCC module will be set up as follows: • GCLK generator 1 (GCLK 32K) clock source • Use double buffering write when set top, compare, or pattern through API • No dithering on the counter or compare • Prescaler is divided by 64 • GCLK reload action • Count upward • Don't run in standby • No waveform outputs • No capture enabled • Don't perform one-shot operations • No event input enabled • No event action • No event generation enabled • Counter starts on 0 • Counter top set to 2000 (about 4s) and generate overflow callback • Channel 0 is set to compare and match value 900 and generate callback • Channel 1 is set to compare and match value 930 and generate callback • Channel 2 is set to compare and match value 1100 and generate callback • Channel 3 is set to compare and match value 1250 and generate callback Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 60 9.3.1. Quick Start 9.3.1.1. Prerequisites For this use case, XOSC32K should be enabled and available through GCLK generator 1 clock source selection. Within Atmel Software Framework (ASF) it can be done through modifying conf_clocks.h. See System Clock Management Driver for more details about clock configuration. 9.3.1.2. Code Add to the main application source file, outside of any functions: struct tcc_module tcc_instance; Copy-paste the following callback function code to your user application: static void tcc_callback_to_toggle_led( struct tcc_module *const module_inst) { port_pin_toggle_output_level(LED0_PIN); } Copy-paste the following setup code to your user application: static void configure_tcc(void) { struct tcc_config config_tcc; tcc_get_config_defaults(&config_tcc, TCC0); config_tcc.counter.clock_source = GCLK_GENERATOR_1; config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV64; config_tcc.counter.period = 2000; config_tcc.compare.match[0] = 900; config_tcc.compare.match[1] = 930; config_tcc.compare.match[2] = 1100; config_tcc.compare.match[3] = 1250; tcc_init(&tcc_instance, TCC0, &config_tcc); } tcc_enable(&tcc_instance); static void configure_tcc_callbacks(void) { tcc_register_callback(&tcc_instance, tcc_callback_to_toggle_led, TCC_CALLBACK_OVERFLOW); tcc_register_callback(&tcc_instance, tcc_callback_to_toggle_led, TCC_CALLBACK_CHANNEL_0); tcc_register_callback(&tcc_instance, tcc_callback_to_toggle_led, TCC_CALLBACK_CHANNEL_1); tcc_register_callback(&tcc_instance, tcc_callback_to_toggle_led, TCC_CALLBACK_CHANNEL_2); tcc_register_callback(&tcc_instance, tcc_callback_to_toggle_led, TCC_CALLBACK_CHANNEL_3); } tcc_enable_callback(&tcc_instance, tcc_enable_callback(&tcc_instance, tcc_enable_callback(&tcc_instance, tcc_enable_callback(&tcc_instance, tcc_enable_callback(&tcc_instance, TCC_CALLBACK_OVERFLOW); TCC_CALLBACK_CHANNEL_0); TCC_CALLBACK_CHANNEL_1); TCC_CALLBACK_CHANNEL_2); TCC_CALLBACK_CHANNEL_3); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 61 Add to user application initialization (typically the start of main()): configure_tcc(); configure_tcc_callbacks(); 9.3.1.3. Workflow 1. Create a module software instance structure for the TCC module to store the TCC driver state while it is in use. struct tcc_module tcc_instance; 2. Note: This should never go out of scope as long as the module is in use. In most cases, this should be global. Configure the TCC module. 1. Create a TCC module configuration struct, which can be filled out to adjust the configuration of a physical TCC peripheral. struct tcc_config config_tcc; 2. Initialize the TCC configuration struct with the module's default values. tcc_get_config_defaults(&config_tcc, TCC0); 3. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Alter the TCC settings to configure the GCLK source, prescaler, period, and compare channel values. config_tcc.counter.clock_source = GCLK_GENERATOR_1; config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV64; config_tcc.counter.period = 2000; config_tcc.compare.match[0] = 900; config_tcc.compare.match[1] = 930; config_tcc.compare.match[2] = 1100; config_tcc.compare.match[3] = 1250; 4. Configure the TCC module with the desired settings. tcc_init(&tcc_instance, TCC0, &config_tcc); 5. Enable the TCC module to start the timer. tcc_enable(&tcc_instance); 3. Configure the TCC callbacks. 1. Register the Overflow and Compare Channel Match callback functions with the driver. tcc_register_callback(&tcc_instance, TCC_CALLBACK_OVERFLOW); tcc_register_callback(&tcc_instance, TCC_CALLBACK_CHANNEL_0); tcc_register_callback(&tcc_instance, TCC_CALLBACK_CHANNEL_1); tcc_register_callback(&tcc_instance, TCC_CALLBACK_CHANNEL_2); tcc_register_callback(&tcc_instance, TCC_CALLBACK_CHANNEL_3); tcc_callback_to_toggle_led, tcc_callback_to_toggle_led, tcc_callback_to_toggle_led, tcc_callback_to_toggle_led, tcc_callback_to_toggle_led, Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 62 2. Enable the Overflow and Compare Channel Match callbacks so that it will be called by the driver when appropriate. tcc_enable_callback(&tcc_instance, tcc_enable_callback(&tcc_instance, tcc_enable_callback(&tcc_instance, tcc_enable_callback(&tcc_instance, tcc_enable_callback(&tcc_instance, 9.3.2. Use Case 9.3.2.1. Code TCC_CALLBACK_OVERFLOW); TCC_CALLBACK_CHANNEL_0); TCC_CALLBACK_CHANNEL_1); TCC_CALLBACK_CHANNEL_2); TCC_CALLBACK_CHANNEL_3); Copy-paste the following code to your user application: system_interrupt_enable_global(); while (true) { } 9.3.2.2. Workflow 1. Enter an infinite loop while the timer is running. while (true) { } 9.4. Quick Start Guide for TCC - Callback The supported board list: • SAM D21/R21/L21/L22/DA1/C21 Xplained Pro In this use case, the TCC will be used to generate a PWM signal, with a varying duty cycle. Here the pulse width is increased each time the timer count matches the set compare value. When the PWM signal connects to LED, LED will light. To see the waveform, you may need an oscilloscope. The PWM output is set up as follows: Board Pin Connect to SAM D21 Xpro PB30 LED0 SAM R21 Xpro PA19 LED0 SAM L21 Xpro PB10 LED0 SAM L22 Xpro PC27 LED0 SAM DA1 Xpro PB30 LED0 SAM C21 Xpro PA15 LED0 The TCC module will be set up as follows: • GCLK generator 0 (GCLK main) clock source • Use double buffering write when set top, compare, or pattern through API • No dithering on the counter or compare Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 63 • • • • • • • • No prescaler Single Slope PWM wave generation GCLK reload action Don't run in standby No faults or waveform extensions No inversion of waveform output No capture enabled Count upward • • • • • Don't perform one-shot operations No event input enabled No event action No event generation enabled Counter starts on 0 9.4.1. Quick Start 9.4.1.1. Prerequisites There are no prerequisites for this use case. 9.4.1.2. Code Add to the main application source file, before any functions: #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX Add to the main application source file, outside of any functions: struct tcc_module tcc_instance; Copy-paste the following callback function code to your user application: static void tcc_callback_to_change_duty_cycle( struct tcc_module *const module_inst) { static uint32_t delay = 10; static uint32_t i = 0; } if (--delay) { return; } delay = 10; i = (i + 0x0800) & 0xFFFF; tcc_set_compare_value(module_inst, (enum tcc_match_capture_channel) (TCC_MATCH_CAPTURE_CHANNEL_0 + CONF_PWM_CHANNEL), i + 1); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 64 Copy-paste the following setup code to your user application: static void configure_tcc(void) { struct tcc_config config_tcc; tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); config_tcc.counter.period = 0xFFFF; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF; config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); } tcc_enable(&tcc_instance); static void configure_tcc_callbacks(void) { tcc_register_callback( &tcc_instance, tcc_callback_to_change_duty_cycle, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); tcc_enable_callback(&tcc_instance, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); } Add to user application initialization (typically the start of main()): configure_tcc(); configure_tcc_callbacks(); 9.4.1.3. Workflow 1. Create a module software instance structure for the TCC module to store the TCC driver state while it is in use. struct tcc_module tcc_instance; 2. Note: This should never go out of scope as long as the module is in use. In most cases, this should be global. Configure the TCC module. 1. Create a TCC module configuration struct, which can be filled out to adjust the configuration of a physical TCC peripheral. struct tcc_config config_tcc; 2. Initialize the TCC configuration struct with the module's default values. tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 65 3. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Alter the TCC settings to configure the counter width, wave generation mode, and the compare channel 0 value. config_tcc.counter.period = 0xFFFF; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF; 4. Alter the TCC settings to configure the PWM output on a physical device pin. config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; 5. Configure the TCC module with the desired settings. tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); 6. Enable the TCC module to start the timer and begin PWM signal generation. tcc_enable(&tcc_instance); 3. Configure the TCC callbacks. 1. Register the Compare Channel 0 Match callback functions with the driver. tcc_register_callback( &tcc_instance, tcc_callback_to_change_duty_cycle, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); 2. Enable the Compare Channel 0 Match callback so that it will be called by the driver when appropriate. tcc_enable_callback(&tcc_instance, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); 9.4.2. Use Case 9.4.2.1. Code Copy-paste the following code to your user application: system_interrupt_enable_global(); while (true) { } 9.4.2.2. Workflow 1. Enter an infinite loop while the PWM wave is generated via the TCC module. while (true) { } Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 66 9.5. Quick Start Guide for TCC - Non-Recoverable Fault The supported kit list: • SAM D21/R21/L21/L22/DA1/C21 Xplained Pro In this use case, the TCC will be used to generate a PWM signal, with a varying duty cycle. Here the pulse width is increased each time the timer count matches the set compare value. There is a nonrecoverable fault input which controls PWM output. When this fault is active (low) the PWM output will be forced to be high. When fault is released (input high) the PWM output will go on. When the PWM signal connects to LED, LED will light. If fault input is from a button, the LED will be off when the button is down and on when the button is up. To see the PWM waveform, you may need an oscilloscope. The PWM output and fault input is set up as follows: Board Pin Connect to SAM D21 Xpro PB30 LED0 SAM D21 Xpro PA15 SW0 SAM R21 Xpro PA19 LED0 SAM R21 Xpro PA28 SW0 SAM L21 Xpro PB10 LED0 SAM L21 Xpro PA16 SW0 SAM L22 Xpro PC27 LED0 SAM L22 Xpro PC01 SW0 SAM DA1 Xpro PB30 LED0 SAM DA1 Xpro PA15 SW0 SAM C21 Xpro PA15 LED0 SAM C21 Xpro PA28 SW0 The TCC module will be set up as follows: • GCLK generator 0 (GCLK main) clock source • Use double buffering write when set top, compare, or pattern through API • No dithering on the counter or compare • No prescaler • Single Slope PWM wave generation • GCLK reload action • Don't run in standby • No waveform extentions • No inversion of waveform output • No capture enabled Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 67 • • • • • • Count upward Don't perform one-shot operations No event input except TCC event0 enabled No event action except TCC event0 acts as Non-Recoverable Fault No event generation enabled Counter starts on 0 9.5.1. Quick Start 9.5.1.1. Prerequisites There are no prerequisites for this use case. 9.5.1.2. Code Add to the main application source file, before any functions: • SAM D21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_15 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC0_EV_0 SAM R21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_8 #define CONF_FAULT_EVENT_USER EVSYS_ID_USER_TCC0_EV_0 Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 68 • SAM L21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_2 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC0_EV_0 SAM L22 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_9 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC0_EV_0 SAM DA1 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 69 #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_15 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC0_EV_0 SAM C21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_8 #define CONF_FAULT_EVENT_USER EVSYS_ID_USER_TCC0_EV_0 Add to the main application source file, before any functions: #include <string.h> Add to the main application source file, outside of any functions: struct tcc_module tcc_instance; struct events_resource event_resource; Copy-paste the following callback function code to your user application: static void tcc_callback_to_change_duty_cycle( struct tcc_module *const module_inst) { static uint32_t delay = 10; static uint32_t i = 0; } if (--delay) { return; } delay = 10; i = (i + 0x0800) & 0xFFFF; tcc_set_compare_value(module_inst, (enum tcc_match_capture_channel) (TCC_MATCH_CAPTURE_CHANNEL_0 + CONF_PWM_CHANNEL), i + 1); static void eic_callback_to_clear_halt(void) { if (port_pin_get_input_level(CONF_FAULT_EIC_PIN)) { tcc_clear_status(&tcc_instance, TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(0)); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 70 } } Copy-paste the following setup code to your user application: static void configure_tcc(void) { struct tcc_config config_tcc; tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); config_tcc.counter.period = 0xFFFF; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF; config_tcc.wave_ext.non_recoverable_fault[0].output = TCC_FAULT_STATE_OUTPUT_1; config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); struct tcc_events events; memset(&events, 0, sizeof(struct tcc_events)); events.on_input_event_perform_action[0] = true; events.input_config[0].modify_action = true; events.input_config[0].action = TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT; tcc_enable_events(&tcc_instance, &events); } tcc_enable(&tcc_instance); static void configure_tcc_callbacks(void) { tcc_register_callback( &tcc_instance, tcc_callback_to_change_duty_cycle, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); tcc_enable_callback(&tcc_instance, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); } static void configure_eic(void) { struct extint_chan_conf config; extint_chan_get_config_defaults(&config); config.filter_input_signal = true; config.detection_criteria = EXTINT_DETECT_BOTH; config.gpio_pin = CONF_FAULT_EIC_PIN; config.gpio_pin_mux = CONF_FAULT_EIC_PIN_MUX; extint_chan_set_config(CONF_FAULT_EIC_LINE, &config); struct extint_events events; Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 71 memset(&events, 0, sizeof(struct extint_events)); events.generate_event_on_detect[CONF_FAULT_EIC_LINE] = true; extint_enable_events(&events); } extint_register_callback(eic_callback_to_clear_halt, CONF_FAULT_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); extint_chan_enable_callback(CONF_FAULT_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); static void configure_event(void) { struct events_config config; events_get_config_defaults(&config); config.generator = CONF_FAULT_EVENT_GENERATOR; config.path = EVENTS_PATH_ASYNCHRONOUS; events_allocate(&event_resource, &config); } events_attach_user(&event_resource, CONF_FAULT_EVENT_USER); Add to user application initialization (typically the start of main()): configure_tcc(); configure_tcc_callbacks(); configure_eic(); configure_event(); 9.5.1.3. Workflow Configure TCC 1. Create a module software instance struct for the TCC module to store the TCC driver state while it is in use. struct tcc_module tcc_instance; 2. Note: This should never go out of scope as long as the module is in use. In most cases, this should be global. Create a TCC module configuration struct, which can be filled out to adjust the configuration of a physical TCC peripheral. struct tcc_config config_tcc; 3. Initialize the TCC configuration struct with the module's default values. tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); 4. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Alter the TCC settings to configure the counter width, wave generation mode, and the compare channel 0 value and fault options. Here the Non-Recoverable Fault output is enabled and set to high level (1). config_tcc.counter.period = 0xFFFF; config_tcc.compare.wave_generation = Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 72 TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF; config_tcc.wave_ext.non_recoverable_fault[0].output = TCC_FAULT_STATE_OUTPUT_1; 5. Alter the TCC settings to configure the PWM output on a physical device pin. config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; 6. Configure the TCC module with the desired settings. tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); 7. Create a TCC events configuration struct, which can be filled out to enable/disable events and configure event settings. Reset all fields to zero. struct tcc_events events; memset(&events, 0, sizeof(struct tcc_events)); 8. Alter the TCC events settings to enable/disable desired events, to change event generating options and modify event actions. Here TCC event0 will act as Non-Recoverable Fault input. events.on_input_event_perform_action[0] = true; events.input_config[0].modify_action = true; events.input_config[0].action = TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT; 9. Enable and apply events settings. tcc_enable_events(&tcc_instance, &events); 10. Enable the TCC module to start the timer and begin PWM signal generation. tcc_enable(&tcc_instance); 11. Register the Compare Channel 0 Match callback functions with the driver. tcc_register_callback( &tcc_instance, tcc_callback_to_change_duty_cycle, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); 12. Enable the Compare Channel 0 Match callback so that it will be called by the driver when appropriate. tcc_enable_callback(&tcc_instance, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); Configure EXTINT for fault input 1. Create an EXTINT module channel configuration struct, which can be filled out to adjust the configuration of a single external interrupt channel. struct extint_chan_conf config; 2. Initialize the channel configuration struct with the module's default values. extint_chan_get_config_defaults(&config); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 73 3. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Adjust the configuration struct to configure the pin MUX (to route the desired physical pin to the logical channel) to the board button, and to configure the channel to detect both rising and falling edges. config.filter_input_signal = true; config.detection_criteria = EXTINT_DETECT_BOTH; config.gpio_pin = CONF_FAULT_EIC_PIN; config.gpio_pin_mux = CONF_FAULT_EIC_PIN_MUX; 4. Configure external interrupt channel with the desired channel settings. extint_chan_set_config(CONF_FAULT_EIC_LINE, &config); 5. Create a TXTINT events configuration struct, which can be filled out to enable/disable events. Reset all fields to zero. struct extint_events events; memset(&events, 0, sizeof(struct extint_events)); 6. Adjust the configuration struct, set the channels to be enabled to true. Here the channel to the board button is used. events.generate_event_on_detect[CONF_FAULT_EIC_LINE] = true; 7. Enable the events. extint_enable_events(&events); 8. Define the EXTINT callback that will be fired when a detection event occurs. For this example, when fault line is released, the TCC fault state is cleared to go on PWM generating. static void eic_callback_to_clear_halt(void) { if (port_pin_get_input_level(CONF_FAULT_EIC_PIN)) { tcc_clear_status(&tcc_instance, TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(0)); } } 9. Register a callback function eic_callback_to_clear_halt() to handle detections from the External Interrupt Controller (EIC). extint_register_callback(eic_callback_to_clear_halt, CONF_FAULT_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); 10. Enable the registered callback function for the configured External Interrupt channel, so that it will be called by the module when the channel detects an edge. extint_chan_enable_callback(CONF_FAULT_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); Configure EVENTS for fault input 1. Create an event resource instance struct for the EVENTS module to store. struct events_resource event_resource; Note: This should never go out of scope as long as the resource is in use. In most cases, this should be global. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 74 2. Create an event channel configuration struct, which can be filled out to adjust the configuration of a single event channel. struct events_config config; 3. Initialize the event channel configuration struct with the module's default values. events_get_config_defaults(&config); Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. 4. Adjust the configuration struct to request that the channel will be attached to the specified event generator, and that the asynchronous event path will be used. Here the EIC channel connected to board button is the event generator. config.generator = CONF_FAULT_EVENT_GENERATOR; config.path = EVENTS_PATH_ASYNCHRONOUS; 5. Allocate and configure the channel using the configuration structure. events_allocate(&event_resource, &config); 6. Note: The existing configuration struct may be re-used, as long as any values that have been altered from the default settings are taken into account by the user application. Attach a user to the channel. Here the user is TCC event0, which has been configured as input of Non-Recoverable Fault. events_attach_user(&event_resource, CONF_FAULT_EVENT_USER); 9.5.2. Use Case 9.5.2.1. Code Copy-paste the following code to your user application: system_interrupt_enable_global(); while (true) { } 9.5.2.2. Workflow 1. Enter an infinite loop while the PWM wave is generated via the TCC module. while (true) { } 9.6. Quick Start Guide for TCC - Recoverable Fault The supported board list: • SAM D21/R21/L21/L22/DA1/C21 Xplained Pro In this use case, the TCC will be used to generate a PWM signal, with a varying duty cycle. Here the pulse width is increased each time the timer count matches the set compare value. There is a recoverable fault input which controls PWM output. When this fault is active (low) the PWM output will be frozen (could be off or on, no light changing). When fault is released (input high) the PWM output will go on. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 75 When the PWM signal connects to LED, LED will light. If fault input is from a button, the LED will be frozen and not changing it's light when the button is down and will go on when the button is up. To see the PWM waveform, you may need an oscilloscope. The PWM output and fault input is set up as follows: Board Pin Connect to SAM D21 Xpro PB30 LED0 SAM D21 Xpro PA15 SW0 SAM R21 Xpro PA06 EXT1 Pin 3 SAM R21 Xpro PA28 SW0 SAM L21 Xpro PB10 LED0 SAM L21 Xpro PA16 SW0 SAM L22 Xpro PB18 EXT3 Pin 9 SAM L22 Xpro PC01 SW0 SAM DA1 Xpro PB30 LED0 SAM DA1 Xpro PA15 SW0 SAM C21 Xpro PA15 LED0 SAM C21 Xpro PA28 SW0 The TCC module will be set up as follows: • GCLK generator 0 (GCLK main) clock source • Use double buffering write when set top, compare, or pattern through API • No dithering on the counter or compare • No prescaler • Single Slope PWM wave generation • GCLK reload action • Don't run in standby • No waveform extentions • No inversion of waveform output • No capture enabled • Count upward • Don't perform one-shot operations • No event input except channel 0 event enabled • No event action • No event generation enabled • Counter starts on 0 • Recoverable Fault A is generated from channel 0 event input, fault halt acts as software halt, other actions or options are all disabled Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 76 9.6.1. Quick Start 9.6.1.1. Prerequisites There are no prerequisites for this use case. 9.6.1.2. Code Add to the main application source file, before any functions, according to the kit used: • SAM D21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_15 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC0_MC_0 SAM R21 Xplained Pro #define CONF_PWM_MODULE TCC1 #define CONF_PWM_CHANNEL 0 #define CONF_PWM_OUTPUT 0 #define CONF_PWM_OUT_PIN PIN_PA06E_TCC1_WO0 #define CONF_PWM_OUT_MUX MUX_PA06E_TCC1_WO0 #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_8 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC1_MC_0 SAM L21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 77 #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_2 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC0_MC_0 SAM L22 Xplained Pro #define CONF_PWM_MODULE TCC0 #define CONF_PWM_CHANNEL 0 #define CONF_PWM_OUTPUT 0 #define CONF_PWM_OUT_PIN PIN_PB18F_TCC0_WO0 #define CONF_PWM_OUT_MUX MUX_PB18F_TCC0_WO0 #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_9 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC0_MC_0 SAM DA1 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_15 #define CONF_FAULT_EVENT_USER • EVSYS_ID_USER_TCC0_MC_0 SAM C21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 78 #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_FAULT_EIC_PIN SW0_EIC_PIN #define CONF_FAULT_EIC_PIN_MUX SW0_EIC_PINMUX #define CONF_FAULT_EIC_LINE SW0_EIC_LINE #define CONF_FAULT_EVENT_GENERATOR EVSYS_ID_GEN_EIC_EXTINT_8 #define CONF_FAULT_EVENT_USER EVSYS_ID_USER_TCC0_EV_0 Add to the main application source file, before any functions: #include <string.h> Add to the main application source file, outside of any functions: struct tcc_module tcc_instance; struct events_resource event_resource; Copy-paste the following callback function code to your user application: static void tcc_callback_to_change_duty_cycle( struct tcc_module *const module_inst) { static uint32_t delay = 10; static uint32_t i = 0; } if (--delay) { return; } delay = 10; i = (i + 0x0800) & 0xFFFF; tcc_set_compare_value(module_inst, (enum tcc_match_capture_channel) (TCC_MATCH_CAPTURE_CHANNEL_0 + CONF_PWM_CHANNEL), i + 1); static void eic_callback_to_clear_halt(void) { if (port_pin_get_input_level(CONF_FAULT_EIC_PIN)) { tcc_clear_status(&tcc_instance, TCC_STATUS_RECOVERABLE_FAULT_OCCUR(CONF_PWM_CHANNEL)); } } Copy-paste the following setup code to your user application: static void configure_tcc(void) { struct tcc_config config_tcc; Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 79 tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); config_tcc.counter.period = 0xFFFF; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF; config_tcc.wave_ext.recoverable_fault[CONF_PWM_CHANNEL].source = TCC_FAULT_SOURCE_ENABLE; config_tcc.wave_ext.recoverable_fault[CONF_PWM_CHANNEL].halt_action = TCC_FAULT_HALT_ACTION_SW_HALT; config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); struct tcc_events events; memset(&events, 0, sizeof(struct tcc_events)); events.on_event_perform_channel_action[CONF_PWM_CHANNEL] = true; tcc_enable_events(&tcc_instance, &events); } tcc_enable(&tcc_instance); static void configure_tcc_callbacks(void) { tcc_register_callback( &tcc_instance, tcc_callback_to_change_duty_cycle, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); tcc_enable_callback(&tcc_instance, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); } static void configure_eic(void) { struct extint_chan_conf config; extint_chan_get_config_defaults(&config); config.filter_input_signal = true; config.detection_criteria = EXTINT_DETECT_BOTH; config.gpio_pin = CONF_FAULT_EIC_PIN; config.gpio_pin_mux = CONF_FAULT_EIC_PIN_MUX; extint_chan_set_config(CONF_FAULT_EIC_LINE, &config); struct extint_events events; memset(&events, 0, sizeof(struct extint_events)); events.generate_event_on_detect[CONF_FAULT_EIC_LINE] = true; extint_enable_events(&events); extint_register_callback(eic_callback_to_clear_halt, CONF_FAULT_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); extint_chan_enable_callback(CONF_FAULT_EIC_LINE, Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 80 EXTINT_CALLBACK_TYPE_DETECT); } static void configure_event(void) { struct events_config config; events_get_config_defaults(&config); config.generator = CONF_FAULT_EVENT_GENERATOR; config.path = EVENTS_PATH_ASYNCHRONOUS; events_allocate(&event_resource, &config); } events_attach_user(&event_resource, CONF_FAULT_EVENT_USER); Add to user application initialization (typically the start of main()): configure_tcc(); configure_tcc_callbacks(); configure_eic(); configure_event(); 9.6.1.3. Workflow Configure TCC 1. Create a module software instance struct for the TCC module to store the TCC driver state while it is in use. struct tcc_module tcc_instance; 2. Note: This should never go out of scope as long as the module is in use. In most cases, this should be global. Create a TCC module configuration struct, which can be filled out to adjust the configuration of a physical TCC peripheral. struct tcc_config config_tcc; 3. Initialize the TCC configuration struct with the module's default values. tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); 4. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Alter the TCC settings to configure the counter width, wave generation mode, and the compare channel 0 value and fault options. Here the Recoverable Fault input is enabled and halt action is set to software mode (must use software to clear halt state). config_tcc.counter.period = 0xFFFF; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF; config_tcc.wave_ext.recoverable_fault[CONF_PWM_CHANNEL].source = TCC_FAULT_SOURCE_ENABLE; Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 81 config_tcc.wave_ext.recoverable_fault[CONF_PWM_CHANNEL].halt_action = TCC_FAULT_HALT_ACTION_SW_HALT; 5. Alter the TCC settings to configure the PWM output on a physical device pin. config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; 6. Configure the TCC module with the desired settings. tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); 7. Create a TCC events configuration struct, which can be filled out to enable/disable events and configure event settings. Reset all fields to zero. struct tcc_events events; memset(&events, 0, sizeof(struct tcc_events)); 8. Alter the TCC events settings to enable/disable desired events, to change event generating options and modify event actions. Here channel event 0 input is enabled as source of recoverable fault. events.on_event_perform_channel_action[CONF_PWM_CHANNEL] = true; 9. Enable and apply events settings. tcc_enable_events(&tcc_instance, &events); 10. Enable the TCC module to start the timer and begin PWM signal generation. tcc_enable(&tcc_instance); 11. Register the Compare Channel 0 Match callback functions with the driver. tcc_register_callback( &tcc_instance, tcc_callback_to_change_duty_cycle, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); 12. Enable the Compare Channel 0 Match callback so that it will be called by the driver when appropriate. tcc_enable_callback(&tcc_instance, (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL)); Configure EXTINT for fault input 1. Create an EXTINT module channel configuration struct, which can be filled out to adjust the configuration of a single external interrupt channel. struct extint_chan_conf config; 2. Initialize the channel configuration struct with the module's default values. extint_chan_get_config_defaults(&config); Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 82 3. Adjust the configuration struct to configure the pin MUX (to route the desired physical pin to the logical channel) to the board button, and to configure the channel to detect both rising and falling edges. config.filter_input_signal = true; config.detection_criteria = EXTINT_DETECT_BOTH; config.gpio_pin = CONF_FAULT_EIC_PIN; config.gpio_pin_mux = CONF_FAULT_EIC_PIN_MUX; 4. Configure external interrupt channel with the desired channel settings. extint_chan_set_config(CONF_FAULT_EIC_LINE, &config); 5. Create a TXTINT events configuration struct, which can be filled out to enable/disable events. Reset all fields to zero. struct extint_events events; memset(&events, 0, sizeof(struct extint_events)); 6. Adjust the configuration struct, set the channels to be enabled to true. Here the channel to the board button is used. events.generate_event_on_detect[CONF_FAULT_EIC_LINE] = true; 7. Enable the events. extint_enable_events(&events); 8. Define the EXTINT callback that will be fired when a detection event occurs. For this example, when fault line is released, the TCC fault state is cleared to go on PWM generating. static void eic_callback_to_clear_halt(void) { if (port_pin_get_input_level(CONF_FAULT_EIC_PIN)) { tcc_clear_status(&tcc_instance, TCC_STATUS_RECOVERABLE_FAULT_OCCUR(CONF_PWM_CHANNEL)); } } 9. Register a callback function eic_callback_to_clear_halt() to handle detections from the External Interrupt Controller (EIC). extint_register_callback(eic_callback_to_clear_halt, CONF_FAULT_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); 10. Enable the registered callback function for the configured External Interrupt channel, so that it will be called by the module when the channel detects an edge. extint_chan_enable_callback(CONF_FAULT_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); Configure EVENTS for fault input 1. Create an event resource instance struct for the EVENTS module to store. struct events_resource event_resource; 2. Note: This should never go out of scope as long as the resource is in use. In most cases, this should be global. Create an event channel configuration struct, which can be filled out to adjust the configuration of a single event channel. struct events_config config; Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 83 3. Initialize the event channel configuration struct with the module's default values. events_get_config_defaults(&config); 4. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Adjust the configuration struct to request that the channel will be attached to the specified event generator, and that the asynchronous event path will be used. Here the EIC channel connected to board button is the event generator. config.generator = CONF_FAULT_EVENT_GENERATOR; config.path = EVENTS_PATH_ASYNCHRONOUS; 5. Allocate and configure the channel using the configuration structure. events_allocate(&event_resource, &config); 6. Note: The existing configuration struct may be re-used, as long as any values that have been altered from the default settings are taken into account by the user application. Attach a user to the channel. Here the user is TCC channel 0 event, which has been configured as input of Recoverable Fault. events_attach_user(&event_resource, CONF_FAULT_EVENT_USER); 9.6.2. Use Case 9.6.2.1. Code Copy-paste the following code to your user application: system_interrupt_enable_global(); while (true) { } 9.6.2.2. Workflow 1. Enter an infinite loop while the PWM wave is generated via the TCC module. while (true) { } 9.7. Quick Start Guide for Using DMA with TCC The supported board list: • SAM D21/R21/L21/L22/DA1/C21 Xplained Pro In this use case, the TCC will be used to generate a PWM signal. Here the pulse width varies through the following values with the help of DMA transfer: one quarter of the period, half of the period, and three quarters of the period. The PWM output can be used to drive a LED. The waveform can also be viewed using an oscilloscope. The output signal is also fed back to another TCC channel by event system, the event stamps are captured and transferred to a buffer by DMA. The PWM output is set up as follows: Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 84 Board Pin Connect to SAM D21 Xpro PB30 LED0 SAM R21 Xpro PA19 LED0 SAM L21 Xpro PB10 LED0 SAM L22 Xpro PC27 LED0 SAM DA1 Xpro PB30 LED0 SAM C21 Xpro PA15 LED0 The TCC module will be setup as follows: • GCLK generator 0 (GCLK main) clock source • Use double buffering write when set top, compare, or pattern through API • No dithering on the counter or compare • Prescaler is set to 1024 • Single Slope PWM wave generation • GCLK reload action • Don't run in standby • No fault or waveform extensions • No inversion of waveform output • No capture enabled • Count upward • Don't perform one-shot operations • Counter starts on 0 • Counter top set to 0x1000 • Channel 0 (on SAM D21 Xpro) or 3 (on SAM R21 Xpro) is set to compare and match value 0x1000*3/4 and generate event • Channel 1 is set to capture on input event The event resource of EVSYS module will be setup as follows: • TCC match capture channel 0 (on SAM D21 Xpro) or 3 (on SAM R21 Xpro) is selected as event generator • Event generation is synchronous, with rising edge detected • TCC match capture channel 1 is the event user The DMA resource of DMAC module will be setup as follows: • Two DMA resources are used • Both DMA resources use peripheral trigger • Both DMA resources perform beat transfer on trigger • Both DMA resources use beat size of 16 bits • Both DMA resources are configured to transfer three beats and then repeat again in same buffer • On DMA resource which controls the compare value • TCC0 overflow triggers DMA transfer • The source address increment is enabled Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 85 • • The destination address is fixed to TCC channel 0 Compare/Capture register On DMA resource which reads the captured value • TCC0 capture on channel 1 triggers DMA transfer • The source address is fixed to TCC channel 1 Compare/Capture register • The destination address increment is enabled • The captured value is transferred to an array in SRAM 9.7.1. Quick Start 9.7.1.1. Prerequisites There are no prerequisites for this use case. 9.7.1.2. Code Add to the main application source file, before any functions, according to the kit used: • SAM D21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_TCC_CAPTURE_CHANNEL 1 #define CONF_TCC_EVENT_GENERATOR EVSYS_ID_GEN_TCC0_MCX_0 #define CONF_TCC_EVENT_USER EVSYS_ID_USER_TCC0_MC_1 #define CONF_COMPARE_TRIGGER TCC0_DMAC_ID_OVF #define CONF_CAPTURE_TRIGGER TCC0_DMAC_ID_MC_1 • SAM R21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_TCC_CAPTURE_CHANNEL 1 #define CONF_TCC_EVENT_GENERATOR EVSYS_ID_GEN_TCC0_MCX_3 Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 86 #define CONF_TCC_EVENT_USER EVSYS_ID_USER_TCC0_MC_1 #define CONF_COMPARE_TRIGGER TCC0_DMAC_ID_OVF #define CONF_CAPTURE_TRIGGER TCC0_DMAC_ID_MC_1 • SAM L21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_TCC_CAPTURE_CHANNEL 1 #define CONF_TCC_EVENT_GENERATOR EVSYS_ID_GEN_TCC0_MCX_0 #define CONF_TCC_EVENT_USER EVSYS_ID_USER_TCC0_MC_1 #define CONF_COMPARE_TRIGGER TCC0_DMAC_ID_OVF • SAM L22 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_TCC_CAPTURE_CHANNEL 1 #define CONF_TCC_EVENT_GENERATOR EVSYS_ID_GEN_TCC0_MCX_0 #define CONF_TCC_EVENT_USER EVSYS_ID_USER_TCC0_MC_1 #define CONF_COMPARE_TRIGGER TCC0_DMAC_ID_OVF • SAM DA1 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 87 #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_TCC_CAPTURE_CHANNEL 1 #define CONF_TCC_EVENT_GENERATOR EVSYS_ID_GEN_TCC0_MCX_0 #define CONF_TCC_EVENT_USER EVSYS_ID_USER_TCC0_MC_1 #define CONF_COMPARE_TRIGGER TCC0_DMAC_ID_OVF #define CONF_CAPTURE_TRIGGER TCC0_DMAC_ID_MC_1 • SAM C21 Xplained Pro #define CONF_PWM_MODULE LED_0_PWM4CTRL_MODULE #define CONF_PWM_CHANNEL LED_0_PWM4CTRL_CHANNEL #define CONF_PWM_OUTPUT LED_0_PWM4CTRL_OUTPUT #define CONF_PWM_OUT_PIN LED_0_PWM4CTRL_PIN #define CONF_PWM_OUT_MUX LED_0_PWM4CTRL_MUX #define CONF_TCC_CAPTURE_CHANNEL 1 #define CONF_TCC_EVENT_GENERATOR EVSYS_ID_GEN_TCC0_MCX_0 #define CONF_TCC_EVENT_USER EVSYS_ID_USER_TCC0_MC_1 #define CONF_COMPARE_TRIGGER TCC0_DMAC_ID_OVF Add to the main application source file, outside of any functions: struct tcc_module tcc_instance; uint16_t capture_values[3] = {0, 0, 0}; struct dma_resource capture_dma_resource; COMPILER_ALIGNED(16) DmacDescriptor capture_dma_descriptor; struct events_resource capture_event_resource; uint16_t compare_values[3] = { (0x1000 / 4), (0x1000 * 2 / 4), (0x1000 * 3 / 4) }; struct dma_resource compare_dma_resource; COMPILER_ALIGNED(16) DmacDescriptor compare_dma_descriptor; Copy-paste the following setup code to your user application: static void config_event_for_capture(void) { struct events_config config; events_get_config_defaults(&config); config.generator config.edge_detect = CONF_TCC_EVENT_GENERATOR; = EVENTS_EDGE_DETECT_RISING; Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 88 config.path config.clock_source = EVENTS_PATH_SYNCHRONOUS; = GCLK_GENERATOR_0; events_allocate(&capture_event_resource, &config); } events_attach_user(&capture_event_resource, CONF_TCC_EVENT_USER); static void config_dma_for_capture(void) { struct dma_resource_config config; dma_get_config_defaults(&config); config.trigger_action = DMA_TRIGGER_ACTON_BEAT; config.peripheral_trigger = CONF_CAPTURE_TRIGGER; dma_allocate(&capture_dma_resource, &config); struct dma_descriptor_config descriptor_config; dma_descriptor_get_config_defaults(&descriptor_config); descriptor_config.block_transfer_count = 3; descriptor_config.beat_size = DMA_BEAT_SIZE_HWORD; descriptor_config.step_selection = DMA_STEPSEL_SRC; descriptor_config.src_increment_enable = false; descriptor_config.source_address = (uint32_t)&CONF_PWM_MODULE->CC[CONF_TCC_CAPTURE_CHANNEL]; descriptor_config.destination_address = (uint32_t)capture_values + sizeof(capture_values); dma_descriptor_create(&capture_dma_descriptor, &descriptor_config); } dma_add_descriptor(&capture_dma_resource, &capture_dma_descriptor); dma_add_descriptor(&capture_dma_resource, &capture_dma_descriptor); dma_start_transfer_job(&capture_dma_resource); static void config_dma_for_wave(void) { struct dma_resource_config config; dma_get_config_defaults(&config); config.trigger_action = DMA_TRIGGER_ACTON_BEAT; config.peripheral_trigger = CONF_COMPARE_TRIGGER; dma_allocate(&compare_dma_resource, &config); struct dma_descriptor_config descriptor_config; dma_descriptor_get_config_defaults(&descriptor_config); descriptor_config.block_transfer_count = 3; descriptor_config.beat_size = DMA_BEAT_SIZE_HWORD; descriptor_config.dst_increment_enable = false; descriptor_config.source_address = (uint32_t)compare_values + sizeof(compare_values); descriptor_config.destination_address = (uint32_t)&CONF_PWM_MODULE->CC[CONF_PWM_CHANNEL]; dma_descriptor_create(&compare_dma_descriptor, &descriptor_config); Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 89 } dma_add_descriptor(&compare_dma_resource, &compare_dma_descriptor); dma_add_descriptor(&compare_dma_resource, &compare_dma_descriptor); dma_start_transfer_job(&compare_dma_resource); static void configure_tcc(void) { struct tcc_config config_tcc; tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1024; config_tcc.counter.period = 0x1000; config_tcc.compare.channel_function[CONF_TCC_CAPTURE_CHANNEL] = TCC_CHANNEL_FUNCTION_CAPTURE; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.wave_polarity[CONF_PWM_CHANNEL] = TCC_WAVE_POLARITY_0; config_tcc.compare.match[CONF_PWM_CHANNEL] = compare_values[2]; config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); struct tcc_events events_tcc = { .input_config[0].modify_action = false, .input_config[1].modify_action = false, .output_config.modify_generation_selection = false, .generate_event_on_channel[CONF_PWM_CHANNEL] = true, .on_event_perform_channel_action[CONF_TCC_CAPTURE_CHANNEL] = true }; tcc_enable_events(&tcc_instance, &events_tcc); config_event_for_capture(); config_dma_for_capture(); config_dma_for_wave(); } tcc_enable(&tcc_instance); Add to user application initialization (typically the start of main()): configure_tcc(); 9.7.1.3. Workflow Configure the TCC 1. Create a module software instance structure for the TCC module to store the TCC driver state while it is in use. struct tcc_module tcc_instance; Note: This should never go out of scope as long as the module is in use. In most cases, this should be global. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 90 2. Create a TCC module configuration struct, which can be filled out to adjust the configuration of a physical TCC peripheral. struct tcc_config config_tcc; 3. Initialize the TCC configuration struct with the module's default values. tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. 4. Alter the TCC settings to configure the counter width, wave generation mode, and the compare channel 0 value. config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1024; config_tcc.counter.period = 0x1000; config_tcc.compare.channel_function[CONF_TCC_CAPTURE_CHANNEL] = TCC_CHANNEL_FUNCTION_CAPTURE; config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; config_tcc.compare.wave_polarity[CONF_PWM_CHANNEL] = TCC_WAVE_POLARITY_0; config_tcc.compare.match[CONF_PWM_CHANNEL] = compare_values[2]; 5. Alter the TCC settings to configure the PWM output on a physical device pin. config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true; config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT] = CONF_PWM_OUT_PIN; config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT] = CONF_PWM_OUT_MUX; 6. Configure the TCC module with the desired settings. tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); 7. Configure and enable the desired events for the TCC module. struct tcc_events events_tcc = { .input_config[0].modify_action = false, .input_config[1].modify_action = false, .output_config.modify_generation_selection = false, .generate_event_on_channel[CONF_PWM_CHANNEL] = true, .on_event_perform_channel_action[CONF_TCC_CAPTURE_CHANNEL] = true }; tcc_enable_events(&tcc_instance, &events_tcc); Configure the Event System Configure the EVSYS module to wire channel 0 event to channel 1. 1. Create an event resource instance. struct events_resource capture_event_resource; 2. Note: This should never go out of scope as long as the resource is in use. In most cases, this should be global. Create an event resource configuration struct. struct events_config config; Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 91 3. Initialize the event resource configuration struct with default values. events_get_config_defaults(&config); 4. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Adjust the event resource configuration to desired values. config.generator config.edge_detect config.path config.clock_source 5. = = = = CONF_TCC_EVENT_GENERATOR; EVENTS_EDGE_DETECT_RISING; EVENTS_PATH_SYNCHRONOUS; GCLK_GENERATOR_0; Allocate and configure the resource using the configuration structure. events_allocate(&capture_event_resource, &config); 6. Attach a user to the resource. events_attach_user(&capture_event_resource, CONF_TCC_EVENT_USER); Configure the DMA for Capture TCC Channel 1 Configure the DMAC module to obtain captured value from TCC channel 1. 1. Create a DMA resource instance. struct dma_resource capture_dma_resource; 2. Note: This should never go out of scope as long as the resource is in use. In most cases, this should be global. Create a DMA resource configuration struct. struct dma_resource_config config; 3. Initialize the DMA resource configuration struct with default values. dma_get_config_defaults(&config); 4. Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. Adjust the DMA resource configurations. config.trigger_action = DMA_TRIGGER_ACTON_BEAT; config.peripheral_trigger = CONF_CAPTURE_TRIGGER; 5. Allocate a DMA resource with the configurations. dma_allocate(&capture_dma_resource, &config); 6. Prepare DMA transfer descriptor. 1. Create a DMA transfer descriptor. COMPILER_ALIGNED(16) DmacDescriptor capture_dma_descriptor; 2. Note: When multiple descriptors are linked, the linked item should never go out of scope before it is loaded (to DMA Write-Back memory section). In most cases, if more than one descriptors are used, they should be global except the very first one. Create a DMA transfer descriptor struct. Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 92 3. Create a DMA transfer descriptor configuration structure, which can be filled out to adjust the configuration of a single DMA transfer. struct dma_descriptor_config descriptor_config; 4. Initialize the DMA transfer descriptor configuration struct with default values. dma_descriptor_get_config_defaults(&descriptor_config); Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings. 5. Adjust the DMA transfer descriptor configurations. descriptor_config.block_transfer_count = 3; descriptor_config.beat_size = DMA_BEAT_SIZE_HWORD; descriptor_config.step_selection = DMA_STEPSEL_SRC; descriptor_config.src_increment_enable = false; descriptor_config.source_address = (uint32_t)&CONF_PWM_MODULE->CC[CONF_TCC_CAPTURE_CHANNEL]; descriptor_config.destination_address = (uint32_t)capture_values + sizeof(capture_values); 6. Create the DMA transfer descriptor with the given configuration. dma_descriptor_create(&capture_dma_descriptor, &descriptor_config); 7. Start DMA transfer job with prepared descriptor. 1. Add the DMA transfer descriptor to the allocated DMA resource. dma_add_descriptor(&capture_dma_resource, &capture_dma_descriptor); dma_add_descriptor(&capture_dma_resource, &capture_dma_descriptor); 2. Note: When adding multiple descriptors, the last one added is linked at the end of the descriptor queue. If ringed list is needed, just add the first descriptor again to build the circle. Start the DMA transfer job with the allocated DMA resource and transfer descriptor. dma_start_transfer_job(&capture_dma_resource); Configure the DMA for Compare TCC Channel 0 Configure the DMAC module to update TCC channel 0 compare value. The flow is similar to last DMA configure step for capture. 1. Allocate and configure the DMA resource. struct dma_resource compare_dma_resource; struct dma_resource_config config; dma_get_config_defaults(&config); config.trigger_action = DMA_TRIGGER_ACTON_BEAT; config.peripheral_trigger = CONF_COMPARE_TRIGGER; dma_allocate(&compare_dma_resource, &config); 2. Prepare DMA transfer descriptor. COMPILER_ALIGNED(16) DmacDescriptor compare_dma_descriptor; struct dma_descriptor_config descriptor_config; Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 93 dma_descriptor_get_config_defaults(&descriptor_config); descriptor_config.block_transfer_count = 3; descriptor_config.beat_size = DMA_BEAT_SIZE_HWORD; descriptor_config.dst_increment_enable = false; descriptor_config.source_address = (uint32_t)compare_values + sizeof(compare_values); descriptor_config.destination_address = (uint32_t)&CONF_PWM_MODULE->CC[CONF_PWM_CHANNEL]; dma_descriptor_create(&compare_dma_descriptor, &descriptor_config); 3. Start DMA transfer job with prepared descriptor. dma_add_descriptor(&compare_dma_resource, &compare_dma_descriptor); dma_add_descriptor(&compare_dma_resource, &compare_dma_descriptor); dma_start_transfer_job(&compare_dma_resource); 4. Enable the TCC module to start the timer and begin PWM signal generation. tcc_enable(&tcc_instance); 9.7.2. Use Case 9.7.2.1. Code Copy-paste the following code to your user application: while (true) { /* Infinite loop */ } 9.7.2.2. Workflow 1. Enter an infinite loop while the PWM wave is generated via the TCC module. while (true) { /* Infinite loop */ } Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 94 10. Document Revision History Doc. Rev. Date Comments 42256C 12/2015 Added support for SAM L21/L22, SAM DA1, and SAM C20/C21 42256B 12/2014 Added fault handling functionality. Added double buffering functionality with use case. Added timer use case. Added SAM R21/D10/D11 support. 42256A 01/2014 Initial release Atmel AT07058: SAM D10/D11/D21/DA1/R/L/C Timer Counter for Control Applications (TCC) Driver [APPLICATION NOTE] Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 95 Atmel Corporation © 1600 Technology Drive, San Jose, CA 95110 USA T: (+1)(408) 441.0311 F: (+1)(408) 436.4200 | www.atmel.com 2015 Atmel Corporation. / Rev.: Atmel-42256C-SAM-Timer-Counter-for-Control-Applications-TCC-Driver_AT07058_Application Note-12/2015 ® ® Atmel , Atmel logo and combinations thereof, Enabling Unlimited Possibilities , and others are registered trademarks or trademarks of Atmel Corporation in U.S. and ® ® other countries. ARM , ARM Connected , and others are registered trademarks of ARM Ltd. Other terms and product names may be trademarks of others. DISCLAIMER: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. SAFETY-CRITICAL, MILITARY, AND AUTOMOTIVE APPLICATIONS DISCLAIMER: Atmel products are not designed for and will not be used in connection with any applications where the failure of such products would reasonably be expected to result in significant personal injury or death (“Safety-Critical Applications”) without an Atmel officer's specific written consent. Safety-Critical Applications include, without limitation, life support devices and systems, equipment or systems for the operation of nuclear facilities and weapons systems. Atmel products are not designed nor intended for use in military or aerospace applications or environments unless specifically designated by Atmel as military-grade. Atmel products are not designed nor intended for use in automotive applications unless specifically designated by Atmel as automotive-grade.