24.8.3.1.3 Workflow

  1. Create a module software instance structure for the TC module to store the TC driver state while it is in use.
    struct tc_module tc_instance;
    
    Note: This should never go out of scope as long as the module is in use. In most cases, this should be global.
  2. Configure the TC module.
    1. Create a TC module configuration struct, which can be filled out to adjust the configuration of a physical TC peripheral.
      struct tc_config config_tc;
      
    2. Initialize the TC configuration struct with the module's default values.
      tc_get_config_defaults(&config_tc);
      
      Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    3. Alter the TC settings to configure the GCLK source, prescaler, period, and compare channel values.
      config_tc.counter_size = TC_COUNTER_SIZE_8BIT;
      config_tc.clock_source = GCLK_GENERATOR_1;
      config_tc.clock_prescaler = TC_CLOCK_PRESCALER_DIV1024;
      config_tc.counter_8_bit.period = 100;
      config_tc.counter_8_bit.compare_capture_channel[0] = 50;
      config_tc.counter_8_bit.compare_capture_channel[1] = 54;
      
    4. Configure the TC module with the desired settings.
      tc_init(&tc_instance, CONF_TC_MODULE, &config_tc);
      
    5. Enable the TC module to start the timer.
      tc_enable(&tc_instance);
      
  3. Configure the TC callbacks.
    1. Register the Overflow and Compare Channel Match callback functions with the driver.
      tc_register_callback(&tc_instance, tc_callback_to_toggle_led,
              TC_CALLBACK_OVERFLOW);
      tc_register_callback(&tc_instance, tc_callback_to_toggle_led,
              TC_CALLBACK_CC_CHANNEL0);
      tc_register_callback(&tc_instance, tc_callback_to_toggle_led,
              TC_CALLBACK_CC_CHANNEL1);
      
    2. Enable the Overflow and Compare Channel Match callbacks so that it will be called by the driver when appropriate.
      tc_enable_callback(&tc_instance, TC_CALLBACK_OVERFLOW);
      tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
      tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL1);