SAM4SD32 (SAM4S-EK2)
Loading...
Searching...
No Matches
Quick Start Guide for the TC driver

This is the quick start guide for the SAM3/4S/4L/4E/4N/4CM/4C/G Timer Counter (TC) Driver, with step-by-step instructions on how to configure and use the driver for a specific use case.

The code examples can be copied into the main application loop or any other function that will need to control the AST module.

Use Cases

TC Capture Mode Basic Usage

This use case will demonstrate how to initialize the TC module to operate in capture mode using interrupts. Note, that the macros used to select the TC channel are device specific. Refer to the appropriate device-specific datasheet for more information.

Setup Steps

Prerequisites

This module requires the following services:

Setup Code

Add these macros to the top of your main application C-file:

  • ID_TC_CAPTURE (target TC peripheral ID)
  • TC_CHANNEL_CAPTURE (capture channel index)
  • TC_Handler mapping for the selected capture channel IRQ

Add this macro and functions to your main application C-file:

#define TC_CAPTURE_TIMER_SELECTION TC_CMR_TCCLKS_TIMER_CLOCK3
static void tc_capture_initialize(void)
{
/* Configure the PMC to enable the TC module */
sysclk_enable_peripheral_clock(ID_TC_CAPTURE);
#if SAMG55
/* Enable PCK output */
pmc_disable_pck(PMC_PCK_3);
pmc_switch_pck_to_mck(PMC_PCK_3, PMC_PCK_PRES(0));
pmc_enable_pck(PMC_PCK_3);
#endif
/* Init TC to capture mode. */
tc_init(TC, TC_CHANNEL_CAPTURE,
TC_CAPTURE_TIMER_SELECTION /* Clock Selection */
| TC_CMR_LDRA_RISING /* RA Loading: rising edge of TIOA */
| TC_CMR_LDRB_FALLING /* RB Loading: falling edge of TIOA */
| TC_CMR_ABETRG /* External Trigger: TIOA */
| TC_CMR_ETRGEDG_FALLING /* External Trigger Edge: Falling edge */
);
}
void TC_Handler(void)
{
if ((tc_get_status(TC, TC_CHANNEL_CAPTURE) & TC_SR_LDRBS) == TC_SR_LDRBS) {
gs_ul_captured_pulses++;
gs_ul_captured_ra = tc_read_ra(TC, TC_CHANNEL_CAPTURE);
gs_ul_captured_rb = tc_read_rb(TC, TC_CHANNEL_CAPTURE);
}
}

Workflow

  1. Enable the TC module's capture pin:
    ioport_set_pin_mode(PIN_TC_CAPTURE, PIN_TC_CAPTURE_MUX);
    ioport_disable_pin(PIN_TC_CAPTURE);
  2. Initialize the capture channel to the following:
    • Load RA on the rising edge of TIOA
    • Load RB on the falling edge of TIOA
    • Set the external trigger to TIOA
    • Set the external trigger to falling edge
      tc_capture_initialize();
  3. Enable the TC interrupt using NVIC:
    NVIC_DisableIRQ(TC_IRQn);
    NVIC_ClearPendingIRQ(TC_IRQn);
    NVIC_SetPriority(TC_IRQn, 0);
    NVIC_EnableIRQ(TC_IRQn);
  4. Enable the capture channel interrupt:
    tc_enable_interrupt(TC, TC_CHANNEL_CAPTURE, TC_IER_LDRBS);
  5. In the TC_Handler() function, the load. RB interrupt can be checked by:
    if ((tc_get_status(TC, TC_CHANNEL_CAPTURE) & TC_SR_LDRBS) == TC_SR_LDRBS) {
  6. In the TC_Handler() function, the RA value. can be read by:
    uint32_t gs_ul_captured_ra;
    gs_ul_captured_ra = tc_read_ra(TC, TC_CHANNEL_CAPTURE);
  7. In the TC_Handler() function, the RB value. can be read by:
    uint32_t gs_ul_captured_rb;
    gs_ul_captured_rb = tc_read_rb(TC, TC_CHANNEL_CAPTURE);

TC Waveform Mode Basic Usage

This use case will demonstrate how to initialize the TC module to operate in waveform mode. Note, that the macros used to select the TC channel are device specific. Refer to the appropriate device-specific datasheet for more information.

Setup Steps

Prerequisites

This module requires the following services:

Setup Code

Add these macros to the top of your main application C-file:

  • ID_TC_WAVEFORM (target TC peripheral ID)
  • TC_CHANNEL_WAVEFORM (waveform channel index)

Add these macros and function to your main application C-file:

#define TC_WAVEFORM_TIMER_SELECTION TC_CMR_TCCLKS_TIMER_CLOCK4
#define TC_WAVEFORM_DIVISOR 128
#define TC_WAVEFORM_FREQUENCY 178
#define TC_WAVEFORM_DUTY_CYCLE 30
static void tc_waveform_initialize(void)
{
uint32_t ra, rc;
// Configure the PMC to enable the TC module.
sysclk_enable_peripheral_clock(ID_TC_WAVEFORM);
// Init TC to waveform mode.
tc_init(TC, TC_CHANNEL_WAVEFORM,
TC_WAVEFORM_TIMER_SELECTION // Waveform Clock Selection
| TC_CMR_WAVE // Waveform mode is enabled
| TC_CMR_ACPA_SET // RA Compare Effect: set
| TC_CMR_ACPC_CLEAR // RC Compare Effect: clear
| TC_CMR_CPCTRG // UP mode with automatic trigger on RC Compare
);
// Configure waveform frequency and duty cycle.
rc = (sysclk_get_peripheral_bus_hz(TC) /
TC_WAVEFORM_DIVISOR /
TC_WAVEFORM_FREQUENCY;
tc_write_rc(TC, TC_CHANNEL_WAVEFORM, rc);
ra = (100 - TC_WAVEFORM_FREQUENCY_DUTY_CYCLE * rc / 100;
tc_write_ra(TC, TC_CHANNEL_WAVEFORM, ra);
// Enable TC TC_CHANNEL_WAVEFORM.
tc_start(TC, TC_CHANNEL_WAVEFORM);
}
#define TC_CMR_CPCTRG
(TC_CMR) RC Compare Trigger Enable
#define TC_CMR_ACPA_SET
(TC_CMR) Set
#define TC_CMR_WAVE
(TC_CMR) Waveform Mode
#define TC_CMR_ACPC_CLEAR
(TC_CMR) Clear
void tc_init(Tc *p_tc, uint32_t ul_Channel, uint32_t ul_Mode)
Configure TC for timer, waveform generation, or capture.
Definition tc.c:70
void tc_write_rc(Tc *p_tc, uint32_t ul_channel, uint32_t ul_value)
Write to TC Register C (RC) on the selected channel.
Definition tc.c:326
void tc_start(Tc *p_tc, uint32_t ul_channel)
Start the TC clock on the specified channel.
Definition tc.c:169
void tc_write_ra(Tc *p_tc, uint32_t ul_channel, uint32_t ul_value)
Write to TC Register A (RA) on the specified channel.
Definition tc.c:286

Workflow

  1. Enable the TC module's waveform pin:
    ioport_set_pin_mode(PIN_TC_WAVEFORM, PIN_TC_WAVEFORM_MUX);
    ioport_disable_pin(PIN_TC_WAVEFORM);
  2. Initialize the waveform channel to the following:
    • Output frequency of 178Hz, with a duty-cycle of 30%
    • Use TC_CMR_TCCLKS_TIMER_CLOCK4, with a divisor of 128
      tc_waveform_initialize();