4 * \brief Power Management Controller (PMC) driver for SAM.
\r
6 * Copyright (c) 2011-2012 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
44 #ifndef PMC_H_INCLUDED
\r
45 #define PMC_H_INCLUDED
\r
47 #include "compiler.h"
\r
57 /** Bit mask for peripheral clocks (PCER0) */
\r
58 #define PMC_MASK_STATUS0 (0xFFFFFFFC)
\r
60 /** Bit mask for peripheral clocks (PCER1) */
\r
61 #define PMC_MASK_STATUS1 (0xFFFFFFFF)
\r
63 /** Loop counter timeout value */
\r
64 #define PMC_TIMEOUT (2048)
\r
66 /** Key to unlock CKGR_MOR register */
\r
67 #define PMC_CKGR_MOR_KEY_VALUE CKGR_MOR_KEY(0x37)
\r
69 /** Key used to write SUPC registers */
\r
70 #define SUPC_KEY_VALUE ((uint32_t) 0xA5)
\r
72 /** PMC xtal startup time */
\r
73 #define PMC_XTAL_STARTUP_TIME (0x3F)
\r
75 /** Mask to access fast startup input */
\r
76 #define PMC_FAST_STARTUP_Msk (0x7FFFFu)
\r
78 /** PMC_WPMR Write Protect KEY, unlock it */
\r
79 #define PMC_WPMR_WPKEY_VALUE PMC_WPMR_WPKEY((uint32_t) 0x504D43)
\r
81 /** Using external oscillator */
\r
82 #define PMC_OSC_XTAL 0
\r
84 /** Oscillator in bypass mode */
\r
85 #define PMC_OSC_BYPASS 1
\r
87 #define PMC_PCK_0 0 /* PCK0 ID */
\r
88 #define PMC_PCK_1 1 /* PCK1 ID */
\r
89 #define PMC_PCK_2 2 /* PCK2 ID */
\r
92 * \name Master clock (MCK) Source and Prescaler configuration
\r
94 * The following functions may be used to select the clock source and
\r
95 * prescaler for the master clock.
\r
99 void pmc_mck_set_prescaler(uint32_t ul_pres);
\r
100 void pmc_mck_set_source(uint32_t ul_source);
\r
101 uint32_t pmc_switch_mck_to_sclk(uint32_t ul_pres);
\r
102 uint32_t pmc_switch_mck_to_mainck(uint32_t ul_pres);
\r
103 uint32_t pmc_switch_mck_to_pllack(uint32_t ul_pres);
\r
104 #if (SAM3S || SAM4S)
\r
105 uint32_t pmc_switch_mck_to_pllbck(uint32_t ul_pres);
\r
107 #if (SAM3XA || SAM3U)
\r
108 uint32_t pmc_switch_mck_to_upllck(uint32_t ul_pres);
\r
114 * \name Slow clock (SLCK) oscillator and configuration
\r
119 void pmc_switch_sclk_to_32kxtal(uint32_t ul_bypass);
\r
120 uint32_t pmc_osc_is_ready_32kxtal(void);
\r
125 * \name Main Clock (MAINCK) oscillator and configuration
\r
130 void pmc_switch_mainck_to_fastrc(uint32_t ul_moscrcf);
\r
131 void pmc_osc_enable_fastrc(uint32_t ul_rc);
\r
132 void pmc_osc_disable_fastrc(void);
\r
133 void pmc_switch_mainck_to_xtal(uint32_t ul_bypass);
\r
134 void pmc_osc_disable_xtal(uint32_t ul_bypass);
\r
135 uint32_t pmc_osc_is_ready_mainck(void);
\r
140 * \name PLL oscillator and configuration
\r
145 void pmc_enable_pllack(uint32_t mula, uint32_t pllacount, uint32_t diva);
\r
146 void pmc_disable_pllack(void);
\r
147 uint32_t pmc_is_locked_pllack(void);
\r
149 #if (SAM3S || SAM4S)
\r
150 void pmc_enable_pllbck(uint32_t mulb, uint32_t pllbcount, uint32_t divb);
\r
151 void pmc_disable_pllbck(void);
\r
152 uint32_t pmc_is_locked_pllbck(void);
\r
155 #if (SAM3XA || SAM3U)
\r
156 void pmc_enable_upll_clock(void);
\r
157 void pmc_disable_upll_clock(void);
\r
158 uint32_t pmc_is_locked_upll(void);
\r
164 * \name Peripherals clock configuration
\r
169 uint32_t pmc_enable_periph_clk(uint32_t ul_id);
\r
170 uint32_t pmc_disable_periph_clk(uint32_t ul_id);
\r
171 void pmc_enable_all_periph_clk(void);
\r
172 void pmc_disable_all_periph_clk(void);
\r
173 uint32_t pmc_is_periph_clk_enabled(uint32_t ul_id);
\r
178 * \name Programmable clock Source and Prescaler configuration
\r
180 * The following functions may be used to select the clock source and
\r
181 * prescaler for the specified programmable clock.
\r
185 void pmc_pck_set_prescaler(uint32_t ul_id, uint32_t ul_pres);
\r
186 void pmc_pck_set_source(uint32_t ul_id, uint32_t ul_source);
\r
187 uint32_t pmc_switch_pck_to_sclk(uint32_t ul_id, uint32_t ul_pres);
\r
188 uint32_t pmc_switch_pck_to_mainck(uint32_t ul_id, uint32_t ul_pres);
\r
189 uint32_t pmc_switch_pck_to_pllack(uint32_t ul_id, uint32_t ul_pres);
\r
190 #if (SAM3S || SAM4S)
\r
191 uint32_t pmc_switch_pck_to_pllbck(uint32_t ul_id, uint32_t ul_pres);
\r
193 #if (SAM3XA || SAM3U)
\r
194 uint32_t pmc_switch_pck_to_upllck(uint32_t ul_id, uint32_t ul_pres);
\r
196 void pmc_enable_pck(uint32_t ul_id);
\r
197 void pmc_disable_pck(uint32_t ul_id);
\r
198 void pmc_enable_all_pck(void);
\r
199 void pmc_disable_all_pck(void);
\r
200 uint32_t pmc_is_pck_enabled(uint32_t ul_id);
\r
205 * \name USB clock configuration
\r
210 #if (SAM3S || SAM3XA || SAM4S)
\r
211 void pmc_switch_udpck_to_pllack(uint32_t ul_usbdiv);
\r
213 #if (SAM3S || SAM4S)
\r
214 void pmc_switch_udpck_to_pllbck(uint32_t ul_usbdiv);
\r
217 void pmc_switch_udpck_to_upllck(uint32_t ul_usbdiv);
\r
219 #if (SAM3S || SAM3XA || SAM4S)
\r
220 void pmc_enable_udpck(void);
\r
221 void pmc_disable_udpck(void);
\r
227 * \name Interrupt and status management
\r
232 void pmc_enable_interrupt(uint32_t ul_sources);
\r
233 void pmc_disable_interrupt(uint32_t ul_sources);
\r
234 uint32_t pmc_get_interrupt_mask(void);
\r
235 uint32_t pmc_get_status(void);
\r
240 * \name Power management
\r
242 * The following functions are used to configure sleep mode and additional
\r
247 void pmc_set_fast_startup_input(uint32_t ul_inputs);
\r
248 void pmc_clr_fast_startup_input(uint32_t ul_inputs);
\r
249 void pmc_enable_sleepmode(uint8_t uc_type);
\r
250 void pmc_enable_waitmode(void);
\r
251 void pmc_enable_backupmode(void);
\r
256 * \name Failure detector
\r
261 void pmc_enable_clock_failure_detector(void);
\r
262 void pmc_disable_clock_failure_detector(void);
\r
267 * \name Write protection
\r
272 void pmc_set_writeprotect(uint32_t ul_enable);
\r
273 uint32_t pmc_get_writeprotect_status(void);
\r
288 * \page sam_pmc_quickstart Quick start guide for the SAM PMC module
\r
290 * This is the quick start guide for the \ref pmc_group "PMC module", with
\r
291 * step-by-step instructions on how to configure and use the driver in a
\r
292 * selection of use cases.
\r
294 * The use cases contain several code fragments. The code fragments in the
\r
295 * steps for setup can be copied into a custom initialization function, while
\r
296 * the steps for usage can be copied into, e.g., the main application function.
\r
298 * \section pmc_use_cases PMC use cases
\r
299 * - \ref pmc_basic_use_case Basic use case - Switch Main Clock sources
\r
300 * - \ref pmc_use_case_2 Advanced use case - Configure Programmable Clocks
\r
302 * \section pmc_basic_use_case Basic use case - Switch Main Clock sources
\r
303 * In this use case, the PMC module is configured for a variety of system clock
\r
304 * sources and speeds. A LED is used to visually indicate the current clock
\r
305 * speed as the source is switched.
\r
307 * \section pmc_basic_use_case_setup Setup
\r
309 * \subsection pmc_basic_use_case_setup_prereq Prerequisites
\r
310 * -# \ref gpio_group "General Purpose I/O Management (gpio)"
\r
312 * \subsection pmc_basic_use_case_setup_code Code
\r
313 * The following function needs to be added to the user application, to flash a
\r
314 * board LED a variable number of times at a rate given in CPU ticks.
\r
317 * #define FLASH_TICK_COUNT 0x00012345
\r
319 * void flash_led(uint32_t tick_count, uint8_t flash_count)
\r
321 * SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
\r
322 * SysTick->LOAD = tick_count;
\r
324 * while (flash_count--)
\r
326 * gpio_toggle_pin(LED0_GPIO);
\r
327 * while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
\r
328 * gpio_toggle_pin(LED0_GPIO);
\r
329 * while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
\r
334 * \section pmc_basic_use_case_usage Use case
\r
336 * \subsection pmc_basic_use_case_usage_code Example code
\r
337 * Add to application C-file:
\r
341 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
\r
342 * flash_led(FLASH_TICK_COUNT, 5);
\r
343 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
\r
344 * flash_led(FLASH_TICK_COUNT, 5);
\r
345 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
\r
346 * flash_led(FLASH_TICK_COUNT, 5);
\r
347 * pmc_switch_mainck_to_xtal(0);
\r
348 * flash_led(FLASH_TICK_COUNT, 5);
\r
352 * \subsection pmc_basic_use_case_usage_flow Workflow
\r
353 * -# Wrap the code in an infinite loop:
\r
357 * -# Switch the Master CPU frequency to the internal 12MHz RC oscillator, flash
\r
358 * a LED on the board several times:
\r
360 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
\r
361 * flash_led(FLASH_TICK_COUNT, 5);
\r
363 * -# Switch the Master CPU frequency to the internal 8MHz RC oscillator, flash
\r
364 * a LED on the board several times:
\r
366 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
\r
367 * flash_led(FLASH_TICK_COUNT, 5);
\r
369 * -# Switch the Master CPU frequency to the internal 4MHz RC oscillator, flash
\r
370 * a LED on the board several times:
\r
372 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
\r
373 * flash_led(FLASH_TICK_COUNT, 5);
\r
375 * -# Switch the Master CPU frequency to the external crystal oscillator, flash
\r
376 * a LED on the board several times:
\r
378 * pmc_switch_mainck_to_xtal(0);
\r
379 * flash_led(FLASH_TICK_COUNT, 5);
\r
384 * \page pmc_use_case_2 Use case #2 - Configure Programmable Clocks
\r
385 * In this use case, the PMC module is configured to start the Slow Clock from
\r
386 * an attached 32KHz crystal, and start one of the Programmable Clock modules
\r
387 * sourced from the Slow Clock divided down with a prescale factor of 64.
\r
389 * \section pmc_use_case_2_setup Setup
\r
391 * \subsection pmc_use_case_2_setup_prereq Prerequisites
\r
392 * -# \ref pio_group "Parallel Input/Output Controller (pio)"
\r
394 * \subsection pmc_use_case_2_setup_code Code
\r
395 * The following code must be added to the user application:
\r
397 * pio_set_peripheral(PIOA, PIO_PERIPH_B, PIO_PA17);
\r
400 * \subsection pmc_use_case_2_setup_code_workflow Workflow
\r
401 * -# Configure the PCK1 pin to output on a specific port pin (in this case,
\r
402 * PIOA pin 17) of the microcontroller.
\r
404 * pio_set_peripheral(PIOA, PIO_PERIPH_B, PIO_PA17);
\r
406 * \note The peripheral selection and pin will vary according to your selected
\r
407 * SAM device model. Refer to the "Peripheral Signal Multiplexing on I/O
\r
408 * Lines" of your device's datasheet.
\r
410 * \section pmc_use_case_2_usage Use case
\r
411 * The generated PCK1 clock output can be viewed on an oscilloscope attached to
\r
412 * the correct pin of the microcontroller.
\r
414 * \subsection pmc_use_case_2_usage_code Example code
\r
415 * Add to application C-file:
\r
417 * pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);
\r
418 * pmc_switch_pck_to_sclk(PMC_PCK_1, PMC_PCK_PRES_CLK_64);
\r
419 * pmc_enable_pck(PMC_PCK_1);
\r
427 * \subsection pmc_use_case_2_usage_flow Workflow
\r
428 * -# Switch the Slow Clock source input to an external 32KHz crystal:
\r
430 * pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);
\r
432 * -# Switch the Programmable Clock module PCK1 source clock to the Slow Clock,
\r
433 * with a prescaler of 64:
\r
435 * pmc_switch_pck_to_sclk(PMC_PCK_1, PMC_PCK_PRES_CLK_64);
\r
437 * -# Enable Programmable Clock module PCK1:
\r
439 * pmc_enable_pck(PMC_PCK_1);
\r
441 * -# Enter an infinite loop:
\r
450 #endif /* PMC_H_INCLUDED */
\r