xref: /btstack/port/samv71-xplained-atwilc3000/ASF/common/services/clock/samv71/osc.h (revision 1b2596b5303dd8caeea8565532c93cca8dab8cc4)
1*1b2596b5SMatthias Ringwald /**
2*1b2596b5SMatthias Ringwald  * \file
3*1b2596b5SMatthias Ringwald  *
4*1b2596b5SMatthias Ringwald  * \brief Chip-specific oscillator management functions.
5*1b2596b5SMatthias Ringwald  *
6*1b2596b5SMatthias Ringwald  * Copyright (c) 2015 Atmel Corporation. All rights reserved.
7*1b2596b5SMatthias Ringwald  *
8*1b2596b5SMatthias Ringwald  * \asf_license_start
9*1b2596b5SMatthias Ringwald  *
10*1b2596b5SMatthias Ringwald  * \page License
11*1b2596b5SMatthias Ringwald  *
12*1b2596b5SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
13*1b2596b5SMatthias Ringwald  * modification, are permitted provided that the following conditions are met:
14*1b2596b5SMatthias Ringwald  *
15*1b2596b5SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright notice,
16*1b2596b5SMatthias Ringwald  *    this list of conditions and the following disclaimer.
17*1b2596b5SMatthias Ringwald  *
18*1b2596b5SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright notice,
19*1b2596b5SMatthias Ringwald  *    this list of conditions and the following disclaimer in the documentation
20*1b2596b5SMatthias Ringwald  *    and/or other materials provided with the distribution.
21*1b2596b5SMatthias Ringwald  *
22*1b2596b5SMatthias Ringwald  * 3. The name of Atmel may not be used to endorse or promote products derived
23*1b2596b5SMatthias Ringwald  *    from this software without specific prior written permission.
24*1b2596b5SMatthias Ringwald  *
25*1b2596b5SMatthias Ringwald  * 4. This software may only be redistributed and used in connection with an
26*1b2596b5SMatthias Ringwald  *    Atmel microcontroller product.
27*1b2596b5SMatthias Ringwald  *
28*1b2596b5SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29*1b2596b5SMatthias Ringwald  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30*1b2596b5SMatthias Ringwald  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31*1b2596b5SMatthias Ringwald  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32*1b2596b5SMatthias Ringwald  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33*1b2596b5SMatthias Ringwald  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34*1b2596b5SMatthias Ringwald  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35*1b2596b5SMatthias Ringwald  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36*1b2596b5SMatthias Ringwald  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37*1b2596b5SMatthias Ringwald  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38*1b2596b5SMatthias Ringwald  * POSSIBILITY OF SUCH DAMAGE.
39*1b2596b5SMatthias Ringwald  *
40*1b2596b5SMatthias Ringwald  * \asf_license_stop
41*1b2596b5SMatthias Ringwald  *
42*1b2596b5SMatthias Ringwald  */
43*1b2596b5SMatthias Ringwald /*
44*1b2596b5SMatthias Ringwald  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45*1b2596b5SMatthias Ringwald  */
46*1b2596b5SMatthias Ringwald 
47*1b2596b5SMatthias Ringwald #ifndef CHIP_OSC_H_INCLUDED
48*1b2596b5SMatthias Ringwald #define CHIP_OSC_H_INCLUDED
49*1b2596b5SMatthias Ringwald 
50*1b2596b5SMatthias Ringwald #include "board.h"
51*1b2596b5SMatthias Ringwald #include "pmc.h"
52*1b2596b5SMatthias Ringwald 
53*1b2596b5SMatthias Ringwald /// @cond 0
54*1b2596b5SMatthias Ringwald /**INDENT-OFF**/
55*1b2596b5SMatthias Ringwald #ifdef __cplusplus
56*1b2596b5SMatthias Ringwald extern "C" {
57*1b2596b5SMatthias Ringwald #endif
58*1b2596b5SMatthias Ringwald /**INDENT-ON**/
59*1b2596b5SMatthias Ringwald /// @endcond
60*1b2596b5SMatthias Ringwald 
61*1b2596b5SMatthias Ringwald /*
62*1b2596b5SMatthias Ringwald  * Below BOARD_XXX macros are related to the specific board, and
63*1b2596b5SMatthias Ringwald  * should be defined by the board code, otherwise default value are used.
64*1b2596b5SMatthias Ringwald  */
65*1b2596b5SMatthias Ringwald #if !defined(BOARD_FREQ_SLCK_XTAL)
66*1b2596b5SMatthias Ringwald #  warning The board slow clock xtal frequency has not been defined.
67*1b2596b5SMatthias Ringwald #  define BOARD_FREQ_SLCK_XTAL      (32768UL)
68*1b2596b5SMatthias Ringwald #endif
69*1b2596b5SMatthias Ringwald 
70*1b2596b5SMatthias Ringwald #if !defined(BOARD_FREQ_SLCK_BYPASS)
71*1b2596b5SMatthias Ringwald #  warning The board slow clock bypass frequency has not been defined.
72*1b2596b5SMatthias Ringwald #  define BOARD_FREQ_SLCK_BYPASS    (32768UL)
73*1b2596b5SMatthias Ringwald #endif
74*1b2596b5SMatthias Ringwald 
75*1b2596b5SMatthias Ringwald #if !defined(BOARD_FREQ_MAINCK_XTAL)
76*1b2596b5SMatthias Ringwald #  warning The board main clock xtal frequency has not been defined.
77*1b2596b5SMatthias Ringwald #  define BOARD_FREQ_MAINCK_XTAL    (12000000UL)
78*1b2596b5SMatthias Ringwald #endif
79*1b2596b5SMatthias Ringwald 
80*1b2596b5SMatthias Ringwald #if !defined(BOARD_FREQ_MAINCK_BYPASS)
81*1b2596b5SMatthias Ringwald #  warning The board main clock bypass frequency has not been defined.
82*1b2596b5SMatthias Ringwald #  define BOARD_FREQ_MAINCK_BYPASS  (12000000UL)
83*1b2596b5SMatthias Ringwald #endif
84*1b2596b5SMatthias Ringwald 
85*1b2596b5SMatthias Ringwald #if !defined(BOARD_OSC_STARTUP_US)
86*1b2596b5SMatthias Ringwald #  warning The board main clock xtal startup time has not been defined.
87*1b2596b5SMatthias Ringwald #  define BOARD_OSC_STARTUP_US      (15625UL)
88*1b2596b5SMatthias Ringwald #endif
89*1b2596b5SMatthias Ringwald 
90*1b2596b5SMatthias Ringwald /**
91*1b2596b5SMatthias Ringwald  * \weakgroup osc_group
92*1b2596b5SMatthias Ringwald  * @{
93*1b2596b5SMatthias Ringwald  */
94*1b2596b5SMatthias Ringwald 
95*1b2596b5SMatthias Ringwald //! \name Oscillator identifiers
96*1b2596b5SMatthias Ringwald //@{
97*1b2596b5SMatthias Ringwald #define OSC_SLCK_32K_RC      0    //!< Internal 32kHz RC oscillator.
98*1b2596b5SMatthias Ringwald #define OSC_SLCK_32K_XTAL    1    //!< External 32kHz crystal oscillator.
99*1b2596b5SMatthias Ringwald #define OSC_SLCK_32K_BYPASS  2    //!< External 32kHz bypass oscillator.
100*1b2596b5SMatthias Ringwald #define OSC_MAINCK_4M_RC     3    //!< Internal 4MHz RC oscillator.
101*1b2596b5SMatthias Ringwald #define OSC_MAINCK_8M_RC     4    //!< Internal 8MHz RC oscillator.
102*1b2596b5SMatthias Ringwald #define OSC_MAINCK_12M_RC    5    //!< Internal 12MHz RC oscillator.
103*1b2596b5SMatthias Ringwald #define OSC_MAINCK_XTAL      6    //!< External crystal oscillator.
104*1b2596b5SMatthias Ringwald #define OSC_MAINCK_BYPASS    7    //!< External bypass oscillator.
105*1b2596b5SMatthias Ringwald //@}
106*1b2596b5SMatthias Ringwald 
107*1b2596b5SMatthias Ringwald //! \name Oscillator clock speed in hertz
108*1b2596b5SMatthias Ringwald //@{
109*1b2596b5SMatthias Ringwald #define OSC_SLCK_32K_RC_HZ      CHIP_FREQ_SLCK_RC         //!< Internal 32kHz RC oscillator.
110*1b2596b5SMatthias Ringwald #define OSC_SLCK_32K_XTAL_HZ    BOARD_FREQ_SLCK_XTAL      //!< External 32kHz crystal oscillator.
111*1b2596b5SMatthias Ringwald #define OSC_SLCK_32K_BYPASS_HZ  BOARD_FREQ_SLCK_BYPASS    //!< External 32kHz bypass oscillator.
112*1b2596b5SMatthias Ringwald #define OSC_MAINCK_4M_RC_HZ     CHIP_FREQ_MAINCK_RC_4MHZ  //!< Internal 4MHz RC oscillator.
113*1b2596b5SMatthias Ringwald #define OSC_MAINCK_8M_RC_HZ     CHIP_FREQ_MAINCK_RC_8MHZ  //!< Internal 8MHz RC oscillator.
114*1b2596b5SMatthias Ringwald #define OSC_MAINCK_12M_RC_HZ    CHIP_FREQ_MAINCK_RC_12MHZ //!< Internal 12MHz RC oscillator.
115*1b2596b5SMatthias Ringwald #define OSC_MAINCK_XTAL_HZ      BOARD_FREQ_MAINCK_XTAL    //!< External crystal oscillator.
116*1b2596b5SMatthias Ringwald #define OSC_MAINCK_BYPASS_HZ    BOARD_FREQ_MAINCK_BYPASS  //!< External bypass oscillator.
117*1b2596b5SMatthias Ringwald //@}
118*1b2596b5SMatthias Ringwald 
osc_enable(uint32_t ul_id)119*1b2596b5SMatthias Ringwald static inline void osc_enable(uint32_t ul_id)
120*1b2596b5SMatthias Ringwald {
121*1b2596b5SMatthias Ringwald 	switch (ul_id) {
122*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_RC:
123*1b2596b5SMatthias Ringwald 		break;
124*1b2596b5SMatthias Ringwald 
125*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_XTAL:
126*1b2596b5SMatthias Ringwald 		pmc_switch_sclk_to_32kxtal(PMC_OSC_XTAL);
127*1b2596b5SMatthias Ringwald 		break;
128*1b2596b5SMatthias Ringwald 
129*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_BYPASS:
130*1b2596b5SMatthias Ringwald 		pmc_switch_sclk_to_32kxtal(PMC_OSC_BYPASS);
131*1b2596b5SMatthias Ringwald 		break;
132*1b2596b5SMatthias Ringwald 
133*1b2596b5SMatthias Ringwald 
134*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_4M_RC:
135*1b2596b5SMatthias Ringwald 		pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
136*1b2596b5SMatthias Ringwald 		break;
137*1b2596b5SMatthias Ringwald 
138*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_8M_RC:
139*1b2596b5SMatthias Ringwald 		pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
140*1b2596b5SMatthias Ringwald 		break;
141*1b2596b5SMatthias Ringwald 
142*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_12M_RC:
143*1b2596b5SMatthias Ringwald 		pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
144*1b2596b5SMatthias Ringwald 		break;
145*1b2596b5SMatthias Ringwald 
146*1b2596b5SMatthias Ringwald 
147*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_XTAL:
148*1b2596b5SMatthias Ringwald 		pmc_switch_mainck_to_xtal(PMC_OSC_XTAL,
149*1b2596b5SMatthias Ringwald 			pmc_us_to_moscxtst(BOARD_OSC_STARTUP_US,
150*1b2596b5SMatthias Ringwald 				OSC_SLCK_32K_RC_HZ));
151*1b2596b5SMatthias Ringwald 		break;
152*1b2596b5SMatthias Ringwald 
153*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_BYPASS:
154*1b2596b5SMatthias Ringwald 		pmc_switch_mainck_to_xtal(PMC_OSC_BYPASS,
155*1b2596b5SMatthias Ringwald 			pmc_us_to_moscxtst(BOARD_OSC_STARTUP_US,
156*1b2596b5SMatthias Ringwald 				OSC_SLCK_32K_RC_HZ));
157*1b2596b5SMatthias Ringwald 		break;
158*1b2596b5SMatthias Ringwald 	}
159*1b2596b5SMatthias Ringwald }
160*1b2596b5SMatthias Ringwald 
osc_disable(uint32_t ul_id)161*1b2596b5SMatthias Ringwald static inline void osc_disable(uint32_t ul_id)
162*1b2596b5SMatthias Ringwald {
163*1b2596b5SMatthias Ringwald 	switch (ul_id) {
164*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_RC:
165*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_XTAL:
166*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_BYPASS:
167*1b2596b5SMatthias Ringwald 		break;
168*1b2596b5SMatthias Ringwald 
169*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_4M_RC:
170*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_8M_RC:
171*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_12M_RC:
172*1b2596b5SMatthias Ringwald 		pmc_osc_disable_fastrc();
173*1b2596b5SMatthias Ringwald 		break;
174*1b2596b5SMatthias Ringwald 
175*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_XTAL:
176*1b2596b5SMatthias Ringwald 		pmc_osc_disable_xtal(PMC_OSC_XTAL);
177*1b2596b5SMatthias Ringwald 		break;
178*1b2596b5SMatthias Ringwald 
179*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_BYPASS:
180*1b2596b5SMatthias Ringwald 		pmc_osc_disable_xtal(PMC_OSC_BYPASS);
181*1b2596b5SMatthias Ringwald 		break;
182*1b2596b5SMatthias Ringwald 	}
183*1b2596b5SMatthias Ringwald }
184*1b2596b5SMatthias Ringwald 
osc_is_ready(uint32_t ul_id)185*1b2596b5SMatthias Ringwald static inline bool osc_is_ready(uint32_t ul_id)
186*1b2596b5SMatthias Ringwald {
187*1b2596b5SMatthias Ringwald 	switch (ul_id) {
188*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_RC:
189*1b2596b5SMatthias Ringwald 		return 1;
190*1b2596b5SMatthias Ringwald 
191*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_XTAL:
192*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_BYPASS:
193*1b2596b5SMatthias Ringwald 		return pmc_osc_is_ready_32kxtal();
194*1b2596b5SMatthias Ringwald 
195*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_4M_RC:
196*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_8M_RC:
197*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_12M_RC:
198*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_XTAL:
199*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_BYPASS:
200*1b2596b5SMatthias Ringwald 		return pmc_osc_is_ready_mainck();
201*1b2596b5SMatthias Ringwald 	}
202*1b2596b5SMatthias Ringwald 
203*1b2596b5SMatthias Ringwald 	return 0;
204*1b2596b5SMatthias Ringwald }
205*1b2596b5SMatthias Ringwald 
osc_get_rate(uint32_t ul_id)206*1b2596b5SMatthias Ringwald static inline uint32_t osc_get_rate(uint32_t ul_id)
207*1b2596b5SMatthias Ringwald {
208*1b2596b5SMatthias Ringwald 	switch (ul_id) {
209*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_RC:
210*1b2596b5SMatthias Ringwald 		return OSC_SLCK_32K_RC_HZ;
211*1b2596b5SMatthias Ringwald 
212*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_XTAL:
213*1b2596b5SMatthias Ringwald 		return BOARD_FREQ_SLCK_XTAL;
214*1b2596b5SMatthias Ringwald 
215*1b2596b5SMatthias Ringwald 	case OSC_SLCK_32K_BYPASS:
216*1b2596b5SMatthias Ringwald 		return BOARD_FREQ_SLCK_BYPASS;
217*1b2596b5SMatthias Ringwald 
218*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_4M_RC:
219*1b2596b5SMatthias Ringwald 		return OSC_MAINCK_4M_RC_HZ;
220*1b2596b5SMatthias Ringwald 
221*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_8M_RC:
222*1b2596b5SMatthias Ringwald 		return OSC_MAINCK_8M_RC_HZ;
223*1b2596b5SMatthias Ringwald 
224*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_12M_RC:
225*1b2596b5SMatthias Ringwald 		return OSC_MAINCK_12M_RC_HZ;
226*1b2596b5SMatthias Ringwald 
227*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_XTAL:
228*1b2596b5SMatthias Ringwald 		return BOARD_FREQ_MAINCK_XTAL;
229*1b2596b5SMatthias Ringwald 
230*1b2596b5SMatthias Ringwald 	case OSC_MAINCK_BYPASS:
231*1b2596b5SMatthias Ringwald 		return BOARD_FREQ_MAINCK_BYPASS;
232*1b2596b5SMatthias Ringwald 	}
233*1b2596b5SMatthias Ringwald 
234*1b2596b5SMatthias Ringwald 	return 0;
235*1b2596b5SMatthias Ringwald }
236*1b2596b5SMatthias Ringwald 
237*1b2596b5SMatthias Ringwald //! @}
238*1b2596b5SMatthias Ringwald 
239*1b2596b5SMatthias Ringwald /// @cond 0
240*1b2596b5SMatthias Ringwald /**INDENT-OFF**/
241*1b2596b5SMatthias Ringwald #ifdef __cplusplus
242*1b2596b5SMatthias Ringwald }
243*1b2596b5SMatthias Ringwald #endif
244*1b2596b5SMatthias Ringwald /**INDENT-ON**/
245*1b2596b5SMatthias Ringwald /// @endcond
246*1b2596b5SMatthias Ringwald 
247*1b2596b5SMatthias Ringwald #endif /* CHIP_OSC_H_INCLUDED */
248