Microchip® Advanced Software Framework

Quick Start Guide for PAC - Basic

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)
{
struct port_config pin_conf;
pin_conf.direction = PORT_PIN_DIR_INPUT;
pin_conf.input_pull = PORT_PIN_PULL_UP;
port_pin_set_config(BUTTON_0_PIN, &pin_conf);
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(LED_0_PIN, &pin_conf);
}

Add to user application initialization (typically the start of main()):

config_port_pins();

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 (port_pin_get_input_level(BUTTON_0_PIN)) {
/* Wait for button press */
}
while (1) {
/* Do nothing */
}

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/HA1, it is Hard Fault exception; For SAM L21/C21, it is system exception, see SYSTEM_Handler().
  3. Enable global interrupts.
    #if (SAML21) || (SAML22) || (SAMC21) || defined(__DOXYGEN__)
    system_pac_enable_interrupt();
    #endif
  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.
  6. Unlock the PORT peripheral registers.
  7. Toggle pin 11, and clear edge detect flag.
  8. Lock the PORT peripheral registers.
  9. Exit the critical section to allow interrupts to function normally again.
  10. Enter an infinite while loop once the module state has been modified successfully.
    while (1) {
    /* Do nothing */
    }