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