Microchip® Advanced Software Framework

tc.h File Reference

SAM TC - Timer Counter 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  tc_16bit_config
 Configuration struct for TC module in 16-bit size counter mode. More...
 
struct  tc_32bit_config
 Configuration struct for TC module in 32-bit size counter mode. More...
 
struct  tc_8bit_config
 Configuration struct for TC module in 8-bit size counter mode. More...
 
struct  tc_config
 TC configuration structure. More...
 
struct  tc_events
 TC event enable/disable structure. More...
 
struct  tc_module
 TC software device instance structure. More...
 
struct  tc_pwm_channel
 Configuration struct for TC module in 32-bit size counter mode. More...
 

Macros

#define FEATURE_TC_DOUBLE_BUFFERED
 Define port features set according to different device family. More...
 
#define FEATURE_TC_SYNCBUSY_SCHEME_VERSION_2
 SYNCBUSY scheme version 2. More...
 
#define FEATURE_TC_STAMP_PW_CAPTURE
 TC time stamp capture and pulse width capture. More...
 
#define FEATURE_TC_READ_SYNC
 Read synchronization of COUNT. More...
 
#define FEATURE_TC_IO_CAPTURE
 I/O pin edge capture. More...
 
#define FEATURE_TC_GENERATE_DMA_TRIGGER
 Generate Direct Memory Access (DMA) triggers. More...
 
Module Status Flags

TC status flags, returned by tc_get_status() and cleared by tc_clear_status().

#define TC_STATUS_CHANNEL_0_MATCH   (1UL << 0)
 Timer channel 0 has matched against its compare value, or has captured a new value. More...
 
#define TC_STATUS_CHANNEL_1_MATCH   (1UL << 1)
 Timer channel 1 has matched against its compare value, or has captured a new value. More...
 
#define TC_STATUS_SYNC_READY   (1UL << 2)
 Timer register synchronization has completed, and the synchronized count value may be read. More...
 
#define TC_STATUS_CAPTURE_OVERFLOW   (1UL << 3)
 A new value was captured before the previous value was read, resulting in lost data. More...
 
#define TC_STATUS_COUNT_OVERFLOW   (1UL << 4)
 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 TC_STATUS_CHN0_BUFFER_VALID   (1UL << 5)
 Channel 0 compare or capture buffer valid. More...
 
#define TC_STATUS_CHN1_BUFFER_VALID   (1UL << 6)
 Channel 1 compare or capture buffer valid. More...
 
#define TC_STATUS_PERIOD_BUFFER_VALID   (1UL << 7)
 Period buffer valid. More...
 
TC Wave Generation Mode
#define TC_WAVE_GENERATION_NORMAL_FREQ_MODE   TC_CTRLA_WAVEGEN_NFRQ
 TC wave generation mode: normal frequency. More...
 
#define TC_WAVE_GENERATION_MATCH_FREQ_MODE   TC_CTRLA_WAVEGEN_MFRQ
 TC wave generation mode: match frequency. More...
 
#define TC_WAVE_GENERATION_NORMAL_PWM_MODE   TC_CTRLA_WAVEGEN_NPWM
 TC wave generation mode: normal PWM. More...
 
#define TC_WAVE_GENERATION_MATCH_PWM_MODE   TC_CTRLA_WAVEGEN_MPWM
 TC wave generation mode: match PWM. More...
 

Enumerations

enum  tc_callback {
  TC_CALLBACK_OVERFLOW,
  TC_CALLBACK_ERROR,
  TC_CALLBACK_CC_CHANNEL0,
  TC_CALLBACK_CC_CHANNEL1
}
 Enum for the possible callback types for the TC module. More...
 
enum  tc_clock_prescaler {
  TC_CLOCK_PRESCALER_DIV1 = TC_CTRLA_PRESCALER(0),
  TC_CLOCK_PRESCALER_DIV2 = TC_CTRLA_PRESCALER(1),
  TC_CLOCK_PRESCALER_DIV4 = TC_CTRLA_PRESCALER(2),
  TC_CLOCK_PRESCALER_DIV8 = TC_CTRLA_PRESCALER(3),
  TC_CLOCK_PRESCALER_DIV16 = TC_CTRLA_PRESCALER(4),
  TC_CLOCK_PRESCALER_DIV64 = TC_CTRLA_PRESCALER(5),
  TC_CLOCK_PRESCALER_DIV256 = TC_CTRLA_PRESCALER(6),
  TC_CLOCK_PRESCALER_DIV1024 = TC_CTRLA_PRESCALER(7)
}
 TC clock prescaler values. More...
 
enum  tc_compare_capture_channel {
  TC_COMPARE_CAPTURE_CHANNEL_0,
  TC_COMPARE_CAPTURE_CHANNEL_1
}
 Index of the compare capture channels. More...
 
enum  tc_count_direction {
  TC_COUNT_DIRECTION_UP,
  TC_COUNT_DIRECTION_DOWN
}
 TC module count direction. More...
 
enum  tc_counter_size {
  TC_COUNTER_SIZE_8BIT = TC_CTRLA_MODE_COUNT8,
  TC_COUNTER_SIZE_16BIT = TC_CTRLA_MODE_COUNT16,
  TC_COUNTER_SIZE_32BIT = TC_CTRLA_MODE_COUNT32
}
 Specifies if the counter is 8-, 16-, or 32-bit. More...
 
enum  tc_reload_action {
  TC_RELOAD_ACTION_GCLK = TC_CTRLA_PRESCSYNC_GCLK,
  TC_RELOAD_ACTION_PRESC = TC_CTRLA_PRESCSYNC_PRESC,
  TC_RELOAD_ACTION_RESYNC = TC_CTRLA_PRESCSYNC_RESYNC
}
 TC Counter reload action enum. More...
 
enum  tc_wave_generation {
  TC_WAVE_GENERATION_NORMAL_FREQ = TC_WAVE_GENERATION_NORMAL_FREQ_MODE,
  TC_WAVE_GENERATION_MATCH_FREQ = TC_WAVE_GENERATION_MATCH_FREQ_MODE,
  TC_WAVE_GENERATION_NORMAL_PWM = TC_WAVE_GENERATION_NORMAL_PWM_MODE,
  TC_WAVE_GENERATION_MATCH_PWM = TC_WAVE_GENERATION_MATCH_PWM_MODE
}
 TC wave generation mode enum. More...
 

Functions

Driver Initialization and Configuration
static bool tc_is_syncing (const struct tc_module *const module_inst)
 Determines if the hardware module(s) are currently synchronizing to the bus. More...
 
static void tc_get_config_defaults (struct tc_config *const config)
 Initializes config with predefined default values. More...
 
enum status_code tc_init (struct tc_module *const module_inst, Tc *const hw, const struct tc_config *const config)
 Initializes a hardware TC module instance. More...
 
Event Management
static void tc_enable_events (struct tc_module *const module_inst, struct tc_events *const events)
 Enables a TC module event input or output. More...
 
static void tc_disable_events (struct tc_module *const module_inst, struct tc_events *const events)
 Disables a TC module event input or output. More...
 
Enable/Disable/Reset
enum status_code tc_reset (const struct tc_module *const module_inst)
 Resets the TC module. More...
 
static void tc_enable (const struct tc_module *const module_inst)
 Enable the TC module. More...
 
static void tc_disable (const struct tc_module *const module_inst)
 Disables the TC module. More...
 
Get/Set Count Value
uint32_t tc_get_count_value (const struct tc_module *const module_inst)
 Get TC module count value. More...
 
enum status_code tc_set_count_value (const struct tc_module *const module_inst, const uint32_t count)
 Sets TC module count value. More...
 
Start/Stop Counter
static void tc_stop_counter (const struct tc_module *const module_inst)
 Stops the counter. More...
 
static void tc_start_counter (const struct tc_module *const module_inst)
 Starts the counter. More...
 
Double Buffering
static void tc_update_double_buffer (const struct tc_module *const module_inst)
 Update double buffer. More...
 
Count Read Synchronization
static void tc_sync_read_count (const struct tc_module *const module_inst)
 Read synchronization of COUNT. More...
 
Generate TC DMA Triggers Command
static void tc_dma_trigger_command (const struct tc_module *const module_inst)
 TC DMA Trigger. More...
 
Get Capture Set Compare
uint32_t tc_get_capture_value (const struct tc_module *const module_inst, const enum tc_compare_capture_channel channel_index)
 Gets the TC module capture value. More...
 
enum status_code tc_set_compare_value (const struct tc_module *const module_inst, const enum tc_compare_capture_channel channel_index, const uint32_t compare_value)
 Sets a TC module compare value. More...
 
Set Top Value
enum status_code tc_set_top_value (const struct tc_module *const module_inst, const uint32_t top_value)
 Set the timer TOP/period value. More...
 
Status Management
static uint32_t tc_get_status (struct tc_module *const module_inst)
 Retrieves the current module status. More...
 
static void tc_clear_status (struct tc_module *const module_inst, const uint32_t status_flags)
 Clears a module status flag. More...
 

Waveform Inversion Mode

#define TC_WAVEFORM_INVERT_CC0_MODE   TC_CTRLC_INVEN(1)
 Waveform inversion CC0 mode. More...
 
#define TC_WAVEFORM_INVERT_CC1_MODE   TC_CTRLC_INVEN(2)
 Waveform inversion CC1 mode. More...
 
enum  tc_waveform_invert_output {
  TC_WAVEFORM_INVERT_OUTPUT_NONE = 0,
  TC_WAVEFORM_INVERT_OUTPUT_CHANNEL_0 = TC_WAVEFORM_INVERT_CC0_MODE,
  TC_WAVEFORM_INVERT_OUTPUT_CHANNEL_1 = TC_WAVEFORM_INVERT_CC1_MODE
}
 Waveform inversion mode. More...
 
enum  tc_event_action {
  TC_EVENT_ACTION_OFF = TC_EVCTRL_EVACT_OFF,
  TC_EVENT_ACTION_RETRIGGER = TC_EVCTRL_EVACT_RETRIGGER,
  TC_EVENT_ACTION_INCREMENT_COUNTER = TC_EVCTRL_EVACT_COUNT,
  TC_EVENT_ACTION_START = TC_EVCTRL_EVACT_START,
  TC_EVENT_ACTION_PPW = TC_EVCTRL_EVACT_PPW,
  TC_EVENT_ACTION_PWP = TC_EVCTRL_EVACT_PWP,
  TC_EVENT_ACTION_STAMP = TC_EVCTRL_EVACT_STAMP,
  TC_EVENT_ACTION_PW = TC_EVCTRL_EVACT_PW
}
 Action to perform when the TC module is triggered by an event. More...
 
typedef void(* tc_callback_t )(struct tc_module *const module)
 Type of the callback functions. More...