Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Timer Counter (TC)

See Quick Start Guide for the XMEGA TC Driver.

This is a driver for the AVR XMEGA Timer Counter (TC). It provides functions for enabling, disabling and configuring the TC modules.

Dependencies

This driver depends on the following modules:

Modules

 
 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the Timer Counter (TC).
 
 Timer Counter (TC) interrupt management
 This group provides functions to configure TC module interrupts.
 
 AWeX extension driver
 This group provides low level drivers to configure AWeX extension.
 
 Hi-Res extension driver
 This group provides low level drivers to configure Hi-Res extension.
 

Macros

#define tc_is_tc0(void)   ((uint16_t)tc&0x40 ? false : true)
 Macro to check if type of passed TC is TC0_t. More...
 
#define tc_is_tc1(void)   ((uint16_t)tc&0x40 ? true : false)
 Macro to check if type of passed TC is TC1_t. More...
 

Typedefs

typedef void(* tc_callback_t )(void)
 Interrupt event callback function type. More...
 

Enumerations

enum  tc_cc_channel_mask_enable_t {
  TC_CCAEN = TC0_CCAEN_bm,
  TC_CCBEN = TC0_CCBEN_bm,
  TC_CCCEN = TC0_CCCEN_bm,
  TC_CCDEN = TC0_CCDEN_bm
}
 Timer Counter Capture Compare Channel index. More...
 
enum  tc_cc_channel_t {
  TC_CCA = 1,
  TC_CCB = 2,
  TC_CCC = 3,
  TC_CCD = 4
}
 Timer Counter Capture Compare Channel index. More...
 
enum  tc_dir_t {
  TC_UP = 0,
  TC_DOWN = 1
}
 Timer Counter Direction. More...
 
enum  TC_INT_LEVEL_t {
  TC_INT_LVL_OFF = 0x00,
  TC_INT_LVL_LO = 0x01,
  TC_INT_LVL_MED = 0x02,
  TC_INT_LVL_HI = 0x03
}
 TC interrupt levels. More...
 
enum  tc_wg_mode_t {
  TC_WG_NORMAL = TC_WGMODE_NORMAL_gc,
  TC_WG_FRQ = TC_WGMODE_FRQ_gc,
  TC_WG_SS = TC_WGMODE_SS_gc,
  TC_WG_DS_T = TC_WGMODE_DS_T_gc,
  TC_WG_DS_TB = TC_WGMODE_DS_TB_gc,
  TC_WG_DS_B = TC_WGMODE_DS_B_gc
}
 Timer Counter Waveform Generator mode. More...
 

Functions

static bool tc_cc_buffer_is_valid (volatile void *tc, enum tc_cc_channel_t channel_index)
 Reports is Capture/Compare Buffer is valid. More...
 
static void tc_clear_cc_interrupt (volatile void *tc, enum tc_cc_channel_t channel_index)
 Clears Capture/Compare interrupt. More...
 
static void tc_clear_error (volatile void *tc)
 Clears the Error flag. More...
 
static void tc_clear_overflow (volatile void *tc)
 Clears the Overflow flag. More...
 
void tc_disable (volatile void *tc)
 Disable TC. More...
 
static void tc_disable_cc_channels (volatile void *tc, enum tc_cc_channel_mask_enable_t disablemask)
 Disables Compare/Capture channel. More...
 
static void tc_disable_delay (volatile void *tc)
 Disables delay. More...
 
void tc_enable (volatile void *tc)
 Enable TC. More...
 
static void tc_enable_cc_channels (volatile void *tc, enum tc_cc_channel_mask_enable_t enablemask)
 Enables Compare/Capture channel. More...
 
static void tc_enable_delay (volatile void *tc)
 Enables delay (used for 32bit timer mode) More...
 
static uint32_t tc_get_resolution (volatile void *tc)
 Get real resolution for a specified TC. More...
 
static bool tc_is_cc_interrupt (volatile void *tc, enum tc_cc_channel_t channel_index)
 Reports if Capture/Compare interrupt has occurred. More...
 
static bool tc_is_overflow (volatile void *tc)
 Tests if the Overflow flag is set. More...
 
static void tc_lock_update_buffers (volatile void *tc)
 Locks the Update of the Buffered registers. More...
 
static bool tc_period_buffer_is_valid (volatile void *tc)
 Tests if the Period Buffer is valid. More...
 
static uint16_t tc_read_cc (volatile void *tc, enum tc_cc_channel_t channel_index)
 Reads the Capture value. More...
 
static uint16_t tc_read_cc_buffer (volatile void *tc, enum tc_cc_channel_t channel_index)
 Reads the Capture/Compare Buffer value. More...
 
static TC_CLKSEL_t tc_read_clock_source (volatile void *tc)
 Read Timer Clock Source. More...
 
static uint16_t tc_read_count (volatile void *tc)
 Reads the Counter value of the Timer. More...
 
static bool tc_read_error (volatile void *tc)
 Tests if the Error flag is set. More...
 
static uint16_t tc_read_period (volatile void *tc)
 Reads the Period value of the Timer. More...
 
static uint16_t tc_read_period_buffer (volatile void *tc)
 Reads the Period Buffer value of the Timer. More...
 
static void tc_reset (volatile void *tc)
 Reset the Timer. More...
 
static void tc_restart (volatile void *tc)
 Restart the Timer. More...
 
static void tc_set_8bits_mode (volatile void *tc)
 Configures the Timer in Byte mode. More...
 
static void tc_set_direction (volatile void *tc, enum tc_dir_t dir)
 Configure Timer Direction. More...
 
static void tc_set_input_capture (volatile void *tc, TC_EVSEL_t eventsource, TC_EVACT_t eventaction)
 Enables Input capture mode. More...
 
static void tc_set_resolution (volatile void *tc, uint32_t resolution)
 Select clock for a specified TC and resolution. More...
 
static void tc_set_wgm (volatile void *tc, enum tc_wg_mode_t wgm)
 Configures TC in the specified Waveform generator mode. More...
 
static void tc_unlock_update_buffers (volatile void *tc)
 Unlocks the Update of the Buffered registers. More...
 
static void tc_update (volatile void *tc)
 Update the Timer. More...
 
static void tc_write_cc (volatile void *tc, enum tc_cc_channel_t channel_index, uint16_t value)
 Writes the CC value. More...
 
static void tc_write_cc_buffer (volatile void *tc, enum tc_cc_channel_t channel_index, uint16_t buffer_value)
 Writes the Capture/Compare Buffer value. More...
 
static void tc_write_clock_source (volatile void *tc, TC_CLKSEL_t TC_CLKSEL_enum)
 Configure Timer Clock Source. More...
 
static void tc_write_count (volatile void *tc, uint16_t cnt_value)
 Write the Counter value of the Timer. More...
 
static void tc_write_period (volatile void *tc, uint16_t per_value)
 Writes the Period value of the Timer. More...
 
static void tc_write_period_buffer (volatile void *tc, uint16_t per_buf)
 Writes the Period Buffer value of the Timer. More...
 

#define tc_is_tc0 (   void)    ((uint16_t)tc&0x40 ? false : true)
#define tc_is_tc1 (   void)    ((uint16_t)tc&0x40 ? true : false)

typedef void(* tc_callback_t)(void)

Interrupt event callback function type.

The interrupt handler can be configured to do a function callback, the callback function must match the tc_callback_t type.

Timer Counter Capture Compare Channel index.

Enumerator
TC_CCAEN 

Channel A Enable mask.

TC_CCBEN 

Channel B Enable mask.

TC_CCCEN 

Channel C Enable mask.

TC_CCDEN 

Channel D Enable mask.

Timer Counter Capture Compare Channel index.

Enumerator
TC_CCA 

Channel A.

TC_CCB 

Channel B.

TC_CCC 

Channel C.

TC_CCD 

Channel D.

enum tc_dir_t

Timer Counter Direction.

Enumerator
TC_UP 

Counting up.

TC_DOWN 

Down Counting B.

TC interrupt levels.

Enumerator
TC_INT_LVL_OFF 
TC_INT_LVL_LO 
TC_INT_LVL_MED 
TC_INT_LVL_HI 

Timer Counter Waveform Generator mode.

Enumerator
TC_WG_NORMAL 

TC in normal Mode.

TC_WG_FRQ 

TC in Frequency Generator mode.

TC_WG_SS 

TC in single slope PWM mode.

TC_WG_DS_T 

TC in dual slope Top PWM mode.

TC_WG_DS_TB 

TC in dual slope Top Bottom PWM mode.

TC_WG_DS_B 

TC in dual slope Bottom PWM mode.

static bool tc_cc_buffer_is_valid ( volatile void *  tc,
enum tc_cc_channel_t  channel_index 
)
inlinestatic

Reports is Capture/Compare Buffer is valid.

Parameters
tcPointer to TC module.
channel_indexCC Channel
Returns
CCx Buffer is valid or not

References TC_CCA, TC_CCB, TC_CCC, TC_CCD, tc_is_tc0, and tc_is_tc1.

static void tc_clear_cc_interrupt ( volatile void *  tc,
enum tc_cc_channel_t  channel_index 
)
inlinestatic

Clears Capture/Compare interrupt.

Parameters
tcPointer to TC module.
channel_indexCC Channel

References TC_CCA, TC_CCB, TC_CCC, TC_CCD, tc_is_tc0, and tc_is_tc1.

Referenced by main().

static void tc_clear_error ( volatile void *  tc)
inlinestatic

Clears the Error flag.

Parameters
tcPointer to TC module.
Note
ERRIF is cleared
static void tc_clear_overflow ( volatile void *  tc)
inlinestatic

Clears the Overflow flag.

Parameters
tcPointer to TC module.
Note
OVFIF is cleared

Referenced by play_game(), and record_buttons().

void tc_disable ( volatile void *  tc)

Disable TC.

Disables the TC.

Parameters
tcPointer to TC module
Note
mask TC clock (sysclk).

References cpu_irq_restore(), cpu_irq_save(), SLEEPMGR_IDLE, sleepmgr_unlock_mode(), sysclk_disable_module(), SYSCLK_HIRES, SYSCLK_PORT_C, SYSCLK_PORT_D, SYSCLK_PORT_E, SYSCLK_PORT_F, SYSCLK_TC0, and SYSCLK_TC1.

Referenced by dma_transfer_is_complete(), and pwm_disable().

static void tc_disable_cc_channels ( volatile void *  tc,
enum tc_cc_channel_mask_enable_t  disablemask 
)
inlinestatic

Disables Compare/Capture channel.

Parameters
tcPointer to TC module.
disablemaskCC channel

References tc_is_tc0, and tc_is_tc1.

static void tc_disable_delay ( volatile void *  tc)
inlinestatic

Disables delay.

Parameters
tcPointer to TC module.
Note
disables Delay mode
void tc_enable ( volatile void *  tc)
static void tc_enable_cc_channels ( volatile void *  tc,
enum tc_cc_channel_mask_enable_t  enablemask 
)
inlinestatic

Enables Compare/Capture channel.

Parameters
tcPointer to TC module.
enablemaskCC channel

References tc_is_tc0, and tc_is_tc1.

Referenced by main(), pwm_start(), and qdec_enabled_tc_freq().

static void tc_enable_delay ( volatile void *  tc)
inlinestatic

Enables delay (used for 32bit timer mode)

Parameters
tcPointer to TC module.
Note
enables Delay mode
static uint32_t tc_get_resolution ( volatile void *  tc)
inlinestatic

Get real resolution for a specified TC.

This function returns the resolution which the specified clock selection of TC will result in. The resolution of a TC is synonymous with its clock frequency.

Note
This function does not handle event channel clock selections.
Parameters
tcPointer of TC module to get resolution for.
Returns
The resolution of tc.

References sysclk_get_per_hz(), and tc_read_clock_source().

Referenced by qdec_enabled_tc_freq(), sha204h_start_timer(), and start_debounce_timer().

static bool tc_is_cc_interrupt ( volatile void *  tc,
enum tc_cc_channel_t  channel_index 
)
inlinestatic

Reports if Capture/Compare interrupt has occurred.

Parameters
tcPointer to TC module.
channel_indexCC Channel
Returns
CCx Interrupt or not

References TC_CCA, TC_CCB, TC_CCC, TC_CCD, tc_is_tc0, and tc_is_tc1.

Referenced by qdec_get_frequency().

static bool tc_is_overflow ( volatile void *  tc)
inlinestatic

Tests if the Overflow flag is set.

Parameters
tcPointer to TC module.
Returns
overflow has occurred or not : OVFIF

Referenced by play_game(), and record_buttons().

static void tc_lock_update_buffers ( volatile void *  tc)
inlinestatic

Locks the Update of the Buffered registers.

Parameters
tcPointer to TC module.
static bool tc_period_buffer_is_valid ( volatile void *  tc)
inlinestatic

Tests if the Period Buffer is valid.

Parameters
tcPointer to TC module.
Returns
period Buffer is valid or not:PERBV
static uint16_t tc_read_cc ( volatile void *  tc,
enum tc_cc_channel_t  channel_index 
)
inlinestatic

Reads the Capture value.

Parameters
tcPointer to TC module.
channel_indexChannel x
Returns
Read value of CCx

References TC_CCA, TC_CCB, TC_CCC, TC_CCD, tc_is_tc0, and tc_is_tc1.

Referenced by cca_callback(), example_overflow_interrupt_callback(), and qdec_get_frequency().

static uint16_t tc_read_cc_buffer ( volatile void *  tc,
enum tc_cc_channel_t  channel_index 
)
inlinestatic

Reads the Capture/Compare Buffer value.

Parameters
tcPointer to TC module.
channel_indexCC Channel
Returns
CCx Buffer value

References TC_CCA, TC_CCB, TC_CCC, TC_CCD, tc_is_tc0, and tc_is_tc1.

static TC_CLKSEL_t tc_read_clock_source ( volatile void *  tc)
inlinestatic

Read Timer Clock Source.

Parameters
tcPointer to TC module.
Returns
TC_CLKSEL_enum Clock source selection

Referenced by tc_get_resolution().

static uint16_t tc_read_count ( volatile void *  tc)
inlinestatic

Reads the Counter value of the Timer.

Parameters
tcPointer to TC module.
Note
Output the Counter value CNT

Referenced by qdec_get_position().

static bool tc_read_error ( volatile void *  tc)
inlinestatic

Tests if the Error flag is set.

Parameters
tcPointer to TC module.
Returns
Error has occurred or not : ERRIF
static uint16_t tc_read_period ( volatile void *  tc)
inlinestatic

Reads the Period value of the Timer.

Parameters
tcPointer to TC module.
Returns
Period value : PER
static uint16_t tc_read_period_buffer ( volatile void *  tc)
inlinestatic

Reads the Period Buffer value of the Timer.

Parameters
tcPointer to TC module.
Returns
Period Buffer value : PERH/PERL
static void tc_reset ( volatile void *  tc)
inlinestatic

Reset the Timer.

Parameters
tcPointer to TC module.
Note
CMD[3:2] in CTRLFSET are set to 1
static void tc_restart ( volatile void *  tc)
inlinestatic

Restart the Timer.

Parameters
tcPointer to TC module.
Note
CMD[3] in CTRLFSET is set to 1 and CMD[2] in CTRLFCLR is set

Referenced by sha204h_start_timer().

static void tc_set_8bits_mode ( volatile void *  tc)
inlinestatic

Configures the Timer in Byte mode.

Parameters
tcPointer to TC module.
Note
Configures BYTEM in CTRLE
static void tc_set_direction ( volatile void *  tc,
enum tc_dir_t  dir 
)
inlinestatic

Configure Timer Direction.

Parameters
tcPointer to TC module.
dirTimer direction :

References TC_UP.

Referenced by run_dma_triggered_with_callback().

static void tc_set_input_capture ( volatile void *  tc,
TC_EVSEL_t  eventsource,
TC_EVACT_t  eventaction 
)
inlinestatic

Enables Input capture mode.

Parameters
tcPointer to TC module.
eventsourceSource for the capture
eventactionEvent action capture type

Referenced by main(), qdec_enabled_tc(), and qdec_enabled_tc_freq().

static void tc_set_resolution ( volatile void *  tc,
uint32_t  resolution 
)
inlinestatic

Select clock for a specified TC and resolution.

This function configures the clock selection, as prescaled CLKper, for a specified TC that gives a resolution at least as high as the one specified. The resolution of a TC is synonymous with its clock frequency.

Note
It is also possible to clock TCs with event channels. This is not handled by this implementation.
Parameters
tcID of TC to get clock selection for.
resolutionDesired resolution for the TC in Hz.

References sysclk_get_per_hz(), and tc_write_clock_source().

Referenced by main(), qdec_enabled_tc_freq(), run_dma_triggered_with_callback(), sha204h_start_timer(), and start_debounce_timer().

static void tc_set_wgm ( volatile void *  tc,
enum tc_wg_mode_t  wgm 
)
inlinestatic

Configures TC in the specified Waveform generator mode.

Parameters
tcPointer to TC module.
wgm: waveform generator

Referenced by main(), pwm_init(), sha204h_timer_init(), and start_debounce_timer().

static void tc_unlock_update_buffers ( volatile void *  tc)
inlinestatic

Unlocks the Update of the Buffered registers.

Parameters
tcPointer to TC module.
Note
Configures LUPD in CTRLFCLR
static void tc_update ( volatile void *  tc)
inlinestatic

Update the Timer.

Parameters
tcPointer to TC module.
Note
CMD[2] in CTRLFSET is set to 1 and CMD[3] in CTRLFCLR is set
static void tc_write_cc ( volatile void *  tc,
enum tc_cc_channel_t  channel_index,
uint16_t  value 
)
inlinestatic

Writes the CC value.

Parameters
tcPointer to TC module.
channel_indexCC Channel
valueCounter value

References TC_CCA, TC_CCB, TC_CCC, TC_CCD, tc_is_tc0, and tc_is_tc1.

Referenced by example_overflow_interrupt_callback(), and main().

static void tc_write_cc_buffer ( volatile void *  tc,
enum tc_cc_channel_t  channel_index,
uint16_t  buffer_value 
)
inlinestatic

Writes the Capture/Compare Buffer value.

Parameters
tcPointer to TC module.
channel_indexCC Channel
buffer_valueCounter Buffer value

References TC_CCA, TC_CCB, TC_CCC, TC_CCD, tc_is_tc0, and tc_is_tc1.

Referenced by example_cca_interrupt_callback(), example_ccb_interrupt_callback(), and pwm_set_duty_cycle_percent().

static void tc_write_clock_source ( volatile void *  tc,
TC_CLKSEL_t  TC_CLKSEL_enum 
)
inlinestatic

Configure Timer Clock Source.

Parameters
tcPointer to TC module.
TC_CLKSEL_enumClock source selection
Note
Configuring the clock also starts the timer

Referenced by pwm_init(), pwm_start(), pwm_stop(), qdec_enabled_tc(), sound_start_timer(), sound_stop_timer(), start_save_timer(), tc_set_resolution(), and timeout_callback().

static void tc_write_count ( volatile void *  tc,
uint16_t  cnt_value 
)
inlinestatic

Write the Counter value of the Timer.

Parameters
tcPointer to TC module.
cnt_valueCounter value :

Referenced by pwm_timer_reset(), qdec_enabled_tc(), qdec_enabled_tc_freq(), and qdec_reset_position().

static void tc_write_period ( volatile void *  tc,
uint16_t  per_value 
)
inlinestatic

Writes the Period value of the Timer.

Parameters
tcPointer to TC module.
per_valuePeriod value : PER

Referenced by init_save_timer(), main(), pwm_start(), qdec_enabled_tc(), qdec_enabled_tc_freq(), run_dma_triggered_with_callback(), sha204h_start_timer(), sound_init(), and start_debounce_timer().

static void tc_write_period_buffer ( volatile void *  tc,
uint16_t  per_buf 
)
inlinestatic

Writes the Period Buffer value of the Timer.

Parameters
tcPointer to TC module.
per_bufPeriod Buffer value : PERH/PERL