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