]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/ttcps_v2_0/src/xttcps_options.c
49d0f5bf8df4d639f1fa1410c356521b36eef6ef
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / ttcps_v2_0 / src / xttcps_options.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2014 Xilinx, Inc.  All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 *
35 * @file xttcps_options.c
36 *
37 * This file contains functions to get or set option features for the device.
38 *
39 * <pre>
40 * MODIFICATION HISTORY:
41 *
42 * Ver   Who    Date     Changes
43 * ----- ------ -------- ---------------------------------------------
44 * 1.00a drg/jz 01/21/10 First release
45 * 1.01a nm     03/05/2012 Removed break statement after return to remove
46 *                         compilation warnings.
47 * </pre>
48 *
49 ******************************************************************************/
50
51 /***************************** Include Files *********************************/
52
53 #include "xttcps.h"
54
55 /************************** Constant Definitions *****************************/
56
57
58 /**************************** Type Definitions *******************************/
59
60
61 /***************** Macros (Inline Functions) Definitions *********************/
62
63
64 /************************** Function Prototypes ******************************/
65
66
67 /************************** Variable Definitions *****************************/
68
69 /*
70  * Create the table of options which are processed to get/set the device
71  * options. These options are table driven to allow easy maintenance and
72  * expansion of the options.
73  */
74 typedef struct {
75         u32 Option;
76         u32 Mask;
77         u32 Register;
78 } OptionsMap;
79
80 static OptionsMap TmrCtrOptionsTable[] = {
81         {XTTCPS_OPTION_EXTERNAL_CLK, XTTCPS_CLK_CNTRL_SRC_MASK,
82          XTTCPS_CLK_CNTRL_OFFSET},
83         {XTTCPS_OPTION_CLK_EDGE_NEG, XTTCPS_CLK_CNTRL_EXT_EDGE_MASK,
84          XTTCPS_CLK_CNTRL_OFFSET},
85         {XTTCPS_OPTION_INTERVAL_MODE, XTTCPS_CNT_CNTRL_INT_MASK,
86          XTTCPS_CNT_CNTRL_OFFSET},
87         {XTTCPS_OPTION_DECREMENT, XTTCPS_CNT_CNTRL_DECR_MASK,
88          XTTCPS_CNT_CNTRL_OFFSET},
89         {XTTCPS_OPTION_MATCH_MODE, XTTCPS_CNT_CNTRL_MATCH_MASK,
90          XTTCPS_CNT_CNTRL_OFFSET},
91         {XTTCPS_OPTION_WAVE_DISABLE, XTTCPS_CNT_CNTRL_EN_WAVE_MASK,
92          XTTCPS_CNT_CNTRL_OFFSET},
93         {XTTCPS_OPTION_WAVE_POLARITY, XTTCPS_CNT_CNTRL_POL_WAVE_MASK,
94          XTTCPS_CNT_CNTRL_OFFSET},
95 };
96
97 #define XTTCPS_NUM_TMRCTR_OPTIONS (sizeof(TmrCtrOptionsTable) / \
98                                 sizeof(OptionsMap))
99
100 /*****************************************************************************/
101 /**
102 *
103 * This function sets the options for the TTC device.
104 *
105 * @param        InstancePtr is a pointer to the XTtcPs instance.
106 * @param        Options contains the specified options to be set. This is a bit
107 *               mask where a 1 means to turn the option on, and a 0 means to
108 *               turn the option off. One or more bit values may be contained
109 *               in the mask. See the bit definitions named XTTCPS_*_OPTION in
110 *               the file xttcps.h.
111 *
112 * @return
113 *               - XST_SUCCESS if options are successfully set.
114 *               - XST_FAILURE if any of the options are unknown.
115 *
116 * @note         None
117 *
118 ******************************************************************************/
119 int XTtcPs_SetOptions(XTtcPs *InstancePtr, u32 Options)
120 {
121         u32 CountReg;
122         u32 ClockReg;
123         unsigned Index;
124
125         Xil_AssertNonvoid(InstancePtr != NULL);
126         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
127
128         ClockReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
129                                     XTTCPS_CLK_CNTRL_OFFSET);
130         CountReg = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
131                                     XTTCPS_CNT_CNTRL_OFFSET);
132
133         /*
134          * Loop through the options table, turning the option on or off
135          * depending on whether the bit is set in the incoming options flag.
136          */
137         for (Index = 0; Index < XTTCPS_NUM_TMRCTR_OPTIONS; Index++) {
138                 if (Options & TmrCtrOptionsTable[Index].Option) {
139
140                         switch (TmrCtrOptionsTable[Index].Register) {
141
142                         case XTTCPS_CLK_CNTRL_OFFSET:
143                                 /* Add option */
144                                 ClockReg |= TmrCtrOptionsTable[Index].Mask;
145                                 break;
146
147                         case XTTCPS_CNT_CNTRL_OFFSET:
148                                 /* Add option */
149                                 CountReg |= TmrCtrOptionsTable[Index].Mask;
150                                 break;
151
152                         default:
153                                 return XST_FAILURE;
154                         }
155                 }
156                 else {
157                         switch (TmrCtrOptionsTable[Index].Register) {
158
159                         case XTTCPS_CLK_CNTRL_OFFSET:
160                                 /* Remove option*/
161                                 ClockReg &= ~TmrCtrOptionsTable[Index].Mask;
162                                 break;
163
164                         case XTTCPS_CNT_CNTRL_OFFSET:
165                                 /* Remove option*/
166                                 CountReg &= ~TmrCtrOptionsTable[Index].Mask;
167                                 break;
168
169                         default:
170                                 return XST_FAILURE;
171                         }
172                 }
173         }
174
175         /*
176          * Now write the registers. Leave it to the upper layers to restart the
177          * device.
178          */
179         XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
180                           XTTCPS_CLK_CNTRL_OFFSET, ClockReg);
181         XTtcPs_WriteReg(InstancePtr->Config.BaseAddress,
182                           XTTCPS_CNT_CNTRL_OFFSET, CountReg);
183
184         return XST_SUCCESS;
185 }
186
187 /*****************************************************************************/
188 /**
189 *
190 * This function gets the settings for the options for the TTC device.
191 *
192 * @param        InstancePtr is a pointer to the XTtcPs instance.
193 *
194 * @return
195 *
196 * The return u32 contains the specified options that are set. This is a bit
197 * mask where a '1' means the option is on, and a'0' means the option is off.
198 * One or more bit values may be contained in the mask. See the bit definitions
199 * named XTTCPS_*_OPTION in the file xttcps.h.
200 *
201 * @note         None.
202 *
203 ******************************************************************************/
204 u32 XTtcPs_GetOptions(XTtcPs *InstancePtr)
205 {
206         u32 OptionsFlag = 0;
207         u32 Register;
208         unsigned Index;
209
210         Xil_AssertNonvoid(InstancePtr != NULL);
211         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
212
213
214         /*
215          * Loop through the options table to determine which options are set
216          */
217         for (Index = 0; Index < XTTCPS_NUM_TMRCTR_OPTIONS; Index++) {
218                 /*
219                  * Get the control register to determine which options are
220                  * currently set.
221                  */
222                 Register = XTtcPs_ReadReg(InstancePtr->Config.BaseAddress,
223                                               TmrCtrOptionsTable[Index].
224                                               Register);
225
226                 if (Register & TmrCtrOptionsTable[Index].Mask) {
227                         OptionsFlag |= TmrCtrOptionsTable[Index].Option;
228                 }
229         }
230
231         return OptionsFlag;
232 }