1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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
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.
31 ******************************************************************************/
32 /****************************************************************************/
36 * @addtogroup uartps_v3_1
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.
43 * MODIFICATION HISTORY:
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.
54 *****************************************************************************/
56 /***************************** Include Files ********************************/
62 /************************** Constant Definitions ****************************/
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
69 #define XUARTPS_MAX_BAUD_ERROR_RATE 3U /* max % error allowed */
71 /**************************** Type Definitions ******************************/
74 /***************** Macros (Inline Functions) Definitions ********************/
77 /************************** Function Prototypes *****************************/
79 static void XUartPs_StubHandler(void *CallBackRef, u32 Event,
82 u32 XUartPs_SendBuffer(XUartPs *InstancePtr);
84 u32 XUartPs_ReceiveBuffer(XUartPs *InstancePtr);
86 /************************** Variable Definitions ****************************/
88 /****************************************************************************/
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.
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
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
118 * The default configuration for the UART after initialization is:
120 * - 19,200 bps or XPAR_DFT_BAUDRATE if defined
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)
127 * All interrupts are disabled.
129 *****************************************************************************/
130 s32 XUartPs_CfgInitialize(XUartPs *InstancePtr,
131 XUartPs_Config * Config, u32 EffectiveAddr)
137 /* Assert validates the input arguments */
138 Xil_AssertNonvoid(InstancePtr != NULL);
139 Xil_AssertNonvoid(Config != NULL);
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;
146 /* Initialize other instance data to default values */
147 InstancePtr->Handler = XUartPs_StubHandler;
149 InstancePtr->SendBuffer.NextBytePtr = NULL;
150 InstancePtr->SendBuffer.RemainingBytes = 0U;
151 InstancePtr->SendBuffer.RequestedBytes = 0U;
153 InstancePtr->ReceiveBuffer.NextBytePtr = NULL;
154 InstancePtr->ReceiveBuffer.RemainingBytes = 0U;
155 InstancePtr->ReceiveBuffer.RequestedBytes = 0U;
157 /* Initialize the platform data */
158 InstancePtr->Platform = XGetPlatform_Info();
160 InstancePtr->is_rxbs_error = 0U;
162 /* Flag that the driver instance is ready to use */
163 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
166 * Set the default baud rate here, can be changed prior to
167 * starting the device
169 BaudRate = (u32)XUARTPS_DFT_BAUDRATE;
170 Status = XUartPs_SetBaudRate(InstancePtr, BaudRate);
171 if (Status != (s32)XST_SUCCESS) {
172 InstancePtr->IsReady = 0U;
176 * Set up the default data format: 8 bit data, 1 stop bit, no
179 ModeRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
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));
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);
192 /* Write the mode register out */
193 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
196 /* Set the RX FIFO trigger at 8 data bytes. */
197 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
198 XUARTPS_RXWM_OFFSET, 0x08U);
200 /* Set the RX timeout to 1, which will be 4 character time */
201 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
202 XUARTPS_RXTOUT_OFFSET, 0x01U);
204 /* Disable all interrupts, polled mode is the default */
205 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IDR_OFFSET,
208 Status = XST_SUCCESS;
213 /****************************************************************************/
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
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
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.
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.
238 * @return The number of bytes actually sent.
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.
246 *****************************************************************************/
247 u32 XUartPs_Send(XUartPs *InstancePtr, u8 *BufferPtr,
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);
258 * Disable the UART transmit interrupts to allow this call to stop a
259 * previous operation that may be interrupt driven.
261 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IDR_OFFSET,
262 (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL));
264 /* Setup the buffer parameters */
265 InstancePtr->SendBuffer.RequestedBytes = NumBytes;
266 InstancePtr->SendBuffer.RemainingBytes = NumBytes;
267 InstancePtr->SendBuffer.NextBytePtr = BufferPtr;
270 * Transmit interrupts will be enabled in XUartPs_SendBuffer(), after
271 * filling the TX FIFO.
273 BytesSent = XUartPs_SendBuffer(InstancePtr);
278 /****************************************************************************/
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.
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.
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.
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
301 * @return The number of bytes received.
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.
308 *****************************************************************************/
309 u32 XUartPs_Recv(XUartPs *InstancePtr,
310 u8 *BufferPtr, u32 NumBytes)
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);
321 * Disable all the interrupts.
322 * This stops a previous operation that may be interrupt driven
324 ImrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
326 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IDR_OFFSET,
329 /* Setup the buffer parameters */
330 InstancePtr->ReceiveBuffer.RequestedBytes = NumBytes;
331 InstancePtr->ReceiveBuffer.RemainingBytes = NumBytes;
332 InstancePtr->ReceiveBuffer.NextBytePtr = BufferPtr;
334 /* Receive the data from the device */
335 ReceivedCount = XUartPs_ReceiveBuffer(InstancePtr);
337 /* Restore the interrupt state */
338 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IER_OFFSET,
341 return ReceivedCount;
344 /****************************************************************************/
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.
352 * This function sends the specified buffer in either polled or interrupt
353 * driven modes. This function is non-blocking.
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
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
365 * @param InstancePtr is a pointer to the XUartPs instance
367 * @return The number of bytes actually sent
371 *****************************************************************************/
372 u32 XUartPs_SendBuffer(XUartPs *InstancePtr)
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.
382 while ((!XUartPs_IsTransmitFull(InstancePtr->Config.BaseAddress)) &&
383 (InstancePtr->SendBuffer.RemainingBytes > SentCount)) {
385 /* Fill the FIFO from the buffer */
386 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
388 ((u32)InstancePtr->SendBuffer.
389 NextBytePtr[SentCount]));
391 /* Increment the send count. */
395 /* Update the buffer to reflect the bytes that were sent from it */
396 InstancePtr->SendBuffer.NextBytePtr += SentCount;
397 InstancePtr->SendBuffer.RemainingBytes -= SentCount;
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
405 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
407 if (((ImrRegister & XUARTPS_IXR_RXFULL) != (u32)0) ||
408 ((ImrRegister & XUARTPS_IXR_RXEMPTY) != (u32)0)||
409 ((ImrRegister & XUARTPS_IXR_RXOVR) != (u32)0)) {
411 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
413 ImrRegister | (u32)XUARTPS_IXR_TXEMPTY);
419 /****************************************************************************/
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.
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.
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.
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
441 * @param InstancePtr is a pointer to the XUartPs instance
443 * @return The number of bytes received.
447 *****************************************************************************/
448 u32 XUartPs_ReceiveBuffer(XUartPs *InstancePtr)
451 u32 ReceivedCount = 0U;
452 u32 ByteStatusValue, EventData;
456 * Read the Channel Status Register to determine if there is any data in
459 CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
463 * Loop until there is no more data in RX FIFO or the specified
464 * number of bytes has been received
466 while((ReceivedCount <= InstancePtr->ReceiveBuffer.RemainingBytes)&&
467 (((CsrRegister & XUARTPS_SR_RXEMPTY) == (u32)0))){
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;
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.
481 InstancePtr->Handler(InstancePtr->CallBackRef,
486 InstancePtr->ReceiveBuffer.NextBytePtr[ReceivedCount] =
487 XUartPs_ReadReg(InstancePtr->Config.
489 XUARTPS_FIFO_OFFSET);
493 CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
496 InstancePtr->is_rxbs_error = 0;
498 * Update the receive buffer to reflect the number of bytes just
501 if(InstancePtr->ReceiveBuffer.NextBytePtr != NULL){
502 InstancePtr->ReceiveBuffer.NextBytePtr += ReceivedCount;
504 InstancePtr->ReceiveBuffer.RemainingBytes -= ReceivedCount;
506 return ReceivedCount;
509 /*****************************************************************************/
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.
517 * @param InstancePtr is a pointer to the XUartPs instance
518 * @param BaudRate to be set
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
527 *****************************************************************************/
528 s32 XUartPs_SetBaudRate(XUartPs *InstancePtr, u32 BaudRate)
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;
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);
548 * Make sure the baud rate is not impossilby large.
549 * Fastest possible baud rate is Input Clock / 2.
551 if ((BaudRate * 2) > InstancePtr->Config.InputClockHz) {
552 return XST_UART_BAUD_ERROR;
554 /* Check whether the input clock is divided by 8 */
555 ModeReg = XUartPs_ReadReg( InstancePtr->Config.BaseAddress,
558 InputClk = InstancePtr->Config.InputClockHz;
559 if(ModeReg & XUARTPS_MR_CLKSEL) {
560 InputClk = InstancePtr->Config.InputClockHz / 8;
564 * Determine the Baud divider. It can be 4to 254.
565 * Loop through all possible combinations
567 for (IterBAUDDIV = 4; IterBAUDDIV < 255; IterBAUDDIV++) {
569 /* Calculate the value for BRGR register */
570 BRGR_Value = InputClk / (BaudRate * (IterBAUDDIV + 1));
572 /* Calculate the baud rate from the BRGR value */
573 CalcBaudRate = InputClk/ (BRGR_Value * (IterBAUDDIV + 1));
575 /* Avoid unsigned integer underflow */
576 if (BaudRate > CalcBaudRate) {
577 BaudError = BaudRate - CalcBaudRate;
580 BaudError = CalcBaudRate - BaudRate;
583 /* Find the calculated baud rate closest to requested baud rate. */
584 if (Best_Error > BaudError) {
586 Best_BRGR = BRGR_Value;
587 Best_BAUDDIV = IterBAUDDIV;
588 Best_Error = BaudError;
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;
598 /* Disable TX and RX to avoid glitches when setting the baud rate. */
599 XUartPs_DisableUart(InstancePtr);
601 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
602 XUARTPS_BAUDGEN_OFFSET, Best_BRGR);
603 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
604 XUARTPS_BAUDDIV_OFFSET, Best_BAUDDIV);
606 /* RX and TX SW reset */
607 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_CR_OFFSET,
608 XUARTPS_CR_TXRST | XUARTPS_CR_RXRST);
611 XUartPs_EnableUart(InstancePtr);
613 InstancePtr->BaudRate = BaudRate;
619 /****************************************************************************/
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.
626 * @param CallBackRef is unused by this function.
627 * @param Event is unused by this function.
628 * @param ByteCount is unused by this function.
634 *****************************************************************************/
635 static void XUartPs_StubHandler(void *CallBackRef, u32 Event,
638 (void *) CallBackRef;
641 /* Assert occurs always since this is a stub and should never be called */
642 Xil_AssertVoidAlways();