8.8.2.1.3 Workflow
- Create an event channel configuration structure instance which will contain the configuration for the event.
struct
events_config config;
- Initialize the event channel configuration struct with safe default values.Note: This shall always be performed before using the configuration struct to ensure that all members are initialized to known default values.
events_get_config_defaults(&config);
- Adjust the configuration structure:
Use EXAMPLE_EVENT_GENRATOR as event generator
Detect events on rising edge
Use the synchronous event path
Use GCLK Generator 0 as event channel clock source
config.generator = CONF_EVENT_GENERATOR;
config.edge_detect = EVENTS_EDGE_DETECT_RISING;
config.path = EVENTS_PATH_SYNCHRONOUS;
config.clock_source = GCLK_GENERATOR_0;
Allocate and configure the channel using the configuration structure.
events_allocate(resource, &config);
- Make sure there is no user attached. To attach a user, change the value of EXAMPLE_EVENT_USER to the correct peripheral ID.
events_attach_user(resource, CONF_EVENT_USER);
- Create config_tc and config_events configuration structure instances.
struct
tc_config config_tc;
struct
tc_events config_events;
- Initialize the TC module configuration structure with safe default values.Note: This function shall always be called on new configuration structure instances to make sure that all structure members are initialized.
tc_get_config_defaults(&config_tc);
- Adjust the config_tc structure:
Set counter size to 8-bit
Set wave generation mode to normal frequency generation
Use GCLK generator 1 to as TC module clock source
Prescale the input clock with 64
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;
- Initialize, configure, and assosiate the tc_instance handle with the TC hardware pointed to by TC_MODULE.
tc_init(tc_instance, CONF_TC_MODULE, &config_tc);
- Adjust the config_events structure to enable event generation on overflow in the timer and then enable the event configuration.
config_events.generate_event_on_overflow =
true
;
tc_enable_events(tc_instance, &config_events);
- Enable the timer/counter module.
tc_enable(tc_instance);
- Create a new interrupt hook and use the function event_counter as hook code.
events_create_hook(hook, event_counter);
- Add the newly created hook to the interrupt hook queue and enable the event detected interrupt.
events_add_hook(resource, hook);
events_enable_interrupt_source(resource, EVENTS_INTERRUPT_DETECT);
Example interrupt hook code. If the hook was triggered by an event detected interrupt on the event channel this code will toggle the LED on the Xplained PRO board and increase the value of the event_count variable. The interrupt is then acknowledged.
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);
}
}