In this use case, the peripheral-lock will be used to lock and unlock the PORT peripheral access, and show how to implement the PAC module when the PORT registers needs to be altered.
The PORT will be set up as follows:
- One pin in input mode, with pull-up and falling edge-detect
- One pin in output mode
Setup
Prerequisites
There are no special setup requirements for this use-case.
Code
Copy-paste the following setup code to your user application:
void config_port_pins(void)
{
}
Add to user application initialization (typically the start of main()
):
Use Case
Code
Copy-paste the following code to your user application:
config_port_pins();
#if (SAML21) || (SAML22) || (SAMC21) || defined(__DOXYGEN__)
system_pac_enable_interrupt();
#endif
}
while (1) {
}
Workflow
- Configure some GPIO port pins for input and output.
- 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/HA1, it is Hard Fault exception; For SAM L21/C21, it is system exception, see SYSTEM_Handler().
- Enable global interrupts.
#if (SAML21) || (SAML22) || (SAMC21) || defined(__DOXYGEN__)
system_pac_enable_interrupt();
#endif
- Loop to wait for a button press before continuing.
- 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.
- Unlock the PORT peripheral registers.
- Toggle pin 11, and clear edge detect flag.
- Lock the PORT peripheral registers.
- Exit the critical section to allow interrupts to function normally again.
- Enter an infinite while loop once the module state has been modified successfully.