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