power_clocks_lib.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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) 2014-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. #ifndef _POWER_CLOCKS_LIB_H_
  40. #define _POWER_CLOCKS_LIB_H_
  41. /**
  42. * \defgroup group_avr32_drivers_pm_power_clocks_lib Power Clocks Library
  43. * \ingroup group_avr32_drivers_pm
  44. *
  45. * \{
  46. */
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. #include <avr32/io.h>
  51. #include "compiler.h"
  52. #ifndef AVR32_PM_VERSION_RESETVALUE
  53. // Support for UC3A, UC3A3, UC3B parts.
  54. #include "pm.h"
  55. #else
  56. //! Device-specific data
  57. #if UC3L
  58. #include "pm_uc3l.h"
  59. #include "scif_uc3l.h"
  60. #include "flashcdw.h"
  61. #elif UC3C
  62. #include "pm_uc3c.h"
  63. #include "scif_uc3c.h"
  64. #include "flashc.h"
  65. #elif UC3D
  66. #include "pm_uc3d.h"
  67. #include "scif_uc3d.h"
  68. #include "flashcdw.h"
  69. #endif
  70. #endif
  71. /*! \name Clocks Management
  72. */
  73. //! @{
  74. //! The different oscillators
  75. typedef enum
  76. {
  77. PCL_OSC0 = 0,
  78. PCL_OSC1 = 1
  79. } pcl_osc_t;
  80. //! The different DFLLs
  81. typedef enum
  82. {
  83. PCL_DFLL0 = 0,
  84. PCL_DFLL1 = 1
  85. } pcl_dfll_t;
  86. //! Possible Main Clock Sources
  87. typedef enum
  88. {
  89. PCL_MC_RCSYS, // Default main clock source, supported by all (aka Slow Clock)
  90. PCL_MC_OSC0, // Supported by all
  91. PCL_MC_OSC1, // Supported by UC3C only
  92. PCL_MC_OSC0_PLL0, // Supported by UC3A, UC3B, UC3A3, UC3C (the main clock source is PLL0 with OSC0 as reference)
  93. PCL_MC_OSC1_PLL0, // Supported by UC3A, UC3B, UC3A3, UC3C (the main clock source is PLL0 with OSC1 as reference)
  94. PCL_MC_OSC0_PLL1, // Supported by UC3C (the main clock source is PLL1 with OSC0 as reference)
  95. PCL_MC_OSC1_PLL1, // Supported by UC3C (the main clock source is PLL1 with OSC1 as reference)
  96. PCL_MC_DFLL0, // Supported by UC3L
  97. PCL_MC_DFLL1, // Not supported yet
  98. PCL_MC_RC120M, // Supported by UC3L, UC3C
  99. PCL_MC_RC8M, // Supported by UC3C
  100. PCL_MC_CRIPOSC // Supported by UC3C
  101. } pcl_mainclk_t;
  102. //! Input and output parameters to configure clocks with pcl_configure_clocks().
  103. // NOTE: regarding the frequency settings, always abide by the datasheet rules and min & max supported frequencies.
  104. #ifndef AVR32_PM_VERSION_RESETVALUE
  105. // Support for UC3A, UC3A3, UC3B parts.
  106. #define pcl_freq_param_t pm_freq_param_t // See pm.h
  107. #else
  108. // Support for UC3C, UC3L parts.
  109. typedef struct
  110. {
  111. //! Main clock source selection (input argument).
  112. pcl_mainclk_t main_clk_src;
  113. //! Target CPU frequency (input/output argument).
  114. unsigned long cpu_f;
  115. //! Target PBA frequency (input/output argument).
  116. unsigned long pba_f;
  117. //! Target PBB frequency (input/output argument).
  118. unsigned long pbb_f;
  119. //! Target PBC frequency (input/output argument).
  120. unsigned long pbc_f;
  121. //! Oscillator 0's external crystal(or external clock) frequency (board dependant) (input argument).
  122. unsigned long osc0_f;
  123. //! Oscillator 0's external crystal(or external clock) startup time: AVR32_PM_OSCCTRL0_STARTUP_x_RCOSC (input argument).
  124. unsigned long osc0_startup;
  125. //! DFLL target frequency (input/output argument) (NOTE: the bigger, the most stable the frequency)
  126. unsigned long dfll_f;
  127. //! Other parameters that might be necessary depending on the device (implementation-dependent).
  128. // For the UC3L DFLL setup, this parameter should be pointing to a structure of
  129. // type (scif_gclk_opt_t *).
  130. void *pextra_params;
  131. } pcl_freq_param_t;
  132. #endif
  133. //! Define "not supported" for the lib.
  134. #define PCL_NOT_SUPPORTED (-10000)
  135. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks
  136. *
  137. * This function needs some parameters stored in a pcl_freq_param_t structure:
  138. * - main_clk_src is the id of the main clock source to use,
  139. * - cpu_f and pba_f and pbb_f are the wanted frequencies,
  140. * - osc0_f is the oscillator 0's external crystal (or external clock) on-board frequency (e.g. FOSC0),
  141. * - osc0_startup is the oscillator 0's external crystal (or external clock) startup time (e.g. OSC0_STARTUP).
  142. * - dfll_f is the target DFLL frequency to set-up if main_clk_src is the dfll.
  143. *
  144. * The CPU, HSB and PBA frequencies programmed after configuration are stored back into cpu_f and pba_f.
  145. *
  146. * \note: since it is dynamically computing the appropriate field values of the
  147. * configuration registers from the parameters structure, this function is not
  148. * optimal in terms of code size. For a code size optimal solution, it is better
  149. * to create a new function from pcl_configure_clocks() and modify it to use
  150. * preprocessor computation from pre-defined target frequencies.
  151. *
  152. * \param param pointer on the configuration structure.
  153. *
  154. * \retval 0 Success.
  155. * \retval <0 The configuration cannot be performed.
  156. */
  157. extern long int pcl_configure_clocks(pcl_freq_param_t *param);
  158. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks using the RCSYS osc as main source clock.
  159. *
  160. * This function needs some parameters stored in a pcl_freq_param_t structure:
  161. * - cpu_f and pba_f and pbb_f are the wanted frequencies
  162. *
  163. * Supported main clock sources: PCL_MC_RCSYS
  164. *
  165. * Supported synchronous clocks frequencies:
  166. * 115200Hz, 57600Hz, 28800Hz, 14400Hz, 7200Hz, 3600Hz, 1800Hz, 900Hz, 450Hz.
  167. *
  168. * \note: by default, this implementation doesn't perform thorough checks on the
  169. * input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  170. *
  171. * \note: since it is dynamically computing the appropriate field values of the
  172. * configuration registers from the parameters structure, this function is not
  173. * optimal in terms of code size. For a code size optimal solution, it is better
  174. * to create a new function from pcl_configure_clocks_rcsys() and modify it to use
  175. * preprocessor computation from pre-defined target frequencies.
  176. *
  177. * \param param pointer on the configuration structure.
  178. *
  179. * \retval 0 Success.
  180. * \retval <0 The configuration cannot be performed.
  181. */
  182. extern long int pcl_configure_clocks_rcsys(pcl_freq_param_t *param);
  183. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks using the RC120M osc as main source clock.
  184. *
  185. * This function needs some parameters stored in a pcl_freq_param_t structure:
  186. * - cpu_f and pba_f and pbb_f are the wanted frequencies
  187. *
  188. * Supported main clock sources: PCL_MC_RC120M
  189. *
  190. * Supported synchronous clocks frequencies:
  191. * 30MHz, 15MHz, 7.5MHz, 3.75MHz, 1.875MHz, 937.5kHz, 468.75kHz.
  192. *
  193. * \note: by default, this implementation doesn't perform thorough checks on the
  194. * input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  195. *
  196. * \note: since it is dynamically computing the appropriate field values of the
  197. * configuration registers from the parameters structure, this function is not
  198. * optimal in terms of code size. For a code size optimal solution, it is better
  199. * to create a new function from pcl_configure_clocks_rc120m() and modify it to
  200. * use preprocessor computation from pre-defined target frequencies.
  201. *
  202. * \param param pointer on the configuration structure.
  203. *
  204. * \retval 0 Success.
  205. * \retval <0 The configuration cannot be performed.
  206. */
  207. extern long int pcl_configure_clocks_rc120m(pcl_freq_param_t *param);
  208. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks using the OSC0 osc as main source clock
  209. *
  210. * This function needs some parameters stored in a pcl_freq_param_t structure:
  211. * - cpu_f and pba_f and pbb_f are the wanted frequencies,
  212. * - osc0_f is the oscillator 0's external crystal (or external clock) on-board frequency (e.g. FOSC0),
  213. * - osc0_startup is the oscillator 0's external crystal (or external clock) startup time (e.g. OSC0_STARTUP).
  214. *
  215. * Supported main clock sources: PCL_MC_OSC0
  216. *
  217. * Supported synchronous clocks frequencies:
  218. * (these obviously depend on the OSC0 frequency; we'll take 16MHz as an example)
  219. * 16MHz, 8MHz, 4MHz, 2MHz, 1MHz, 500kHz, 250kHz, 125kHz, 62.5kHz.
  220. *
  221. * \note: by default, this implementation doesn't perform thorough checks on the
  222. * input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  223. *
  224. * \note: since it is dynamically computing the appropriate field values of the
  225. * configuration registers from the parameters structure, this function is not
  226. * optimal in terms of code size. For a code size optimal solution, it is better
  227. * to create a new function from pcl_configure_clocks_osc0() and modify it to use
  228. * preprocessor computation from pre-defined target frequencies.
  229. *
  230. * \param param pointer on the configuration structure.
  231. *
  232. * \retval 0 Success.
  233. * \retval <0 The configuration cannot be performed.
  234. */
  235. extern long int pcl_configure_clocks_osc0(pcl_freq_param_t *param);
  236. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks using the DFLL0 as main source clock
  237. *
  238. * This function needs some parameters stored in a pcl_freq_param_t structure:
  239. * - cpu_f and pba_f and pbb_f are the wanted frequencies,
  240. * - dfll_f is the target DFLL frequency to set-up
  241. *
  242. * \note: when the DFLL0 is to be used as main source clock for the synchronous clocks,
  243. * the target frequency of the DFLL should be chosen to be as high as possible
  244. * within the specification range (for stability reasons); the target cpu and pbx
  245. * frequencies will then be reached by appropriate division ratio.
  246. *
  247. * Supported main clock sources: PCL_MC_DFLL0
  248. *
  249. * Supported synchronous clocks frequencies:
  250. * (these obviously depend on the DFLL target frequency; we'll take 100MHz as an example)
  251. * 50MHz, 25MHz, 12.5MHz, 6.25MHz, 3.125MHz, 1562.5kHz, 781.25kHz, 390.625kHz.
  252. *
  253. * \note: by default, this implementation doesn't perform thorough checks on the
  254. * input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  255. *
  256. * \note: since it is dynamically computing the appropriate field values of the
  257. * configuration registers from the parameters structure, this function is not
  258. * optimal in terms of code size. For a code size optimal solution, it is better
  259. * to create a new function from pcl_configure_clocks_dfll0() and modify it to
  260. * use preprocessor computation from pre-defined target frequencies.
  261. *
  262. * \param param pointer on the configuration structure.
  263. *
  264. * \retval 0 Success.
  265. * \retval <0 The configuration cannot be performed.
  266. */
  267. extern long int pcl_configure_clocks_dfll0(pcl_freq_param_t *param);
  268. /*! \brief Switch the main clock source to Osc0 configured in crystal mode
  269. *
  270. * \param osc The oscillator to enable and switch to.
  271. * \param fcrystal Oscillator external crystal frequency (Hz)
  272. * \param startup Oscillator startup time.
  273. *
  274. * \return Status.
  275. * \retval 0 Success.
  276. * \retval <0 An error occurred.
  277. */
  278. extern long int pcl_switch_to_osc(pcl_osc_t osc, unsigned int fcrystal, unsigned int startup);
  279. /*! \brief Enable the clock of a module.
  280. *
  281. * \param module The module to clock (use one of the defines in the part-specific
  282. * header file under "toolchain folder"/avr32/inc(lude)/avr32/; depending on the
  283. * clock domain, look for the sections "CPU clocks", "HSB clocks", "PBx clocks"
  284. * or look in the module section).
  285. *
  286. * \return Status.
  287. * \retval 0 Success.
  288. * \retval <0 An error occurred.
  289. */
  290. #ifndef AVR32_PM_VERSION_RESETVALUE
  291. // Implementation for UC3A, UC3A3, UC3B parts.
  292. #define pcl_enable_module(module) pm_enable_module(&AVR32_PM, module)
  293. #else
  294. // Implementation for UC3C, UC3L parts.
  295. #define pcl_enable_module(module) pm_enable_module(module)
  296. #endif
  297. /*! \brief Disable the clock of a module.
  298. *
  299. * \param module The module to shut down (use one of the defines in the part-specific
  300. * header file under "toolchain folder"/avr32/inc(lude)/avr32/; depending on the
  301. * clock domain, look for the sections "CPU clocks", "HSB clocks", "PBx clocks"
  302. * or look in the module section).
  303. *
  304. * \return Status.
  305. * \retval 0 Success.
  306. * \retval <0 An error occurred.
  307. */
  308. #ifndef AVR32_PM_VERSION_RESETVALUE
  309. // Implementation for UC3A, UC3A3, UC3B parts.
  310. #define pcl_disable_module(module) pm_disable_module(&AVR32_PM, module)
  311. #else
  312. // Implementation for UC3C, UC3L parts.
  313. #define pcl_disable_module(module) pm_disable_module(module)
  314. #endif
  315. /*! \brief Configure the USB Clock
  316. *
  317. * \return Status.
  318. * \retval 0 Success.
  319. * \retval <0 An error occurred.
  320. */
  321. extern long int pcl_configure_usb_clock(void);
  322. //! @}
  323. /*! \name Power Management
  324. */
  325. //! @{
  326. /*!
  327. * \brief Read the content of the GPLP registers
  328. * \param gplp GPLP register index (0,1,... depending on the number of GPLP registers for a given part)
  329. *
  330. * \return The content of the chosen GPLP register.
  331. */
  332. extern unsigned long pcl_read_gplp(unsigned long gplp);
  333. /*!
  334. * \brief Write into the GPLP registers
  335. * \param gplp GPLP register index (0,1,... depending on the number of GPLP registers for a given part)
  336. * \param value Value to write
  337. */
  338. extern void pcl_write_gplp(unsigned long gplp, unsigned long value);
  339. //! @}
  340. #ifdef __cplusplus
  341. }
  342. #endif
  343. /**
  344. * \}
  345. */
  346. #endif // _POWER_CLOCKS_LIB_H_