In this use case, the EVENT module is configured for:
- Synchronous event path with rising edge detection
- TC4 as event generator on the allocated event channel (TC0 is used for SAM L22)
- One event channel user attached
- An event interrupt hook is used to execute some code when an event is detected
In this use case TC is used as event generator, generating events on overflow. One user attached, counting events on the channel. To be able to execute some code when an event is detected, an interrupt hook is used. The interrupt hook will also count the number of events detected and toggle a LED on the board each time an event is detected.
- Note
- Because this example is showing how to set up an interrupt hook there is no user attached to the user.
Setup
Prerequisites
There are no special setup requirements for this use case.
Code
Add to the main application source file, before any functions, according to the kit used:
- SAM D20 Xplained Pro:
- SAM D21 Xplained Pro:
- SAM R21 Xplained Pro:
- SAM D11 Xplained Pro:
- SAM L21 Xplained Pro:
- SAM L22 Xplained Pro:
- SAM DA1 Xplained Pro:
- SAM C21 Xplained Pro:
- SAM HA1G16A Xplained Pro Copy-paste the following setup code to your user application: Add to user application initialization (typically the start of
main()
):
Workflow
- Create an event channel configuration structure instance which will contain the configuration for the event.
- 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.
- 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
Allocate and configure the channel using the configuration structure.
- Make sure there is no user attached. To attach a user, change the value of EXAMPLE_EVENT_USER to the correct peripheral ID.
- Create config_tc and config_events configuration structure instances.
- 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.
- 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
- Initialize, configure, and assosiate the tc_instance handle with the TC hardware pointed to by TC_MODULE.
- Adjust the config_events structure to enable event generation on overflow in the timer and then enable the event configuration.
- Enable the timer/counter module.
- Create a new interrupt hook and use the function event_counter as hook code.
- Add the newly created hook to the interrupt hook queue and enable the event detected interrupt.
- 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.
Use Case
Code
Copy-paste the following code to your user application:
Workflow
- Wait for the event channel to become ready.
- Start the timer/counter.