]> git.sur5r.net Git - freertos/blob - Demo/AVR32_UC3A_GCC/Atmel_SW_Framework/DRIVERS/PM/power_clocks_lib.c
Temporarily revert the AVR32 port back to the V6.0.5 files. Work will continue on...
[freertos] / Demo / AVR32_UC3A_GCC / Atmel_SW_Framework / DRIVERS / PM / power_clocks_lib.c
1 /* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */\r
2
3 /*This file has been prepared for Doxygen automatic documentation generation.*/\r
4 /*! \file *********************************************************************\r
5  *\r
6  * \brief High-level library abstracting features such as oscillators/pll/dfll\r
7  *        configuration, clock configuration, System-sensible parameters\r
8  *        configuration, buses clocks configuration, sleep mode, reset.\r
9  *\r
10  *\r
11  * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32\r
12  * - Supported devices:  All AVR32 devices.\r
13  * - AppNote:\r
14  *\r
15  * \author               Atmel Corporation: http://www.atmel.com \n\r
16  *                       Support and FAQ: http://support.atmel.no/\r
17  *\r
18  *****************************************************************************/\r
19 \r
20 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.\r
21  *\r
22  * Redistribution and use in source and binary forms, with or without\r
23  * modification, are permitted provided that the following conditions are met:\r
24  *\r
25  * 1. Redistributions of source code must retain the above copyright notice, this\r
26  * list of conditions and the following disclaimer.\r
27  *\r
28  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
29  * this list of conditions and the following disclaimer in the documentation\r
30  * and/or other materials provided with the distribution.\r
31  *\r
32  * 3. The name of Atmel may not be used to endorse or promote products derived\r
33  * from this software without specific prior written permission.\r
34  *\r
35  * 4. This software may only be redistributed and used in connection with an Atmel\r
36  * AVR product.\r
37  *\r
38  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED\r
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
40  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
41  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR\r
42  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\r
48  *\r
49  */\r
50 #include "power_clocks_lib.h"\r
51 \r
52 \r
53 //! Device-specific data\r
54 #if UC3L\r
55 static long int pcl_configure_clocks_uc3l(pcl_freq_param_t *param); // FORWARD declaration\r
56 #endif\r
57 \r
58 #if UC3C\r
59 static long int pcl_configure_clocks_uc3c(pcl_freq_param_t *param); // FORWARD declaration\r
60 #endif\r
61 \r
62 long int pcl_configure_clocks(pcl_freq_param_t *param)\r
63 {\r
64 #ifndef AVR32_PM_VERSION_RESETVALUE\r
65   // Implementation for UC3A, UC3A3, UC3B parts.\r
66   return(pm_configure_clocks(param));\r
67 #else\r
68   #ifdef AVR32_PM_410_H_INCLUDED\r
69     // Implementation for UC3C parts.\r
70     return(pcl_configure_clocks_uc3c(param));\r
71   #else\r
72     // Implementation for UC3L parts.\r
73     return(pcl_configure_clocks_uc3l(param));\r
74   #endif\r
75 #endif\r
76 }\r
77 \r
78 \r
79 //! Device-specific implementation\r
80 #if UC3L\r
81 // FORWARD declaration\r
82 static long int pcl_configure_synchronous_clocks( pm_clk_src_t main_clk_src,\r
83                                                   unsigned long main_clock_freq_hz,\r
84                                                   pcl_freq_param_t *param);\r
85 \r
86 long int pcl_configure_clocks_rcsys(pcl_freq_param_t *param)\r
87 {\r
88   // Supported main clock sources: PCL_MC_RCSYS\r
89 \r
90   // Supported synchronous clocks frequencies if RCSYS is the main clock source:\r
91   // 115200Hz, 57600Hz, 28800Hz, 14400Hz, 7200Hz, 3600Hz, 1800Hz, 900Hz, 450Hz.\r
92 \r
93   // NOTE: by default, this implementation doesn't perform thorough checks on the\r
94   // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.\r
95 \r
96 #ifdef AVR32SFW_INPUT_CHECK\r
97   // Verify that fCPU >= fPBx\r
98   if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))\r
99     return(-1);\r
100 #endif\r
101 \r
102 #ifdef AVR32SFW_INPUT_CHECK\r
103     // Verify that the target frequencies are reachable.\r
104     if((param->cpu_f > SCIF_SLOWCLOCK_FREQ_HZ) || (param->pba_f > SCIF_SLOWCLOCK_FREQ_HZ)\r
105       || (param->pbb_f > SCIF_SLOWCLOCK_FREQ_HZ))\r
106       return(-1);\r
107 #endif\r
108 \r
109   return(pcl_configure_synchronous_clocks(PM_CLK_SRC_SLOW, SCIF_SLOWCLOCK_FREQ_HZ, param));\r
110 }\r
111 \r
112 \r
113 long int pcl_configure_clocks_rc120m(pcl_freq_param_t *param)\r
114 {\r
115   // Supported main clock sources: PCL_MC_RC120M\r
116 \r
117   // Supported synchronous clocks frequencies if RC120M is the main clock source:\r
118   // 30MHz, 15MHz, 7.5MHz, 3.75MHz, 1.875MHz, 937.5kHz, 468.75kHz.\r
119 \r
120   // NOTE: by default, this implementation doesn't perform thorough checks on the\r
121   // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.\r
122 \r
123 #ifdef AVR32SFW_INPUT_CHECK\r
124   // Verify that fCPU >= fPBx\r
125   if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))\r
126     return(-1);\r
127 #endif\r
128 \r
129 #ifdef AVR32SFW_INPUT_CHECK\r
130   // Verify that the target frequencies are reachable.\r
131   if((param->cpu_f > SCIF_RC120M_FREQ_HZ) || (param->pba_f > SCIF_RC120M_FREQ_HZ)\r
132     || (param->pbb_f > SCIF_RC120M_FREQ_HZ))\r
133     return(-1);\r
134 #endif\r
135 \r
136   // Start the 120MHz internal RCosc (RC120M) clock\r
137   scif_start_rc120M();\r
138 \r
139   return(pcl_configure_synchronous_clocks(PM_CLK_SRC_RC120M, SCIF_RC120M_FREQ_HZ, param));\r
140 }\r
141 \r
142 \r
143 long int pcl_configure_clocks_osc0(pcl_freq_param_t *param)\r
144 {\r
145   // Supported main clock sources: PCL_MC_OSC0\r
146 \r
147   // Supported synchronous clocks frequencies if OSC0 is the main clock source:\r
148   // (these obviously depend on the OSC0 frequency; we'll take 16MHz as an example)\r
149   // 16MHz, 8MHz, 4MHz, 2MHz, 1MHz, 500kHz, 250kHz, 125kHz, 62.5kHz.\r
150 \r
151   // NOTE: by default, this implementation doesn't perform thorough checks on the\r
152   // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.\r
153 \r
154   unsigned long               main_clock_freq;\r
155 \r
156 \r
157 #ifdef AVR32SFW_INPUT_CHECK\r
158   // Verify that fCPU >= fPBx\r
159   if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))\r
160     return(-1);\r
161 #endif\r
162 \r
163   main_clock_freq = param->osc0_f;\r
164 #ifdef AVR32SFW_INPUT_CHECK\r
165   // Verify that the target frequencies are reachable.\r
166   if((param->cpu_f > main_clock_freq) || (param->pba_f > main_clock_freq)\r
167     || (param->pbb_f > main_clock_freq))\r
168     return(-1);\r
169 #endif\r
170   // Configure OSC0 in crystal mode, external crystal with a fcrystal Hz frequency.\r
171   scif_configure_osc_crystalmode(SCIF_OSC0, main_clock_freq);\r
172   // Enable the OSC0\r
173   scif_enable_osc(SCIF_OSC0, param->osc0_startup, true);\r
174 \r
175   return(pcl_configure_synchronous_clocks(PM_CLK_SRC_OSC0, main_clock_freq, param));\r
176 }\r
177 \r
178 \r
179 long int pcl_configure_clocks_dfll0(pcl_freq_param_t *param)\r
180 {\r
181   // Supported main clock sources: PCL_MC_DFLL\r
182 \r
183   // Supported synchronous clocks frequencies if DFLL is the main clock source:\r
184   // (these obviously depend on the DFLL target frequency; we'll take 100MHz as an example)\r
185   // 50MHz, 25MHz, 12.5MHz, 6.25MHz, 3.125MHz, 1562.5kHz, 781.25kHz, 390.625kHz.\r
186 \r
187   // NOTE: by default, this implementation doesn't perform thorough checks on the\r
188   // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.\r
189 \r
190   unsigned long   main_clock_freq;\r
191   scif_gclk_opt_t *pgc_dfllif_ref_opt;\r
192 \r
193 \r
194 #ifdef AVR32SFW_INPUT_CHECK\r
195   // Verify that fCPU >= fPBx\r
196   if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))\r
197     return(-1);\r
198 #endif\r
199 \r
200   main_clock_freq = param->dfll_f;\r
201 #ifdef AVR32SFW_INPUT_CHECK\r
202   // Verify that the target DFLL output frequency is in the correct range.\r
203   if((main_clock_freq > SCIF_DFLL_MAXFREQ_HZ) || (main_clock_freq < SCIF_DFLL_MINFREQ_HZ))\r
204     return(-1);\r
205   // Verify that the target frequencies are reachable.\r
206   if((param->cpu_f > main_clock_freq) || (param->pba_f > main_clock_freq)\r
207     || (param->pbb_f > main_clock_freq))\r
208     return(-1);\r
209 #endif\r
210   pgc_dfllif_ref_opt = (scif_gclk_opt_t *)param->pextra_params;\r
211   // Implementation note: this implementation configures the DFLL in closed-loop\r
212   // mode (because it gives the best accuracy) which enables the generic clock CLK_DFLLIF_REF\r
213   // as a reference (RCSYS being used as the generic clock source, undivided).\r
214   scif_dfll0_closedloop_configure_and_start(pgc_dfllif_ref_opt, main_clock_freq, TRUE);\r
215 \r
216   return(pcl_configure_synchronous_clocks(PM_CLK_SRC_DFLL0, main_clock_freq, param));\r
217 }\r
218 \r
219 \r
220 static long int pcl_configure_clocks_uc3l(pcl_freq_param_t *param)\r
221 {\r
222   // Supported main clock sources: PCL_MC_RCSYS, PCL_MC_OSC0, PCL_MC_DFLL0, PCL_MC_RC120M\r
223 \r
224   // Supported synchronous clocks frequencies if RCSYS is the main clock source:\r
225   // 115200Hz, 57600Hz, 28800Hz, 14400Hz, 7200Hz, 3600Hz, 1800Hz, 900Hz, 450Hz.\r
226 \r
227   // Supported synchronous clocks frequencies if RC120M is the main clock source:\r
228   // 30MHz, 15MHz, 7.5MHz, 3.75MHz, 1.875MHz, 937.5kHz, 468.75kHz.\r
229 \r
230   // Supported synchronous clocks frequencies if OSC0 is the main clock source:\r
231   // (these obviously depend on the OSC0 frequency; we'll take 16MHz as an example)\r
232   // 16MHz, 8MHz, 4MHz, 2MHz, 1MHz, 500kHz, 250kHz, 125kHz, 62.5kHz.\r
233 \r
234   // Supported synchronous clocks frequencies if DFLL is the main clock source:\r
235   // (these obviously depend on the DFLL target frequency; we'll take 100MHz as an example)\r
236   // 50MHz, 25MHz, 12.5MHz, 6.25MHz, 3.125MHz, 1562.5kHz, 781.25kHz, 390.625kHz.\r
237 \r
238   // NOTE: by default, this implementation doesn't perform thorough checks on the\r
239   // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.\r
240 \r
241 \r
242 #ifdef AVR32SFW_INPUT_CHECK\r
243   // Verify that fCPU >= fPBx\r
244   if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))\r
245     return(-1);\r
246 #endif\r
247 \r
248   if(PCL_MC_RCSYS == param->main_clk_src)\r
249   {\r
250     return(pcl_configure_clocks_rcsys(param));\r
251   }\r
252   else if(PCL_MC_RC120M == param->main_clk_src)\r
253   {\r
254     return(pcl_configure_clocks_rc120m(param));\r
255   }\r
256   else if(PCL_MC_OSC0 == param->main_clk_src)\r
257   {\r
258     return(pcl_configure_clocks_osc0(param));\r
259   }\r
260   else // PCL_MC_DFLL0 == param->main_clk_src\r
261   {\r
262     return(pcl_configure_clocks_dfll0(param));\r
263   }\r
264 }\r
265 \r
266 static long int pcl_configure_synchronous_clocks(pm_clk_src_t main_clk_src, unsigned long main_clock_freq_hz, pcl_freq_param_t *param)\r
267 {\r
268   //#\r
269   //# Set the Synchronous clock division ratio for each clock domain\r
270   //#\r
271   pm_set_all_cksel(main_clock_freq_hz, param->cpu_f, param->pba_f, param->pbb_f);\r
272 \r
273   //#\r
274   //# Set the Flash wait state and the speed read mode (depending on the target CPU frequency).\r
275   //#\r
276 #if UC3L\r
277     flashcdw_set_flash_waitstate_and_readmode(param->cpu_f);\r
278 #elif UC3C\r
279     flashc_set_flash_waitstate_and_readmode(param->cpu_f);\r
280 #endif\r
281 \r
282 \r
283   //#\r
284   //# Switch the main clock source to the selected clock.\r
285   //#\r
286   pm_set_mclk_source(main_clk_src);\r
287 \r
288   return PASS;\r
289 }\r
290 \r
291 #endif // UC3L device-specific implementation\r
292 \r
293 //! UC3C Device-specific implementation\r
294 #if UC3C\r
295 static long int pcl_configure_clocks_uc3c(pcl_freq_param_t *param)\r
296 {\r
297   #define PM_MAX_MUL                         ((1 << AVR32_SCIF_PLLMUL_SIZE) - 1)\r
298   #define AVR32_PM_PBA_MAX_FREQ              66000000\r
299   #define AVR32_PM_PLL_VCO_RANGE0_MAX_FREQ   240000000\r
300   #define AVR32_PM_PLL_VCO_RANGE0_MIN_FREQ   160000000\r
301 \r
302     // Implementation for  UC3C parts.\r
303         // Supported frequencies:\r
304         // Fosc0 mul div PLL div2_en cpu_f pba_f   Comment\r
305         //  12   15   1  192     1     12    12\r
306         //  12    9   3   40     1     20    20    PLL out of spec\r
307         //  12   15   1  192     1     24    12\r
308         //  12    9   1  120     1     30    15\r
309         //  12    9   3   40     0     40    20    PLL out of spec\r
310         //  12   15   1  192     1     48    12\r
311         //  12   15   1  192     1     48    24\r
312         //  12    8   1  108     1     54    27\r
313         //  12    9   1  120     1     60    15\r
314         //  12    9   1  120     1     60    30\r
315         //  12   10   1  132     1     66    16.5\r
316         //\r
317         unsigned long in_cpu_f  = param->cpu_f;\r
318         unsigned long in_osc0_f = param->osc0_f;\r
319         unsigned long mul, div, div2_en = 0, div2_cpu = 0, div2_pba = 0;\r
320         unsigned long pll_freq, rest;\r
321         Bool b_div2_pba, b_div2_cpu;\r
322 \r
323         // Configure OSC0 in crystal mode, external crystal with a FOSC0 Hz frequency.\r
324         scif_configure_osc_crystalmode(SCIF_OSC0, in_osc0_f);\r
325         // Enable the OSC0\r
326         scif_enable_osc(SCIF_OSC0, param->osc0_startup, true);\r
327         // Set the main clock source as being OSC0.\r
328         pm_set_mclk_source(PM_CLK_SRC_OSC0);\r
329 \r
330         // Start with CPU freq config\r
331         if (in_cpu_f == in_osc0_f)\r
332         {\r
333           param->cpu_f = in_osc0_f;\r
334           param->pba_f = in_osc0_f;\r
335           return PASS;\r
336         }\r
337         else if (in_cpu_f < in_osc0_f)\r
338         {\r
339           // TBD\r
340         }\r
341 \r
342         rest = in_cpu_f % in_osc0_f;\r
343 \r
344         for (div = 1; div < 32; div++)\r
345         {\r
346           if ((div * rest) % in_osc0_f == 0)\r
347             break;\r
348         }\r
349         if (div == 32)\r
350           return FAIL;\r
351 \r
352         mul = (in_cpu_f * div) / in_osc0_f;\r
353 \r
354         if (mul > PM_MAX_MUL)\r
355           return FAIL;\r
356 \r
357         // export 2power from PLL div to div2_cpu\r
358         while (!(div % 2))\r
359         {\r
360           div /= 2;\r
361           div2_cpu++;\r
362         }\r
363 \r
364         // Here we know the mul and div parameter of the PLL config.\r
365         // . Check out if the PLL has a valid in_cpu_f.\r
366         // . Try to have for the PLL frequency (VCO output) the highest possible value\r
367         //   to reduce jitter.\r
368         while (in_osc0_f * 2 * mul / div < AVR32_PM_PLL_VCO_RANGE0_MAX_FREQ)\r
369         {\r
370           if (2 * mul > PM_MAX_MUL)\r
371             break;\r
372           mul *= 2;\r
373           div2_cpu++;\r
374         }\r
375 \r
376         if (div2_cpu != 0)\r
377         {\r
378           div2_cpu--;\r
379           div2_en = 1;\r
380         }\r
381 \r
382         pll_freq = in_osc0_f * mul / (div * (1 << div2_en));\r
383 \r
384         // Update real CPU Frequency\r
385         param->cpu_f = pll_freq / (1 << div2_cpu);\r
386         mul--;\r
387 \r
388         scif_pll_opt_t opt;\r
389 \r
390         opt.osc = SCIF_OSC0,     // Sel Osc0 or Osc1\r
391         opt.lockcount = 16,      // lockcount in main clock for the PLL wait lock\r
392         opt.div = div,             // DIV=1 in the formula\r
393         opt.mul = mul,             // MUL=7 in the formula\r
394         opt.pll_div2 = div2_en,        // pll_div2 Divide the PLL output frequency by 2 (this settings does not change the FVCO value)\r
395         opt.pll_wbwdisable = 0,  //pll_wbwdisable 1 Disable the Wide-Bandith Mode (Wide-Bandwith mode allow a faster startup time and out-of-lock time). 0 to enable the Wide-Bandith Mode.\r
396         opt.pll_freq = (pll_freq < AVR32_PM_PLL_VCO_RANGE0_MIN_FREQ) ? 1 : 0,        // Set to 1 for VCO frequency range 80-180MHz, set to 0 for VCO frequency range 160-240Mhz.\r
397 \r
398 \r
399         scif_pll_setup(SCIF_PLL0, opt); // lockcount in main clock for the PLL wait lock\r
400 \r
401         /* Enable PLL0 */\r
402         scif_pll_enable(SCIF_PLL0);\r
403 \r
404         /* Wait for PLL0 locked */\r
405         scif_wait_for_pll_locked(SCIF_PLL0) ;\r
406 \r
407         rest = pll_freq;\r
408         while (rest > AVR32_PM_PBA_MAX_FREQ ||\r
409                rest != param->pba_f)\r
410         {\r
411           div2_pba++;\r
412           rest = pll_freq / (1 << div2_pba);\r
413           if (rest < param->pba_f)\r
414             break;\r
415         }\r
416 \r
417         // Update real PBA Frequency\r
418         param->pba_f = pll_freq / (1 << div2_pba);\r
419 \r
420 \r
421         if (div2_cpu)\r
422         {\r
423           b_div2_cpu = TRUE;\r
424           div2_cpu--;\r
425         }\r
426         else\r
427           b_div2_cpu = FALSE;\r
428 \r
429         if (div2_pba)\r
430         {\r
431           b_div2_pba = TRUE;\r
432           div2_pba--;\r
433         }\r
434         else\r
435           b_div2_pba = FALSE;\r
436 \r
437         if (b_div2_cpu == TRUE )\r
438         {\r
439           pm_set_clk_domain_div(PM_CLK_DOMAIN_0, (pm_divratio_t) div2_cpu); // CPU\r
440           pm_set_clk_domain_div(PM_CLK_DOMAIN_1, (pm_divratio_t) div2_cpu); // HSB\r
441           pm_set_clk_domain_div(PM_CLK_DOMAIN_3, (pm_divratio_t) div2_cpu); // PBB\r
442         }\r
443         if (b_div2_pba == TRUE )\r
444         {\r
445           pm_set_clk_domain_div(PM_CLK_DOMAIN_2, (pm_divratio_t) div2_pba); // PBA\r
446           pm_set_clk_domain_div(PM_CLK_DOMAIN_4, (pm_divratio_t) div2_pba); // PBC\r
447         }\r
448 \r
449         // Set Flashc Wait State\r
450         flashc_set_flash_waitstate_and_readmode(param->cpu_f);\r
451 \r
452         // Set the main clock source as being PLL0.\r
453         pm_set_mclk_source(PM_CLK_SRC_PLL0);\r
454 \r
455         return PASS;\r
456 }\r
457 #endif // UC3C device-specific implementation\r
458 \r
459 long int pcl_switch_to_osc(pcl_osc_t osc, unsigned int fcrystal, unsigned int startup)\r
460 {\r
461 #ifndef AVR32_PM_VERSION_RESETVALUE\r
462 // Implementation for UC3A, UC3A3, UC3B parts.\r
463   if(PCL_OSC0 == osc)\r
464   {\r
465     // Configure OSC0 in crystal mode, external crystal with a FOSC0 Hz frequency,\r
466     // enable the OSC0, set the main clock source as being OSC0.\r
467     pm_switch_to_osc0(&AVR32_PM, fcrystal, startup);\r
468   }\r
469   else\r
470   {\r
471     return PCL_NOT_SUPPORTED;\r
472   }\r
473 #else\r
474 // Implementation for UC3C, UC3L parts.\r
475   #if AVR32_PM_VERSION_RESETVALUE < 0x400\r
476     return PCL_NOT_SUPPORTED;\r
477   #else\r
478   if(PCL_OSC0 == osc)\r
479   {\r
480     // Configure OSC0 in crystal mode, external crystal with a fcrystal Hz frequency.\r
481     scif_configure_osc_crystalmode(SCIF_OSC0, fcrystal);\r
482     // Enable the OSC0\r
483     scif_enable_osc(SCIF_OSC0, startup, true);\r
484     // Set the Flash wait state and the speed read mode (depending on the target CPU frequency).\r
485 #if UC3L\r
486     flashcdw_set_flash_waitstate_and_readmode(fcrystal);\r
487 #elif UC3C\r
488     flashc_set_flash_waitstate_and_readmode(fcrystal);\r
489 #endif\r
490     // Set the main clock source as being OSC0.\r
491     pm_set_mclk_source(PM_CLK_SRC_OSC0);\r
492   }\r
493   else\r
494   {\r
495     return PCL_NOT_SUPPORTED;\r
496   }\r
497   #endif\r
498 #endif\r
499   return PASS;\r
500 }\r
501 \r
502 long int pcl_configure_usb_clock(void)\r
503 {\r
504 #ifndef AVR32_PM_VERSION_RESETVALUE\r
505 // Implementation for UC3A, UC3A3, UC3B parts.\r
506   pm_configure_usb_clock();\r
507   return PASS;\r
508 #else\r
509   #ifdef AVR32_PM_410_H_INCLUDED\r
510     const scif_pll_opt_t opt = {\r
511               .osc = SCIF_OSC0,     // Sel Osc0 or Osc1\r
512               .lockcount = 16,      // lockcount in main clock for the PLL wait lock\r
513               .div = 1,             // DIV=1 in the formula\r
514               .mul = 5,             // MUL=7 in the formula\r
515               .pll_div2 = 1,        // pll_div2 Divide the PLL output frequency by 2 (this settings does not change the FVCO value)\r
516               .pll_wbwdisable = 0,  //pll_wbwdisable 1 Disable the Wide-Bandith Mode (Wide-Bandwith mode allow a faster startup time and out-of-lock time). 0 to enable the Wide-Bandith Mode.\r
517               .pll_freq = 1,        // Set to 1 for VCO frequency range 80-180MHz, set to 0 for VCO frequency range 160-240Mhz.\r
518     };\r
519 \r
520     /* Setup PLL1 on Osc0, mul=7 ,no divisor, lockcount=16, ie. 16Mhzx6 = 96MHz output */\r
521     scif_pll_setup(SCIF_PLL1, opt); // lockcount in main clock for the PLL wait lock\r
522 \r
523     /* Enable PLL1 */\r
524     scif_pll_enable(SCIF_PLL1);\r
525 \r
526     /* Wait for PLL1 locked */\r
527     scif_wait_for_pll_locked(SCIF_PLL1) ;\r
528 \r
529   // Implementation for UC3C parts.\r
530     // Setup the generic clock for USB\r
531     scif_gc_setup(AVR32_SCIF_GCLK_USB,\r
532                   SCIF_GCCTRL_PLL1,\r
533                   AVR32_SCIF_GC_NO_DIV_CLOCK,\r
534                   0);\r
535     // Now enable the generic clock\r
536     scif_gc_enable(AVR32_SCIF_GCLK_USB);\r
537     return PASS;\r
538   #else\r
539       return PCL_NOT_SUPPORTED;\r
540   #endif\r
541 #endif\r
542 }\r
543 \r
544 \r
545 #if UC3L\r
546 #else\r
547 void pcl_write_gplp(unsigned long gplp, unsigned long value)\r
548 {\r
549 #ifndef AVR32_PM_VERSION_RESETVALUE\r
550 // Implementation for UC3A, UC3A3, UC3B parts.\r
551   pm_write_gplp(&AVR32_PM,gplp,value);\r
552 #else\r
553   scif_write_gplp(gplp,value);\r
554 #endif\r
555 }\r
556 \r
557 unsigned long pcl_read_gplp(unsigned long gplp)\r
558 {\r
559 #ifndef AVR32_PM_VERSION_RESETVALUE\r
560 // Implementation for UC3A, UC3A3, UC3B parts.\r
561   return pm_read_gplp(&AVR32_PM,gplp);\r
562 #else\r
563   return scif_read_gplp(gplp);\r
564 #endif\r
565 }\r
566 #endif\r