1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef __DA1469X_CLOCK_H_
21 #define __DA1469X_CLOCK_H_
22 
23 #include <stdint.h>
24 #include <da1469x_config.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #ifndef CLK_FREQ_TRIM_REG__XTAL32M_TRIM__DEFAULT
31 #define CLK_FREQ_TRIM_REG__XTAL32M_TRIM__DEFAULT   0x120
32 #endif
33 
34 /**
35  * Initialize XTAL32M
36  */
37 void da1469x_clock_sys_xtal32m_init(void);
38 
39 /**
40  * Enable XTAL32M
41  */
42 void da1469x_clock_sys_xtal32m_enable(void);
43 
44 /**
45  * Wait for XTAL32M to settle
46  */
47 void da1469x_clock_sys_xtal32m_wait_to_settle(void);
48 
49 /**
50  * Switch sys_clk to XTAL32M
51  *
52  * Caller shall ensure that XTAL32M is already settled.
53  */
54 void da1469x_clock_sys_xtal32m_switch(void);
55 
56 /**
57  * Switch sys_clk to XTAL32M
58  *
59  * Waits for XTAL32M to settle before switching.
60  */
61 void da1469x_clock_sys_xtal32m_switch_safe(void);
62 
63 /**
64  * Disable RC32M
65  */
66 void da1469x_clock_sys_rc32m_disable(void);
67 
68 /**
69  * Enable XTAL32K
70  */
71 void da1469x_clock_lp_xtal32k_enable(void);
72 
73 /**
74  * Switch lp_clk to XTAL32K
75  *
76  * Caller shall ensure XTAL32K is already settled.
77  */
78 void da1469x_clock_lp_xtal32k_switch(void);
79 
80 /**
81  * Enable RCX
82  */
83 void da1469x_clock_lp_rcx_enable(void);
84 
85 /**
86  * Switch lp_clk to RCX
87  *
88  * Caller shall ensure RCX is already settled.
89  */
90 void da1469x_clock_lp_rcx_switch(void);
91 
92 /**
93  * Calibrate RCX
94  */
95 void da1469x_clock_lp_rcx_calibrate(void);
96 
97 /**
98  * Calibrate RC32K
99  */
100 void da1469x_clock_lp_rc32k_calibrate(void);
101 
102 /**
103  * Calibrate RC32M
104  */
105 void da1469x_clock_lp_rc32m_calibrate(void);
106 
107 /**
108  * Get calibrated (measured) RCX frequency
109  */
110 uint32_t da1469x_clock_lp_rcx_freq_get(void);
111 
112 /**
113  * Get calibrated (measured) RC32K frequency
114  */
115 uint32_t da1469x_clock_lp_rc32k_freq_get(void);
116 
117 /**
118  * Get calibrated (measured) RC32M frequency
119  */
120 uint32_t da1469x_clock_lp_rc32m_freq_get(void);
121 
122 /**
123  * Disable RCX
124  */
125 void da1469x_clock_lp_rcx_disable(void);
126 
127 /**
128  * Enable AMBA clock(s)
129  *
130  * @param mask
131  */
132 static inline void
da1469x_clock_amba_enable(uint32_t mask)133 da1469x_clock_amba_enable(uint32_t mask)
134 {
135     uint32_t primask;
136 
137     primask = DA1469X_IRQ_DISABLE();
138     CRG_TOP->CLK_AMBA_REG |= mask;
139     DA1469X_IRQ_ENABLE(primask);
140 }
141 
142 /**
143  * Disable AMBA clock(s)
144  *
145  * @param uint32_t mask
146  */
147 static inline void
da1469x_clock_amba_disable(uint32_t mask)148 da1469x_clock_amba_disable(uint32_t mask)
149 {
150     uint32_t primask;
151 
152     primask = DA1469X_IRQ_DISABLE();
153     CRG_TOP->CLK_AMBA_REG &= ~mask;
154     DA1469X_IRQ_ENABLE(primask);
155 }
156 
157 /**
158  * Enable PLL96
159  */
160 void da1469x_clock_sys_pll_enable(void);
161 
162 /**
163  * Disable PLL96
164  *
165  * If PLL was used as SYS_CLOCK switches to XTAL32M.
166  */
167 void da1469x_clock_sys_pll_disable(void);
168 
169 /**
170  * Checks whether PLL96 is locked and can be use as system clock or USB clock
171  *
172  * @return 0 if PLL is off, non-0 it its running
173  */
174 static inline int
da1469x_clock_is_pll_locked(void)175 da1469x_clock_is_pll_locked(void)
176 {
177     return 0 != (CRG_XTAL->PLL_SYS_STATUS_REG & CRG_XTAL_PLL_SYS_STATUS_REG_PLL_LOCK_FINE_Msk);
178 }
179 
180 /**
181  * Waits for PLL96 to lock.
182  */
183 void da1469x_clock_pll_wait_to_lock(void);
184 
185 /**
186  * Switches system clock to PLL96
187  *
188  * Caller shall ensure that PLL is already locked.
189  */
190 void da1469x_clock_sys_pll_switch(void);
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 
196 #endif /* __MCU_DA1469X_CLOCK_H_ */
197