SAMC21 Xplained External Interrupt with the Event System

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to run AT05567: TC Capture on External Interrupt with the Event System on SAM D20

on SAM C21, and have made some changes to make it work with the C21, but I am unable to make it work. Can someone please guide me on what I am doing wrong

#include <asf.h>

/* TC module struct */
struct tc_module tc0_module;
/* TC config struct */
struct tc_config tc0_config;

struct events_resource extint_event;

void tc_configuration(void);
void extint_configuration(void);
void evsys_configuration(void);
void tc_compare_callback_function(struct tc_module *const module_inst);
void tc_capture_callback_function(struct tc_module *const module_inst);
void configure_extint_channel(void);

void configure_extint_channel(void){
	struct extint_chan_conf config_extint_chan;
	extint_chan_get_config_defaults(&config_extint_chan);
	config_extint_chan.gpio_pin = BUTTON_0_EIC_PIN;
	config_extint_chan.gpio_pin_mux = BUTTON_0_EIC_MUX;
	config_extint_chan.gpio_pin_pull = EXTINT_PULL_UP;
	config_extint_chan.detection_criteria = EXTINT_DETECT_BOTH;
	extint_chan_set_config(BUTTON_0_EIC_LINE, &config_extint_chan);
}

/* Configure 16-bit TC0 module for capture */
void tc_configuration(void)
{
    tc_reset(&tc0_module);
    tc_get_config_defaults(&tc0_config);
	tc0_config.clock_source = GCLK_GENERATOR_1;
    tc0_config.clock_prescaler              = TC_CLOCK_PRESCALER_DIV1024;
    tc0_config.enable_capture_on_channel[0] = true;
    tc_init(&tc0_module, TC0, &tc0_config);
    struct tc_events tc_event = {
        .generate_event_on_compare_channel[0] =	true,
        .generate_event_on_compare_channel[1] =	true,
        .on_event_perform_action = true,
        .invert_event_input = true,
        .event_action = TC_EVENT_ACTION_PPW
    };
    tc_enable_events(&tc0_module, &tc_event);
    events_attach_user(&extint_event, EVSYS_ID_USER_TC0_EVU);
    /* Callback function for capture */
    tc_register_callback(&tc0_module, tc_capture_callback_function, TC_CALLBACK_CC_CHANNEL0);
    tc_enable_callback(&tc0_module, TC_CALLBACK_CC_CHANNEL0);
}

/* Configure external interrupt controller */
void extint_configuration(void)
{
    struct extint_chan_conf extint_chan_config;
    extint_chan_get_config_defaults(&extint_chan_config);
    extint_chan_config.gpio_pin = BUTTON_0_EIC_PIN;
    extint_chan_config.gpio_pin_mux = BUTTON_0_EIC_MUX;
	extint_chan_config.gpio_pin_pull = EXTINT_PULL_UP;
    extint_chan_config.detection_criteria = EXTINT_DETECT_LOW;
    extint_chan_set_config(BUTTON_0_EIC_PIN, &extint_chan_config);
    /* Configure external interrupt module to be event generator */
    struct extint_events extint_event_conf;
    extint_event_conf.generate_event_on_detect[BUTTON_0_EIC_PIN] = true;
    extint_enable_events(&extint_event_conf);

}

/* Configure event system: */
void evsys_configuration(void)
{
    struct events_config config;
    events_get_config_defaults(&config);
    config.generator = EVSYS_ID_GEN_EIC_EXTINT_8;
    config.edge_detect = EVENTS_EDGE_DETECT_BOTH;
    config.path = EVENTS_PATH_ASYNCHRONOUS;
    config.clock_source = GCLK_GENERATOR_1;
    events_allocate(&extint_event, &config);
}

/* Capture callback function */
void tc_capture_callback_function(struct tc_module *const module_inst)
{
    uint32_t button_time = tc_get_capture_value(&tc0_module, TC_COMPARE_CAPTURE_CHANNEL_0);
    /* Configure timer to wait for compare match */
    tc_disable(&tc0_module); //Disable TC0 module to be able to configure it
    tc0_config.enable_capture_on_channel[0] = false;
    tc_init(&tc0_module, TC0, &tc0_config);
    /*Set the value of CC0 as it has been reset*/
    tc_set_compare_value(&tc0_module, TC_COMPARE_CAPTURE_CHANNEL_0, button_time);
    /* Change callback function to tc_led_callback_function */
    tc_register_callback(&tc0_module, tc_compare_callback_function,
    TC_CALLBACK_CC_CHANNEL0);
    tc_enable_callback(&tc0_module, TC_CALLBACK_CC_CHANNEL0);
    tc_enable(&tc0_module); //Enable TC0 module again
    /* LED0 turned on for as long as the button was pushed (button_time) */
    port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
}

/* Compare callback function */
void tc_compare_callback_function(struct tc_module *const module_inst)
{
    /* LED0 turned off */
    port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
    /* Configure timer back to pulse-width capture operation by setting the timer
    to wait for compare match*/
    tc_disable(&tc0_module);
    tc0_config.enable_capture_on_channel[0] = true;
    tc_init(&tc0_module, TC0, &tc0_config);
    /*Change callback function back to tc_button_callback_function*/
    tc_register_callback(&tc0_module, tc_capture_callback_function,
    TC_CALLBACK_CC_CHANNEL0);
    tc_enable_callback(&tc0_module, TC_CALLBACK_CC_CHANNEL0);
    struct tc_events tc_event = {
        .generate_event_on_compare_channel[0] = true,
        .generate_event_on_compare_channel[1] = true,
        .on_event_perform_action = true
    };
    tc_enable_events(&tc0_module, &tc_event);
    tc_enable(&tc0_module); //Enable TC0 module again
}	

int main (void)
{
    /*Initialization and configuration*/
    system_init();
	evsys_configuration();
    tc_configuration();
    extint_configuration();
    /* Enable TC0 module */
    tc_enable(&tc0_module);
    /*Enable interrupts*/
    system_interrupt_enable(TC0_IRQn);
    system_interrupt_enable_global();
    while (1) {
		//port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
	    /*Wait for button to get pushed*/
    }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0