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
36 * @addtogroup iicps_v3_0
39 * Contains functions for the configuration of the XIccPs driver.
42 * MODIFICATION HISTORY:
44 * Ver Who Date Changes
45 * ----- ------ -------- -----------------------------------------------
46 * 1.00a drg/jz 01/30/10 First release
47 * 1.02a sg 08/29/12 Updated the logic to arrive at the best divisors
48 * to achieve I2C clock with minimum error.
49 * This is a fix for CR #674195
50 * 1.03a hk 05/04/13 Initialized BestDivA and BestDivB to 0.
51 * This is fix for CR#704398 to remove warning.
52 * 2.0 hk 03/07/14 Limited frequency set when 100KHz or 400KHz is
53 * selected. This is a hardware limitation. CR#779290.
54 * 2.1 hk 04/24/14 Fix for CR# 761060 - provision for repeated start.
55 * 2.3 sk 10/07/14 Repeated start feature removed.
56 * 3.0 sk 12/06/14 Implemented Repeated start feature.
57 * 01/31/15 Modified the code according to MISRAC 2012 Compliant.
61 ******************************************************************************/
63 /***************************** Include Files *********************************/
67 /************************** Constant Definitions *****************************/
70 /**************************** Type Definitions *******************************/
73 /***************** Macros (Inline Functions) Definitions *********************/
76 /************************** Function Prototypes ******************************/
79 /************************** Variable Definitions *****************************/
81 * Create the table of options which are processed to get/set the device
82 * options. These options are table driven to allow easy maintenance and
83 * expansion of the options.
90 static OptionsMap OptionsTable[] = {
91 {XIICPS_7_BIT_ADDR_OPTION, XIICPS_CR_NEA_MASK},
92 {XIICPS_10_BIT_ADDR_OPTION, XIICPS_CR_NEA_MASK},
93 {XIICPS_SLAVE_MON_OPTION, XIICPS_CR_SLVMON_MASK},
94 {XIICPS_REP_START_OPTION, XIICPS_CR_HOLD_MASK},
97 #define XIICPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(OptionsMap))
99 /*****************************************************************************/
102 * This function sets the options for the IIC device driver. The options control
103 * how the device behaves relative to the IIC bus. The device must be idle
104 * rather than busy transferring data before setting these device options.
106 * @param InstancePtr is a pointer to the XIicPs instance.
107 * @param Options contains the specified options to be set. This is a bit
108 * mask where a 1 means to turn the option on. One or more bit
109 * values may be contained in the mask. See the bit definitions
110 * named XIICPS_*_OPTION in xiicps.h.
113 * - XST_SUCCESS if options are successfully set.
114 * - XST_DEVICE_IS_STARTED if the device is currently transferring
115 * data. The transfer must complete or be aborted before setting
120 ******************************************************************************/
121 s32 XIicPs_SetOptions(XIicPs *InstancePtr, u32 Options)
125 u32 OptionsVar = Options;
127 Xil_AssertNonvoid(InstancePtr != NULL);
128 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
130 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
134 * If repeated start option is requested, set the flag.
135 * The hold bit in CR will be written by driver when the next transfer
138 if ((OptionsVar & XIICPS_REP_START_OPTION) != 0U ) {
139 InstancePtr->IsRepeatedStart = 1;
140 OptionsVar = OptionsVar & (~XIICPS_REP_START_OPTION);
144 * Loop through the options table, turning the option on.
146 for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
147 if ((OptionsVar & OptionsTable[Index].Option) != (u32)0x0U) {
149 * 10-bit option is specially treated, because it is
150 * using the 7-bit option, so turning it on means
151 * turning 7-bit option off.
153 if ((OptionsTable[Index].Option &
154 XIICPS_10_BIT_ADDR_OPTION) != (u32)0x0U) {
156 ControlReg &= ~OptionsTable[Index].Mask;
159 ControlReg |= OptionsTable[Index].Mask;
165 * Now write to the control register. Leave it to the upper layers
166 * to restart the device.
168 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
172 * Keep a copy of what options this instance has.
174 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
176 return (s32)XST_SUCCESS;
179 /*****************************************************************************/
182 * This function clears the options for the IIC device driver. The options
183 * control how the device behaves relative to the IIC bus. The device must be
184 * idle rather than busy transferring data before setting these device options.
186 * @param InstancePtr is a pointer to the XIicPs instance.
187 * @param Options contains the specified options to be cleared. This is a
188 * bit mask where a 1 means to turn the option off. One or more bit
189 * values may be contained in the mask. See the bit definitions
190 * named XIICPS_*_OPTION in xiicps.h.
193 * - XST_SUCCESS if options are successfully set.
194 * - XST_DEVICE_IS_STARTED if the device is currently transferring
195 * data. The transfer must complete or be aborted before setting
200 ******************************************************************************/
201 s32 XIicPs_ClearOptions(XIicPs *InstancePtr, u32 Options)
205 u32 OptionsVar = Options;
207 Xil_AssertNonvoid(InstancePtr != NULL);
208 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
210 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
214 * If repeated start option is cleared, set the flag.
215 * The hold bit in CR will be cleared by driver when the
216 * following transfer ends.
218 if ((OptionsVar & XIICPS_REP_START_OPTION) != (u32)0x0U ) {
219 InstancePtr->IsRepeatedStart = 0;
220 OptionsVar = OptionsVar & (~XIICPS_REP_START_OPTION);
224 * Loop through the options table and clear the specified options.
226 for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
227 if ((OptionsVar & OptionsTable[Index].Option) != (u32)0x0U) {
230 * 10-bit option is specially treated, because it is
231 * using the 7-bit option, so clearing it means turning
234 if ((OptionsTable[Index].Option &
235 XIICPS_10_BIT_ADDR_OPTION) != (u32)0x0U) {
238 ControlReg |= OptionsTable[Index].Mask;
242 ControlReg &= ~OptionsTable[Index].Mask;
249 * Now write the control register. Leave it to the upper layers
250 * to restart the device.
252 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
256 * Keep a copy of what options this instance has.
258 InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
263 /*****************************************************************************/
266 * This function gets the options for the IIC device. The options control how
267 * the device behaves relative to the IIC bus.
269 * @param InstancePtr is a pointer to the XIicPs instance.
271 * @return 32 bit mask of the options, where a 1 means the option is on,
272 * and a 0 means to the option is off. One or more bit values may
273 * be contained in the mask. See the bit definitions named
274 * XIICPS_*_OPTION in the file xiicps.h.
278 ******************************************************************************/
279 u32 XIicPs_GetOptions(XIicPs *InstancePtr)
281 u32 OptionsFlag = 0U;
285 Xil_AssertNonvoid(InstancePtr != NULL);
286 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
289 * Read control register to find which options are currently set.
291 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
295 * Loop through the options table to determine which options are set.
297 for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
298 if ((ControlReg & OptionsTable[Index].Mask) != (u32)0x0U) {
299 OptionsFlag |= OptionsTable[Index].Option;
301 if ((ControlReg & XIICPS_CR_NEA_MASK) == (u32)0x0U) {
302 OptionsFlag |= XIICPS_10_BIT_ADDR_OPTION;
306 if (InstancePtr->IsRepeatedStart != 0 ) {
307 OptionsFlag |= XIICPS_REP_START_OPTION;
312 /*****************************************************************************/
315 * This function sets the serial clock rate for the IIC device. The device
316 * must be idle rather than busy transferring data before setting these device
319 * The data rate is set by values in the control register. The formula for
320 * determining the correct register values is:
321 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
322 * See the hardware data sheet for a full explanation of setting the serial
325 * @param InstancePtr is a pointer to the XIicPs instance.
326 * @param FsclHz is the clock frequency in Hz. The two most common clock
327 * rates are 100KHz and 400KHz.
330 * - XST_SUCCESS if options are successfully set.
331 * - XST_DEVICE_IS_STARTED if the device is currently transferring
332 * data. The transfer must complete or be aborted before setting
334 * - XST_FAILURE if the Fscl frequency can not be set.
336 * @note The clock can not be faster than the input clock divide by 22.
338 ******************************************************************************/
339 s32 XIicPs_SetSClk(XIicPs *InstancePtr, u32 FsclHz)
354 u32 FsclHzVar = FsclHz;
356 Xil_AssertNonvoid(InstancePtr != NULL);
357 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
358 Xil_AssertNonvoid(FsclHzVar > 0U);
360 if (0U != XIicPs_In32((InstancePtr->Config.BaseAddress) +
361 XIICPS_TRANS_SIZE_OFFSET)) {
362 return (s32)XST_DEVICE_IS_STARTED;
366 * Assume Div_a is 0 and calculate (divisor_a+1) x (divisor_b+1).
368 Temp = (InstancePtr->Config.InputClockHz) / ((u32)22U * FsclHzVar);
371 * If the answer is negative or 0, the Fscl input is out of range.
373 if ((u32)(0U) == Temp) {
374 return (s32)XST_FAILURE;
378 * If frequency 400KHz is selected, 384.6KHz should be set.
379 * If frequency 100KHz is selected, 90KHz should be set.
380 * This is due to a hardware limitation.
382 if(FsclHzVar > 384600U) {
386 if((FsclHzVar <= 100000U) && (FsclHzVar > 90000U)) {
391 * TempLimit helps in iterating over the consecutive value of Temp to
392 * find the closest clock rate achievable with divisors.
393 * Iterate over the next value only if fractional part is involved.
395 TempLimit = (((InstancePtr->Config.InputClockHz) %
396 ((u32)22 * FsclHzVar)) != (u32)0x0U) ?
397 Temp + (u32)1U : Temp;
398 BestError = FsclHzVar;
402 for ( ; Temp <= TempLimit ; Temp++)
404 LastError = FsclHzVar;
408 for (Div_b = 0U; Div_b < 64U; Div_b++) {
410 Div_a = Temp / (Div_b + 1U);
413 Div_a = Div_a - (u32)1U;
418 ActualFscl = (InstancePtr->Config.InputClockHz) /
419 (22U * (Div_a + 1U) * (Div_b + 1U));
421 if (ActualFscl > FsclHzVar){
422 CurrentError = (ActualFscl - FsclHzVar);}
424 CurrentError = (FsclHzVar - ActualFscl);}
426 if (LastError > CurrentError) {
429 LastError = CurrentError;
434 * Used to capture the best divisors.
436 if (LastError < BestError) {
437 BestError = LastError;
445 * Read the control register and mask the Divisors.
447 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
448 (u32)XIICPS_CR_OFFSET);
449 ControlReg &= ~((u32)XIICPS_CR_DIV_A_MASK | (u32)XIICPS_CR_DIV_B_MASK);
450 ControlReg |= (BestDivA << XIICPS_CR_DIV_A_SHIFT) |
451 (BestDivB << XIICPS_CR_DIV_B_SHIFT);
453 XIicPs_WriteReg(InstancePtr->Config.BaseAddress, (u32)XIICPS_CR_OFFSET,
456 return (s32)XST_SUCCESS;
459 /*****************************************************************************/
462 * This function gets the serial clock rate for the IIC device. The device
463 * must be idle rather than busy transferring data before setting these device
466 * @param InstancePtr is a pointer to the XIicPs instance.
468 * @return The value of the IIC clock to the nearest Hz based on the
469 * control register settings. The actual value may not be exact to
470 * to integer math rounding errors.
474 ******************************************************************************/
475 u32 XIicPs_GetSClk(XIicPs *InstancePtr)
482 Xil_AssertNonvoid(InstancePtr != NULL);
483 Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
485 ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
488 Div_a = (ControlReg & XIICPS_CR_DIV_A_MASK) >> XIICPS_CR_DIV_A_SHIFT;
489 Div_b = (ControlReg & XIICPS_CR_DIV_B_MASK) >> XIICPS_CR_DIV_B_SHIFT;
491 ActualFscl = (InstancePtr->Config.InputClockHz) /
492 (22U * (Div_a + 1U) * (Div_b + 1U));