Configure TCC

  1. Create a module software instance struct for the TCC module to store the TCC driver state while it is in use.
    struct tcc_module tcc_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. Create a TCC module configuration struct, which can be filled out to adjust the configuration of a physical TCC peripheral.
    struct tcc_config config_tcc;
    
  3. Initialize the TCC configuration struct with the module's default values.
    tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE);
    
    Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
  4. Alter the TCC settings to configure the counter width, wave generation mode and the compare channel 0 value and fault options. Here the Recoverable Fault input is enabled and halt action is set to software mode (must use software to clear halt state).
    config_tcc.counter.period = 0xFFFF;
    config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
    config_tcc.compare.match[CONF_PWM_CHANNEL] = 0xFFFF;
    
    config_tcc.wave_ext.recoverable_fault[CONF_PWM_CHANNEL].source =
            TCC_FAULT_SOURCE_ENABLE;
    config_tcc.wave_ext.recoverable_fault[CONF_PWM_CHANNEL].halt_action =
            TCC_FAULT_HALT_ACTION_SW_HALT;
    
  5. Alter the TCC settings to configure the PWM output on a physical device pin.
    config_tcc.pins.enable_wave_out_pin[CONF_PWM_OUTPUT] = true;
    config_tcc.pins.wave_out_pin[CONF_PWM_OUTPUT]        = CONF_PWM_OUT_PIN;
    config_tcc.pins.wave_out_pin_mux[CONF_PWM_OUTPUT]    = CONF_PWM_OUT_MUX;
    
  6. Configure the TCC module with the desired settings.
    tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc);
    
  7. Create a TCC events configuration struct, which can be filled out to enable/disable events and configure event settings. Reset all fields to zero.
    struct tcc_events events;
    memset(&events, 0, sizeof(struct tcc_events));
    
  8. Alter the TCC events settings to enable/disable desired events, to change event generating options and modify event actions. Here channel event 0 input is enabled as source of recoverable fault.
    events.on_event_perform_channel_action[CONF_PWM_CHANNEL] = true;
    
  9. Enable and apply events settings.
    tcc_enable_events(&tcc_instance, &events);
    
  10. Enable the TCC module to start the timer and begin PWM signal generation.
    tcc_enable(&tcc_instance);
    
  11. Register the Compare Channel 0 Match callback functions with the driver.
    tcc_register_callback(
            &tcc_instance,
            tcc_callback_to_change_duty_cycle,
            (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL));
    
  12. Enable the Compare Channel 0 Match callback so that it will be called by the driver when appropriate.
    tcc_enable_callback(&tcc_instance,
            (enum tcc_callback)(TCC_CALLBACK_CHANNEL_0 + CONF_PWM_CHANNEL));