pm.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 Power Manager driver.
  6. *
  7. *
  8. * - Compiler: IAR EWAVR32 and GNU GCC for AVR32
  9. * - Supported devices: All AVR32 devices.
  10. * - AppNote:
  11. *
  12. * \author Atmel Corporation: http://www.atmel.com \n
  13. * Support and FAQ: http://support.atmel.no/
  14. *
  15. *****************************************************************************/
  16. /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright notice, this
  22. * list of conditions and the following disclaimer.
  23. *
  24. * 2. Redistributions in binary form must reproduce the above copyright notice,
  25. * this list of conditions and the following disclaimer in the documentation
  26. * and/or other materials provided with the distribution.
  27. *
  28. * 3. The name of Atmel may not be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * 4. This software may only be redistributed and used in connection with an Atmel
  32. * AVR product.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  35. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  37. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  38. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  39. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  41. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  44. *
  45. */
  46. #ifndef _PM_H_
  47. #define _PM_H_
  48. #include <avr32/io.h>
  49. #include "compiler.h"
  50. #include "preprocessor.h"
  51. /*! \brief Sets the MCU in the specified sleep mode.
  52. *
  53. * \param mode Sleep mode:
  54. * \arg \c AVR32_PM_SMODE_IDLE: Idle;
  55. * \arg \c AVR32_PM_SMODE_FROZEN: Frozen;
  56. * \arg \c AVR32_PM_SMODE_STANDBY: Standby;
  57. * \arg \c AVR32_PM_SMODE_STOP: Stop;
  58. * \arg \c AVR32_PM_SMODE_DEEP_STOP: DeepStop;
  59. * \arg \c AVR32_PM_SMODE_STATIC: Static.
  60. */
  61. #define SLEEP(mode) {__asm__ __volatile__ ("sleep "STRINGZ(mode));}
  62. //! Input and output parameters when initializing PM clocks using pm_configure_clocks().
  63. typedef struct
  64. {
  65. //! CPU frequency (input/output argument).
  66. unsigned long cpu_f;
  67. //! PBA frequency (input/output argument).
  68. unsigned long pba_f;
  69. //! Oscillator 0's external crystal(or external clock) frequency (board dependant) (input argument).
  70. unsigned long osc0_f;
  71. //! Oscillator 0's external crystal(or external clock) startup time: AVR32_PM_OSCCTRL0_STARTUP_x_RCOSC (input argument).
  72. unsigned long osc0_startup;
  73. } pm_freq_param_t;
  74. #define PM_FREQ_STATUS_FAIL (-1)
  75. #define PM_FREQ_STATUS_OK (0)
  76. /*! \brief Gets the MCU reset cause.
  77. *
  78. * \param pm Base address of the Power Manager instance (i.e. &AVR32_PM).
  79. *
  80. * \return The MCU reset cause which can be masked with the
  81. * \c AVR32_PM_RCAUSE_x_MASK bit-masks to isolate specific causes.
  82. */
  83. #if (defined __GNUC__)
  84. __attribute__((__always_inline__))
  85. #endif
  86. extern __inline__ unsigned int pm_get_reset_cause(volatile avr32_pm_t *pm)
  87. {
  88. return pm->rcause;
  89. }
  90. /*!
  91. * \brief This function will enable the external clock mode of the oscillator 0.
  92. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  93. */
  94. extern void pm_enable_osc0_ext_clock(volatile avr32_pm_t *pm);
  95. /*!
  96. * \brief This function will enable the crystal mode of the oscillator 0.
  97. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  98. * \param fosc0 Oscillator 0 crystal frequency (Hz)
  99. */
  100. extern void pm_enable_osc0_crystal(volatile avr32_pm_t *pm, unsigned int fosc0);
  101. /*!
  102. * \brief This function will enable the oscillator 0 to be used with a startup time.
  103. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  104. * \param startup Clock 0 startup time. AVR32_PM_OSCCTRL0_STARTUP_x_RCOSC.
  105. */
  106. extern void pm_enable_clk0(volatile avr32_pm_t *pm, unsigned int startup);
  107. /*!
  108. * \brief This function will disable the oscillator 0.
  109. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  110. */
  111. extern void pm_disable_clk0(volatile avr32_pm_t *pm);
  112. /*!
  113. * \brief This function will enable the oscillator 0 to be used with no startup time.
  114. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  115. * \param startup Clock 0 startup time, for which the function does not wait. AVR32_PM_OSCCTRL0_STARTUP_x_RCOSC.
  116. */
  117. extern void pm_enable_clk0_no_wait(volatile avr32_pm_t *pm, unsigned int startup);
  118. /*!
  119. * \brief This function will wait until the Osc0 clock is ready.
  120. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  121. */
  122. extern void pm_wait_for_clk0_ready(volatile avr32_pm_t *pm);
  123. /*!
  124. * \brief This function will enable the external clock mode of the oscillator 1.
  125. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  126. */
  127. extern void pm_enable_osc1_ext_clock(volatile avr32_pm_t *pm);
  128. /*!
  129. * \brief This function will enable the crystal mode of the oscillator 1.
  130. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  131. * \param fosc1 Oscillator 1 crystal frequency (Hz)
  132. */
  133. extern void pm_enable_osc1_crystal(volatile avr32_pm_t *pm, unsigned int fosc1);
  134. /*!
  135. * \brief This function will enable the oscillator 1 to be used with a startup time.
  136. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  137. * \param startup Clock 1 startup time. AVR32_PM_OSCCTRL1_STARTUP_x_RCOSC.
  138. */
  139. extern void pm_enable_clk1(volatile avr32_pm_t *pm, unsigned int startup);
  140. /*!
  141. * \brief This function will disable the oscillator 1.
  142. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  143. */
  144. extern void pm_disable_clk1(volatile avr32_pm_t *pm);
  145. /*!
  146. * \brief This function will enable the oscillator 1 to be used with no startup time.
  147. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  148. * \param startup Clock 1 startup time, for which the function does not wait. AVR32_PM_OSCCTRL1_STARTUP_x_RCOSC.
  149. */
  150. extern void pm_enable_clk1_no_wait(volatile avr32_pm_t *pm, unsigned int startup);
  151. /*!
  152. * \brief This function will wait until the Osc1 clock is ready.
  153. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  154. */
  155. extern void pm_wait_for_clk1_ready(volatile avr32_pm_t *pm);
  156. /*!
  157. * \brief This function will enable the external clock mode of the 32-kHz oscillator.
  158. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  159. */
  160. extern void pm_enable_osc32_ext_clock(volatile avr32_pm_t *pm);
  161. /*!
  162. * \brief This function will enable the crystal mode of the 32-kHz oscillator.
  163. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  164. */
  165. extern void pm_enable_osc32_crystal(volatile avr32_pm_t *pm);
  166. /*!
  167. * \brief This function will enable the oscillator 32 to be used with a startup time.
  168. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  169. * \param startup Clock 32 kHz startup time. AVR32_PM_OSCCTRL32_STARTUP_x_RCOSC.
  170. */
  171. extern void pm_enable_clk32(volatile avr32_pm_t *pm, unsigned int startup);
  172. /*!
  173. * \brief This function will disable the oscillator 32.
  174. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  175. */
  176. extern void pm_disable_clk32(volatile avr32_pm_t *pm);
  177. /*!
  178. * \brief This function will enable the oscillator 32 to be used with no startup time.
  179. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  180. * \param startup Clock 32 kHz startup time, for which the function does not wait. AVR32_PM_OSCCTRL32_STARTUP_x_RCOSC.
  181. */
  182. extern void pm_enable_clk32_no_wait(volatile avr32_pm_t *pm, unsigned int startup);
  183. /*!
  184. * \brief This function will wait until the osc32 clock is ready.
  185. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  186. */
  187. extern void pm_wait_for_clk32_ready(volatile avr32_pm_t *pm);
  188. /*!
  189. * \brief This function will select all the power manager clocks.
  190. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  191. * \param pbadiv Peripheral Bus A clock divisor enable
  192. * \param pbasel Peripheral Bus A select
  193. * \param pbbdiv Peripheral Bus B clock divisor enable
  194. * \param pbbsel Peripheral Bus B select
  195. * \param hsbdiv High Speed Bus clock divisor enable (CPU clock = HSB clock)
  196. * \param hsbsel High Speed Bus select (CPU clock = HSB clock )
  197. */
  198. extern void pm_cksel(volatile avr32_pm_t *pm, unsigned int pbadiv, unsigned int pbasel, unsigned int pbbdiv, unsigned int pbbsel, unsigned int hsbdiv, unsigned int hsbsel);
  199. /*!
  200. * \brief This function will setup a generic clock.
  201. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  202. * \param gc generic clock number (0 for gc0...)
  203. * \param osc_or_pll Use OSC (=0) or PLL (=1)
  204. * \param pll_osc Select Osc0/PLL0 or Osc1/PLL1
  205. * \param diven Generic clock divisor enable
  206. * \param div Generic clock divisor
  207. */
  208. extern void pm_gc_setup(volatile avr32_pm_t *pm, unsigned int gc, unsigned int osc_or_pll, unsigned int pll_osc, unsigned int diven, unsigned int div);
  209. /*!
  210. * \brief This function will enable a generic clock.
  211. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  212. * \param gc generic clock number (0 for gc0...)
  213. */
  214. extern void pm_gc_enable(volatile avr32_pm_t *pm, unsigned int gc);
  215. /*!
  216. * \brief This function will disable a generic clock.
  217. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  218. * \param gc generic clock number (0 for gc0...)
  219. */
  220. extern void pm_gc_disable(volatile avr32_pm_t *pm, unsigned int gc);
  221. /*!
  222. * \brief This function will setup a PLL.
  223. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  224. * \param pll PLL number(0 for PLL0, 1 for PLL1)
  225. * \param mul PLL MUL in the PLL formula
  226. * \param div PLL DIV in the PLL formula
  227. * \param osc OSC number (0 for osc0, 1 for osc1)
  228. * \param lockcount PLL lockount
  229. */
  230. extern void pm_pll_setup(volatile avr32_pm_t *pm, unsigned int pll, unsigned int mul, unsigned int div, unsigned int osc, unsigned int lockcount);
  231. /*!
  232. * \brief This function will set a PLL option.
  233. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  234. * \param pll PLL number(0 for PLL0, 1 for PLL1)
  235. * \param pll_freq Set to 1 for VCO frequency range 80-180MHz, set to 0 for VCO frequency range 160-240Mhz.
  236. * \param pll_div2 Divide the PLL output frequency by 2 (this settings does not change the FVCO value)
  237. * \param 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.
  238. */
  239. extern void pm_pll_set_option(volatile avr32_pm_t *pm, unsigned int pll, unsigned int pll_freq, unsigned int pll_div2, unsigned int pll_wbwdisable);
  240. /*!
  241. * \brief This function will get a PLL option.
  242. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  243. * \param pll PLL number(0 for PLL0, 1 for PLL1)
  244. * \return Option
  245. */
  246. extern unsigned int pm_pll_get_option(volatile avr32_pm_t *pm, unsigned int pll);
  247. /*!
  248. * \brief This function will enable a PLL.
  249. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  250. * \param pll PLL number(0 for PLL0, 1 for PLL1)
  251. */
  252. extern void pm_pll_enable(volatile avr32_pm_t *pm, unsigned int pll);
  253. /*!
  254. * \brief This function will disable a PLL.
  255. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  256. * \param pll PLL number(0 for PLL0, 1 for PLL1)
  257. */
  258. extern void pm_pll_disable(volatile avr32_pm_t *pm, unsigned int pll);
  259. /*!
  260. * \brief This function will wait for PLL0 locked
  261. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  262. */
  263. extern void pm_wait_for_pll0_locked(volatile avr32_pm_t *pm);
  264. /*!
  265. * \brief This function will wait for PLL1 locked
  266. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  267. */
  268. extern void pm_wait_for_pll1_locked(volatile avr32_pm_t *pm);
  269. /*!
  270. * \brief This function will switch the power manager main clock.
  271. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  272. * \param clock Clock to be switched on. AVR32_PM_MCSEL_SLOW for RCOsc, AVR32_PM_MCSEL_OSC0 for Osc0, AVR32_PM_MCSEL_PLL0 for PLL0.
  273. */
  274. extern void pm_switch_to_clock(volatile avr32_pm_t *pm, unsigned long clock);
  275. /*!
  276. * \brief Switch main clock to clock Osc0 (crystal mode)
  277. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  278. * \param fosc0 Oscillator 0 crystal frequency (Hz)
  279. * \param startup Crystal 0 startup time. AVR32_PM_OSCCTRL0_STARTUP_x_RCOSC.
  280. */
  281. extern void pm_switch_to_osc0(volatile avr32_pm_t *pm, unsigned int fosc0, unsigned int startup);
  282. /*! \brief Enables the Brown-Out Detector interrupt.
  283. *
  284. * \param pm Base address of the Power Manager (i.e. &AVR32_PM).
  285. */
  286. extern void pm_bod_enable_irq(volatile avr32_pm_t *pm);
  287. /*! \brief Disables the Brown-Out Detector interrupt.
  288. *
  289. * \param pm Base address of the Power Manager (i.e. &AVR32_PM).
  290. */
  291. extern void pm_bod_disable_irq(volatile avr32_pm_t *pm);
  292. /*! \brief Clears the Brown-Out Detector interrupt flag.
  293. *
  294. * \param pm Base address of the Power Manager (i.e. &AVR32_PM).
  295. */
  296. extern void pm_bod_clear_irq(volatile avr32_pm_t *pm);
  297. /*! \brief Gets the Brown-Out Detector interrupt flag.
  298. *
  299. * \param pm Base address of the Power Manager (i.e. &AVR32_PM).
  300. *
  301. * \retval 0 No BOD interrupt.
  302. * \retval 1 BOD interrupt pending.
  303. */
  304. extern unsigned long pm_bod_get_irq_status(volatile avr32_pm_t *pm);
  305. /*! \brief Gets the Brown-Out Detector interrupt enable status.
  306. *
  307. * \param pm Base address of the Power Manager (i.e. &AVR32_PM).
  308. *
  309. * \retval 0 BOD interrupt disabled.
  310. * \retval 1 BOD interrupt enabled.
  311. */
  312. extern unsigned long pm_bod_get_irq_enable_bit(volatile avr32_pm_t *pm);
  313. /*! \brief Gets the triggering threshold of the Brown-Out Detector.
  314. *
  315. * \param pm Base address of the Power Manager (i.e. &AVR32_PM).
  316. *
  317. * \return Triggering threshold of the BOD. See the electrical characteristics
  318. * in the part datasheet for actual voltage levels.
  319. */
  320. extern unsigned long pm_bod_get_level(volatile avr32_pm_t *pm);
  321. /*!
  322. * \brief Read the content of the PM GPLP registers
  323. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  324. * \param gplp GPLP register index (0,1,... depending on the number of GPLP registers for a given part)
  325. *
  326. * \return The content of the chosen GPLP register.
  327. */
  328. extern unsigned long pm_read_gplp(volatile avr32_pm_t *pm, unsigned long gplp);
  329. /*!
  330. * \brief Write into the PM GPLP registers
  331. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  332. * \param gplp GPLP register index (0,1,... depending on the number of GPLP registers for a given part)
  333. * \param value Value to write
  334. */
  335. extern void pm_write_gplp(volatile avr32_pm_t *pm, unsigned long gplp, unsigned long value);
  336. /*! \brief Enable the clock of a module.
  337. *
  338. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  339. * \param module The module to clock (use one of the defines in the part-specific
  340. * header file under "toolchain folder"/avr32/inc(lude)/avr32/; depending on the
  341. * clock domain, look for the sections "CPU clocks", "HSB clocks", "PBx clocks")
  342. *
  343. * \return Status.
  344. * \retval 0 Success.
  345. * \retval <0 An error occured.
  346. */
  347. extern long pm_enable_module(volatile avr32_pm_t *pm, unsigned long module);
  348. /*! \brief Disable the clock of a module.
  349. *
  350. * \param pm Base address of the Power Manager (i.e. &AVR32_PM)
  351. * \param module The module to shut down (use one of the defines in the part-specific
  352. * header file under "toolchain folder"/avr32/inc(lude)/avr32/; depending on the
  353. * clock domain, look for the sections "CPU clocks", "HSB clocks", "PBx clocks")
  354. *
  355. * \return Status.
  356. * \retval 0 Success.
  357. * \retval <0 An error occured.
  358. */
  359. extern long pm_disable_module(volatile avr32_pm_t *pm, unsigned long module);
  360. /*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks
  361. * according to the user wishes.
  362. *
  363. * This function needs some parameters stored in a pm_freq_param_t structure:
  364. * - cpu_f and pba_f are the wanted frequencies,
  365. * - osc0_f is the oscillator 0 on-board frequency (e.g. FOSC0),
  366. * - osc0_startup is the oscillator 0 startup time (e.g. OSC0_STARTUP).
  367. *
  368. * The function will then configure the clocks using the following rules:
  369. * - It first try to find a valid PLL frequency (the highest possible value to avoid jitter) in order
  370. * to satisfy the CPU frequency,
  371. * - It optimizes the configuration depending the various divide stages,
  372. * - Then, the PBA frequency is configured from the CPU freq.
  373. * - Note that HSB and PBB are configured with the same frequency as CPU.
  374. * - Note also that the number of wait states of the flash read accesses is automatically set-up depending
  375. * the CPU frequency. As a consequence, the application needs the FLASHC driver to compile.
  376. *
  377. * The CPU, HSB and PBA frequencies programmed after configuration are stored back into cpu_f and pba_f.
  378. *
  379. * \param param pointer on the configuration structure.
  380. *
  381. * \retval PM_FREQ_STATUS_OK Mode successfully initialized.
  382. * \retval PM_FREQ_STATUS_FAIL The configuration can not be done.
  383. */
  384. extern int pm_configure_clocks(pm_freq_param_t *param);
  385. /*! \brief Automatically configure the USB clock.
  386. *
  387. * USB clock is configured to 48MHz, using the PLL1 from the Oscillator0, assuming
  388. * a 12 MHz crystal is connected to it.
  389. */
  390. extern void pm_configure_usb_clock(void);
  391. #endif // _PM_H_