gossamer 0.0.1
a very lightweight app framework for SAMD and SAML chips
Loading...
Searching...
No Matches
Timer/Counter for Control Applications

Functions for configuring and using the TCC peripheral, a more advanced timer/counter. More...

Typedefs

typedef void(* tcc_cb_t) (tcc_interrupt_t intflag)
 Callback type for TCC interrupts. The callback receives a bitmask of the interrupt flags that triggered the interrupt. Check against TCC_INTERRUPT_OVF, TCC_INTERRUPT_MC0 through TCC_INTERRUPT_MC3.
 

Enumerations

enum  tcc_prescaler_value_t {
  TCC_PRESCALER_DIV1 = 0 , TCC_PRESCALER_DIV2 = 1 , TCC_PRESCALER_DIV4 = 2 , TCC_PRESCALER_DIV8 = 3 ,
  TCC_PRESCALER_DIV16 = 4 , TCC_PRESCALER_DIV64 = 5 , TCC_PRESCALER_DIV256 = 6 , TCC_PRESCALER_DIV1024 = 7
}
 
enum  tcc_wavegen_t { TCC_WAVEGEN_NORMAL_FREQUENCY = 0 , TCC_WAVEGEN_MATCH_FREQUENCY = 1 , TCC_WAVEGEN_NORMAL_PWM = 2 }
 
enum  tcc_output_matrix_t { TCC_OUTPUT_MATRIX_MODULO_4 = 0 , TCC_OUTPUT_MATRIX_MODULO_2 = 1 , TCC_OUTPUT_MATRIX_ALL_CC0 = 2 , TCC_OUTPUT_MATRIX_WO0_CC0_OTHERS_CC1 = 3 }
 
enum  tcc_channel_polarity_t { TCC_CHANNEL_POLARITY_NORMAL = 0 , TCC_CHANNEL_POLARITY_INVERTED = 1 }
 
enum  tcc_event_action_t {
  TCC_EVENT_ACTION_OFF = 0 , TCC_EVENT_ACTION_RETRIGGER = 1 , TCC_EVENT_ACTION_COUNT_EVENT = 2 , TCC_EVENT_ACTION_START = 3 ,
  TCC_EVENT_ACTION_INC = 4 , TCC_EVENT_ACTION_COUNT = 5 , TCC_EVENT_ACTION_FAULT = 7
}
 
enum  tcc_interrupt_t {
  TCC_INTERRUPT_OVF = (1 << 0) , TCC_INTERRUPT_MC0 = (1 << 16) , TCC_INTERRUPT_MC1 = (1 << 17) , TCC_INTERRUPT_MC2 = (1 << 18) ,
  TCC_INTERRUPT_MC3 = (1 << 19)
}
 

Functions

bool tcc_init (uint8_t instance, generic_clock_generator_t clocksource, tcc_prescaler_value_t prescaler)
 Enables the peripheral clock for the TCC and clocks it with the selected clock source. Also resets the TCC to its starting configuration.
 
void tcc_set_run_in_standby (uint8_t instance, bool runStandby)
 Sets whether the TCC should run in standby mode.
 
void tcc_set_wavegen (uint8_t instance, tcc_wavegen_t mode)
 Sets the waveform generation mode for the TCC.
 
void tcc_set_output_matrix (uint8_t instance, tcc_output_matrix_t mode)
 Sets the output matrix for the TCC.
 
void tcc_set_channel_polarity (uint8_t instance, uint8_t channel, tcc_channel_polarity_t polarity)
 Sets the polarity of a channel.
 
void tcc_enable (uint8_t instance)
 Enables the TCC. Make sure to call tcc_init first to set it up.
 
bool tcc_is_enabled (uint8_t instance)
 Checks whether the TCC is enabled.
 
void tcc_set_period (uint8_t instance, uint32_t period, bool buffered)
 Sets the period of the TCC.
 
uint32_t tcc_get_period (uint8_t instance)
 Gets the period of the TCC.
 
void tcc_set_cc (uint8_t instance, uint8_t channel, uint32_t value, bool buffered)
 Sets the value of a compare channel.
 
void tcc_set_count (uint8_t instance, uint32_t value)
 Sets the value of the counter.
 
uint32_t tcc_get_count (uint8_t instance)
 Gets the current value of the counter.
 
void tcc_stop (uint8_t instance)
 Issues a STOP command to the TCC.
 
void tcc_retrigger (uint8_t instance)
 Issues a RETRIGGER command to the TCC.
 
void tcc_update (uint8_t instance)
 Issues an UPDATE command to the TCC.
 
void tcc_disable (uint8_t instance)
 Disables the TCC, but retains all its settings.
 
void tcc_enable_overflow_event_output (uint8_t instance, bool enabled)
 Enables or disables overflow event output for the TCC.
 
void tcc_set_event_action (uint8_t instance, uint8_t event_index, tcc_event_action_t action)
 Sets the action taken when the TCC receives an event.
 
void tcc_enable_match_event_input (uint8_t instance, uint8_t channel, bool enabled)
 Enables or disables match/capture event input for a TCC channel.
 
void tcc_enable_match_event_output (uint8_t instance, uint8_t channel, bool enabled)
 Enables or disables match/capture event output for a TCC channel.
 
void tcc_configure_callback (uint8_t instance, tcc_cb_t callback)
 Registers a callback for TCC interrupts.
 
void tcc_enable_interrupt (uint8_t instance, tcc_interrupt_t mask)
 Enables TCC interrupts for the specified sources.
 
void tcc_disable_interrupt (uint8_t instance, tcc_interrupt_t mask)
 Disables TCC interrupts for the specified sources.
 
void tcc_irq_handler (uint8_t instance)
 TCC interrupt handler. Call this from your irq_handler_tccN() override.
 

Detailed Description

Functions for configuring and using the TCC peripheral, a more advanced timer/counter.

Note
SAM D51 erratum DS80000748F ยง2.21.1: TCC event inputs are not compatible with EVSYS channels in synchronous or resynchronized mode on the D51. Use an asynchronous channel path for any event routed to a TCC.

Function Documentation

◆ tcc_configure_callback()

void tcc_configure_callback ( uint8_t instance,
tcc_cb_t callback )

Registers a callback for TCC interrupts.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet.
callbackThe function to call when an enabled TCC interrupt fires, or NULL to unregister.

◆ tcc_disable()

void tcc_disable ( uint8_t instance)

Disables the TCC, but retains all its settings.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.

◆ tcc_disable_interrupt()

void tcc_disable_interrupt ( uint8_t instance,
tcc_interrupt_t mask )

Disables TCC interrupts for the specified sources.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet.
maskBitmask of interrupt sources to disable.

◆ tcc_enable()

void tcc_enable ( uint8_t instance)

Enables the TCC. Make sure to call tcc_init first to set it up.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.

◆ tcc_enable_interrupt()

void tcc_enable_interrupt ( uint8_t instance,
tcc_interrupt_t mask )

Enables TCC interrupts for the specified sources.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet.
maskBitmask of interrupt sources to enable. Combine with bitwise OR: TCC_INTERRUPT_OVF, TCC_INTERRUPT_MC0 through TCC_INTERRUPT_MC3.
Note
You must also override the weak irq_handler_tccN() symbol in your application to call tcc_irq_handler(N). For example:
void irq_handler_tcc0(void) { tcc_irq_handler(0); }
void tcc_irq_handler(uint8_t instance)
TCC interrupt handler. Call this from your irq_handler_tccN() override.
Definition tcc.c:196

◆ tcc_enable_match_event_input()

void tcc_enable_match_event_input ( uint8_t instance,
uint8_t channel,
bool enabled )

Enables or disables match/capture event input for a TCC channel.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet.
channelThe match/capture channel.
enabledtrue to enable, false to disable.
Note
Must be called before tcc_enable() (EVCTRL is enable-protected).

◆ tcc_enable_match_event_output()

void tcc_enable_match_event_output ( uint8_t instance,
uint8_t channel,
bool enabled )

Enables or disables match/capture event output for a TCC channel.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet.
channelThe match/capture channel.
enabledtrue to enable, false to disable.
Note
Must be called before tcc_enable() (EVCTRL is enable-protected).

◆ tcc_enable_overflow_event_output()

void tcc_enable_overflow_event_output ( uint8_t instance,
bool enabled )

Enables or disables overflow event output for the TCC.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet.
enabledtrue to enable overflow event output, false to disable.
Note
Must be called before tcc_enable() (EVCTRL is enable-protected).

◆ tcc_get_count()

uint32_t tcc_get_count ( uint8_t instance)

Gets the current value of the counter.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
Returns
The current value of the counter.

◆ tcc_get_period()

uint32_t tcc_get_period ( uint8_t instance)

Gets the period of the TCC.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
Returns
The current period of value.

◆ tcc_init()

bool tcc_init ( uint8_t instance,
generic_clock_generator_t clocksource,
tcc_prescaler_value_t prescaler )

Enables the peripheral clock for the TCC and clocks it with the selected clock source. Also resets the TCC to its starting configuration.

This just sets up the TCC in its reset state. You are still responsible for configuring it with the options you need for waveform output.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
clocksourceThe clock source to use for the TCC.
prescalerThe prescaler factor, which divides the clock source by a factor:
  • TCC_PRESCALER_DIV1 - clock source unchanged
  • TCC_PRESCALER_DIV2 - clock source divided by 2
  • TCC_PRESCALER_DIV4 - clock source divided by 4
  • TCC_PRESCALER_DIV8 - clock source divided by 8
  • TCC_PRESCALER_DIV16 - clock source divided by 16
  • TCC_PRESCALER_DIV64 - clock source divided by 64
  • TCC_PRESCALER_DIV256 - clock source divided by 256
  • TCC_PRESCALER_DIV102 - clock source divided by 1024
Returns
true if the TCC was set up, false if instance was out of range (i.e. you asked for TCC2, but the microcontroller only has one TCC).
Note
if this function returns false, you shouldn't interact with this TCC instance with any other functions; they don't do the bounds check that this does.

◆ tcc_irq_handler()

void tcc_irq_handler ( uint8_t instance)

TCC interrupt handler. Call this from your irq_handler_tccN() override.

Parameters
instanceThe TCC peripheral instance number.

◆ tcc_is_enabled()

bool tcc_is_enabled ( uint8_t instance)

Checks whether the TCC is enabled.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
Returns
true if the TCC is enabled, false if instance is disabled.

◆ tcc_retrigger()

void tcc_retrigger ( uint8_t instance)

Issues a RETRIGGER command to the TCC.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.

◆ tcc_set_cc()

void tcc_set_cc ( uint8_t instance,
uint8_t channel,
uint32_t value,
bool buffered )

Sets the value of a compare channel.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
channelThe compare channel to set, from 0 to 3 (or 0-1 on some TCCs; check the data sheet)
bufferedIf true, sets the CC value in the buffered register, which will be loaded at the next UPDATE condition. If false, sets the period directly. Note that double buffering is not available on the SAM D11 and SAM D21, and a value of true on those platforms will be ignored.
valueThe value to set the compare channel to. Max 24 bits (16,777,215).

◆ tcc_set_channel_polarity()

void tcc_set_channel_polarity ( uint8_t instance,
uint8_t channel,
tcc_channel_polarity_t polarity )

Sets the polarity of a channel.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
channelThe waveform output channel from 0 to 7.
polarityThe polarity to set. One of:
  • TCC_CHANNEL_POLARITY_NORMAL - The output is high when WO[n] is set.
  • TCC_CHANNEL_POLARITY_INVERTED - The output is low when WO[n] is set.

Inverting one of the waveform outputs is useful to drive a device with a differential signal; inverting several outputs can be useful if you are driving something whose polarity is reversed, like a common anode LED.

◆ tcc_set_count()

void tcc_set_count ( uint8_t instance,
uint32_t value )

Sets the value of the counter.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
valueThe value to set the counter to. Max 24 bits (16,777,215).

◆ tcc_set_event_action()

void tcc_set_event_action ( uint8_t instance,
uint8_t event_index,
tcc_event_action_t action )

Sets the action taken when the TCC receives an event.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet.
event_indexWhich event input to configure (0 or 1). Each TCC has two independent event inputs.
actionThe action to take on the event.
See also
tcc_event_action_t
Note
Must be called before tcc_enable() (EVCTRL is enable-protected).

◆ tcc_set_output_matrix()

void tcc_set_output_matrix ( uint8_t instance,
tcc_output_matrix_t mode )

Sets the output matrix for the TCC.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
modeThe output matrix mode to use. One of:
  • TCC_OUTPUT_MATRIX_MODULO_4 - Compare channels CC0-CC3 are distributed across waveform outputs WO[0]-WO[7] in a modulo 4 fashion, such that CC0 controls WO[0] and WO[4], CC1 controls WO[1] and WO[5], CC2 controls WO[2] and WO[6], and CC3 controls WO[3] and WO[7].
  • TCC_OUTPUT_MATRIX_MODULO_2 - Compare channels CC0-CC1 are distributed across waveform outputs WO[0]-WO[7] in a modulo 2 fashion, such that CC0 controls WO0, WO2, WO4, and WO6, and CC1 controls WO1, WO3, WO5, and WO7.
  • TCC_OUTPUT_MATRIX_ALL_CC0 - Compare channel CC0 controls all waveform outputs.
  • TCC_OUTPUT_MATRIX_WO0_CC0_OTHERS_CC1 - Compare channel CC0 controls waveform output WO0, and compare channel CC1 controls all other waveform outputs.

◆ tcc_set_period()

void tcc_set_period ( uint8_t instance,
uint32_t period,
bool buffered )

Sets the period of the TCC.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
periodThe period to set.
bufferedIf true, sets the period in the buffered register, which will be loaded at the next UPDATE condition. If false, sets the period directly. Note that double buffering is not available on the SAM D11 and SAM D21, and a value of true on those platforms will be ignored.

The period is the value that the counter counts up to before resetting. The frequency of the timer is determined by the period and the prescaler value.

◆ tcc_set_run_in_standby()

void tcc_set_run_in_standby ( uint8_t instance,
bool runStandby )

Sets whether the TCC should run in standby mode.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
runStandbytrue if the TCC should run in standby mode, false otherwise.

◆ tcc_set_wavegen()

void tcc_set_wavegen ( uint8_t instance,
tcc_wavegen_t mode )

Sets the waveform generation mode for the TCC.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.
modeThe waveform generation mode to use. One of:
  • TCC_WAVEGEN_NORMAL_FREQUENCY - Normal frequency generation. The TOP value is PER, and waveform output WO[n] toggles on a match with a compare channel (n % 4 by default;
See also
tcc_set_output_matrix for other options). This mode results in a square wave with a frequency dependent on the PER register, and a duty cycle that depends on the CCn register.
  • TCC_WAVEGEN_MATCH_FREQUENCY - Match frequency generation. The TOP value is CC0. WO[0] toggles on a match with TOP / CC0 and the counter resets. This results in a square wave with a 50% duty cycle whose frequency depends on CC0.
  • TCC_WAVEGEN_NORMAL_PWM - Normal pulse-width modulation. The TOP value is PER. Waveform output WO[n] is set at the start of the period and cleared on a match with a compare channel (n % 4 by default;
tcc_set_output_matrix for other options). This mode results in a square wave with a frequency defined by the PER register, and a duty cycle defined by the CCn register.

◆ tcc_stop()

void tcc_stop ( uint8_t instance)

Issues a STOP command to the TCC.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.

When a stop is detected while the counter is running, the counter will maintain its current value, and the Stop bit in the Status register will be set (STATUS.STOP).

Note
There is a more complex way to set what happens to the waveform outputs in a stop condition: you can set values for each waveform in the DRVCTRL's Non-Recoverable State Output Enable (DRVCTRL.NREx) and Non- Recoverable State Output Value (DRVCTRL.NRVx) bits. This is not currently implemented; see the data sheet for more information.

◆ tcc_update()

void tcc_update ( uint8_t instance)

Issues an UPDATE command to the TCC.

Parameters
instanceThe TCC peripheral instance as numbered in the data sheet, or 0.