gossamer 0.0.1
a very lightweight app framework for SAMD and SAML chips
Loading...
Searching...
No Matches
System Internals

Functions for configuring the system and putting it in standby mode. More...

Functions

void sys_init (void)
 Initializes the system clocks and performs any required system-wide setup.
 
uint32_t get_cpu_frequency (void)
 Gets the CPU frequency.
 
bool set_cpu_frequency (uint32_t freq)
 Sets the CPU frequency.
 
void _enable_48mhz_gclk1 (void)
 Enables the 48 MHz clock on GCLK1 for USB operation.
 
void _enter_standby_mode (void)
 Enters the low-power STANDBY mode. Does not return until an interrupt fires.
 

Detailed Description

Functions for configuring the system and putting it in standby mode.

Function Documentation

◆ _enable_48mhz_gclk1()

void _enable_48mhz_gclk1 ( void  )

Enables the 48 MHz clock on GCLK1 for USB operation.

This function is called by the USB stack when it is initialized.

Warning
At this time, the 48 MHz clock is hard-coded to use USB Clock Recovery Mode, and as such will only be available if your device is plugged into a USB host. It is possible to derive the 48 MHz DFLL from an external 32kHz crystal, but this is not currently implemented in Gossamer.

◆ _enter_standby_mode()

void _enter_standby_mode ( void  )

Enters the low-power STANDBY mode. Does not return until an interrupt fires.

You should not generally need to call this function; at the end of every app_loop invocation, the main run loop enters standby mode if your app indicated that it was appropriate.

◆ get_cpu_frequency()

uint32_t get_cpu_frequency ( void  )

Gets the CPU frequency.

Returns
8000000 by default, or another value if set by set_cpu_frequency

◆ set_cpu_frequency()

bool set_cpu_frequency ( uint32_t  freq)

Sets the CPU frequency.

Parameters
freqone of 1000000, 2000000, 4000000, 8000000, 12000000 or 16000000.

Supported CPU frequencies vary from chip to chip:

  • On the SAM D11 and D21, you can choose 8, 4, 2 or 1 MHz.
  • On the SAM L21 and L22, you can choose 16, 12, 8 or 4 MHz. While you can run these chips at higher frequencies using the DFLL, Gossamer aims for simplicity: this means that it only supports the internal oscillator at one of its standard frequencies.
    Note
    Some peripherals like SERCOMs and TC/TCCs read the CPU frequency when they are configured, to set critical timing-related parameters like baud rate or period. If you change the CPU frequency after setting up these peripherals and then attempt to use them, they will very likely behave in unexpected ways. Thus you should de-init any peripherals that depend on GCLK0 before calling set_cpu_frequency, and then re-init them after.

◆ sys_init()

void sys_init ( void  )

Initializes the system clocks and performs any required system-wide setup.

Gossamer aims for consistency at system startup, no matter which chip you are working with. To that end:

  • GCLK0, the main system clock, is set to 8 MHz.
  • GCLK2 is the low-power 32768 Hz clock, OSCULP32K
  • GCLK3 is a 1024 Hz clock, derived from the most accurate source available. GCLK0 is set to ONDEMAND with RUNSTDBY off, so the main clock remains off when in standby mode. GCLK2 and GCLK3 are also ONDEMAND, but RUNSTDBY is on. This ensures that if a peripheral requests it, the clock will remain on in standby, but if no peripheral requests it, it will not run at all. Also note that while GCLK1 is not turned on at startup, it may be reserved for internal use depending on your use case. If you intend to use USB, the usb_init function will claim GCLK1 for the 48 MHz DFLL clock.