12.8.1.2.2 Workflow
- Configure some GPIO port pins for input and output.
config_port_pins();
- Lock peripheral access for the PORT module; attempting to update the module while it is in a protected state will cause a CPU exception. For SAM D20/D21/D10/D11/R21/DA0/DA1, it is Hard Fault exception; For SAM L21/C21, it is system exception, see SYSTEM_Handler().
system_peripheral_lock(SYSTEM_PERIPHERAL_ID(PORT),
~SYSTEM_PERIPHERAL_ID(PORT));
- Enable global interrupts.
#if (SAML21) || (SAML22) || (SAMC21) || defined(__DOXYGEN__)
system_pac_enable_interrupt();
#endif
system_interrupt_enable_global();
- Loop to wait for a button press before continuing.
while
(port_pin_get_input_level(BUTTON_0_PIN)) {
/* Wait for button press */
}
- Enter a critical section, so that the PAC module can be unlocked safely and the peripheral manipulated without the possibility of an interrupt modifying the protected module's state.
system_interrupt_enter_critical_section();
- Unlock the PORT peripheral registers.
system_peripheral_unlock(SYSTEM_PERIPHERAL_ID(PORT),
~SYSTEM_PERIPHERAL_ID(PORT));
- Toggle pin 11, and clear edge detect flag.
port_pin_toggle_output_level(LED_0_PIN);
- Lock the PORT peripheral registers.
system_peripheral_lock(SYSTEM_PERIPHERAL_ID(PORT),
~SYSTEM_PERIPHERAL_ID(PORT));
- Exit the critical section to allow interrupts to function normally again.
system_interrupt_leave_critical_section();
- Enter an infinite while loop once the module state has been modified successfully.
while
(1) {
/* Do nothing */
}