]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/iicps_v3_0/src/xiicps_options.c
Add in the CORTEX_A53_64-bit_UltraScale_MPSoC demo application (a demo has been inclu...
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / iicps_v3_0 / src / xiicps_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 xiicps_options.c
36 *
37 * Contains functions for the configuration of the XIccPs driver.
38 *
39 * <pre>
40 * MODIFICATION HISTORY:
41 *
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.
56 *
57 * </pre>
58 *
59 ******************************************************************************/
60
61 /***************************** Include Files *********************************/
62
63 #include "xiicps.h"
64
65 /************************** Constant Definitions *****************************/
66
67
68 /**************************** Type Definitions *******************************/
69
70
71 /***************** Macros (Inline Functions) Definitions *********************/
72
73
74 /************************** Function Prototypes ******************************/
75
76
77 /************************** Variable Definitions *****************************/
78 /*
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.
82  */
83 typedef struct {
84                 u32 Option;
85                 u32 Mask;
86 } OptionsMap;
87
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},
93 };
94
95 #define XIICPS_NUM_OPTIONS      (sizeof(OptionsTable) / sizeof(OptionsMap))
96
97 /*****************************************************************************/
98 /**
99 *
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.
103 *
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.
109 *
110 * @return
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
114 *               options.
115 *
116 * @note         None.
117 *
118 ******************************************************************************/
119 s32 XIicPs_SetOptions(XIicPs *InstancePtr, u32 Options)
120 {
121         u32 ControlReg;
122         u32 Index;
123         u32 OptionsVar = Options;
124
125         Xil_AssertNonvoid(InstancePtr != NULL);
126         Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
127
128         ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
129                                       XIICPS_CR_OFFSET);
130
131         /*
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
134          * is initiated.
135          */
136         if ((OptionsVar & XIICPS_REP_START_OPTION) != 0U ) {
137                 InstancePtr->IsRepeatedStart = 1;
138                 OptionsVar = OptionsVar & (~XIICPS_REP_START_OPTION);
139         }
140
141         /*
142          * Loop through the options table, turning the option on.
143          */
144         for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
145                 if ((OptionsVar & OptionsTable[Index].Option) != (u32)0x0U) {
146                         /*
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.
150                          */
151                         if ((OptionsTable[Index].Option &
152                                 XIICPS_10_BIT_ADDR_OPTION) != (u32)0x0U) {
153                                 /* Turn 7-bit off */
154                                 ControlReg &= ~OptionsTable[Index].Mask;
155                         } else {
156                                 /* Turn 7-bit on */
157                                 ControlReg |= OptionsTable[Index].Mask;
158                         }
159                 }
160         }
161
162         /*
163          * Now write to the control register. Leave it to the upper layers
164          * to restart the device.
165          */
166         XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
167                           ControlReg);
168
169         /*
170          * Keep a copy of what options this instance has.
171          */
172         InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
173
174         return (s32)XST_SUCCESS;
175 }
176
177 /*****************************************************************************/
178 /**
179 *
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.
183 *
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.
189 *
190 * @return
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
194 *               options.
195 *
196 * @note         None
197 *
198 ******************************************************************************/
199 s32 XIicPs_ClearOptions(XIicPs *InstancePtr, u32 Options)
200 {
201         u32 ControlReg;
202         u32 Index;
203         u32 OptionsVar = Options;
204
205         Xil_AssertNonvoid(InstancePtr != NULL);
206         Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
207
208         ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
209                                         XIICPS_CR_OFFSET);
210
211         /*
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.
215          */
216         if ((OptionsVar & XIICPS_REP_START_OPTION) != (u32)0x0U ) {
217                 InstancePtr->IsRepeatedStart = 0;
218                 OptionsVar = OptionsVar & (~XIICPS_REP_START_OPTION);
219         }
220
221         /*
222          * Loop through the options table and clear the specified options.
223          */
224         for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
225                 if ((OptionsVar & OptionsTable[Index].Option) != (u32)0x0U) {
226
227                         /*
228                          * 10-bit option is specially treated, because it is
229                          * using the 7-bit option, so clearing it means turning
230                          * 7-bit option on.
231                          */
232                         if ((OptionsTable[Index].Option &
233                                 XIICPS_10_BIT_ADDR_OPTION) != (u32)0x0U) {
234
235                                 /* Turn 7-bit on */
236                                 ControlReg |= OptionsTable[Index].Mask;
237                         } else {
238
239                                 /* Turn 7-bit off */
240                                 ControlReg &= ~OptionsTable[Index].Mask;
241                         }
242                 }
243         }
244
245
246         /*
247          * Now write the control register. Leave it to the upper layers
248          * to restart the device.
249          */
250         XIicPs_WriteReg(InstancePtr->Config.BaseAddress, XIICPS_CR_OFFSET,
251                           ControlReg);
252
253         /*
254          * Keep a copy of what options this instance has.
255          */
256         InstancePtr->Options = XIicPs_GetOptions(InstancePtr);
257
258         return XST_SUCCESS;
259 }
260
261 /*****************************************************************************/
262 /**
263 *
264 * This function gets the options for the IIC device. The options control how
265 * the device behaves relative to the IIC bus.
266 *
267 * @param        InstancePtr is a pointer to the XIicPs instance.
268 *
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.
273 *
274 * @note         None.
275 *
276 ******************************************************************************/
277 u32 XIicPs_GetOptions(XIicPs *InstancePtr)
278 {
279         u32 OptionsFlag = 0U;
280         u32 ControlReg;
281         u32 Index;
282
283         Xil_AssertNonvoid(InstancePtr != NULL);
284         Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
285
286         /*
287          * Read control register to find which options are currently set.
288          */
289         ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
290                                       XIICPS_CR_OFFSET);
291
292         /*
293          * Loop through the options table to determine which options are set.
294          */
295         for (Index = 0U; Index < XIICPS_NUM_OPTIONS; Index++) {
296                 if ((ControlReg & OptionsTable[Index].Mask) != (u32)0x0U) {
297                         OptionsFlag |= OptionsTable[Index].Option;
298                 }
299                 if ((ControlReg & XIICPS_CR_NEA_MASK) == (u32)0x0U) {
300                         OptionsFlag |= XIICPS_10_BIT_ADDR_OPTION;
301                 }
302         }
303
304         if (InstancePtr->IsRepeatedStart != 0 ) {
305                 OptionsFlag |= XIICPS_REP_START_OPTION;
306         }
307         return OptionsFlag;
308 }
309
310 /*****************************************************************************/
311 /**
312 *
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
315 * options.
316 *
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
321 * clock rate.
322 *
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.
326 *
327 * @return
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
331 *               options.
332 *               - XST_FAILURE if the Fscl frequency can not be set.
333 *
334 * @note         The clock can not be faster than the input clock divide by 22.
335 *
336 ******************************************************************************/
337 s32 XIicPs_SetSClk(XIicPs *InstancePtr, u32 FsclHz)
338 {
339         u32 Div_a;
340         u32 Div_b;
341         u32 ActualFscl;
342         u32 Temp;
343         u32 TempLimit;
344         u32 LastError;
345         u32 BestError;
346         u32 CurrentError;
347         u32 ControlReg;
348         u32 CalcDivA;
349         u32 CalcDivB;
350         u32 BestDivA = 0;
351         u32 BestDivB = 0;
352         u32 FsclHzVar = FsclHz;
353
354         Xil_AssertNonvoid(InstancePtr != NULL);
355         Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
356         Xil_AssertNonvoid(FsclHzVar > 0U);
357
358         if (0U != XIicPs_In32((InstancePtr->Config.BaseAddress) +
359                                         XIICPS_TRANS_SIZE_OFFSET)) {
360                 return (s32)XST_DEVICE_IS_STARTED;
361         }
362
363         /*
364          * Assume Div_a is 0 and calculate (divisor_a+1) x (divisor_b+1).
365          */
366         Temp = (InstancePtr->Config.InputClockHz) / ((u32)22U * FsclHzVar);
367
368         /*
369          * If the answer is negative or 0, the Fscl input is out of range.
370          */
371         if ((u32)(0U) == Temp) {
372                 return (s32)XST_FAILURE;
373         }
374
375         /*
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.
379          */
380         if(FsclHzVar > 384600U) {
381                 FsclHzVar = 384600U;
382         }
383
384         if((FsclHzVar <= 100000U) && (FsclHzVar > 90000U)) {
385                 FsclHzVar = 90000U;
386         }
387
388         /*
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.
392          */
393         TempLimit = (((InstancePtr->Config.InputClockHz) %
394                         ((u32)22 * FsclHzVar)) !=       (u32)0x0U) ?
395                                                 Temp + (u32)1U : Temp;
396         BestError = FsclHzVar;
397
398         BestDivA = 0U;
399         BestDivB = 0U;
400         for ( ; Temp <= TempLimit ; Temp++)
401         {
402                 LastError = FsclHzVar;
403                 CalcDivA = 0U;
404                 CalcDivB = 0U;
405
406                 for (Div_b = 0U; Div_b < 64U; Div_b++) {
407
408                         Div_a = Temp / (Div_b + 1U);
409
410                         if (Div_a != 0U){
411                                 Div_a = Div_a - (u32)1U;
412                         }
413                         if (Div_a > 3U){
414                                 continue;
415                         }
416                         ActualFscl = (InstancePtr->Config.InputClockHz) /
417                                                 (22U * (Div_a + 1U) * (Div_b + 1U));
418
419                         if (ActualFscl > FsclHzVar){
420                                 CurrentError = (ActualFscl - FsclHzVar);}
421                         else{
422                                 CurrentError = (FsclHzVar - ActualFscl);}
423
424                         if (LastError > CurrentError) {
425                                 CalcDivA = Div_a;
426                                 CalcDivB = Div_b;
427                                 LastError = CurrentError;
428                         }
429                 }
430
431                 /*
432                  * Used to capture the best divisors.
433                  */
434                 if (LastError < BestError) {
435                         BestError = LastError;
436                         BestDivA = CalcDivA;
437                         BestDivB = CalcDivB;
438                 }
439         }
440
441
442         /*
443          * Read the control register and mask the Divisors.
444          */
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);
450
451         XIicPs_WriteReg(InstancePtr->Config.BaseAddress, (u32)XIICPS_CR_OFFSET,
452                           ControlReg);
453
454         return (s32)XST_SUCCESS;
455 }
456
457 /*****************************************************************************/
458 /**
459 *
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
462 * options.
463 *
464 * @param        InstancePtr is a pointer to the XIicPs instance.
465 *
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.
469 *
470 * @note         None.
471 *
472 ******************************************************************************/
473 u32 XIicPs_GetSClk(XIicPs *InstancePtr)
474 {
475         u32 ControlReg;
476         u32 ActualFscl;
477         u32 Div_a;
478         u32 Div_b;
479
480         Xil_AssertNonvoid(InstancePtr != NULL);
481         Xil_AssertNonvoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
482
483         ControlReg = XIicPs_ReadReg(InstancePtr->Config.BaseAddress,
484                                           XIICPS_CR_OFFSET);
485
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;
488
489         ActualFscl = (InstancePtr->Config.InputClockHz) /
490                 (22U * (Div_a + 1U) * (Div_b + 1U));
491
492         return ActualFscl;
493 }