12.8.1.2.2 Workflow

  1. Configure some GPIO port pins for input and output.
    config_port_pins();
    
  2. 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));
    
  3. Enable global interrupts.
    #if (SAML21) || (SAML22) || (SAMC21) || defined(__DOXYGEN__)
        system_pac_enable_interrupt();
    #endif
        system_interrupt_enable_global();
    
  4. Loop to wait for a button press before continuing.
    while (port_pin_get_input_level(BUTTON_0_PIN)) {
        /* Wait for button press */
    }
    
  5. 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();
    
  6. Unlock the PORT peripheral registers.
    system_peripheral_unlock(SYSTEM_PERIPHERAL_ID(PORT),
            ~SYSTEM_PERIPHERAL_ID(PORT));
    
  7. Toggle pin 11, and clear edge detect flag.
    port_pin_toggle_output_level(LED_0_PIN);
    
  8. Lock the PORT peripheral registers.
    system_peripheral_lock(SYSTEM_PERIPHERAL_ID(PORT),
            ~SYSTEM_PERIPHERAL_ID(PORT));
    
  9. Exit the critical section to allow interrupts to function normally again.
    system_interrupt_leave_critical_section();
    
  10. Enter an infinite while loop once the module state has been modified successfully.
    while (1) {
        /* Do nothing */
    }