pm_uc3l.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*This file has been prepared for Doxygen automatic documentation generation.*/
  2. /*! \file *********************************************************************
  3. *
  4. * \brief Power Manager(PM) driver interface.
  5. *
  6. *
  7. * - Compiler: IAR EWAVR32 and GNU GCC for AVR32
  8. * - Supported devices: All AVR32 UC3L devices.
  9. * - AppNote:
  10. *
  11. * \author Atmel Corporation: http://www.atmel.com \n
  12. * Support and FAQ: http://support.atmel.no/
  13. *
  14. *****************************************************************************/
  15. /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions are met:
  19. *
  20. * 1. Redistributions of source code must retain the above copyright notice, this
  21. * list of conditions and the following disclaimer.
  22. *
  23. * 2. Redistributions in binary form must reproduce the above copyright notice,
  24. * this list of conditions and the following disclaimer in the documentation
  25. * and/or other materials provided with the distribution.
  26. *
  27. * 3. The name of Atmel may not be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * 4. This software may only be redistributed and used in connection with an Atmel
  31. * AVR product.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  34. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  35. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  36. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  37. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  38. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  40. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  41. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  42. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  43. *
  44. */
  45. #ifndef _PM_UC3L_H_
  46. #define _PM_UC3L_H_
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. #include <avr32/io.h>
  51. #include "compiler.h"
  52. //! Device-specific data
  53. //! The clock sources of the power manager.
  54. typedef enum
  55. {
  56. PM_CLK_SRC_SLOW = AVR32_PM_MCSEL_SLOW,
  57. PM_CLK_SRC_OSC0 = AVR32_PM_MCSEL_OSC0,
  58. PM_CLK_SRC_DFLL0 = AVR32_PM_MCSEL_DFLL0,
  59. PM_CLK_SRC_RC120M = AVR32_PM_MCSEL_RC120M,
  60. PM_CLK_SRC_INVALID
  61. } pm_clk_src_t;
  62. //! The clock domains of the power manager.
  63. typedef enum
  64. {
  65. PM_CLK_DOMAIN_0 = AVR32_PM_CLK_GRP_CPU,
  66. PM_CLK_DOMAIN_1 = AVR32_PM_CLK_GRP_HSB,
  67. PM_CLK_DOMAIN_2 = AVR32_PM_CLK_GRP_PBA,
  68. PM_CLK_DOMAIN_3 = AVR32_PM_CLK_GRP_PBB,
  69. PM_CLK_DOMAIN_INVALID
  70. } pm_clk_domain_t;
  71. //! The possible synchronous clock division ratio.
  72. typedef enum
  73. {
  74. PM_CKSEL_DIVRATIO_2 = 0, // Divide the main clock by 2^1
  75. PM_CKSEL_DIVRATIO_4, // Divide the main clock by 2^2
  76. PM_CKSEL_DIVRATIO_8, // Divide the main clock by 2^3
  77. PM_CKSEL_DIVRATIO_16, // Divide the main clock by 2^4
  78. PM_CKSEL_DIVRATIO_32, // Divide the main clock by 2^5
  79. PM_CKSEL_DIVRATIO_64, // Divide the main clock by 2^6
  80. PM_CKSEL_DIVRATIO_128, // Divide the main clock by 2^7
  81. PM_CKSEL_DIVRATIO_256, // Divide the main clock by 2^8
  82. PM_CKSEL_DIVRATIO_ERROR
  83. } pm_divratio_t;
  84. //! The maximum synchronous clock division ratio for each clock domain.
  85. typedef enum
  86. {
  87. PM_CPUSEL_DIVRATIO_MAX = AVR32_PM_CPUSEL_CPUSEL_MASK >> AVR32_PM_CPUSEL_CPUSEL_OFFSET,
  88. PM_HSBSEL_DIVRATIO_MAX = AVR32_PM_HSBSEL_HSBSEL_MASK >> AVR32_PM_HSBSEL_HSBSEL_OFFSET,
  89. PM_PBASEL_DIVRATIO_MAX = AVR32_PM_PBASEL_PBSEL_MASK >> AVR32_PM_PBASEL_PBSEL_OFFSET,
  90. PM_PBBSEL_DIVRATIO_MAX = AVR32_PM_PBBSEL_PBSEL_MASK >> AVR32_PM_PBBSEL_PBSEL_OFFSET
  91. } pm_divratio_max_t;
  92. //! The timeguard used for polling (expressed in ticks).
  93. #define PM_POLL_TIMEOUT 100000
  94. //! Define "not supported" for the power manager features.
  95. #define PM_NOT_SUPPORTED (-10000)
  96. //! Unlock PM register macro
  97. #define PM_UNLOCK(reg) (AVR32_PM.unlock = (unsigned long)(AVR32_PM_UNLOCK_KEY_VALUE << AVR32_PM_UNLOCK_KEY_OFFSET)|(reg))
  98. /*! \name Clock Functions
  99. */
  100. //! @{
  101. /*! \brief Set the main clock.
  102. *
  103. * \param src The clock to use as the main clock.
  104. *
  105. * \warning The input clock to use as the main clock must be enabled before calling
  106. * this function, otherwise a deadlock will occur.
  107. *
  108. * \return Status.
  109. * \retval 0 Success.
  110. * \retval <0 An error occured when trying to set the main clock.
  111. */
  112. extern long pm_set_mclk_source(pm_clk_src_t src);
  113. /*! \brief Configure the main clock safety mechanisms.
  114. *
  115. * \note Refer to the section Clock Failure Detector of the PM chapter in the datasheet
  116. *
  117. * \warning The Critical Path Oscillator (CRIPOSC) must be enabled before
  118. * enabling the Over Clock Protection mechanism.
  119. *
  120. * \param cfd Enable/disable the Clock Failure Detection mechanism
  121. * \param ocp Enable/disable the Over Clock Protection mechanism
  122. * \param final If true, make this configuration definitive
  123. *
  124. * \return Status.
  125. * \retval =0 Success.
  126. * \retval <0 An error occured.
  127. */
  128. extern long pm_config_mainclk_safety(bool cfd, bool ocp, bool final);
  129. /*! \brief Set the division ratio for a clock domain.
  130. *
  131. * \param clock_domain The clock domain to alter.
  132. * \param divratio The division ratio to set.
  133. *
  134. * \warning Care should be taken that each new frequency of the synchronous clocks
  135. * does not exceed the maximum frequency for each clock domain.
  136. *
  137. * \return Status.
  138. * \retval =0 Success.
  139. * \retval <0 An error occured.
  140. */
  141. extern long pm_set_clk_domain_div(pm_clk_domain_t clock_domain, pm_divratio_t divratio);
  142. /*! \brief Disable the division ratio for a clock domain.
  143. *
  144. * \param clock_domain The clock domain to alter.
  145. *
  146. * \warning Care should be taken that each new frequency of the synchronous clocks
  147. * does not exceed the maximum frequency for each clock domain.
  148. *
  149. * \return Status.
  150. * \retval =0 Success.
  151. * \retval <0 An error occured.
  152. */
  153. extern long pm_disable_clk_domain_div(pm_clk_domain_t clock_domain);
  154. /*! \brief Enable or Disable the division ratio for each clock domain depending
  155. * on the main source clock frequency and each clock domain target frequency.
  156. *
  157. * \param main_clock_f_hz The main source clock frequency
  158. * \param cpu_f_hz The target CPU clock domain frequency
  159. * \param pba_f_hz The target PBA clock domain frequency
  160. * \param pbb_f_hz The target PBB clock domain frequency
  161. *
  162. * \warning Care should be taken that each new frequency of the synchronous clocks
  163. * does not exceed the maximum frequency for each clock domain.
  164. *
  165. */
  166. extern void pm_set_all_cksel( unsigned long main_clock_f_hz, unsigned long cpu_f_hz,
  167. unsigned long pba_f_hz, unsigned long pbb_f_hz );
  168. /*! \brief Wait actively for the clock settings to be effective.
  169. *
  170. * \note To avoid an infinite loop, this function checks the clock ready flag
  171. * PM_POLL_TIMEOUT times.
  172. *
  173. * \return Status.
  174. * \retval 0 Success.
  175. * \retval <0 Unable to reach a clock ready status within the polling limit.
  176. */
  177. extern long pm_wait_for_clk_ready(void);
  178. //! @}
  179. /*! \name Module Functions
  180. */
  181. //! @{
  182. /*! \brief Enable the clock of a module.
  183. *
  184. * \param module The module to clock (use one of the defines in the part-specific
  185. * header file under "toolchain folder"/avr32/inc(lude)/avr32/; depending on the
  186. * clock domain, look for the sections "CPU clocks", "HSB clocks", "PBx clocks")
  187. *
  188. * \return Status.
  189. * \retval 0 Success.
  190. * \retval <0 An error occured.
  191. */
  192. extern long pm_enable_module(unsigned long module);
  193. /*! \brief Disable the clock of a module.
  194. *
  195. * \param module The module to shut down (use one of the defines in the part-specific
  196. * header file under "toolchain folder"/avr32/inc(lude)/avr32/; depending on the
  197. * clock domain, look for the sections "CPU clocks", "HSB clocks", "PBx clocks")
  198. *
  199. * \return Status.
  200. * \retval 0 Success.
  201. * \retval <0 An error occured.
  202. */
  203. extern long pm_disable_module(unsigned long module);
  204. //! @}
  205. /*! \name Sleep Functions
  206. */
  207. //! @{
  208. /*! \brief Sets the MCU in the specified sleep mode
  209. *
  210. * \note Precautions should be taken before entering sleep mode. Refer to the
  211. * datasheet chapter 'Power Manager'.
  212. *
  213. * \param sleep_mode The sleep mode index
  214. */
  215. #define SLEEP(sleep_mode) {__asm__ __volatile__ ("sleep "STRINGZ(sleep_mode));}
  216. /*! \brief Returns MCU wake cause
  217. *
  218. * \return The MCU wake cause which can be masked with the
  219. * \c AVR32_PM_WCAUSE_x_MASK bit-masks to isolate specific causes.
  220. */
  221. #ifdef __GNUC__
  222. NUT_FORCE_INLINE
  223. #endif
  224. static inline unsigned long pm_get_wake_cause(void)
  225. {
  226. return AVR32_PM.wcause;
  227. }
  228. /*! \brief Enable one or several asynchronous wake-up source.
  229. *
  230. * \param awen_mask Mask of asynchronous wake-up sources (use one of the defines
  231. * AVR32_PM_AWEN_xxxxWEN_MASK in the part-specific header file under
  232. * "toolchain folder"/avr32/inc(lude)/avr32/)
  233. */
  234. #ifdef __GNUC__
  235. NUT_FORCE_INLINE
  236. #endif
  237. static inline void pm_asyn_wake_up_enable(unsigned long awen_mask)
  238. {
  239. AVR32_PM.awen |= awen_mask;
  240. }
  241. /*! \brief Disable one or several asynchronous wake-up sources
  242. *
  243. * \param awen_mask Mask of asynchronous wake-up sources (use one of the defines
  244. * AVR32_PM_AWEN_xxxxWEN_MASK in the part-specific header file under
  245. * "toolchain folder"/avr32/inc(lude)/avr32/)
  246. */
  247. #ifdef __GNUC__
  248. NUT_FORCE_INLINE
  249. #endif
  250. static inline void pm_asyn_wake_up_disable(unsigned long awen_mask)
  251. {
  252. AVR32_PM.awen &= ~awen_mask;
  253. }
  254. //! @}
  255. /*! \name Reset Functions
  256. */
  257. //! @{
  258. /*! \brief Returns MCU last reset cause
  259. *
  260. * \return The MCU last reset cause which can be masked with the
  261. * \c AVR32_PM_RCAUSE_x_MASK bit-masks to isolate specific causes.
  262. */
  263. #ifdef __GNUC__
  264. NUT_FORCE_INLINE
  265. #endif
  266. static inline unsigned long pm_get_reset_cause(void)
  267. {
  268. return AVR32_PM.rcause;
  269. }
  270. //! @}
  271. /*! \name Interrupt Functions
  272. */
  273. //! @{
  274. /*! \brief Enable power manager interrupts.
  275. *
  276. * \param mask the interrupts to enable.
  277. *
  278. */
  279. #ifdef __GNUC__
  280. NUT_FORCE_INLINE
  281. #endif
  282. static inline void pm_enable_interrupts(unsigned long mask)
  283. {
  284. AVR32_PM.ier |= mask;
  285. }
  286. /*! \brief Disable power manager interrupts.
  287. *
  288. * \param mask the interrupts to disable.
  289. *
  290. */
  291. #ifdef __GNUC__
  292. NUT_FORCE_INLINE
  293. #endif
  294. static inline void pm_disable_interrupts(unsigned long mask)
  295. {
  296. AVR32_PM.idr |= mask;
  297. }
  298. /*! \brief Read the enabled power manager interrupts.
  299. *
  300. * \return mask of the enabled interrupts.
  301. */
  302. #ifdef __GNUC__
  303. NUT_FORCE_INLINE
  304. #endif
  305. static inline unsigned long pm_get_enabled_interrupts(void)
  306. {
  307. return(AVR32_PM.imr);
  308. }
  309. /*! \brief Read the interrupts status of the power manager.
  310. *
  311. * \return mask of the interrupts that have been triggered.
  312. */
  313. #ifdef __GNUC__
  314. NUT_FORCE_INLINE
  315. #endif
  316. static inline unsigned long pm_get_interrupts_status(void)
  317. {
  318. return(AVR32_PM.isr);
  319. }
  320. /*! \brief Clear raised interrupts from the power manager.
  321. *
  322. * \param mask The interrupts to clear.
  323. */
  324. #ifdef __GNUC__
  325. NUT_FORCE_INLINE
  326. #endif
  327. static inline void pm_clear_interrupt_status(unsigned long mask)
  328. {
  329. AVR32_PM.icr |= mask;
  330. }
  331. //! @}
  332. /*! \name Misc Functions
  333. */
  334. //! @{
  335. /*! \brief Get the PM status
  336. *
  337. * \return The content of the PM Status register.
  338. */
  339. #ifdef __GNUC__
  340. NUT_FORCE_INLINE
  341. #endif
  342. static inline unsigned long pm_get_status(void)
  343. {
  344. return AVR32_PM.sr;
  345. }
  346. //! @}
  347. #ifdef __cplusplus
  348. }
  349. #endif
  350. #endif // _PM_UC3L_H_