power_clocks_lib.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*****************************************************************************
  2. *
  3. * \file
  4. *
  5. * \brief High-level library abstracting features such as oscillators/pll/dfll
  6. * configuration, clock configuration, System-sensible parameters
  7. * configuration, buses clocks configuration, sleep mode, reset.
  8. *
  9. * Copyright (c) 2009-2018 Microchip Technology Inc. and its subsidiaries.
  10. *
  11. * \asf_license_start
  12. *
  13. * \page License
  14. *
  15. * Subject to your compliance with these terms, you may use Microchip
  16. * software and any derivatives exclusively with Microchip products.
  17. * It is your responsibility to comply with third party license terms applicable
  18. * to your use of third party software (including open source software) that
  19. * may accompany Microchip software.
  20. *
  21. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  22. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  23. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  24. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  25. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  26. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  27. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  28. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  29. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  30. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  31. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  32. *
  33. * \asf_license_stop
  34. *
  35. *****************************************************************************/
  36. /*
  37. * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
  38. */
  39. #include "power_clocks_lib.h"
  40. //! Device-specific data
  41. #if UC3L
  42. static long int pcl_configure_clocks_uc3l(pcl_freq_param_t *param); // FORWARD declaration
  43. #endif
  44. #if UC3C
  45. static long int pcl_configure_clocks_uc3c(pcl_freq_param_t *param); // FORWARD declaration
  46. #endif
  47. #if UC3D
  48. static long int pcl_configure_clocks_uc3d(pcl_freq_param_t *param); // FORWARD declaration
  49. #endif
  50. long int pcl_configure_clocks(pcl_freq_param_t *param)
  51. {
  52. #ifndef AVR32_PM_VERSION_RESETVALUE
  53. // Implementation for UC3A, UC3A3, UC3B parts.
  54. return(pm_configure_clocks(param));
  55. #else
  56. #if (defined AVR32_PM_410_H_INCLUDED ) || (defined AVR32_PM_412_H_INCLUDED )
  57. #if UC3D
  58. // Implementation for UC3D parts.
  59. return(pcl_configure_clocks_uc3d(param));
  60. #else
  61. // Implementation for UC3C parts.
  62. return(pcl_configure_clocks_uc3c(param));
  63. #endif
  64. #else
  65. // Implementation for UC3L parts.
  66. return(pcl_configure_clocks_uc3l(param));
  67. #endif
  68. #endif
  69. }
  70. //! Device-specific implementation
  71. #if UC3L
  72. // FORWARD declaration
  73. static long int pcl_configure_synchronous_clocks( pm_clk_src_t main_clk_src,
  74. unsigned long main_clock_freq_hz,
  75. pcl_freq_param_t *param);
  76. long int pcl_configure_clocks_rcsys(pcl_freq_param_t *param)
  77. {
  78. // Supported main clock sources: PCL_MC_RCSYS
  79. // Supported synchronous clocks frequencies if RCSYS is the main clock source:
  80. // 115200Hz, 57600Hz, 28800Hz, 14400Hz, 7200Hz, 3600Hz, 1800Hz, 900Hz, 450Hz.
  81. // NOTE: by default, this implementation doesn't perform thorough checks on the
  82. // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  83. #ifdef AVR32SFW_INPUT_CHECK
  84. // Verify that fCPU >= fPBx
  85. if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))
  86. return(-1);
  87. #endif
  88. #ifdef AVR32SFW_INPUT_CHECK
  89. // Verify that the target frequencies are reachable.
  90. if((param->cpu_f > SCIF_SLOWCLOCK_FREQ_HZ) || (param->pba_f > SCIF_SLOWCLOCK_FREQ_HZ)
  91. || (param->pbb_f > SCIF_SLOWCLOCK_FREQ_HZ))
  92. return(-1);
  93. #endif
  94. return(pcl_configure_synchronous_clocks(PM_CLK_SRC_SLOW, SCIF_SLOWCLOCK_FREQ_HZ, param));
  95. }
  96. long int pcl_configure_clocks_rc120m(pcl_freq_param_t *param)
  97. {
  98. // Supported main clock sources: PCL_MC_RC120M
  99. // Supported synchronous clocks frequencies if RC120M is the main clock source:
  100. // 30MHz, 15MHz, 7.5MHz, 3.75MHz, 1.875MHz, 937.5kHz, 468.75kHz.
  101. // NOTE: by default, this implementation doesn't perform thorough checks on the
  102. // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  103. #ifdef AVR32SFW_INPUT_CHECK
  104. // Verify that fCPU >= fPBx
  105. if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))
  106. return(-1);
  107. #endif
  108. #ifdef AVR32SFW_INPUT_CHECK
  109. // Verify that the target frequencies are reachable.
  110. if((param->cpu_f > SCIF_RC120M_FREQ_HZ) || (param->pba_f > SCIF_RC120M_FREQ_HZ)
  111. || (param->pbb_f > SCIF_RC120M_FREQ_HZ))
  112. return(-1);
  113. #endif
  114. // Start the 120MHz internal RCosc (RC120M) clock
  115. scif_start_rc120M();
  116. return(pcl_configure_synchronous_clocks(PM_CLK_SRC_RC120M, SCIF_RC120M_FREQ_HZ, param));
  117. }
  118. long int pcl_configure_clocks_osc0(pcl_freq_param_t *param)
  119. {
  120. // Supported main clock sources: PCL_MC_OSC0
  121. // Supported synchronous clocks frequencies if OSC0 is the main clock source:
  122. // (these obviously depend on the OSC0 frequency; we'll take 16MHz as an example)
  123. // 16MHz, 8MHz, 4MHz, 2MHz, 1MHz, 500kHz, 250kHz, 125kHz, 62.5kHz.
  124. // NOTE: by default, this implementation doesn't perform thorough checks on the
  125. // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  126. unsigned long main_clock_freq;
  127. #ifdef AVR32SFW_INPUT_CHECK
  128. // Verify that fCPU >= fPBx
  129. if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))
  130. return(-1);
  131. #endif
  132. main_clock_freq = param->osc0_f;
  133. #ifdef AVR32SFW_INPUT_CHECK
  134. // Verify that the target frequencies are reachable.
  135. if((param->cpu_f > main_clock_freq) || (param->pba_f > main_clock_freq)
  136. || (param->pbb_f > main_clock_freq))
  137. return(-1);
  138. #endif
  139. // Configure OSC0 in crystal mode, external crystal with a fcrystal Hz frequency.
  140. scif_configure_osc_crystalmode(SCIF_OSC0, main_clock_freq);
  141. // Enable the OSC0
  142. scif_enable_osc(SCIF_OSC0, param->osc0_startup, true);
  143. return(pcl_configure_synchronous_clocks(PM_CLK_SRC_OSC0, main_clock_freq, param));
  144. }
  145. long int pcl_configure_clocks_dfll0(pcl_freq_param_t *param)
  146. {
  147. // Supported main clock sources: PCL_MC_DFLL
  148. // Supported synchronous clocks frequencies if DFLL is the main clock source:
  149. // (these obviously depend on the DFLL target frequency; we'll take 100MHz as an example)
  150. // 50MHz, 25MHz, 12.5MHz, 6.25MHz, 3.125MHz, 1562.5kHz, 781.25kHz, 390.625kHz.
  151. // NOTE: by default, this implementation doesn't perform thorough checks on the
  152. // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  153. unsigned long main_clock_freq;
  154. scif_gclk_opt_t *pgc_dfllif_ref_opt;
  155. #ifdef AVR32SFW_INPUT_CHECK
  156. // Verify that fCPU >= fPBx
  157. if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))
  158. return(-1);
  159. #endif
  160. main_clock_freq = param->dfll_f;
  161. #ifdef AVR32SFW_INPUT_CHECK
  162. // Verify that the target DFLL output frequency is in the correct range.
  163. if((main_clock_freq > SCIF_DFLL_MAXFREQ_HZ) || (main_clock_freq < SCIF_DFLL_MINFREQ_HZ))
  164. return(-1);
  165. // Verify that the target frequencies are reachable.
  166. if((param->cpu_f > main_clock_freq) || (param->pba_f > main_clock_freq)
  167. || (param->pbb_f > main_clock_freq))
  168. return(-1);
  169. #endif
  170. pgc_dfllif_ref_opt = (scif_gclk_opt_t *)param->pextra_params;
  171. // Implementation note: this implementation configures the DFLL in closed-loop
  172. // mode (because it gives the best accuracy) which enables the generic clock CLK_DFLLIF_REF
  173. // as a reference (RCSYS being used as the generic clock source, undivided).
  174. scif_dfll0_closedloop_configure_and_start(pgc_dfllif_ref_opt, main_clock_freq, true);
  175. return(pcl_configure_synchronous_clocks(PM_CLK_SRC_DFLL0, main_clock_freq, param));
  176. }
  177. static long int pcl_configure_clocks_uc3l(pcl_freq_param_t *param)
  178. {
  179. // Supported main clock sources: PCL_MC_RCSYS, PCL_MC_OSC0, PCL_MC_DFLL0, PCL_MC_RC120M
  180. // Supported synchronous clocks frequencies if RCSYS is the main clock source:
  181. // 115200Hz, 57600Hz, 28800Hz, 14400Hz, 7200Hz, 3600Hz, 1800Hz, 900Hz, 450Hz.
  182. // Supported synchronous clocks frequencies if RC120M is the main clock source:
  183. // 30MHz, 15MHz, 7.5MHz, 3.75MHz, 1.875MHz, 937.5kHz, 468.75kHz.
  184. // Supported synchronous clocks frequencies if OSC0 is the main clock source:
  185. // (these obviously depend on the OSC0 frequency; we'll take 16MHz as an example)
  186. // 16MHz, 8MHz, 4MHz, 2MHz, 1MHz, 500kHz, 250kHz, 125kHz, 62.5kHz.
  187. // Supported synchronous clocks frequencies if DFLL is the main clock source:
  188. // (these obviously depend on the DFLL target frequency; we'll take 100MHz as an example)
  189. // 50MHz, 25MHz, 12.5MHz, 6.25MHz, 3.125MHz, 1562.5kHz, 781.25kHz, 390.625kHz.
  190. // NOTE: by default, this implementation doesn't perform thorough checks on the
  191. // input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  192. #ifdef AVR32SFW_INPUT_CHECK
  193. // Verify that fCPU >= fPBx
  194. if((param->cpu_f < param->pba_f) || (param->cpu_f < param->pbb_f))
  195. return(-1);
  196. #endif
  197. if(PCL_MC_RCSYS == param->main_clk_src)
  198. {
  199. return(pcl_configure_clocks_rcsys(param));
  200. }
  201. else if(PCL_MC_RC120M == param->main_clk_src)
  202. {
  203. return(pcl_configure_clocks_rc120m(param));
  204. }
  205. else if(PCL_MC_OSC0 == param->main_clk_src)
  206. {
  207. return(pcl_configure_clocks_osc0(param));
  208. }
  209. else // PCL_MC_DFLL0 == param->main_clk_src
  210. {
  211. return(pcl_configure_clocks_dfll0(param));
  212. }
  213. }
  214. 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)
  215. {
  216. //#
  217. //# Set the Synchronous clock division ratio for each clock domain
  218. //#
  219. pm_set_all_cksel(main_clock_freq_hz, param->cpu_f, param->pba_f, param->pbb_f);
  220. //#
  221. //# Set the Flash wait state and the speed read mode (depending on the target CPU frequency).
  222. //#
  223. #if UC3L
  224. flashcdw_set_flash_waitstate_and_readmode(param->cpu_f);
  225. #elif UC3C
  226. flashc_set_flash_waitstate_and_readmode(param->cpu_f);
  227. #endif
  228. //#
  229. //# Switch the main clock source to the selected clock.
  230. //#
  231. pm_set_mclk_source(main_clk_src);
  232. return PASS;
  233. }
  234. #endif // UC3L device-specific implementation
  235. //! UC3C Device-specific implementation
  236. #if UC3C
  237. static long int pcl_configure_clocks_uc3c(pcl_freq_param_t *param)
  238. {
  239. #define PM_MAX_MUL ((1 << AVR32_SCIF_PLLMUL_SIZE) - 1)
  240. #define AVR32_PM_PBA_MAX_FREQ 66000000
  241. #define AVR32_PM_PLL_VCO_RANGE0_MAX_FREQ 240000000
  242. #define AVR32_PM_PLL_VCO_RANGE0_MIN_FREQ 160000000
  243. // Implementation for UC3C parts.
  244. // Supported frequencies:
  245. // Fosc0 mul div PLL div2_en cpu_f pba_f Comment
  246. // 12 15 1 192 1 12 12
  247. // 12 9 3 40 1 20 20 PLL out of spec
  248. // 12 15 1 192 1 24 12
  249. // 12 9 1 120 1 30 15
  250. // 12 9 3 40 0 40 20 PLL out of spec
  251. // 12 15 1 192 1 48 12
  252. // 12 15 1 192 1 48 24
  253. // 12 8 1 108 1 54 27
  254. // 12 9 1 120 1 60 15
  255. // 12 9 1 120 1 60 30
  256. // 12 10 1 132 1 66 16.5
  257. //
  258. unsigned long in_cpu_f = param->cpu_f;
  259. unsigned long in_osc0_f = param->osc0_f;
  260. unsigned long mul, div, div2_en = 0, div2_cpu = 0, div2_pba = 0;
  261. unsigned long pll_freq, rest;
  262. bool b_div2_pba, b_div2_cpu;
  263. // Configure OSC0 in crystal mode, external crystal with a FOSC0 Hz frequency.
  264. scif_configure_osc_crystalmode(SCIF_OSC0, in_osc0_f);
  265. // Enable the OSC0
  266. scif_enable_osc(SCIF_OSC0, param->osc0_startup, true);
  267. // Set the main clock source as being OSC0.
  268. pm_set_mclk_source(PM_CLK_SRC_OSC0);
  269. // Start with CPU freq config
  270. if (in_cpu_f == in_osc0_f)
  271. {
  272. param->cpu_f = in_osc0_f;
  273. param->pba_f = in_osc0_f;
  274. return PASS;
  275. }
  276. else if (in_cpu_f < in_osc0_f)
  277. {
  278. // TBD
  279. }
  280. rest = in_cpu_f % in_osc0_f;
  281. for (div = 1; div < 32; div++)
  282. {
  283. if ((div * rest) % in_osc0_f == 0)
  284. break;
  285. }
  286. if (div == 32)
  287. return FAIL;
  288. mul = (in_cpu_f * div) / in_osc0_f;
  289. if (mul > PM_MAX_MUL)
  290. return FAIL;
  291. // export 2power from PLL div to div2_cpu
  292. while (!(div % 2))
  293. {
  294. div /= 2;
  295. div2_cpu++;
  296. }
  297. // Here we know the mul and div parameter of the PLL config.
  298. // . Check out if the PLL has a valid in_cpu_f.
  299. // . Try to have for the PLL frequency (VCO output) the highest possible value
  300. // to reduce jitter.
  301. while (in_osc0_f * 2 * mul / div < AVR32_PM_PLL_VCO_RANGE0_MAX_FREQ)
  302. {
  303. if (2 * mul > PM_MAX_MUL)
  304. break;
  305. mul *= 2;
  306. div2_cpu++;
  307. }
  308. if (div2_cpu != 0)
  309. {
  310. div2_cpu--;
  311. div2_en = 1;
  312. }
  313. pll_freq = in_osc0_f * mul / (div * (1 << div2_en));
  314. // Update real CPU Frequency
  315. param->cpu_f = pll_freq / (1 << div2_cpu);
  316. mul--;
  317. scif_pll_opt_t opt;
  318. opt.osc = SCIF_OSC0, // Sel Osc0 or Osc1
  319. opt.lockcount = 16, // lockcount in main clock for the PLL wait lock
  320. opt.div = div, // DIV=1 in the formula
  321. opt.mul = mul, // MUL=7 in the formula
  322. opt.pll_div2 = div2_en, // pll_div2 Divide the PLL output frequency by 2 (this settings does not change the FVCO value)
  323. 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.
  324. 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.
  325. scif_pll_setup(SCIF_PLL0, &opt); // lockcount in main clock for the PLL wait lock
  326. /* Enable PLL0 */
  327. scif_pll_enable(SCIF_PLL0);
  328. /* Wait for PLL0 locked */
  329. scif_wait_for_pll_locked(SCIF_PLL0) ;
  330. rest = pll_freq;
  331. while (rest > AVR32_PM_PBA_MAX_FREQ ||
  332. rest != param->pba_f)
  333. {
  334. div2_pba++;
  335. rest = pll_freq / (1 << div2_pba);
  336. if (rest < param->pba_f)
  337. break;
  338. }
  339. // Update real PBA Frequency
  340. param->pba_f = pll_freq / (1 << div2_pba);
  341. if (div2_cpu)
  342. {
  343. b_div2_cpu = true;
  344. div2_cpu--;
  345. }
  346. else
  347. b_div2_cpu = false;
  348. if (div2_pba)
  349. {
  350. b_div2_pba = true;
  351. div2_pba--;
  352. }
  353. else
  354. b_div2_pba = false;
  355. if (b_div2_cpu == true )
  356. {
  357. pm_set_clk_domain_div(PM_CLK_DOMAIN_0, (pm_divratio_t) div2_cpu); // CPU
  358. pm_set_clk_domain_div(PM_CLK_DOMAIN_1, (pm_divratio_t) div2_cpu); // HSB
  359. pm_set_clk_domain_div(PM_CLK_DOMAIN_3, (pm_divratio_t) div2_cpu); // PBB
  360. }
  361. if (b_div2_pba == true )
  362. {
  363. pm_set_clk_domain_div(PM_CLK_DOMAIN_2, (pm_divratio_t) div2_pba); // PBA
  364. pm_set_clk_domain_div(PM_CLK_DOMAIN_4, (pm_divratio_t) div2_pba); // PBC
  365. }
  366. // Set Flashc Wait State
  367. flashc_set_flash_waitstate_and_readmode(param->cpu_f);
  368. // Set the main clock source as being PLL0.
  369. pm_set_mclk_source(PM_CLK_SRC_PLL0);
  370. return PASS;
  371. }
  372. #endif // UC3C device-specific implementation
  373. #if UC3D
  374. static long int pcl_configure_clocks_uc3d(pcl_freq_param_t *param)
  375. {
  376. #define PM_MAX_MUL ((1 << AVR32_SCIF_PLLMUL_SIZE) - 1)
  377. #define AVR32_PM_PBA_MAX_FREQ 48000000
  378. #define AVR32_PM_PLL_VCO_RANGE0_MAX_FREQ 240000000
  379. #define AVR32_PM_PLL_VCO_RANGE0_MIN_FREQ 160000000
  380. // Implementation for UC3C parts.
  381. // Supported frequencies:
  382. // Fosc0 mul div PLL div2_en cpu_f pba_f Comment
  383. // 12 15 1 192 1 12 12
  384. // 12 9 3 40 1 20 20 PLL out of spec
  385. // 12 15 1 192 1 24 12
  386. // 12 9 1 120 1 30 15
  387. // 12 9 3 40 0 40 20 PLL out of spec
  388. // 12 15 1 192 1 48 12
  389. // 12 15 1 192 1 48 24
  390. // 12 8 1 108 1 54 27
  391. // 12 9 1 120 1 60 15
  392. // 12 9 1 120 1 60 30
  393. // 12 10 1 132 1 66 16.5
  394. //
  395. unsigned long in_cpu_f = param->cpu_f;
  396. unsigned long in_osc0_f = param->osc0_f;
  397. unsigned long mul, div, div2_en = 0, div2_cpu = 0, div2_pba = 0;
  398. unsigned long pll_freq, rest;
  399. Bool b_div2_pba, b_div2_cpu;
  400. // Configure OSC0 in crystal mode, external crystal with a FOSC0 Hz frequency.
  401. scif_configure_osc_crystalmode(SCIF_OSC0, in_osc0_f);
  402. // Enable the OSC0
  403. scif_enable_osc(SCIF_OSC0, param->osc0_startup, true);
  404. // Set the main clock source as being OSC0.
  405. pm_set_mclk_source(PM_CLK_SRC_OSC0);
  406. // Start with CPU freq config
  407. if (in_cpu_f == in_osc0_f)
  408. {
  409. param->cpu_f = in_osc0_f;
  410. param->pba_f = in_osc0_f;
  411. return PASS;
  412. }
  413. else if (in_cpu_f < in_osc0_f)
  414. {
  415. // TBD
  416. }
  417. rest = in_cpu_f % in_osc0_f;
  418. for (div = 1; div < 32; div++)
  419. {
  420. if ((div * rest) % in_osc0_f == 0)
  421. break;
  422. }
  423. if (div == 32)
  424. return FAIL;
  425. mul = (in_cpu_f * div) / in_osc0_f;
  426. if (mul > PM_MAX_MUL)
  427. return FAIL;
  428. // export 2power from PLL div to div2_cpu
  429. while (!(div % 2))
  430. {
  431. div /= 2;
  432. div2_cpu++;
  433. }
  434. // Here we know the mul and div parameter of the PLL config.
  435. // . Check out if the PLL has a valid in_cpu_f.
  436. // . Try to have for the PLL frequency (VCO output) the highest possible value
  437. // to reduce jitter.
  438. while (in_osc0_f * 2 * mul / div < AVR32_PM_PLL_VCO_RANGE0_MAX_FREQ)
  439. {
  440. if (2 * mul > PM_MAX_MUL)
  441. break;
  442. mul *= 2;
  443. div2_cpu++;
  444. }
  445. if (div2_cpu != 0)
  446. {
  447. div2_cpu--;
  448. div2_en = 1;
  449. }
  450. pll_freq = in_osc0_f * mul / (div * (1 << div2_en));
  451. // Update real CPU Frequency
  452. param->cpu_f = pll_freq / (1 << div2_cpu);
  453. mul--;
  454. scif_pll_opt_t opt;
  455. opt.osc = SCIF_OSC0, // Sel Osc0 or Osc1
  456. opt.lockcount = 16, // lockcount in main clock for the PLL wait lock
  457. opt.div = div, // DIV=1 in the formula
  458. opt.mul = mul, // MUL=7 in the formula
  459. opt.pll_div2 = div2_en, // pll_div2 Divide the PLL output frequency by 2 (this settings does not change the FVCO value)
  460. 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.
  461. 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.
  462. scif_pll_setup(SCIF_PLL0, &opt); // lockcount in main clock for the PLL wait lock
  463. /* Enable PLL0 */
  464. scif_pll_enable(SCIF_PLL0);
  465. /* Wait for PLL0 locked */
  466. scif_wait_for_pll_locked(SCIF_PLL0) ;
  467. rest = pll_freq;
  468. while (rest > AVR32_PM_PBA_MAX_FREQ ||
  469. rest != param->pba_f)
  470. {
  471. div2_pba++;
  472. rest = pll_freq / (1 << div2_pba);
  473. if (rest < param->pba_f)
  474. break;
  475. }
  476. // Update real PBA Frequency
  477. param->pba_f = pll_freq / (1 << div2_pba);
  478. if (div2_cpu)
  479. {
  480. b_div2_cpu = true;
  481. div2_cpu--;
  482. }
  483. else
  484. b_div2_cpu = false;
  485. if (div2_pba)
  486. {
  487. b_div2_pba = true;
  488. div2_pba--;
  489. }
  490. else
  491. b_div2_pba = false;
  492. if (b_div2_cpu == true )
  493. {
  494. pm_set_clk_domain_div(PM_CLK_DOMAIN_0, (pm_divratio_t) div2_cpu); // CPU
  495. pm_set_clk_domain_div(PM_CLK_DOMAIN_1, (pm_divratio_t) div2_cpu); // HSB
  496. pm_set_clk_domain_div(PM_CLK_DOMAIN_3, (pm_divratio_t) div2_cpu); // PBB
  497. }
  498. if (b_div2_pba == true )
  499. {
  500. pm_set_clk_domain_div(PM_CLK_DOMAIN_2, (pm_divratio_t) div2_pba); // PBA
  501. }
  502. // Set Flashc Wait State
  503. flashcdw_set_flash_waitstate_and_readmode(param->cpu_f);
  504. // Set the main clock source as being PLL0.
  505. pm_set_mclk_source(PM_CLK_SRC_PLL0);
  506. return PASS;
  507. }
  508. #endif // UC3D device-specific implementation
  509. long int pcl_switch_to_osc(pcl_osc_t osc, unsigned int fcrystal, unsigned int startup)
  510. {
  511. #ifndef AVR32_PM_VERSION_RESETVALUE
  512. // Implementation for UC3A, UC3A3, UC3B parts.
  513. if(PCL_OSC0 == osc)
  514. {
  515. // Configure OSC0 in crystal mode, external crystal with a FOSC0 Hz frequency,
  516. // enable the OSC0, set the main clock source as being OSC0.
  517. pm_switch_to_osc0(&AVR32_PM, fcrystal, startup);
  518. }
  519. else
  520. {
  521. return PCL_NOT_SUPPORTED;
  522. }
  523. #else
  524. // Implementation for UC3C, UC3L parts.
  525. #if AVR32_PM_VERSION_RESETVALUE < 0x400
  526. return PCL_NOT_SUPPORTED;
  527. #else
  528. if(PCL_OSC0 == osc)
  529. {
  530. // Configure OSC0 in crystal mode, external crystal with a fcrystal Hz frequency.
  531. scif_configure_osc_crystalmode(SCIF_OSC0, fcrystal);
  532. // Enable the OSC0
  533. scif_enable_osc(SCIF_OSC0, startup, true);
  534. // Set the Flash wait state and the speed read mode (depending on the target CPU frequency).
  535. #if UC3L || UC3D
  536. flashcdw_set_flash_waitstate_and_readmode(fcrystal);
  537. #elif UC3C
  538. flashc_set_flash_waitstate_and_readmode(fcrystal);
  539. #endif
  540. // Set the main clock source as being OSC0.
  541. pm_set_mclk_source(PM_CLK_SRC_OSC0);
  542. }
  543. else
  544. {
  545. return PCL_NOT_SUPPORTED;
  546. }
  547. #endif
  548. #endif
  549. return PASS;
  550. }
  551. long int pcl_configure_usb_clock(void)
  552. {
  553. #ifndef AVR32_PM_VERSION_RESETVALUE
  554. // Implementation for UC3A, UC3A3, UC3B parts.
  555. pm_configure_usb_clock();
  556. return PASS;
  557. #else
  558. #if UC3C
  559. const scif_pll_opt_t opt = {
  560. .osc = SCIF_OSC0, // Sel Osc0 or Osc1
  561. .lockcount = 16, // lockcount in main clock for the PLL wait lock
  562. .div = 1, // DIV=1 in the formula
  563. .mul = 5, // MUL=7 in the formula
  564. .pll_div2 = 1, // pll_div2 Divide the PLL output frequency by 2 (this settings does not change the FVCO value)
  565. .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.
  566. .pll_freq = 1, // Set to 1 for VCO frequency range 80-180MHz, set to 0 for VCO frequency range 160-240Mhz.
  567. };
  568. /* Setup PLL1 on Osc0, mul=7 ,no divisor, lockcount=16, ie. 16Mhzx6 = 96MHz output */
  569. scif_pll_setup(SCIF_PLL1, &opt); // lockcount in main clock for the PLL wait lock
  570. /* Enable PLL1 */
  571. scif_pll_enable(SCIF_PLL1);
  572. /* Wait for PLL1 locked */
  573. scif_wait_for_pll_locked(SCIF_PLL1) ;
  574. // Implementation for UC3C parts.
  575. // Setup the generic clock for USB
  576. scif_gc_setup(
  577. #if (defined AVR32_USBB)
  578. AVR32_SCIF_GCLK_USB,
  579. #else
  580. AVR32_SCIF_GCLK_USBC,
  581. #endif
  582. SCIF_GCCTRL_PLL1,
  583. AVR32_SCIF_GC_NO_DIV_CLOCK,
  584. 0);
  585. // Now enable the generic clock
  586. scif_gc_enable(
  587. #if (defined AVR32_USBB)
  588. AVR32_SCIF_GCLK_USB
  589. #else
  590. AVR32_SCIF_GCLK_USBC
  591. #endif
  592. );
  593. return PASS;
  594. #else
  595. return PCL_NOT_SUPPORTED;
  596. #endif
  597. #endif
  598. }
  599. #if UC3L
  600. #else
  601. void pcl_write_gplp(unsigned long gplp, unsigned long value)
  602. {
  603. #ifndef AVR32_PM_VERSION_RESETVALUE
  604. // Implementation for UC3A, UC3A3, UC3B parts.
  605. pm_write_gplp(&AVR32_PM,gplp,value);
  606. #else
  607. scif_write_gplp(gplp,value);
  608. #endif
  609. }
  610. unsigned long pcl_read_gplp(unsigned long gplp)
  611. {
  612. #ifndef AVR32_PM_VERSION_RESETVALUE
  613. // Implementation for UC3A, UC3A3, UC3B parts.
  614. return pm_read_gplp(&AVR32_PM,gplp);
  615. #else
  616. return scif_read_gplp(gplp);
  617. #endif
  618. }
  619. #endif