tc.c File Reference


Detailed Description

TC driver for AVR32 UC3.

AVR32 Timer/Counter driver module.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file tc.c.

#include <avr32/io.h>
#include "compiler.h"
#include "tc.h"

Go to the source code of this file.

Functions

int tc_configure_interrupts (volatile avr32_tc_t *tc, unsigned int channel, const tc_interrupt_t *bitfield)
 Enables various timer/counter interrupts.
int tc_get_interrupt_settings (volatile avr32_tc_t *tc, unsigned int channel)
 Reads timer/counter interrupt settings.
int tc_init_capture (volatile avr32_tc_t *tc, const tc_capture_opt_t *opt)
 Sets options for timer/counter capture initialization.
int tc_init_waveform (volatile avr32_tc_t *tc, const tc_waveform_opt_t *opt)
 Sets options for timer/counter waveform initialization.
int tc_read_ra (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the channel's RA register and returns the value.
int tc_read_rb (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the channel's RB register and returns the value.
int tc_read_rc (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the channel's RC register and returns the value.
int tc_read_sr (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the status register.
int tc_read_tc (volatile avr32_tc_t *tc, unsigned int channel)
 Reads the channel's TC counter and returns the value.
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.
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.
int tc_start (volatile avr32_tc_t *tc, unsigned int channel)
 Starts a timer/counter.
int tc_stop (volatile avr32_tc_t *tc, unsigned int channel)
 Stops a timer/counter.
void tc_sync_start (volatile avr32_tc_t *tc)
 Start all TC channels simultaneously.
void tc_sync_trigger (volatile avr32_tc_t *tc)
 Asserts a SYNC signal to generate a software trigger and reset all channels.
int tc_write_ra (volatile avr32_tc_t *tc, unsigned int channel, unsigned short value)
 Writes a value to the channel's RA register.
int tc_write_rb (volatile avr32_tc_t *tc, unsigned int channel, unsigned short value)
 Writes a value to the channel's RB register.
int tc_write_rc (volatile avr32_tc_t *tc, unsigned int channel, unsigned short value)
 Writes a value to the channel's RC register.


Function Documentation

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

Enables various timer/counter interrupts.

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
bitfield The interrupt enable configuration.
Return values:
0 Success.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 63 of file tc.c.

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

Referenced by main().

00064 {
00065   Bool global_interrupt_enabled = Is_global_interrupt_enabled();
00066 
00067   // Check for valid input.
00068   if (channel >= TC_NUMBER_OF_CHANNELS)
00069     return TC_INVALID_ARGUMENT;
00070 
00071   // Enable the appropriate interrupts.
00072   tc->channel[channel].ier = bitfield->etrgs << AVR32_TC_ETRGS_OFFSET |
00073                              bitfield->ldrbs << AVR32_TC_LDRBS_OFFSET |
00074                              bitfield->ldras << AVR32_TC_LDRAS_OFFSET |
00075                              bitfield->cpcs << AVR32_TC_CPCS_OFFSET |
00076                              bitfield->cpbs << AVR32_TC_CPBS_OFFSET |
00077                              bitfield->cpas << AVR32_TC_CPAS_OFFSET |
00078                              bitfield->lovrs << AVR32_TC_LOVRS_OFFSET |
00079                              bitfield->covfs << AVR32_TC_COVFS_OFFSET;
00080 
00081   // Disable the appropriate interrupts.
00082   if (global_interrupt_enabled) Disable_global_interrupt();
00083   tc->channel[channel].idr = (~bitfield->etrgs & 1) << AVR32_TC_ETRGS_OFFSET |
00084                              (~bitfield->ldrbs & 1) << AVR32_TC_LDRBS_OFFSET |
00085                              (~bitfield->ldras & 1) << AVR32_TC_LDRAS_OFFSET |
00086                              (~bitfield->cpcs & 1) << AVR32_TC_CPCS_OFFSET |
00087                              (~bitfield->cpbs & 1) << AVR32_TC_CPBS_OFFSET |
00088                              (~bitfield->cpas & 1) << AVR32_TC_CPAS_OFFSET |
00089                              (~bitfield->lovrs & 1) << AVR32_TC_LOVRS_OFFSET |
00090                              (~bitfield->covfs & 1) << AVR32_TC_COVFS_OFFSET;
00091   tc->channel[channel].sr;
00092   if (global_interrupt_enabled) Enable_global_interrupt();
00093 
00094   return 0;
00095 }

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

Reads timer/counter interrupt settings.

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
>=0 The interrupt enable configuration organized according to tc_interrupt_t.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 53 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00054 {
00055   // Check for valid input.
00056   if (channel >= TC_NUMBER_OF_CHANNELS)
00057     return TC_INVALID_ARGUMENT;
00058 
00059   return tc->channel[channel].imr;
00060 }

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

Sets options for timer/counter capture initialization.

Parameters:
tc Pointer to the TC instance to access.
opt Options for capture mode.
Return values:
0 Success.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 112 of file tc.c.

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.

Referenced by init_tc_input().

00113 {
00114   // Check for valid input.
00115   if (opt->channel >= TC_NUMBER_OF_CHANNELS)
00116     return TC_INVALID_ARGUMENT;
00117 
00118   // MEASURE SIGNALS: Capture operating mode.
00119   tc->channel[opt->channel].cmr = opt->ldrb << AVR32_TC_LDRB_OFFSET |
00120                                   opt->ldra << AVR32_TC_LDRA_OFFSET |
00121                                   0 << AVR32_TC_WAVE_OFFSET |
00122                                   opt->cpctrg << AVR32_TC_CPCTRG_OFFSET |
00123                                   opt->abetrg << AVR32_TC_ABETRG_OFFSET |
00124                                   opt->etrgedg << AVR32_TC_ETRGEDG_OFFSET|
00125                                   opt->ldbdis << AVR32_TC_LDBDIS_OFFSET |
00126                                   opt->ldbstop << AVR32_TC_LDBSTOP_OFFSET |
00127                                   opt->burst << AVR32_TC_BURST_OFFSET |
00128                                   opt->clki << AVR32_TC_CLKI_OFFSET |
00129                                   opt->tcclks << AVR32_TC_TCCLKS_OFFSET;
00130 
00131   return 0;
00132 }

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

Sets options for timer/counter waveform initialization.

Parameters:
tc Pointer to the TC instance to access.
opt Options for waveform generation.
Return values:
0 Success.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 135 of file tc.c.

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.

00136 {
00137   // Check for valid input.
00138   if (opt->channel >= TC_NUMBER_OF_CHANNELS)
00139     return TC_INVALID_ARGUMENT;
00140 
00141   // GENERATE SIGNALS: Waveform operating mode.
00142   tc->channel[opt->channel].cmr = opt->bswtrg << AVR32_TC_BSWTRG_OFFSET |
00143                                   opt->beevt << AVR32_TC_BEEVT_OFFSET |
00144                                   opt->bcpc << AVR32_TC_BCPC_OFFSET |
00145                                   opt->bcpb << AVR32_TC_BCPB_OFFSET |
00146                                   opt->aswtrg << AVR32_TC_ASWTRG_OFFSET |
00147                                   opt->aeevt << AVR32_TC_AEEVT_OFFSET |
00148                                   opt->acpc << AVR32_TC_ACPC_OFFSET |
00149                                   opt->acpa << AVR32_TC_ACPA_OFFSET |
00150                                   1 << AVR32_TC_WAVE_OFFSET |
00151                                   opt->wavsel << AVR32_TC_WAVSEL_OFFSET |
00152                                   opt->enetrg << AVR32_TC_ENETRG_OFFSET |
00153                                   opt->eevt << AVR32_TC_EEVT_OFFSET |
00154                                   opt->eevtedg << AVR32_TC_EEVTEDG_OFFSET |
00155                                   opt->cpcdis << AVR32_TC_CPCDIS_OFFSET |
00156                                   opt->cpcstop << AVR32_TC_CPCSTOP_OFFSET |
00157                                   opt->burst << AVR32_TC_BURST_OFFSET |
00158                                   opt->clki << AVR32_TC_CLKI_OFFSET |
00159                                   opt->tcclks << AVR32_TC_TCCLKS_OFFSET;
00160 
00161   return 0;
00162 }

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

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

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
>=0 RA register value.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 243 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00244 {
00245   // Check for valid input.
00246   if (channel >= TC_NUMBER_OF_CHANNELS)
00247     return TC_INVALID_ARGUMENT;
00248 
00249   return Rd_bitfield(tc->channel[channel].ra, AVR32_TC_RA_MASK);
00250 }

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

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

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
>=0 RB register value.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 253 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00254 {
00255   // Check for valid input.
00256   if (channel >= TC_NUMBER_OF_CHANNELS)
00257     return TC_INVALID_ARGUMENT;
00258 
00259   return Rd_bitfield(tc->channel[channel].rb, AVR32_TC_RB_MASK);
00260 }

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

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

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
>=0 RC register value.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 263 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00264 {
00265   // Check for valid input.
00266   if (channel >= TC_NUMBER_OF_CHANNELS)
00267     return TC_INVALID_ARGUMENT;
00268 
00269   return Rd_bitfield(tc->channel[channel].rc, AVR32_TC_RC_MASK);
00270 }

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

Reads the status register.

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
>=0 Status register value.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 223 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

Referenced by tc_irq_handler().

00224 {
00225   // Check for valid input.
00226   if (channel >= TC_NUMBER_OF_CHANNELS)
00227     return TC_INVALID_ARGUMENT;
00228 
00229   return tc->channel[channel].sr;
00230 }

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

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

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
>=0 TC counter value.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 233 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00234 {
00235   // Check for valid input.
00236   if (channel >= TC_NUMBER_OF_CHANNELS)
00237     return TC_INVALID_ARGUMENT;
00238 
00239   return Rd_bitfield(tc->channel[channel].cv, AVR32_TC_CV_MASK);
00240 }

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:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
ext_clk_sig_src External 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:
0 Success.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 98 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00099 {
00100   // Check for valid input.
00101   if (channel >= TC_NUMBER_OF_CHANNELS || ext_clk_sig_src >= 1 << AVR32_TC_BMR_TC0XC0S_SIZE)
00102     return TC_INVALID_ARGUMENT;
00103 
00104   // Clear bit-field and set the correct behavior.
00105   tc->bmr = (tc->bmr & ~(AVR32_TC_BMR_TC0XC0S_MASK << (channel * AVR32_TC_BMR_TC0XC0S_SIZE))) |
00106             (ext_clk_sig_src << (channel * AVR32_TC_BMR_TC0XC0S_SIZE));
00107 
00108   return 0;
00109 }

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:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
0 Success.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 191 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00192 {
00193   // Check for valid input.
00194   if (channel >= TC_NUMBER_OF_CHANNELS)
00195     return TC_INVALID_ARGUMENT;
00196 
00197   // Reset the selected timer/counter channel.
00198   tc->channel[channel].ccr = AVR32_TC_SWTRG_MASK;
00199 
00200   return 0;
00201 }

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

Starts a timer/counter.

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
0 Success.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 165 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

Referenced by main().

00166 {
00167   // Check for valid input.
00168   if (channel >= TC_NUMBER_OF_CHANNELS)
00169     return TC_INVALID_ARGUMENT;
00170 
00171   // Enable, reset and start the selected timer/counter channel.
00172   tc->channel[channel].ccr = AVR32_TC_SWTRG_MASK | AVR32_TC_CLKEN_MASK;
00173 
00174   return 0;
00175 }

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

Stops a timer/counter.

Parameters:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
Return values:
0 Success.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 178 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00179 {
00180   // Check for valid input.
00181   if (channel >= TC_NUMBER_OF_CHANNELS)
00182     return TC_INVALID_ARGUMENT;
00183 
00184   // Disable the selected timer/counter channel.
00185   tc->channel[channel].ccr = AVR32_TC_CLKDIS_MASK;
00186 
00187   return 0;
00188 }

void tc_sync_start ( volatile avr32_tc_t *  tc  ) 

Start all TC channels simultaneously.

Parameters:
tc Pointer to the TC instance to access.

Definition at line 211 of file tc.c.

References TC_NUMBER_OF_CHANNELS.

00212 {
00213   unsigned int i;
00214   // Enable the clock for each channel.
00215   for(i=0; i<TC_NUMBER_OF_CHANNELS;i++)
00216     tc->channel[i].ccr = AVR32_TC_CLKEN_MASK;
00217     
00218   // Reset all channels of the selected timer/counter.
00219   tc->bcr = AVR32_TC_BCR_SYNC_MASK;
00220 }

void tc_sync_trigger ( volatile avr32_tc_t *  tc  ) 

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

Parameters:
tc Pointer to the TC instance to access.

Definition at line 204 of file tc.c.

00205 {
00206   // Reset all channels of the selected timer/counter.
00207   tc->bcr = AVR32_TC_BCR_SYNC_MASK;
00208 }

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:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
value Value to write to the RA register.
Return values:
>=0 Written value.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 273 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00274 {
00275   // Check for valid input.
00276   if (channel >= TC_NUMBER_OF_CHANNELS)
00277     return TC_INVALID_ARGUMENT;
00278 
00279   // This function is only available in WAVEFORM mode.
00280   if (Tst_bits(tc->channel[channel].cmr, AVR32_TC_WAVE_MASK))
00281     Wr_bitfield(tc->channel[channel].ra, AVR32_TC_RA_MASK, value);
00282 
00283   return value;
00284 }

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:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
value Value to write to the RB register.
Return values:
>=0 Written value.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 287 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00288 {
00289   // Check for valid input.
00290   if (channel >= TC_NUMBER_OF_CHANNELS)
00291     return TC_INVALID_ARGUMENT;
00292 
00293   // This function is only available in WAVEFORM mode.
00294   if (Tst_bits(tc->channel[channel].cmr, AVR32_TC_WAVE_MASK))
00295     Wr_bitfield(tc->channel[channel].rb, AVR32_TC_RB_MASK, value);
00296 
00297   return value;
00298 }

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:
tc Pointer to the TC instance to access.
channel The TC instance channel to access.
value Value to write to the RC register.
Return values:
>=0 Written value.
TC_INVALID_ARGUMENT Invalid argument(s).

Definition at line 301 of file tc.c.

References TC_INVALID_ARGUMENT, and TC_NUMBER_OF_CHANNELS.

00302 {
00303   // Check for valid input.
00304   if (channel >= TC_NUMBER_OF_CHANNELS)
00305     return TC_INVALID_ARGUMENT;
00306 
00307   // This function is only available in WAVEFORM mode.
00308   if (Tst_bits(tc->channel[channel].cmr, AVR32_TC_WAVE_MASK))
00309     Wr_bitfield(tc->channel[channel].rc, AVR32_TC_RC_MASK, value);
00310 
00311   return value;
00312 }


Generated on Fri Feb 19 02:26:39 2010 for AVR32 UC3 - TC Driver by  doxygen 1.5.5