1 /******************************************************************************
3 * Copyright (C) 2010 - 2014 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 /****************************************************************************/
35 * @file xuartps_options.c
36 * @addtogroup uartps_v2_1
39 * The implementation of the options functions for the XUartPs driver.
42 * MODIFICATION HISTORY:
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.
52 *****************************************************************************/
54 /***************************** Include Files ********************************/
58 /************************** Constant Definitions ****************************/
60 /**************************** Type Definitions ******************************/
62 /***************** Macros (Inline Functions) Definitions ********************/
64 /************************** Variable Definitions ****************************/
66 * The following data type is a map from an option to the offset in the
67 * register to which it belongs as well as its bit mask in that register.
76 * Create the table which contains options which are to be processed to get/set
77 * the options. These options are table driven to allow easy maintenance and
78 * expansion of the options.
81 static Mapping OptionsTable[] = {
82 {XUARTPS_OPTION_SET_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STARTBRK},
83 {XUARTPS_OPTION_STOP_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STOPBRK},
84 {XUARTPS_OPTION_RESET_TMOUT, XUARTPS_CR_OFFSET, XUARTPS_CR_TORST},
85 {XUARTPS_OPTION_RESET_TX, XUARTPS_CR_OFFSET, XUARTPS_CR_TXRST},
86 {XUARTPS_OPTION_RESET_RX, XUARTPS_CR_OFFSET, XUARTPS_CR_RXRST},
87 {XUARTPS_OPTION_ASSERT_RTS, XUARTPS_MODEMCR_OFFSET,
89 {XUARTPS_OPTION_ASSERT_DTR, XUARTPS_MODEMCR_OFFSET,
91 {XUARTPS_OPTION_SET_FCM, XUARTPS_MODEMCR_OFFSET, XUARTPS_MODEMCR_FCM}
94 /* Create a constant for the number of entries in the table */
96 #define XUARTPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(Mapping))
98 /************************** Function Prototypes *****************************/
100 /****************************************************************************/
103 * Gets the options for the specified driver instance. The options are
104 * implemented as bit masks such that multiple options may be enabled or
105 * disabled simulataneously.
107 * @param InstancePtr is a pointer to the XUartPs instance.
111 * The current options for the UART. The optionss are bit masks that are
112 * contained in the file xuartps.h and named XUARTPS_OPTION_*.
116 *****************************************************************************/
117 u16 XUartPs_GetOptions(XUartPs *InstancePtr)
124 * Assert validates the input arguments
126 Xil_AssertNonvoid(InstancePtr != NULL);
127 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
130 * Loop thru the options table to map the physical options in the
131 * registers of the UART to the logical options to be returned
133 for (Index = 0; Index < XUARTPS_NUM_OPTIONS; Index++) {
134 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
139 * If the bit in the register which correlates to the option
140 * is set, then set the corresponding bit in the options,
141 * ignoring any bits which are zero since the options variable
142 * is initialized to zero
144 if (Register & OptionsTable[Index].Mask) {
145 Options |= OptionsTable[Index].Option;
152 /****************************************************************************/
155 * Sets the options for the specified driver instance. The options are
156 * implemented as bit masks such that multiple options may be enabled or
157 * disabled simultaneously.
159 * The GetOptions function may be called to retrieve the currently enabled
160 * options. The result is ORed in the desired new settings to be enabled and
161 * ANDed with the inverse to clear the settings to be disabled. The resulting
162 * value is then used as the options for the SetOption function call.
164 * @param InstancePtr is a pointer to the XUartPs instance.
165 * @param Options contains the options to be set which are bit masks
166 * contained in the file xuartps.h and named XUARTPS_OPTION_*.
172 *****************************************************************************/
173 void XUartPs_SetOptions(XUartPs *InstancePtr, u16 Options)
179 * Assert validates the input arguments
181 Xil_AssertVoid(InstancePtr != NULL);
182 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
185 * Loop thru the options table to map the logical options to the
186 * physical options in the registers of the UART.
188 for (Index = 0; Index < XUARTPS_NUM_OPTIONS; Index++) {
191 * Read the register which contains option so that the register
192 * can be changed without destoying any other bits of the
195 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
200 * If the option is set in the input, then set the corresponding
201 * bit in the specified register, otherwise clear the bit in
204 if (Options & OptionsTable[Index].Option) {
205 Register |= OptionsTable[Index].Mask;
208 Register &= ~OptionsTable[Index].Mask;
211 /* Write the new value to the register to set the option */
212 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
213 OptionsTable[Index].RegisterOffset,
219 /****************************************************************************/
222 * This function gets the receive FIFO trigger level. The receive trigger
223 * level indicates the number of bytes in the receive FIFO that cause a receive
224 * data event (interrupt) to be generated.
226 * @param InstancePtr is a pointer to the XUartPs instance.
228 * @return The current receive FIFO trigger level. This is a value
233 *****************************************************************************/
234 u8 XUartPs_GetFifoThreshold(XUartPs *InstancePtr)
239 * Assert validates the input arguments
241 Xil_AssertNonvoid(InstancePtr != NULL);
242 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
245 * Read the value of the FIFO control register so that the threshold
246 * can be retrieved, this read takes special register processing
248 RtrigRegister = (u8) XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
249 XUARTPS_RXWM_OFFSET);
251 /* Return only the trigger level from the register value */
253 return (RtrigRegister & XUARTPS_RXWM_MASK);
256 /****************************************************************************/
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.
263 * @param InstancePtr is a pointer to the XUartPs instance.
264 * @param TriggerLevel contains the trigger level to set.
270 *****************************************************************************/
271 void XUartPs_SetFifoThreshold(XUartPs *InstancePtr, u8 TriggerLevel)
276 * Assert validates the input arguments
278 Xil_AssertVoid(InstancePtr != NULL);
279 Xil_AssertVoid(TriggerLevel <= XUARTPS_RXWM_MASK);
280 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
282 RtrigRegister = TriggerLevel & XUARTPS_RXWM_MASK;
285 * Write the new value for the FIFO control register to it such that the
286 * threshold is changed
288 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
289 XUARTPS_RXWM_OFFSET, RtrigRegister);
293 /****************************************************************************/
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
302 * @param InstancePtr is a pointer to the XUartPs instance.
306 * The modem status which are bit masks that are contained in the file
307 * xuartps.h and named XUARTPS_MODEM_*.
311 * The bit masks used for the modem status are the exact bits of the modem
312 * status register with no abstraction.
314 *****************************************************************************/
315 u16 XUartPs_GetModemStatus(XUartPs *InstancePtr)
317 u32 ModemStatusRegister;
320 * Assert validates the input arguments
322 Xil_AssertNonvoid(InstancePtr != NULL);
323 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
325 /* Read the modem status register to return
327 ModemStatusRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
328 XUARTPS_MODEMSR_OFFSET);
329 return ModemStatusRegister;
332 /****************************************************************************/
335 * This function determines if the specified UART is sending data.
337 * @param InstancePtr is a pointer to the XUartPs instance.
340 * - TRUE if the UART is sending data
341 * - FALSE if UART is not sending data
345 *****************************************************************************/
346 u32 XUartPs_IsSending(XUartPs *InstancePtr)
348 u32 ChanStatRegister;
351 * Assert validates the input arguments
353 Xil_AssertNonvoid(InstancePtr != NULL);
354 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
357 * Read the channel status register to determine if the transmitter is
360 ChanStatRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
364 * If the transmitter is active, or the TX FIFO is not empty, then indicate
365 * that the UART is still sending some data
367 return ((XUARTPS_SR_TACTIVE == (ChanStatRegister &
368 XUARTPS_SR_TACTIVE)) ||
369 (XUARTPS_SR_TXEMPTY != (ChanStatRegister &
370 XUARTPS_SR_TXEMPTY)));
373 /****************************************************************************/
376 * This function gets the operational mode of the UART. The UART can operate
377 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
380 * @param InstancePtr is a pointer to the XUartPs instance.
384 * The operational mode is specified by constants defined in xuartps.h. The
385 * constants are named XUARTPS_OPER_MODE_*
389 *****************************************************************************/
390 u8 XUartPs_GetOperMode(XUartPs *InstancePtr)
396 * Assert validates the input arguments
398 Xil_AssertNonvoid(InstancePtr != NULL);
399 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
402 * Read the Mode register.
405 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
408 ModeRegister &= XUARTPS_MR_CHMODE_MASK;
410 * Return the constant
412 switch (ModeRegister) {
413 case XUARTPS_MR_CHMODE_NORM:
414 OperMode = XUARTPS_OPER_MODE_NORMAL;
416 case XUARTPS_MR_CHMODE_ECHO:
417 OperMode = XUARTPS_OPER_MODE_AUTO_ECHO;
419 case XUARTPS_MR_CHMODE_L_LOOP:
420 OperMode = XUARTPS_OPER_MODE_LOCAL_LOOP;
422 case XUARTPS_MR_CHMODE_R_LOOP:
423 OperMode = XUARTPS_OPER_MODE_REMOTE_LOOP;
426 OperMode = (u8) ((ModeRegister & XUARTPS_MR_CHMODE_MASK) >>
427 XUARTPS_MR_CHMODE_SHIFT);
433 /****************************************************************************/
436 * This function sets the operational mode of the UART. The UART can operate
437 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
440 * @param InstancePtr is a pointer to the XUartPs instance.
441 * @param OperationMode is the mode of the UART.
447 *****************************************************************************/
448 void XUartPs_SetOperMode(XUartPs *InstancePtr, u8 OperationMode)
453 * Assert validates the input arguments.
455 Xil_AssertVoid(InstancePtr != NULL);
456 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
457 Xil_AssertVoid(OperationMode <= XUARTPS_OPER_MODE_REMOTE_LOOP);
460 * Read the Mode register.
463 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
467 * Set the correct value by masking the bits, then ORing the const.
469 ModeRegister &= ~XUARTPS_MR_CHMODE_MASK;
471 switch (OperationMode) {
472 case XUARTPS_OPER_MODE_NORMAL:
473 ModeRegister |= XUARTPS_MR_CHMODE_NORM;
475 case XUARTPS_OPER_MODE_AUTO_ECHO:
476 ModeRegister |= XUARTPS_MR_CHMODE_ECHO;
478 case XUARTPS_OPER_MODE_LOCAL_LOOP:
479 ModeRegister |= XUARTPS_MR_CHMODE_L_LOOP;
481 case XUARTPS_OPER_MODE_REMOTE_LOOP:
482 ModeRegister |= XUARTPS_MR_CHMODE_R_LOOP;
486 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
491 /****************************************************************************/
494 * This function sets the Flow Delay.
495 * 0 - 3: Flow delay inactive
496 * 4 - 32: If Flow Control mode is enabled, UART_rtsN is deactivated when the
497 * receive FIFO fills to this level.
499 * @param InstancePtr is a pointer to the XUartPs instance.
503 * The Flow Delay is specified by constants defined in xuartps_hw.h. The
504 * constants are named XUARTPS_FLOWDEL*
508 *****************************************************************************/
509 u8 XUartPs_GetFlowDelay(XUartPs *InstancePtr)
514 * Assert validates the input arguments
516 Xil_AssertNonvoid(InstancePtr != NULL);
517 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
520 * Read the Mode register.
522 FdelRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
523 XUARTPS_FLOWDEL_OFFSET);
526 * Return the contents of the flow delay register
528 return (u8) (FdelRegister & XUARTPS_FLOWDEL_MASK);
531 /****************************************************************************/
534 * This function sets the Flow Delay.
535 * 0 - 3: Flow delay inactive
536 * 4 - 63: If Flow Control mode is enabled, UART_rtsN is deactivated when the
537 * receive FIFO fills to this level.
539 * @param InstancePtr is a pointer to the XUartPs instance.
540 * @param FlowDelayValue is the Setting for the flow delay.
546 *****************************************************************************/
547 void XUartPs_SetFlowDelay(XUartPs *InstancePtr, u8 FlowDelayValue)
552 * Assert validates the input arguments
554 Xil_AssertVoid(InstancePtr != NULL);
555 Xil_AssertVoid(FlowDelayValue > XUARTPS_FLOWDEL_MASK);
556 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
559 * Set the correct value by shifting the input constant, then masking
562 FdelRegister = (FlowDelayValue & XUARTPS_FLOWDEL_MASK);
564 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
565 XUARTPS_FLOWDEL_OFFSET, FdelRegister);
569 /****************************************************************************/
572 * This function gets the Receive Timeout of the UART.
574 * @param InstancePtr is a pointer to the XUartPs instance.
576 * @return The current setting for receive time out.
580 *****************************************************************************/
581 u8 XUartPs_GetRecvTimeout(XUartPs *InstancePtr)
586 * Assert validates the input arguments
588 Xil_AssertNonvoid(InstancePtr != NULL);
589 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
592 * Read the Recieve Timeout register.
594 RtoRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
595 XUARTPS_RXTOUT_OFFSET);
598 * Return the contents of the mode register shifted appropriately
600 return (RtoRegister & XUARTPS_RXTOUT_MASK);
603 /****************************************************************************/
606 * This function sets the Receive Timeout of the UART.
608 * @param InstancePtr is a pointer to the XUartPs instance.
609 * @param RecvTimeout setting allows the UART to detect an idle connection
610 * on the reciever data line.
611 * Timeout duration = RecvTimeout x 4 x Bit Period. 0 disables the
618 *****************************************************************************/
619 void XUartPs_SetRecvTimeout(XUartPs *InstancePtr, u8 RecvTimeout)
624 * Assert validates the input arguments
626 Xil_AssertVoid(InstancePtr != NULL);
627 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
630 * Set the correct value by masking the bits
632 RtoRegister = (RecvTimeout & XUARTPS_RXTOUT_MASK);
634 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
635 XUARTPS_RXTOUT_OFFSET, RtoRegister);
638 * Configure CR to restart the receiver timeout counter
641 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
643 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_CR_OFFSET,
644 (RtoRegister | XUARTPS_CR_TORST));
647 /****************************************************************************/
650 * Sets the data format for the device. The data format includes the
651 * baud rate, number of data bits, number of stop bits, and parity. It is the
652 * caller's responsibility to ensure that the UART is not sending or receiving
653 * data when this function is called.
655 * @param InstancePtr is a pointer to the XUartPs instance.
656 * @param FormatPtr is a pointer to a format structure containing the data
660 * - XST_SUCCESS if the data format was successfully set.
661 * - XST_UART_BAUD_ERROR indicates the baud rate could not be
662 * set because of the amount of error with the baud rate and
663 * the input clock frequency.
664 * - XST_INVALID_PARAM if one of the parameters was not valid.
668 * The data types in the format type, data bits and parity, are 32 bit fields
669 * to prevent a compiler warning.
670 * The asserts in this function will cause a warning if these fields are
674 *****************************************************************************/
675 int XUartPs_SetDataFormat(XUartPs *InstancePtr,
676 XUartPsFormat * FormatPtr)
681 Xil_AssertNonvoid(InstancePtr != NULL);
682 Xil_AssertNonvoid(FormatPtr != NULL);
683 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
686 * Verify the inputs specified are valid
688 if ((FormatPtr->DataBits > XUARTPS_FORMAT_6_BITS) ||
689 (FormatPtr->StopBits > XUARTPS_FORMAT_2_STOP_BIT) ||
690 (FormatPtr->Parity > XUARTPS_FORMAT_NO_PARITY)) {
691 return XST_INVALID_PARAM;
695 * Try to set the baud rate and if it's not successful then don't
696 * continue altering the data format, this is done first to avoid the
697 * format from being altered when an error occurs
699 Status = XUartPs_SetBaudRate(InstancePtr, FormatPtr->BaudRate);
700 if (Status != XST_SUCCESS) {
705 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
709 * Set the length of data (8,7,6) by first clearing out the bits
710 * that control it in the register, then set the length in the register
712 ModeRegister &= ~XUARTPS_MR_CHARLEN_MASK;
713 ModeRegister |= (FormatPtr->DataBits << XUARTPS_MR_CHARLEN_SHIFT);
716 * Set the number of stop bits in the mode register by first clearing
717 * out the bits that control it in the register, then set the number
718 * of stop bits in the register.
720 ModeRegister &= ~XUARTPS_MR_STOPMODE_MASK;
721 ModeRegister |= (FormatPtr->StopBits << XUARTPS_MR_STOPMODE_SHIFT);
724 * Set the parity by first clearing out the bits that control it in the
725 * register, then set the bits in the register, the default is no parity
726 * after clearing the register bits
728 ModeRegister &= ~XUARTPS_MR_PARITY_MASK;
729 ModeRegister |= (FormatPtr->Parity << XUARTPS_MR_PARITY_SHIFT);
732 * Update the mode register
734 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
740 /****************************************************************************/
743 * Gets the data format for the specified UART. The data format includes the
744 * baud rate, number of data bits, number of stop bits, and parity.
746 * @param InstancePtr is a pointer to the XUartPs instance.
747 * @param FormatPtr is a pointer to a format structure that will contain
748 * the data format after this call completes.
755 *****************************************************************************/
756 void XUartPs_GetDataFormat(XUartPs *InstancePtr, XUartPsFormat * FormatPtr)
762 * Assert validates the input arguments
764 Xil_AssertVoid(InstancePtr != NULL);
765 Xil_AssertVoid(FormatPtr != NULL);
766 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
769 * Get the baud rate from the instance, this is not retrieved from the
770 * hardware because it is only kept as a divisor such that it is more
771 * difficult to get back to the baud rate
773 FormatPtr->BaudRate = InstancePtr->BaudRate;
775 ModeRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
779 * Get the length of data (8,7,6,5)
781 FormatPtr->DataBits =
782 (ModeRegister & XUARTPS_MR_CHARLEN_MASK) >>
783 XUARTPS_MR_CHARLEN_SHIFT;
786 * Get the number of stop bits
788 FormatPtr->StopBits =
789 (ModeRegister & XUARTPS_MR_STOPMODE_MASK) >>
790 XUARTPS_MR_STOPMODE_SHIFT;
793 * Determine what parity is
796 (ModeRegister & XUARTPS_MR_PARITY_MASK) >>
797 XUARTPS_MR_PARITY_SHIFT;