gclk.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Generic Clock Driver
  5. *
  6. * Copyright (C) 2012-2016 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="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef SYSTEM_CLOCK_GCLK_H_INCLUDED
  47. #define SYSTEM_CLOCK_GCLK_H_INCLUDED
  48. /**
  49. * \addtogroup asfdoc_sam0_system_clock_group
  50. *
  51. * @{
  52. */
  53. #include <compiler.h>
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * \brief List of available GCLK generators.
  59. *
  60. * List of Available GCLK generators. This enum is used in the peripheral
  61. * device drivers to select the GCLK generator to be used for its operation.
  62. *
  63. * The number of GCLK generators available is device dependent.
  64. */
  65. enum gclk_generator {
  66. /** GCLK generator channel 0 */
  67. GCLK_GENERATOR_0,
  68. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 0)
  69. /** GCLK generator channel 1 */
  70. GCLK_GENERATOR_1,
  71. #endif
  72. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 1)
  73. /** GCLK generator channel 2 */
  74. GCLK_GENERATOR_2,
  75. #endif
  76. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 2)
  77. /** GCLK generator channel 3 */
  78. GCLK_GENERATOR_3,
  79. #endif
  80. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 3)
  81. /** GCLK generator channel 4 */
  82. GCLK_GENERATOR_4,
  83. #endif
  84. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 4)
  85. /** GCLK generator channel 5 */
  86. GCLK_GENERATOR_5,
  87. #endif
  88. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 5)
  89. /** GCLK generator channel 6 */
  90. GCLK_GENERATOR_6,
  91. #endif
  92. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 6)
  93. /** GCLK generator channel 7 */
  94. GCLK_GENERATOR_7,
  95. #endif
  96. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 7)
  97. /** GCLK generator channel 8 */
  98. GCLK_GENERATOR_8,
  99. #endif
  100. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 8)
  101. /** GCLK generator channel 9 */
  102. GCLK_GENERATOR_9,
  103. #endif
  104. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 9)
  105. /** GCLK generator channel 10 */
  106. GCLK_GENERATOR_10,
  107. #endif
  108. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 10)
  109. /** GCLK generator channel 11 */
  110. GCLK_GENERATOR_11,
  111. #endif
  112. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 11)
  113. /** GCLK generator channel 12 */
  114. GCLK_GENERATOR_12,
  115. #endif
  116. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 12)
  117. /** GCLK generator channel 13 */
  118. GCLK_GENERATOR_13,
  119. #endif
  120. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 13)
  121. /** GCLK generator channel 14 */
  122. GCLK_GENERATOR_14,
  123. #endif
  124. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 14)
  125. /** GCLK generator channel 15 */
  126. GCLK_GENERATOR_15,
  127. #endif
  128. #if defined(__DOXYGEN__) || (GCLK_GEN_NUM_MSB > 15)
  129. /** GCLK generator channel 16 */
  130. GCLK_GENERATOR_16,
  131. #endif
  132. };
  133. /**
  134. * \brief Generic Clock Generator configuration structure.
  135. *
  136. * Configuration structure for a Generic Clock Generator channel. This
  137. * structure should be initialized by the
  138. * \ref system_gclk_gen_get_config_defaults() function before being modified by
  139. * the user application.
  140. */
  141. struct system_gclk_gen_config {
  142. /** Source clock input channel index, see the \ref system_clock_source */
  143. uint8_t source_clock;
  144. /** If \c true, the generator output level is high when disabled */
  145. bool high_when_disabled;
  146. /** Integer division factor of the clock output compared to the input */
  147. uint32_t division_factor;
  148. /** If \c true, the clock is kept enabled during device standby mode */
  149. bool run_in_standby;
  150. /** If \c true, enables GCLK generator clock output to a GPIO pin */
  151. bool output_enable;
  152. };
  153. /**
  154. * \brief Generic Clock configuration structure.
  155. *
  156. * Configuration structure for a Generic Clock channel. This structure
  157. * should be initialized by the \ref system_gclk_chan_get_config_defaults()
  158. * function before being modified by the user application.
  159. */
  160. struct system_gclk_chan_config {
  161. /** Generic Clock Generator source channel */
  162. enum gclk_generator source_generator;
  163. };
  164. /** \name Generic Clock Management
  165. * @{
  166. */
  167. void system_gclk_init(void);
  168. /** @} */
  169. /**
  170. * \name Generic Clock Management (Generators)
  171. * @{
  172. */
  173. /**
  174. * \brief Initializes a Generic Clock Generator configuration structure to defaults.
  175. *
  176. * Initializes a given Generic Clock Generator configuration structure to
  177. * a set of known default values. This function should be called on all
  178. * new instances of these configuration structures before being modified
  179. * by the user application.
  180. *
  181. * The default configuration is:
  182. * \li The clock is generated undivided from the source frequency
  183. * \li The clock generator output is low when the generator is disabled
  184. * \li The input clock is sourced from input clock channel 0
  185. * \li The clock will be disabled during sleep
  186. * \li The clock output will not be routed to a physical GPIO pin
  187. *
  188. * \param[out] config Configuration structure to initialize to default values
  189. */
  190. static inline void system_gclk_gen_get_config_defaults(
  191. struct system_gclk_gen_config *const config)
  192. {
  193. /* Sanity check arguments */
  194. Assert(config);
  195. /* Default configuration values */
  196. config->division_factor = 1;
  197. config->high_when_disabled = false;
  198. #if SAML21 || SAML22 || SAMR30
  199. config->source_clock = GCLK_SOURCE_OSC16M;
  200. #elif (SAMC20) || (SAMC21)
  201. config->source_clock = GCLK_SOURCE_OSC48M;
  202. #else
  203. config->source_clock = GCLK_SOURCE_OSC8M;
  204. #endif
  205. config->run_in_standby = false;
  206. config->output_enable = false;
  207. }
  208. void system_gclk_gen_set_config(
  209. const uint8_t generator,
  210. struct system_gclk_gen_config *const config);
  211. void system_gclk_gen_enable(
  212. const uint8_t generator);
  213. void system_gclk_gen_disable(
  214. const uint8_t generator);
  215. bool system_gclk_gen_is_enabled(
  216. const uint8_t generator);
  217. /** @} */
  218. /**
  219. * \name Generic Clock Management (Channels)
  220. * @{
  221. */
  222. /**
  223. * \brief Initializes a Generic Clock configuration structure to defaults.
  224. *
  225. * Initializes a given Generic Clock configuration structure to a set of
  226. * known default values. This function should be called on all new
  227. * instances of these configuration structures before being modified by the
  228. * user application.
  229. *
  230. * The default configuration is as follows:
  231. * \li The clock is sourced from the Generic Clock Generator channel 0
  232. * \li The clock configuration will not be write-locked when set
  233. *
  234. * \param[out] config Configuration structure to initialize to default values
  235. */
  236. static inline void system_gclk_chan_get_config_defaults(
  237. struct system_gclk_chan_config *const config)
  238. {
  239. /* Sanity check arguments */
  240. Assert(config);
  241. /* Default configuration values */
  242. config->source_generator = GCLK_GENERATOR_0;
  243. }
  244. void system_gclk_chan_set_config(
  245. const uint8_t channel,
  246. struct system_gclk_chan_config *const config);
  247. void system_gclk_chan_enable(
  248. const uint8_t channel);
  249. void system_gclk_chan_disable(
  250. const uint8_t channel);
  251. bool system_gclk_chan_is_enabled(
  252. const uint8_t channel);
  253. void system_gclk_chan_lock(
  254. const uint8_t channel);
  255. bool system_gclk_chan_is_locked(
  256. const uint8_t channel);
  257. /** @} */
  258. /**
  259. * \name Generic Clock Frequency Retrieval
  260. * @{
  261. */
  262. uint32_t system_gclk_gen_get_hz(
  263. const uint8_t generator);
  264. uint32_t system_gclk_chan_get_hz(
  265. const uint8_t channel);
  266. /** @} */
  267. #ifdef __cplusplus
  268. }
  269. #endif
  270. /** @} */
  271. #endif