Microchip® Advanced Software Framework

tcc.h File Reference

SAM TCC - Timer Counter for Control Applications Driver.

Copyright (c) 2013-2020 Microchip Technology Inc. and its subsidiaries.

#include <compiler.h>
#include <clock.h>
#include <gclk.h>
#include <pinmux.h>
#include <system_interrupt.h>

Data Structures

struct  tcc_capture_config
 Configuration struct for the TCC module capture. More...
 
struct  tcc_config
 TCC configuration structure. More...
 
struct  tcc_counter_config
 Configuration struct for the TCC module base counter. More...
 
struct  tcc_events
 TCC event enable/disable structure. More...
 
struct  tcc_input_event_config
 TCC input event enable/disable/configure structure. More...
 
struct  tcc_match_wave_config
 Configuration struct for the TCC module match/wave generation. More...
 
struct  tcc_module
 TCC software device instance structure. More...
 
struct  tcc_non_recoverable_fault_config
 Configuration struct for TCC module non-recoverable fault. More...
 
struct  tcc_output_event_config
 TCC output event enable/disable/configure structure. More...
 
struct  tcc_pins_config
 Configuration struct for the TCC module output pins. More...
 
struct  tcc_recoverable_fault_config
 Configuration struct for TCC module recoverable fault. More...
 
struct  tcc_wave_extension_config
 Configuration struct for the TCC module waveform extension. More...
 

Macros

#define _TCC_CHANNEL_ENUM_LIST(type)   MREPEAT(TCC_NUM_CHANNELS, _TCC_ENUM, type##_CHANNEL)
 Generates table enum list entries for all channels of a given type and channel number on TCC module. More...
 
#define _TCC_ENUM(n, type)   TCC_##type##_##n,
 Generates a table enum list entry for a given type and index (e.g. More...
 
#define _TCC_WO_ENUM_LIST(type)   MREPEAT(TCC_NUM_WAVE_OUTPUTS, _TCC_ENUM, type)
 Generates table enum list entries for all output of a given type and waveform output number on TCC module. More...
 
#define TCC_NUM_CHANNELS   4
 Maximum number of channels supported by the driver (Channel index from 0 to TCC_NUM_CHANNELS - 1). More...
 
#define TCC_NUM_FAULTS   2
 Maximum number of (recoverable) faults supported by the driver. More...
 
#define TCC_NUM_WAVE_OUTPUTS   8
 Maximum number of wave outputs lines supported by the driver (Output line index from 0 to TCC_NUM_WAVE_OUTPUTS - 1). More...
 
Driver Feature Definition

Define port features set according to different device family.

#define FEATURE_TCC_GENERATE_DMA_TRIGGER
 Generate DMA triggers. More...
 
Module Status Flags

TCC status flags, returned by tcc_get_status() and cleared by tcc_clear_status().

#define TCC_STATUS_CHANNEL_MATCH_CAPTURE(ch)   (1UL << (ch))
 Timer channel ch (0 ~ 3) has matched against its compare value, or has captured a new value. More...
 
#define TCC_STATUS_CHANNEL_OUTPUT(ch)   (1UL << ((ch)+8))
 Timer channel ch (0 ~ 3) match/compare output state. More...
 
#define TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(x)   (1UL << ((x)+16))
 A Non-Recoverable Fault x (0 ~ 1) has occurred. More...
 
#define TCC_STATUS_RECOVERABLE_FAULT_OCCUR(n)   (1UL << ((n)+18))
 A Recoverable Fault n (0 ~ 1 representing A ~ B) has occured. More...
 
#define TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(x)   (1UL << ((x)+20))
 The Non-Recoverable Fault x (0 ~ 1) input is present. More...
 
#define TCC_STATUS_RECOVERABLE_FAULT_PRESENT(n)   (1UL << ((n)+22))
 A Recoverable Fault n (0 ~ 1 representing A ~ B) is present. More...
 
#define TCC_STATUS_SYNC_READY   (1UL << 23)
 Timer registers synchronization has completed, and the synchronized count value may be read. More...
 
#define TCC_STATUS_CAPTURE_OVERFLOW   (1UL << 24)
 A new value was captured before the previous value was read, resulting in lost data. More...
 
#define TCC_STATUS_COUNTER_EVENT   (1UL << 25)
 A counter event occurred. More...
 
#define TCC_STATUS_COUNTER_RETRIGGERED   (1UL << 26)
 A counter retrigger occurred. More...
 
#define TCC_STATUS_COUNT_OVERFLOW   (1UL << 27)
 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. More...
 
#define TCC_STATUS_RAMP_CYCLE_INDEX   (1UL << 28)
 Ramp period cycle index. More...
 
#define TCC_STATUS_STOPPED   (1UL << 29)
 The counter has been stopped (due to disable, stop command, or one-shot). More...
 

Typedefs

typedef void(* tcc_callback_t )(struct tcc_module *const module)
 Type definition for the TCC callback function. More...
 

Enumerations

enum  tcc_callback {
  TCC_CALLBACK_OVERFLOW,
  TCC_CALLBACK_RETRIGGER,
  TCC_CALLBACK_COUNTER_EVENT,
  TCC_CALLBACK_ERROR,
  TCC_CALLBACK_FAULTA,
  TCC_CALLBACK_FAULTB,
  TCC_CALLBACK_FAULT0,
  TCC_CALLBACK_FAULT1,
  TCC_CALLBACK_CHANNEL_n = n
}
 Enum for the possible callback types for the TCC module. More...
 
enum  tcc_channel_function {
  TCC_CHANNEL_FUNCTION_COMPARE,
  TCC_CHANNEL_FUNCTION_CAPTURE
}
 TCC channel operation modes. More...
 
enum  tcc_clock_prescaler {
  TCC_CLOCK_PRESCALER_DIV1,
  TCC_CLOCK_PRESCALER_DIV2,
  TCC_CLOCK_PRESCALER_DIV4,
  TCC_CLOCK_PRESCALER_DIV8,
  TCC_CLOCK_PRESCALER_DIV16,
  TCC_CLOCK_PRESCALER_DIV64,
  TCC_CLOCK_PRESCALER_DIV256,
  TCC_CLOCK_PRESCALER_DIV1024
}
 TCC clock prescaler values. More...
 
enum  tcc_count_direction {
  TCC_COUNT_DIRECTION_UP,
  TCC_COUNT_DIRECTION_DOWN
}
 TCC module count direction. More...
 
enum  tcc_count_overflow_dma_trigger_mode {
  TCC_COUNT_OVERFLOW_DMA_TRIGGER_MODE_CONTINUE,
  TCC_COUNT_OVERFLOW_DMA_TRIGGER_MODE_ONE_SHOT
}
 TCC module counter overflow DMA request mode. More...
 
enum  tcc_event0_action {
  TCC_EVENT0_ACTION_OFF = TCC_EVENT_ACTION_OFF,
  TCC_EVENT0_ACTION_RETRIGGER = TCC_EVENT_ACTION_RETRIGGER,
  TCC_EVENT0_ACTION_COUNT_EVENT = TCC_EVENT_ACTION_COUNT_EVENT,
  TCC_EVENT0_ACTION_START = TCC_EVENT_ACTION_START,
  TCC_EVENT0_ACTION_INCREMENT = TCC_EVENT_ACTION_INCREMENT,
  TCC_EVENT0_ACTION_COUNT_DURING_ACTIVE = TCC_EVENT_ACTION_COUNT_DURING_ACTIVE,
  TCC_EVENT0_ACTION_NON_RECOVERABLE_FAULT = TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT
}
 Action to be performed when the TCC module is triggered by event0. More...
 
enum  tcc_event1_action {
  TCC_EVENT1_ACTION_OFF = TCC_EVENT_ACTION_OFF,
  TCC_EVENT1_ACTION_RETRIGGER = TCC_EVENT_ACTION_RETRIGGER,
  TCC_EVENT1_ACTION_DIR_CONTROL = TCC_EVENT_ACTION_DIR_CONTROL,
  TCC_EVENT1_ACTION_STOP = TCC_EVENT_ACTION_STOP,
  TCC_EVENT1_ACTION_DECREMENT = TCC_EVENT_ACTION_DECREMENT,
  TCC_EVENT1_ACTION_PERIOD_PULSE_WIDTH_CAPTURE = TCC_EVENT_ACTION_PERIOD_PULSE_WIDTH_CAPTURE,
  TCC_EVENT1_ACTION_PULSE_WIDTH_PERIOD_CAPTURE = TCC_EVENT_ACTION_PULSE_WIDTH_PERIOD_CAPTURE,
  TCC_EVENT1_ACTION_NON_RECOVERABLE_FAULT = TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT
}
 Action to perform when the TCC module is triggered by event1. More...
 
enum  tcc_event_action {
  TCC_EVENT_ACTION_OFF,
  TCC_EVENT_ACTION_STOP,
  TCC_EVENT_ACTION_RETRIGGER,
  TCC_EVENT_ACTION_START,
  TCC_EVENT_ACTION_COUNT_EVENT,
  TCC_EVENT_ACTION_DIR_CONTROL,
  TCC_EVENT_ACTION_INCREMENT,
  TCC_EVENT_ACTION_DECREMENT,
  TCC_EVENT_ACTION_COUNT_DURING_ACTIVE,
  TCC_EVENT_ACTION_PERIOD_PULSE_WIDTH_CAPTURE,
  TCC_EVENT_ACTION_PULSE_WIDTH_PERIOD_CAPTURE,
  TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT
}
 Action to perform when the TCC module is triggered by events. More...
 
enum  tcc_event_generation_selection {
  TCC_EVENT_GENERATION_SELECTION_START,
  TCC_EVENT_GENERATION_SELECTION_END,
  TCC_EVENT_GENERATION_SELECTION_BETWEEN,
  TCC_EVENT_GENERATION_SELECTION_BOUNDARY
}
 On which part of the counter cycle the counter event output is generated. More...
 
enum  tcc_fault_blanking {
  TCC_FAULT_BLANKING_DISABLE,
  TCC_FAULT_BLANKING_RISING_EDGE,
  TCC_FAULT_BLANKING_FALLING_EDGE,
  TCC_FAULT_BLANKING_BOTH_EDGE
}
 TCC (recoverable) fault Input Blanking Start Point. More...
 
enum  tcc_fault_capture_action {
  TCC_FAULT_CAPTURE_DISABLE,
  TCC_FAULT_CAPTURE_EACH,
  TCC_FAULT_CAPTURE_MINIMUM,
  TCC_FAULT_CAPTURE_MAXIMUM,
  TCC_FAULT_CAPTURE_SMALLER,
  TCC_FAULT_CAPTURE_BIGGER,
  TCC_FAULT_CAPTURE_CHANGE
}
 TCC (recoverable) fault Capture action. More...
 
enum  tcc_fault_capture_channel {
  TCC_FAULT_CAPTURE_CHANNEL_0,
  TCC_FAULT_CAPTURE_CHANNEL_1,
  TCC_FAULT_CAPTURE_CHANNEL_2,
  TCC_FAULT_CAPTURE_CHANNEL_3
}
 Capture Channel triggered by TCC (recoverable) fault. More...
 
enum  tcc_fault_halt_action {
  TCC_FAULT_HALT_ACTION_DISABLE,
  TCC_FAULT_HALT_ACTION_HW_HALT,
  TCC_FAULT_HALT_ACTION_SW_HALT,
  TCC_FAULT_HALT_ACTION_NON_RECOVERABLE
}
 TCC (recoverable) fault Halt action. More...
 
enum  tcc_fault_keep {
  TCC_FAULT_KEEP_DISABLE,
  TCC_FAULT_KEEP_TILL_END
}
 TCC (recoverable) fault Output Keep Action. More...
 
enum  tcc_fault_qualification {
  TCC_FAULT_QUALIFICATION_DISABLE,
  TCC_FAULT_QUALIFICATION_BY_OUTPUT
}
 TCC (recoverable) fault Input Qualification Action. More...
 
enum  tcc_fault_restart {
  TCC_FAULT_RESTART_DISABLE,
  TCC_FAULT_RESTART_ENABLE
}
 TCC (recoverable) fault Restart Action. More...
 
enum  tcc_fault_source {
  TCC_FAULT_SOURCE_DISABLE,
  TCC_FAULT_SOURCE_ENABLE,
  TCC_FAULT_SOURCE_INVERT,
  TCC_FAULT_SOURCE_ALTFAULT
}
 TCC (recoverable) fault Input Source. More...
 
enum  tcc_fault_state_output {
  TCC_FAULT_STATE_OUTPUT_OFF,
  TCC_FAULT_STATE_OUTPUT_0,
  TCC_FAULT_STATE_OUTPUT_1
}
 TCC Non-recoverable State Outupt. More...
 
enum  tcc_match_capture_channel { TCC_MATCH_CAPTURE_CHANNEL_n = n }
 Index of the match capture channels. More...
 
enum  tcc_output_invertion {
  TCC_OUTPUT_INVERTION_DISABLE,
  TCC_OUTPUT_INVERTION_ENABLE
}
 TCC output inversion. More...
 
enum  tcc_output_pattern {
  TCC_OUTPUT_PATTERN_DISABLE,
  TCC_OUTPUT_PATTERN_0,
  TCC_OUTPUT_PATTERN_1
}
 TCC pattern generator for outputs. More...
 
enum  tcc_ramp {
  TCC_RAMP_RAMP1 = 0,
  TCC_RAMP_RAMP2A,
  TCC_RAMP_RAMP2
}
 Ramp Operations which are supported in single-slope PWM generation. More...
 
enum  tcc_ramp_index {
  TCC_RAMP_INDEX_DEFAULT,
  TCC_RAMP_INDEX_FORCE_B,
  TCC_RAMP_INDEX_FORCE_A,
  TCC_RAMP_INDEX_FORCE_KEEP
}
 Ramp Index for TCC wave generation. More...
 
enum  tcc_reload_action {
  TCC_RELOAD_ACTION_GCLK,
  TCC_RELOAD_ACTION_PRESC,
  TCC_RELOAD_ACTION_RESYNC
}
 TCC Counter reload action enum. More...
 
enum  tcc_wave_generation {
  TCC_WAVE_GENERATION_NORMAL_FREQ = 0,
  TCC_WAVE_GENERATION_MATCH_FREQ = 1,
  TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM = 2,
  TCC_WAVE_GENERATION_DOUBLE_SLOPE_CRITICAL = 4,
  TCC_WAVE_GENERATION_DOUBLE_SLOPE_BOTTOM = 5,
  TCC_WAVE_GENERATION_DOUBLE_SLOPE_BOTH = 6,
  TCC_WAVE_GENERATION_DOUBLE_SLOPE_TOP = 7
}
 TCC wave generation mode enum. More...
 
enum  tcc_wave_output { TCC_WAVE_OUTPUT_n = n }
 Index of the wave outputs. More...
 
enum  tcc_wave_polarity {
  TCC_WAVE_POLARITY_0,
  TCC_WAVE_POLARITY_1
}
 Polarity of TCC wave generation on channels. More...
 

Functions

Driver Initialization and Configuration
static bool tcc_is_syncing (const struct tcc_module *const module_inst)
 Determines if the hardware module is currently synchronizing to the bus. More...
 
void tcc_get_config_defaults (struct tcc_config *const config, Tcc *const hw)
 Initializes config with predefined default values. More...
 
enum status_code tcc_init (struct tcc_module *const module_inst, Tcc *const hw, const struct tcc_config *const config)
 Initializes a hardware TCC module instance. More...
 
Event Management
enum status_code tcc_enable_events (struct tcc_module *const module_inst, struct tcc_events *const events)
 Enables the TCC module event input or output. More...
 
void tcc_disable_events (struct tcc_module *const module_inst, struct tcc_events *const events)
 Disables the event input or output of a TCC instance. More...
 
Enable/Disable/Reset
static void tcc_enable (const struct tcc_module *const module_inst)
 Enable the TCC module. More...
 
static void tcc_disable (const struct tcc_module *const module_inst)
 Disables the TCC module. More...
 
static void tcc_reset (const struct tcc_module *const module_inst)
 Resets the TCC module. More...
 
Set/Toggle Count Direction
static void tcc_set_count_direction (const struct tcc_module *const module_inst, enum tcc_count_direction dir)
 Sets the TCC module count direction. More...
 
static void tcc_toggle_count_direction (const struct tcc_module *const module_inst)
 Toggles the TCC module count direction. More...
 
Get/Set Count Value
uint32_t tcc_get_count_value (const struct tcc_module *const module_inst)
 Get count value of the given TCC module. More...
 
enum status_code tcc_set_count_value (const struct tcc_module *const module_inst, const uint32_t count)
 Sets count value for the given TCC module. More...
 
Stop/Restart Counter
static void tcc_stop_counter (const struct tcc_module *const module_inst)
 Stops the counter. More...
 
static void tcc_restart_counter (const struct tcc_module *const module_inst)
 Starts the counter from beginning. More...
 
Generate TCC DMA Triggers Command
static void tcc_dma_trigger_command (const struct tcc_module *const module_inst)
 TCC DMA Trigger. More...
 
Get/Set Compare/Capture Register
uint32_t tcc_get_capture_value (const struct tcc_module *const module_inst, const enum tcc_match_capture_channel channel_index)
 Gets the TCC module capture value. More...
 
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)
 Sets a TCC module compare value. More...
 
Set Top Value
enum status_code tcc_set_top_value (const struct tcc_module *const module_inst, const uint32_t top_value)
 Set the timer TOP/PERIOD value. More...
 
Set 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)
 Sets the TCC module waveform output pattern. More...
 
Set Ramp Index
static void tcc_set_ramp_index (const struct tcc_module *const module_inst, const enum tcc_ramp_index ramp_index)
 Sets the TCC module ramp index on next cycle. More...
 
Status Management
static bool tcc_is_running (struct tcc_module *const module_inst)
 Checks if the timer/counter is running. More...
 
uint32_t tcc_get_status (struct tcc_module *const module_inst)
 Retrieves the current module status. More...
 
void tcc_clear_status (struct tcc_module *const module_inst, const uint32_t status_flags)
 Clears a module status flag. More...
 
Double Buffering Management
static void tcc_enable_double_buffering (struct tcc_module *const module_inst)
 Enable TCC double buffering write. More...
 
static void tcc_disable_double_buffering (struct tcc_module *const module_inst)
 Disable TCC double buffering Write. More...
 
static void tcc_lock_double_buffer_update (struct tcc_module *const module_inst)
 Lock the TCC double buffered registers updates. More...
 
static void tcc_unlock_double_buffer_update (struct tcc_module *const module_inst)
 Unlock the TCC double buffered registers updates. More...
 
static void tcc_force_double_buffer_update (struct tcc_module *const module_inst)
 Force the TCC double buffered registers to update once. More...
 
static void tcc_enable_circular_buffer_top (struct tcc_module *const module_inst)
 Enable Circular option for double buffered Top/Period Values. More...
 
static void tcc_disable_circular_buffer_top (struct tcc_module *const module_inst)
 Disable Circular option for double buffered Top/Period Values. More...
 
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)
 Set the timer TOP/PERIOD value and buffer value. More...
 
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 double buffered compare values. More...
 
enum status_code tcc_disable_circular_buffer_compare (struct tcc_module *const module_inst, enum tcc_match_capture_channel channel_index)
 Disable circular option for double buffered compare values. More...
 
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)
 Sets a TCC module compare value and buffer value. More...