]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4E_Atmel_Studio/src/ASF/sam/drivers/pmc/pmc.h
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4E_Atmel_Studio / src / ASF / sam / drivers / pmc / pmc.h
1 /**\r
2  * \file\r
3  *\r
4  * \brief Power Management Controller (PMC) driver for SAM.\r
5  *\r
6  * Copyright (c) 2011 - 2013 Atmel Corporation. All rights reserved.\r
7  *\r
8  * \asf_license_start\r
9  *\r
10  * \page License\r
11  *\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
14  *\r
15  * 1. Redistributions of source code must retain the above copyright notice,\r
16  *    this list of conditions and the following disclaimer.\r
17  *\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
21  *\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
24  *\r
25  * 4. This software may only be redistributed and used in connection with an\r
26  *    Atmel microcontroller product.\r
27  *\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
39  *\r
40  * \asf_license_stop\r
41  *\r
42  */\r
43 \r
44 #ifndef PMC_H_INCLUDED\r
45 #define PMC_H_INCLUDED\r
46 \r
47 #include "compiler.h"\r
48 \r
49 /// @cond 0\r
50 /**INDENT-OFF**/\r
51 #ifdef __cplusplus\r
52 extern "C" {\r
53 #endif\r
54 /**INDENT-ON**/\r
55 /// @endcond\r
56 \r
57 /** Bit mask for peripheral clocks (PCER0) */\r
58 #define PMC_MASK_STATUS0        (0xFFFFFFFC)\r
59 \r
60 /** Bit mask for peripheral clocks (PCER1) */\r
61 #define PMC_MASK_STATUS1        (0xFFFFFFFF)\r
62 \r
63 /** Loop counter timeout value */\r
64 #define PMC_TIMEOUT             (2048)\r
65 \r
66 /** Key to unlock CKGR_MOR register */\r
67 #define PMC_CKGR_MOR_KEY_VALUE  CKGR_MOR_KEY(0x37)\r
68 \r
69 /** Key used to write SUPC registers */\r
70 #define SUPC_KEY_VALUE          ((uint32_t) 0xA5)\r
71 \r
72 /** Mask to access fast startup input */\r
73 #define PMC_FAST_STARTUP_Msk    (0x7FFFFu)\r
74 \r
75 /** PMC_WPMR Write Protect KEY, unlock it */\r
76 #define PMC_WPMR_WPKEY_VALUE    PMC_WPMR_WPKEY((uint32_t) 0x504D43)\r
77 \r
78 /** Using external oscillator */\r
79 #define PMC_OSC_XTAL            0\r
80 \r
81 /** Oscillator in bypass mode */\r
82 #define PMC_OSC_BYPASS          1\r
83 \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
87 \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
93 #endif\r
94 \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
99 \r
100 /**\r
101  * \name Master clock (MCK) Source and Prescaler configuration\r
102  *\r
103  * \note The following functions may be used to select the clock source and\r
104  * prescaler for the master clock.\r
105  */\r
106 //@{\r
107 \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
115 #endif\r
116 #if (SAM3XA || SAM3U)\r
117 uint32_t pmc_switch_mck_to_upllck(uint32_t ul_pres);\r
118 #endif\r
119 #if (SAM4S || SAM4E || SAM4N)\r
120 void pmc_set_flash_in_wait_mode(uint32_t ul_flash_state);\r
121 #endif\r
122 \r
123 \r
124 //@}\r
125 \r
126 /**\r
127  * \name Slow clock (SLCK) oscillator and configuration\r
128  *\r
129  */\r
130 //@{\r
131 \r
132 void pmc_switch_sclk_to_32kxtal(uint32_t ul_bypass);\r
133 uint32_t pmc_osc_is_ready_32kxtal(void);\r
134 \r
135 //@}\r
136 \r
137 /**\r
138  * \name Main Clock (MAINCK) oscillator and configuration\r
139  *\r
140  */\r
141 //@{\r
142 \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
157 \r
158 //@}\r
159 \r
160 /**\r
161  * \name PLL oscillator and configuration\r
162  *\r
163  */\r
164 //@{\r
165 \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
169 \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
174 #endif\r
175 \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
180 #endif\r
181 \r
182 //@}\r
183 \r
184 /**\r
185  * \name Peripherals clock configuration\r
186  *\r
187  */\r
188 //@{\r
189 \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
195 \r
196 //@}\r
197 \r
198 /**\r
199  * \name Programmable clock Source and Prescaler configuration\r
200  *\r
201  * The following functions may be used to select the clock source and\r
202  * prescaler for the specified programmable clock.\r
203  */\r
204 //@{\r
205 \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
213 #endif\r
214 #if (SAM3XA || SAM3U)\r
215 uint32_t pmc_switch_pck_to_upllck(uint32_t ul_id, uint32_t ul_pres);\r
216 #endif\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
222 \r
223 //@}\r
224 \r
225 /**\r
226  * \name USB clock configuration\r
227  *\r
228  */\r
229 //@{\r
230 \r
231 #if (SAM3S || SAM3XA || SAM4S || SAM4E)\r
232 void pmc_switch_udpck_to_pllack(uint32_t ul_usbdiv);\r
233 #endif\r
234 #if (SAM3S || SAM4S)\r
235 void pmc_switch_udpck_to_pllbck(uint32_t ul_usbdiv);\r
236 #endif\r
237 #if (SAM3XA)\r
238 void pmc_switch_udpck_to_upllck(uint32_t ul_usbdiv);\r
239 #endif\r
240 #if (SAM3S || SAM3XA || SAM4S || SAM4E)\r
241 void pmc_enable_udpck(void);\r
242 void pmc_disable_udpck(void);\r
243 #endif\r
244 \r
245 //@}\r
246 \r
247 /**\r
248  * \name Interrupt and status management\r
249  *\r
250  */\r
251 //@{\r
252 \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
257 \r
258 //@}\r
259 \r
260 /**\r
261  * \name Power management\r
262  *\r
263  * The following functions are used to configure sleep mode and additional\r
264  * wake up inputs.\r
265  */\r
266 //@{\r
267 \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
273 \r
274 //@}\r
275 \r
276 /**\r
277  * \name Failure detector\r
278  *\r
279  */\r
280 //@{\r
281 \r
282 void pmc_enable_clock_failure_detector(void);\r
283 void pmc_disable_clock_failure_detector(void);\r
284 \r
285 //@}\r
286 \r
287 #if SAM4N\r
288 /**\r
289  * \name Slow Crystal Oscillator Frequency Monitoring\r
290  *\r
291  */\r
292 //@{\r
293 \r
294 void pmc_enable_sclk_osc_freq_monitor(void);\r
295 void pmc_disable_sclk_osc_freq_monitor(void);\r
296 \r
297 //@}\r
298 #endif\r
299 \r
300 /**\r
301  * \name Write protection\r
302  *\r
303  */\r
304 //@{\r
305 \r
306 void pmc_set_writeprotect(uint32_t ul_enable);\r
307 uint32_t pmc_get_writeprotect_status(void);\r
308 \r
309 //@}\r
310 \r
311 /// @cond 0\r
312 /**INDENT-OFF**/\r
313 #ifdef __cplusplus\r
314 }\r
315 #endif\r
316 /**INDENT-ON**/\r
317 /// @endcond\r
318 \r
319 //! @}\r
320 \r
321 /**\r
322  * \page sam_pmc_quickstart Quick start guide for the SAM PMC module\r
323  *\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
327  *\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
331  *\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
335  *\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
340  *\r
341  * \section pmc_basic_use_case_setup Setup\r
342  *\r
343  * \subsection pmc_basic_use_case_setup_prereq Prerequisites\r
344  * -# \ref gpio_group "General Purpose I/O Management (gpio)"\r
345  *\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
349  *\r
350  * \code\r
351  * #define FLASH_TICK_COUNT   0x00012345\r
352  *\r
353  * void flash_led(uint32_t tick_count, uint8_t flash_count)\r
354  * {\r
355  *     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;\r
356  *     SysTick->LOAD = tick_count;\r
357  *\r
358  *     while (flash_count--)\r
359  *     {\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
364  *     }\r
365  * }\r
366  * \endcode\r
367  *\r
368  * \section pmc_basic_use_case_usage Use case\r
369  *\r
370  * \subsection pmc_basic_use_case_usage_code Example code\r
371  * Add to application C-file:\r
372  * \code\r
373  *    for (;;)\r
374  *    {\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
383  *    }\r
384  * \endcode\r
385  *\r
386  * \subsection pmc_basic_use_case_usage_flow Workflow\r
387  * -# Wrap the code in an infinite loop:\r
388  *   \code\r
389  *   for (;;)\r
390  *   \endcode\r
391  * -# Switch the Master CPU frequency to the internal 12MHz RC oscillator, flash\r
392  *    a LED on the board several times:\r
393  *   \code\r
394  *   pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);\r
395  *   flash_led(FLASH_TICK_COUNT, 5);\r
396  *   \endcode\r
397  * -# Switch the Master CPU frequency to the internal 8MHz RC oscillator, flash\r
398  *    a LED on the board several times:\r
399  *   \code\r
400  *   pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);\r
401  *   flash_led(FLASH_TICK_COUNT, 5);\r
402  *   \endcode\r
403  * -# Switch the Master CPU frequency to the internal 4MHz RC oscillator, flash\r
404  *    a LED on the board several times:\r
405  *   \code\r
406  *   pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);\r
407  *   flash_led(FLASH_TICK_COUNT, 5);\r
408  *   \endcode\r
409  * -# Switch the Master CPU frequency to the external crystal oscillator, flash\r
410  *    a LED on the board several times:\r
411  *   \code\r
412  *   pmc_switch_mainck_to_xtal(0, BOARD_OSC_STARTUP_US);\r
413  *   flash_led(FLASH_TICK_COUNT, 5);\r
414  *   \endcode\r
415  */\r
416 \r
417 /**\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
422  *\r
423  * \section pmc_use_case_2_setup Setup\r
424  *\r
425  * \subsection pmc_use_case_2_setup_prereq Prerequisites\r
426  * -# \ref pio_group "Parallel Input/Output Controller (pio)"\r
427  *\r
428  * \subsection pmc_use_case_2_setup_code Code\r
429  * The following code must be added to the user application:\r
430  * \code\r
431  *      pio_set_peripheral(PIOA, PIO_PERIPH_B, PIO_PA17);\r
432  * \endcode\r
433  *\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
437  *   \code\r
438  *   pio_set_peripheral(PIOA, PIO_PERIPH_B, PIO_PA17);\r
439  *   \endcode\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
443  *\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
447  *\r
448  * \subsection pmc_use_case_2_usage_code Example code\r
449  * Add to application C-file:\r
450  * \code\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
454  *\r
455  *  for (;;)\r
456  *  {\r
457  *      // Do Nothing\r
458  *  }\r
459  * \endcode\r
460  *\r
461  * \subsection pmc_use_case_2_usage_flow Workflow\r
462  * -# Switch the Slow Clock source input to an external 32KHz crystal:\r
463  *   \code\r
464  *   pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);\r
465  *   \endcode\r
466  * -# Switch the Programmable Clock module PCK1 source clock to the Slow Clock,\r
467  *    with a prescaler of 64:\r
468  *   \code\r
469  *   pmc_switch_pck_to_sclk(PMC_PCK_1, PMC_PCK_PRES_CLK_64);\r
470  *   \endcode\r
471  * -# Enable Programmable Clock module PCK1:\r
472  *   \code\r
473  *   pmc_enable_pck(PMC_PCK_1);\r
474  *   \endcode\r
475  * -# Enter an infinite loop:\r
476  *   \code\r
477  *   for (;;)\r
478  *   {\r
479  *      // Do Nothing\r
480  *   }\r
481  *   \endcode\r
482  */\r
483 \r
484 #endif /* PMC_H_INCLUDED */\r