1 /*
2  * Copyright  2017-2022 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_swm.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.swm"
14 #endif
15 
16 /*******************************************************************************
17  * Variables
18  ******************************************************************************/
19 #if (defined(FSL_FEATURE_SWM_HAS_FLEXTIMER_PINASSIGN_REGISTER) && \
20      (FSL_FEATURE_SWM_HAS_FLEXTIMER_PINASSIGN_REGISTER == 1))
21 /*! @brief Flextimer pin assignments. */
22 static const uint32_t s_swmFlextimerFuncMatrix[18][4] = {
23     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
24      0xff}, /* FTM0_EXTCLK: P0_24, P0_30, none, Not connected */
25     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, (uint32_t)kSWM_FTM_Selection2,
26      0xff}, /* FTM0_CH0: P0_17, P1_1, none, Not connected */
27     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, (uint32_t)kSWM_FTM_Selection2,
28      0xff}, /* FTM0_CH1: P0_18, P1_2, P0_16, Not connected */
29     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, (uint32_t)kSWM_FTM_Selection2,
30      0xff}, /* FTM0_CH2: P0_19, P1_3, P1_2, Not connected */
31     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, (uint32_t)kSWM_FTM_Selection2,
32      0xff}, /* FTM0_CH3: P0_20, P1_4, P0_27, Not connected */
33     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, (uint32_t)kSWM_FTM_Selection2,
34      0xff}, /* FTM0_CH4: P0_21, P1_5, P0_25, Not connected */
35     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, (uint32_t)kSWM_FTM_Selection2,
36      0xff}, /* FTM0_CH5: P0_22, P1_6, P0_24, Not connected */
37     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, (uint32_t)kSWM_FTM_Selection2,
38      0xff}, /* FTM0_FAULT0: P0_10, P1_7, P0_28, Not connected */
39     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, (uint32_t)kSWM_FTM_Selection2,
40      0xff}, /* FTM0_FAULT1: P0_11, P1_12, P1_3, Not connected */
41     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
42      0xff}, /* FTM0_FAULT2: P0_13, P1_13, none, Not connected */
43     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
44      0xff}, /* FTM0_FAULT3: P0_23, P1_14, none, Not connected */
45     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
46      0xff}, /* FTM1_EXTCLK: P0_25, P0_29, none, Not connected */
47     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
48      0xff}, /* FTM1_CH0: P0_15, P1_8, none, Not connected */
49     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
50      0xff}, /* FTM1_CH1: P0_16, P1_9, none, Not connected */
51     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
52      0xff}, /* FTM1_CH2: P0_26, P0_31, none, Not connected */
53     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
54      0xff}, /* FTM1_CH3: P0_27, P1_0, none, Not connected */
55     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
56      0xff}, /* FTM1_QD_PHA: P0_24, P0_29, none, Not connected */
57     {(uint32_t)kSWM_FTM_Selection0, (uint32_t)kSWM_FTM_Selection1, 0xff,
58      0xff} /* FTM1_QD_PHB: P0_25, P0_30, none, Not connected */
59 };
60 #endif /* FSL_FEATURE_SWM_HAS_PINASSIGNFIXED0_REGISTER */
61 /*******************************************************************************
62  * Prototypes
63  ******************************************************************************/
64 
65 /*******************************************************************************
66  * Code
67  ******************************************************************************/
68 
69 /*!
70  * brief Assignment of digital peripheral functions to pins.
71  *
72  * This function will selects a pin (designated by its GPIO port and
73  * bit numbers) to a function.
74  *
75  * param base SWM peripheral base address.
76  * param func any function name that is movable.
77  * param swm_port_pin any pin which has a GPIO port number and bit number.
78  */
SWM_SetMovablePinSelect(SWM_Type * base,swm_select_movable_t func,swm_port_pin_type_t swm_port_pin)79 void SWM_SetMovablePinSelect(SWM_Type *base, swm_select_movable_t func, swm_port_pin_type_t swm_port_pin)
80 {
81     /* Check arguments */
82     assert(NULL != base);
83     assert(func < kSWM_MOVABLE_NUM_FUNCS);
84 
85     uint32_t temp;
86     uint32_t pinassign = 0;
87     uint32_t shifter   = 0;
88 
89     pinassign = (uint32_t)func / 4U;
90     shifter   = ((uint32_t)func % 4U) * 8U;
91 
92     temp = base->PINASSIGN_DATA[pinassign];
93     temp &= ~(0xFFUL << (shifter));
94     temp |= ((uint32_t)swm_port_pin << shifter);
95     base->PINASSIGN_DATA[pinassign] = temp;
96 }
97 
98 #if (defined(FSL_FEATURE_SWM_HAS_PINASSIGNFIXED0_REGISTER) && (FSL_FEATURE_SWM_HAS_PINASSIGNFIXED0_REGISTER == 1))
99 /*!
100  * brief Assignment of digital peripheral functions to pins.
101  *
102  * This function will selects a pin (designated by its GPIO port and
103  * bit numbers) to a function.
104  *
105  * param base SWM peripheral base address.
106  * param func any function name that is movable.
107  * param swm_port_pin any pin which has a GPIO port number and bit number.
108  */
SWM_SetFixedMovablePinSelect(SWM_Type * base,swm_select_fixed_movable_t func,swm_fixed_port_pin_type_t swm_port_pin)109 void SWM_SetFixedMovablePinSelect(SWM_Type *base,
110                                   swm_select_fixed_movable_t func,
111                                   swm_fixed_port_pin_type_t swm_port_pin)
112 {
113     /* Check arguments */
114     assert(NULL != base);
115     assert(func < kSWM_PINASSINGNFIXED_MOVABLE_NUM_FUNCS);
116 
117     uint32_t temp;
118     uint32_t shifter = 0;
119 
120     shifter = (uint32_t)func * 2U;
121 
122     temp = base->PINASSIGNFIXED0;
123     temp &= ~(0x03UL << (shifter));
124     temp |= ((uint32_t)swm_port_pin << shifter);
125     base->PINASSIGNFIXED0 = temp;
126 }
127 #endif /* FSL_FEATURE_SWM_HAS_PINASSIGNFIXED0_REGISTER */
128 
129 /*!
130  * brief Enable the fixed-pin function.
131  *
132  * This function will enables a fixed-pin function in PINENABLE0 or PINENABLE1.
133  *
134  * param base SWM peripheral base address.
135  * param func any function name that is fixed pin.
136  * param enable enable or disable.
137  */
SWM_SetFixedPinSelect(SWM_Type * base,swm_select_fixed_pin_t func,bool enable)138 void SWM_SetFixedPinSelect(SWM_Type *base, swm_select_fixed_pin_t func, bool enable)
139 {
140     /* Check arguments */
141     assert(NULL != base);
142 
143     if (enable)
144     {
145         if (((uint32_t)func & 0x80000000U) == 0x00U)
146         {
147             base->PINENABLE0 &= ~((uint32_t)func);
148         }
149 #if (defined(FSL_FEATURE_SWM_HAS_PINENABLE1_REGISTER) && (FSL_FEATURE_SWM_HAS_PINENABLE1_REGISTER == 1))
150         else
151         {
152             base->PINENABLE1 &= ~((uint32_t)func);
153         }
154 #endif /* FSL_FEATURE_SWM_HAS_PINENABLE1_REGISTER */
155     }
156     else
157     {
158         if (((uint32_t)func & 0x80000000U) == 0x00U)
159         {
160             base->PINENABLE0 |= ((uint32_t)func);
161         }
162 #if (defined(FSL_FEATURE_SWM_HAS_PINENABLE1_REGISTER) && (FSL_FEATURE_SWM_HAS_PINENABLE1_REGISTER == 1))
163         else
164         {
165             base->PINENABLE1 |= ((uint32_t)func);
166         }
167 #endif /* FSL_FEATURE_SWM_HAS_PINENABLE1_REGISTER */
168     }
169 }
170 
171 #if (defined(FSL_FEATURE_SWM_HAS_FLEXTIMER_PINASSIGN_REGISTER) && \
172      (FSL_FEATURE_SWM_HAS_FLEXTIMER_PINASSIGN_REGISTER == 1))
173 /*!
174  * brief Enable the flextimer function.
175  *
176  * This function will enables a flextimer function in FTM_PINASSIGN0 or FTM_PINASSIGN1.
177  *
178  * param base SWM peripheral base address.
179  * param func any function name that is flextimer.
180  * param selection flextimer pin selection.
181  */
SWM_SetFlextimerPinSelect(SWM_Type * base,swm_flextimer_pin_func_t func,swm_flextimer_pin_sel_t selection)182 void SWM_SetFlextimerPinSelect(SWM_Type *base, swm_flextimer_pin_func_t func, swm_flextimer_pin_sel_t selection)
183 {
184     /* Check arguments */
185     assert(NULL != base);
186     assert(func < kSWM_FTM_NUM_FUNCS);
187 
188     uint32_t temp = 0x00U;
189 
190     if (((uint32_t)func & 0x10U) == 0x00U)
191     {
192         temp = base->FTM_PINASSIGN0;
193         temp &= ~(0x03UL << ((uint32_t)func * 2U));
194         temp |= ((uint32_t)s_swmFlextimerFuncMatrix[func][selection] << ((uint32_t)func * 2U));
195         base->FTM_PINASSIGN0 = temp;
196     }
197     else
198     {
199         temp = base->FTM_PINASSIGN1;
200         temp &= ~(0x03UL << (((uint32_t)func & 0x0FU) * 2U));
201         temp |= ((uint32_t)s_swmFlextimerFuncMatrix[func][selection] << (((uint32_t)func & 0x0FU) * 2U));
202         base->FTM_PINASSIGN1 = temp;
203     }
204 }
205 #endif /* FSL_FEATURE_SWM_HAS_PINASSIGNFIXED0_REGISTER */
206