]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/uartps_v3_3/src/xuartps_options.c
xTaskGenericNotify() now sets xYieldPending to pdTRUE even when the 'higher priority...
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / uartps_v3_3 / src / xuartps_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 xuartps_options.c
36 * @addtogroup uartps_v3_1
37 * @{
38 *
39 * The implementation of the options functions for the XUartPs driver.
40 *
41 * <pre>
42 * MODIFICATION HISTORY:
43 *
44 * Ver   Who    Date     Changes
45 * ----- ------ -------- -----------------------------------------------
46 * 1.00  drg/jz 01/13/10 First Release
47 * 1.00  sdm    09/27/11 Fixed a bug in XUartPs_SetFlowDelay where the input
48 *                       value was not being written to the register.
49 * 3.00  kvn    02/13/15 Modified code for MISRA-C:2012 compliance.
50 * 3.2   rk     07/20/16 Modified the logic for transmission break bit set
51 *
52 * </pre>
53 *
54 *****************************************************************************/
55
56 /***************************** Include Files ********************************/
57
58 #include "xuartps.h"
59
60 /************************** Constant Definitions ****************************/
61
62 /**************************** Type Definitions ******************************/
63
64 /***************** Macros (Inline Functions) Definitions ********************/
65
66 /************************** Variable Definitions ****************************/
67 /*
68  * The following data type is a map from an option to the offset in the
69  * register to which it belongs as well as its bit mask in that register.
70  */
71 typedef struct {
72         u16 Option;
73         u16 RegisterOffset;
74         u32 Mask;
75 } Mapping;
76
77 /*
78  * Create the table which contains options which are to be processed to get/set
79  * the options. These options are table driven to allow easy maintenance and
80  * expansion of the options.
81  */
82
83 static Mapping OptionsTable[] = {
84         {XUARTPS_OPTION_SET_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STARTBRK},
85         {XUARTPS_OPTION_STOP_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STOPBRK},
86         {XUARTPS_OPTION_RESET_TMOUT, XUARTPS_CR_OFFSET, XUARTPS_CR_TORST},
87         {XUARTPS_OPTION_RESET_TX, XUARTPS_CR_OFFSET, XUARTPS_CR_TXRST},
88         {XUARTPS_OPTION_RESET_RX, XUARTPS_CR_OFFSET, XUARTPS_CR_RXRST},
89         {XUARTPS_OPTION_ASSERT_RTS, XUARTPS_MODEMCR_OFFSET,
90          XUARTPS_MODEMCR_RTS},
91         {XUARTPS_OPTION_ASSERT_DTR, XUARTPS_MODEMCR_OFFSET,
92          XUARTPS_MODEMCR_DTR},
93         {XUARTPS_OPTION_SET_FCM, XUARTPS_MODEMCR_OFFSET, XUARTPS_MODEMCR_FCM}
94 };
95
96 /* Create a constant for the number of entries in the table */
97
98 #define XUARTPS_NUM_OPTIONS       (sizeof(OptionsTable) / sizeof(Mapping))
99
100 /************************** Function Prototypes *****************************/
101
102 /****************************************************************************/
103 /**
104 *
105 * Gets the options for the specified driver instance. The options are
106 * implemented as bit masks such that multiple options may be enabled or
107 * disabled simulataneously.
108 *
109 * @param        InstancePtr is a pointer to the XUartPs instance.
110 *
111 * @return
112 *
113 * The current options for the UART. The optionss are bit masks that are
114 * contained in the file xuartps.h and named XUARTPS_OPTION_*.
115 *
116 * @note         None.
117 *
118 *****************************************************************************/
119 u16 XUartPs_GetOptions(XUartPs *InstancePtr)
120 {
121         u16 Options = 0U;
122         u32 Register;
123         u32 Index;
124
125         /* Assert validates the input arguments */
126         Xil_AssertNonvoid(InstancePtr != NULL);
127         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
128
129         /*
130          * Loop thru the options table to map the physical options in the
131          * registers of the UART to the logical options to be returned
132          */
133         for (Index = 0U; Index < XUARTPS_NUM_OPTIONS; Index++) {
134                 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
135                                                  OptionsTable[Index].
136                                                  RegisterOffset);
137
138                 /*
139                  * If the bit in the register which correlates to the option
140                  * is set, then set the corresponding bit in the options,
141                  * ignoring any bits which are zero since the options variable
142                  * is initialized to zero
143                  */
144                 if ((Register & OptionsTable[Index].Mask) != (u32)0) {
145                         Options |= OptionsTable[Index].Option;
146                 }
147         }
148
149         return Options;
150 }
151
152 /****************************************************************************/
153 /**
154 *
155 * Sets the options for the specified driver instance. The options are
156 * implemented as bit masks such that multiple options may be enabled or
157 * disabled simultaneously.
158 *
159 * The GetOptions function may be called to retrieve the currently enabled
160 * options. The result is ORed in the desired new settings to be enabled and
161 * ANDed with the inverse to clear the settings to be disabled. The resulting
162 * value is then used as the options for the SetOption function call.
163 *
164 * @param        InstancePtr is a pointer to the XUartPs instance.
165 * @param        Options contains the options to be set which are bit masks
166 *               contained in the file xuartps.h and named XUARTPS_OPTION_*.
167 *
168 * @return       None.
169 *
170 * @note         None.
171 *
172 *****************************************************************************/
173 void XUartPs_SetOptions(XUartPs *InstancePtr, u16 Options)
174 {
175         u32 Index;
176         u32 Register;
177
178         /* Assert validates the input arguments */
179         Xil_AssertVoid(InstancePtr != NULL);
180         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
181
182         /*
183          * Loop thru the options table to map the logical options to the
184          * physical options in the registers of the UART.
185          */
186         for (Index = 0U; Index < XUARTPS_NUM_OPTIONS; Index++) {
187
188                 /*
189                  * Read the register which contains option so that the register
190                  * can be changed without destoying any other bits of the
191                  * register.
192                  */
193                 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
194                                                  OptionsTable[Index].
195                                                  RegisterOffset);
196
197                 /*
198                  * If the option is set in the input, then set the corresponding
199                  * bit in the specified register, otherwise clear the bit in
200                  * the register.
201                  */
202                 if ((Options & OptionsTable[Index].Option) != (u16)0) {
203                         if(OptionsTable[Index].Option == XUARTPS_OPTION_SET_BREAK)
204                                 Register &= ~XUARTPS_CR_STOPBRK;
205                         Register |= OptionsTable[Index].Mask;
206                 }
207                 else {
208                         Register &= ~OptionsTable[Index].Mask;
209                 }
210
211                 /* Write the new value to the register to set the option */
212                 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
213                                    OptionsTable[Index].RegisterOffset,
214                                    Register);
215         }
216
217 }
218
219 /****************************************************************************/
220 /**
221 *
222 * This function gets the receive FIFO trigger level. The receive trigger
223 * level indicates the number of bytes in the receive FIFO that cause a receive
224 * data event (interrupt) to be generated.
225 *
226 * @param        InstancePtr is a pointer to the XUartPs instance.
227 *
228 * @return       The current receive FIFO trigger level. This is a value
229 *               from 0-31.
230 *
231 * @note         None.
232 *
233 *****************************************************************************/
234 u8 XUartPs_GetFifoThreshold(XUartPs *InstancePtr)
235 {
236         u8 RtrigRegister;
237
238         /* Assert validates the input arguments */
239         Xil_AssertNonvoid(InstancePtr != NULL);
240         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
241
242         /*
243          * Read the value of the FIFO control register so that the threshold
244          * can be retrieved, this read takes special register processing
245          */
246         RtrigRegister = (u8) XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
247                                                    XUARTPS_RXWM_OFFSET);
248
249         /* Return only the trigger level from the register value */
250
251         RtrigRegister &= (u8)XUARTPS_RXWM_MASK;
252         return RtrigRegister;
253 }
254
255 /****************************************************************************/
256 /**
257 *
258 * This functions sets the receive FIFO trigger level. The receive trigger
259 * level specifies the number of bytes in the receive FIFO that cause a receive
260 * data event (interrupt) to be generated.
261 *
262 * @param        InstancePtr is a pointer to the XUartPs instance.
263 * @param        TriggerLevel contains the trigger level to set.
264 *
265 * @return       None
266 *
267 * @note         None.
268 *
269 *****************************************************************************/
270 void XUartPs_SetFifoThreshold(XUartPs *InstancePtr, u8 TriggerLevel)
271 {
272         u32 RtrigRegister;
273
274         /* Assert validates the input arguments */
275         Xil_AssertVoid(InstancePtr != NULL);
276         Xil_AssertVoid(TriggerLevel <= (u8)XUARTPS_RXWM_MASK);
277         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
278
279         RtrigRegister = ((u32)TriggerLevel) & (u32)XUARTPS_RXWM_MASK;
280
281         /*
282          * Write the new value for the FIFO control register to it such that the
283          * threshold is changed
284          */
285         XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
286                            XUARTPS_RXWM_OFFSET, RtrigRegister);
287
288 }
289
290 /****************************************************************************/
291 /**
292 *
293 * This function gets the modem status from the specified UART. The modem
294 * status indicates any changes of the modem signals. This function allows
295 * the modem status to be read in a polled mode. The modem status is updated
296 * whenever it is read such that reading it twice may not yield the same
297 * results.
298 *
299 * @param        InstancePtr is a pointer to the XUartPs instance.
300 *
301 * @return
302 *
303 * The modem status which are bit masks that are contained in the file
304 * xuartps.h and named XUARTPS_MODEM_*.
305 *
306 * @note
307 *
308 * The bit masks used for the modem status are the exact bits of the modem
309 * status register with no abstraction.
310 *
311 *****************************************************************************/
312 u16 XUartPs_GetModemStatus(XUartPs *InstancePtr)
313 {
314         u32 ModemStatusRegister;
315         u16 TmpRegister;
316         /* Assert validates the input arguments */
317         Xil_AssertNonvoid(InstancePtr != NULL);
318         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
319
320         /* Read the modem status register to return
321          */
322         ModemStatusRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
323                                                 XUARTPS_MODEMSR_OFFSET);
324         TmpRegister = (u16)ModemStatusRegister;
325         return TmpRegister;
326 }
327
328 /****************************************************************************/
329 /**
330 *
331 * This function determines if the specified UART is sending data.
332 *
333 * @param        InstancePtr is a pointer to the XUartPs instance.
334 *
335 * @return
336 *               - TRUE if the UART is sending data
337 *               - FALSE if UART is not sending data
338 *
339 * @note         None.
340 *
341 *****************************************************************************/
342 u32 XUartPs_IsSending(XUartPs *InstancePtr)
343 {
344         u32 ChanStatRegister;
345         u32 ChanTmpSRegister;
346         u32 ActiveResult;
347         u32 EmptyResult;
348
349         /* Assert validates the input arguments */
350         Xil_AssertNonvoid(InstancePtr != NULL);
351         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
352
353         /*
354          * Read the channel status register to determine if the transmitter is
355          * active
356          */
357         ChanStatRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
358                                                  XUARTPS_SR_OFFSET);
359
360         /*
361          * If the transmitter is active, or the TX FIFO is not empty, then indicate
362          * that the UART is still sending some data
363          */
364         ActiveResult = ChanStatRegister & ((u32)XUARTPS_SR_TACTIVE);
365         EmptyResult = ChanStatRegister & ((u32)XUARTPS_SR_TXEMPTY);
366         ChanTmpSRegister = (((u32)XUARTPS_SR_TACTIVE) == ActiveResult) ||
367                 (((u32)XUARTPS_SR_TXEMPTY) != EmptyResult);
368
369         return ChanTmpSRegister;
370 }
371
372 /****************************************************************************/
373 /**
374 *
375 * This function gets the operational mode of the UART. The UART can operate
376 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
377 * echo.
378 *
379 * @param        InstancePtr is a pointer to the XUartPs instance.
380 *
381 * @return
382 *
383 * The operational mode is specified by constants defined in xuartps.h. The
384 * constants are named XUARTPS_OPER_MODE_*
385 *
386 * @note         None.
387 *
388 *****************************************************************************/
389 u8 XUartPs_GetOperMode(XUartPs *InstancePtr)
390 {
391         u32 ModeRegister;
392         u8 OperMode;
393
394         /* Assert validates the input arguments */
395         Xil_AssertNonvoid(InstancePtr != NULL);
396         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
397
398         /* Read the Mode register. */
399         ModeRegister =
400                 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
401                                   XUARTPS_MR_OFFSET);
402
403         ModeRegister &= (u32)XUARTPS_MR_CHMODE_MASK;
404         /* Return the constant */
405         switch (ModeRegister) {
406         case XUARTPS_MR_CHMODE_NORM:
407                 OperMode = XUARTPS_OPER_MODE_NORMAL;
408                 break;
409         case XUARTPS_MR_CHMODE_ECHO:
410                 OperMode = XUARTPS_OPER_MODE_AUTO_ECHO;
411                 break;
412         case XUARTPS_MR_CHMODE_L_LOOP:
413                 OperMode = XUARTPS_OPER_MODE_LOCAL_LOOP;
414                 break;
415         case XUARTPS_MR_CHMODE_R_LOOP:
416                 OperMode = XUARTPS_OPER_MODE_REMOTE_LOOP;
417                 break;
418         default:
419                 OperMode = (u8) ((ModeRegister & (u32)XUARTPS_MR_CHMODE_MASK) >>
420                         XUARTPS_MR_CHMODE_SHIFT);
421                 break;
422         }
423
424         return OperMode;
425 }
426
427 /****************************************************************************/
428 /**
429 *
430 * This function sets the operational mode of the UART. The UART can operate
431 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
432 * echo.
433 *
434 * @param        InstancePtr is a pointer to the XUartPs instance.
435 * @param        OperationMode is the mode of the UART.
436 *
437 * @return       None.
438 *
439 * @note         None.
440 *
441 *****************************************************************************/
442 void XUartPs_SetOperMode(XUartPs *InstancePtr, u8 OperationMode)
443 {
444         u32 ModeRegister;
445
446         /* Assert validates the input arguments. */
447         Xil_AssertVoid(InstancePtr != NULL);
448         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
449         Xil_AssertVoid(OperationMode <= XUARTPS_OPER_MODE_REMOTE_LOOP);
450
451         /* Read the Mode register. */
452         ModeRegister =
453                 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
454                                   XUARTPS_MR_OFFSET);
455
456         /* Set the correct value by masking the bits, then ORing the const. */
457         ModeRegister &= (u32)(~XUARTPS_MR_CHMODE_MASK);
458
459         switch (OperationMode) {
460                 case XUARTPS_OPER_MODE_NORMAL:
461                         ModeRegister |= (u32)XUARTPS_MR_CHMODE_NORM;
462                         break;
463                 case XUARTPS_OPER_MODE_AUTO_ECHO:
464                         ModeRegister |= (u32)XUARTPS_MR_CHMODE_ECHO;
465                         break;
466                 case XUARTPS_OPER_MODE_LOCAL_LOOP:
467                         ModeRegister |= (u32)XUARTPS_MR_CHMODE_L_LOOP;
468                         break;
469                 case XUARTPS_OPER_MODE_REMOTE_LOOP:
470                         ModeRegister |= (u32)XUARTPS_MR_CHMODE_R_LOOP;
471                         break;
472                 default:
473                         /* Default case made for MISRA-C Compliance. */
474                         break;
475         }
476
477         XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
478                            ModeRegister);
479
480 }
481
482 /****************************************************************************/
483 /**
484 *
485 * This function sets the Flow Delay.
486 * 0 - 3: Flow delay inactive
487 * 4 - 32: If Flow Control mode is enabled, UART_rtsN is deactivated when the
488 * receive FIFO fills to this level.
489 *
490 * @param        InstancePtr is a pointer to the XUartPs instance.
491 *
492 * @return
493 *
494 * The Flow Delay is specified by constants defined in xuartps_hw.h. The
495 * constants are named XUARTPS_FLOWDEL*
496 *
497 * @note         None.
498 *
499 *****************************************************************************/
500 u8 XUartPs_GetFlowDelay(XUartPs *InstancePtr)
501 {
502         u32 FdelTmpRegister;
503
504         /* Assert validates the input arguments */
505         Xil_AssertNonvoid(InstancePtr != NULL);
506         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
507
508         /* Read the Mode register. */
509         FdelTmpRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
510                                          XUARTPS_FLOWDEL_OFFSET);
511
512         /* Return the contents of the flow delay register */
513         FdelTmpRegister = (u8)(FdelTmpRegister & (u32)XUARTPS_FLOWDEL_MASK);
514         return  FdelTmpRegister;
515 }
516
517 /****************************************************************************/
518 /**
519 *
520 * This function sets the Flow Delay.
521 * 0 - 3: Flow delay inactive
522 * 4 - 63: If Flow Control mode is enabled, UART_rtsN is deactivated when the
523 * receive FIFO fills to this level.
524 *
525 * @param        InstancePtr is a pointer to the XUartPs instance.
526 * @param        FlowDelayValue is the Setting for the flow delay.
527 *
528 * @return       None.
529 *
530 * @note         None.
531 *
532 *****************************************************************************/
533 void XUartPs_SetFlowDelay(XUartPs *InstancePtr, u8 FlowDelayValue)
534 {
535         u32 FdelRegister;
536
537         /* Assert validates the input arguments */
538         Xil_AssertVoid(InstancePtr != NULL);
539         Xil_AssertVoid(FlowDelayValue > (u8)XUARTPS_FLOWDEL_MASK);
540         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
541
542         /*
543          * Set the correct value by shifting the input constant, then masking
544          * the bits
545          */
546         FdelRegister = ((u32)FlowDelayValue) & (u32)XUARTPS_FLOWDEL_MASK;
547
548         XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
549                            XUARTPS_FLOWDEL_OFFSET, FdelRegister);
550
551 }
552
553 /****************************************************************************/
554 /**
555 *
556 * This function gets the Receive Timeout of the UART.
557 *
558 * @param        InstancePtr is a pointer to the XUartPs instance.
559 *
560 * @return       The current setting for receive time out.
561 *
562 * @note         None.
563 *
564 *****************************************************************************/
565 u8 XUartPs_GetRecvTimeout(XUartPs *InstancePtr)
566 {
567         u32 RtoRegister;
568         u8 RtoRTmpRegister;
569
570         /* Assert validates the input arguments */
571         Xil_AssertNonvoid(InstancePtr != NULL);
572         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
573
574         /* Read the Receive Timeout register. */
575         RtoRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
576                                         XUARTPS_RXTOUT_OFFSET);
577
578         /* Return the contents of the mode register shifted appropriately */
579         RtoRTmpRegister = (u8)(RtoRegister & (u32)XUARTPS_RXTOUT_MASK);
580         return RtoRTmpRegister;
581 }
582
583 /****************************************************************************/
584 /**
585 *
586 * This function sets the Receive Timeout of the UART.
587 *
588 * @param        InstancePtr is a pointer to the XUartPs instance.
589 * @param        RecvTimeout setting allows the UART to detect an idle connection
590 *               on the reciever data line.
591 *               Timeout duration = RecvTimeout x 4 x Bit Period. 0 disables the
592 *               timeout function.
593 *
594 * @return       None.
595 *
596 * @note         None.
597 *
598 *****************************************************************************/
599 void XUartPs_SetRecvTimeout(XUartPs *InstancePtr, u8 RecvTimeout)
600 {
601         u32 RtoRegister;
602
603         /* Assert validates the input arguments */
604         Xil_AssertVoid(InstancePtr != NULL);
605         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
606
607         /* Set the correct value by masking the bits */
608         RtoRegister = ((u32)RecvTimeout & (u32)XUARTPS_RXTOUT_MASK);
609
610         XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
611                            XUARTPS_RXTOUT_OFFSET, RtoRegister);
612
613         /* Configure CR to restart the receiver timeout counter */
614         RtoRegister =
615                 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
616                                   XUARTPS_CR_OFFSET);
617         XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_CR_OFFSET,
618                            (RtoRegister | XUARTPS_CR_TORST));
619
620 }
621 /****************************************************************************/
622 /**
623 *
624 * Sets the data format for the device. The data format includes the
625 * baud rate, number of data bits, number of stop bits, and parity. It is the
626 * caller's responsibility to ensure that the UART is not sending or receiving
627 * data when this function is called.
628 *
629 * @param        InstancePtr is a pointer to the XUartPs instance.
630 * @param        FormatPtr is a pointer to a format structure containing the data
631 *               format to be set.
632 *
633 * @return
634 *               - XST_SUCCESS if the data format was successfully set.
635 *               - XST_UART_BAUD_ERROR indicates the baud rate could not be
636 *               set because of the amount of error with the baud rate and
637 *               the input clock frequency.
638 *               - XST_INVALID_PARAM if one of the parameters was not valid.
639 *
640 * @note
641 *
642 * The data types in the format type, data bits and parity, are 32 bit fields
643 * to prevent a compiler warning.
644 * The asserts in this function will cause a warning if these fields are
645 * bytes.
646 * <br><br>
647 *
648 *****************************************************************************/
649 s32 XUartPs_SetDataFormat(XUartPs *InstancePtr,
650                         XUartPsFormat * FormatPtr)
651 {
652         s32 Status;
653         u32 ModeRegister;
654
655         Xil_AssertNonvoid(InstancePtr != NULL);
656         Xil_AssertNonvoid(FormatPtr != NULL);
657         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
658
659         /* Verify the inputs specified are valid */
660         if ((FormatPtr->DataBits > ((u32)XUARTPS_FORMAT_6_BITS)) ||
661                 (FormatPtr->StopBits > ((u8)XUARTPS_FORMAT_2_STOP_BIT)) ||
662                 (FormatPtr->Parity > ((u32)XUARTPS_FORMAT_NO_PARITY))) {
663                 Status = XST_INVALID_PARAM;
664         } else {
665
666                 /*
667                  * Try to set the baud rate and if it's not successful then don't
668                  * continue altering the data format, this is done first to avoid the
669                  * format from being altered when an error occurs
670                  */
671                 Status = XUartPs_SetBaudRate(InstancePtr, FormatPtr->BaudRate);
672                 if (Status != (s32)XST_SUCCESS) {
673                         ;
674                 } else {
675
676                         ModeRegister =
677                                 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
678                                                   XUARTPS_MR_OFFSET);
679
680                         /*
681                          * Set the length of data (8,7,6) by first clearing out the bits
682                          * that control it in the register, then set the length in the register
683                          */
684                         ModeRegister &= (u32)(~XUARTPS_MR_CHARLEN_MASK);
685                         ModeRegister |= (FormatPtr->DataBits << XUARTPS_MR_CHARLEN_SHIFT);
686
687                         /*
688                          * Set the number of stop bits in the mode register by first clearing
689                          * out the bits that control it in the register, then set the number
690                          * of stop bits in the register.
691                          */
692                         ModeRegister &= (u32)(~XUARTPS_MR_STOPMODE_MASK);
693                         ModeRegister |= (((u32)FormatPtr->StopBits) << XUARTPS_MR_STOPMODE_SHIFT);
694
695                         /*
696                          * Set the parity by first clearing out the bits that control it in the
697                          * register, then set the bits in the register, the default is no parity
698                          * after clearing the register bits
699                          */
700                         ModeRegister &= (u32)(~XUARTPS_MR_PARITY_MASK);
701                         ModeRegister |= (FormatPtr->Parity << XUARTPS_MR_PARITY_SHIFT);
702
703                         /* Update the mode register */
704                         XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
705                                            ModeRegister);
706
707                         Status = XST_SUCCESS;
708                 }
709         }
710         return Status;
711 }
712
713 /****************************************************************************/
714 /**
715 *
716 * Gets the data format for the specified UART. The data format includes the
717 * baud rate, number of data bits, number of stop bits, and parity.
718 *
719 * @param        InstancePtr is a pointer to the XUartPs instance.
720 * @param        FormatPtr is a pointer to a format structure that will contain
721 *               the data format after this call completes.
722 *
723 * @return       None.
724 *
725 * @note         None.
726 *
727 *
728 *****************************************************************************/
729 void XUartPs_GetDataFormat(XUartPs *InstancePtr, XUartPsFormat * FormatPtr)
730 {
731         u32 ModeRegister;
732
733
734         /* Assert validates the input arguments */
735         Xil_AssertVoid(InstancePtr != NULL);
736         Xil_AssertVoid(FormatPtr != NULL);
737         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
738
739         /*
740          * Get the baud rate from the instance, this is not retrieved from the
741          * hardware because it is only kept as a divisor such that it is more
742          * difficult to get back to the baud rate
743          */
744         FormatPtr->BaudRate = InstancePtr->BaudRate;
745
746         ModeRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
747                                   XUARTPS_MR_OFFSET);
748
749         /* Get the length of data (8,7,6,5) */
750         FormatPtr->DataBits =
751                 ((ModeRegister & (u32)XUARTPS_MR_CHARLEN_MASK) >>
752                 XUARTPS_MR_CHARLEN_SHIFT);
753
754         /* Get the number of stop bits */
755         FormatPtr->StopBits =
756                 (u8)((ModeRegister & (u32)XUARTPS_MR_STOPMODE_MASK) >>
757                 XUARTPS_MR_STOPMODE_SHIFT);
758
759         /* Determine what parity is */
760         FormatPtr->Parity =
761                 (u32)((ModeRegister & (u32)XUARTPS_MR_PARITY_MASK) >>
762                 XUARTPS_MR_PARITY_SHIFT);
763 }
764 /** @} */