1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017, 2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_mrt.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.mrt"
14 #endif
15 
16 /*******************************************************************************
17  * Prototypes
18  ******************************************************************************/
19 /*!
20  * @brief Gets the instance from the base address
21  *
22  * @param base Multi-Rate timer peripheral base address
23  *
24  * @return The MRT instance
25  */
26 static uint32_t MRT_GetInstance(MRT_Type *base);
27 
28 /*******************************************************************************
29  * Variables
30  ******************************************************************************/
31 /*! @brief Pointers to MRT bases for each instance. */
32 static MRT_Type *const s_mrtBases[] = MRT_BASE_PTRS;
33 
34 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
35 /*! @brief Pointers to MRT clocks for each instance. */
36 static const clock_ip_name_t s_mrtClocks[] = MRT_CLOCKS;
37 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
38 
39 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
40 #if defined(MRT_RSTS_N)
41 /*! @brief Pointers to MRT resets for each instance, writing a zero asserts the reset */
42 static const reset_ip_name_t s_mrtResets[] = MRT_RSTS_N;
43 #elif defined(MRT_RSTS)
44 /*! @brief Pointers to MRT resets for each instance, writing a one asserts the reset */
45 static const reset_ip_name_t s_mrtResets[] = MRT_RSTS;
46 #endif
47 #endif /* FSL_SDK_DISABLE_DRIVER_RESET_CONTROL */
48 
49 /*******************************************************************************
50  * Code
51  ******************************************************************************/
MRT_GetInstance(MRT_Type * base)52 static uint32_t MRT_GetInstance(MRT_Type *base)
53 {
54     uint32_t instance;
55     uint32_t mrtArrayCount = (sizeof(s_mrtBases) / sizeof(s_mrtBases[0]));
56 
57     /* Find the instance index from base address mappings. */
58     for (instance = 0; instance < mrtArrayCount; instance++)
59     {
60         if (MSDK_REG_SECURE_ADDR(s_mrtBases[instance]) == MSDK_REG_SECURE_ADDR(base))
61         {
62             break;
63         }
64     }
65 
66     assert(instance < mrtArrayCount);
67 
68     return instance;
69 }
70 
71 /*!
72  * brief Ungates the MRT clock and configures the peripheral for basic operation.
73  *
74  * note This API should be called at the beginning of the application using the MRT driver.
75  *
76  * param base   Multi-Rate timer peripheral base address
77  * param config Pointer to user's MRT config structure. If MRT has  MULTITASK bit field in
78  *               MODCFG reigster, param config is useless.
79  */
MRT_Init(MRT_Type * base,const mrt_config_t * config)80 void MRT_Init(MRT_Type *base, const mrt_config_t *config)
81 {
82     assert(config != NULL);
83 
84 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
85     /* Ungate the MRT clock */
86     CLOCK_EnableClock(s_mrtClocks[MRT_GetInstance(base)]);
87 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
88 
89 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
90 #if defined(MRT_RSTS_N) || defined(MRT_RSTS)
91     /* Reset the module. */
92     RESET_PeripheralReset(s_mrtResets[MRT_GetInstance(base)]);
93 #endif
94 #endif /* FSL_SDK_DISABLE_DRIVER_RESET_CONTROL */
95 
96 #if !(defined(FSL_FEATURE_MRT_HAS_NO_MODCFG_MULTITASK) && FSL_FEATURE_MRT_HAS_NO_MODCFG_MULTITASK)
97     /* Set timer operating mode */
98     base->MODCFG = MRT_MODCFG_MULTITASK(config->enableMultiTask);
99 #endif
100 }
101 
102 /*!
103  * brief Gate the MRT clock
104  *
105  * param base Multi-Rate timer peripheral base address
106  */
MRT_Deinit(MRT_Type * base)107 void MRT_Deinit(MRT_Type *base)
108 {
109     /* Stop all the timers */
110     MRT_StopTimer(base, kMRT_Channel_0);
111     MRT_StopTimer(base, kMRT_Channel_1);
112 #if (FSL_FEATURE_MRT_NUMBER_OF_CHANNELS > 2U)
113     MRT_StopTimer(base, kMRT_Channel_2);
114 #endif
115 #if (FSL_FEATURE_MRT_NUMBER_OF_CHANNELS > 3U)
116     MRT_StopTimer(base, kMRT_Channel_3);
117 #endif
118 
119 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
120     /* Gate the MRT clock*/
121     CLOCK_DisableClock(s_mrtClocks[MRT_GetInstance(base)]);
122 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
123 }
124 
125 /*!
126  * brief Used to update the timer period in units of count.
127  *
128  * The new value will be immediately loaded or will be loaded at the end of the current time
129  * interval. For one-shot interrupt mode the new value will be immediately loaded.
130  *
131  * note User can call the utility macros provided in fsl_common.h to convert to ticks
132  *
133  * param base          Multi-Rate timer peripheral base address
134  * param channel       Timer channel number
135  * param count         Timer period in units of ticks
136  * param immediateLoad true: Load the new value immediately into the TIMER register;
137  *                      false: Load the new value at the end of current timer interval
138  */
MRT_UpdateTimerPeriod(MRT_Type * base,mrt_chnl_t channel,uint32_t count,bool immediateLoad)139 void MRT_UpdateTimerPeriod(MRT_Type *base, mrt_chnl_t channel, uint32_t count, bool immediateLoad)
140 {
141     assert((uint8_t)channel < (uint8_t)FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
142 
143     uint32_t newValue = count;
144     if (((base->CHANNEL[channel].CTRL & MRT_CHANNEL_CTRL_MODE_MASK) == (uint8_t)kMRT_OneShotMode) || (immediateLoad))
145     {
146         /* For one-shot interrupt mode, load the new value immediately even if user forgot to enable */
147         newValue |= MRT_CHANNEL_INTVAL_LOAD_MASK;
148     }
149 
150     /* Update the timer interval value */
151     base->CHANNEL[channel].INTVAL = newValue;
152 }
153