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 xiicps_options.c
37 * Contains functions for the configuration of the XIccPs driver.
40 * MODIFICATION HISTORY:
42 * Ver Who Date Changes
43 * ----- ------ -------- -----------------------------------------------
44 * 1.00a drg/jz 01/30/10 First release
45 * 1.02a sg 08/29/12 Updated the logic to arrive at the best divisors
46 * to achieve I2C clock with minimum error.
47 * This is a fix for CR #674195
48 * 1.03a hk 05/04/13 Initialized BestDivA and BestDivB to 0.
49 * This is fix for CR#704398 to remove warning.
50 * 2.0 hk 03/07/14 Limited frequency set when 100KHz or 400KHz is
51 * selected. This is a hardware limitation. CR#779290.
52 * 2.1 hk 04/24/14 Fix for CR# 761060 - provision for repeated start.
53 * 2.3 sk 10/07/14 Repeated start feature removed.
54 * 3.0 sk 12/06/14 Implemented Repeated start feature.
55 * 01/31/15 Modified the code according to MISRAC 2012 Compliant.
59 ******************************************************************************/
61 /***************************** Include Files *********************************/
65 /************************** Constant Definitions *****************************/
68 /**************************** Type Definitions *******************************/
71 /***************** Macros (Inline Functions) Definitions *********************/
74 /************************** Function Prototypes ******************************/
77 /************************** Variable Definitions *****************************/
79 * Create the table of options which are processed to get/set the device
80 * options. These options are table driven to allow easy maintenance and
81 * expansion of the options.
88 static OptionsMap OptionsTable[] = {
89 {XIICPS_7_BIT_ADDR_OPTION, XIICPS_CR_NEA_MASK},
90 {XIICPS_10_BIT_ADDR_OPTION, XIICPS_CR_NEA_MASK},
91 {XIICPS_SLAVE_MON_OPTION, XIICPS_CR_SLVMON_MASK},
92 {XIICPS_REP_START_OPTION, XIICPS_CR_HOLD_MASK},
95 #define XIICPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionsMap))
97 /*****************************************************************************/
100 * This function sets the options for the IIC device driver. The options control
101 * how the device behaves relative to the IIC bus. The device must be idle
102 * rather than busy transferring data before setting these device options.
104 * @param InstancePtr is a pointer to the XIicPs instance.
105 * @param Options contains the specified options to be set. This is a bit
106 * mask where a 1 means to turn the option on. One or more bit
107 * values may be contained in the mask. See the bit definitions
108 * named XIICPS_*_OPTION in xiicps.h.
111 * - XST_SUCCESS if options are successfully set.
112 * - XST_DEVICE_IS_STARTED if the device is currently transferring
113 * data. The transfer must complete or be aborted before setting
118 ******************************************************************************/
119 s32 XIicPs_SetOptions(XIicPs *InstancePtr, u32 Options)
123 u32 OptionsVar = Options;
125 Xil_AssertNonvoid(InstancePtr != NULL);
126 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
128 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
132 * If repeated start option is requested, set the flag.
133 * The hold bit in CR will be written by driver when the next transfer
136 if ((OptionsVar & XIICPS_REP_START_OPTION) != 0U ) {
137 InstancePtr->IsRepeatedStart = 1;
138 OptionsVar = OptionsVar & (~XIICPS_REP_START_OPTION);
142 * Loop through the options table, turning the option on.
144 for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
145 if ((OptionsVar & OptionsTable[Index].Option) != (u32)0x0U) {
147 * 10-bit option is specially treated, because it is
148 * using the 7-bit option, so turning it on means
149 * turning 7-bit option off.
151 if ((OptionsTable[Index].Option &
152 XIICPS_10_BIT_ADDR_OPTION) != (u32)0x0U) {
154 ControlReg &= ~OptionsTable[Index].Mask;
157 ControlReg |= OptionsTable[Index].Mask;
163 * Now write to the control register. Leave it to the upper layers
164 * to restart the device.
166 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
170 * Keep a copy of what options this instance has.
172 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
174 return (s32)XST_SUCCESS;
177 /*****************************************************************************/
180 * This function clears the options for the IIC device driver. The options
181 * control how the device behaves relative to the IIC bus. The device must be
182 * idle rather than busy transferring data before setting these device options.
184 * @param InstancePtr is a pointer to the XIicPs instance.
185 * @param Options contains the specified options to be cleared. This is a
186 * bit mask where a 1 means to turn the option off. One or more bit
187 * values may be contained in the mask. See the bit definitions
188 * named XIICPS_*_OPTION in xiicps.h.
191 * - XST_SUCCESS if options are successfully set.
192 * - XST_DEVICE_IS_STARTED if the device is currently transferring
193 * data. The transfer must complete or be aborted before setting
198 ******************************************************************************/
199 s32 XIicPs_ClearOptions(XIicPs *InstancePtr, u32 Options)
203 u32 OptionsVar = Options;
205 Xil_AssertNonvoid(InstancePtr != NULL);
206 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
208 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
212 * If repeated start option is cleared, set the flag.
213 * The hold bit in CR will be cleared by driver when the
214 * following transfer ends.
216 if ((OptionsVar & XIICPS_REP_START_OPTION) != (u32)0x0U ) {
217 InstancePtr->IsRepeatedStart = 0;
218 OptionsVar = OptionsVar & (~XIICPS_REP_START_OPTION);
222 * Loop through the options table and clear the specified options.
224 for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
225 if ((OptionsVar & OptionsTable[Index].Option) != (u32)0x0U) {
228 * 10-bit option is specially treated, because it is
229 * using the 7-bit option, so clearing it means turning
232 if ((OptionsTable[Index].Option &
233 XIICPS_10_BIT_ADDR_OPTION) != (u32)0x0U) {
236 ControlReg |= OptionsTable[Index].Mask;
240 ControlReg &= ~OptionsTable[Index].Mask;
247 * Now write the control register. Leave it to the upper layers
248 * to restart the device.
250 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
254 * Keep a copy of what options this instance has.
256 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
261 /*****************************************************************************/
264 * This function gets the options for the IIC device. The options control how
265 * the device behaves relative to the IIC bus.
267 * @param InstancePtr is a pointer to the XIicPs instance.
269 * @return 32 bit mask of the options, where a 1 means the option is on,
270 * and a 0 means to the option is off. One or more bit values may
271 * be contained in the mask. See the bit definitions named
272 * XIICPS_*_OPTION in the file xiicps.h.
276 ******************************************************************************/
277 u32 XIicPs_GetOptions(XIicPs *InstancePtr)
279 u32 OptionsFlag = 0U;
283 Xil_AssertNonvoid(InstancePtr != NULL);
284 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
287 * Read control register to find which options are currently set.
289 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
293 * Loop through the options table to determine which options are set.
295 for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
296 if ((ControlReg & OptionsTable[Index].Mask) != (u32)0x0U) {
297 OptionsFlag |= OptionsTable[Index].Option;
299 if ((ControlReg & XIICPS_CR_NEA_MASK) == (u32)0x0U) {
300 OptionsFlag |= XIICPS_10_BIT_ADDR_OPTION;
304 if (InstancePtr->IsRepeatedStart != 0 ) {
305 OptionsFlag |= XIICPS_REP_START_OPTION;
310 /*****************************************************************************/
313 * This function sets the serial clock rate for the IIC device. The device
314 * must be idle rather than busy transferring data before setting these device
317 * The data rate is set by values in the control register. The formula for
318 * determining the correct register values is:
319 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
320 * See the hardware data sheet for a full explanation of setting the serial
323 * @param InstancePtr is a pointer to the XIicPs instance.
324 * @param FsclHz is the clock frequency in Hz. The two most common clock
325 * rates are 100KHz and 400KHz.
328 * - XST_SUCCESS if options are successfully set.
329 * - XST_DEVICE_IS_STARTED if the device is currently transferring
330 * data. The transfer must complete or be aborted before setting
332 * - XST_FAILURE if the Fscl frequency can not be set.
334 * @note The clock can not be faster than the input clock divide by 22.
336 ******************************************************************************/
337 s32 XIicPs_SetSClk(XIicPs *InstancePtr, u32 FsclHz)
352 u32 FsclHzVar = FsclHz;
354 Xil_AssertNonvoid(InstancePtr != NULL);
355 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
356 Xil_AssertNonvoid(FsclHzVar > 0U);
358 if (0U != XIicPs_In32((InstancePtr->Config.BaseAddress) +
359 XIICPS_TRANS_SIZE_OFFSET)) {
360 return (s32)XST_DEVICE_IS_STARTED;
364 * Assume Div_a is 0 and calculate (divisor_a+1) x (divisor_b+1).
366 Temp = (InstancePtr->Config.InputClockHz) / ((u32)22U * FsclHzVar);
369 * If the answer is negative or 0, the Fscl input is out of range.
371 if ((u32)(0U) == Temp) {
372 return (s32)XST_FAILURE;
376 * If frequency 400KHz is selected, 384.6KHz should be set.
377 * If frequency 100KHz is selected, 90KHz should be set.
378 * This is due to a hardware limitation.
380 if(FsclHzVar > 384600U) {
384 if((FsclHzVar <= 100000U) && (FsclHzVar > 90000U)) {
389 * TempLimit helps in iterating over the consecutive value of Temp to
390 * find the closest clock rate achievable with divisors.
391 * Iterate over the next value only if fractional part is involved.
393 TempLimit = (((InstancePtr->Config.InputClockHz) %
394 ((u32)22 * FsclHzVar)) != (u32)0x0U) ?
395 Temp + (u32)1U : Temp;
396 BestError = FsclHzVar;
400 for ( ; Temp <= TempLimit ; Temp++)
402 LastError = FsclHzVar;
406 for (Div_b = 0U; Div_b < 64U; Div_b++) {
408 Div_a = Temp / (Div_b + 1U);
411 Div_a = Div_a - (u32)1U;
416 ActualFscl = (InstancePtr->Config.InputClockHz) /
417 (22U * (Div_a + 1U) * (Div_b + 1U));
419 if (ActualFscl > FsclHzVar){
420 CurrentError = (ActualFscl - FsclHzVar);}
422 CurrentError = (FsclHzVar - ActualFscl);}
424 if (LastError > CurrentError) {
427 LastError = CurrentError;
432 * Used to capture the best divisors.
434 if (LastError < BestError) {
435 BestError = LastError;
443 * Read the control register and mask the Divisors.
445 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
446 (u32)XIICPS_CR_OFFSET);
447 ControlReg &= ~((u32)XIICPS_CR_DIV_A_MASK | (u32)XIICPS_CR_DIV_B_MASK);
448 ControlReg |= (BestDivA << XIICPS_CR_DIV_A_SHIFT) |
449 (BestDivB << XIICPS_CR_DIV_B_SHIFT);
451 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, (u32)XIICPS_CR_OFFSET,
454 return (s32)XST_SUCCESS;
457 /*****************************************************************************/
460 * This function gets the serial clock rate for the IIC device. The device
461 * must be idle rather than busy transferring data before setting these device
464 * @param InstancePtr is a pointer to the XIicPs instance.
466 * @return The value of the IIC clock to the nearest Hz based on the
467 * control register settings. The actual value may not be exact to
468 * to integer math rounding errors.
472 ******************************************************************************/
473 u32 XIicPs_GetSClk(XIicPs *InstancePtr)
480 Xil_AssertNonvoid(InstancePtr != NULL);
481 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
483 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
486 Div_a = (ControlReg & XIICPS_CR_DIV_A_MASK) >> XIICPS_CR_DIV_A_SHIFT;
487 Div_b = (ControlReg & XIICPS_CR_DIV_B_MASK) >> XIICPS_CR_DIV_B_SHIFT;
489 ActualFscl = (InstancePtr->Config.InputClockHz) /
490 (22U * (Div_a + 1U) * (Div_b + 1U));