1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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
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.
31 ******************************************************************************/
32 /*****************************************************************************/
35 * @file xspips_options.c
37 * Contains functions for the configuration of the XSpiPs driver.
40 * MODIFICATION HISTORY:
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.
55 ******************************************************************************/
57 /***************************** Include Files *********************************/
61 /************************** Constant Definitions *****************************/
64 /**************************** Type Definitions *******************************/
67 /***************** Macros (Inline Functions) Definitions *********************/
70 /************************** Function Prototypes ******************************/
73 /************************** Variable Definitions *****************************/
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.
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}
94 #define XSPIPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionsMap))
96 /*****************************************************************************/
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.
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
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.
116 * This function is not thread-safe.
118 ******************************************************************************/
119 s32 XSpiPs_SetOptions(XSpiPs *InstancePtr, u32 Options)
123 u32 CurrentConfigReg;
126 Xil_AssertNonvoid(InstancePtr != NULL);
127 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
130 * Do not allow the slave select to change while a transfer is in
131 * progress. Not thread-safe.
133 if (InstancePtr->IsBusy == TRUE) {
134 Status = (s32)XST_DEVICE_BUSY;
137 ConfigReg = XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
140 CurrentConfigReg = ConfigReg;
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.
146 for (Index = 0U; Index < XSPIPS_NUM_OPTIONS; Index++) {
147 if ((Options & OptionsTable[Index].Option) != (u32)0U) {
149 ConfigReg |= OptionsTable[Index].Mask;
153 ConfigReg &= ~(OptionsTable[Index].Mask);
159 * If CPOL-CPHA bits are toggled from previous state,
160 * disable before writing the configuration register and then enable.
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);
170 * Now write the Config register. Leave it to the upper layers
171 * to restart the device.
173 XSpiPs_WriteReg(InstancePtr->Config.BaseAddress,
174 XSPIPS_CR_OFFSET, ConfigReg);
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);
186 Status = (s32)XST_SUCCESS;
191 /*****************************************************************************/
194 * This function gets the options for the SPI device. The options control how
195 * the device behaves relative to the SPI bus.
197 * @param InstancePtr is a pointer to the XSpiPs instance.
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.
207 ******************************************************************************/
208 u32 XSpiPs_GetOptions(XSpiPs *InstancePtr)
210 u32 OptionsFlag = 0U;
214 Xil_AssertNonvoid(InstancePtr != NULL);
215 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
218 * Get the current options
221 XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
225 * Loop through the options table to grab options
227 for (Index = 0; Index < XSPIPS_NUM_OPTIONS; Index++) {
228 if (ConfigReg & OptionsTable[Index].Mask) {
229 OptionsFlag |= OptionsTable[Index].Option;
236 /*****************************************************************************/
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
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.
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.
254 * This function is not thread-safe.
256 ******************************************************************************/
257 s32 XSpiPs_SetClkPrescaler(XSpiPs *InstancePtr, u8 Prescaler)
262 Xil_AssertNonvoid(InstancePtr != NULL);
263 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
264 Xil_AssertNonvoid((Prescaler > 0U) && (Prescaler <= XSPIPS_CR_PRESC_MAXIMUM));
267 * Do not allow the prescaler to be changed while a transfer is in
268 * progress. Not thread-safe.
270 if (InstancePtr->IsBusy == TRUE) {
271 Status = (s32)XST_DEVICE_BUSY;
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.
279 ConfigReg = XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
282 ConfigReg &= (u32)(~XSPIPS_CR_PRESC_MASK);
283 ConfigReg |= (u32) ((u32)Prescaler & (u32)XSPIPS_CR_PRESC_MAXIMUM) <<
284 XSPIPS_CR_PRESC_SHIFT;
286 XSpiPs_WriteReg(InstancePtr->Config.BaseAddress,
290 Status = (s32)XST_SUCCESS;
295 /*****************************************************************************/
298 * This function gets the clock prescaler of an SPI device.
300 * @param InstancePtr is a pointer to the XSpiPs instance.
302 * @return The prescaler value.
307 ******************************************************************************/
308 u8 XSpiPs_GetClkPrescaler(XSpiPs *InstancePtr)
312 Xil_AssertNonvoid(InstancePtr != NULL);
313 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
315 ConfigReg = XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
318 ConfigReg &= XSPIPS_CR_PRESC_MASK;
320 return (u8)(ConfigReg >> XSPIPS_CR_PRESC_SHIFT);
324 /*****************************************************************************/
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.
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.
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.
354 ******************************************************************************/
355 s32 XSpiPs_SetDelays(XSpiPs *InstancePtr, u8 DelayNss, u8 DelayBtwn,
356 u8 DelayAfter, u8 DelayInit)
361 Xil_AssertNonvoid(InstancePtr != NULL);
362 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
365 * Do not allow the delays to change while a transfer is in
366 * progress. Not thread-safe.
368 if (InstancePtr->IsBusy == TRUE) {
369 Status = (s32)XST_DEVICE_BUSY;
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;
378 XSpiPs_WriteReg(InstancePtr->Config.BaseAddress,
379 XSPIPS_DR_OFFSET, DelayRegister);
381 Status = (s32)XST_SUCCESS;
386 /*****************************************************************************/
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.
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.
407 ******************************************************************************/
408 void XSpiPs_GetDelays(XSpiPs *InstancePtr,u8 *DelayNss, u8 *DelayBtwn,
409 u8 *DelayAfter, u8 *DelayInit)
413 Xil_AssertVoid(InstancePtr != NULL);
414 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
416 DelayRegister = XSpiPs_ReadReg(InstancePtr->Config.BaseAddress,
419 *DelayInit = (u8)(DelayRegister & XSPIPS_DR_INIT_MASK);
421 *DelayAfter = (u8)((DelayRegister & XSPIPS_DR_AFTER_MASK) >>
422 XSPIPS_DR_AFTER_SHIFT);
424 *DelayBtwn = (u8)((DelayRegister & XSPIPS_DR_BTWN_MASK) >>
425 XSPIPS_DR_BTWN_SHIFT);
427 *DelayNss = (u8)((DelayRegister & XSPIPS_DR_NSS_MASK) >>
428 XSPIPS_DR_NSS_SHIFT);