Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TC - Timer/Counter

Driver for the TC (Timer/Counter).

The TC includes three identical 16-bit Timer/Counter channels. Each channel can be independently programmed to perform a wide range of functions including frequency measurement, event counting, interval measurement, pulse generation, delay timing and pulse width modulation.

Data Structures

struct  tc_capture_opt_t
 Parameters when initializing a timer/counter in capture mode. More...
 
struct  tc_interrupt_t
 Timer/counter interrupts. More...
 
struct  tc_waveform_opt_t
 Parameters when initializing a timer/counter in waveform mode. More...
 

Macros

#define TC_INVALID_ARGUMENT   (-1)
 TC driver functions return value in case of invalid argument(s). More...
 
#define TC_NUMBER_OF_CHANNELS   (sizeof(((avr32_tc_t *)0)->channel) / sizeof(avr32_tc_channel_t))
 Number of timer/counter channels. More...
 

Functions

int tc_configure_interrupts (volatile avr32_tc_t *tc, unsigned int channel, const tc_interrupt_t *bitfield)
 Enables various timer/counter interrupts. More...
 
int tc_get_interrupt_settings (volatile avr32_tc_t *tc, unsigned int channel)
 Reads timer/counter interrupt settings. More...
 
int tc_init_capture (volatile avr32_tc_t *tc, const tc_capture_opt_t *opt)
 Sets options for timer/counter capture initialization. More...
 
int tc_init_waveform (volatile avr32_tc_t *tc, const tc_waveform_opt_t *opt)
 Sets options for timer/counter waveform initialization. More...
 
int tc_read_ra (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the channel's RA register and returns the value. More...
 
int tc_read_rb (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the channel's RB register and returns the value. More...
 
int tc_read_rc (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the channel's RC register and returns the value. More...
 
int tc_read_sr (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the status register. More...
 
int tc_read_tc (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the channel's TC counter and returns the value. More...
 
int tc_select_external_clock (volatile avr32_tc_t *tc, unsigned int channel, unsigned int ext_clk_sig_src)
 Selects which external clock to use and how to configure it. More...
 
int tc_software_trigger (volatile avr32_tc_t *tc, unsigned int channel)
 Performs a software trigger: the counter is reset and the clock is started. More...
 
int tc_start (volatile avr32_tc_t *tc, unsigned int channel)
 Starts a timer/counter. More...
 
int tc_stop (volatile avr32_tc_t *tc, unsigned int channel)
 Stops a timer/counter. More...
 
void tc_sync_start (volatile avr32_tc_t *tc)
 Start all TC channels simultaneously. More...
 
void tc_sync_trigger (volatile avr32_tc_t *tc)
 Asserts a SYNC signal to generate a software trigger and reset all channels. More...
 
int tc_write_ra (volatile avr32_tc_t *tc, unsigned int channel, unsigned short value)
 Writes a value to the channel's RA register. More...
 
int tc_write_rb (volatile avr32_tc_t *tc, unsigned int channel, unsigned short value)
 Writes a value to the channel's RB register. More...
 
int tc_write_rc (volatile avr32_tc_t *tc, unsigned int channel, unsigned short value)
 Writes a value to the channel's RC register. More...
 

External Clock Signal 0 Selection

#define TC_CH0_EXT_CLK0_SRC_TCLK0   AVR32_TC_TC0XC0S_TCLK0
 
#define TC_CH0_EXT_CLK0_SRC_NO_CLK   AVR32_TC_TC0XC0S_NO_CLK
 
#define TC_CH0_EXT_CLK0_SRC_TIOA1   AVR32_TC_TC0XC0S_TIOA1
 
#define TC_CH0_EXT_CLK0_SRC_TIOA2   AVR32_TC_TC0XC0S_TIOA2
 

External Clock Signal 1 Selection

#define TC_CH1_EXT_CLK1_SRC_TCLK1   AVR32_TC_TC1XC1S_TCLK1
 
#define TC_CH1_EXT_CLK1_SRC_NO_CLK   AVR32_TC_TC1XC1S_NO_CLK
 
#define TC_CH1_EXT_CLK1_SRC_TIOA0   AVR32_TC_TC1XC1S_TIOA0
 
#define TC_CH1_EXT_CLK1_SRC_TIOA2   AVR32_TC_TC1XC1S_TIOA2
 

External Clock Signal 2 Selection

#define TC_CH2_EXT_CLK2_SRC_TCLK2   AVR32_TC_TC2XC2S_TCLK2
 
#define TC_CH2_EXT_CLK2_SRC_NO_CLK   AVR32_TC_TC2XC2S_NO_CLK
 
#define TC_CH2_EXT_CLK2_SRC_TIOA0   AVR32_TC_TC2XC2S_TIOA0
 
#define TC_CH2_EXT_CLK2_SRC_TIOA1   AVR32_TC_TC2XC2S_TIOA1
 

Event/Trigger Actions on Output

#define TC_EVT_EFFECT_NOOP   AVR32_TC_NONE
 
#define TC_EVT_EFFECT_SET   AVR32_TC_SET
 
#define TC_EVT_EFFECT_CLEAR   AVR32_TC_CLEAR
 
#define TC_EVT_EFFECT_TOGGLE   AVR32_TC_TOGGLE
 

RC Compare Trigger Enable

#define TC_NO_TRIGGER_COMPARE_RC   0
 
#define TC_TRIGGER_COMPARE_RC   1
 

Waveform Selection

#define TC_WAVEFORM_SEL_UP_MODE   AVR32_TC_WAVSEL_UP_NO_AUTO
 
#define TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER   AVR32_TC_WAVSEL_UP_AUTO
 
#define TC_WAVEFORM_SEL_UPDOWN_MODE   AVR32_TC_WAVSEL_UPDOWN_NO_AUTO
 
#define TC_WAVEFORM_SEL_UPDOWN_MODE_RC_TRIGGER   AVR32_TC_WAVSEL_UPDOWN_AUTO
 

TIOA or TIOB External Trigger Selection

#define TC_EXT_TRIG_SEL_TIOA   1
 
#define TC_EXT_TRIG_SEL_TIOB   0
 

External Event Selection

Attention
See this note for important details about this setting.
#define TC_EXT_EVENT_SEL_TIOB_INPUT   AVR32_TC_EEVT_TIOB_INPUT
 
#define TC_EXT_EVENT_SEL_XC0_OUTPUT   AVR32_TC_EEVT_XC0_OUTPUT
 
#define TC_EXT_EVENT_SEL_XC1_OUTPUT   AVR32_TC_EEVT_XC1_OUTPUT
 
#define TC_EXT_EVENT_SEL_XC2_OUTPUT   AVR32_TC_EEVT_XC2_OUTPUT
 

Edge Selection

#define TC_SEL_NO_EDGE   AVR32_TC_EEVTEDG_NO_EDGE
 
#define TC_SEL_RISING_EDGE   AVR32_TC_EEVTEDG_POS_EDGE
 
#define TC_SEL_FALLING_EDGE   AVR32_TC_EEVTEDG_NEG_EDGE
 
#define TC_SEL_EACH_EDGE   AVR32_TC_EEVTEDG_BOTH_EDGES
 

Burst Signal Selection

#define TC_BURST_NOT_GATED   AVR32_TC_BURST_NOT_GATED
 
#define TC_BURST_CLK_AND_XC0   AVR32_TC_BURST_CLK_AND_XC0
 
#define TC_BURST_CLK_AND_XC1   AVR32_TC_BURST_CLK_AND_XC1
 
#define TC_BURST_CLK_AND_XC2   AVR32_TC_BURST_CLK_AND_XC2
 

Clock Invert

#define TC_CLOCK_RISING_EDGE   0
 
#define TC_CLOCK_FALLING_EDGE   1
 

Clock Selection

#define TC_CLOCK_SOURCE_TC1   AVR32_TC_TCCLKS_TIMER_CLOCK1
 
#define TC_CLOCK_SOURCE_TC2   AVR32_TC_TCCLKS_TIMER_CLOCK2
 
#define TC_CLOCK_SOURCE_TC3   AVR32_TC_TCCLKS_TIMER_CLOCK3
 
#define TC_CLOCK_SOURCE_TC4   AVR32_TC_TCCLKS_TIMER_CLOCK4
 
#define TC_CLOCK_SOURCE_TC5   AVR32_TC_TCCLKS_TIMER_CLOCK5
 
#define TC_CLOCK_SOURCE_XC0   AVR32_TC_TCCLKS_XC0
 
#define TC_CLOCK_SOURCE_XC1   AVR32_TC_TCCLKS_XC1
 
#define TC_CLOCK_SOURCE_XC2   AVR32_TC_TCCLKS_XC2
 

#define TC_BURST_CLK_AND_XC0   AVR32_TC_BURST_CLK_AND_XC0
#define TC_BURST_CLK_AND_XC1   AVR32_TC_BURST_CLK_AND_XC1
#define TC_BURST_CLK_AND_XC2   AVR32_TC_BURST_CLK_AND_XC2
#define TC_BURST_NOT_GATED   AVR32_TC_BURST_NOT_GATED
#define TC_CH0_EXT_CLK0_SRC_NO_CLK   AVR32_TC_TC0XC0S_NO_CLK
#define TC_CH0_EXT_CLK0_SRC_TCLK0   AVR32_TC_TC0XC0S_TCLK0
#define TC_CH0_EXT_CLK0_SRC_TIOA1   AVR32_TC_TC0XC0S_TIOA1
#define TC_CH0_EXT_CLK0_SRC_TIOA2   AVR32_TC_TC0XC0S_TIOA2
#define TC_CH1_EXT_CLK1_SRC_NO_CLK   AVR32_TC_TC1XC1S_NO_CLK
#define TC_CH1_EXT_CLK1_SRC_TCLK1   AVR32_TC_TC1XC1S_TCLK1
#define TC_CH1_EXT_CLK1_SRC_TIOA0   AVR32_TC_TC1XC1S_TIOA0
#define TC_CH1_EXT_CLK1_SRC_TIOA2   AVR32_TC_TC1XC1S_TIOA2
#define TC_CH2_EXT_CLK2_SRC_NO_CLK   AVR32_TC_TC2XC2S_NO_CLK
#define TC_CH2_EXT_CLK2_SRC_TCLK2   AVR32_TC_TC2XC2S_TCLK2
#define TC_CH2_EXT_CLK2_SRC_TIOA0   AVR32_TC_TC2XC2S_TIOA0
#define TC_CH2_EXT_CLK2_SRC_TIOA1   AVR32_TC_TC2XC2S_TIOA1
#define TC_CLOCK_FALLING_EDGE   1
#define TC_CLOCK_RISING_EDGE   0
#define TC_CLOCK_SOURCE_TC1   AVR32_TC_TCCLKS_TIMER_CLOCK1
#define TC_CLOCK_SOURCE_TC2   AVR32_TC_TCCLKS_TIMER_CLOCK2
#define TC_CLOCK_SOURCE_TC3   AVR32_TC_TCCLKS_TIMER_CLOCK3
#define TC_CLOCK_SOURCE_TC4   AVR32_TC_TCCLKS_TIMER_CLOCK4
#define TC_CLOCK_SOURCE_TC5   AVR32_TC_TCCLKS_TIMER_CLOCK5
#define TC_CLOCK_SOURCE_XC0   AVR32_TC_TCCLKS_XC0
#define TC_CLOCK_SOURCE_XC1   AVR32_TC_TCCLKS_XC1
#define TC_CLOCK_SOURCE_XC2   AVR32_TC_TCCLKS_XC2
#define TC_EVT_EFFECT_CLEAR   AVR32_TC_CLEAR
#define TC_EVT_EFFECT_NOOP   AVR32_TC_NONE

Referenced by init_timer_isr().

#define TC_EVT_EFFECT_SET   AVR32_TC_SET
#define TC_EVT_EFFECT_TOGGLE   AVR32_TC_TOGGLE
#define TC_EXT_EVENT_SEL_TIOB_INPUT   AVR32_TC_EEVT_TIOB_INPUT
#define TC_EXT_EVENT_SEL_XC0_OUTPUT   AVR32_TC_EEVT_XC0_OUTPUT
#define TC_EXT_EVENT_SEL_XC1_OUTPUT   AVR32_TC_EEVT_XC1_OUTPUT
#define TC_EXT_EVENT_SEL_XC2_OUTPUT   AVR32_TC_EEVT_XC2_OUTPUT
#define TC_EXT_TRIG_SEL_TIOA   1
#define TC_EXT_TRIG_SEL_TIOB   0
#define TC_NO_TRIGGER_COMPARE_RC   0
#define TC_NUMBER_OF_CHANNELS   (sizeof(((avr32_tc_t *)0)->channel) / sizeof(avr32_tc_channel_t))
#define TC_SEL_EACH_EDGE   AVR32_TC_EEVTEDG_BOTH_EDGES
#define TC_SEL_FALLING_EDGE   AVR32_TC_EEVTEDG_NEG_EDGE
#define TC_SEL_NO_EDGE   AVR32_TC_EEVTEDG_NO_EDGE

Referenced by init_timer_isr().

#define TC_SEL_RISING_EDGE   AVR32_TC_EEVTEDG_POS_EDGE
#define TC_TRIGGER_COMPARE_RC   1
#define TC_WAVEFORM_SEL_UP_MODE   AVR32_TC_WAVSEL_UP_NO_AUTO
#define TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER   AVR32_TC_WAVSEL_UP_AUTO

Referenced by init_timer_isr().

#define TC_WAVEFORM_SEL_UPDOWN_MODE   AVR32_TC_WAVSEL_UPDOWN_NO_AUTO
#define TC_WAVEFORM_SEL_UPDOWN_MODE_RC_TRIGGER   AVR32_TC_WAVSEL_UPDOWN_AUTO

int tc_configure_interrupts ( volatile avr32_tc_t *  tc,
unsigned int  channel,
const tc_interrupt_t bitfield 
)

Enables various timer/counter interrupts.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
bitfieldThe interrupt enable configuration.
Return values
0Success.
TC_INVALID_ARGUMENTInvalid argument(s).

References tc_interrupt_t::covfs, tc_interrupt_t::cpas, tc_interrupt_t::cpbs, tc_interrupt_t::cpcs, Disable_global_interrupt, Enable_global_interrupt, tc_interrupt_t::etrgs, Is_global_interrupt_enabled, tc_interrupt_t::ldras, tc_interrupt_t::ldrbs, tc_interrupt_t::lovrs, TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

Referenced by init_timer_isr().

int tc_get_interrupt_settings ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Reads timer/counter interrupt settings.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
>=0The interrupt enable configuration organized according to tc_interrupt_t.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

int tc_init_capture ( volatile avr32_tc_t *  tc,
const tc_capture_opt_t opt 
)

Sets options for timer/counter capture initialization.

Parameters
tcPointer to the TC instance to access.
optOptions for capture mode.
Return values
0Success.
TC_INVALID_ARGUMENTInvalid argument(s).

References tc_capture_opt_t::abetrg, tc_capture_opt_t::burst, tc_capture_opt_t::channel, tc_capture_opt_t::clki, tc_capture_opt_t::cpctrg, tc_capture_opt_t::etrgedg, tc_capture_opt_t::ldbdis, tc_capture_opt_t::ldbstop, tc_capture_opt_t::ldra, tc_capture_opt_t::ldrb, TC_INVALID_ARGUMENT, TC_NUMBER_OF_CHANNELS, and tc_capture_opt_t::tcclks.

int tc_init_waveform ( volatile avr32_tc_t *  tc,
const tc_waveform_opt_t opt 
)

Sets options for timer/counter waveform initialization.

Parameters
tcPointer to the TC instance to access.
optOptions for waveform generation.
Return values
0Success.
TC_INVALID_ARGUMENTInvalid argument(s).

Attention
If the external event selection, EEVT, is set to TC_EXT_EVENT_SEL_TIOB_INPUT or the equivalent value 0, TIOB becomes an input even if external event triggers are not enabled (ENETRG).
Therefore, unless it is the intention to use TIOB as an external event trigger, EEVT should be set to any other value than TC_EXT_EVENT_SEL_TIOB_INPUT to allow normal operation of TIOB.

References tc_waveform_opt_t::acpa, tc_waveform_opt_t::acpc, tc_waveform_opt_t::aeevt, tc_waveform_opt_t::aswtrg, tc_waveform_opt_t::bcpb, tc_waveform_opt_t::bcpc, tc_waveform_opt_t::beevt, tc_waveform_opt_t::bswtrg, tc_waveform_opt_t::burst, tc_waveform_opt_t::channel, tc_waveform_opt_t::clki, tc_waveform_opt_t::cpcdis, tc_waveform_opt_t::cpcstop, tc_waveform_opt_t::eevt, tc_waveform_opt_t::eevtedg, tc_waveform_opt_t::enetrg, TC_INVALID_ARGUMENT, TC_NUMBER_OF_CHANNELS, tc_waveform_opt_t::tcclks, and tc_waveform_opt_t::wavsel.

Referenced by init_timer_isr().

int tc_read_ra ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Reads the channel's RA register and returns the value.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
>=0RA register value.
TC_INVALID_ARGUMENTInvalid argument(s).

References Rd_bitfield, TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

int tc_read_rb ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Reads the channel's RB register and returns the value.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
>=0RB register value.
TC_INVALID_ARGUMENTInvalid argument(s).

References Rd_bitfield, TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

int tc_read_rc ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Reads the channel's RC register and returns the value.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
>=0RC register value.
TC_INVALID_ARGUMENTInvalid argument(s).

References Rd_bitfield, TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

int tc_read_sr ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Reads the status register.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
>=0Status register value.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

Referenced by ISR().

int tc_read_tc ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Reads the channel's TC counter and returns the value.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
>=0TC counter value.
TC_INVALID_ARGUMENTInvalid argument(s).

References Rd_bitfield, TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

int tc_select_external_clock ( volatile avr32_tc_t *  tc,
unsigned int  channel,
unsigned int  ext_clk_sig_src 
)

Selects which external clock to use and how to configure it.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
ext_clk_sig_srcExternal clock signal selection:
  • TC_CH0_EXT_CLK0_SRC_TCLK0;
  • TC_CH0_EXT_CLK0_SRC_NO_CLK;
  • TC_CH0_EXT_CLK0_SRC_TIOA1;
  • TC_CH0_EXT_CLK0_SRC_TIOA2;
  • TC_CH1_EXT_CLK1_SRC_TCLK1;
  • TC_CH1_EXT_CLK1_SRC_NO_CLK;
  • TC_CH1_EXT_CLK1_SRC_TIOA0;
  • TC_CH1_EXT_CLK1_SRC_TIOA2;
  • TC_CH2_EXT_CLK2_SRC_TCLK2;
  • TC_CH2_EXT_CLK2_SRC_NO_CLK;
  • TC_CH2_EXT_CLK2_SRC_TIOA0;
  • TC_CH2_EXT_CLK2_SRC_TIOA1.
Return values
0Success.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

int tc_software_trigger ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Performs a software trigger: the counter is reset and the clock is started.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
0Success.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

int tc_start ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Starts a timer/counter.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
0Success.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

Referenced by init_timer_isr().

int tc_stop ( volatile avr32_tc_t *  tc,
unsigned int  channel 
)

Stops a timer/counter.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
Return values
0Success.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

void tc_sync_start ( volatile avr32_tc_t *  tc)

Start all TC channels simultaneously.

Parameters
tcPointer to the TC instance to access.

References TC_NUMBER_OF_CHANNELS.

void tc_sync_trigger ( volatile avr32_tc_t *  tc)

Asserts a SYNC signal to generate a software trigger and reset all channels.

Parameters
tcPointer to the TC instance to access.
int tc_write_ra ( volatile avr32_tc_t *  tc,
unsigned int  channel,
unsigned short  value 
)

Writes a value to the channel's RA register.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
valueValue to write to the RA register.
Return values
>=0Written value.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, TC_NUMBER_OF_CHANNELS, Tst_bits, and Wr_bitfield.

int tc_write_rb ( volatile avr32_tc_t *  tc,
unsigned int  channel,
unsigned short  value 
)

Writes a value to the channel's RB register.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
valueValue to write to the RB register.
Return values
>=0Written value.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, TC_NUMBER_OF_CHANNELS, Tst_bits, and Wr_bitfield.

int tc_write_rc ( volatile avr32_tc_t *  tc,
unsigned int  channel,
unsigned short  value 
)

Writes a value to the channel's RC register.

Parameters
tcPointer to the TC instance to access.
channelThe TC instance channel to access.
valueValue to write to the RC register.
Return values
>=0Written value.
TC_INVALID_ARGUMENTInvalid argument(s).

References TC_INVALID_ARGUMENT, TC_NUMBER_OF_CHANNELS, Tst_bits, and Wr_bitfield.

Referenced by init_timer_isr().