power_clocks_lib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
  2. /*This file has been prepared for Doxygen automatic documentation generation.*/
  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. *
  10. * - Compiler: IAR EWAVR32 and GNU GCC for AVR32
  11. * - Supported devices: All AVR32 devices.
  12. * - AppNote:
  13. *
  14. * \author Atmel Corporation: http://www.atmel.com \n
  15. * Support and FAQ: http://support.atmel.no/
  16. *
  17. *****************************************************************************/
  18. /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright notice, this
  24. * list of conditions and the following disclaimer.
  25. *
  26. * 2. Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials provided with the distribution.
  29. *
  30. * 3. The name of Atmel may not be used to endorse or promote products derived
  31. * from this software without specific prior written permission.
  32. *
  33. * 4. This software may only be redistributed and used in connection with an Atmel
  34. * AVR product.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  39. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  40. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  41. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  45. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  46. *
  47. */
  48. #ifndef _POWER_CLOCKS_LIB_H_
  49. #define _POWER_CLOCKS_LIB_H_
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #include <avr32/io.h>
  54. #include "compiler.h"
  55. #ifndef AVR32_PM_VERSION_RESETVALUE
  56. // Support for UC3A, UC3A3, UC3B parts.
  57. #include "pm.h"
  58. #else
  59. //! Device-specific data
  60. #if UC3L
  61. #include "pm_uc3l.h"
  62. #include "scif_uc3l.h"
  63. #include "flashcdw.h"
  64. #elif UC3C
  65. #include "pm_uc3c.h"
  66. #include "scif_uc3c.h"
  67. #include "flashc.h"
  68. #endif
  69. #endif
  70. /*! \name Clocks Management
  71. */
  72. //! @{
  73. //! The different oscillators
  74. typedef enum
  75. {
  76. PCL_OSC0 = 0,
  77. PCL_OSC1 = 1
  78. } pcl_osc_t;
  79. //! The different DFLLs
  80. typedef enum
  81. {
  82. PCL_DFLL0 = 0,
  83. PCL_DFLL1 = 1
  84. } pcl_dfll_t;
  85. //! Possible Main Clock Sources
  86. typedef enum
  87. {
  88. PCL_MC_RCSYS, // Default main clock source, supported by all (aka Slow Clock)
  89. PCL_MC_OSC0, // Supported by all
  90. PCL_MC_OSC1, // Supported by UC3C only
  91. PCL_MC_OSC0_PLL0, // Supported by UC3A, UC3B, UC3A3, UC3C (the main clock source is PLL0 with OSC0 as reference)
  92. PCL_MC_OSC1_PLL0, // Supported by UC3A, UC3B, UC3A3, UC3C (the main clock source is PLL0 with OSC1 as reference)
  93. PCL_MC_OSC0_PLL1, // Supported by UC3C (the main clock source is PLL1 with OSC0 as reference)
  94. PCL_MC_OSC1_PLL1, // Supported by UC3C (the main clock source is PLL1 with OSC1 as reference)
  95. PCL_MC_DFLL0, // Supported by UC3L
  96. PCL_MC_DFLL1, // Not supported yet
  97. PCL_MC_RC120M, // Supported by UC3L, UC3C
  98. PCL_MC_RC8M, // Supported by UC3C
  99. PCL_MC_CRIPOSC // Supported by UC3C
  100. } pcl_mainclk_t;
  101. //! Input and output parameters to configure clocks with pcl_configure_clocks().
  102. // NOTE: regarding the frequency settings, always abide by the datasheet rules and min & max supported frequencies.
  103. #ifndef AVR32_PM_VERSION_RESETVALUE
  104. // Support for UC3A, UC3A3, UC3B parts.
  105. #define pcl_freq_param_t pm_freq_param_t // See pm.h
  106. #else
  107. // Support for UC3C, UC3L parts.
  108. typedef struct
  109. {
  110. //! Main clock source selection (input argument).
  111. pcl_mainclk_t main_clk_src;
  112. //! Target CPU frequency (input/output argument).
  113. unsigned long cpu_f;
  114. //! Target PBA frequency (input/output argument).
  115. unsigned long pba_f;
  116. //! Target PBB frequency (input/output argument).
  117. unsigned long pbb_f;
  118. //! Target PBC frequency (input/output argument).
  119. unsigned long pbc_f;
  120. //! Oscillator 0's external crystal(or external clock) frequency (board dependant) (input argument).
  121. unsigned long osc0_f;
  122. //! Oscillator 0's external crystal(or external clock) startup time: AVR32_PM_OSCCTRL0_STARTUP_x_RCOSC (input argument).
  123. unsigned long osc0_startup;
  124. //! DFLL target frequency (input/output argument) (NOTE: the bigger, the most stable the frequency)
  125. unsigned long dfll_f;
  126. //! Other parameters that might be necessary depending on the device (implementation-dependent).
  127. // For the UC3L DFLL setup, this parameter should be pointing to a structure of
  128. // type (scif_gclk_opt_t *).
  129. void *pextra_params;
  130. } pcl_freq_param_t;
  131. #endif
  132. //! Define "not supported" for the lib.
  133. #define PCL_NOT_SUPPORTED (-10000)
  134. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks
  135. *
  136. * This function needs some parameters stored in a pcl_freq_param_t structure:
  137. * - main_clk_src is the id of the main clock source to use,
  138. * - cpu_f and pba_f and pbb_f are the wanted frequencies,
  139. * - osc0_f is the oscillator 0's external crystal (or external clock) on-board frequency (e.g. FOSC0),
  140. * - osc0_startup is the oscillator 0's external crystal (or external clock) startup time (e.g. OSC0_STARTUP).
  141. * - dfll_f is the target DFLL frequency to set-up if main_clk_src is the dfll.
  142. *
  143. * The CPU, HSB and PBA frequencies programmed after configuration are stored back into cpu_f and pba_f.
  144. *
  145. * \note: since it is dynamically computing the appropriate field values of the
  146. * configuration registers from the parameters structure, this function is not
  147. * optimal in terms of code size. For a code size optimal solution, it is better
  148. * to create a new function from pcl_configure_clocks() and modify it to use
  149. * preprocessor computation from pre-defined target frequencies.
  150. *
  151. * \param param pointer on the configuration structure.
  152. *
  153. * \retval 0 Success.
  154. * \retval <0 The configuration cannot be performed.
  155. */
  156. extern long int pcl_configure_clocks(pcl_freq_param_t *param);
  157. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks using the RCSYS osc as main source clock.
  158. *
  159. * This function needs some parameters stored in a pcl_freq_param_t structure:
  160. * - cpu_f and pba_f and pbb_f are the wanted frequencies
  161. *
  162. * Supported main clock sources: PCL_MC_RCSYS
  163. *
  164. * Supported synchronous clocks frequencies:
  165. * 115200Hz, 57600Hz, 28800Hz, 14400Hz, 7200Hz, 3600Hz, 1800Hz, 900Hz, 450Hz.
  166. *
  167. * \note: by default, this implementation doesn't perform thorough checks on the
  168. * input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  169. *
  170. * \note: since it is dynamically computing the appropriate field values of the
  171. * configuration registers from the parameters structure, this function is not
  172. * optimal in terms of code size. For a code size optimal solution, it is better
  173. * to create a new function from pcl_configure_clocks_rcsys() and modify it to use
  174. * preprocessor computation from pre-defined target frequencies.
  175. *
  176. * \param param pointer on the configuration structure.
  177. *
  178. * \retval 0 Success.
  179. * \retval <0 The configuration cannot be performed.
  180. */
  181. extern long int pcl_configure_clocks_rcsys(pcl_freq_param_t *param);
  182. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks using the RC120M osc as main source clock.
  183. *
  184. * This function needs some parameters stored in a pcl_freq_param_t structure:
  185. * - cpu_f and pba_f and pbb_f are the wanted frequencies
  186. *
  187. * Supported main clock sources: PCL_MC_RC120M
  188. *
  189. * Supported synchronous clocks frequencies:
  190. * 30MHz, 15MHz, 7.5MHz, 3.75MHz, 1.875MHz, 937.5kHz, 468.75kHz.
  191. *
  192. * \note: by default, this implementation doesn't perform thorough checks on the
  193. * input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  194. *
  195. * \note: since it is dynamically computing the appropriate field values of the
  196. * configuration registers from the parameters structure, this function is not
  197. * optimal in terms of code size. For a code size optimal solution, it is better
  198. * to create a new function from pcl_configure_clocks_rc120m() and modify it to
  199. * use preprocessor computation from pre-defined target frequencies.
  200. *
  201. * \param param pointer on the configuration structure.
  202. *
  203. * \retval 0 Success.
  204. * \retval <0 The configuration cannot be performed.
  205. */
  206. extern long int pcl_configure_clocks_rc120m(pcl_freq_param_t *param);
  207. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks using the OSC0 osc as main source clock
  208. *
  209. * This function needs some parameters stored in a pcl_freq_param_t structure:
  210. * - cpu_f and pba_f and pbb_f are the wanted frequencies,
  211. * - osc0_f is the oscillator 0's external crystal (or external clock) on-board frequency (e.g. FOSC0),
  212. * - osc0_startup is the oscillator 0's external crystal (or external clock) startup time (e.g. OSC0_STARTUP).
  213. *
  214. * Supported main clock sources: PCL_MC_OSC0
  215. *
  216. * Supported synchronous clocks frequencies:
  217. * (these obviously depend on the OSC0 frequency; we'll take 16MHz as an example)
  218. * 16MHz, 8MHz, 4MHz, 2MHz, 1MHz, 500kHz, 250kHz, 125kHz, 62.5kHz.
  219. *
  220. * \note: by default, this implementation doesn't perform thorough checks on the
  221. * input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  222. *
  223. * \note: since it is dynamically computing the appropriate field values of the
  224. * configuration registers from the parameters structure, this function is not
  225. * optimal in terms of code size. For a code size optimal solution, it is better
  226. * to create a new function from pcl_configure_clocks_osc0() and modify it to use
  227. * preprocessor computation from pre-defined target frequencies.
  228. *
  229. * \param param pointer on the configuration structure.
  230. *
  231. * \retval 0 Success.
  232. * \retval <0 The configuration cannot be performed.
  233. */
  234. extern long int pcl_configure_clocks_osc0(pcl_freq_param_t *param);
  235. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks using the DFLL0 as main source clock
  236. *
  237. * This function needs some parameters stored in a pcl_freq_param_t structure:
  238. * - cpu_f and pba_f and pbb_f are the wanted frequencies,
  239. * - dfll_f is the target DFLL frequency to set-up
  240. *
  241. * \note: when the DFLL0 is to be used as main source clock for the synchronous clocks,
  242. * the target frequency of the DFLL should be chosen to be as high as possible
  243. * within the specification range (for stability reasons); the target cpu and pbx
  244. * frequencies will then be reached by appropriate division ratio.
  245. *
  246. * Supported main clock sources: PCL_MC_DFLL0
  247. *
  248. * Supported synchronous clocks frequencies:
  249. * (these obviously depend on the DFLL target frequency; we'll take 100MHz as an example)
  250. * 50MHz, 25MHz, 12.5MHz, 6.25MHz, 3.125MHz, 1562.5kHz, 781.25kHz, 390.625kHz.
  251. *
  252. * \note: by default, this implementation doesn't perform thorough checks on the
  253. * input parameters. To enable the checks, define AVR32SFW_INPUT_CHECK.
  254. *
  255. * \note: since it is dynamically computing the appropriate field values of the
  256. * configuration registers from the parameters structure, this function is not
  257. * optimal in terms of code size. For a code size optimal solution, it is better
  258. * to create a new function from pcl_configure_clocks_dfll0() and modify it to
  259. * use preprocessor computation from pre-defined target frequencies.
  260. *
  261. * \param param pointer on the configuration structure.
  262. *
  263. * \retval 0 Success.
  264. * \retval <0 The configuration cannot be performed.
  265. */
  266. extern long int pcl_configure_clocks_dfll0(pcl_freq_param_t *param);
  267. /*! \brief Switch the main clock source to Osc0 configured in crystal mode
  268. *
  269. * \param osc The oscillator to enable and switch to.
  270. * \param fcrystal Oscillator external crystal frequency (Hz)
  271. * \param startup Oscillator startup time.
  272. *
  273. * \return Status.
  274. * \retval 0 Success.
  275. * \retval <0 An error occured.
  276. */
  277. extern long int pcl_switch_to_osc(pcl_osc_t osc, unsigned int fcrystal, unsigned int startup);
  278. /*! \brief Enable the clock of a module.
  279. *
  280. * \param module The module to clock (use one of the defines in the part-specific
  281. * header file under "toolchain folder"/avr32/inc(lude)/avr32/; depending on the
  282. * clock domain, look for the sections "CPU clocks", "HSB clocks", "PBx clocks"
  283. * or look in the module section).
  284. *
  285. * \return Status.
  286. * \retval 0 Success.
  287. * \retval <0 An error occured.
  288. */
  289. #ifndef AVR32_PM_VERSION_RESETVALUE
  290. // Implementation for UC3A, UC3A3, UC3B parts.
  291. #define pcl_enable_module(module) pm_enable_module(&AVR32_PM, module)
  292. #else
  293. // Implementation for UC3C, UC3L parts.
  294. #define pcl_enable_module(module) pm_enable_module(module)
  295. #endif
  296. /*! \brief Disable the clock of a module.
  297. *
  298. * \param module The module to shut down (use one of the defines in the part-specific
  299. * header file under "toolchain folder"/avr32/inc(lude)/avr32/; depending on the
  300. * clock domain, look for the sections "CPU clocks", "HSB clocks", "PBx clocks"
  301. * or look in the module section).
  302. *
  303. * \return Status.
  304. * \retval 0 Success.
  305. * \retval <0 An error occured.
  306. */
  307. #ifndef AVR32_PM_VERSION_RESETVALUE
  308. // Implementation for UC3A, UC3A3, UC3B parts.
  309. #define pcl_disable_module(module) pm_disable_module(&AVR32_PM, module)
  310. #else
  311. // Implementation for UC3C, UC3L parts.
  312. #define pcl_disable_module(module) pm_disable_module(module)
  313. #endif
  314. /*! \brief Configure the USB Clock
  315. *
  316. *
  317. * \return Status.
  318. * \retval 0 Success.
  319. * \retval <0 An error occured.
  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. #endif // _POWER_CLOCKS_LIB_H_