1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_wkt.h"
9 /* Component ID definition, used by tools. */
10 #ifndef FSL_COMPONENT_ID
11 #define FSL_COMPONENT_ID "platform.drivers.wkt"
12 #endif
13 
14 /*******************************************************************************
15  * Prototypes
16  ******************************************************************************/
17 
18 /*!
19  * @brief Gets the instance from the base address
20  *
21  * @param base WKT peripheral base address
22  *
23  * @return The WKT instance
24  */
25 static uint32_t WKT_GetInstance(WKT_Type *base);
26 
27 /*******************************************************************************
28  * Variables
29  ******************************************************************************/
30 /*! @brief Pointers to WKT bases for each instance. */
31 static WKT_Type *const s_wktBases[] = WKT_BASE_PTRS;
32 
33 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
34 /*! @brief Pointers to WKT clocks for each instance. */
35 static const clock_ip_name_t s_wktClocks[] = WKT_CLOCKS;
36 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
37 
38 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
39 /*! @brief Pointers to WKT resets for each instance. */
40 static const reset_ip_name_t s_wktResets[] = WKT_RSTS_N;
41 #endif /* FSL_SDK_DISABLE_DRIVER_RESET_CONTROL */
42 
43 /*******************************************************************************
44  * Code
45  ******************************************************************************/
WKT_GetInstance(WKT_Type * base)46 static uint32_t WKT_GetInstance(WKT_Type *base)
47 {
48     uint32_t instance;
49     uint32_t wktArrayCount = (sizeof(s_wktBases) / sizeof(s_wktBases[0]));
50 
51     /* Find the instance index from base address mappings. */
52     for (instance = 0; instance < wktArrayCount; instance++)
53     {
54         if (s_wktBases[instance] == base)
55         {
56             break;
57         }
58     }
59 
60     assert(instance < wktArrayCount);
61 
62     return instance;
63 }
64 
65 /*!
66  * brief Ungates the WKT clock and configures the peripheral for basic operation.
67  *
68  * note This API should be called at the beginning of the application using the WKT driver.
69  *
70  * param base   WKT peripheral base address
71  * param config Pointer to user's WKT config structure.
72  */
WKT_Init(WKT_Type * base,const wkt_config_t * config)73 void WKT_Init(WKT_Type *base, const wkt_config_t *config)
74 {
75     assert(config);
76 
77 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
78     /* Enable the WKT peripheral clock */
79     CLOCK_EnableClock(s_wktClocks[WKT_GetInstance(base)]);
80 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
81 
82 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
83     /* Reset the module. */
84     RESET_PeripheralReset(s_wktResets[WKT_GetInstance(base)]);
85 #endif /* FSL_SDK_DISABLE_DRIVER_RESET_CONTROL */
86 
87     /* Clear wake-up or alarm timer flag */
88     WKT_ClearStatusFlags(WKT, (uint32_t)kWKT_AlarmFlag);
89 
90 #if defined(FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK) && FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK
91     if (config->clockSource == kWKT_ExternalClockSource)
92     {
93         /* Select external clock source */
94         base->CTRL |= WKT_CTRL_SEL_EXTCLK_MASK;
95     }
96     else
97     {
98 #endif /* FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK */
99         base->CTRL &= ~(
100 #if defined(FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK) && FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK
101             WKT_CTRL_SEL_EXTCLK_MASK |
102 #endif /* FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK */
103             WKT_CTRL_CLKSEL_MASK);
104         /* Select divided FRO clock or Low power clock */
105         base->CTRL |= WKT_CTRL_CLKSEL(config->clockSource);
106 #if defined(FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK) && FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK
107     }
108 #endif /* FSL_FEATURE_WKT_HAS_CTRL_SEL_EXTCLK */
109 }
110 
111 /*!
112  * brief Gate the WKT clock
113  *
114  * param base WKT peripheral base address
115  */
WKT_Deinit(WKT_Type * base)116 void WKT_Deinit(WKT_Type *base)
117 {
118     /* Stop the timer */
119     WKT_StopTimer(base);
120 
121 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
122     /* Gate the module clock */
123     CLOCK_DisableClock(kCLOCK_Wkt);
124 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
125 }
126