8.8.2.1.2 Code
Add to the main application source file, before any functions, according to the kit used:
- SAM D20 Xplained Pro
#define CONF_EVENT_GENERATOR EVSYS_ID_GEN_TC4_OVF
#define CONF_EVENT_USER EVSYS_ID_USER_TC0_EVU
#define CONF_TC_MODULE TC4
- SAM D21 Xplained Pro
#define CONF_EVENT_GENERATOR EVSYS_ID_GEN_TC4_OVF
#define CONF_EVENT_USER EVSYS_ID_USER_DMAC_CH_0
#define CONF_TC_MODULE TC4
- SAM R21 Xplained Pro
#define CONF_EVENT_GENERATOR EVSYS_ID_GEN_TC4_OVF
#define CONF_EVENT_USER EVSYS_ID_USER_DMAC_CH_0
#define CONF_TC_MODULE TC4
- SAM D11 Xplained Pro
#define CONF_EVENT_GENERATOR EVSYS_ID_GEN_TC2_OVF
#define CONF_EVENT_USER EVSYS_ID_USER_DMAC_CH_0
#define CONF_TC_MODULE TC2
- SAM L21 Xplained Pro
#define CONF_EVENT_GENERATOR EVSYS_ID_GEN_TC4_OVF
#define CONF_EVENT_USER EVSYS_ID_USER_DMAC_CH_0
#define CONF_TC_MODULE TC4
- SAM L22 Xplained Pro
#define CONF_EVENT_GENERATOR EVSYS_ID_GEN_TC0_OVF
#define CONF_EVENT_USER EVSYS_ID_USER_DMAC_CH_0
#define CONF_TC_MODULE TC0
- SAM DA1 Xplained Pro
#define CONF_EVENT_GENERATOR EVSYS_ID_GEN_TC4_OVF
#define CONF_EVENT_USER EVSYS_ID_USER_DMAC_CH_0
#define CONF_TC_MODULE TC4
- SAM C21 Xplained Pro
#define CONF_EVENT_GENERATOR EVSYS_ID_GEN_TC4_OVF
#define CONF_EVENT_USER EVSYS_ID_USER_DMAC_CH_0
Copy-paste the following setup code to your user application:#define CONF_TC_MODULE TC4
Add to user application initialization (typically the start of main()):static
volatile
uint32_t event_count = 0;
void
event_counter(
struct
events_resource *resource);
static
void
configure_event_channel(
struct
events_resource *resource)
{
struct
events_config config;
events_get_config_defaults(&config);
config.generator = CONF_EVENT_GENERATOR;
config.edge_detect = EVENTS_EDGE_DETECT_RISING;
config.path = EVENTS_PATH_SYNCHRONOUS;
config.clock_source = GCLK_GENERATOR_0;
events_allocate(resource, &config);
}
static
void
configure_event_user(
struct
events_resource *resource)
{
events_attach_user(resource, CONF_EVENT_USER);
}
static
void
configure_tc(
struct
tc_module *tc_instance)
{
struct
tc_config config_tc;
struct
tc_events config_events;
tc_get_config_defaults(&config_tc);
config_tc.counter_size = TC_COUNTER_SIZE_8BIT;
config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_FREQ;
config_tc.clock_source = GCLK_GENERATOR_1;
config_tc.clock_prescaler = TC_CLOCK_PRESCALER_DIV64;
tc_init(tc_instance, CONF_TC_MODULE, &config_tc);
config_events.generate_event_on_overflow =
true
;
tc_enable_events(tc_instance, &config_events);
tc_enable(tc_instance);
}
static
void
configure_event_interrupt(
struct
events_resource *resource,
struct
events_hook *hook)
{
events_create_hook(hook, event_counter);
events_add_hook(resource, hook);
events_enable_interrupt_source(resource, EVENTS_INTERRUPT_DETECT);
}
void
event_counter(
struct
events_resource *resource)
{
if
(events_is_interrupt_set(resource, EVENTS_INTERRUPT_DETECT)) {
port_pin_toggle_output_level(LED_0_PIN);
event_count++;
events_ack_interrupt(resource, EVENTS_INTERRUPT_DETECT);
}
}
struct
tc_module tc_instance;
struct
events_resource example_event;
struct
events_hook hook;
system_init();
system_interrupt_enable_global();
configure_event_channel(&example_event);
configure_event_user(&example_event);
configure_event_interrupt(&example_event, &hook);
configure_tc(&tc_instance);