]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_ATSAM3X_Atmel_Studio/src/asf/sam/drivers/pmc/pmc.h
Add SAM3X-EK demo.
[freertos] / FreeRTOS / Demo / CORTEX_ATSAM3X_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-2012 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 /** PMC xtal startup time */\r
73 #define PMC_XTAL_STARTUP_TIME   (0x3F)\r
74 \r
75 /** Mask to access fast startup input */\r
76 #define PMC_FAST_STARTUP_Msk    (0x7FFFFu)\r
77 \r
78 /** PMC_WPMR Write Protect KEY, unlock it */\r
79 #define PMC_WPMR_WPKEY_VALUE    PMC_WPMR_WPKEY((uint32_t) 0x504D43)\r
80 \r
81 /** Using external oscillator */\r
82 #define PMC_OSC_XTAL            0\r
83 \r
84 /** Oscillator in bypass mode */\r
85 #define PMC_OSC_BYPASS          1\r
86 \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
90 \r
91 /**\r
92  * \name Master clock (MCK) Source and Prescaler configuration\r
93  *\r
94  * The following functions may be used to select the clock source and\r
95  * prescaler for the master clock.\r
96  */\r
97 //@{\r
98 \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
106 #endif\r
107 #if (SAM3XA || SAM3U)\r
108 uint32_t pmc_switch_mck_to_upllck(uint32_t ul_pres);\r
109 #endif\r
110 \r
111 //@}\r
112 \r
113 /**\r
114  * \name Slow clock (SLCK) oscillator and configuration\r
115  *\r
116  */\r
117 //@{\r
118 \r
119 void pmc_switch_sclk_to_32kxtal(uint32_t ul_bypass);\r
120 uint32_t pmc_osc_is_ready_32kxtal(void);\r
121 \r
122 //@}\r
123 \r
124 /**\r
125  * \name Main Clock (MAINCK) oscillator and configuration\r
126  *\r
127  */\r
128 //@{\r
129 \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
136 \r
137 //@}\r
138 \r
139 /**\r
140  * \name PLL oscillator and configuration\r
141  *\r
142  */\r
143 //@{\r
144 \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
148 \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
153 #endif\r
154 \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
159 #endif\r
160 \r
161 //@}\r
162 \r
163 /**\r
164  * \name Peripherals clock configuration\r
165  *\r
166  */\r
167 //@{\r
168 \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
174 \r
175 //@}\r
176 \r
177 /**\r
178  * \name Programmable clock Source and Prescaler configuration\r
179  *\r
180  * The following functions may be used to select the clock source and\r
181  * prescaler for the specified programmable clock.\r
182  */\r
183 //@{\r
184 \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
192 #endif\r
193 #if (SAM3XA || SAM3U)\r
194 uint32_t pmc_switch_pck_to_upllck(uint32_t ul_id, uint32_t ul_pres);\r
195 #endif\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
201 \r
202 //@}\r
203 \r
204 /**\r
205  * \name USB clock configuration\r
206  *\r
207  */\r
208 //@{\r
209 \r
210 #if (SAM3S || SAM3XA || SAM4S)\r
211 void pmc_switch_udpck_to_pllack(uint32_t ul_usbdiv);\r
212 #endif\r
213 #if (SAM3S || SAM4S)\r
214 void pmc_switch_udpck_to_pllbck(uint32_t ul_usbdiv);\r
215 #endif\r
216 #if (SAM3XA)\r
217 void pmc_switch_udpck_to_upllck(uint32_t ul_usbdiv);\r
218 #endif\r
219 #if (SAM3S || SAM3XA || SAM4S)\r
220 void pmc_enable_udpck(void);\r
221 void pmc_disable_udpck(void);\r
222 #endif\r
223 \r
224 //@}\r
225 \r
226 /**\r
227  * \name Interrupt and status management\r
228  *\r
229  */\r
230 //@{\r
231 \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
236 \r
237 //@}\r
238 \r
239 /**\r
240  * \name Power management\r
241  *\r
242  * The following functions are used to configure sleep mode and additional \r
243  * wake up inputs.\r
244  */\r
245 //@{\r
246 \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
252 \r
253 //@}\r
254 \r
255 /**\r
256  * \name Failure detector\r
257  *\r
258  */\r
259 //@{\r
260 \r
261 void pmc_enable_clock_failure_detector(void);\r
262 void pmc_disable_clock_failure_detector(void);\r
263 \r
264 //@}\r
265 \r
266 /**\r
267  * \name Write protection\r
268  *\r
269  */\r
270 //@{\r
271 \r
272 void pmc_set_writeprotect(uint32_t ul_enable);\r
273 uint32_t pmc_get_writeprotect_status(void);\r
274 \r
275 //@}\r
276 \r
277 /// @cond 0\r
278 /**INDENT-OFF**/\r
279 #ifdef __cplusplus\r
280 }\r
281 #endif\r
282 /**INDENT-ON**/\r
283 /// @endcond\r
284 \r
285 //! @}\r
286 \r
287 /**\r
288  * \page sam_pmc_quickstart Quick start guide for the SAM PMC module\r
289  *\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
293  *\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
297  *\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
301  *\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
306  *\r
307  * \section pmc_basic_use_case_setup Setup\r
308  *\r
309  * \subsection pmc_basic_use_case_setup_prereq Prerequisites\r
310  * -# \ref gpio_group "General Purpose I/O Management (gpio)"\r
311  *\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
315  * \r
316  * \code\r
317  * #define FLASH_TICK_COUNT   0x00012345\r
318  *\r
319  * void flash_led(uint32_t tick_count, uint8_t flash_count)\r
320  * {\r
321  *     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;\r
322  *     SysTick->LOAD = tick_count;\r
323  * \r
324  *     while (flash_count--)\r
325  *     {\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
330  *     }\r
331  * }\r
332  * \endcode\r
333  *\r
334  * \section pmc_basic_use_case_usage Use case\r
335  *\r
336  * \subsection pmc_basic_use_case_usage_code Example code\r
337  * Add to application C-file:\r
338  * \code\r
339  *    for (;;)\r
340  *    {\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
349  *    }\r
350  * \endcode\r
351  *\r
352  * \subsection pmc_basic_use_case_usage_flow Workflow\r
353  * -# Wrap the code in an infinite loop:\r
354  *   \code\r
355  *   for (;;)\r
356  *   \endcode\r
357  * -# Switch the Master CPU frequency to the internal 12MHz RC oscillator, flash\r
358  *    a LED on the board several times:\r
359  *   \code\r
360  *   pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);\r
361  *   flash_led(FLASH_TICK_COUNT, 5); \r
362  *   \endcode\r
363  * -# Switch the Master CPU frequency to the internal 8MHz RC oscillator, flash\r
364  *    a LED on the board several times:\r
365  *   \code\r
366  *   pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);\r
367  *   flash_led(FLASH_TICK_COUNT, 5); \r
368  *   \endcode\r
369  * -# Switch the Master CPU frequency to the internal 4MHz RC oscillator, flash\r
370  *    a LED on the board several times:\r
371  *   \code\r
372  *   pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);\r
373  *   flash_led(FLASH_TICK_COUNT, 5); \r
374  *   \endcode\r
375  * -# Switch the Master CPU frequency to the external crystal oscillator, flash\r
376  *    a LED on the board several times:\r
377  *   \code\r
378  *   pmc_switch_mainck_to_xtal(0);\r
379  *   flash_led(FLASH_TICK_COUNT, 5); \r
380  *   \endcode\r
381  */\r
382 \r
383 /**\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
388  *\r
389  * \section pmc_use_case_2_setup Setup\r
390  *\r
391  * \subsection pmc_use_case_2_setup_prereq Prerequisites\r
392  * -# \ref pio_group "Parallel Input/Output Controller (pio)"\r
393  *\r
394  * \subsection pmc_use_case_2_setup_code Code\r
395  * The following code must be added to the user application:\r
396  * \code\r
397  *      pio_set_peripheral(PIOA, PIO_PERIPH_B, PIO_PA17);\r
398  * \endcode\r
399  *\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
403  *   \code\r
404  *   pio_set_peripheral(PIOA, PIO_PERIPH_B, PIO_PA17);\r
405  *   \endcode\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
409  *\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
413  *\r
414  * \subsection pmc_use_case_2_usage_code Example code\r
415  * Add to application C-file:\r
416  * \code\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
420  *\r
421  *  for (;;)\r
422  *  {\r
423  *      // Do Nothing\r
424  *  }\r
425  * \endcode\r
426  *\r
427  * \subsection pmc_use_case_2_usage_flow Workflow\r
428  * -# Switch the Slow Clock source input to an external 32KHz crystal:\r
429  *   \code\r
430  *   pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);\r
431  *   \endcode\r
432  * -# Switch the Programmable Clock module PCK1 source clock to the Slow Clock,\r
433  *    with a prescaler of 64:\r
434  *   \code\r
435  *   pmc_switch_pck_to_sclk(PMC_PCK_1, PMC_PCK_PRES_CLK_64);\r
436  *   \endcode\r
437  * -# Enable Programmable Clock module PCK1:\r
438  *   \code\r
439  *   pmc_enable_pck(PMC_PCK_1);\r
440  *   \endcode\r
441  * -# Enter an infinite loop:\r
442  *   \code\r
443  *   for (;;)\r
444  *   {\r
445  *      // Do Nothing\r
446  *   }\r
447  *   \endcode\r
448  */\r
449 \r
450 #endif /* PMC_H_INCLUDED */\r