4 * \brief SAM D20 System related functionality
\r
6 * Copyright (C) 2012-2013 Atmel Corporation. All rights reserved.
\r
12 * Redistribution and use in source and binary forms, with or without
\r
13 * modification, are permitted provided that the following conditions are met:
\r
15 * 1. Redistributions of source code must retain the above copyright notice,
\r
16 * this list of conditions and the following disclaimer.
\r
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
19 * this list of conditions and the following disclaimer in the documentation
\r
20 * and/or other materials provided with the distribution.
\r
22 * 3. The name of Atmel may not be used to endorse or promote products derived
\r
23 * from this software without specific prior written permission.
\r
25 * 4. This software may only be redistributed and used in connection with an
\r
26 * Atmel microcontroller product.
\r
28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
\r
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
\r
32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\r
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
38 * POSSIBILITY OF SUCH DAMAGE.
\r
43 #ifndef SYSTEM_H_INCLUDED
\r
44 #define SYSTEM_H_INCLUDED
\r
46 #include <compiler.h>
\r
52 * \defgroup asfdoc_samd20_system_group SAM D20 System Driver (SYSTEM)
\r
54 * This driver for SAM D20 devices provides an interface for the configuration
\r
55 * and management of the device's system relation functionality, necessary for
\r
56 * the basic device operation. This is not limited to a single peripheral, but
\r
57 * extends across multiple hardware peripherals,
\r
59 * The following peripherals are used by this module:
\r
61 * - SYSCTRL (System Control)
\r
62 * - PM (Power Manager)
\r
64 * The outline of this documentation is as follows:
\r
65 * - \ref asfdoc_samd20_system_prerequisites
\r
66 * - \ref asfdoc_samd20_system_module_overview
\r
67 * - \ref asfdoc_samd20_system_special_considerations
\r
68 * - \ref asfdoc_samd20_system_extra_info
\r
69 * - \ref asfdoc_samd20_system_examples
\r
70 * - \ref asfdoc_samd20_system_api_overview
\r
73 * \section asfdoc_samd20_system_prerequisites Prerequisites
\r
75 * There are no prerequisites for this module.
\r
78 * \section asfdoc_samd20_system_module_overview Module Overview
\r
80 * The System driver provides a collection of interfaces between the user
\r
81 * application logic, and the core device functionality (such as clocks, reset
\r
82 * cause determination, etc.) that is required for all applications. It contains
\r
83 * a number of sub-modules that control one specific aspect of the device:
\r
85 * - System Core (this module)
\r
86 * - \ref asfdoc_samd20_system_clock_group "System Clock Control" (sub-module)
\r
87 * - \ref asfdoc_samd20_system_interrupt_group "System Interrupt Control" (sub-module)
\r
88 * - \ref asfdoc_samd20_system_pinmux_group "System Pin Multiplexer Control" (sub-module)
\r
91 * \subsection asfdoc_samd20_system_module_overview_vref Voltage References
\r
92 * The various analog modules within the SAM D20 devices (such as AC, ADC and
\r
93 * DAC) require a voltage reference to be configured to act as a reference point
\r
94 * for comparisons and conversions.
\r
96 * The SAM D20 devices contain multiple references, including an internal
\r
97 * temperature sensor, and a fixed band-gap voltage source. When enabled, the
\r
98 * associated voltage reference can be selected within the desired peripheral
\r
101 * \subsection asfdoc_samd20_system_module_overview_reset_cause System Reset Cause
\r
102 * In some application there may be a need to execute a different program
\r
103 * flow based on how the device was reset. For example, if the cause of reset
\r
104 * was the Watchdog timer (WDT), this might indicate an error in the application
\r
105 * and a form of error handling or error logging might be needed.
\r
107 * For this reason, an API is provided to retrieve the cause of the last system
\r
108 * reset, so that appropriate action can be taken.
\r
110 * \subsection asfdoc_samd20_system_module_overview_sleep_mode Sleep Modes
\r
111 * The SAM D20 devices have several sleep modes, where the sleep mode controls
\r
112 * which clock systems on the device will remain enabled or disabled when the
\r
113 * device enters a low power sleep mode.
\r
114 * \ref asfdoc_samd20_system_module_sleep_mode_table "The table below" lists the
\r
115 * clock settings of the different sleep modes.
\r
117 * \anchor asfdoc_samd20_system_module_sleep_mode_table
\r
119 * <caption>SAM D20 Device Sleep Modes</caption>
\r
121 * <th>Sleep mode</th>
\r
122 * <th>CPU clock</th>
\r
123 * <th>AHB clock</th>
\r
124 * <th>APB clocks</th>
\r
125 * <th>Clock sources</th>
\r
126 * <th>System clock</th>
\r
128 * <th>Reg mode</th>
\r
129 * <th>RAM mode</th>
\r
172 * <td>Low Power</td>
\r
173 * <td>Source/Drain biasing</td>
\r
177 * To enter device sleep, one of the available sleep modes must be set, and the
\r
178 * function to enter sleep called. The device will automatically wake up in
\r
179 * response to an interrupt being generated or other device event.
\r
181 * Some peripheral clocks will remain enabled during sleep, depending on their
\r
182 * configuration; if desired, modules can remain clocked during sleep to allow
\r
183 * them to continue to operate while other parts of the system are powered down
\r
187 * \section asfdoc_samd20_system_special_considerations Special Considerations
\r
189 * Most of the functions in this driver have device specific restrictions and
\r
190 * caveats; refer to your device datasheet.
\r
193 * \section asfdoc_samd20_system_extra_info Extra Information
\r
195 * For extra information see \ref asfdoc_samd20_system_extra. This includes:
\r
196 * - \ref asfdoc_samd20_system_extra_acronyms
\r
197 * - \ref asfdoc_samd20_system_extra_dependencies
\r
198 * - \ref asfdoc_samd20_system_extra_errata
\r
199 * - \ref asfdoc_samd20_system_extra_history
\r
202 * \section asfdoc_samd20_system_examples Examples
\r
204 * For SYSTEM module related examples, please refer to the sub-modules listed in
\r
205 * the \ref asfdoc_samd20_system_module_overview "system module overview".
\r
208 * \section asfdoc_samd20_system_api_overview API Overview
\r
213 * \brief Voltage references within the device.
\r
215 * List of available voltage references (VREF) that may be used within the
\r
218 enum system_voltage_reference {
\r
219 /** Temperature sensor voltage reference. */
\r
220 SYSTEM_VOLTAGE_REFERENCE_TEMPSENSE,
\r
221 /** Bandgap voltage reference. */
\r
222 SYSTEM_VOLTAGE_REFERENCE_BANDGAP,
\r
226 * \brief Device sleep modes.
\r
228 * List of available sleep modes in the device. A table of clocks available in
\r
229 * different sleep modes can be found in \ref asfdoc_samd20_system_module_overview_sleep_mode.
\r
231 enum system_sleepmode {
\r
232 /** IDLE 0 sleep mode. */
\r
233 SYSTEM_SLEEPMODE_IDLE_0,
\r
234 /** IDLE 1 sleep mode. */
\r
235 SYSTEM_SLEEPMODE_IDLE_1,
\r
236 /** IDLE 2 sleep mode. */
\r
237 SYSTEM_SLEEPMODE_IDLE_2,
\r
238 /** Standby sleep mode. */
\r
239 SYSTEM_SLEEPMODE_STANDBY,
\r
243 * \brief Reset causes of the system.
\r
245 * List of possible reset causes of the system.
\r
247 enum system_reset_cause {
\r
248 /** The system was last reset by a software reset. */
\r
249 SYSTEM_RESET_CAUSE_SOFTWARE = PM_RCAUSE_SYST,
\r
250 /** The system was last reset by the watchdog timer. */
\r
251 SYSTEM_RESET_CAUSE_WDT = PM_RCAUSE_WDT,
\r
252 /** The system was last reset because the external reset line was pulled low. */
\r
253 SYSTEM_RESET_CAUSE_EXTERNAL_RESET = PM_RCAUSE_EXT,
\r
254 /** The system was last reset by the BOD33. */
\r
255 SYSTEM_RESET_CAUSE_BOD33 = PM_RCAUSE_BOD33,
\r
256 /** The system was last reset by the BOD12. */
\r
257 SYSTEM_RESET_CAUSE_BOD12 = PM_RCAUSE_BOD12,
\r
258 /** The system was last reset by the POR (Power on reset). */
\r
259 SYSTEM_RESET_CAUSE_POR = PM_RCAUSE_POR,
\r
263 * \name System identification
\r
268 * \brief Retrieve the device identification signature
\r
270 * Retrieves the signature of the current device.
\r
272 * \return Device ID signature as a 32-bit integer.
\r
274 static inline uint32_t system_get_device_id(void)
\r
276 return DSU->DID.reg;
\r
285 * \name Voltage references
\r
290 * \brief Enable the selected voltage reference
\r
292 * Enables the selected voltage reference source, making the voltage reference
\r
293 * available on a pin as well as an input source to the analog peripherals.
\r
295 * \param[in] vref Voltage reference to enable
\r
297 static inline void system_voltage_reference_enable(
\r
298 const enum system_voltage_reference vref)
\r
301 case SYSTEM_VOLTAGE_REFERENCE_TEMPSENSE:
\r
302 SYSCTRL->VREF.reg |= SYSCTRL_VREF_TSEN;
\r
305 case SYSTEM_VOLTAGE_REFERENCE_BANDGAP:
\r
306 SYSCTRL->VREF.reg |= SYSCTRL_VREF_BGOUTEN;
\r
316 * \brief Disable the selected voltage reference
\r
318 * Disables the selected voltage reference source.
\r
320 * \param[in] vref Voltage reference to disable
\r
322 static inline void system_voltage_reference_disable(
\r
323 const enum system_voltage_reference vref)
\r
326 case SYSTEM_VOLTAGE_REFERENCE_TEMPSENSE:
\r
327 SYSCTRL->VREF.reg &= ~SYSCTRL_VREF_TSEN;
\r
330 case SYSTEM_VOLTAGE_REFERENCE_BANDGAP:
\r
331 SYSCTRL->VREF.reg &= ~SYSCTRL_VREF_BGOUTEN;
\r
346 * \name Device sleep
\r
351 * \brief Set the sleep mode of the device
\r
353 * Sets the sleep mode of the device; the configured sleep mode will be entered
\r
354 * upon the next call of the \ref system_sleep() function.
\r
356 * For an overview of which systems are disabled in sleep for the different
\r
357 * sleep modes, see \ref asfdoc_samd20_system_module_overview_sleep_mode.
\r
359 * \param[in] sleep_mode Sleep mode to configure for the next sleep operation
\r
361 * \retval STATUS_OK Operation completed successfully
\r
362 * \retval STATUS_ERR_INVALID_ARG The requested sleep mode was invalid or not
\r
365 static inline enum status_code system_set_sleepmode(
\r
366 const enum system_sleepmode sleep_mode)
\r
368 switch (sleep_mode) {
\r
369 case SYSTEM_SLEEPMODE_IDLE_0:
\r
370 case SYSTEM_SLEEPMODE_IDLE_1:
\r
371 case SYSTEM_SLEEPMODE_IDLE_2:
\r
372 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
\r
373 PM->SLEEP.reg = sleep_mode;
\r
376 case SYSTEM_SLEEPMODE_STANDBY:
\r
377 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
\r
381 return STATUS_ERR_INVALID_ARG;
\r
388 * \brief Put the system to sleep waiting for interrupt
\r
390 * Executes a device DSB (Data Synchronization Barrier) instruction to ensure
\r
391 * all ongoing memory accesses have completed, then a WFI (Wait For Interrupt)
\r
392 * instruction to place the device into the sleep mode specified by
\r
393 * \ref system_set_sleepmode until woken by an interrupt.
\r
395 static inline void system_sleep(void)
\r
406 * \name Reset control
\r
411 * \brief Reset the MCU
\r
413 * Resets the MCU and all associated peripherals and registers, except RTC, all 32kHz sources,
\r
414 * WDT (if ALWAYSON is set) and GCLK (if WRTLOCK is set).
\r
417 static inline void system_reset(void)
\r
419 NVIC_SystemReset();
\r
423 * \brief Return the reset cause
\r
425 * Retrieves the cause of the last system reset.
\r
427 * \return An enum value indicating the cause of the last system reset.
\r
429 static inline enum system_reset_cause system_get_reset_cause(void)
\r
431 return (enum system_reset_cause)PM->RCAUSE.reg;
\r
440 * \name System initialization
\r
444 void system_init(void);
\r
456 * \page asfdoc_samd20_system_extra Extra Information for SYSTEM Driver
\r
458 * \section asfdoc_samd20_system_extra_acronyms Acronyms
\r
459 * Below is a table listing the acronyms used in this module, along with their
\r
460 * intended meanings.
\r
465 * <th>Definition</th>
\r
469 * <td>Power Manager</td>
\r
473 * <td>System control interface</td>
\r
478 * \section asfdoc_samd20_system_extra_dependencies Dependencies
\r
479 * This driver has the following dependencies:
\r
484 * \section asfdoc_samd20_system_extra_errata Errata
\r
485 * There are no errata related to this driver.
\r
488 * \section asfdoc_samd20_system_extra_history Module History
\r
489 * An overview of the module history is presented in the table below, with
\r
490 * details on the enhancements and fixes made to the module since its first
\r
491 * release. The current version of this corresponds to the newest version in
\r
496 * <th>Changelog</th>
\r
499 * <td>Added new \c system_reset() to reset the complete MCU with some exceptions</td>
\r
502 * <td>Added new \c system_get_device_id() function to retrieved the device
\r
506 * <td>Initial Release</td>
\r
510 * \page asfdoc_samd20_system_document_revision_history Document Revision History
\r
514 * <th>Doc. Rev.</td>
\r
516 * <th>Comments</td>
\r
521 * <td>Corrected documentation typos.</td>
\r
526 * <td>Initial release</td>
\r
531 #endif /* SYSTEM_H_INCLUDED */
\r