aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/ext/mcux-sdk/devices/LPC802/drivers/fsl_clock.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/ext/mcux-sdk/devices/LPC802/drivers/fsl_clock.h')
-rw-r--r--lib/chibios-contrib/ext/mcux-sdk/devices/LPC802/drivers/fsl_clock.h506
1 files changed, 506 insertions, 0 deletions
diff --git a/lib/chibios-contrib/ext/mcux-sdk/devices/LPC802/drivers/fsl_clock.h b/lib/chibios-contrib/ext/mcux-sdk/devices/LPC802/drivers/fsl_clock.h
new file mode 100644
index 000000000..1b1fe3c28
--- /dev/null
+++ b/lib/chibios-contrib/ext/mcux-sdk/devices/LPC802/drivers/fsl_clock.h
@@ -0,0 +1,506 @@
1/*
2 * Copyright 2017-2020 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef _FSL_CLOCK_H_
9#define _FSL_CLOCK_H_
10
11#include "fsl_common.h"
12
13/*! @addtogroup clock */
14/*! @{ */
15
16/*! @file */
17
18/*******************************************************************************
19 * Definitions
20 *****************************************************************************/
21
22/*! @name Driver version */
23/*@{*/
24/*! @brief CLOCK driver version 2.3.2. */
25#define FSL_CLOCK_DRIVER_VERSION (MAKE_VERSION(2, 3, 2))
26/*@}*/
27
28/* Definition for delay API in clock driver, users can redefine it to the real application. */
29#ifndef SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY
30#define SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY (15000000UL)
31#endif
32
33/*! @brief watchdog oscilltor clock frequency.
34 *
35 * This variable is used to store the watchdog oscillator frequency which is
36 * set by CLOCK_InitWdtOsc, and it is returned by CLOCK_GetWdtOscFreq.
37 */
38extern volatile uint32_t g_Wdt_Osc_Freq;
39
40/*! @brief external clock frequency.
41 *
42 * This variable is used to store the external clock frequency which is include
43 * external oscillator clock and external clk in clock frequency value, it is
44 * set by CLOCK_InitExtClkin when CLK IN is used as external clock or by CLOCK_InitSysOsc
45 * when external oscillator is used as external clock ,and it is returned by
46 * CLOCK_GetExtClkFreq.
47 */
48extern volatile uint32_t g_Ext_Clk_Freq;
49
50/*! @brief FRO clock setting API address in ROM. */
51#define CLOCK_FRO_SETTING_API_ROM_ADDRESS (0x0F001CD3U)
52/*! @brief FAIM base address*/
53#define CLOCK_FAIM_BASE (0x50010000U)
54
55/*! @brief Clock ip name array for ADC. */
56#define ADC_CLOCKS \
57 { \
58 kCLOCK_Adc, \
59 }
60/*! @brief Clock ip name array for ACMP. */
61#define ACMP_CLOCKS \
62 { \
63 kCLOCK_Acmp, \
64 }
65/*! @brief Clock ip name array for DAC. */
66#define DAC_CLOCKS \
67 { \
68 kCLOCK_Dac0, kCLOCK_Dac1, \
69 }
70/*! @brief Clock ip name array for SWM. */
71#define SWM_CLOCKS \
72 { \
73 kCLOCK_Swm, \
74 }
75/*! @brief Clock ip name array for ROM. */
76#define ROM_CLOCKS \
77 { \
78 kCLOCK_Rom, \
79 }
80/*! @brief Clock ip name array for SRAM. */
81#define SRAM_CLOCKS \
82 { \
83 kCLOCK_Ram0_1, \
84 }
85/*! @brief Clock ip name array for IOCON. */
86#define IOCON_CLOCKS \
87 { \
88 kCLOCK_Iocon, \
89 }
90/*! @brief Clock ip name array for GPIO. */
91#define GPIO_CLOCKS \
92 { \
93 kCLOCK_Gpio0, \
94 }
95/*! @brief Clock ip name array for GPIO_INT. */
96#define GPIO_INT_CLOCKS \
97 { \
98 kCLOCK_GpioInt, \
99 }
100/*! @brief Clock ip name array for DMA. */
101#define DMA_CLOCKS \
102 { \
103 kCLOCK_Dma, \
104 }
105/*! @brief Clock ip name array for CRC. */
106#define CRC_CLOCKS \
107 { \
108 kCLOCK_Crc, \
109 }
110/*! @brief Clock ip name array for WWDT. */
111#define WWDT_CLOCKS \
112 { \
113 kCLOCK_Wwdt, \
114 }
115/*! @brief Clock ip name array for SCT0. */
116#define SCT_CLOCKS \
117 { \
118 kCLOCK_Sct, \
119 }
120/*! @brief Clock ip name array for I2C. */
121#define I2C_CLOCKS \
122 { \
123 kCLOCK_I2c0, \
124 }
125/*! @brief Clock ip name array for I2C. */
126#define USART_CLOCKS \
127 { \
128 kCLOCK_Uart0, kCLOCK_Uart1, \
129 }
130/*! @brief Clock ip name array for SPI. */
131#define SPI_CLOCKS \
132 { \
133 kCLOCK_Spi0, \
134 }
135/*! @brief Clock ip name array for CAPT. */
136#define CAPT_CLOCKS \
137 { \
138 kCLOCK_Capt, \
139 }
140/*! @brief Clock ip name array for CTIMER. */
141#define CTIMER_CLOCKS \
142 { \
143 kCLOCK_Ctimer0, \
144 }
145/*! @brief Clock ip name array for MTB. */
146#define MTB_CLOCKS \
147 { \
148 kCLOCK_Mtb, \
149 }
150/*! @brief Clock ip name array for MRT. */
151#define MRT_CLOCKS \
152 { \
153 kCLOCK_Mrt, \
154 }
155/*! @brief Clock ip name array for WKT. */
156#define WKT_CLOCKS \
157 { \
158 kCLOCK_Wkt, \
159 }
160
161/*! @brief Internal used Clock definition only. */
162#define CLK_GATE_DEFINE(reg, bit) ((((reg)&0xFFU) << 8U) | ((bit)&0xFFU))
163#define CLK_GATE_GET_REG(x) (((x) >> 8U) & 0xFFU)
164#define CLK_GATE_GET_BITS_SHIFT(x) ((uint32_t)(x)&0xFFU)
165/* clock mux register definition */
166#define CLK_MUX_DEFINE(reg, mux) (((offsetof(SYSCON_Type, reg) & 0xFFU) << 8U) | ((mux)&0xFFU))
167#define CLK_MUX_GET_REG(x) ((volatile uint32_t *)(((uint32_t)(SYSCON)) + (((uint32_t)(x) >> 8U) & 0xFFU)))
168#define CLK_MUX_GET_MUX(x) (((uint32_t)x) & 0xFFU)
169#define CLK_MAIN_CLK_MUX_DEFINE(preMux, mux) ((preMux) << 8U | (mux))
170#define CLK_MAIN_CLK_MUX_GET_PRE_MUX(x) ((((uint32_t)(x)) >> 8U) & 0xFFU)
171#define CLK_MAIN_CLK_MUX_GET_MUX(x) (((uint32_t)(x)) & 0xFFU)
172/* clock divider register definition */
173#define CLK_DIV_DEFINE(reg) (((uint32_t)offsetof(SYSCON_Type, reg)) & 0xFFFU)
174
175#define CLK_DIV_GET_REG(x) *((volatile uint32_t *)(((uint32_t)(SYSCON)) + (((uint32_t)(x)) & 0xFFFU)))
176/* watch dog oscillator definition */
177#define CLK_WDT_OSC_DEFINE(freq, regValue) (((freq)&0xFFFFFFU) | (((regValue)&0xFFU) << 24U))
178#define CLK_WDT_OSC_GET_FREQ(x) ((x)&0xFFFFFFU)
179#define CLK_WDT_OSC_GET_REG(x) (((x) >> 24U) & 0xFFU)
180/* Fractional clock register map */
181#define CLK_FRG_DIV_REG_MAP(base) (*(base))
182#define CLK_FRG_MUL_REG_MAP(base) (*((uint32_t *)((uint32_t)(base) + 4U)))
183#define CLK_FRG_SEL_REG_MAP(base) (*((uint32_t *)((uint32_t)(base) + 8U)))
184/* register offset */
185#define SYS_AHB_CLK_CTRL0 (0U)
186/*! @brief Clock gate name used for CLOCK_EnableClock/CLOCK_DisableClock. */
187typedef enum _clock_ip_name
188{
189 kCLOCK_IpInvalid = 0U,
190 kCLOCK_Rom = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 1U),
191 kCLOCK_Ram0 = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 2U),
192 kCLOCK_Flash = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 4U),
193 kCLOCK_I2c0 = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 5U),
194 kCLOCK_Gpio0 = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 6U),
195 kCLOCK_Swm = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 7U),
196 kCLOCK_Wkt = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 9U),
197 kCLOCK_Mrt = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 10U),
198 kCLOCK_Spi0 = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 11U),
199 kCLOCK_Crc = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 13U),
200 kCLOCK_Uart0 = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 14U),
201 kCLOCK_Uart1 = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 15U),
202 kCLOCK_Wwdt = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 17U),
203 kCLOCK_Iocon = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 18U),
204 kCLOCK_Acmp = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 19U),
205 kCLOCK_Adc = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 24U),
206 kCLOCK_Ctimer0 = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 25U),
207 kCLOCK_GpioInt = CLK_GATE_DEFINE(SYS_AHB_CLK_CTRL0, 28U),
208
209} clock_ip_name_t;
210
211/*! @brief Clock name used to get clock frequency. */
212typedef enum _clock_name
213{
214 kCLOCK_CoreSysClk, /*!< Cpu/AHB/AHB matrix/Memories,etc */
215 kCLOCK_MainClk, /*!< Main clock */
216
217 kCLOCK_Fro, /*!< FRO18/24/30 */
218 kCLOCK_FroDiv, /*!< FRO div clock */
219 kCLOCK_ExtClk, /*!< External Clock */
220 kCLOCK_LPOsc, /*!< Low power Oscillator */
221 kCLOCK_Frg, /*!< fractional rate0 */
222} clock_name_t;
223
224/*! @brief Clock Mux Switches
225 *CLK_MUX_DEFINE(reg, mux)
226 *reg is used to define the mux register
227 *mux is used to define the mux value
228 *
229 */
230typedef enum _clock_select
231{
232
233 kADC_Clk_From_Fro = CLK_MUX_DEFINE(ADCCLKSEL, 0U),
234 kADC_Clk_From_ClkIn = CLK_MUX_DEFINE(ADCCLKSEL, 1U),
235
236 kUART0_Clk_From_Fro = CLK_MUX_DEFINE(UART0CLKSEL, 0U),
237 kUART0_Clk_From_MainClk = CLK_MUX_DEFINE(UART0CLKSEL, 1U),
238 kUART0_Clk_From_Frg0Clk = CLK_MUX_DEFINE(UART0CLKSEL, 2U),
239 kUART0_Clk_From_Fro_Div = CLK_MUX_DEFINE(UART0CLKSEL, 4U),
240
241 kUART1_Clk_From_Fro = CLK_MUX_DEFINE(UART1CLKSEL, 0U),
242 kUART1_Clk_From_MainClk = CLK_MUX_DEFINE(UART1CLKSEL, 1U),
243 kUART1_Clk_From_Frg0Clk = CLK_MUX_DEFINE(UART1CLKSEL, 2U),
244 kUART1_Clk_From_Fro_Div = CLK_MUX_DEFINE(UART1CLKSEL, 4U),
245
246 kI2C_Clk_From_Fro = CLK_MUX_DEFINE(I2C0CLKSEL, 0U),
247 kI2C_Clk_From_MainClk = CLK_MUX_DEFINE(I2C0CLKSEL, 1U),
248 kI2C_Clk_From_Frg0Clk = CLK_MUX_DEFINE(I2C0CLKSEL, 2U),
249 kI2C_Clk_From_Fro_Div = CLK_MUX_DEFINE(I2C0CLKSEL, 4U),
250
251 kSPI_Clk_From_Fro = CLK_MUX_DEFINE(SPI0CLKSEL, 0U),
252 kSPI_Clk_From_MainClk = CLK_MUX_DEFINE(SPI0CLKSEL, 1U),
253 kSPI_Clk_From_Frg0Clk = CLK_MUX_DEFINE(SPI0CLKSEL, 2U),
254 kSPI_Clk_From_Fro_Div = CLK_MUX_DEFINE(SPI0CLKSEL, 4U),
255
256 kFRG0_Clk_From_Fro = CLK_MUX_DEFINE(FRG[0].FRGCLKSEL, 0U),
257 kFRG0_Clk_From_MainClk = CLK_MUX_DEFINE(FRG[0].FRGCLKSEL, 1U),
258
259 kCLKOUT_From_Fro = CLK_MUX_DEFINE(CLKOUTSEL, 0U),
260 kCLKOUT_From_MainClk = CLK_MUX_DEFINE(CLKOUTSEL, 1U),
261 kCLKOUT_From_ExtClk = CLK_MUX_DEFINE(CLKOUTSEL, 3U),
262 kCLKOUT_From_WdtOsc = CLK_MUX_DEFINE(CLKOUTSEL, 4U)
263} clock_select_t;
264
265/*! @brief Clock divider
266 */
267typedef enum _clock_divider
268{
269 kCLOCK_DivAdcClk = CLK_DIV_DEFINE(ADCCLKDIV),
270 kCLOCK_DivClkOut = CLK_DIV_DEFINE(CLKOUTDIV)
271} clock_divider_t;
272
273/*! @brief fro output frequency source definition */
274typedef enum _clock_fro_src
275{
276 kCLOCK_FroSrcLpwrBootValue = 0U, /*!< fro source from the fro oscillator divided by low power boot value */
277 // kCLOCK_FroSrcFroOsc = 1U << SYSCON_FROOSCCTRL_DIRECT_SHIFT,
278} clock_fro_src_t;
279
280/*! @brief fro oscillator output frequency value definition */
281typedef enum _clock_fro_osc_freq
282{
283 kCLOCK_FroOscOut18M = 18000U, /*!< FRO oscillator output 18M */
284 kCLOCK_FroOscOut24M = 24000U, /*!< FRO oscillator output 24M */
285 kCLOCK_FroOscOut30M = 30000U, /*!< FRO oscillator output 30M */
286} clock_fro_osc_freq_t;
287
288/*! @brief Main clock source definition */
289typedef enum _clock_main_clk_src
290{
291 kCLOCK_MainClkSrcFro = CLK_MAIN_CLK_MUX_DEFINE(0U, 0U), /*!< main clock source from FRO */
292 kCLOCK_MainClkSrcExtClk = CLK_MAIN_CLK_MUX_DEFINE(1U, 0U), /*!< main clock source from Ext clock */
293 kCLOCK_MainClkSrcLPOsc = CLK_MAIN_CLK_MUX_DEFINE(2U, 0U), /*!< main clock source from watchdog oscillator */
294 kCLOCK_MainClkSrcFroDiv = CLK_MAIN_CLK_MUX_DEFINE(3U, 0U), /*!< main clock source from FRO Div */
295} clock_main_clk_src_t;
296
297/*******************************************************************************
298 * API
299 ******************************************************************************/
300
301#if defined(__cplusplus)
302extern "C" {
303#endif /* __cplusplus */
304
305/*!
306 * @name Clock gate, mux, and divider.
307 * @{
308 */
309
310/*
311 *! @brief enable ip clock.
312 *
313 * @param clk clock ip definition.
314 */
315static inline void CLOCK_EnableClock(clock_ip_name_t clk)
316{
317 SYSCON->SYSAHBCLKCTRL0 |= 1UL << CLK_GATE_GET_BITS_SHIFT(clk);
318}
319
320/*
321 *!@brief disable ip clock.
322 *
323 * @param clk clock ip definition.
324 */
325static inline void CLOCK_DisableClock(clock_ip_name_t clk)
326{
327 SYSCON->SYSAHBCLKCTRL0 &= ~(1UL << CLK_GATE_GET_BITS_SHIFT(clk));
328}
329
330/*
331 *! @brief Configure the clock selection muxes.
332 * @param mux : Clock to be configured.
333 * @return Nothing
334 */
335static inline void CLOCK_Select(clock_select_t sel)
336{
337 *(CLK_MUX_GET_REG(sel)) = CLK_MUX_GET_MUX(sel);
338}
339
340/*
341 *! @brief Setup peripheral clock dividers.
342 * @param name : Clock divider name
343 * @param value: Value to be divided
344 * @return Nothing
345 */
346static inline void CLOCK_SetClkDivider(clock_divider_t name, uint32_t value)
347{
348 CLK_DIV_GET_REG(name) = value & 0xFFU;
349}
350
351/*
352 *! @brief Get peripheral clock dividers.
353 * @param name : Clock divider name
354 * @return clock divider value
355 */
356static inline uint32_t CLOCK_GetClkDivider(clock_divider_t name)
357{
358 return CLK_DIV_GET_REG(name) & 0xFFU;
359}
360
361/*
362 *! @brief Setup Core clock dividers.
363 * Be careful about the core divider value, due to core/system frequency should be lower than 30MHZ.
364 * @param value: Value to be divided
365 * @return Nothing
366 */
367static inline void CLOCK_SetCoreSysClkDiv(uint32_t value)
368{
369 assert(value != 0U);
370
371 SYSCON->SYSAHBCLKDIV = (SYSCON->SYSAHBCLKDIV & (~SYSCON_SYSAHBCLKDIV_DIV_MASK)) | SYSCON_SYSAHBCLKDIV_DIV(value);
372}
373
374/*! @brief Set main clock reference source.
375 * @param src Reference clock_main_clk_src_t to set the main clock source.
376 */
377void CLOCK_SetMainClkSrc(clock_main_clk_src_t src);
378
379/*
380 *! @brief Set Fractional generator multiplier value.
381 * @param base: Fractional generator register address
382 * @param mul : FRG multiplier value.
383 * @return Nothing
384 */
385static inline void CLOCK_SetFRGClkMul(uint32_t *base, uint32_t mul)
386{
387 CLK_FRG_DIV_REG_MAP(base) = SYSCON_FRG_FRGDIV_DIV_MASK;
388 CLK_FRG_MUL_REG_MAP(base) = SYSCON_FRG_FRGMULT_MULT(mul);
389}
390/* @} */
391
392/*!
393 * @name Get frequency
394 * @{
395 */
396
397/*! @brief Return Frequency of FRG0 Clock.
398 * @return Frequency of FRG0 Clock.
399 */
400uint32_t CLOCK_GetFRGClkFreq(void);
401
402/*! @brief Return Frequency of Main Clock.
403 * @return Frequency of Main Clock.
404 */
405uint32_t CLOCK_GetMainClkFreq(void);
406
407/*! @brief Return Frequency of FRO.
408 * @return Frequency of FRO.
409 */
410uint32_t CLOCK_GetFroFreq(void);
411
412/*! @brief Return Frequency of core.
413 * @return Frequency of core.
414 */
415static inline uint32_t CLOCK_GetCoreSysClkFreq(void)
416{
417 return CLOCK_GetMainClkFreq() / (SYSCON->SYSAHBCLKDIV & 0xffU);
418}
419
420/*! @brief Return Frequency of ClockOut
421 * @return Frequency of ClockOut
422 */
423uint32_t CLOCK_GetClockOutClkFreq(void);
424
425/*! @brief Get UART0 frequency
426 * @retval UART0 frequency value.
427 */
428uint32_t CLOCK_GetUart0ClkFreq(void);
429
430/*! @brief Get UART1 frequency
431 * @retval UART1 frequency value.
432 */
433uint32_t CLOCK_GetUart1ClkFreq(void);
434
435/*! @brief Return Frequency of selected clock
436 * @return Frequency of selected clock
437 */
438uint32_t CLOCK_GetFreq(clock_name_t clockName);
439
440/*! @brief Get watch dog OSC frequency
441 * @retval watch dog OSC frequency value.
442 */
443static inline uint32_t CLOCK_GetLPOscFreq(void)
444{
445 return 1000000;
446}
447
448/*! @brief Get external clock frequency
449 * @retval external clock frequency value.
450 */
451static inline uint32_t CLOCK_GetExtClkFreq(void)
452{
453 return g_Ext_Clk_Freq;
454}
455/* @} */
456
457/* @} */
458
459/*!
460 * @name Fractional clock operations
461 * @{
462 */
463
464/*! @brief Set FRG0 output frequency.
465 * @param freq, target output frequency,freq < input and (input / freq) < 2 should be satisfy.
466 * @retval true - successfully, false - input argument is invalid.
467 *
468 */
469bool CLOCK_SetFRGClkFreq(uint32_t freq);
470
471/* @} */
472
473/*!
474 * @name External/internal oscillator clock operations
475 * @{
476 */
477
478/*! @brief Init external CLK IN, select the CLKIN as the external clock source.
479 * @param clkInFreq external clock in frequency.
480 */
481void CLOCK_InitExtClkin(uint32_t clkInFreq);
482
483/*! @brief Deinit watch dog OSC
484 * @param config oscillator configuration.
485 */
486static inline void CLOCK_DeinitLPOsc(void)
487{
488 SYSCON->PDRUNCFG |= SYSCON_PDRUNCFG_LPOSC_PD_MASK;
489}
490
491/*! @brief Set FRO oscillator output frequency.
492 * Initialize the FRO clock to given frequency (18, 24 or 30 MHz).
493 * @param freq, please reference clock_fro_osc_freq_t definition, frequency must be one of 18000, 24000 or 30000 KHz.
494 *
495 */
496void CLOCK_SetFroOscFreq(clock_fro_osc_freq_t freq);
497
498/* @} */
499
500#if defined(__cplusplus)
501}
502#endif /* __cplusplus */
503
504/*! @} */
505
506#endif /* _FSL_CLOCK_H_ */