]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/spips_v3_0/src/xspips_options.c
Completely re-generate the Zynq 7000 demo using the 2016.1 SDK tools.
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / spips_v3_0 / src / xspips_options.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2015 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 xspips_options.c
36 *
37 * Contains functions for the configuration of the XSpiPs driver.
38 *
39 * <pre>
40 * MODIFICATION HISTORY:
41 *
42 * Ver   Who    Date     Changes
43 * ----- ------ -------- -----------------------------------------------
44 * 1.00  drg/jz 01/25/10 First release
45 * 1.00  sdm    10/25/11 Removed the Divide by 2 in the SPI Clock Prescaler
46 *                       options as this is not supported in the device
47 * 1.04a sg     01/30/13 Added XSPIPS_MANUAL_START_OPTION. SetDelays and
48 *                       GetDelays API's include DelayNss parameter.
49 * 1.05a hk         26/04/13 Added disable and enable in XSpiPs_SetOptions when
50 *                               CPOL/CPHA bits are set/reset. Fix for CR#707669.
51 * 3.00  kvn    02/13/15 Modified code for MISRA-C:2012 compliance.
52 *
53 * </pre>
54 *
55 ******************************************************************************/
56
57 /***************************** Include Files *********************************/
58
59 #include "xspips.h"
60
61 /************************** Constant Definitions *****************************/
62
63
64 /**************************** Type Definitions *******************************/
65
66
67 /***************** Macros (Inline Functions) Definitions *********************/
68
69
70 /************************** Function Prototypes ******************************/
71
72
73 /************************** Variable Definitions *****************************/
74
75 /*
76  * Create the table of options which are processed to get/set the device
77  * options. These options are table driven to allow easy maintenance and
78  * expansion of the options.
79  */
80 typedef struct {
81         u32 Option;
82         u32 Mask;
83 } OptionsMap;
84
85 static OptionsMap OptionsTable[] = {
86         {XSPIPS_MASTER_OPTION, XSPIPS_CR_MSTREN_MASK},
87         {XSPIPS_CLK_ACTIVE_LOW_OPTION, XSPIPS_CR_CPOL_MASK},
88         {XSPIPS_CLK_PHASE_1_OPTION, XSPIPS_CR_CPHA_MASK},
89         {XSPIPS_DECODE_SSELECT_OPTION, XSPIPS_CR_SSDECEN_MASK},
90         {XSPIPS_FORCE_SSELECT_OPTION, XSPIPS_CR_SSFORCE_MASK},
91         {XSPIPS_MANUAL_START_OPTION, XSPIPS_CR_MANSTRTEN_MASK}
92 };
93
94 #define XSPIPS_NUM_OPTIONS      (sizeof(OptionsTable) / sizeof(OptionsMap))
95
96 /*****************************************************************************/
97 /**
98 *
99 * This function sets the options for the SPI device driver. The options control
100 * how the device behaves relative to the SPI bus. The device must be idle
101 * rather than busy transferring data before setting these device options.
102 *
103 * @param        InstancePtr is a pointer to the XSpiPs instance.
104 * @param        Options contains the specified options to be set. This is a bit
105 *               mask where a 1 means to turn the option on, and a 0 means to
106 *               turn the option off. One or more bit values may be contained in
107 *               the mask. See the bit definitions named XSPIPS_*_OPTIONS in the
108 *               file xspips.h.
109 *
110 * @return
111 *               - XST_SUCCESS if options are successfully set.
112 *               - XST_DEVICE_BUSY if the device is currently transferring data.
113 *               The transfer must complete or be aborted before setting options.
114 *
115 * @note
116 * This function is not thread-safe.
117 *
118 ******************************************************************************/
119 s32 XSpiPs_SetOptions(XSpiPs *InstancePtr, u32 Options)
120 {
121         u32 ConfigReg;
122         u32 Index;
123         u32 CurrentConfigReg;
124         s32 Status;
125
126         Xil_AssertNonvoid(InstancePtr != NULL);
127         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
128
129         /*
130          * Do not allow the slave select to change while a transfer is in
131          * progress. Not thread-safe.
132          */
133         if (InstancePtr->IsBusy == TRUE) {
134                 Status = (s32)XST_DEVICE_BUSY;
135         } else {
136
137                 ConfigReg = XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
138                                          XSPIPS_CR_OFFSET);
139
140                 CurrentConfigReg = ConfigReg;
141
142                 /*
143                  * Loop through the options table, turning the option on or off
144                  * depending on whether the bit is set in the incoming options flag.
145                  */
146                 for (Index = 0U; Index < XSPIPS_NUM_OPTIONS; Index++) {
147                         if ((Options & OptionsTable[Index].Option) != (u32)0U) {
148                                 /* Turn it on */
149                                 ConfigReg |= OptionsTable[Index].Mask;
150                         }
151                         else {
152                                 /* Turn it off */
153                                 ConfigReg &= ~(OptionsTable[Index].Mask);
154                         }
155                 }
156
157
158                 /*
159                  * If CPOL-CPHA bits are toggled from previous state,
160                  * disable before writing the configuration register and then enable.
161                  */
162                 if( ((CurrentConfigReg & XSPIPS_CR_CPOL_MASK) !=
163                         (ConfigReg & XSPIPS_CR_CPOL_MASK)) ||
164                         ((CurrentConfigReg & XSPIPS_CR_CPHA_MASK) !=
165                         (ConfigReg & XSPIPS_CR_CPHA_MASK)) ) {
166                                 XSpiPs_Disable(InstancePtr);
167                         }
168
169                 /*
170                  * Now write the Config register. Leave it to the upper layers
171                  * to restart the device.
172                  */
173                 XSpiPs_WriteReg(InstancePtr->Config.BaseAddress,
174                                         XSPIPS_CR_OFFSET, ConfigReg);
175
176                 /*
177                  * Enable
178                  */
179                 if( ((CurrentConfigReg & XSPIPS_CR_CPOL_MASK) !=
180                         (ConfigReg & XSPIPS_CR_CPOL_MASK)) ||
181                         ((CurrentConfigReg & XSPIPS_CR_CPHA_MASK) !=
182                         (ConfigReg & XSPIPS_CR_CPHA_MASK)) ) {
183                                 XSpiPs_Enable(InstancePtr);
184                         }
185
186                 Status = (s32)XST_SUCCESS;
187         }
188         return Status;
189 }
190
191 /*****************************************************************************/
192 /**
193 *
194 * This function gets the options for the SPI device. The options control how
195 * the device behaves relative to the SPI bus.
196 *
197 * @param        InstancePtr is a pointer to the XSpiPs instance.
198 *
199 * @return
200 *
201 * Options contains the specified options currently set. This is a bit value
202 * where a 1 means the option is on, and a 0 means the option is off.
203 * See the bit definitions named XSPIPS_*_OPTIONS in file xspips.h.
204 *
205 * @note         None.
206 *
207 ******************************************************************************/
208 u32 XSpiPs_GetOptions(XSpiPs *InstancePtr)
209 {
210         u32 OptionsFlag = 0U;
211         u32 ConfigReg;
212         u32 Index;
213
214         Xil_AssertNonvoid(InstancePtr != NULL);
215         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
216
217         /*
218          * Get the current options
219          */
220         ConfigReg =
221                 XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
222                                  XSPIPS_CR_OFFSET);
223
224         /*
225          * Loop through the options table to grab options
226          */
227         for (Index = 0; Index < XSPIPS_NUM_OPTIONS; Index++) {
228                 if (ConfigReg & OptionsTable[Index].Mask) {
229                         OptionsFlag |= OptionsTable[Index].Option;
230                 }
231         }
232
233         return OptionsFlag;
234 }
235
236 /*****************************************************************************/
237 /**
238 *
239 * This function sets the clock prescaler for an SPI device. The device
240 * must be idle rather than busy transferring data before setting these device
241 * options.
242 *
243 * @param        InstancePtr is a pointer to the XSpiPs instance.
244 * @param        Prescaler is the value that determine how much the clock should
245 *               be divided by. Use the XSPIPS_CLK_PRESCALE_* constants defined
246 *               in xspips.h for this setting.
247 *
248 * @return
249 *               - XST_SUCCESS if options are successfully set.
250 *               - XST_DEVICE_BUSY if the device is currently transferring data.
251 *               The transfer must complete or be aborted before setting options.
252 *
253 * @note
254 * This function is not thread-safe.
255 *
256 ******************************************************************************/
257 s32 XSpiPs_SetClkPrescaler(XSpiPs *InstancePtr, u8 Prescaler)
258 {
259         u32 ConfigReg;
260         s32 Status;
261
262         Xil_AssertNonvoid(InstancePtr != NULL);
263         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
264         Xil_AssertNonvoid((Prescaler > 0U) && (Prescaler <= XSPIPS_CR_PRESC_MAXIMUM));
265
266         /*
267          * Do not allow the prescaler to be changed while a transfer is in
268          * progress. Not thread-safe.
269          */
270         if (InstancePtr->IsBusy == TRUE) {
271                 Status = (s32)XST_DEVICE_BUSY;
272         } else {
273
274                 /*
275                  * Read the Config register, mask out the interesting bits, and set
276                  * them with the shifted value passed into the function. Write the
277                  * results back to the Config register.
278                  */
279                 ConfigReg = XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
280                                          XSPIPS_CR_OFFSET);
281
282                 ConfigReg &= (u32)(~XSPIPS_CR_PRESC_MASK);
283                 ConfigReg |= (u32) ((u32)Prescaler & (u32)XSPIPS_CR_PRESC_MAXIMUM) <<
284                         XSPIPS_CR_PRESC_SHIFT;
285
286                 XSpiPs_WriteReg(InstancePtr->Config.BaseAddress,
287                                 XSPIPS_CR_OFFSET,
288                                 ConfigReg);
289
290                 Status = (s32)XST_SUCCESS;
291         }
292         return Status;
293 }
294
295 /*****************************************************************************/
296 /**
297 *
298 * This function gets the clock prescaler of an SPI device.
299 *
300 * @param        InstancePtr is a pointer to the XSpiPs instance.
301 *
302 * @return       The prescaler value.
303 *
304 * @note         None.
305 *
306 *
307 ******************************************************************************/
308 u8 XSpiPs_GetClkPrescaler(XSpiPs *InstancePtr)
309 {
310         u32 ConfigReg;
311
312         Xil_AssertNonvoid(InstancePtr != NULL);
313         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
314
315         ConfigReg = XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
316                         XSPIPS_CR_OFFSET);
317
318         ConfigReg &= XSPIPS_CR_PRESC_MASK;
319
320         return (u8)(ConfigReg >> XSPIPS_CR_PRESC_SHIFT);
321
322 }
323
324 /*****************************************************************************/
325 /**
326 *
327 * This function sets the delay register for the SPI device driver.
328 * The delay register controls the Delay Between Transfers, Delay After
329 * Transfers, and the Delay Initially. The default value is 0x0. The range of
330 * each delay value is 0-255.
331 *
332 * @param        InstancePtr is a pointer to the XSpiPs instance.
333 * @param        DelayNss is the delay for which the chip select outputs will
334 *               be de-asserted between words when CPHA=0.
335 * @param        DelayBtwn is the delay between one Slave Select being
336 *               de-activated and the activation of another slave. The delay is
337 *               the number of master clock periods given by DelayBtwn + 2.
338 * @param        DelayAfter define the delay between the last bit of the current
339 *               byte transfer and the first bit of the next byte transfer.
340 *               The delay in number of master clock periods is given as:
341 *               CPHA=0:DelayInit+DelayAfter+3
342 *               CPHA=1:DelayAfter+1
343 * @param        DelayInit is the delay between asserting the slave select signal
344 *               and the first bit transfer. The delay int number of master clock
345 *               periods is DelayInit+1.
346 *
347 * @return
348 *               - XST_SUCCESS if delays are successfully set.
349 *               - XST_DEVICE_BUSY if the device is currently transferring data.
350 *               The transfer must complete or be aborted before setting options.
351 *
352 * @note         None.
353 *
354 ******************************************************************************/
355 s32 XSpiPs_SetDelays(XSpiPs *InstancePtr, u8 DelayNss, u8 DelayBtwn,
356                          u8 DelayAfter, u8 DelayInit)
357 {
358         u32 DelayRegister;
359         s32 Status;
360
361         Xil_AssertNonvoid(InstancePtr != NULL);
362         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
363
364         /*
365          * Do not allow the delays to change while a transfer is in
366          * progress. Not thread-safe.
367          */
368         if (InstancePtr->IsBusy == TRUE) {
369                 Status = (s32)XST_DEVICE_BUSY;
370         } else {
371
372                 /* Shift, Mask and OR the values to build the register settings */
373                 DelayRegister = (u32) DelayNss << XSPIPS_DR_NSS_SHIFT;
374                 DelayRegister |= (u32) DelayBtwn << XSPIPS_DR_BTWN_SHIFT;
375                 DelayRegister |= (u32) DelayAfter << XSPIPS_DR_AFTER_SHIFT;
376                 DelayRegister |= (u32) DelayInit;
377
378                 XSpiPs_WriteReg(InstancePtr->Config.BaseAddress,
379                                 XSPIPS_DR_OFFSET, DelayRegister);
380
381                 Status = (s32)XST_SUCCESS;
382         }
383         return Status;
384 }
385
386 /*****************************************************************************/
387 /**
388 *
389 * This function gets the delay settings for an SPI device.
390 * The delay register controls the Delay Between Transfers, Delay After
391 * Transfers, and the Delay Initially. The default value is 0x0.
392 *
393 * @param        InstancePtr is a pointer to the XSpiPs instance.
394 * @param        DelayNss is a pointer to the delay for which the chip select
395 *               outputs will be de-asserted between words when CPHA=0.
396 * @param        DelayBtwn is a pointer to the Delay Between transfers value.
397 *               This is a return parameter.
398 * @param        DelayAfter is a pointer to the Delay After transfer value.
399 *               This is a return parameter.
400 * @param        DelayInit is a pointer to the Delay Initially value. This is
401 *               a return parameter.
402 *
403 * @return       None.
404 *
405 * @note         None.
406 *
407 ******************************************************************************/
408 void XSpiPs_GetDelays(XSpiPs *InstancePtr,u8 *DelayNss, u8 *DelayBtwn,
409                         u8 *DelayAfter, u8 *DelayInit)
410 {
411         u32 DelayRegister;
412
413         Xil_AssertVoid(InstancePtr != NULL);
414         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
415
416         DelayRegister = XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
417                                         XSPIPS_DR_OFFSET);
418
419         *DelayInit = (u8)(DelayRegister & XSPIPS_DR_INIT_MASK);
420
421         *DelayAfter = (u8)((DelayRegister & XSPIPS_DR_AFTER_MASK) >>
422                                  XSPIPS_DR_AFTER_SHIFT);
423
424         *DelayBtwn = (u8)((DelayRegister & XSPIPS_DR_BTWN_MASK) >>
425                                 XSPIPS_DR_BTWN_SHIFT);
426
427         *DelayNss = (u8)((DelayRegister & XSPIPS_DR_NSS_MASK) >>
428                                 XSPIPS_DR_NSS_SHIFT);
429
430 }