My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

genclk.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * \file
  3. *
  4. * \brief Chip-specific generic clock management.
  5. *
  6. * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="https://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef CHIP_GENCLK_H_INCLUDED
  47. #define CHIP_GENCLK_H_INCLUDED
  48. #include <osc.h>
  49. #include <pll.h>
  50. /// @cond 0
  51. /**INDENT-OFF**/
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**INDENT-ON**/
  56. /// @endcond
  57. /**
  58. * \weakgroup genclk_group
  59. * @{
  60. */
  61. //! \name Programmable Clock Identifiers (PCK)
  62. //@{
  63. #define GENCLK_PCK_0 0 //!< PCK0 ID
  64. #define GENCLK_PCK_1 1 //!< PCK1 ID
  65. #define GENCLK_PCK_2 2 //!< PCK2 ID
  66. //@}
  67. //! \name Programmable Clock Sources (PCK)
  68. //@{
  69. enum genclk_source {
  70. GENCLK_PCK_SRC_SLCK_RC = 0, //!< Internal 32kHz RC oscillator as PCK source clock
  71. GENCLK_PCK_SRC_SLCK_XTAL = 1, //!< External 32kHz crystal oscillator as PCK source clock
  72. GENCLK_PCK_SRC_SLCK_BYPASS = 2, //!< External 32kHz bypass oscillator as PCK source clock
  73. GENCLK_PCK_SRC_MAINCK_4M_RC = 3, //!< Internal 4MHz RC oscillator as PCK source clock
  74. GENCLK_PCK_SRC_MAINCK_8M_RC = 4, //!< Internal 8MHz RC oscillator as PCK source clock
  75. GENCLK_PCK_SRC_MAINCK_12M_RC = 5, //!< Internal 12MHz RC oscillator as PCK source clock
  76. GENCLK_PCK_SRC_MAINCK_XTAL = 6, //!< External crystal oscillator as PCK source clock
  77. GENCLK_PCK_SRC_MAINCK_BYPASS = 7, //!< External bypass oscillator as PCK source clock
  78. GENCLK_PCK_SRC_PLLACK = 8, //!< Use PLLACK as PCK source clock
  79. GENCLK_PCK_SRC_PLLBCK = 9, //!< Use PLLBCK as PCK source clock
  80. GENCLK_PCK_SRC_MCK = 10, //!< Use Master Clk as PCK source clock
  81. };
  82. //@}
  83. //! \name Programmable Clock Prescalers (PCK)
  84. //@{
  85. enum genclk_divider {
  86. GENCLK_PCK_PRES_1 = PMC_PCK_PRES_CLK_1, //!< Set PCK clock prescaler to 1
  87. GENCLK_PCK_PRES_2 = PMC_PCK_PRES_CLK_2, //!< Set PCK clock prescaler to 2
  88. GENCLK_PCK_PRES_4 = PMC_PCK_PRES_CLK_4, //!< Set PCK clock prescaler to 4
  89. GENCLK_PCK_PRES_8 = PMC_PCK_PRES_CLK_8, //!< Set PCK clock prescaler to 8
  90. GENCLK_PCK_PRES_16 = PMC_PCK_PRES_CLK_16, //!< Set PCK clock prescaler to 16
  91. GENCLK_PCK_PRES_32 = PMC_PCK_PRES_CLK_32, //!< Set PCK clock prescaler to 32
  92. GENCLK_PCK_PRES_64 = PMC_PCK_PRES_CLK_64, //!< Set PCK clock prescaler to 64
  93. };
  94. //@}
  95. struct genclk_config {
  96. uint32_t ctrl;
  97. };
  98. static inline void genclk_config_defaults(struct genclk_config *p_cfg,
  99. uint32_t ul_id)
  100. {
  101. ul_id = ul_id;
  102. p_cfg->ctrl = 0;
  103. }
  104. static inline void genclk_config_read(struct genclk_config *p_cfg,
  105. uint32_t ul_id)
  106. {
  107. p_cfg->ctrl = PMC->PMC_PCK[ul_id];
  108. }
  109. static inline void genclk_config_write(const struct genclk_config *p_cfg,
  110. uint32_t ul_id)
  111. {
  112. PMC->PMC_PCK[ul_id] = p_cfg->ctrl;
  113. }
  114. //! \name Programmable Clock Source and Prescaler configuration
  115. //@{
  116. static inline void genclk_config_set_source(struct genclk_config *p_cfg,
  117. enum genclk_source e_src)
  118. {
  119. p_cfg->ctrl &= (~PMC_PCK_CSS_Msk);
  120. switch (e_src) {
  121. case GENCLK_PCK_SRC_SLCK_RC:
  122. case GENCLK_PCK_SRC_SLCK_XTAL:
  123. case GENCLK_PCK_SRC_SLCK_BYPASS:
  124. p_cfg->ctrl |= (PMC_PCK_CSS_SLOW_CLK);
  125. break;
  126. case GENCLK_PCK_SRC_MAINCK_4M_RC:
  127. case GENCLK_PCK_SRC_MAINCK_8M_RC:
  128. case GENCLK_PCK_SRC_MAINCK_12M_RC:
  129. case GENCLK_PCK_SRC_MAINCK_XTAL:
  130. case GENCLK_PCK_SRC_MAINCK_BYPASS:
  131. p_cfg->ctrl |= (PMC_PCK_CSS_MAIN_CLK);
  132. break;
  133. case GENCLK_PCK_SRC_PLLACK:
  134. p_cfg->ctrl |= (PMC_PCK_CSS_PLLA_CLK);
  135. break;
  136. case GENCLK_PCK_SRC_PLLBCK:
  137. p_cfg->ctrl |= (PMC_PCK_CSS_UPLL_CLK);
  138. break;
  139. case GENCLK_PCK_SRC_MCK:
  140. p_cfg->ctrl |= (PMC_PCK_CSS_MCK);
  141. break;
  142. }
  143. }
  144. static inline void genclk_config_set_divider(struct genclk_config *p_cfg,
  145. uint32_t e_divider)
  146. {
  147. p_cfg->ctrl &= ~PMC_PCK_PRES_Msk;
  148. p_cfg->ctrl |= e_divider;
  149. }
  150. //@}
  151. static inline void genclk_enable(const struct genclk_config *p_cfg,
  152. uint32_t ul_id)
  153. {
  154. PMC->PMC_PCK[ul_id] = p_cfg->ctrl;
  155. pmc_enable_pck(ul_id);
  156. }
  157. static inline void genclk_disable(uint32_t ul_id)
  158. {
  159. pmc_disable_pck(ul_id);
  160. }
  161. static inline void genclk_enable_source(enum genclk_source e_src)
  162. {
  163. switch (e_src) {
  164. case GENCLK_PCK_SRC_SLCK_RC:
  165. if (!osc_is_ready(OSC_SLCK_32K_RC)) {
  166. osc_enable(OSC_SLCK_32K_RC);
  167. osc_wait_ready(OSC_SLCK_32K_RC);
  168. }
  169. break;
  170. case GENCLK_PCK_SRC_SLCK_XTAL:
  171. if (!osc_is_ready(OSC_SLCK_32K_XTAL)) {
  172. osc_enable(OSC_SLCK_32K_XTAL);
  173. osc_wait_ready(OSC_SLCK_32K_XTAL);
  174. }
  175. break;
  176. case GENCLK_PCK_SRC_SLCK_BYPASS:
  177. if (!osc_is_ready(OSC_SLCK_32K_BYPASS)) {
  178. osc_enable(OSC_SLCK_32K_BYPASS);
  179. osc_wait_ready(OSC_SLCK_32K_BYPASS);
  180. }
  181. break;
  182. case GENCLK_PCK_SRC_MAINCK_4M_RC:
  183. if (!osc_is_ready(OSC_MAINCK_4M_RC)) {
  184. osc_enable(OSC_MAINCK_4M_RC);
  185. osc_wait_ready(OSC_MAINCK_4M_RC);
  186. }
  187. break;
  188. case GENCLK_PCK_SRC_MAINCK_8M_RC:
  189. if (!osc_is_ready(OSC_MAINCK_8M_RC)) {
  190. osc_enable(OSC_MAINCK_8M_RC);
  191. osc_wait_ready(OSC_MAINCK_8M_RC);
  192. }
  193. break;
  194. case GENCLK_PCK_SRC_MAINCK_12M_RC:
  195. if (!osc_is_ready(OSC_MAINCK_12M_RC)) {
  196. osc_enable(OSC_MAINCK_12M_RC);
  197. osc_wait_ready(OSC_MAINCK_12M_RC);
  198. }
  199. break;
  200. case GENCLK_PCK_SRC_MAINCK_XTAL:
  201. if (!osc_is_ready(OSC_MAINCK_XTAL)) {
  202. osc_enable(OSC_MAINCK_XTAL);
  203. osc_wait_ready(OSC_MAINCK_XTAL);
  204. }
  205. break;
  206. case GENCLK_PCK_SRC_MAINCK_BYPASS:
  207. if (!osc_is_ready(OSC_MAINCK_BYPASS)) {
  208. osc_enable(OSC_MAINCK_BYPASS);
  209. osc_wait_ready(OSC_MAINCK_BYPASS);
  210. }
  211. break;
  212. #ifdef CONFIG_PLL0_SOURCE
  213. case GENCLK_PCK_SRC_PLLACK:
  214. pll_enable_config_defaults(0);
  215. break;
  216. #endif
  217. #ifdef CONFIG_PLL1_SOURCE
  218. case GENCLK_PCK_SRC_PLLBCK:
  219. pll_enable_config_defaults(1);
  220. break;
  221. #endif
  222. case GENCLK_PCK_SRC_MCK:
  223. break;
  224. default:
  225. Assert(false);
  226. break;
  227. }
  228. }
  229. //! @}
  230. /// @cond 0
  231. /**INDENT-OFF**/
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. /**INDENT-ON**/
  236. /// @endcond
  237. #endif /* CHIP_GENCLK_H_INCLUDED */