]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5_bsp/psu_cortexr5_0/libsrc/uartps_v3_1/src/xuartps.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.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.c
36 * @addtogroup uartps_v3_1
37 * @{
38 *
39 * This file contains the implementation of the interface functions for XUartPs
40 * driver. Refer to the header file xuartps.h for more detailed information.
41 *
42 * <pre>
43 * MODIFICATION HISTORY:
44 *
45 * Ver   Who    Date      Changes
46 * ----- ------ -------- ----------------------------------------------
47 * 1.00  drg/jz 01/13/10 First Release
48 * 2.2   hk     06/23/14 SW reset of RX and TX should be done when changing
49 *                       baud rate. CR# 804281.
50 * 3.00  kvn    02/13/15 Modified code for MISRA-C:2012 compliance.
51 * 3.1   kvn    04/10/15 Modified code for latest RTL changes.
52 * </pre>
53 *
54 *****************************************************************************/
55
56 /***************************** Include Files ********************************/
57
58 #include "xstatus.h"
59 #include "xuartps.h"
60 #include "xil_io.h"
61
62 /************************** Constant Definitions ****************************/
63
64 /* The following constant defines the amount of error that is allowed for
65  * a specified baud rate. This error is the difference between the actual
66  * baud rate that will be generated using the specified clock and the
67  * desired baud rate.
68  */
69 #define XUARTPS_MAX_BAUD_ERROR_RATE              3U     /* max % error allowed */
70
71 /**************************** Type Definitions ******************************/
72
73
74 /***************** Macros (Inline Functions) Definitions ********************/
75
76
77 /************************** Function Prototypes *****************************/
78
79 static void XUartPs_StubHandler(void *CallBackRef, u32 Event,
80                                  u32 ByteCount);
81
82 u32  XUartPs_SendBuffer(XUartPs *InstancePtr);
83
84 u32  XUartPs_ReceiveBuffer(XUartPs *InstancePtr);
85
86 /************************** Variable Definitions ****************************/
87
88 /****************************************************************************/
89 /**
90 *
91 * Initializes a specific XUartPs instance such that it is ready to be used.
92 * The data format of the device is setup for 8 data bits, 1 stop bit, and no
93 * parity by default. The baud rate is set to a default value specified by
94 * Config->DefaultBaudRate if set, otherwise it is set to 19.2K baud. The
95 * receive FIFO threshold is set for 8 bytes. The default operating mode of the
96 * driver is polled mode.
97 *
98 * @param        InstancePtr is a pointer to the XUartPs instance.
99 * @param        Config is a reference to a structure containing information
100 *               about a specific XUartPs driver.
101 * @param        EffectiveAddr is the device base address in the virtual memory
102 *               address space. The caller is responsible for keeping the address
103 *               mapping from EffectiveAddr to the device physical base address
104 *               unchanged once this function is invoked. Unexpected errors may
105 *               occur if the address mapping changes after this function is
106 *               called. If address translation is not used, pass in the physical
107 *               address instead.
108 *
109 * @return
110 *
111 *               - XST_SUCCESS if initialization was successful
112 *               - XST_UART_BAUD_ERROR if the baud rate is not possible because
113 *                 the inputclock frequency is not divisible with an acceptable
114 *                 amount of error
115 *
116 * @note
117 *
118 * The default configuration for the UART after initialization is:
119 *
120 * - 19,200 bps or XPAR_DFT_BAUDRATE if defined
121 * - 8 data bits
122 * - 1 stop bit
123 * - no parity
124 * - FIFO's are enabled with a receive threshold of 8 bytes
125 * - The RX timeout is enabled with a timeout of 1 (4 char times)
126 *
127 *   All interrupts are disabled.
128 *
129 *****************************************************************************/
130 s32 XUartPs_CfgInitialize(XUartPs *InstancePtr,
131                                    XUartPs_Config * Config, u32 EffectiveAddr)
132 {
133         s32 Status;
134         u32 ModeRegister;
135         u32 BaudRate;
136
137         /* Assert validates the input arguments */
138         Xil_AssertNonvoid(InstancePtr != NULL);
139         Xil_AssertNonvoid(Config != NULL);
140
141         /* Setup the driver instance using passed in parameters */
142         InstancePtr->Config.BaseAddress = EffectiveAddr;
143         InstancePtr->Config.InputClockHz = Config->InputClockHz;
144         InstancePtr->Config.ModemPinsConnected = Config->ModemPinsConnected;
145
146         /* Initialize other instance data to default values */
147         InstancePtr->Handler = XUartPs_StubHandler;
148
149         InstancePtr->SendBuffer.NextBytePtr = NULL;
150         InstancePtr->SendBuffer.RemainingBytes = 0U;
151         InstancePtr->SendBuffer.RequestedBytes = 0U;
152
153         InstancePtr->ReceiveBuffer.NextBytePtr = NULL;
154         InstancePtr->ReceiveBuffer.RemainingBytes = 0U;
155         InstancePtr->ReceiveBuffer.RequestedBytes = 0U;
156
157         /* Initialize the platform data */
158         InstancePtr->Platform = XGetPlatform_Info();
159
160         InstancePtr->is_rxbs_error = 0U;
161
162         /* Flag that the driver instance is ready to use */
163         InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
164
165         /*
166          * Set the default baud rate here, can be changed prior to
167          * starting the device
168          */
169         BaudRate = (u32)XUARTPS_DFT_BAUDRATE;
170         Status = XUartPs_SetBaudRate(InstancePtr, BaudRate);
171         if (Status != (s32)XST_SUCCESS) {
172                 InstancePtr->IsReady = 0U;
173         } else {
174
175                 /*
176                  * Set up the default data format: 8 bit data, 1 stop bit, no
177                  * parity
178                  */
179                 ModeRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
180                                           XUARTPS_MR_OFFSET);
181
182                 /* Mask off what's already there */
183                 ModeRegister &= (~((u32)XUARTPS_MR_CHARLEN_MASK |
184                                                  (u32)XUARTPS_MR_STOPMODE_MASK |
185                                                  (u32)XUARTPS_MR_PARITY_MASK));
186
187                 /* Set the register value to the desired data format */
188                 ModeRegister |= ((u32)XUARTPS_MR_CHARLEN_8_BIT |
189                                                  (u32)XUARTPS_MR_STOPMODE_1_BIT |
190                                                  (u32)XUARTPS_MR_PARITY_NONE);
191
192                 /* Write the mode register out */
193                 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
194                                    ModeRegister);
195
196                 /* Set the RX FIFO trigger at 8 data bytes. */
197                 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
198                                    XUARTPS_RXWM_OFFSET, 0x08U);
199
200                 /* Set the RX timeout to 1, which will be 4 character time */
201                 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
202                                    XUARTPS_RXTOUT_OFFSET, 0x01U);
203
204                 /* Disable all interrupts, polled mode is the default */
205                 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IDR_OFFSET,
206                                    XUARTPS_IXR_MASK);
207
208                 Status = XST_SUCCESS;
209         }
210         return Status;
211 }
212
213 /****************************************************************************/
214 /**
215 *
216 * This functions sends the specified buffer using the device in either
217 * polled or interrupt driven mode. This function is non-blocking, if the device
218 * is busy sending data, it will return and indicate zero bytes were sent.
219 * Otherwise, it fills the TX FIFO as much as it can, and return the number of
220 * bytes sent.
221 *
222 * In a polled mode, this function will only send as much data as TX FIFO can
223 * buffer. The application may need to call it repeatedly to send the entire
224 * buffer.
225 *
226 * In interrupt mode, this function will start sending the specified buffer,
227 * then the interrupt handler will continue sending data until the entire
228 * buffer has been sent. A callback function, as specified by the application,
229 * will be called to indicate the completion of sending.
230 *
231 * @param        InstancePtr is a pointer to the XUartPs instance.
232 * @param        BufferPtr is pointer to a buffer of data to be sent.
233 * @param        NumBytes contains the number of bytes to be sent. A value of
234 *               zero will stop a previous send operation that is in progress
235 *               in interrupt mode. Any data that was already put into the
236 *               transmit FIFO will be sent.
237 *
238 * @return       The number of bytes actually sent.
239 *
240 * @note
241 *
242 * The number of bytes is not asserted so that this function may be called with
243 * a value of zero to stop an operation that is already in progress.
244 * <br><br>
245 *
246 *****************************************************************************/
247 u32 XUartPs_Send(XUartPs *InstancePtr, u8 *BufferPtr,
248                            u32 NumBytes)
249 {
250         u32 BytesSent;
251
252         /* Asserts validate the input arguments */
253         Xil_AssertNonvoid(InstancePtr != NULL);
254         Xil_AssertNonvoid(BufferPtr != NULL);
255         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
256
257         /*
258          * Disable the UART transmit interrupts to allow this call to stop a
259          * previous operation that may be interrupt driven.
260          */
261         XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IDR_OFFSET,
262                                           (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL));
263
264         /* Setup the buffer parameters */
265         InstancePtr->SendBuffer.RequestedBytes = NumBytes;
266         InstancePtr->SendBuffer.RemainingBytes = NumBytes;
267         InstancePtr->SendBuffer.NextBytePtr = BufferPtr;
268
269         /*
270          * Transmit interrupts will be enabled in XUartPs_SendBuffer(), after
271          * filling the TX FIFO.
272          */
273         BytesSent = XUartPs_SendBuffer(InstancePtr);
274
275         return BytesSent;
276 }
277
278 /****************************************************************************/
279 /**
280 *
281 * This function attempts to receive a specified number of bytes of data
282 * from the device and store it into the specified buffer. This function works
283 * for both polled or interrupt driven modes. It is non-blocking.
284 *
285 * In a polled mode, this function will only receive the data already in the
286 * RX FIFO. The application may need to call it repeatedly to receive the
287 * entire buffer. Polled mode is the default mode of operation for the device.
288 *
289 * In interrupt mode, this function will start the receiving, if not the entire
290 * buffer has been received, the interrupt handler will continue receiving data
291 * until the entire buffer has been received. A callback function, as specified
292 * by the application, will be called to indicate the completion of the
293 * receiving or error conditions.
294 *
295 * @param        InstancePtr is a pointer to the XUartPs instance
296 * @param        BufferPtr is pointer to buffer for data to be received into
297 * @param        NumBytes is the number of bytes to be received. A value of zero
298 *               will stop a previous receive operation that is in progress in
299 *               interrupt mode.
300 *
301 * @return       The number of bytes received.
302 *
303 * @note
304 *
305 * The number of bytes is not asserted so that this function may be called
306 * with a value of zero to stop an operation that is already in progress.
307 *
308 *****************************************************************************/
309 u32 XUartPs_Recv(XUartPs *InstancePtr,
310                           u8 *BufferPtr, u32 NumBytes)
311 {
312         u32 ReceivedCount;
313         u32 ImrRegister;
314
315         /* Assert validates the input arguments */
316         Xil_AssertNonvoid(InstancePtr != NULL);
317         Xil_AssertNonvoid(BufferPtr != NULL);
318         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
319
320         /*
321          * Disable all the interrupts.
322          * This stops a previous operation that may be interrupt driven
323          */
324         ImrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
325                                   XUARTPS_IMR_OFFSET);
326         XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IDR_OFFSET,
327                 XUARTPS_IXR_MASK);
328
329         /* Setup the buffer parameters */
330         InstancePtr->ReceiveBuffer.RequestedBytes = NumBytes;
331         InstancePtr->ReceiveBuffer.RemainingBytes = NumBytes;
332         InstancePtr->ReceiveBuffer.NextBytePtr = BufferPtr;
333
334         /* Receive the data from the device */
335         ReceivedCount = XUartPs_ReceiveBuffer(InstancePtr);
336
337         /* Restore the interrupt state */
338         XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IER_OFFSET,
339                 ImrRegister);
340
341         return ReceivedCount;
342 }
343
344 /****************************************************************************/
345 /*
346 *
347 * This function sends a buffer that has been previously specified by setting
348 * up the instance variables of the instance. This function is an internal
349 * function for the XUartPs driver such that it may be called from a shell
350 * function that sets up the buffer or from an interrupt handler.
351 *
352 * This function sends the specified buffer in either polled or interrupt
353 * driven modes. This function is non-blocking.
354 *
355 * In a polled mode, this function only sends as much data as the TX FIFO
356 * can buffer. The application may need to call it repeatedly to send the
357 * entire buffer.
358 *
359 * In interrupt mode, this function starts the sending of the buffer, if not
360 * the entire buffer has been sent, then the interrupt handler continues the
361 * sending until the entire buffer has been sent. A callback function, as
362 * specified by the application, will be called to indicate the completion of
363 * sending.
364 *
365 * @param        InstancePtr is a pointer to the XUartPs instance
366 *
367 * @return       The number of bytes actually sent
368 *
369 * @note         None.
370 *
371 *****************************************************************************/
372 u32 XUartPs_SendBuffer(XUartPs *InstancePtr)
373 {
374         u32 SentCount = 0U;
375         u32 ImrRegister;
376
377         /*
378          * If the TX FIFO is full, send nothing.
379          * Otherwise put bytes into the TX FIFO unil it is full, or all of the
380          * data has been put into the FIFO.
381          */
382         while ((!XUartPs_IsTransmitFull(InstancePtr->Config.BaseAddress)) &&
383                    (InstancePtr->SendBuffer.RemainingBytes > SentCount)) {
384
385                 /* Fill the FIFO from the buffer */
386                 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
387                                    XUARTPS_FIFO_OFFSET,
388                                    ((u32)InstancePtr->SendBuffer.
389                                    NextBytePtr[SentCount]));
390
391                 /* Increment the send count. */
392                 SentCount++;
393         }
394
395         /* Update the buffer to reflect the bytes that were sent from it */
396         InstancePtr->SendBuffer.NextBytePtr += SentCount;
397         InstancePtr->SendBuffer.RemainingBytes -= SentCount;
398
399         /*
400          * If interrupts are enabled as indicated by the receive interrupt, then
401          * enable the TX FIFO empty interrupt, so further action can be taken
402          * for this sending.
403          */
404         ImrRegister =
405                 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
406                                   XUARTPS_IMR_OFFSET);
407         if (((ImrRegister & XUARTPS_IXR_RXFULL) != (u32)0) ||
408                 ((ImrRegister & XUARTPS_IXR_RXEMPTY) != (u32)0)||
409                 ((ImrRegister & XUARTPS_IXR_RXOVR) != (u32)0)) {
410
411                 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
412                                            XUARTPS_IER_OFFSET,
413                                            ImrRegister | (u32)XUARTPS_IXR_TXEMPTY);
414         }
415
416         return SentCount;
417 }
418
419 /****************************************************************************/
420 /*
421 *
422 * This function receives a buffer that has been previously specified by setting
423 * up the instance variables of the instance. This function is an internal
424 * function, and it may be called from a shell function that sets up the buffer
425 * or from an interrupt handler.
426 *
427 * This function attempts to receive a specified number of bytes from the
428 * device and store it into the specified buffer. This function works for
429 * either polled or interrupt driven modes. It is non-blocking.
430 *
431 * In polled mode, this function only receives as much data as in the RX FIFO.
432 * The application may need to call it repeatedly to receive the entire buffer.
433 * Polled mode is the default mode for the driver.
434 *
435 * In interrupt mode, this function starts the receiving, if not the entire
436 * buffer has been received, the interrupt handler will continue until the
437 * entire buffer has been received. A callback function, as specified by the
438 * application, will be called to indicate the completion of the receiving or
439 * error conditions.
440 *
441 * @param        InstancePtr is a pointer to the XUartPs instance
442 *
443 * @return       The number of bytes received.
444 *
445 * @note         None.
446 *
447 *****************************************************************************/
448 u32 XUartPs_ReceiveBuffer(XUartPs *InstancePtr)
449 {
450         u32 CsrRegister;
451         u32 ReceivedCount = 0U;
452         u32 ByteStatusValue, EventData;
453         u32 Event;
454
455         /*
456          * Read the Channel Status Register to determine if there is any data in
457          * the RX FIFO
458          */
459         CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
460                                 XUARTPS_SR_OFFSET);
461
462         /*
463          * Loop until there is no more data in RX FIFO or the specified
464          * number of bytes has been received
465          */
466         while((ReceivedCount <= InstancePtr->ReceiveBuffer.RemainingBytes)&&
467                 (((CsrRegister & XUARTPS_SR_RXEMPTY) == (u32)0))){
468
469                 if (InstancePtr->is_rxbs_error) {
470                         ByteStatusValue = XUartPs_ReadReg(
471                                                 InstancePtr->Config.BaseAddress,
472                                                 XUARTPS_RXBS_OFFSET);
473                         if((ByteStatusValue & XUARTPS_RXBS_MASK)!= (u32)0) {
474                                 EventData = ByteStatusValue;
475                                 Event = XUARTPS_EVENT_PARE_FRAME_BRKE;
476                                 /*
477                                  * Call the application handler to indicate that there is a receive
478                                  * error or a break interrupt, if the application cares about the
479                                  * error it call a function to get the last errors.
480                                  */
481                                 InstancePtr->Handler(InstancePtr->CallBackRef,
482                                                         Event, EventData);
483                         }
484                 }
485
486                 InstancePtr->ReceiveBuffer.NextBytePtr[ReceivedCount] =
487                         XUartPs_ReadReg(InstancePtr->Config.
488                                   BaseAddress,
489                                   XUARTPS_FIFO_OFFSET);
490
491                 ReceivedCount++;
492
493                 CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
494                                                                 XUARTPS_SR_OFFSET);
495         }
496         InstancePtr->is_rxbs_error = 0;
497         /*
498          * Update the receive buffer to reflect the number of bytes just
499          * received
500          */
501         if(InstancePtr->ReceiveBuffer.NextBytePtr != NULL){
502                 InstancePtr->ReceiveBuffer.NextBytePtr += ReceivedCount;
503         }
504         InstancePtr->ReceiveBuffer.RemainingBytes -= ReceivedCount;
505
506         return ReceivedCount;
507 }
508
509 /*****************************************************************************/
510 /**
511 *
512 * Sets the baud rate for the device. Checks the input value for
513 * validity and also verifies that the requested rate can be configured to
514 * within the maximum error range specified by XUARTPS_MAX_BAUD_ERROR_RATE.
515 * If the provided rate is not possible, the current setting is unchanged.
516 *
517 * @param        InstancePtr is a pointer to the XUartPs instance
518 * @param        BaudRate to be set
519 *
520 * @return
521 *               - XST_SUCCESS if everything configured as expected
522 *               - XST_UART_BAUD_ERROR if the requested rate is not available
523 *                 because there was too much error
524 *
525 * @note         None.
526 *
527 *****************************************************************************/
528 s32 XUartPs_SetBaudRate(XUartPs *InstancePtr, u32 BaudRate)
529 {
530         u32 IterBAUDDIV;        /* Iterator for available baud divisor values */
531         u32 BRGR_Value;         /* Calculated value for baud rate generator */
532         u32 CalcBaudRate;       /* Calculated baud rate */
533         u32 BaudError;          /* Diff between calculated and requested baud rate */
534         u32 Best_BRGR = 0U;     /* Best value for baud rate generator */
535         u8 Best_BAUDDIV = 0U;   /* Best value for baud divisor */
536         u32 Best_Error = 0xFFFFFFFFU;
537         u32 PercentError;
538         u32 ModeReg;
539         u32 InputClk;
540
541         /* Asserts validate the input arguments */
542         Xil_AssertNonvoid(InstancePtr != NULL);
543         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
544         Xil_AssertNonvoid(BaudRate <= (u32)XUARTPS_MAX_RATE);
545         Xil_AssertNonvoid(BaudRate >= (u32)XUARTPS_MIN_RATE);
546
547         /*
548          * Make sure the baud rate is not impossilby large.
549          * Fastest possible baud rate is Input Clock / 2.
550          */
551         if ((BaudRate * 2) > InstancePtr->Config.InputClockHz) {
552                 return XST_UART_BAUD_ERROR;
553         }
554         /* Check whether the input clock is divided by 8 */
555         ModeReg = XUartPs_ReadReg( InstancePtr->Config.BaseAddress,
556                                  XUARTPS_MR_OFFSET);
557
558         InputClk = InstancePtr->Config.InputClockHz;
559         if(ModeReg & XUARTPS_MR_CLKSEL) {
560                 InputClk = InstancePtr->Config.InputClockHz / 8;
561         }
562
563         /*
564          * Determine the Baud divider. It can be 4to 254.
565          * Loop through all possible combinations
566          */
567         for (IterBAUDDIV = 4; IterBAUDDIV < 255; IterBAUDDIV++) {
568
569                 /* Calculate the value for BRGR register */
570                 BRGR_Value = InputClk / (BaudRate * (IterBAUDDIV + 1));
571
572                 /* Calculate the baud rate from the BRGR value */
573                 CalcBaudRate = InputClk/ (BRGR_Value * (IterBAUDDIV + 1));
574
575                 /* Avoid unsigned integer underflow */
576                 if (BaudRate > CalcBaudRate) {
577                         BaudError = BaudRate - CalcBaudRate;
578                 }
579                 else {
580                         BaudError = CalcBaudRate - BaudRate;
581                 }
582
583                 /* Find the calculated baud rate closest to requested baud rate. */
584                 if (Best_Error > BaudError) {
585
586                         Best_BRGR = BRGR_Value;
587                         Best_BAUDDIV = IterBAUDDIV;
588                         Best_Error = BaudError;
589                 }
590         }
591
592         /* Make sure the best error is not too large. */
593         PercentError = (Best_Error * 100) / BaudRate;
594         if (XUARTPS_MAX_BAUD_ERROR_RATE < PercentError) {
595                 return XST_UART_BAUD_ERROR;
596         }
597
598         /* Disable TX and RX to avoid glitches when setting the baud rate. */
599         XUartPs_DisableUart(InstancePtr);
600
601         XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
602                            XUARTPS_BAUDGEN_OFFSET, Best_BRGR);
603         XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
604                            XUARTPS_BAUDDIV_OFFSET, Best_BAUDDIV);
605
606         /* RX and TX SW reset */
607         XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_CR_OFFSET,
608                                 XUARTPS_CR_TXRST | XUARTPS_CR_RXRST);
609
610         /* Enable device */
611         XUartPs_EnableUart(InstancePtr);
612
613         InstancePtr->BaudRate = BaudRate;
614
615         return XST_SUCCESS;
616
617 }
618
619 /****************************************************************************/
620 /**
621 *
622 * This function is a stub handler that is the default handler such that if the
623 * application has not set the handler when interrupts are enabled, this
624 * function will be called.
625 *
626 * @param        CallBackRef is unused by this function.
627 * @param        Event is unused by this function.
628 * @param        ByteCount is unused by this function.
629 *
630 * @return       None.
631 *
632 * @note         None.
633 *
634 *****************************************************************************/
635 static void XUartPs_StubHandler(void *CallBackRef, u32 Event,
636                                  u32 ByteCount)
637 {
638         (void *) CallBackRef;
639         (void) Event;
640         (void) ByteCount;
641         /* Assert occurs always since this is a stub and should never be called */
642         Xil_AssertVoidAlways();
643 }
644 /** @} */