4 * \brief Power Management Controller (PMC) driver for SAM.
\r
6 * Copyright (c) 2011 - 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
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 /** Mask to access fast startup input */
\r
73 #define PMC_FAST_STARTUP_Msk (0x7FFFFu)
\r
75 /** PMC_WPMR Write Protect KEY, unlock it */
\r
76 #define PMC_WPMR_WPKEY_VALUE PMC_WPMR_WPKEY((uint32_t) 0x504D43)
\r
78 /** Using external oscillator */
\r
79 #define PMC_OSC_XTAL 0
\r
81 /** Oscillator in bypass mode */
\r
82 #define PMC_OSC_BYPASS 1
\r
84 #define PMC_PCK_0 0 /* PCK0 ID */
\r
85 #define PMC_PCK_1 1 /* PCK1 ID */
\r
86 #define PMC_PCK_2 2 /* PCK2 ID */
\r
88 #if SAM4S || SAM4E || SAM4N
\r
89 /** Flash state in Wait Mode */
\r
90 #define PMC_WAIT_MODE_FLASH_STANDBY PMC_FSMR_FLPM_FLASH_STANDBY
\r
91 #define PMC_WAIT_MODE_FLASH_DEEP_POWERDOWN PMC_FSMR_FLPM_FLASH_DEEP_POWERDOWN
\r
92 #define PMC_WAIT_MODE_FLASH_IDLE PMC_FSMR_FLPM_FLASH_IDLE
\r
95 /** Convert startup time from us to MOSCXTST */
\r
96 #define pmc_us_to_moscxtst(startup_us, slowck_freq) \
\r
97 ((startup_us * slowck_freq / 8 / 1000000) < 0x100 ? \
\r
98 (startup_us * slowck_freq / 8 / 1000000) : 0xFF)
\r
101 * \name Master clock (MCK) Source and Prescaler configuration
\r
103 * \note The following functions may be used to select the clock source and
\r
104 * prescaler for the master clock.
\r
108 void pmc_mck_set_prescaler(uint32_t ul_pres);
\r
109 void pmc_mck_set_source(uint32_t ul_source);
\r
110 uint32_t pmc_switch_mck_to_sclk(uint32_t ul_pres);
\r
111 uint32_t pmc_switch_mck_to_mainck(uint32_t ul_pres);
\r
112 uint32_t pmc_switch_mck_to_pllack(uint32_t ul_pres);
\r
113 #if (SAM3S || SAM4S)
\r
114 uint32_t pmc_switch_mck_to_pllbck(uint32_t ul_pres);
\r
116 #if (SAM3XA || SAM3U)
\r
117 uint32_t pmc_switch_mck_to_upllck(uint32_t ul_pres);
\r
119 #if (SAM4S || SAM4E || SAM4N)
\r
120 void pmc_set_flash_in_wait_mode(uint32_t ul_flash_state);
\r
127 * \name Slow clock (SLCK) oscillator and configuration
\r
132 void pmc_switch_sclk_to_32kxtal(uint32_t ul_bypass);
\r
133 uint32_t pmc_osc_is_ready_32kxtal(void);
\r
138 * \name Main Clock (MAINCK) oscillator and configuration
\r
143 void pmc_switch_mainck_to_fastrc(uint32_t ul_moscrcf);
\r
144 void pmc_osc_enable_fastrc(uint32_t ul_rc);
\r
145 void pmc_osc_disable_fastrc(void);
\r
146 uint32_t pmc_osc_is_ready_fastrc(void);
\r
147 void pmc_osc_enable_main_xtal(uint32_t ul_xtal_startup_time);
\r
148 void pmc_osc_bypass_main_xtal(void);
\r
149 void pmc_osc_disable_main_xtal(void);
\r
150 uint32_t pmc_osc_is_bypassed_main_xtal(void);
\r
151 uint32_t pmc_osc_is_ready_main_xtal(void);
\r
152 void pmc_switch_mainck_to_xtal(uint32_t ul_bypass,
\r
153 uint32_t ul_xtal_startup_time);
\r
154 void pmc_osc_disable_xtal(uint32_t ul_bypass);
\r
155 uint32_t pmc_osc_is_ready_mainck(void);
\r
156 void pmc_mainck_osc_select(uint32_t ul_xtal_rc);
\r
161 * \name PLL oscillator and configuration
\r
166 void pmc_enable_pllack(uint32_t mula, uint32_t pllacount, uint32_t diva);
\r
167 void pmc_disable_pllack(void);
\r
168 uint32_t pmc_is_locked_pllack(void);
\r
170 #if (SAM3S || SAM4S)
\r
171 void pmc_enable_pllbck(uint32_t mulb, uint32_t pllbcount, uint32_t divb);
\r
172 void pmc_disable_pllbck(void);
\r
173 uint32_t pmc_is_locked_pllbck(void);
\r
176 #if (SAM3XA || SAM3U)
\r
177 void pmc_enable_upll_clock(void);
\r
178 void pmc_disable_upll_clock(void);
\r
179 uint32_t pmc_is_locked_upll(void);
\r
185 * \name Peripherals clock configuration
\r
190 uint32_t pmc_enable_periph_clk(uint32_t ul_id);
\r
191 uint32_t pmc_disable_periph_clk(uint32_t ul_id);
\r
192 void pmc_enable_all_periph_clk(void);
\r
193 void pmc_disable_all_periph_clk(void);
\r
194 uint32_t pmc_is_periph_clk_enabled(uint32_t ul_id);
\r
199 * \name Programmable clock Source and Prescaler configuration
\r
201 * The following functions may be used to select the clock source and
\r
202 * prescaler for the specified programmable clock.
\r
206 void pmc_pck_set_prescaler(uint32_t ul_id, uint32_t ul_pres);
\r
207 void pmc_pck_set_source(uint32_t ul_id, uint32_t ul_source);
\r
208 uint32_t pmc_switch_pck_to_sclk(uint32_t ul_id, uint32_t ul_pres);
\r
209 uint32_t pmc_switch_pck_to_mainck(uint32_t ul_id, uint32_t ul_pres);
\r
210 uint32_t pmc_switch_pck_to_pllack(uint32_t ul_id, uint32_t ul_pres);
\r
211 #if (SAM3S || SAM4S)
\r
212 uint32_t pmc_switch_pck_to_pllbck(uint32_t ul_id, uint32_t ul_pres);
\r
214 #if (SAM3XA || SAM3U)
\r
215 uint32_t pmc_switch_pck_to_upllck(uint32_t ul_id, uint32_t ul_pres);
\r
217 void pmc_enable_pck(uint32_t ul_id);
\r
218 void pmc_disable_pck(uint32_t ul_id);
\r
219 void pmc_enable_all_pck(void);
\r
220 void pmc_disable_all_pck(void);
\r
221 uint32_t pmc_is_pck_enabled(uint32_t ul_id);
\r
226 * \name USB clock configuration
\r
231 #if (SAM3S || SAM3XA || SAM4S || SAM4E)
\r
232 void pmc_switch_udpck_to_pllack(uint32_t ul_usbdiv);
\r
234 #if (SAM3S || SAM4S)
\r
235 void pmc_switch_udpck_to_pllbck(uint32_t ul_usbdiv);
\r
238 void pmc_switch_udpck_to_upllck(uint32_t ul_usbdiv);
\r
240 #if (SAM3S || SAM3XA || SAM4S || SAM4E)
\r
241 void pmc_enable_udpck(void);
\r
242 void pmc_disable_udpck(void);
\r
248 * \name Interrupt and status management
\r
253 void pmc_enable_interrupt(uint32_t ul_sources);
\r
254 void pmc_disable_interrupt(uint32_t ul_sources);
\r
255 uint32_t pmc_get_interrupt_mask(void);
\r
256 uint32_t pmc_get_status(void);
\r
261 * \name Power management
\r
263 * The following functions are used to configure sleep mode and additional
\r
268 void pmc_set_fast_startup_input(uint32_t ul_inputs);
\r
269 void pmc_clr_fast_startup_input(uint32_t ul_inputs);
\r
270 void pmc_enable_sleepmode(uint8_t uc_type);
\r
271 void pmc_enable_waitmode(void);
\r
272 void pmc_enable_backupmode(void);
\r
277 * \name Failure detector
\r
282 void pmc_enable_clock_failure_detector(void);
\r
283 void pmc_disable_clock_failure_detector(void);
\r
289 * \name Slow Crystal Oscillator Frequency Monitoring
\r
294 void pmc_enable_sclk_osc_freq_monitor(void);
\r
295 void pmc_disable_sclk_osc_freq_monitor(void);
\r
301 * \name Write protection
\r
306 void pmc_set_writeprotect(uint32_t ul_enable);
\r
307 uint32_t pmc_get_writeprotect_status(void);
\r
322 * \page sam_pmc_quickstart Quick start guide for the SAM PMC module
\r
324 * This is the quick start guide for the \ref pmc_group "PMC module", with
\r
325 * step-by-step instructions on how to configure and use the driver in a
\r
326 * selection of use cases.
\r
328 * The use cases contain several code fragments. The code fragments in the
\r
329 * steps for setup can be copied into a custom initialization function, while
\r
330 * the steps for usage can be copied into, e.g., the main application function.
\r
332 * \section pmc_use_cases PMC use cases
\r
333 * - \ref pmc_basic_use_case Basic use case - Switch Main Clock sources
\r
334 * - \ref pmc_use_case_2 Advanced use case - Configure Programmable Clocks
\r
336 * \section pmc_basic_use_case Basic use case - Switch Main Clock sources
\r
337 * In this use case, the PMC module is configured for a variety of system clock
\r
338 * sources and speeds. A LED is used to visually indicate the current clock
\r
339 * speed as the source is switched.
\r
341 * \section pmc_basic_use_case_setup Setup
\r
343 * \subsection pmc_basic_use_case_setup_prereq Prerequisites
\r
344 * -# \ref gpio_group "General Purpose I/O Management (gpio)"
\r
346 * \subsection pmc_basic_use_case_setup_code Code
\r
347 * The following function needs to be added to the user application, to flash a
\r
348 * board LED a variable number of times at a rate given in CPU ticks.
\r
351 * #define FLASH_TICK_COUNT 0x00012345
\r
353 * void flash_led(uint32_t tick_count, uint8_t flash_count)
\r
355 * SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
\r
356 * SysTick->LOAD = tick_count;
\r
358 * while (flash_count--)
\r
360 * gpio_toggle_pin(LED0_GPIO);
\r
361 * while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
\r
362 * gpio_toggle_pin(LED0_GPIO);
\r
363 * while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
\r
368 * \section pmc_basic_use_case_usage Use case
\r
370 * \subsection pmc_basic_use_case_usage_code Example code
\r
371 * Add to application C-file:
\r
375 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
\r
376 * flash_led(FLASH_TICK_COUNT, 5);
\r
377 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
\r
378 * flash_led(FLASH_TICK_COUNT, 5);
\r
379 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
\r
380 * flash_led(FLASH_TICK_COUNT, 5);
\r
381 * pmc_switch_mainck_to_xtal(0);
\r
382 * flash_led(FLASH_TICK_COUNT, 5);
\r
386 * \subsection pmc_basic_use_case_usage_flow Workflow
\r
387 * -# Wrap the code in an infinite loop:
\r
391 * -# Switch the Master CPU frequency to the internal 12MHz RC oscillator, flash
\r
392 * a LED on the board several times:
\r
394 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
\r
395 * flash_led(FLASH_TICK_COUNT, 5);
\r
397 * -# Switch the Master CPU frequency to the internal 8MHz RC oscillator, flash
\r
398 * a LED on the board several times:
\r
400 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
\r
401 * flash_led(FLASH_TICK_COUNT, 5);
\r
403 * -# Switch the Master CPU frequency to the internal 4MHz RC oscillator, flash
\r
404 * a LED on the board several times:
\r
406 * pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
\r
407 * flash_led(FLASH_TICK_COUNT, 5);
\r
409 * -# Switch the Master CPU frequency to the external crystal oscillator, flash
\r
410 * a LED on the board several times:
\r
412 * pmc_switch_mainck_to_xtal(0, BOARD_OSC_STARTUP_US);
\r
413 * flash_led(FLASH_TICK_COUNT, 5);
\r
418 * \page pmc_use_case_2 Use case #2 - Configure Programmable Clocks
\r
419 * In this use case, the PMC module is configured to start the Slow Clock from
\r
420 * an attached 32KHz crystal, and start one of the Programmable Clock modules
\r
421 * sourced from the Slow Clock divided down with a prescale factor of 64.
\r
423 * \section pmc_use_case_2_setup Setup
\r
425 * \subsection pmc_use_case_2_setup_prereq Prerequisites
\r
426 * -# \ref pio_group "Parallel Input/Output Controller (pio)"
\r
428 * \subsection pmc_use_case_2_setup_code Code
\r
429 * The following code must be added to the user application:
\r
431 * pio_set_peripheral(PIOA, PIO_PERIPH_B, PIO_PA17);
\r
434 * \subsection pmc_use_case_2_setup_code_workflow Workflow
\r
435 * -# Configure the PCK1 pin to output on a specific port pin (in this case,
\r
436 * PIOA pin 17) of the microcontroller.
\r
438 * pio_set_peripheral(PIOA, PIO_PERIPH_B, PIO_PA17);
\r
440 * \note The peripheral selection and pin will vary according to your selected
\r
441 * SAM device model. Refer to the "Peripheral Signal Multiplexing on I/O
\r
442 * Lines" of your device's datasheet.
\r
444 * \section pmc_use_case_2_usage Use case
\r
445 * The generated PCK1 clock output can be viewed on an oscilloscope attached to
\r
446 * the correct pin of the microcontroller.
\r
448 * \subsection pmc_use_case_2_usage_code Example code
\r
449 * Add to application C-file:
\r
451 * pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);
\r
452 * pmc_switch_pck_to_sclk(PMC_PCK_1, PMC_PCK_PRES_CLK_64);
\r
453 * pmc_enable_pck(PMC_PCK_1);
\r
461 * \subsection pmc_use_case_2_usage_flow Workflow
\r
462 * -# Switch the Slow Clock source input to an external 32KHz crystal:
\r
464 * pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);
\r
466 * -# Switch the Programmable Clock module PCK1 source clock to the Slow Clock,
\r
467 * with a prescaler of 64:
\r
469 * pmc_switch_pck_to_sclk(PMC_PCK_1, PMC_PCK_PRES_CLK_64);
\r
471 * -# Enable Programmable Clock module PCK1:
\r
473 * pmc_enable_pck(PMC_PCK_1);
\r
475 * -# Enter an infinite loop:
\r
484 #endif /* PMC_H_INCLUDED */
\r