Microchip® Advanced Software Framework

Quick start guide for the SAM PMC module

This is the quick start guide for the PMC module, with step-by-step instructions on how to configure and use the driver in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

PMC use cases

Basic use case - Switch Main Clock sources

In this use case, the PMC module is configured for a variety of system clock sources and speeds. A LED is used to visually indicate the current clock speed as the source is switched.

Setup

Prerequisites

  1. General Purpose I/O Management (gpio)

Code

The following function needs to be added to the user application, to flash a board LED a variable number of times at a rate given in CPU ticks.

#define FLASH_TICK_COUNT 0x00012345
void flash_led(uint32_t tick_count, uint8_t flash_count)
{
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
SysTick->LOAD = tick_count;
while (flash_count--)
{
gpio_toggle_pin(LED0_GPIO);
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
gpio_toggle_pin(LED0_GPIO);
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
}
}

Use case

Example code

Add to application C-file:

for (;;)
{
pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
flash_led(FLASH_TICK_COUNT, 5);
pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
flash_led(FLASH_TICK_COUNT, 5);
pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
flash_led(FLASH_TICK_COUNT, 5);
flash_led(FLASH_TICK_COUNT, 5);
}

Workflow

  1. Wrap the code in an infinite loop:
    for (;;)
  2. Switch the Master CPU frequency to the internal 12MHz RC oscillator, flash a LED on the board several times:
    pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
    flash_led(FLASH_TICK_COUNT, 5);
  3. Switch the Master CPU frequency to the internal 8MHz RC oscillator, flash a LED on the board several times:
    pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
    flash_led(FLASH_TICK_COUNT, 5);
  4. Switch the Master CPU frequency to the internal 4MHz RC oscillator, flash a LED on the board several times:
    pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
    flash_led(FLASH_TICK_COUNT, 5);
  5. Switch the Master CPU frequency to the external crystal oscillator, flash a LED on the board several times:
    flash_led(FLASH_TICK_COUNT, 5);

Use case #2 - Configure Programmable Clocks

In this use case, the PMC module is configured to start the Slow Clock from an attached 32KHz crystal, and start one of the Programmable Clock modules sourced from the Slow Clock divided down with a prescale factor of 64.

Setup

Prerequisites

  1. Parallel Input/Output Controller (pio)

Code

The following code must be added to the user application:

Workflow

  1. Configure the PCK1 pin to output on a specific port pin (in this case, PIOA pin 17) of the microcontroller.
    Note
    The peripheral selection and pin will vary according to your selected SAM device model. Refer to the "Peripheral Signal Multiplexing on I/O Lines" of your device's datasheet.

Use case

The generated PCK1 clock output can be viewed on an oscilloscope attached to the correct pin of the microcontroller.

Example code

Add to application C-file:

Workflow

  1. Switch the Slow Clock source input to an external 32KHz crystal:
  2. Switch the Programmable Clock module PCK1 source clock to the Slow Clock, with a prescaler of 64:
  3. Enable Programmable Clock module PCK1:
  4. Enter an infinite loop:
    for (;;)
    {
    // Do Nothing
    }