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