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.

osc.h 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * \file
  3. *
  4. * \brief Chip-specific oscillator management functions.
  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_OSC_H_INCLUDED
  47. #define CHIP_OSC_H_INCLUDED
  48. #include "compiler.h"
  49. /// @cond 0
  50. /**INDENT-OFF**/
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**INDENT-ON**/
  55. /// @endcond
  56. /*
  57. * Below BOARD_XXX macros are related to the specific board, and
  58. * should be defined by the board code, otherwise default value are used.
  59. */
  60. #ifndef BOARD_FREQ_SLCK_XTAL
  61. # warning The board slow clock xtal frequency has not been defined.
  62. # define BOARD_FREQ_SLCK_XTAL (32768UL)
  63. #endif
  64. #ifndef BOARD_FREQ_SLCK_BYPASS
  65. # warning The board slow clock bypass frequency has not been defined.
  66. # define BOARD_FREQ_SLCK_BYPASS (32768UL)
  67. #endif
  68. #ifndef BOARD_FREQ_MAINCK_XTAL
  69. # warning The board main clock xtal frequency has not been defined.
  70. # define BOARD_FREQ_MAINCK_XTAL (12000000UL)
  71. #endif
  72. #ifndef BOARD_FREQ_MAINCK_BYPASS
  73. # warning The board main clock bypass frequency has not been defined.
  74. # define BOARD_FREQ_MAINCK_BYPASS (12000000UL)
  75. #endif
  76. #ifndef BOARD_OSC_STARTUP_US
  77. # warning The board main clock xtal startup time has not been defined.
  78. # define BOARD_OSC_STARTUP_US (15625UL)
  79. #endif
  80. /**
  81. * \weakgroup osc_group
  82. * @{
  83. */
  84. //! \name Oscillator identifiers
  85. //@{
  86. #define OSC_SLCK_32K_RC 0 //!< Internal 32kHz RC oscillator.
  87. #define OSC_SLCK_32K_XTAL 1 //!< External 32kHz crystal oscillator.
  88. #define OSC_SLCK_32K_BYPASS 2 //!< External 32kHz bypass oscillator.
  89. #define OSC_MAINCK_4M_RC 3 //!< Internal 4MHz RC oscillator.
  90. #define OSC_MAINCK_8M_RC 4 //!< Internal 8MHz RC oscillator.
  91. #define OSC_MAINCK_12M_RC 5 //!< Internal 12MHz RC oscillator.
  92. #define OSC_MAINCK_XTAL 6 //!< External crystal oscillator.
  93. #define OSC_MAINCK_BYPASS 7 //!< External bypass oscillator.
  94. //@}
  95. //! \name Oscillator clock speed in hertz
  96. //@{
  97. #define OSC_SLCK_32K_RC_HZ CHIP_FREQ_SLCK_RC //!< Internal 32kHz RC oscillator.
  98. #define OSC_SLCK_32K_XTAL_HZ BOARD_FREQ_SLCK_XTAL //!< External 32kHz crystal oscillator.
  99. #define OSC_SLCK_32K_BYPASS_HZ BOARD_FREQ_SLCK_BYPASS //!< External 32kHz bypass oscillator.
  100. #define OSC_MAINCK_4M_RC_HZ CHIP_FREQ_MAINCK_RC_4MHZ //!< Internal 4MHz RC oscillator.
  101. #define OSC_MAINCK_8M_RC_HZ CHIP_FREQ_MAINCK_RC_8MHZ //!< Internal 8MHz RC oscillator.
  102. #define OSC_MAINCK_12M_RC_HZ CHIP_FREQ_MAINCK_RC_12MHZ //!< Internal 12MHz RC oscillator.
  103. #define OSC_MAINCK_XTAL_HZ BOARD_FREQ_MAINCK_XTAL //!< External crystal oscillator.
  104. #define OSC_MAINCK_BYPASS_HZ BOARD_FREQ_MAINCK_BYPASS //!< External bypass oscillator.
  105. //@}
  106. static inline void osc_enable(uint32_t ul_id)
  107. {
  108. switch (ul_id) {
  109. case OSC_SLCK_32K_RC:
  110. break;
  111. case OSC_SLCK_32K_XTAL:
  112. pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);
  113. break;
  114. case OSC_SLCK_32K_BYPASS:
  115. pmc_switch_sclk_to_32kxtal(PMC_OSC_BYPASS);
  116. break;
  117. case OSC_MAINCK_4M_RC:
  118. pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
  119. break;
  120. case OSC_MAINCK_8M_RC:
  121. pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
  122. break;
  123. case OSC_MAINCK_12M_RC:
  124. pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
  125. break;
  126. case OSC_MAINCK_XTAL:
  127. pmc_switch_mainck_to_xtal(PMC_OSC_XTAL/*,
  128. pmc_us_to_moscxtst(BOARD_OSC_STARTUP_US,
  129. OSC_SLCK_32K_RC_HZ)*/);
  130. break;
  131. case OSC_MAINCK_BYPASS:
  132. pmc_switch_mainck_to_xtal(PMC_OSC_BYPASS/*,
  133. pmc_us_to_moscxtst(BOARD_OSC_STARTUP_US,
  134. OSC_SLCK_32K_RC_HZ)*/);
  135. break;
  136. }
  137. }
  138. static inline void osc_disable(uint32_t ul_id)
  139. {
  140. switch (ul_id) {
  141. case OSC_SLCK_32K_RC:
  142. case OSC_SLCK_32K_XTAL:
  143. case OSC_SLCK_32K_BYPASS:
  144. break;
  145. case OSC_MAINCK_4M_RC:
  146. case OSC_MAINCK_8M_RC:
  147. case OSC_MAINCK_12M_RC:
  148. pmc_osc_disable_fastrc();
  149. break;
  150. case OSC_MAINCK_XTAL:
  151. pmc_osc_disable_xtal(PMC_OSC_XTAL);
  152. break;
  153. case OSC_MAINCK_BYPASS:
  154. pmc_osc_disable_xtal(PMC_OSC_BYPASS);
  155. break;
  156. }
  157. }
  158. static inline bool osc_is_ready(uint32_t ul_id)
  159. {
  160. switch (ul_id) {
  161. case OSC_SLCK_32K_RC:
  162. return 1;
  163. case OSC_SLCK_32K_XTAL:
  164. case OSC_SLCK_32K_BYPASS:
  165. return pmc_osc_is_ready_32kxtal();
  166. case OSC_MAINCK_4M_RC:
  167. case OSC_MAINCK_8M_RC:
  168. case OSC_MAINCK_12M_RC:
  169. case OSC_MAINCK_XTAL:
  170. case OSC_MAINCK_BYPASS:
  171. return pmc_osc_is_ready_mainck();
  172. }
  173. return 0;
  174. }
  175. static inline uint32_t osc_get_rate(uint32_t ul_id)
  176. {
  177. switch (ul_id) {
  178. case OSC_SLCK_32K_RC:
  179. return OSC_SLCK_32K_RC_HZ;
  180. case OSC_SLCK_32K_XTAL:
  181. return BOARD_FREQ_SLCK_XTAL;
  182. case OSC_SLCK_32K_BYPASS:
  183. return BOARD_FREQ_SLCK_BYPASS;
  184. case OSC_MAINCK_4M_RC:
  185. return OSC_MAINCK_4M_RC_HZ;
  186. case OSC_MAINCK_8M_RC:
  187. return OSC_MAINCK_8M_RC_HZ;
  188. case OSC_MAINCK_12M_RC:
  189. return OSC_MAINCK_12M_RC_HZ;
  190. case OSC_MAINCK_XTAL:
  191. return BOARD_FREQ_MAINCK_XTAL;
  192. case OSC_MAINCK_BYPASS:
  193. return BOARD_FREQ_MAINCK_BYPASS;
  194. }
  195. return 0;
  196. }
  197. /**
  198. * \brief Wait until the oscillator identified by \a id is ready
  199. *
  200. * This function will busy-wait for the oscillator identified by \a id
  201. * to become stable and ready to use as a clock source.
  202. *
  203. * \param id A number identifying the oscillator to wait for.
  204. */
  205. static inline void osc_wait_ready(uint8_t id)
  206. {
  207. while (!osc_is_ready(id)) {
  208. /* Do nothing */
  209. }
  210. }
  211. //! @}
  212. /// @cond 0
  213. /**INDENT-OFF**/
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. /**INDENT-ON**/
  218. /// @endcond
  219. #endif /* CHIP_OSC_H_INCLUDED */